@kabel-project/kabel 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (273) hide show
  1. package/dist/comment-renderer/renderer.d.ts +32 -0
  2. package/dist/comment-renderer/renderer.d.ts.map +1 -0
  3. package/dist/controllers/base.d.ts +39 -0
  4. package/dist/controllers/base.d.ts.map +1 -0
  5. package/dist/controllers/wasd.d.ts +33 -0
  6. package/dist/controllers/wasd.d.ts.map +1 -0
  7. package/dist/events/comment-drag-handle.d.ts +2 -0
  8. package/dist/events/comment-drag-handle.d.ts.map +1 -0
  9. package/dist/events/comment-input.d.ts +4 -0
  10. package/dist/events/comment-input.d.ts.map +1 -0
  11. package/dist/events/connection-line.d.ts +2 -0
  12. package/dist/events/connection-line.d.ts.map +1 -0
  13. package/dist/events/connector.d.ts +2 -0
  14. package/dist/events/connector.d.ts.map +1 -0
  15. package/dist/events/draggable.d.ts +2 -0
  16. package/dist/events/draggable.d.ts.map +1 -0
  17. package/{events/events.ts → dist/events/events.d.ts} +8 -7
  18. package/dist/events/events.d.ts.map +1 -0
  19. package/dist/events/input-box.d.ts +2 -0
  20. package/dist/events/input-box.d.ts.map +1 -0
  21. package/dist/events/node-x-btn.d.ts +2 -0
  22. package/dist/events/node-x-btn.d.ts.map +1 -0
  23. package/dist/kabel.js +2 -0
  24. package/dist/kabel.js.map +1 -0
  25. package/dist/renderers/apollo/apollo.d.ts +12 -0
  26. package/dist/renderers/apollo/apollo.d.ts.map +1 -0
  27. package/dist/renderers/apollo/constants.d.ts +21 -0
  28. package/dist/renderers/apollo/constants.d.ts.map +1 -0
  29. package/dist/renderers/apollo/renderer.d.ts +97 -0
  30. package/dist/renderers/apollo/renderer.d.ts.map +1 -0
  31. package/{renderers/atlas/atlas.ts → dist/renderers/atlas/atlas.d.ts} +6 -15
  32. package/dist/renderers/atlas/atlas.d.ts.map +1 -0
  33. package/dist/renderers/constants.d.ts +51 -0
  34. package/dist/renderers/constants.d.ts.map +1 -0
  35. package/dist/renderers/renderer.d.ts +470 -0
  36. package/dist/renderers/renderer.d.ts.map +1 -0
  37. package/dist/renderers/representer-node.d.ts +27 -0
  38. package/dist/renderers/representer-node.d.ts.map +1 -0
  39. package/dist/renderers/representer.d.ts +13 -0
  40. package/dist/renderers/representer.d.ts.map +1 -0
  41. package/dist/src/category.d.ts +48 -0
  42. package/dist/src/category.d.ts.map +1 -0
  43. package/{src/colors.ts → dist/src/colors.d.ts} +21 -20
  44. package/dist/src/colors.d.ts.map +1 -0
  45. package/dist/src/comment.d.ts +88 -0
  46. package/dist/src/comment.d.ts.map +1 -0
  47. package/dist/src/connection.d.ts +52 -0
  48. package/dist/src/connection.d.ts.map +1 -0
  49. package/dist/src/context-menu.d.ts +76 -0
  50. package/dist/src/context-menu.d.ts.map +1 -0
  51. package/dist/src/coordinates.d.ts +52 -0
  52. package/dist/src/coordinates.d.ts.map +1 -0
  53. package/dist/src/core.d.ts +153 -0
  54. package/dist/src/core.d.ts.map +1 -0
  55. package/dist/src/ctx-menu-registry.d.ts +22 -0
  56. package/dist/src/ctx-menu-registry.d.ts.map +1 -0
  57. package/dist/src/dropdown-menu.d.ts +87 -0
  58. package/dist/src/dropdown-menu.d.ts.map +1 -0
  59. package/dist/src/field.d.ts +305 -0
  60. package/dist/src/field.d.ts.map +1 -0
  61. package/dist/src/flyout.d.ts +41 -0
  62. package/dist/src/flyout.d.ts.map +1 -0
  63. package/dist/src/fonts-manager.d.ts +6 -0
  64. package/dist/src/fonts-manager.d.ts.map +1 -0
  65. package/dist/src/grid.d.ts +60 -0
  66. package/dist/src/grid.d.ts.map +1 -0
  67. package/dist/src/headless-node.d.ts +11 -0
  68. package/dist/src/headless-node.d.ts.map +1 -0
  69. package/dist/src/index.d.ts +38 -0
  70. package/dist/src/index.d.ts.map +1 -0
  71. package/dist/src/inject-headless.d.ts +4 -0
  72. package/dist/src/inject-headless.d.ts.map +1 -0
  73. package/{src/inject.ts → dist/src/inject.d.ts} +142 -213
  74. package/dist/src/inject.d.ts.map +1 -0
  75. package/{src/main-workspace.ts → dist/src/main-workspace.d.ts} +31 -51
  76. package/dist/src/main-workspace.d.ts.map +1 -0
  77. package/dist/src/mutator.d.ts +2 -0
  78. package/dist/src/mutator.d.ts.map +1 -0
  79. package/{src/node-types.ts → dist/src/node-types.d.ts} +26 -27
  80. package/dist/src/node-types.d.ts.map +1 -0
  81. package/dist/src/nodesvg.d.ts +266 -0
  82. package/dist/src/nodesvg.d.ts.map +1 -0
  83. package/{src/prototypes.ts → dist/src/prototypes.d.ts} +10 -9
  84. package/dist/src/prototypes.d.ts.map +1 -0
  85. package/{src/renderer-map.ts → dist/src/renderer-map.d.ts} +51 -86
  86. package/dist/src/renderer-map.d.ts.map +1 -0
  87. package/dist/src/toolbox.d.ts +51 -0
  88. package/dist/src/toolbox.d.ts.map +1 -0
  89. package/{src/types.ts → dist/src/types.d.ts} +159 -205
  90. package/dist/src/types.d.ts.map +1 -0
  91. package/dist/src/undo-redo.d.ts +15 -0
  92. package/dist/src/undo-redo.d.ts.map +1 -0
  93. package/{src/visual-types.ts → dist/src/visual-types.d.ts} +34 -29
  94. package/dist/src/visual-types.d.ts.map +1 -0
  95. package/dist/src/widget-prototypes.d.ts +10 -0
  96. package/dist/src/widget-prototypes.d.ts.map +1 -0
  97. package/dist/src/widget.d.ts +62 -0
  98. package/dist/src/widget.d.ts.map +1 -0
  99. package/{src/workspace-coords.ts → dist/src/workspace-coords.d.ts} +10 -14
  100. package/dist/src/workspace-coords.d.ts.map +1 -0
  101. package/dist/src/workspace-svg.d.ts +371 -0
  102. package/dist/src/workspace-svg.d.ts.map +1 -0
  103. package/dist/src/workspace.d.ts +50 -0
  104. package/dist/src/workspace.d.ts.map +1 -0
  105. package/dist/themes/dark.d.ts +4 -0
  106. package/dist/themes/dark.d.ts.map +1 -0
  107. package/dist/themes/default.d.ts +4 -0
  108. package/dist/themes/default.d.ts.map +1 -0
  109. package/dist/themes/themes.d.ts +6 -0
  110. package/dist/themes/themes.d.ts.map +1 -0
  111. package/dist/util/emitter.d.ts +10 -0
  112. package/dist/util/emitter.d.ts.map +1 -0
  113. package/dist/util/env.d.ts +7 -0
  114. package/dist/util/env.d.ts.map +1 -0
  115. package/{util/escape-html.ts → dist/util/escape-html.d.ts} +16 -22
  116. package/dist/util/escape-html.d.ts.map +1 -0
  117. package/dist/util/eventer.d.ts +29 -0
  118. package/dist/util/eventer.d.ts.map +1 -0
  119. package/dist/util/has-prop.d.ts +2 -0
  120. package/dist/util/has-prop.d.ts.map +1 -0
  121. package/dist/util/parse-color.d.ts +6 -0
  122. package/dist/util/parse-color.d.ts.map +1 -0
  123. package/dist/util/path.d.ts +25 -0
  124. package/dist/util/path.d.ts.map +1 -0
  125. package/dist/util/styler.d.ts +12 -0
  126. package/dist/util/styler.d.ts.map +1 -0
  127. package/dist/util/uid.d.ts +42 -0
  128. package/dist/util/uid.d.ts.map +1 -0
  129. package/{util/unescape-html.ts → dist/util/unescape-html.d.ts} +16 -22
  130. package/dist/util/unescape-html.d.ts.map +1 -0
  131. package/dist/util/user-state.d.ts +37 -0
  132. package/dist/util/user-state.d.ts.map +1 -0
  133. package/{util/wait-anim-frames.ts → dist/util/wait-anim-frames.d.ts} +11 -24
  134. package/dist/util/wait-anim-frames.d.ts.map +1 -0
  135. package/dist/util/window-listeners.d.ts +8 -0
  136. package/dist/util/window-listeners.d.ts.map +1 -0
  137. package/package.json +4 -1
  138. package/(1.0.7)kabel.md +0 -18
  139. package/_READ_ME_MEDIA_/documentation/docs.md +0 -293
  140. package/_READ_ME_MEDIA_/workspace.png +0 -0
  141. package/comment-renderer/renderer.ts +0 -228
  142. package/controllers/base.ts +0 -186
  143. package/controllers/wasd.ts +0 -132
  144. package/docs/README.md +0 -98
  145. package/docs/_media/docs.md +0 -289
  146. package/docs/_media/workspace.png +0 -0
  147. package/docs/classes/CommentModel.md +0 -271
  148. package/docs/classes/CommentRenderer.md +0 -457
  149. package/docs/classes/ConnectableField.md +0 -597
  150. package/docs/classes/Connection.md +0 -191
  151. package/docs/classes/ContextMenuHTML.md +0 -163
  152. package/docs/classes/Coordinates.md +0 -187
  153. package/docs/classes/DropdownContainer.md +0 -300
  154. package/docs/classes/DummyField.md +0 -393
  155. package/docs/classes/Eventer.md +0 -185
  156. package/docs/classes/Field.md +0 -461
  157. package/docs/classes/InjectMsg.md +0 -85
  158. package/docs/classes/NodeSvg.md +0 -1011
  159. package/docs/classes/NumberField.md +0 -559
  160. package/docs/classes/OptConnectField.md +0 -624
  161. package/docs/classes/Renderer.md +0 -1636
  162. package/docs/classes/RendererConstants.md +0 -343
  163. package/docs/classes/Representer.md +0 -95
  164. package/docs/classes/RepresenterNode.md +0 -175
  165. package/docs/classes/TextField.md +0 -559
  166. package/docs/classes/Toolbox.md +0 -172
  167. package/docs/classes/WASDController.md +0 -616
  168. package/docs/classes/Widget.md +0 -195
  169. package/docs/classes/WorkspaceController.md +0 -385
  170. package/docs/classes/WorkspaceCoords.md +0 -218
  171. package/docs/classes/WorkspaceSvg.md +0 -1380
  172. package/docs/functions/clearMainWorkspace.md +0 -20
  173. package/docs/functions/getMainWorkspace.md +0 -19
  174. package/docs/functions/inject.md +0 -35
  175. package/docs/functions/setMainWorkspace.md +0 -28
  176. package/docs/globals.md +0 -95
  177. package/docs/interfaces/ColorStyle.md +0 -43
  178. package/docs/interfaces/ConnectorToFrom.md +0 -57
  179. package/docs/interfaces/DrawState.md +0 -81
  180. package/docs/interfaces/FieldConnectionData.md +0 -25
  181. package/docs/interfaces/FieldOptions.md +0 -63
  182. package/docs/interfaces/FieldRawBoxData.md +0 -25
  183. package/docs/interfaces/FieldVisualInfo.md +0 -65
  184. package/docs/interfaces/GridOptions.md +0 -61
  185. package/docs/interfaces/InjectOptions.md +0 -133
  186. package/docs/interfaces/InputFieldJson.md +0 -50
  187. package/docs/interfaces/KabelCommentRendering.md +0 -31
  188. package/docs/interfaces/KabelInterface.md +0 -469
  189. package/docs/interfaces/KabelNodeRendering.md +0 -77
  190. package/docs/interfaces/KabelUIX.md +0 -105
  191. package/docs/interfaces/KabelUtils.md +0 -215
  192. package/docs/interfaces/NodeEvents.md +0 -42
  193. package/docs/interfaces/NodeJson.md +0 -104
  194. package/docs/interfaces/NodePrototype.md +0 -82
  195. package/docs/interfaces/RegisteredEl.md +0 -53
  196. package/docs/interfaces/SerializedNode.md +0 -128
  197. package/docs/interfaces/TblxCategoryStruct.md +0 -41
  198. package/docs/interfaces/TblxFieldStruct.md +0 -28
  199. package/docs/interfaces/TblxNodeStruct.md +0 -35
  200. package/docs/interfaces/WidgetOptions.md +0 -115
  201. package/docs/interfaces/WidgetPrototypeList.md +0 -15
  202. package/docs/type-aliases/AnyField.md +0 -13
  203. package/docs/type-aliases/AnyFieldCls.md +0 -13
  204. package/docs/type-aliases/Color.md +0 -13
  205. package/docs/type-aliases/Connectable.md +0 -13
  206. package/docs/type-aliases/EventArgs.md +0 -11
  207. package/docs/type-aliases/EventSetupFn.md +0 -25
  208. package/docs/type-aliases/Hex.md +0 -13
  209. package/docs/type-aliases/RGBObject.md +0 -37
  210. package/docs/type-aliases/RGBString.md +0 -13
  211. package/docs/type-aliases/RGBTuple.md +0 -13
  212. package/docs/type-aliases/TblxObjStruct.md +0 -52
  213. package/docs/variables/CategoryColors.md +0 -29
  214. package/docs/variables/FieldMap.md +0 -41
  215. package/docs/variables/NodePrototypes.md +0 -18
  216. package/docs/variables/default.md +0 -11
  217. package/events/comment-drag-handle.ts +0 -61
  218. package/events/comment-input.ts +0 -291
  219. package/events/connection-line.ts +0 -68
  220. package/events/connector.ts +0 -116
  221. package/events/draggable.ts +0 -119
  222. package/events/input-box.ts +0 -213
  223. package/events/node-x-btn.ts +0 -25
  224. package/index.d.ts +0 -4
  225. package/renderers/apollo/apollo.ts +0 -21
  226. package/renderers/apollo/constants.ts +0 -40
  227. package/renderers/apollo/renderer.ts +0 -331
  228. package/renderers/constants.ts +0 -87
  229. package/renderers/renderer.ts +0 -1288
  230. package/renderers/representer-node.ts +0 -52
  231. package/renderers/representer.ts +0 -25
  232. package/src/category.ts +0 -107
  233. package/src/comment.ts +0 -142
  234. package/src/connection.ts +0 -114
  235. package/src/context-menu.ts +0 -194
  236. package/src/coordinates.ts +0 -74
  237. package/src/core.ts +0 -202
  238. package/src/ctx-menu-registry.ts +0 -143
  239. package/src/dropdown-menu.ts +0 -215
  240. package/src/field.ts +0 -595
  241. package/src/flyout.ts +0 -165
  242. package/src/fonts-manager.ts +0 -38
  243. package/src/grid.ts +0 -162
  244. package/src/headless-node.ts +0 -27
  245. package/src/index.ts +0 -115
  246. package/src/inject-headless.ts +0 -18
  247. package/src/mutator.ts +0 -40
  248. package/src/nodesvg.ts +0 -756
  249. package/src/styles.css +0 -224
  250. package/src/toolbox.ts +0 -125
  251. package/src/undo-redo.ts +0 -87
  252. package/src/widget-prototypes.ts +0 -11
  253. package/src/widget.ts +0 -139
  254. package/src/workspace-svg.ts +0 -736
  255. package/src/workspace.ts +0 -155
  256. package/test-server.js +0 -61
  257. package/themes/dark.ts +0 -32
  258. package/themes/default.ts +0 -28
  259. package/themes/themes.ts +0 -9
  260. package/tsconfig.json +0 -25
  261. package/typedoc.json +0 -10
  262. package/util/emitter.ts +0 -33
  263. package/util/env.ts +0 -11
  264. package/util/eventer.ts +0 -108
  265. package/util/has-prop.ts +0 -4
  266. package/util/parse-color.ts +0 -42
  267. package/util/path.ts +0 -99
  268. package/util/styler.ts +0 -41
  269. package/util/uid.ts +0 -184
  270. package/util/user-state.ts +0 -68
  271. package/util/window-listeners.ts +0 -62
  272. package/webpack.config.js +0 -80
  273. /package/{docs/_media → dist}/index.html +0 -0
