UNPKG

3.19 kBPlain TextView Raw
1/*
2* Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
3*/
4
5/*
6* This is an extension and not part of the main GoJS library.
7* Note that the API for this class may change with any version, even point releases.
8* If you intend to use an extension in production, you should copy the code to your own source directory.
9* Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
10* See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
11*/
12
13import * as go from '../release/go-module.js';
14
15// HTML + JavaScript text editor using an HTML Select Element and HTMLInfo.
16// This file exposes one instance of HTMLInfo, window.TextEditorSelectBox
17// see /samples/customTextEditingTool.html
18// see also textEditorRadioButton.js for another custom editor
19// see also textEditor.html for a re-implementation of the default text editor
20((window: any) => {
21 const customEditor = new go.HTMLInfo();
22
23 const customSelectBox = document.createElement('select');
24
25 customEditor.show = (textBlock, diagram, tool) => {
26 if (!(textBlock instanceof go.TextBlock)) return;
27
28 // Populate the select box:
29 customSelectBox.innerHTML = '';
30
31 let list = textBlock.choices;
32 // Perhaps give some default choices if textBlock.choices is null
33 if (list === null) list = ['Default A', 'Default B', 'Default C'];
34 const l = list.length;
35 for (let i = 0; i < l; i++) {
36 const op = document.createElement('option');
37 op.text = list[i];
38 op.value = list[i];
39 if (list[i] === textBlock.text) op.selected = true;
40 customSelectBox.add(op);
41
42 // consider also adding the current value, if it is not in the choices list
43 }
44
45 // After the list is populated, set the value:
46 customSelectBox.value = textBlock.text;
47
48 // Do a few different things when a user presses a key
49 customSelectBox.addEventListener('keydown', (e) => {
50 const key = e.key;
51 if (key === "Enter") { // Accept on Enter
52 (tool as any).acceptText(go.TextEditingTool.Enter);
53 return;
54 } else if (key === "Tab") { // Accept on Tab
55 (tool as any).acceptText(go.TextEditingTool.Tab);
56 e.preventDefault();
57 return false;
58 } else if (key === "Escape") { // Cancel on Esc
59 tool.doCancel();
60 if (tool.diagram) tool.diagram.focus();
61 }
62 }, false);
63
64 const loc = textBlock.getDocumentPoint(go.Spot.TopLeft);
65 const pos = diagram.transformDocToView(loc);
66 customSelectBox.style.left = pos.x + 'px';
67 customSelectBox.style.top = pos.y + 'px';
68 customSelectBox.style.position = 'absolute';
69 customSelectBox.style.zIndex = (100).toString(); // place it in front of the Diagram
70
71 if (diagram.div !== null) diagram.div.appendChild(customSelectBox);
72 customSelectBox.focus();
73 };
74
75 customEditor.hide = (diagram, tool) => {
76 if (diagram.div !== null) diagram.div.removeChild(customSelectBox);
77 };
78
79 customEditor.valueFunction = () => customSelectBox.value;
80
81 window.TextEditorSelectBox = customEditor;
82})(window);