From 32732d4bf1ba8255243810a9349e45f5cfd631c9 Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Fri, 28 Aug 2020 12:11:02 +0200 Subject: [PATCH] feat: avoid regenerate color scheme when image URL is changed Use MD5 as key to identify images, and .Slug as id --- assets/ts/color.ts | 17 ++++++++++------- assets/ts/main.ts | 10 +++++++--- layouts/partials/article-list/default.html | 4 ++-- layouts/partials/article-list/tile.html | 5 +++-- layouts/partials/article/components/header.html | 15 ++++++++------- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/assets/ts/color.ts b/assets/ts/color.ts index 938bc38..d46ef16 100644 --- a/assets/ts/color.ts +++ b/assets/ts/color.ts @@ -1,4 +1,5 @@ interface colorScheme { + key: string, /// Regenerate color scheme when the image is changed DarkMuted: { hex: string, rgb: Number[], @@ -13,20 +14,21 @@ interface colorScheme { let colorsCache: { [key: string]: colorScheme } = {}; -if (localStorage.hasOwnProperty('colorsCache')) { +if (localStorage.hasOwnProperty('StackColorsCache')) { try { - colorsCache = JSON.parse(localStorage.getItem('colorsCache')); + colorsCache = JSON.parse(localStorage.getItem('StackColorsCache')); } catch (e) { colorsCache = {}; } } -async function getColor(imageURL: string) { - if (!colorsCache.hasOwnProperty(imageURL)) { +async function getColor(id: string, key: string, imageURL: string) { + if (!id || !colorsCache.hasOwnProperty(id) || colorsCache[id].key !== key) { const palette = await Vibrant.from(imageURL).getPalette(); - colorsCache[imageURL] = { + colorsCache[id] = { + key: key, Vibrant: { hex: palette.Vibrant.hex, rgb: palette.Vibrant.rgb, @@ -39,9 +41,10 @@ async function getColor(imageURL: string) { } } - localStorage.setItem('colorsCache', JSON.stringify(colorsCache)); + localStorage.setItem('StackColorsCache', JSON.stringify(colorsCache)); } - return colorsCache[imageURL]; + + return colorsCache[id]; } export { diff --git a/assets/ts/main.ts b/assets/ts/main.ts index 07c0413..24acf7c 100644 --- a/assets/ts/main.ts +++ b/assets/ts/main.ts @@ -25,9 +25,11 @@ let Stack = { * Add color to tags */ document.querySelectorAll('.color-tag').forEach(async (tag: HTMLLinkElement) => { - const imageURL = tag.getAttribute('data-image'); + const imageURL = tag.getAttribute('data-image'), + id = tag.getAttribute('data-id'), + key = tag.getAttribute('data-key'); - const colors = await getColor(imageURL); + const colors = await getColor(id, key, imageURL); tag.style.color = colors.Vibrant.bodyTextColor; tag.style.background = colors.Vibrant.hex; @@ -47,9 +49,11 @@ let Stack = { articles.forEach(async articles => { const image = articles.querySelector('img'), imageURL = image.src, + id = image.getAttribute('data-id'), + key = image.getAttribute('data-key'), articleDetails: HTMLDivElement = articles.querySelector('.article-details'); - const colors = await getColor(imageURL); + const colors = await getColor(id, key, imageURL); articleDetails.style.background = ` linear-gradient(0deg, diff --git a/layouts/partials/article-list/default.html b/layouts/partials/article-list/default.html index 4b9451b..84fc93b 100644 --- a/layouts/partials/article-list/default.html +++ b/layouts/partials/article-list/default.html @@ -20,10 +20,10 @@
{{ range $categories }} {{ if $i }} - {{- $image := partial "helper/image" $context -}} + {{- $image := partial "helper/image" $context | resources.Fingerprint "md5" -}} {{- $20x := $image.Fill "20x20 smart" -}} {{ . | humanize }} + data-image="{{ $20x.RelPermalink }}" data-id="{{ $context.Slug }}" data-key="{{ $image.Data.Integrity }}">{{ . | humanize }} {{ else }} {{ . | humanize }} {{ end }} diff --git a/layouts/partials/article-list/tile.html b/layouts/partials/article-list/tile.html index 1602651..3905b3d 100644 --- a/layouts/partials/article-list/tile.html +++ b/layouts/partials/article-list/tile.html @@ -1,10 +1,11 @@
{{ if .context.Params.image }} - {{- $thumbnail := (partial "helper/image" (.context) ).Fill .size -}} + {{- $image := partial "helper/image" .context | resources.Fingerprint "md5" -}} + {{- $thumbnail := $image.Fill .size -}}
+ loading="lazy" data-id="{{ .context.Slug }}" data-key="{{ $image.Data.Integrity }}">
{{ end }} diff --git a/layouts/partials/article/components/header.html b/layouts/partials/article/components/header.html index df9e374..632be2f 100644 --- a/layouts/partials/article/components/header.html +++ b/layouts/partials/article/components/header.html @@ -5,9 +5,6 @@ {{- $tablet := $image.Resize "1024x" -}} {{- $desktop := $image.Resize "2000x" -}} - {{- $20x := $image.Fill "20x20 smart" -}} - {{- .Scratch.Set "20x" $20x -}} -
{{ end }} + {{ $i := .Params.image }} + {{ $context := . }}
- {{ with $category := .Params.categories }} + {{ with $categories := .Params.categories }}