1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | import * as go from '../release/go-module.js';
|
12 |
|
13 |
|
14 |
|
15 | (function (window) {
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const contextMenuDIV = document.createElement('div');
|
23 | contextMenuDIV.id = 'contextMenuDIV';
|
24 |
|
25 | const cmLight = document.createElement('div');
|
26 | cmLight.id = 'cmLight';
|
27 | cmLight.className = 'goCXforeground';
|
28 | const cmDark = document.createElement('div');
|
29 | cmDark.id = 'cmDark';
|
30 | cmDark.className = 'goCXbackground';
|
31 | contextMenuDIV.appendChild(cmLight);
|
32 | contextMenuDIV.appendChild(cmDark);
|
33 | const cxMenuButtons = [
|
34 | {
|
35 | text: 'Copy',
|
36 | command: (diagram) => { diagram.commandHandler.copySelection(); },
|
37 | isVisible: (diagram) => diagram.commandHandler.canCopySelection()
|
38 | }, {
|
39 | text: 'Cut',
|
40 | command: (diagram) => { diagram.commandHandler.cutSelection(); },
|
41 | isVisible: (diagram) => diagram.commandHandler.canCutSelection()
|
42 | }, {
|
43 | text: 'Delete',
|
44 | command: (diagram) => { diagram.commandHandler.deleteSelection(); },
|
45 | isVisible: (diagram) => diagram.commandHandler.canDeleteSelection()
|
46 | }, {
|
47 | text: 'Paste',
|
48 | command: (diagram) => { diagram.commandHandler.pasteSelection(diagram.toolManager.contextMenuTool.mouseDownPoint); },
|
49 | isVisible: (diagram) => diagram.commandHandler.canPasteSelection(diagram.toolManager.contextMenuTool.mouseDownPoint)
|
50 | }, {
|
51 | text: 'Select All',
|
52 | command: (diagram) => { diagram.commandHandler.selectAll(); },
|
53 | isVisible: (diagram) => diagram.commandHandler.canSelectAll()
|
54 | }, {
|
55 | text: 'Undo',
|
56 | command: (diagram) => { diagram.commandHandler.undo(); },
|
57 | isVisible: (diagram) => diagram.commandHandler.canUndo()
|
58 | }, {
|
59 | text: 'Redo',
|
60 | command: (diagram) => { diagram.commandHandler.redo(); },
|
61 | isVisible: (diagram) => diagram.commandHandler.canRedo()
|
62 | }, {
|
63 | text: 'Scroll To Part',
|
64 | command: (diagram) => { diagram.commandHandler.scrollToPart(); },
|
65 | isVisible: (diagram) => diagram.commandHandler.canScrollToPart()
|
66 | }, {
|
67 | text: 'Zoom To Fit',
|
68 | command: (diagram) => { diagram.commandHandler.zoomToFit(); },
|
69 | isVisible: (diagram) => diagram.commandHandler.canZoomToFit()
|
70 | }, {
|
71 | text: 'Reset Zoom',
|
72 | command: (diagram) => { diagram.commandHandler.resetZoom(); },
|
73 | isVisible: (diagram) => diagram.commandHandler.canResetZoom()
|
74 | }, {
|
75 | text: 'Group Selection',
|
76 | command: (diagram) => { diagram.commandHandler.groupSelection(); },
|
77 | isVisible: (diagram) => diagram.commandHandler.canGroupSelection()
|
78 | }, {
|
79 | text: 'Ungroup Selection',
|
80 | command: (diagram) => { diagram.commandHandler.ungroupSelection(); },
|
81 | isVisible: (diagram) => diagram.commandHandler.canUngroupSelection()
|
82 | }, {
|
83 | text: 'Edit Text',
|
84 | command: (diagram) => { diagram.commandHandler.editTextBlock(); },
|
85 | isVisible: (diagram) => diagram.commandHandler.canEditTextBlock()
|
86 | }
|
87 | ];
|
88 | const $ = go.GraphObject.make;
|
89 | const myContextMenu = $(go.HTMLInfo, {
|
90 | show: showContextMenu,
|
91 | hide: hideContextMenu
|
92 | });
|
93 | let firstTime = true;
|
94 | function showContextMenu(obj, diagram, tool) {
|
95 | if (firstTime) {
|
96 |
|
97 | cmLight.addEventListener('contextmenu', (e) => { e.preventDefault(); return false; }, false);
|
98 | cmLight.addEventListener('selectstart', (e) => { e.preventDefault(); return false; }, false);
|
99 | contextMenuDIV.addEventListener('contextmenu', (e) => { e.preventDefault(); return false; }, false);
|
100 |
|
101 | contextMenuDIV.addEventListener('click', (e) => { diagram.currentTool.stopTool(); return false; }, false);
|
102 | firstTime = false;
|
103 | }
|
104 |
|
105 | cmLight.innerHTML = '';
|
106 | const ul = document.createElement('ul');
|
107 | cmLight.appendChild(ul);
|
108 | for (let i = 0; i < cxMenuButtons.length; i++) {
|
109 | const button = cxMenuButtons[i];
|
110 | const command = button.command;
|
111 | const isVisible = button.isVisible;
|
112 | if (!(typeof command === 'function'))
|
113 | continue;
|
114 |
|
115 | if (typeof isVisible === 'function' && !isVisible(diagram))
|
116 | continue;
|
117 | const li = document.createElement('li');
|
118 | const ahref = document.createElement('a');
|
119 | ahref.href = '#';
|
120 | ahref._command = button.command;
|
121 | ahref.addEventListener('click', (e) => {
|
122 | ahref._command(diagram);
|
123 | tool.stopTool();
|
124 | e.preventDefault();
|
125 | return false;
|
126 | }, false);
|
127 | ahref.textContent = button.text;
|
128 | li.appendChild(ahref);
|
129 | ul.appendChild(li);
|
130 | }
|
131 |
|
132 | document.body.appendChild(contextMenuDIV);
|
133 | }
|
134 | function hideContextMenu(diagram, tool) {
|
135 | document.body.removeChild(contextMenuDIV);
|
136 | }
|
137 | window.myHTMLLightBox = myContextMenu;
|
138 | })(window);
|