diff --git a/lib/core/ElementRegistry.js b/lib/core/ElementRegistry.js index 54ba7ba0a..4e8bbf94f 100644 --- a/lib/core/ElementRegistry.js +++ b/lib/core/ElementRegistry.js @@ -260,6 +260,42 @@ ElementRegistry.prototype.getGraphics = function(filter, secondary) { return container && (secondary ? container.secondaryGfx : container.gfx); }; +/** + * Return the graphical SVG representation of an element. + * + * @example + * + * ```javascript + * elementRegistry.getGraphicsSvg('SomeElementId_1'); // + * + * elementRegistry.getGraphicsSvg(rootElement); // + * ``` + * + * @param {ElementLike|string} filter The element or its ID. + * + * @return {SVGElement} The graphical SVG representation. + */ +ElementRegistry.prototype.getGraphicsSvg = function(filter) { + /** + * @type {SVGElement} + */ + var gfx = this.getGraphics(filter).cloneNode(true); + + gfx.removeAttribute('transform'); + + /** + * @type {SVGRectElement} + */ + var rect = gfx.querySelector('rect').cloneNode(true); + + var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); + svg.setAttribute('width', rect.getAttribute('width')); + svg.setAttribute('height', rect.getAttribute('height')); + svg.appendChild(gfx); + + return svg; +}; + /** * Validate an ID and throw an error if invalid. * diff --git a/lib/core/ElementRegistry.spec.ts b/lib/core/ElementRegistry.spec.ts index 987071916..ec83ff918 100644 --- a/lib/core/ElementRegistry.spec.ts +++ b/lib/core/ElementRegistry.spec.ts @@ -57,6 +57,10 @@ elementRegistry.getGraphics(shapeLike); elementRegistry.getGraphics(shape); +elementRegistry.getGraphicsSvg(shapeLike); + +elementRegistry.getGraphicsSvg(shape); + elementRegistry.updateGraphics('shape', shapeGfx1); elementRegistry.updateGraphics(shapeLike, shapeGfx1);