Skip to content

Commit

Permalink
add shareable links (#1252)
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke authored Jan 18, 2024
1 parent c8197c0 commit 6dfea47
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,34 @@ function handleSliderInput() {
debouncedReenable();
}

function readState() {
try {
const hash = window.location.hash.slice(1);
if (!hash) {
return {};
}

return JSON.parse(window.decodeURIComponent(window.atob(hash)));
} catch (err) {
console.error(err);
return {};
}
}

function writeState() {
try {
window.location.hash = window.btoa(window.encodeURIComponent(JSON.stringify({
cs: colorSpaceInput.value,
im: interpolationMethodInput.value,
p: colorMixPercentage.value,
a: colorInputA.value,
b: colorInputB.value,
})));
} catch (err) {
console.error(err);
}
}

function renderResult() {
if (
!colorSpaceInput ||
Expand All @@ -55,6 +83,8 @@ function renderResult() {
return;
}

writeState();

switch (colorSpaceInput.value) {
case 'hsl':
case 'hwb':
Expand Down Expand Up @@ -108,3 +138,24 @@ colorMixPercentage.addEventListener('input', handleSliderInput);

addEventListener('change', renderResult);
addEventListener('keyup', renderResult);

{
const state = readState();
if (state.cs) {
colorSpaceInput.value = state.cs;
}
if (state.im) {
interpolationMethodInput.value = state.im;
}
if (state.p) {
colorMixPercentage.value = state.p;
}
if (state.a) {
colorInputA.value = state.a;
}
if (state.b) {
colorInputB.value = state.b;
}

renderResult();
}

0 comments on commit 6dfea47

Please sign in to comment.