-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4724 from Yokozuna59/chore/convert-svgDrawCommon-…
…to-ts convert `diagrams/common/svgDrawCommon.js` to ts
- Loading branch information
Showing
9 changed files
with
201 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
export interface RectData { | ||
x: number; | ||
y: number; | ||
fill: string; | ||
width: number; | ||
height: number; | ||
stroke: string; | ||
class?: string; | ||
color?: string; | ||
rx?: number; | ||
ry?: number; | ||
attrs?: Record<string, string | number>; | ||
anchor?: string; | ||
} | ||
|
||
export interface Bound { | ||
startx: number; | ||
stopx: number; | ||
starty: number; | ||
stopy: number; | ||
fill: string; | ||
stroke: string; | ||
} | ||
|
||
export interface TextData { | ||
x: number; | ||
y: number; | ||
anchor: string; | ||
text: string; | ||
textMargin: number; | ||
class?: string; | ||
} | ||
|
||
export interface TextObject { | ||
x: number; | ||
y: number; | ||
width: number; | ||
height: number; | ||
fill?: string; | ||
anchor?: string; | ||
'text-anchor': string; | ||
style: string; | ||
textMargin: number; | ||
rx: number; | ||
ry: number; | ||
tspan: boolean; | ||
valign?: string; | ||
} | ||
|
||
export type D3RectElement = d3.Selection<SVGRectElement, unknown, Element | null, unknown>; | ||
|
||
export type D3UseElement = d3.Selection<SVGUseElement, unknown, Element | null, unknown>; | ||
|
||
export type D3ImageElement = d3.Selection<SVGImageElement, unknown, Element | null, unknown>; | ||
|
||
export type D3TextElement = d3.Selection<SVGTextElement, unknown, Element | null, unknown>; | ||
|
||
export type D3TSpanElement = d3.Selection<SVGTSpanElement, unknown, Element | null, unknown>; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { sanitizeUrl } from '@braintree/sanitize-url'; | ||
import type { Group, SVG } from '../../diagram-api/types.js'; | ||
import type { | ||
Bound, | ||
D3ImageElement, | ||
D3RectElement, | ||
D3TSpanElement, | ||
D3TextElement, | ||
D3UseElement, | ||
RectData, | ||
TextData, | ||
TextObject, | ||
} from './commonTypes.js'; | ||
import { lineBreakRegex } from './common.js'; | ||
|
||
export const drawRect = (element: SVG | Group, rectData: RectData): D3RectElement => { | ||
const rectElement: D3RectElement = element.append('rect'); | ||
rectElement.attr('x', rectData.x); | ||
rectElement.attr('y', rectData.y); | ||
rectElement.attr('fill', rectData.fill); | ||
rectElement.attr('stroke', rectData.stroke); | ||
rectElement.attr('width', rectData.width); | ||
rectElement.attr('height', rectData.height); | ||
rectData.rx !== undefined && rectElement.attr('rx', rectData.rx); | ||
rectData.ry !== undefined && rectElement.attr('ry', rectData.ry); | ||
|
||
if (rectData.attrs !== undefined) { | ||
for (const attrKey in rectData.attrs) { | ||
rectElement.attr(attrKey, rectData.attrs[attrKey]); | ||
} | ||
} | ||
|
||
rectData.class !== undefined && rectElement.attr('class', rectData.class); | ||
|
||
return rectElement; | ||
}; | ||
|
||
/** | ||
* Draws a background rectangle | ||
* | ||
* @param element - Diagram (reference for bounds) | ||
* @param bounds - Shape of the rectangle | ||
*/ | ||
export const drawBackgroundRect = (element: SVG | Group, bounds: Bound): void => { | ||
const rectData: RectData = { | ||
x: bounds.startx, | ||
y: bounds.starty, | ||
width: bounds.stopx - bounds.startx, | ||
height: bounds.stopy - bounds.starty, | ||
fill: bounds.fill, | ||
stroke: bounds.stroke, | ||
class: 'rect', | ||
}; | ||
const rectElement: D3RectElement = drawRect(element, rectData); | ||
rectElement.lower(); | ||
}; | ||
|
||
export const drawText = (element: SVG | Group, textData: TextData): D3TextElement => { | ||
const nText: string = textData.text.replace(lineBreakRegex, ' '); | ||
|
||
const textElem: D3TextElement = element.append('text'); | ||
textElem.attr('x', textData.x); | ||
textElem.attr('y', textData.y); | ||
textElem.attr('class', 'legend'); | ||
|
||
textElem.style('text-anchor', textData.anchor); | ||
textData.class !== undefined && textElem.attr('class', textData.class); | ||
|
||
const tspan: D3TSpanElement = textElem.append('tspan'); | ||
tspan.attr('x', textData.x + textData.textMargin * 2); | ||
tspan.text(nText); | ||
|
||
return textElem; | ||
}; | ||
|
||
export const drawImage = (elem: SVG | Group, x: number, y: number, link: string): void => { | ||
const imageElement: D3ImageElement = elem.append('image'); | ||
imageElement.attr('x', x); | ||
imageElement.attr('y', y); | ||
const sanitizedLink: string = sanitizeUrl(link); | ||
imageElement.attr('xlink:href', sanitizedLink); | ||
}; | ||
|
||
export const drawEmbeddedImage = ( | ||
element: SVG | Group, | ||
x: number, | ||
y: number, | ||
link: string | ||
): void => { | ||
const imageElement: D3UseElement = element.append('use'); | ||
imageElement.attr('x', x); | ||
imageElement.attr('y', y); | ||
const sanitizedLink: string = sanitizeUrl(link); | ||
imageElement.attr('xlink:href', `#${sanitizedLink}`); | ||
}; | ||
|
||
export const getNoteRect = (): RectData => { | ||
const noteRectData: RectData = { | ||
x: 0, | ||
y: 0, | ||
width: 100, | ||
height: 100, | ||
fill: '#EDF2AE', | ||
stroke: '#666', | ||
anchor: 'start', | ||
rx: 0, | ||
ry: 0, | ||
}; | ||
return noteRectData; | ||
}; | ||
|
||
export const getTextObj = (): TextObject => { | ||
const testObject: TextObject = { | ||
x: 0, | ||
y: 0, | ||
width: 100, | ||
height: 100, | ||
'text-anchor': 'start', | ||
style: '#666', | ||
textMargin: 0, | ||
rx: 0, | ||
ry: 0, | ||
tspan: true, | ||
}; | ||
return testObject; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.