Skip to content

Commit

Permalink
Create svg export options object
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Fontorbe <[email protected]>
  • Loading branch information
gfontorbe committed May 15, 2024
1 parent eaa0652 commit 27ba44e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
10 changes: 7 additions & 3 deletions packages/sprotty/src/features/export/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,22 @@ export class ExportSvgKeyListener extends KeyListener {
}
}

export interface ExportSvgOptions {
skipCopyStyles?: boolean
}

export interface RequestExportSvgAction extends RequestAction<ExportSvgAction> {
kind: typeof RequestExportSvgAction.KIND
skipCopyStyles?: boolean
options?: ExportSvgOptions
}
export namespace RequestExportSvgAction {
export const KIND = 'requestExportSvg';

export function create(skipCopyStyles?: boolean): RequestExportSvgAction {
export function create(options: ExportSvgOptions = {}): RequestExportSvgAction {
return {
kind: KIND,
requestId: generateRequestId(),
skipCopyStyles
options
};
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/sprotty/src/features/export/svg-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ import { TYPES } from '../../base/types';
import { ViewerOptions } from '../../base/views/viewer-options';
import { ILogger } from '../../utils/logging';
import { isBoundsAware } from '../bounds/model';
import { RequestExportSvgAction } from "./export";
import { RequestExportSvgAction, ExportSvgOptions } from "./export";
import { ISvgExportPostProcessor } from "./svg-export-postprocessor";

export interface ExportSvgAction extends ResponseAction {
kind: typeof ExportSvgAction.KIND;
svg: string;
responseId: string;
skipCopyStyles?: boolean;
options?: ExportSvgOptions;
}
export namespace ExportSvgAction {
export const KIND = 'exportSvg';

export function create(svg: string, requestId: string, skipCopyStyles: boolean = false): ExportSvgAction {
export function create(svg: string, requestId: string, options?: ExportSvgOptions): ExportSvgAction {
return {
kind: KIND,
svg,
responseId: requestId,
skipCopyStyles
options
};
}
}
Expand All @@ -67,12 +67,12 @@ export class SvgExporter {
this.log.warn(this, `No svg element found in ${this.options.hiddenDiv} div. Nothing to export.`);
return;
}
const svg = this.createSvg(svgElement, root, request?.skipCopyStyles, request);
this.actionDispatcher.dispatch(ExportSvgAction.create(svg, request ? request.requestId : '', request?.skipCopyStyles));
const svg = this.createSvg(svgElement, root, request?.options || {}, request);
this.actionDispatcher.dispatch(ExportSvgAction.create(svg, request ? request.requestId : '', request?.options));
}
}

protected createSvg(svgElementOrig: SVGSVGElement, root: SModelRootImpl, skipCopyStyles: boolean = false, cause?: Action): string {
protected createSvg(svgElementOrig: SVGSVGElement, root: SModelRootImpl, options?: ExportSvgOptions, cause?: Action): string {
const serializer = new XMLSerializer();
const svgCopy = serializer.serializeToString(svgElementOrig);
const iframe: HTMLIFrameElement = document.createElement('iframe');
Expand All @@ -85,7 +85,7 @@ export class SvgExporter {
docCopy.close();
const svgElementNew = docCopy.querySelector('svg')!;
svgElementNew.removeAttribute('opacity');
if (!skipCopyStyles) {
if (!options?.skipCopyStyles) {
// inline-size copied from sprotty-hidden svg shrinks the svg so it is not visible.
this.copyStyles(svgElementOrig, svgElementNew, ['width', 'height', 'opacity', 'inline-size']);
}
Expand Down

0 comments on commit 27ba44e

Please sign in to comment.