feat: avoid regenerate color scheme when image URL is changed

Use MD5 as key to identify images, and .Slug as id
This commit is contained in:
Jimmy Cai 2020-08-28 12:11:02 +02:00
parent ced9823d99
commit 32732d4bf1
5 changed files with 30 additions and 21 deletions

View file

@ -1,4 +1,5 @@
interface colorScheme { interface colorScheme {
key: string, /// Regenerate color scheme when the image is changed
DarkMuted: { DarkMuted: {
hex: string, hex: string,
rgb: Number[], rgb: Number[],
@ -13,20 +14,21 @@ interface colorScheme {
let colorsCache: { [key: string]: colorScheme } = {}; let colorsCache: { [key: string]: colorScheme } = {};
if (localStorage.hasOwnProperty('colorsCache')) { if (localStorage.hasOwnProperty('StackColorsCache')) {
try { try {
colorsCache = JSON.parse(localStorage.getItem('colorsCache')); colorsCache = JSON.parse(localStorage.getItem('StackColorsCache'));
} }
catch (e) { catch (e) {
colorsCache = {}; colorsCache = {};
} }
} }
async function getColor(imageURL: string) { async function getColor(id: string, key: string, imageURL: string) {
if (!colorsCache.hasOwnProperty(imageURL)) { if (!id || !colorsCache.hasOwnProperty(id) || colorsCache[id].key !== key) {
const palette = await Vibrant.from(imageURL).getPalette(); const palette = await Vibrant.from(imageURL).getPalette();
colorsCache[imageURL] = { colorsCache[id] = {
key: key,
Vibrant: { Vibrant: {
hex: palette.Vibrant.hex, hex: palette.Vibrant.hex,
rgb: palette.Vibrant.rgb, 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 { export {

View file

@ -25,9 +25,11 @@ let Stack = {
* Add color to tags * Add color to tags
*/ */
document.querySelectorAll('.color-tag').forEach(async (tag: HTMLLinkElement) => { 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.color = colors.Vibrant.bodyTextColor;
tag.style.background = colors.Vibrant.hex; tag.style.background = colors.Vibrant.hex;
@ -47,9 +49,11 @@ let Stack = {
articles.forEach(async articles => { articles.forEach(async articles => {
const image = articles.querySelector('img'), const image = articles.querySelector('img'),
imageURL = image.src, imageURL = image.src,
id = image.getAttribute('data-id'),
key = image.getAttribute('data-key'),
articleDetails: HTMLDivElement = articles.querySelector('.article-details'); articleDetails: HTMLDivElement = articles.querySelector('.article-details');
const colors = await getColor(imageURL); const colors = await getColor(id, key, imageURL);
articleDetails.style.background = ` articleDetails.style.background = `
linear-gradient(0deg, linear-gradient(0deg,

View file

@ -20,10 +20,10 @@
<header class="article-category"> <header class="article-category">
{{ range $categories }} {{ range $categories }}
{{ if $i }} {{ if $i }}
{{- $image := partial "helper/image" $context -}} {{- $image := partial "helper/image" $context | resources.Fingerprint "md5" -}}
{{- $20x := $image.Fill "20x20 smart" -}} {{- $20x := $image.Fill "20x20 smart" -}}
<a href="/categories/{{ . | urlize }}" class="color-tag" <a href="/categories/{{ . | urlize }}" class="color-tag"
data-image="{{ $20x.RelPermalink }}">{{ . | humanize }}</a> data-image="{{ $20x.RelPermalink }}" data-id="{{ $context.Slug }}" data-key="{{ $image.Data.Integrity }}">{{ . | humanize }}</a>
{{ else }} {{ else }}
<a href="/categories/{{ . | urlize }}">{{ . | humanize }}</a> <a href="/categories/{{ . | urlize }}">{{ . | humanize }}</a>
{{ end }} {{ end }}

View file

@ -1,10 +1,11 @@
<article class="{{ if .context.Params.image }}has-image{{ end }}"> <article class="{{ if .context.Params.image }}has-image{{ end }}">
<a href="{{ .context.Permalink }}"> <a href="{{ .context.Permalink }}">
{{ if .context.Params.image }} {{ if .context.Params.image }}
{{- $thumbnail := (partial "helper/image" (.context) ).Fill .size -}} {{- $image := partial "helper/image" .context | resources.Fingerprint "md5" -}}
{{- $thumbnail := $image.Fill .size -}}
<div class="article-image"> <div class="article-image">
<img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" <img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}"
loading="lazy"> loading="lazy" data-id="{{ .context.Slug }}" data-key="{{ $image.Data.Integrity }}">
</div> </div>
{{ end }} {{ end }}

View file

@ -5,9 +5,6 @@
{{- $tablet := $image.Resize "1024x" -}} {{- $tablet := $image.Resize "1024x" -}}
{{- $desktop := $image.Resize "2000x" -}} {{- $desktop := $image.Resize "2000x" -}}
{{- $20x := $image.Fill "20x20 smart" -}}
{{- .Scratch.Set "20x" $20x -}}
<div class="article-image"> <div class="article-image">
<img srcset="{{ $tablet.RelPermalink }} 1024w, {{ $desktop.RelPermalink }} 2000w" <img srcset="{{ $tablet.RelPermalink }} 1024w, {{ $desktop.RelPermalink }} 2000w"
src="{{ $desktop.RelPermalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}" src="{{ $desktop.RelPermalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}"
@ -16,13 +13,17 @@
</div> </div>
{{ end }} {{ end }}
{{ $i := .Params.image }}
{{ $context := . }}
<div class="article-details"> <div class="article-details">
{{ with $category := .Params.categories }} {{ with $categories := .Params.categories }}
<header class="article-category"> <header class="article-category">
{{ range $category }} {{ range $categories }}
{{ if $.Params.image }} {{ if $i }}
{{- $image := partial "helper/image" $context | resources.Fingerprint "md5" -}}
{{- $20x := $image.Fill "20x20 smart" -}}
<a href="/categories/{{ . | urlize }}" class="color-tag" <a href="/categories/{{ . | urlize }}" class="color-tag"
data-image="{{ ($.Scratch.Get "20x").RelPermalink }}">{{ . | humanize }}</a> data-image="{{ $20x.RelPermalink }}" data-id="{{ $context.Slug }}" data-key="{{ $image.Data.Integrity }}">{{ . | humanize }}</a>
{{ else }} {{ else }}
<a href="/categories/{{ . | urlize }}">{{ . | humanize }}</a> <a href="/categories/{{ . | urlize }}">{{ . | humanize }}</a>
{{ end }} {{ end }}