Skip to content

Commit 6dad9d0

Browse files
authored
feat: add url params (#565)
1 parent cd53df4 commit 6dad9d0

15 files changed

+716
-413
lines changed

src/eggy-js.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module '@s-r0/eggy-js';

src/lib/packages/helpers.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
// Includes all functions that the utils functions use
22

3-
import copy from 'copy-to-clipboard';
4-
5-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
6-
// @ts-ignore
7-
import {Eggy} from '@s-r0/eggy-js';
83
import {
94
getColumnGap,
105
getNumberOfColumns,
116
getNumberOfRows,
127
getRowGap,
138
} from '../getElements';
149

10+
import {Eggy} from '@s-r0/eggy-js';
11+
import copy from 'copy-to-clipboard';
12+
13+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
14+
// @ts-ignore
15+
1516
export function createDownloadLink(fileName: string, url: string) {
1617
const link = document.createElement('a');
1718
link.download = fileName;
@@ -200,6 +201,25 @@ export const actOnGenerator = (
200201
}
201202
};
202203

204+
export function getQueryParam(param: string): string | null {
205+
const urlParams = new URLSearchParams(window.location.search);
206+
return urlParams.get(param);
207+
}
208+
209+
export function setQueryParam(param: string, value: string): void {
210+
const url = new URL(window.location.toString());
211+
const urlParams = url.searchParams;
212+
urlParams.set(param, value);
213+
window.history.replaceState({}, '', url.toString());
214+
}
215+
216+
export function deleteQueryParam(param: string): void {
217+
const url = new URL(window.location.toString());
218+
const urlParams = url.searchParams;
219+
urlParams.delete(param);
220+
window.history.replaceState({}, '', url.toString());
221+
}
222+
203223
function convertLinearGradientToTailwind(gradient: string): string {
204224
const angle = extractDegreeFromGradient(gradient);
205225
const direction = getTailwindDirectionClass(angle);

src/lib/packages/utils.ts

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
import DomToImage from 'dom-to-image';
2-
3-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
4-
// @ts-ignore
5-
import {Eggy} from '@s-r0/eggy-js';
61
import {
2+
actOnGenerator,
3+
actOnTailwindGenerator,
4+
createDownloadLink,
5+
setQueryParam,
6+
} from './helpers';
7+
import {
8+
getAllColorInput,
9+
getAllInputElements,
10+
getDegreeSpanElement,
711
getGradientPreview,
812
getNewColorButton,
913
getParentElementOfColors,
14+
getRange,
1015
getRemoveNewColorButton,
11-
getAllColorInput,
16+
getResetButton,
1217
} from '../getElements';
1318

14-
import {
15-
createDownloadLink,
16-
actOnGenerator,
17-
actOnTailwindGenerator,
18-
} from './helpers';
19+
import DomToImage from 'dom-to-image';
20+
import {Eggy} from '@s-r0/eggy-js';
21+
22+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
23+
// @ts-ignore
1924

2025
export const setGradientDegreeValue = (degreeElement: HTMLElement): void =>
2126
degreeElement.addEventListener('input', (e) => {
@@ -54,6 +59,11 @@ export const createGradientPreview = (
5459
).style.background = `linear-gradient(${fill}deg, ${getColorsValue(
5560
attribute
5661
).join(', ')})`;
62+
63+
setQueryParam(
64+
'values',
65+
`linear-gradient(${fill}deg, ${getColorsValue(attribute).join(', ')})`
66+
);
5767
};
5868

5969
/**
@@ -192,13 +202,23 @@ export function getColorsValue(attribute: string): Array<string> {
192202
return colorValues;
193203
}
194204

205+
export function setColorsValue(colors: string[] | null, attribute: string) {
206+
if (!colors) return;
207+
const colorInputs = getAllColorInput(attribute);
208+
209+
colorInputs.forEach(
210+
(colorInput, index) => (colorInput.value = colors[index])
211+
);
212+
}
213+
195214
export function removeColorPicker(attribute: string): void {
196215
const getParentElementToRemoveFrom = getParentElementOfColors(attribute);
197216
const numberOfChildren = getParentElementToRemoveFrom?.childElementCount;
198217

199218
if (numberOfChildren === undefined || numberOfChildren === 2) return;
200219
getParentElementToRemoveFrom?.lastChild?.remove();
201220

221+
createGradientPreview(getRange(attribute), attribute);
202222
whatColorButtonShouldShow(attribute);
203223
}
204224

@@ -233,3 +253,55 @@ export function closeDropdown(
233253
}
234254
});
235255
}
256+
257+
export function inputEventListner(attribute: string) {
258+
const gradientInputs = getAllInputElements(attribute);
259+
260+
gradientInputs.forEach((inputElement) => {
261+
inputElement.addEventListener('input', () => {
262+
createGradientPreview(getRange(attribute), attribute);
263+
});
264+
});
265+
}
266+
267+
export function addEventListenerToTheNewColorPicker(attribute: string) {
268+
const resetButton = getResetButton(attribute);
269+
270+
inputEventListner(attribute);
271+
if (resetButton.classList.contains('reset-show')) return;
272+
resetButton.classList.add('reset-show');
273+
}
274+
275+
export function applyGradientValues(values: string, attribute: string) {
276+
const colors = values.match(/#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b/g) as
277+
| string[]
278+
| null;
279+
const degreeValueReg = values.match(/linear-gradient\((\d+)deg/) as
280+
| string[]
281+
| null;
282+
283+
if (degreeValueReg) {
284+
const degreeValue = degreeValueReg[1];
285+
const degreeSpanElement = getDegreeSpanElement(attribute);
286+
const rangeElement = getRange(attribute);
287+
288+
degreeSpanElement.innerHTML = `${degreeValue}deg`;
289+
rangeElement.value = degreeValue;
290+
}
291+
292+
if (colors) {
293+
const colorLength = colors.length;
294+
295+
if (colorLength > 2) {
296+
for (let index = 2; index < colorLength; index++) {
297+
addNewColorPicker(attribute);
298+
addEventListenerToTheNewColorPicker(attribute);
299+
}
300+
whatColorButtonShouldShow(attribute);
301+
}
302+
303+
setColorsValue(colors, attribute);
304+
}
305+
306+
createGradientPreview(getRange(attribute), attribute);
307+
}

src/main.ts

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
import {
33
addAnimationListener,
44
animationGenerator,
5+
applyAnimationValues,
56
displayAnimationPreview,
67
} from './pages/animation';
78
import {
89
addBorderRadiusListener,
10+
applyBorderRadiusValues,
911
borderRadiusGenerator,
1012
} from './pages/border-radius';
11-
import {addBoxShadowListener, boxShadowGenerator} from './pages/box-shadow';
13+
import {
14+
addBoxShadowListener,
15+
applyBoxShadowValues,
16+
boxShadowGenerator,
17+
} from './pages/box-shadow';
1218
import {
1319
addGradientBackgroundListener,
1420
gradientBackgroundGenerator,
@@ -21,10 +27,14 @@ import {
2127
addGradientTextListener,
2228
gradientTextGenerator,
2329
} from './pages/gradient-text';
24-
import {rangeGenerator} from './pages/input-range';
30+
import {applyInputRangeValues, rangeGenerator} from './pages/input-range';
2531
import {picTextGenerator} from './pages/pic-text';
26-
import {addTextShadowListener, textShadowGenerator} from './pages/text-shadow';
27-
import {gridGenerator} from './pages/grid-generator';
32+
import {
33+
addTextShadowListener,
34+
applyTextShadowValues,
35+
textShadowGenerator,
36+
} from './pages/text-shadow';
37+
import {applyGridValues, gridGenerator} from './pages/grid-generator';
2838

2939
// Packages
3040
import * as FilePond from 'filepond';
@@ -44,8 +54,18 @@ import {
4454
getRange,
4555
getResultPage,
4656
} from './lib/getElements';
47-
import {addTransformListener, transformGenerator} from './pages/transform';
57+
import {
58+
addTransformListener,
59+
applyTransformValue,
60+
transformGenerator,
61+
} from './pages/transform';
4862
import {scrollGenerator} from './pages/scroll';
63+
import {
64+
deleteQueryParam,
65+
getQueryParam,
66+
setQueryParam,
67+
} from './lib/packages/helpers';
68+
import {applyGradientValues} from './lib/packages/utils';
4969

5070
FilePond.registerPlugin(
5171
FilePondPluginImagePreview,
@@ -204,6 +224,31 @@ FilePond.create(getImageEntryElement, {
204224
},
205225
});
206226

227+
const generatorParam = getQueryParam('generator');
228+
const valuesParam = getQueryParam('values');
229+
230+
if (generatorParam) {
231+
showContent(generatorParam);
232+
233+
if (valuesParam) {
234+
generatorParam === 'animation' &&
235+
applyAnimationValues(JSON.parse(valuesParam));
236+
generatorParam === 'border-radius' &&
237+
applyBorderRadiusValues(JSON.parse(valuesParam));
238+
generatorParam === 'box-shadow' && applyBoxShadowValues(valuesParam);
239+
generatorParam === 'gradient-background' &&
240+
applyGradientValues(valuesParam, 'gradient-background');
241+
generatorParam === 'gradient-border' &&
242+
applyGradientValues(valuesParam, 'gradient-border');
243+
generatorParam === 'gradient-text' &&
244+
applyGradientValues(valuesParam, 'gradient-text');
245+
generatorParam === 'grid-generators' && applyGridValues(valuesParam);
246+
generatorParam === 'input-range' && applyInputRangeValues(valuesParam);
247+
generatorParam === 'text-shadow' && applyTextShadowValues(valuesParam);
248+
generatorParam === 'transform' && applyTransformValue(valuesParam);
249+
}
250+
}
251+
207252
/**
208253
* sets which generator to call
209254
*
@@ -229,7 +274,7 @@ function generatorsFunction(attribute: string, type: openResults): void {
229274
* @param attribute The attribute name of the generator element
230275
* @param display display type
231276
*/
232-
function showContent(attribute: string, display: Display): void {
277+
function showInputSection(attribute: string, display: Display): void {
233278
const generatorsContent = document.querySelectorAll(`[data-content]`);
234279
const showGen = document.querySelector(
235280
`[data-content=${attribute}]`
@@ -312,6 +357,19 @@ function showOpenPreviousResultText() {
312357
getOpenPreviousResult.style.animationFillMode = 'backwards';
313358
}
314359

360+
function showContent(generatorName: string) {
361+
!navBar?.classList.contains('closed-nav') &&
362+
openOrCloseNavigationBar('close');
363+
364+
sidebar.style.display = 'none';
365+
366+
if (getHomePage && getGeneratorSection) {
367+
getHomePage.style.display = 'none';
368+
getGeneratorSection.style.display = 'flex';
369+
showInputSection(generatorName, 'flex');
370+
}
371+
}
372+
315373
// clicking outside the nav bar should close the nav bar
316374
document.addEventListener('click', (e: Event) => {
317375
const event = e.target as HTMLElement;
@@ -364,6 +422,9 @@ getHeaderText?.addEventListener('click', () => {
364422
});
365423
getHomePage.style.display = 'flex';
366424
getGeneratorSection.style.display = 'none';
425+
426+
deleteQueryParam('generator');
427+
deleteQueryParam('values');
367428
});
368429

369430
// clicking on the get result icon should show the old results
@@ -396,24 +457,15 @@ getDegreeElement?.addEventListener('change', () => displayAnimationPreview());
396457
// adds event listner for which generator should show
397458
generators.forEach((generator) => {
398459
generator?.addEventListener('click', (): void => {
399-
const checking = generator.getAttribute('data-gen');
460+
const generatorName = generator.getAttribute('data-gen');
400461
openSidePanelButton.style.display = 'none';
401462

402-
if (
403-
checking === null ||
404-
getHomePage === null ||
405-
getGeneratorSection === null
406-
)
407-
return;
463+
if (generatorName === null) return;
408464

409-
!navBar?.classList.contains('closed-nav') &&
410-
openOrCloseNavigationBar('close');
465+
showContent(generatorName);
411466

412-
sidebar.style.display = 'none';
413-
attributeValue = checking;
414-
getHomePage.style.display = 'none';
415-
getGeneratorSection.style.display = 'flex';
416-
showContent(attributeValue, 'flex');
467+
deleteQueryParam('values');
468+
setQueryParam('generator', generatorName);
417469
});
418470
});
419471

0 commit comments

Comments
 (0)