From 3ae302e1911d14f95983b087bb9f8a1e361dc874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Sp=C3=B6nemann?= Date: Tue, 9 Jul 2024 10:50:36 +0000 Subject: [PATCH] Extract SVG export actions to sprotty-protocol --- packages/sprotty-protocol/src/actions.ts | 45 +++++++++++++++++++ .../src/features/export/export.spec.ts | 5 ++- .../sprotty/src/features/export/export.ts | 12 +++-- .../src/features/export/svg-exporter.ts | 14 +++--- .../src/model-source/diagram-server.ts | 4 +- .../src/model-source/local-model-source.ts | 3 +- .../sprotty/src/model-source/model-source.ts | 3 +- 7 files changed, 69 insertions(+), 17 deletions(-) diff --git a/packages/sprotty-protocol/src/actions.ts b/packages/sprotty-protocol/src/actions.ts index 6a3a509e..1371b0bb 100644 --- a/packages/sprotty-protocol/src/actions.ts +++ b/packages/sprotty-protocol/src/actions.ts @@ -719,6 +719,51 @@ export namespace HoverFeedbackAction { } } +/** + * Request to extract the currently displayed diagram as an SVG. + */ +export interface RequestExportSvgAction extends RequestAction { + kind: typeof RequestExportSvgAction.KIND + options?: ExportSvgOptions +} +export namespace RequestExportSvgAction { + export const KIND = 'requestExportSvg'; + + export function create(options?: ExportSvgOptions): RequestExportSvgAction { + return { + kind: KIND, + requestId: generateRequestId(), + options + }; + } +} + +export interface ExportSvgOptions { + skipCopyStyles?: boolean +} + +/** + * Response to a `RequestExportSvgAction` containing the current diagram's SVG code. + */ +export interface ExportSvgAction extends ResponseAction { + kind: typeof ExportSvgAction.KIND; + svg: string; + responseId: string; + options?: ExportSvgOptions; +} +export namespace ExportSvgAction { + export const KIND = 'exportSvg'; + + export function create(svg: string, requestId: string, options?: ExportSvgOptions): ExportSvgAction { + return { + kind: KIND, + svg, + responseId: requestId, + options + }; + } +} + /** * Create an element with the given schema and add it to the diagram. */ diff --git a/packages/sprotty/src/features/export/export.spec.ts b/packages/sprotty/src/features/export/export.spec.ts index d2c8c219..d87ecad0 100644 --- a/packages/sprotty/src/features/export/export.spec.ts +++ b/packages/sprotty/src/features/export/export.spec.ts @@ -21,9 +21,10 @@ import { TYPES } from '../../base/types'; import { ConsoleLogger } from '../../utils/logging'; import { CommandExecutionContext } from '../../base/commands/command'; import { SNodeImpl, SGraphImpl } from '../../graph/sgraph'; -import { ExportSvgCommand, RequestExportSvgAction } from './export'; +import { ExportSvgCommand } from './export'; import defaultModule from '../../base/di.config'; -import { SNode } from 'sprotty-protocol'; +import { RequestExportSvgAction } from 'sprotty-protocol/lib/actions'; +import { SNode } from 'sprotty-protocol/lib/model'; import { IModelFactory } from '../../base/model/smodel-factory'; import { registerModelElement } from '../../base/model/smodel-utils'; diff --git a/packages/sprotty/src/features/export/export.ts b/packages/sprotty/src/features/export/export.ts index 1784a706..16308d26 100644 --- a/packages/sprotty/src/features/export/export.ts +++ b/packages/sprotty/src/features/export/export.ts @@ -14,9 +14,9 @@ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ -import { injectable, inject } from "inversify"; +import { injectable, inject } from 'inversify'; import { VNode } from 'snabbdom'; -import { Action, generateRequestId, RequestAction } from "sprotty-protocol/lib/actions"; +import { Action, ExportSvgAction, generateRequestId, RequestAction } from 'sprotty-protocol/lib/actions'; import { CommandExecutionContext, HiddenCommand, CommandResult } from '../../base/commands/command'; import { IVNodePostprocessor } from '../../base/views/vnode-postprocessor'; import { isSelectable } from '../select/model'; @@ -24,7 +24,7 @@ import { SModelElementImpl, SModelRootImpl } from '../../base/model/smodel'; import { KeyListener } from '../../base/views/key-tool'; import { matchesKeystroke } from '../../utils/keyboard'; import { isExportable } from './model'; -import { SvgExporter, ExportSvgAction } from './svg-exporter'; +import { SvgExporter } from './svg-exporter'; import { isViewport } from '../viewport/model'; import { isHoverable } from '../hover/model'; import { TYPES } from '../../base/types'; @@ -39,10 +39,16 @@ export class ExportSvgKeyListener extends KeyListener { } } +/** + * @deprecated Use the definition from `sprotty-protocol` instead. + */ export interface ExportSvgOptions { skipCopyStyles?: boolean } +/** + * @deprecated Use the definition from `sprotty-protocol` instead. + */ export interface RequestExportSvgAction extends RequestAction { kind: typeof RequestExportSvgAction.KIND options?: ExportSvgOptions diff --git a/packages/sprotty/src/features/export/svg-exporter.ts b/packages/sprotty/src/features/export/svg-exporter.ts index e113df84..e4c0e57e 100644 --- a/packages/sprotty/src/features/export/svg-exporter.ts +++ b/packages/sprotty/src/features/export/svg-exporter.ts @@ -14,8 +14,8 @@ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ -import { inject, injectable, multiInject, optional } from "inversify"; -import { Action, ResponseAction } from 'sprotty-protocol/lib/actions'; +import { inject, injectable, multiInject, optional } from 'inversify'; +import { Action, ExportSvgOptions, RequestExportSvgAction, ResponseAction } from 'sprotty-protocol/lib/actions'; import { Bounds } from 'sprotty-protocol/lib/utils/geometry'; import { ActionDispatcher } from '../../base/actions/action-dispatcher'; import { SModelRootImpl } from '../../base/model/smodel'; @@ -23,9 +23,11 @@ import { TYPES } from '../../base/types'; import { ViewerOptions } from '../../base/views/viewer-options'; import { ILogger } from '../../utils/logging'; import { isBoundsAware } from '../bounds/model'; -import { RequestExportSvgAction, ExportSvgOptions } from "./export"; -import { ISvgExportPostProcessor } from "./svg-export-postprocessor"; +import { ISvgExportPostProcessor } from './svg-export-postprocessor'; +/** + * @deprecated Use the definition from `sprotty-protocol` instead. + */ export interface ExportSvgAction extends ResponseAction { kind: typeof ExportSvgAction.KIND; svg: string; @@ -67,7 +69,7 @@ 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?.options || {}, request); + const svg = this.createSvg(svgElement, root, request?.options ?? {}, request); this.actionDispatcher.dispatch(ExportSvgAction.create(svg, request ? request.requestId : '', request?.options)); } } @@ -115,7 +117,7 @@ export class SvgExporter { if (skippedProperties.indexOf(key) === -1) { const value = sourceStyle.getPropertyValue(key); if (targetStyle.getPropertyValue(key) !== value) { - diffStyle += key + ":" + value + ";"; + diffStyle += key + ':' + value + ';'; } } } diff --git a/packages/sprotty/src/model-source/diagram-server.ts b/packages/sprotty/src/model-source/diagram-server.ts index 9e68b808..02716b22 100644 --- a/packages/sprotty/src/model-source/diagram-server.ts +++ b/packages/sprotty/src/model-source/diagram-server.ts @@ -18,7 +18,8 @@ import { saveAs } from 'file-saver'; import { inject, injectable } from 'inversify'; import { Action, OpenAction, ActionMessage, isActionMessage, CollapseExpandAction, CollapseExpandAllAction, - ComputedBoundsAction, RequestModelAction, RequestPopupModelAction, SetModelAction, UpdateModelAction + ComputedBoundsAction, RequestModelAction, RequestPopupModelAction, SetModelAction, UpdateModelAction, + ExportSvgAction } from 'sprotty-protocol/lib/actions'; import { SModelRoot } from 'sprotty-protocol/lib/model'; import { ActionHandlerRegistry } from '../base/actions/action-handler'; @@ -26,7 +27,6 @@ import { ICommand } from '../base/commands/command'; import { SetModelCommand } from '../base/features/set-model'; import { TYPES } from '../base/types'; import { RequestBoundsCommand } from '../features/bounds/bounds-manipulation'; -import { ExportSvgAction } from '../features/export/svg-exporter'; import { UpdateModelCommand } from '../features/update/update-model'; import { ILogger } from '../utils/logging'; import { ComputedBoundsApplicator, ModelSource } from './model-source'; diff --git a/packages/sprotty/src/model-source/local-model-source.ts b/packages/sprotty/src/model-source/local-model-source.ts index af1c2e1c..9a1ee889 100644 --- a/packages/sprotty/src/model-source/local-model-source.ts +++ b/packages/sprotty/src/model-source/local-model-source.ts @@ -17,7 +17,7 @@ import { saveAs } from 'file-saver'; import { inject, injectable, optional } from 'inversify'; import { - Action, ComputedBoundsAction, RequestBoundsAction, RequestModelAction, RequestPopupModelAction, + Action, ComputedBoundsAction, ExportSvgAction, RequestBoundsAction, RequestModelAction, RequestPopupModelAction, SetModelAction, SetPopupModelAction, UpdateModelAction } from 'sprotty-protocol/lib/actions'; import { SModelElement, SModelRoot, Viewport} from 'sprotty-protocol/lib/model'; @@ -29,7 +29,6 @@ import { FluentIterable } from '../utils/iterable'; import { TYPES } from '../base/types'; import { ActionHandlerRegistry } from '../base/actions/action-handler'; import { EMPTY_ROOT } from '../base/model/smodel-factory'; -import { ExportSvgAction } from '../features/export/svg-exporter'; import { applyMatches, Match } from '../features/update/model-matching'; import { ModelSource, ComputedBoundsApplicator } from './model-source'; diff --git a/packages/sprotty/src/model-source/model-source.ts b/packages/sprotty/src/model-source/model-source.ts index 868d56a6..511211e9 100644 --- a/packages/sprotty/src/model-source/model-source.ts +++ b/packages/sprotty/src/model-source/model-source.ts @@ -15,7 +15,7 @@ ********************************************************************************/ import { inject, injectable } from 'inversify'; -import { Action, ComputedBoundsAction, RequestModelAction } from 'sprotty-protocol/lib/actions'; +import { Action, ComputedBoundsAction, ExportSvgAction, RequestModelAction } from 'sprotty-protocol/lib/actions'; import { SModelElement, SModelRoot } from 'sprotty-protocol/lib/model'; import { Dimension, Point } from 'sprotty-protocol/lib/utils/geometry'; import { SModelIndex } from 'sprotty-protocol/lib/utils/model-utils'; @@ -24,7 +24,6 @@ import { ActionHandlerRegistry, IActionHandler, IActionHandlerInitializer } from import { ICommand } from '../base/commands/command'; import { TYPES } from '../base/types'; import { ViewerOptions } from '../base/views/viewer-options'; -import { ExportSvgAction } from '../features/export/svg-exporter'; /** * A model source is serving the model to the event cycle. It represents