@eclipse-glsp/client
Version:
A sprotty-based client for GLSP
118 lines • 6.24 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GLSPSvgExporter = void 0;
/********************************************************************************
* Copyright (c) 2022-2024 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
const sprotty_1 = require("@eclipse-glsp/sprotty");
const inversify_1 = require("inversify");
const uuid_1 = require("uuid");
let GLSPSvgExporter = class GLSPSvgExporter extends sprotty_1.SvgExporter {
export(root, request) {
var _a;
if (typeof document !== 'undefined') {
let svgElement = this.findSvgElement();
if (svgElement) {
svgElement = this.prepareSvgElement(svgElement, root, request);
const serializedSvg = this.createSvg(svgElement, root, (_a = request === null || request === void 0 ? void 0 : request.options) !== null && _a !== void 0 ? _a : {}, request);
const svgExport = this.getSvgExport(serializedSvg, svgElement, root, request);
// do not give request/response id here as otherwise the action is treated as an unrequested response
this.actionDispatcher.dispatch(sprotty_1.ExportSvgAction.create(svgExport, { responseId: request === null || request === void 0 ? void 0 : request.requestId, options: request === null || request === void 0 ? void 0 : request.options }));
}
}
}
createSvg(svgElement, root, options, cause) {
// createSvg requires the svg to have a non-empty id, so we generate one if necessary
const originalId = svgElement.id;
try {
svgElement.id = originalId || (0, uuid_1.v4)();
return super.createSvg(svgElement, root, options, cause);
}
finally {
svgElement.id = originalId;
}
}
findSvgElement() {
const div = document.getElementById(this.options.hiddenDiv);
// search for first svg element as hierarchy within Sprotty might change
return div && div.querySelector('svg');
}
prepareSvgElement(svgElement, root, request) {
return svgElement;
}
copyStyles(source, target, skippedProperties) {
this.copyStyle(source, target, skippedProperties);
// IE doesn't retrun anything on source.children
for (let i = 0; i < source.childNodes.length; ++i) {
const sourceChild = source.childNodes[i];
const targetChild = target.childNodes[i];
if (sourceChild instanceof Element) {
this.copyStyles(sourceChild, targetChild, []);
}
}
}
copyStyle(source, target, skippedProperties) {
const sourceStyle = getComputedStyle(source);
const targetStyle = getComputedStyle(target);
let style = '';
for (let i = 0; i < sourceStyle.length; i++) {
const propertyName = sourceStyle[i];
if (!skippedProperties.includes(propertyName)) {
const propertyValue = sourceStyle.getPropertyValue(propertyName);
const propertyPriority = sourceStyle.getPropertyPriority(propertyName);
if (targetStyle.getPropertyValue(propertyName) !== propertyValue) {
if (this.shouldUpdateStyle(target)) {
// rather set the property directly on the element to keep other values intact
target.style.setProperty(propertyName, propertyValue);
}
else {
// collect all properties to set them at once
style += `${propertyName}: ${propertyValue}${propertyPriority ? ' !' + propertyPriority : ''}; `;
}
}
}
}
if (style !== '') {
target.setAttribute('style', style.trim());
}
}
shouldUpdateStyle(element) {
// we want to simply update the style of elements and keep other values intact if they have a style property
return 'tagName' in element && 'style' in element;
}
getSvgExport(serializedSvgElement, svgElement, root, request) {
const svgExportStyle = this.getSvgExportStyle(svgElement, root, request);
return svgExportStyle ? serializedSvgElement.replace('style="', `style="${svgExportStyle}`) : serializedSvgElement;
}
getSvgExportStyle(svgElement, root, request) {
// provide generated svg code with respective sizing for proper viewing in browser and remove undesired border
const bounds = this.getBounds(root, document);
return (`width: ${bounds.width}px !important;` +
`height: ${bounds.height}px !important;` +
'border: none !important;' +
'cursor: default !important;');
}
};
exports.GLSPSvgExporter = GLSPSvgExporter;
exports.GLSPSvgExporter = GLSPSvgExporter = __decorate([
(0, inversify_1.injectable)()
], GLSPSvgExporter);
//# sourceMappingURL=glsp-svg-exporter.js.map