package/src/field.ts DELETED
@@ -1,595 +0,0 @@
1
- import Connection, { Connectable } from "./connection";
2
- import { G, Path, Rect, Svg, SVG, Text } from '@svgdotjs/svg.js';
3
- import NodeSvg from "./nodesvg";
4
- import { ConnectorToFrom } from "../renderers/renderer";
5
- import dropdownContainer, { DropdownOptions } from "./dropdown-menu";
6
- import WorkspaceSvg from "./workspace-svg";
7
- /**
8
- * Options used to initialize a Field.
9
- */
10
- export interface FieldOptions { /** Name of the field */
11
- name: string;
12
- /** Human-readable label for the field */
13
- label: string;
14
- /** Key used to identify the field type in FieldTypesMap */
15
- type: string;
16
- /** Field value (optional) */
17
- value?: any;
18
- /** Only used by OptConnectField to determine internal value type */
19
- fld_type?: "number" | "string";
20
- [key: string]: any;
21
- };
22
-
23
- export interface FieldVisualInfo {
24
- measuredWidth: number; // The width approximated by the field itself.
25
- measuredHeight: number; // The height approximated by the field itself.
26
- background: Rect; // The node's background element.
27
- svg: Svg; // The workspace's SVG.js svg
28
- nodeGroup: G; // Group for the node
29
- fieldGroup: G; // A group containing the label & input.
30
- xUsed: number; // The amount of x space used up by the label.
31
- }
32
- export interface FieldRawBoxData {
33
- box: Rect;
34
- txt: Text;
35
- }
36
- export interface FieldConnectionData {
37
- connector: Path;
38
- connState: ConnectorToFrom
39
- }
40
- /**
41
- * Base class for all fields.
42
- * @template T The type of the value stored in the field
43
- */
44
- class Field<T = any> {
45
- label: string;
46
- name: string;
47
- type: string;
48
- node?: NodeSvg;
49
- editable: boolean;
50
- svgGroup?: G;
51
- protected value: T | null;
52
- static register(name: string, cls: Function) { };
53
- static unregister(name: string) { };
54
- constructor() {
55
- this.label = '';
56
- this.name = '';
57
- this.type = '';
58
- this.value = null;
59
- this.editable = true;
60
- }
61
- onDraw(rawBox?: FieldRawBoxData, connectionBubble?: FieldConnectionData) {
62
-
63
- }
64
- canEditConnector() {
65
- return false;
66
- }
67
- /**
68
- * Set whether or not you can edit this field.
69
- * @param val - The value to set to.
70
- */
71
- setEditable(val: boolean) {
72
- this.editable = val;
73
- if (this.node) {
74
- this.node?.workspace?.renderer?.rerenderNode?.(this.node)
75
- }
76
- }
77
- /**
78
- * Ask whether or not we can edit this field.
79
- */
80
- canEdit() {
81
- return this.editable;
82
- }
83
- /**
84
- * Set field name to something else.
85
- * @param name string
86
- * @returns the new name.
87
- */
88
- setName(name: string) {
89
- return this.name = name;
90
- }
91
- /**
92
- * Initialize the field from JSON options.
93
- * @param json FieldOptions object
94
- */
95
- fromJson(json: FieldOptions, workspace?: WorkspaceSvg) {
96
- if (json.name !== undefined) this.name = json.name;
97
- if (json.label !== undefined) this.label = json.label;
98
- if (json.type !== undefined) this.type = json.type;
99
- if (json.value !== undefined) this.value = json.value;
100
- if (json.editable) this.setEditable(Boolean(json.editable));
101
- }
102
-
103
- /** @returns The field's name/key */
104
- getName(): string {
105
- return this.name;
106
- }
107
-
108
- /** @returns The human-readable label */
109
- getLabel(): string {
110
- return this.label;
111
- }
112
-
113
- /**
114
- * Set the human-readable label
115
- * @param label New label
116
- * @returns The updated label
117
- */
118
- setLabel(label: string): string {
119
- return this.label = label;
120
- }
121
-
122
- /** @returns Whether this field is a raw value field (text/number) */
123
- hasRaw(): boolean {
124
- return true;
125
- }
126
-
127
- /** @returns Whether this field supports connections */
128
- hasConnectable(): boolean {
129
- return false;
130
- }
131
- /** @returns Whether we have a custom editor/input look */
132
- isCustomEditor(): boolean {
133
- return false;
134
- }
135
- /**
136
- * Make the input's custom look.
137
- * @param fieldVisualInfo - The visual info of the field, mutate this if needed.
138
- */
139
- drawMyself(fieldVisualInfo: FieldVisualInfo) {
140
- return;
141
- }
142
- /** Return width & height of your field's custom editor */
143
- measureMyself() {
144
- return { width: null, height: null }; // if either is null then the renderer measures for us (meaning we have a connection or other type of raw field.)
145
- }
146
- /** @returns The stored value */
147
- getValue(): T | null {
148
- return this.value;
149
- }
150
-
151
- /**
152
- * Set the stored value
153
- * @param val New value
154
- */
155
- setValue(val: T) {
156
- this.value = val;
157
- }
158
-
159
- /** @returns The value as it should be displayed (can differ from internal value) */
160
- getDisplayValue(): T | null {
161
- return this.getValue();
162
- }
163
- toJson(deep: boolean, alreadyProcessed: { [key: string]: any }): FieldOptions {
164
- return {
165
- name: this.name,
166
- label: this.label,
167
- type: this.type,
168
- value: this.getValue()
169
- };
170
- }
171
- }
172
- /**
173
- * Used when you want just a label with no actual value. Any value related methods are no-op.
174
- */
175
- export class DummyField {
176
- label: string;
177
- name: string;
178
- type: string;
179
- node?: NodeSvg;
180
- editable: boolean;
181
- svgGroup?: G;
182
-
183
- constructor() {
184
- this.label = '';
185
- this.name = '';
186
- this.type = '';
187
- this.editable = false;
188
- }
189
- onDraw(rawBox?: FieldRawBoxData, connectionBubble?: FieldConnectionData) {
190
-
191
- }
192
- canEditConnector() {
193
- return false;
194
- }
195
- /**
196
- * Set whether or not you can edit this field.
197
- * @param val - The value to set to.
198
- */
199
- setEditable(val: boolean) {
200
- this.editable = val;
201
- if (this.node) {
202
- this.node?.workspace?.renderer?.rerenderNode?.(this.node)
203
- }
204
- }
205
- /**
206
- * Ask whether or not we can edit this field.
207
- */
208
- canEdit() {
209
- return this.editable;
210
- }
211
- /**
212
- * Set field name to something else.
213
- * @param name string
214
- * @returns the new name.
215
- */
216
- setName(name: string) {
217
- return this.name = name;
218
- }
219
- /**
220
- * Initialize the field from JSON options.
221
- * @param json FieldOptions object
222
- */
223
- fromJson(json: FieldOptions) {
224
- if (json.name !== undefined) this.name = json.name;
225
- if (json.label !== undefined) this.label = json.label;
226
- if (json.type !== undefined) this.type = json.type;
227
- }
228
- /** @returns Whether this field is a raw value field (text/number) */
229
- hasRaw(): boolean {
230
- return false;
231
- }
232
-
233
- /** @returns Whether this field supports connections */
234
- hasConnectable(): boolean {
235
- return false;
236
- }
237
- /** @returns The field's name/key */
238
- getName(): string {
239
- return this.name;
240
- }
241
-
242
- /** @returns The human-readable label */
243
- getLabel(): string {
244
- return this.label;
245
- }
246
- /**
247
- * Set the human-readable label
248
- * @param label New label
249
- * @returns The updated label
250
- */
251
- setLabel(label: string): string {
252
- return this.label = label;
253
- }
254
- /** @returns Whether we have a custom editor/input look */
255
- isCustomEditor(): boolean {
256
- return false;
257
- }
258
- /**
259
- * Make the input's custom look.
260
- * @param fieldVisualInfo - The visual info of the field, mutate this if needed.
261
- */
262
- drawMyself(fieldVisualInfo: FieldVisualInfo) {
263
- return;
264
- }
265
- /** Return width & height of your field's custom editor */
266
- measureMyself() {
267
- return { width: 0, height: 0 }; // if either is null then the renderer measures for us (meaning we have a connection or other type of raw field.)
268
- }
269
- /**
270
- * Dummy fields have no value.
271
- * @returns {null}
272
- */
273
- getValue() {
274
- return null;
275
- }
276
- /**
277
- * No-op for dummy fields
278
- */
279
- setValue(_: any) { }
280
- /** @returns The value as it should be displayed (can differ from internal value) */
281
- getDisplayValue() {
282
- return this.getValue();
283
- }
284
- toJson(deep: boolean, alreadyProcessed: { [key: string]: any }): FieldOptions {
285
- return {
286
- name: this.name,
287
- label: this.label,
288
- type: this.type,
289
- value: this.getValue()
290
- };
291
- }
292
- }
293
- /**
294
- * Base class for fields that can be connected to other fields.
295
- * @template T The type of the value stored in the field
296
- */
297
- export class ConnectableField<T = any> extends Field<T> {
298
- connection: Connection;
299
-
300
- constructor() {
301
- super();
302
- this.connection = new Connection(this, null);
303
- this.value = null as T;
304
- }
305
- hasConnectable(): boolean {
306
- return true;
307
- }
308
-
309
- hasRaw(): boolean {
310
- return false;
311
- }
312
-
313
- /** Disconnect this field from any connected Connectable */
314
- disconnect() {
315
- const to = this.connection.to;
316
- if (to instanceof NodeSvg && to?.previousConnection?.from) {
317
- to.previousConnection.from = null;
318
- this.connection.to = null;
319
- }
320
- }
321
- fromJson(json: FieldOptions, workspace?: WorkspaceSvg) {
322
- super.fromJson(json);
323
-
324
- // If the value is a serialized node, rebuild it
325
- if (json.value && typeof json.value === "object" && json.value.id) {
326
- this.value = NodeSvg._deserialize(json.value, {}, workspace) as any;
327
- this.connection.setTo(this.value as Connectable);
328
- } else if (json.value instanceof NodeSvg) {
329
- this.value = json.value as any;
330
- this.connection.setTo(this.value as Connectable);
331
- } else {
332
- this.value = json.value;
333
- }
334
-
335
- return this;
336
- }
337
- toJson(deep: boolean, alreadyProcessed: { [key: string]: any }): FieldOptions {
338
- let val: any = this.getValue();
339
- // If it's connected to a node, store it's serialized form.
340
- if (val instanceof NodeSvg) val = { node: alreadyProcessed[val.id] ? (deep ? alreadyProcessed[val.id] : val.id) : val.serialize(alreadyProcessed) };
341
-
342
- return {
343
- ...super.toJson(true, alreadyProcessed),
344
- value: val
345
- };
346
- }
347
- }
348
-
349
- /** Field storing a numeric value */
350
- export class NumberField extends Field<number> {
351
- constructor() {
352
- super();
353
- }
354
-
355
- fromJson(json: FieldOptions) {
356
- super.fromJson(json);
357
- if (json.value !== undefined) this.setValue(Number(json.value));
358
- }
359
- setValue(val: number) {
360
- this.value = Number(val);
361
- }
362
- }
363
-
364
- /** Field storing a string value */
365
- export class TextField extends Field<string> {
366
- constructor() {
367
- super();
368
- }
369
- fromJson(json: FieldOptions) {
370
- super.fromJson(json);
371
- if (json.value !== undefined) this.setValue(String(json.value));
372
- }
373
- setValue(val: string) {
374
- this.value = String(val);
375
- }
376
- }
377
-
378
- /**
379
- * Optional connectable field.
380
- * Can store either a number or string depending on fld_type.
381
- */
382
- export class OptConnectField extends ConnectableField<number | string | NodeSvg> {
383
- fldType: "number" | "string";
384
-
385
- constructor() {
386
- super();
387
- this.fldType = "string";
388
- }
389
- canEditConnector(): boolean {
390
- return true;
391
- }
392
- canEdit() {
393
- if (this.getValue() instanceof NodeSvg) {
394
- return false;
395
- }
396
- return this.editable;
397
- }
398
- getValue(): string | number | NodeSvg | null {
399
- if (this.connection && this.connection.getTo()) {
400
- return this.connection.getTo() as NodeSvg;
401
- } else {
402
- return this.value;
403
- }
404
- }
405
- hasRaw() {
406
- return true;
407
- }
408
- hasConnectable() {
409
- return true;
410
- }
411
- /**
412
- * Set field type.
413
- * @param type "number"|"string"
414
- */
415
- setFieldType(type: "number" | "string") {
416
- this.fldType = type;
417
- }
418
- /**
419
- * Initialize from JSON, respecting fld_type
420
- * @param json FieldOptions
421
- */
422
- fromJson(json: FieldOptions, workspace?: WorkspaceSvg) {
423
- super.fromJson(json, workspace);
424
- this.fldType = json.fld_type || "string";
425
- if (this.value != null && typeof this.value === this.fldType) {
426
- this.setValue(this.value as number | string);
427
- }
428
- return this;
429
- }
430
-
431
- /**
432
- * Set the value, converting to number or string depending on fld_type
433
- * @param val The new value
434
- */
435
- setValue(val: number | string) {
436
- if (this.fldType === "number") this.value = Number(val);
437
- else this.value = String(val);
438
- }
439
-
440
- /**
441
- * @returns Display value for UI purposes (never null)
442
- */
443
- getDisplayValue(): string {
444
- if (this.value == null) return this.fldType === "number" ? "0" : "";
445
- if (this.getValue() instanceof NodeSvg) return '[NODE]'; // If theres a connection
446
- return String(this.value);
447
- }
448
- toJson(deep: boolean, alreadyProcessed: { [key: string]: any }): FieldOptions {
449
- let val: any = this.getValue();
450
- // If it's connected to a node, store it's serialized form.
451
- if (val instanceof NodeSvg) val = { node: deep ? val.serialize(alreadyProcessed) : val.id };
452
-
453
- return {
454
- ...super.toJson(true, alreadyProcessed),
455
- fld_type: this.fldType,
456
- value: val
457
- };
458
- }
459
- }
460
- export type DropdownItem = { text: string, value: string } | string
461
- export class DropdownField extends Field<string> {
462
- options: DropdownItem[] | null;
463
- _selected: DropdownItem | null;
464
- _isOpen: boolean;
465
-
466
- constructor() {
467
- super();
468
- this.options = null;
469
- this._selected = null;
470
- this._isOpen = false;
471
- }
472
-
473
- onDraw(rawBox?: FieldRawBoxData) {
474
- if (!rawBox || !this.options) return;
475
-
476
- const { box, txt } = rawBox;
477
- const toggle = () => this.toggleDropdown(rawBox);
478
- box.click(toggle);
479
- txt.click(toggle);
480
- }
481
-
482
- private toggleDropdown(rawBox: FieldRawBoxData) {
483
- if (dropdownContainer.getOwner() === this) {
484
- this.closeDropdown();
485
- } else {
486
- this.openDropdown(rawBox);
487
- }
488
- rawBox.txt.text(this.getDisplayValue());
489
- }
490
-
491
- private openDropdown(rawBox: FieldRawBoxData) {
492
- if (!this.options) return;
493
-
494
- const items = this.options.map(option => ({
495
- label: typeof option === 'string' ? option : option.text,
496
- value: typeof option === 'string' ? option : option.value
497
- }));
498
-
499
- dropdownContainer.show(this, {
500
- items,
501
- width: rawBox.box.bbox().width * 2,
502
- onSelect: (value: string) => {
503
- if (!this.options) return;
504
- const original = this.options.find(
505
- e => e === value || (typeof e !== 'string' && e.value === value)
506
- );
507
- if (original) {
508
- this._selected = original;
509
- this.setValue(value);
510
- this.closeDropdown();
511
- this?.node?.workspace?.emitChange?.();
512
- this?.node?.workspace?.renderer?.rerenderNode?.(this.node);
513
- }
514
- }
515
- } as DropdownOptions);
516
- }
517
-
518
- private closeDropdown() {
519
- dropdownContainer.hideIfOwner(this);
520
- }
521
-
522
- canEdit() {
523
- return false;
524
- }
525
-
526
- getSelected() {
527
- return this._selected;
528
- }
529
- fromJson(json: FieldOptions) {
530
- super.fromJson(json);
531
- this.options = json.options as DropdownItem[] || null;
532
- this._selected = this.options?.[0] ?? null;
533
- if (this._selected) this.setValue(typeof this._selected === 'string' ? this._selected : this._selected.value);
534
- }
535
-
536
- /**
537
- * Display value of dropdowns.
538
- * @returns - Display value
539
- */
540
- getDisplayValue(): string {
541
- const text = typeof this._selected === 'string' ? this._selected : this._selected?.text || '';
542
- const arrow = dropdownContainer.getOwner() == this ? '▲' : '▼'; // toggles open/closed
543
- return text + ' ' + arrow;
544
- }
545
-
546
- /**
547
- * Set options on this dropdown
548
- * @param options - List of options
549
- */
550
- setOptions(options: DropdownItem[]) {
551
- this.options = options;
552
- this._selected = this.options?.[0] ?? null;
553
- if (this._selected) this.setValue(
554
- typeof this._selected === 'string' ? this._selected : this._selected.value
555
- );
556
- }
557
- /**
558
- *
559
- * @param deep - Whether to recursive it.
560
- * @param alreadyProcessed - Map of already serialized nodes.
561
- * @returns - Json of this field.
562
- */
563
- toJson(deep: boolean, alreadyProcessed: { [key: string]: any }): FieldOptions {
564
- return { ...super.toJson(deep, alreadyProcessed), options: this.options };
565
- }
566
- }
567
-
568
- /**
569
- * Any field instance type
570
- */
571
- export type AnyField = Field | OptConnectField | NumberField | TextField | DummyField | ConnectableField;
572
- /**
573
- * Any field class type
574
- */
575
- export type AnyFieldCls = typeof Field | typeof OptConnectField | typeof ConnectableField | typeof NumberField | typeof TextField | typeof DummyField;
576
- export const FieldMap: {
577
- field_both: typeof OptConnectField;
578
- field_string: typeof TextField;
579
- field_num: typeof NumberField;
580
- field_dummy: typeof DummyField;
581
- field_str: typeof TextField;
582
- connection: typeof ConnectableField;
583
- [key: string]: AnyFieldCls;
584
- } = {
585
- 'field_both': OptConnectField,
586
- 'field_string': TextField,
587
- 'field_num': NumberField,
588
- 'field_dummy': DummyField,
589
- 'field_str': TextField,
590
- 'connection': ConnectableField,
591
- 'dropdown': DropdownField
592
- }
593
-
594
-
595
- export default Field;