Skip to content

Commit

Permalink
Merge pull request #459 from eclipse-sprotty/extract-svg-action
Browse files Browse the repository at this point in the history
Extract SVG export actions to sprotty-protocol
  • Loading branch information
jbicker authored Jul 10, 2024
2 parents 9aa2083 + 3ae302e commit 2d82b8c
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 17 deletions.
45 changes: 45 additions & 0 deletions packages/sprotty-protocol/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,51 @@ export namespace HoverFeedbackAction {
}
}

/**
* Request to extract the currently displayed diagram as an SVG.
*/
export interface RequestExportSvgAction extends RequestAction<ExportSvgAction> {
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.
*/
Expand Down
5 changes: 3 additions & 2 deletions packages/sprotty/src/features/export/export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
12 changes: 9 additions & 3 deletions packages/sprotty/src/features/export/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
* 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';
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';
Expand All @@ -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<ExportSvgAction> {
kind: typeof RequestExportSvgAction.KIND
options?: ExportSvgOptions
Expand Down
14 changes: 8 additions & 6 deletions packages/sprotty/src/features/export/svg-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
* 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';
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;
Expand Down Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -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 + ';';
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/sprotty/src/model-source/diagram-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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';
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';
Expand Down
3 changes: 1 addition & 2 deletions packages/sprotty/src/model-source/local-model-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';

Expand Down
3 changes: 1 addition & 2 deletions packages/sprotty/src/model-source/model-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down

0 comments on commit 2d82b8c

Please sign in to comment.