|
| 1 | +import { sanitizeUrl } from '@braintree/sanitize-url'; |
| 2 | +import type { Group, SVG } from '../../diagram-api/types.js'; |
| 3 | +import type { |
| 4 | + Bound, |
| 5 | + D3ImageElement, |
| 6 | + D3RectElement, |
| 7 | + D3TSpanElement, |
| 8 | + D3TextElement, |
| 9 | + D3UseElement, |
| 10 | + RectData, |
| 11 | + TextData, |
| 12 | + TextObject, |
| 13 | +} from './commonTypes.js'; |
| 14 | +import { lineBreakRegex } from './common.js'; |
| 15 | + |
| 16 | +export const drawRect = (element: SVG | Group, rectData: RectData): D3RectElement => { |
| 17 | + const rectElement: D3RectElement = element.append('rect'); |
| 18 | + rectElement.attr('x', rectData.x); |
| 19 | + rectElement.attr('y', rectData.y); |
| 20 | + rectElement.attr('fill', rectData.fill); |
| 21 | + rectElement.attr('stroke', rectData.stroke); |
| 22 | + rectElement.attr('width', rectData.width); |
| 23 | + rectElement.attr('height', rectData.height); |
| 24 | + rectData.rx !== undefined && rectElement.attr('rx', rectData.rx); |
| 25 | + rectData.ry !== undefined && rectElement.attr('ry', rectData.ry); |
| 26 | + |
| 27 | + if (rectData.attrs !== undefined) { |
| 28 | + for (const attrKey in rectData.attrs) { |
| 29 | + rectElement.attr(attrKey, rectData.attrs[attrKey]); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + rectData.class !== undefined && rectElement.attr('class', rectData.class); |
| 34 | + |
| 35 | + return rectElement; |
| 36 | +}; |
| 37 | + |
| 38 | +/** |
| 39 | + * Draws a background rectangle |
| 40 | + * |
| 41 | + * @param element - Diagram (reference for bounds) |
| 42 | + * @param bounds - Shape of the rectangle |
| 43 | + */ |
| 44 | +export const drawBackgroundRect = (element: SVG | Group, bounds: Bound): void => { |
| 45 | + const rectData: RectData = { |
| 46 | + x: bounds.startx, |
| 47 | + y: bounds.starty, |
| 48 | + width: bounds.stopx - bounds.startx, |
| 49 | + height: bounds.stopy - bounds.starty, |
| 50 | + fill: bounds.fill, |
| 51 | + stroke: bounds.stroke, |
| 52 | + class: 'rect', |
| 53 | + }; |
| 54 | + const rectElement: D3RectElement = drawRect(element, rectData); |
| 55 | + rectElement.lower(); |
| 56 | +}; |
| 57 | + |
| 58 | +export const drawText = (element: SVG | Group, textData: TextData): D3TextElement => { |
| 59 | + const nText: string = textData.text.replace(lineBreakRegex, ' '); |
| 60 | + |
| 61 | + const textElem: D3TextElement = element.append('text'); |
| 62 | + textElem.attr('x', textData.x); |
| 63 | + textElem.attr('y', textData.y); |
| 64 | + textElem.attr('class', 'legend'); |
| 65 | + |
| 66 | + textElem.style('text-anchor', textData.anchor); |
| 67 | + textData.class !== undefined && textElem.attr('class', textData.class); |
| 68 | + |
| 69 | + const tspan: D3TSpanElement = textElem.append('tspan'); |
| 70 | + tspan.attr('x', textData.x + textData.textMargin * 2); |
| 71 | + tspan.text(nText); |
| 72 | + |
| 73 | + return textElem; |
| 74 | +}; |
| 75 | + |
| 76 | +export const drawImage = (elem: SVG | Group, x: number, y: number, link: string): void => { |
| 77 | + const imageElement: D3ImageElement = elem.append('image'); |
| 78 | + imageElement.attr('x', x); |
| 79 | + imageElement.attr('y', y); |
| 80 | + const sanitizedLink: string = sanitizeUrl(link); |
| 81 | + imageElement.attr('xlink:href', sanitizedLink); |
| 82 | +}; |
| 83 | + |
| 84 | +export const drawEmbeddedImage = ( |
| 85 | + element: SVG | Group, |
| 86 | + x: number, |
| 87 | + y: number, |
| 88 | + link: string |
| 89 | +): void => { |
| 90 | + const imageElement: D3UseElement = element.append('use'); |
| 91 | + imageElement.attr('x', x); |
| 92 | + imageElement.attr('y', y); |
| 93 | + const sanitizedLink: string = sanitizeUrl(link); |
| 94 | + imageElement.attr('xlink:href', `#${sanitizedLink}`); |
| 95 | +}; |
| 96 | + |
| 97 | +export const getNoteRect = (): RectData => { |
| 98 | + const noteRectData: RectData = { |
| 99 | + x: 0, |
| 100 | + y: 0, |
| 101 | + width: 100, |
| 102 | + height: 100, |
| 103 | + fill: '#EDF2AE', |
| 104 | + stroke: '#666', |
| 105 | + anchor: 'start', |
| 106 | + rx: 0, |
| 107 | + ry: 0, |
| 108 | + }; |
| 109 | + return noteRectData; |
| 110 | +}; |
| 111 | + |
| 112 | +export const getTextObj = (): TextObject => { |
| 113 | + const testObject: TextObject = { |
| 114 | + x: 0, |
| 115 | + y: 0, |
| 116 | + width: 100, |
| 117 | + height: 100, |
| 118 | + 'text-anchor': 'start', |
| 119 | + style: '#666', |
| 120 | + textMargin: 0, |
| 121 | + rx: 0, |
| 122 | + ry: 0, |
| 123 | + tspan: true, |
| 124 | + }; |
| 125 | + return testObject; |
| 126 | +}; |
0 commit comments