|
| 1 | +/** |
| 2 | + * @license This demo file is part of the VSDX Export for yFiles for HTML. Copyright (c) |
| 3 | + * by yWorks GmbH, Vor dem Kreuzberg 28, 72070 Tuebingen, Germany. All rights reserved. |
| 4 | + * |
| 5 | + * YFiles demo files exhibit VSDX Export for yFiles for HTML functionalities. Any redistribution of |
| 6 | + * demo files in source code or binary form, with or without modification, is not permitted. |
| 7 | + * |
| 8 | + * Owners of a valid software license for a VSDX Export for yFiles for HTML version that this demo |
| 9 | + * is shipped with are allowed to use the demo source code as basis for their own VSDX Export for |
| 10 | + * yFiles for HTML powered applications. Use of such programs is governed by the rights and |
| 11 | + * conditions as set out in the VSDX Export for yFiles for HTML license agreement. |
| 12 | + * |
| 13 | + * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 14 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 15 | + * DISCLAIMED. IN NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 16 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 17 | + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 18 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 19 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 20 | + * THE POSSIBILITY OF SUCH DAMAGE. |
| 21 | + */ |
| 22 | + |
| 23 | +import '@yfiles/yfiles/yfiles.css' |
| 24 | +import '@yfiles/demo-resources/style/demo.css' |
| 25 | + |
| 26 | +import { |
| 27 | + Command, |
| 28 | + GraphComponent, |
| 29 | + GraphViewerInputMode, |
| 30 | + type IGraph, |
| 31 | + ImageNodeStyle, |
| 32 | + INode, |
| 33 | + Insets, |
| 34 | + License, |
| 35 | + PolylineEdgeStyle, |
| 36 | +} from '@yfiles/yfiles' |
| 37 | +import { |
| 38 | + bindAction, |
| 39 | + bindCommand, |
| 40 | + showApp, |
| 41 | + showLoadingIndicator, |
| 42 | +} from '@yfiles/demo-resources/demo-ui/demo-app' |
| 43 | +import { initDemoStyles } from '@yfiles/demo-resources/demo-ui/demo-styles' |
| 44 | +import licenseData from '../../license.json' |
| 45 | +import { |
| 46 | + AutoRoutingStyleProcessingStep, |
| 47 | + EdgePathProcessingStep, |
| 48 | + MasterProviderBase, |
| 49 | + MasterProviderContext, |
| 50 | + NodeLayoutProcessingStep, |
| 51 | + NodePortLocationProcessingStep, |
| 52 | + PolylineEdgeProvider, |
| 53 | + PortConnectionProcessingStep, |
| 54 | + type ShapeStyleConfiguration, |
| 55 | + VoidPortProvider, |
| 56 | + VsdxExportConfiguration, |
| 57 | + VsdxIO, |
| 58 | +} from '@yfiles/vsdx-export' |
| 59 | +import saveBlob from '@yfiles/demo-resources/save-blob' |
| 60 | + |
| 61 | +let graphComponent: GraphComponent = null! |
| 62 | + |
| 63 | +License.value = licenseData |
| 64 | +// initialize the GraphComponent and GraphOverviewComponent |
| 65 | +graphComponent = new GraphComponent('graphComponent') |
| 66 | + |
| 67 | +// bind toolbar commands |
| 68 | +initializeUI() |
| 69 | + |
| 70 | +// Assign the default demo styles |
| 71 | +initDemoStyles(graphComponent.graph) |
| 72 | + |
| 73 | +// load the first graph |
| 74 | +createSampleGraph(graphComponent.graph) |
| 75 | + |
| 76 | +// create the input mode |
| 77 | +graphComponent.inputMode = new GraphViewerInputMode() |
| 78 | + |
| 79 | +void graphComponent.fitGraphBounds() |
| 80 | + |
| 81 | +showApp(graphComponent) |
| 82 | + |
| 83 | +async function runExport(): Promise<void> { |
| 84 | + const fileName = 'Diagram.vsdx' |
| 85 | + |
| 86 | + // load the template file |
| 87 | + const template = await fetch('/template.vstx').then((r) => r.blob()) |
| 88 | + const vsdxIO = await VsdxIO.fromBlob(template) |
| 89 | + |
| 90 | + // create a configuration |
| 91 | + const config = VsdxExportConfiguration.createEmpty() |
| 92 | + |
| 93 | + class ComputerStencilMasterProvider extends MasterProviderBase { |
| 94 | + node( |
| 95 | + node: INode, |
| 96 | + context: MasterProviderContext, |
| 97 | + ): Promise<ShapeStyleConfiguration | null> | ShapeStyleConfiguration | null { |
| 98 | + const computerMaster = context.masters.find((master) => master.nameId.name === 'Computer') |
| 99 | + return computerMaster ? { master: computerMaster } : null |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + config.margins = new Insets(200) |
| 104 | + config.evaluateFormulas = true |
| 105 | + |
| 106 | + config.masterProviders.push(new PolylineEdgeProvider()) |
| 107 | + config.masterProviders.push(new VoidPortProvider()) |
| 108 | + config.masterProviders.push(new ComputerStencilMasterProvider()) |
| 109 | + |
| 110 | + config.shapeProcessingSteps.push(new NodeLayoutProcessingStep()) |
| 111 | + config.shapeProcessingSteps.push(new NodePortLocationProcessingStep()) |
| 112 | + config.shapeProcessingSteps.push(new EdgePathProcessingStep()) |
| 113 | + config.shapeProcessingSteps.push(new PortConnectionProcessingStep()) |
| 114 | + config.shapeProcessingSteps.push(new AutoRoutingStyleProcessingStep()) |
| 115 | + |
| 116 | + // add the yFiles diagram |
| 117 | + const page = vsdxIO.vsdxPackage.pages.get(1) |
| 118 | + await vsdxIO.addGraph(graphComponent!, config, page) |
| 119 | + |
| 120 | + // save the created VSDX file |
| 121 | + const blob = await vsdxIO.writeBlob(config) |
| 122 | + saveBlob(blob, fileName) |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Registers commands for the toolbar buttons. |
| 127 | + */ |
| 128 | +function initializeUI(): void { |
| 129 | + bindAction("button[data-command='ExportToVisio']", async () => { |
| 130 | + await showLoadingIndicator(true) |
| 131 | + |
| 132 | + setTimeout(async () => { |
| 133 | + await runExport() |
| 134 | + await showLoadingIndicator(false) |
| 135 | + }) |
| 136 | + }) |
| 137 | + |
| 138 | + bindCommand("button[data-command='ZoomIn']", Command.INCREASE_ZOOM, graphComponent) |
| 139 | + bindCommand("button[data-command='ZoomOut']", Command.DECREASE_ZOOM, graphComponent) |
| 140 | + bindCommand("button[data-command='FitContent']", Command.FIT_GRAPH_BOUNDS, graphComponent) |
| 141 | + bindCommand("button[data-command='ZoomOriginal']", Command.ZOOM, graphComponent, 1.0) |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * Creates the sample graph |
| 146 | + */ |
| 147 | +function createSampleGraph(graph: IGraph): void { |
| 148 | + graph.edgeDefaults.style = new PolylineEdgeStyle() |
| 149 | + graph.nodeDefaults.style = new ImageNodeStyle('/workstation.svg') |
| 150 | + const n1 = graph.createNodeAt({ location: [0, 0] }) |
| 151 | + const n2 = graph.createNodeAt({ location: [-50, 100] }) |
| 152 | + const n3 = graph.createNodeAt({ location: [50, 100] }) |
| 153 | + graph.createEdge({ source: n1, target: n2 }) |
| 154 | + graph.createEdge({ source: n2, target: n3 }) |
| 155 | + graph.createEdge({ source: n3, target: n1 }) |
| 156 | +} |
0 commit comments