Files
lucide/site/src/components/SvgPreview/utils.ts

187 lines
5.5 KiB
TypeScript
Raw Normal View History

Added github comment icon workflow (#946) * feat: added github comment icon workflow * feat: improved segment node styling * feat: improved segment node styling * fix: fixed grid alignment issue * chore: cleanup * fix: added ref forwarding to SvgPreview component * chore: removed svg preview from icon detail overlay * chore: updated tj-actions/changed-files * chore: switched to pull_request_target * chore: simplified path segment highlighting logic * Fixes incorrect relative links in documentation pages (#973) * Fixes incorrect relative links in documentation pages * Unifies documentation page names to avoid 404 links --------- Co-authored-by: Karsa <karsa@karsa.org> * Add forklift icon (#943) * Fix vercel build * Add forklift icon Co-Authored-By: willythewizard <119956499+willythewizard@users.noreply.github.com> --------- Co-authored-by: willythewizard <119956499+willythewizard@users.noreply.github.com> * adds utility-pole icon (#971) Co-authored-by: Karsa <karsa@karsa.org> * :package: Bump lucide package versions to 0.123.0 * Adds `nfc` icons (#960) * added nfc icons * fixes smartphone-nfc * Update icons/nfc.svg Co-authored-by: Jakob Guddas <github@jguddas.de> * Update icons/smartphone-nfc.svg Co-authored-by: Jakob Guddas <github@jguddas.de> --------- Co-authored-by: Karsa <karsa@karsa.org> Co-authored-by: Jakob Guddas <github@jguddas.de> * :package: Bump lucide package versions to 0.124.0 * fix: updated pnpm-lock.yaml * fix: added missing api endpoint file * chore: fixed nextjs path name parsing in production * chore: only run workflow when path includes icons/*.svg * chore: added Cache-Control header to gh-icon api route response * feat: added dark mode support to gh-icon * feat: switched to using picture tag for gh-icon * feat: added space between gh-icons in pr comment * fix: changed icon size base back to 24x24 * feat: added title to gh-icon comment image * fix: changed gh-icon url * chore: added groups with class names * feat: improved shadow masking * Removes need for building duplicate icons by supporting CSS based dark mode * chore: resolved type issues * feat: changed image width from 48% to 400px --------- Co-authored-by: Karsa <contact@karsa.org> Co-authored-by: Karsa <karsa@karsa.org> Co-authored-by: Eric Fennis <eric.fennis@gmail.com> Co-authored-by: willythewizard <119956499+willythewizard@users.noreply.github.com> Co-authored-by: Lucide Bot <lucide-bot@users.noreply.github.com>
2023-04-03 21:05:59 +02:00
import { INode, parseSync } from 'svgson';
import toPath from 'element-to-path';
import { SVGPathData, encodeSVGPath } from 'svg-pathdata';
import { Path, Point } from './types';
function assertNever(x: never): never {
throw new Error('Unknown type: ' + x['type']);
}
export function assert(value: unknown): asserts value {
if (value === undefined) {
throw new Error('value must be defined');
}
}
const extractPaths = (node: INode): { d: string; name: typeof node.name }[] => {
if (/(rect|circle|ellipse|polygon|polyline|line|path)/.test(node.name)) {
return [{ d: toPath(node), name: node.name }];
} else if (node.children && Array.isArray(node.children)) {
return node.children.flatMap(extractPaths);
}
return [];
};
export const getCommands = (src: string) =>
extractPaths(parseSync(src)).flatMap(({ d, name }, idx) =>
new SVGPathData(d).toAbs().commands.map((c) => ({ ...c, id: idx, name }))
);
export const getPaths = (src: string) => {
const commands = getCommands(src.includes('<svg') ? src : `<svg>${src}</svg>`);
Added github comment icon workflow (#946) * feat: added github comment icon workflow * feat: improved segment node styling * feat: improved segment node styling * fix: fixed grid alignment issue * chore: cleanup * fix: added ref forwarding to SvgPreview component * chore: removed svg preview from icon detail overlay * chore: updated tj-actions/changed-files * chore: switched to pull_request_target * chore: simplified path segment highlighting logic * Fixes incorrect relative links in documentation pages (#973) * Fixes incorrect relative links in documentation pages * Unifies documentation page names to avoid 404 links --------- Co-authored-by: Karsa <karsa@karsa.org> * Add forklift icon (#943) * Fix vercel build * Add forklift icon Co-Authored-By: willythewizard <119956499+willythewizard@users.noreply.github.com> --------- Co-authored-by: willythewizard <119956499+willythewizard@users.noreply.github.com> * adds utility-pole icon (#971) Co-authored-by: Karsa <karsa@karsa.org> * :package: Bump lucide package versions to 0.123.0 * Adds `nfc` icons (#960) * added nfc icons * fixes smartphone-nfc * Update icons/nfc.svg Co-authored-by: Jakob Guddas <github@jguddas.de> * Update icons/smartphone-nfc.svg Co-authored-by: Jakob Guddas <github@jguddas.de> --------- Co-authored-by: Karsa <karsa@karsa.org> Co-authored-by: Jakob Guddas <github@jguddas.de> * :package: Bump lucide package versions to 0.124.0 * fix: updated pnpm-lock.yaml * fix: added missing api endpoint file * chore: fixed nextjs path name parsing in production * chore: only run workflow when path includes icons/*.svg * chore: added Cache-Control header to gh-icon api route response * feat: added dark mode support to gh-icon * feat: switched to using picture tag for gh-icon * feat: added space between gh-icons in pr comment * fix: changed icon size base back to 24x24 * feat: added title to gh-icon comment image * fix: changed gh-icon url * chore: added groups with class names * feat: improved shadow masking * Removes need for building duplicate icons by supporting CSS based dark mode * chore: resolved type issues * feat: changed image width from 48% to 400px --------- Co-authored-by: Karsa <contact@karsa.org> Co-authored-by: Karsa <karsa@karsa.org> Co-authored-by: Eric Fennis <eric.fennis@gmail.com> Co-authored-by: willythewizard <119956499+willythewizard@users.noreply.github.com> Co-authored-by: Lucide Bot <lucide-bot@users.noreply.github.com>
2023-04-03 21:05:59 +02:00
const paths: Path[] = [];
let prev: Point | undefined = undefined;
let start: Point | undefined = undefined;
const addPath = (c: (typeof commands)[number], next: Point, d?: string) => {
assert(prev);
paths.push({
c,
d: d || `M${prev.x} ${prev.y}L${next.x} ${next.y}`,
prev,
next,
isStart: start === prev,
});
prev = next;
};
let prevCP: Point | undefined = undefined;
for (let i = 0; i < commands.length; i++) {
const previousCommand = commands[i - 1];
const c = commands[i];
switch (c.type) {
case SVGPathData.MOVE_TO: {
prev = c;
start = c;
break;
}
case SVGPathData.LINE_TO: {
assert(prev);
addPath(c, c);
break;
}
case SVGPathData.HORIZ_LINE_TO: {
assert(prev);
addPath(c, { x: c.x, y: prev.y });
break;
}
case SVGPathData.VERT_LINE_TO: {
assert(prev);
addPath(c, { x: prev.x, y: c.y });
break;
}
case SVGPathData.CLOSE_PATH: {
assert(prev);
assert(start);
addPath(c, start);
start = undefined;
break;
}
case SVGPathData.CURVE_TO: {
assert(prev);
addPath(c, c, `M ${prev.x},${prev.y} ${encodeSVGPath(c)}`);
break;
}
case SVGPathData.SMOOTH_CURVE_TO: {
assert(prev);
assert(previousCommand);
const reflectedCp1 = {
x:
previousCommand &&
(previousCommand.type === SVGPathData.SMOOTH_CURVE_TO ||
previousCommand.type === SVGPathData.CURVE_TO)
? previousCommand.relative
? previousCommand.x2 - previousCommand.x
: previousCommand.x2 - prev.x
: 0,
y:
previousCommand &&
(previousCommand.type === SVGPathData.SMOOTH_CURVE_TO ||
previousCommand.type === SVGPathData.CURVE_TO)
? previousCommand.relative
? previousCommand.y2 - previousCommand.y
: previousCommand.y2 - prev.y
: 0,
};
addPath(
c,
c,
`M ${prev.x},${prev.y} ${encodeSVGPath({
type: SVGPathData.CURVE_TO,
relative: false,
x: c.x,
y: c.y,
x1: prev.x - reflectedCp1.x,
y1: prev.y - reflectedCp1.y,
x2: c.x2,
y2: c.y2,
})}`
);
break;
}
case SVGPathData.QUAD_TO: {
assert(prev);
addPath(c, c, `M ${prev.x},${prev.y} ${encodeSVGPath(c)}`);
break;
}
case SVGPathData.SMOOTH_QUAD_TO: {
assert(prev);
const backTrackCP = (
index: number,
currentPoint: { x: number; y: number }
): { x: number; y: number } => {
const previousCommand = commands[index - 1];
if (!previousCommand) {
return currentPoint;
}
if (previousCommand.type === SVGPathData.QUAD_TO) {
return {
x: previousCommand.relative
? currentPoint.x - (previousCommand.x1 - previousCommand.x)
: currentPoint.x - (previousCommand.x1 - currentPoint.x),
y: previousCommand.relative
? currentPoint.y - (previousCommand.y1 - previousCommand.y)
: currentPoint.y - (previousCommand.y1 - currentPoint.y),
};
}
if (previousCommand.type === SVGPathData.SMOOTH_QUAD_TO) {
if (!prevCP) {
return currentPoint;
}
return {
x: currentPoint.x - (prevCP.x - currentPoint.x),
y: currentPoint.y - (prevCP.y - currentPoint.y),
};
}
return currentPoint;
};
prevCP = backTrackCP(i, prev);
addPath(
c,
c,
`M ${prev.x},${prev.y} ${encodeSVGPath({
type: SVGPathData.QUAD_TO,
relative: false,
x: c.x,
y: c.y,
x1: prevCP.x,
y1: prevCP.y,
})}`
);
break;
}
case SVGPathData.ARC: {
assert(prev);
addPath(
c,
c,
`M ${prev.x},${prev.y} A ${c.rX} ${c.rY} ${c.xRot} ${c.lArcFlag} ${c.sweepFlag} ${c.x} ${c.y}`
);
break;
}
default: {
assertNever(c);
}
}
}
return paths;
};