1- import { Command , CommandExecutionContext , CommandReturn , TYPES } from "sprotty" ;
1+ import { Command , CommandExecutionContext , CommandReturn , IVNodePostprocessor , ModelRenderer , TYPES , ViewRegistration , ViewRegistry } from "sprotty" ;
22import themeCss from "../assets/theme.css?raw" ;
33import elementCss from "../diagram/style.css?raw" ;
4+ import toHTML from "snabbdom-to-html"
45import { Action } from "sprotty-protocol" ;
5- import { inject } from "inversify" ;
6+ import { inject , multiInject } from "inversify" ;
67import { FileName } from "../fileName/fileName" ;
78
89export namespace SaveImageAction {
@@ -21,21 +22,24 @@ export class SaveImageCommand extends Command {
2122 constructor (
2223 @inject ( TYPES . Action ) _ : Action ,
2324 @inject ( FileName ) private readonly fileName : FileName ,
25+ @inject ( TYPES . ViewRegistry ) private readonly viewRegistry : ViewRegistry ,
26+ @multiInject ( TYPES . IVNodePostprocessor ) private readonly postProcessors : IVNodePostprocessor [ ]
2427 ) {
2528 super ( ) ;
2629 }
2730
2831 execute ( context : CommandExecutionContext ) : CommandReturn {
29- const root = document . getElementById ( "sprotty_root" ) ;
30- if ( ! root ) return context . root ;
31- const firstChild = root . children [ 0 ] ;
32- if ( ! firstChild ) return context . root ;
33- const innerSvg = firstChild . innerHTML ;
32+ const renderer = new ModelRenderer ( this . viewRegistry , 'main' , this . postProcessors )
33+ const svg = renderer . renderElement ( context . root )
34+ if ( ! svg ) return context . root
35+ console . debug ( toHTML ( svg ) )
36+
37+
3438 /* The result svg will render (0,0) as the top left corner of the svg.
3539 * We calculate the minimum translation of all children.
3640 * We then offset the whole svg by this opposite of this amount.
3741 */
38- const minTranslate = { x : Infinity , y : Infinity } ;
42+ /* const minTranslate = { x: Infinity, y: Infinity };
3943 for (const child of firstChild.children) {
4044 const childTranslate = this.getMinTranslate(child as HTMLElement);
4145 minTranslate.x = Math.min(minTranslate.x, childTranslate.x);
@@ -47,8 +51,8 @@ export class SaveImageCommand extends Command {
4751 const url = URL.createObjectURL(blob);
4852 const link = document.createElement("a");
4953 link.href = url;
50- link . download = this . fileName . getName ( ) + ".svg" ;
51- link . click ( ) ;
54+ link.download = this.fileName.getName() + ".svg";*/
55+ // link.click();
5256
5357 return context . root ;
5458 }
0 commit comments