|
| 1 | +import { TD3SVGElementSelection, TD3Selection } from '@/types'; |
| 2 | +import { |
| 3 | + TEllipseNode, |
| 4 | + TPolygonNode, |
| 5 | + TRectangleNode, |
| 6 | + TStarNode, |
| 7 | +} from '@pda/types/dtif'; |
| 8 | +import { getElementId } from '../other'; |
| 9 | +import { appendNodeWrapperGElement } from './append-node-wrapper-g-element'; |
| 10 | + |
| 11 | +export async function appendBasicShapeNodeElement( |
| 12 | + parent: TD3SVGElementSelection, |
| 13 | + props: { |
| 14 | + node: TRectangleNode | TEllipseNode | TStarNode | TPolygonNode; |
| 15 | + shapeAppend: ( |
| 16 | + parent: TD3SVGElementSelection |
| 17 | + ) => Promise<TD3SVGElementSelection>; |
| 18 | + index?: number; |
| 19 | + } |
| 20 | +): Promise<TD3Selection<SVGGElement>> { |
| 21 | + const { node, shapeAppend, index = 0 } = props; |
| 22 | + const fillClipPathId = getElementId({ |
| 23 | + id: node.id, |
| 24 | + index, |
| 25 | + type: node.type.toLocaleLowerCase(), |
| 26 | + category: 'fill-clip', |
| 27 | + isDefinition: true, |
| 28 | + }); |
| 29 | + |
| 30 | + // Create element |
| 31 | + const gElement = appendNodeWrapperGElement(parent, { node, index }); |
| 32 | + |
| 33 | + // Append children |
| 34 | + await appendDefsElement(gElement, { fillClipPathId, shapeAppend }); |
| 35 | + // TODO: fill |
| 36 | + |
| 37 | + return gElement; |
| 38 | +} |
| 39 | + |
| 40 | +async function appendDefsElement( |
| 41 | + parent: TD3SVGElementSelection, |
| 42 | + props: { |
| 43 | + fillClipPathId: string; |
| 44 | + shapeAppend: ( |
| 45 | + parent: TD3SVGElementSelection |
| 46 | + ) => Promise<TD3SVGElementSelection>; |
| 47 | + } |
| 48 | +) { |
| 49 | + const defsElement = parent.append('defs'); |
| 50 | + await appendClipPathElement(defsElement, props); |
| 51 | + return defsElement; |
| 52 | +} |
| 53 | + |
| 54 | +async function appendClipPathElement( |
| 55 | + parent: TD3SVGElementSelection, |
| 56 | + props: { |
| 57 | + fillClipPathId: string; |
| 58 | + shapeAppend: ( |
| 59 | + parent: TD3SVGElementSelection |
| 60 | + ) => Promise<TD3SVGElementSelection>; |
| 61 | + } |
| 62 | +) { |
| 63 | + const { fillClipPathId, shapeAppend } = props; |
| 64 | + const clipPathElement = parent.append('clipPath').attr('id', fillClipPathId); |
| 65 | + |
| 66 | + // Append children |
| 67 | + await shapeAppend(clipPathElement); |
| 68 | + |
| 69 | + return clipPathElement; |
| 70 | +} |
0 commit comments