2021-02-15 14:56:02 +00:00
|
|
|
import React from "react";
|
|
|
|
import { SVGProps } from "react";
|
|
|
|
import { BBox } from "../vendor/bezier-js";
|
|
|
|
import { FILL_REPLACEMENT_COLOR, STROKE_REPLACEMENT_COLOR } from "./colors";
|
|
|
|
import { Specs } from "./specs";
|
2021-02-27 02:30:38 +00:00
|
|
|
import type { SvgSymbolMetadata } from "./svg-symbol-metadata";
|
2021-02-15 14:56:02 +00:00
|
|
|
import { VisibleSpecs } from "./visible-specs";
|
|
|
|
|
2021-02-17 12:29:56 +00:00
|
|
|
const DEFAULT_UNIFORM_STROKE_WIDTH = 1;
|
|
|
|
|
2021-02-15 14:56:02 +00:00
|
|
|
export type SvgSymbolData = {
|
|
|
|
name: string;
|
|
|
|
bbox: BBox;
|
|
|
|
layers: SvgSymbolElement[];
|
2021-02-27 02:30:38 +00:00
|
|
|
meta?: SvgSymbolMetadata;
|
2021-02-15 14:56:02 +00:00
|
|
|
specs?: Specs;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SvgSymbolElement = (
|
|
|
|
| {
|
|
|
|
tagName: "g";
|
|
|
|
props: SVGProps<SVGGElement>;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
tagName: "path";
|
|
|
|
props: SVGProps<SVGPathElement>;
|
|
|
|
}
|
|
|
|
) & {
|
|
|
|
children: SvgSymbolElement[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SvgSymbolContext = {
|
|
|
|
stroke: string;
|
|
|
|
fill: string;
|
|
|
|
showSpecs: boolean;
|
2021-02-17 12:29:56 +00:00
|
|
|
uniformStrokeWidth?: number;
|
2021-02-15 14:56:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const DEFAULT_CONTEXT: SvgSymbolContext = {
|
|
|
|
stroke: "#000000",
|
|
|
|
fill: "#ffffff",
|
|
|
|
showSpecs: false,
|
2021-02-17 12:29:56 +00:00
|
|
|
uniformStrokeWidth: DEFAULT_UNIFORM_STROKE_WIDTH,
|
2021-02-15 14:56:02 +00:00
|
|
|
};
|
|
|
|
|
2021-03-06 00:38:25 +00:00
|
|
|
export function swapColors<T extends SvgSymbolContext>(ctx: T): T {
|
|
|
|
return {
|
|
|
|
...ctx,
|
|
|
|
fill: ctx.stroke,
|
|
|
|
stroke: ctx.fill,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-15 14:56:02 +00:00
|
|
|
export function createSvgSymbolContext(
|
|
|
|
ctx: Partial<SvgSymbolContext> = {}
|
|
|
|
): SvgSymbolContext {
|
|
|
|
return {
|
|
|
|
...DEFAULT_CONTEXT,
|
|
|
|
...ctx,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getColor(
|
|
|
|
ctx: SvgSymbolContext,
|
|
|
|
color: string | undefined
|
|
|
|
): string | undefined {
|
|
|
|
switch (color) {
|
|
|
|
case STROKE_REPLACEMENT_COLOR:
|
|
|
|
return ctx.stroke;
|
|
|
|
case FILL_REPLACEMENT_COLOR:
|
|
|
|
return ctx.fill;
|
|
|
|
}
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reactifySvgSymbolElement(
|
|
|
|
ctx: SvgSymbolContext,
|
|
|
|
el: SvgSymbolElement,
|
|
|
|
key: number
|
|
|
|
): JSX.Element {
|
2021-02-16 18:36:14 +00:00
|
|
|
let { fill, stroke, strokeWidth } = el.props;
|
2021-02-17 12:29:56 +00:00
|
|
|
let vectorEffect;
|
2021-02-15 14:56:02 +00:00
|
|
|
fill = getColor(ctx, fill);
|
|
|
|
stroke = getColor(ctx, stroke);
|
2021-02-20 16:41:41 +00:00
|
|
|
if (strokeWidth !== undefined && typeof ctx.uniformStrokeWidth === "number") {
|
2021-02-17 12:29:56 +00:00
|
|
|
strokeWidth = ctx.uniformStrokeWidth;
|
|
|
|
vectorEffect = "non-scaling-stroke";
|
2021-02-16 18:36:14 +00:00
|
|
|
}
|
2021-02-27 18:28:44 +00:00
|
|
|
const props: typeof el.props = {
|
|
|
|
...el.props,
|
|
|
|
id: undefined,
|
|
|
|
vectorEffect,
|
|
|
|
strokeWidth,
|
|
|
|
fill,
|
|
|
|
stroke,
|
|
|
|
key,
|
|
|
|
};
|
2021-02-15 14:56:02 +00:00
|
|
|
return React.createElement(
|
|
|
|
el.tagName,
|
2021-02-27 18:28:44 +00:00
|
|
|
props,
|
2021-02-15 14:56:02 +00:00
|
|
|
el.children.map(reactifySvgSymbolElement.bind(null, ctx))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const SvgSymbolContent: React.FC<
|
|
|
|
{ data: SvgSymbolData } & SvgSymbolContext
|
|
|
|
> = (props) => {
|
|
|
|
const d = props.data;
|
|
|
|
|
|
|
|
return (
|
2021-02-27 18:28:44 +00:00
|
|
|
<g data-symbol-name={d.name}>
|
2021-02-15 14:56:02 +00:00
|
|
|
{props.data.layers.map(reactifySvgSymbolElement.bind(null, props))}
|
|
|
|
{props.showSpecs && d.specs && <VisibleSpecs specs={d.specs} />}
|
2021-02-27 18:28:44 +00:00
|
|
|
</g>
|
2021-02-15 14:56:02 +00:00
|
|
|
);
|
|
|
|
};
|