All files / components/Image Image.svelte

91.97% Statements 149/162
68.75% Branches 22/32
33.33% Functions 1/3
91.97% Lines 149/162

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 1631x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x  
<script lang="ts">
  import "./Image.scss";
  import { browser } from "$app/environment";
  import { intersectionObserverSupport, lazyImageLoadingSupport } from "$lib/constants/support";
  import classNames from "$lib/util/classNames";
  import IntersectionObserver from "$lib/components/IntersectionObserver";
  import warn from "$lib/util/warn";
  import type { Loading, LazyLoading, Color, Sources, Srcset } from "./types";
 
  export let height: undefined | number = undefined;
  export let width: undefined | number = undefined;
 
  export let src: string;
 
  // Tuple of [src, width]
  const getSrcsetAttr = ([defaultSrc, ...widths]: Srcset) =>
    [
      ...(widths ?? []).flatMap(([source, sourceWidth]) =>
        !width || width >= sourceWidth ? [`${source} ${sourceWidth}w`] : []
      ),
      defaultSrc,
    ].join(", ");
 
  export let sources: Sources = [];
 
  export let alt: string;
  export let loading: Loading = "lazy";
  export let blurhash: undefined | string = undefined;
  export let mean: undefined | Color = undefined;
  let className: string | undefined = undefined;
  export { className as class };
  export let imageClass: string | undefined = undefined;
 
  const getLazyLoadingType = (
    loading: Loading,
    lazyImageLoadingSupport: boolean,
    intersectionObserverSupport: boolean,
    explicitLazyLoadingType?: LazyLoading
  ): LazyLoading => {
    if (explicitLazyLoadingType) return explicitLazyLoadingType;
    if (loading !== "lazy") return "none";
    if (lazyImageLoadingSupport) return "native";
    if (intersectionObserverSupport) return "intersectionObserver";
    return "none";
  };
 
  // Used to set the lazy loading type explicitly in Storybook
  let explicitLazyLoadingType: LazyLoading | undefined = undefined;
  export { explicitLazyLoadingType as lazyLoadingType };
 
  let lazyLoadingType: LazyLoading;
  $: lazyLoadingType = getLazyLoadingType(
    loading,
    lazyImageLoadingSupport,
    intersectionObserverSupport,
    explicitLazyLoadingType
  );
 
  if (!width || !height) warn("image width or height was missing!");
 
  $: decoding = loading === "lazy" ? ("async" as const) : ("auto" as const);
 
  const canvasSize = 32;
 
  let thisBg: HTMLCanvasElement;
  let thisContainer: HTMLDivElement;
 
  let imageLoaded = false;
  $: if (!src) imageLoaded = false;
 
  let intersecting = false;
 
  const withNoSrcProp = {};
  let srcProps = withNoSrcProp;
  $: withSrcProp = { src };
  $: if (!src) {
    srcProps = withNoSrcProp;
  } else if (loading === "eager") {
    srcProps = withSrcProp;
  } else if (!browser) {
    srcProps = withNoSrcProp;
  } else if (lazyLoadingType === "native" || lazyLoadingType === "none" || intersecting) {
    srcProps = withSrcProp;
  } else {
    srcProps = withNoSrcProp;
  }
 
  $: imageLoadClass = imageLoaded
    ? "ldaf-img__loaded"
    : src
    ? "ldaf-img__loading"
    : "ldaf-img__unloaded";
 
  // This theoretically shouldn't be needed since the BlurhashRenderer script will have already run and
  // drawn the blurhash before Svelte has mounted and this runs. Unfortunately, when Svelte first
  // runs the component code it does something (not sure what) that clears the canvas, which
  // flashes the average-color background until the image loads. This line fixes
  // that. Unfortunately this does not solve the problem of drawing the blurhashes when the
  // BlurhashRenderer has been omitted, since this relies on the window global that is set by that
  // script.
  $: if (blurhash && thisBg && window.drawBlurhash) {
    window.drawBlurhash(thisBg, blurhash);
  }
 
  const imgProps = { class: imageClass, width, height, border: 0, ...$$restProps };
</script>
 
<IntersectionObserver
  target={thisContainer}
  once={true}
  on:intersect={() => (intersecting = true)}
  enabled={lazyLoadingType === "intersectionObserver"}
>
  <div
    role="img"
    aria-label={alt}
    class={classNames(
      "ldaf-img",
      "ldaf-img__container",
      loading === "eager" && "ldaf-img__eager",
      className
    )}
    bind:this={thisContainer}
  >
    {#if loading === "lazy"}
      <noscript>
        <img {...imgProps} class="ldaf-img__backup-img" {src} alt="" />
      </noscript>
    {/if}
    <picture>
      {#each sources as { media, type, srcset }}
        <source {media} {type} srcset={getSrcsetAttr(srcset)} />
      {/each}
      <img
        {...imgProps}
        alt=""
        class={classNames("ldaf-img__img", imageLoadClass, imageClass)}
        on:load={() => (imageLoaded = true)}
        {loading}
        {decoding}
        {...srcProps}
      />
    </picture>
    {#if blurhash}
      <canvas
        class="ldaf-img__blur-bg"
        width={canvasSize}
        height={canvasSize}
        data-blurhash={blurhash}
        bind:this={thisBg}
      />
    {/if}
    {#if mean}
      <div
        class="ldaf-img__color-bg"
        style={`background-color: rgb(${Math.round(mean.r)}, ${Math.round(mean.g)}, ${Math.round(
          mean.b
        )});`}
      />
    {/if}
  </div>
</IntersectionObserver>