@knotx/core 0.0.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 格桑
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,418 @@
1
+ 'use strict';
2
+
3
+ const core = require('@knotx/core');
4
+ const data = require('@knotx/data');
5
+ const lodashEs = require('lodash-es');
6
+ const rxjs = require('rxjs');
7
+ const utils = require('@knotx/utils');
8
+
9
+ var __defProp$4 = Object.defineProperty;
10
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __publicField$3 = (obj, key, value) => __defNormalProp$4(obj, key + "" , value);
12
+ var Layer = /* @__PURE__ */ ((Layer2) => {
13
+ Layer2[Layer2["Canvas"] = 0] = "Canvas";
14
+ Layer2[Layer2["Background"] = 4] = "Background";
15
+ Layer2[Layer2["Edges"] = 16] = "Edges";
16
+ Layer2[Layer2["Nodes"] = 64] = "Nodes";
17
+ Layer2[Layer2["Foreground"] = 256] = "Foreground";
18
+ return Layer2;
19
+ })(Layer || {});
20
+ const _BasePlugin = class _BasePlugin {
21
+ constructor() {
22
+ __publicField$3(this, "_instanceId", _BasePlugin._instanceId++);
23
+ }
24
+ get pluginId() {
25
+ return `${this.name}:${this._instanceId}`;
26
+ }
27
+ };
28
+ __publicField$3(_BasePlugin, "_instanceId", 0);
29
+ let BasePlugin = _BasePlugin;
30
+
31
+ var __defProp$3 = Object.defineProperty;
32
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
33
+ var __publicField$2 = (obj, key, value) => __defNormalProp$3(obj, key + "" , value);
34
+ var InteractionPriority = /* @__PURE__ */ ((InteractionPriority2) => {
35
+ InteractionPriority2[InteractionPriority2["InputActive"] = 2e3] = "InputActive";
36
+ InteractionPriority2[InteractionPriority2["EntityConnectDrag"] = 1930] = "EntityConnectDrag";
37
+ InteractionPriority2[InteractionPriority2["EntityMeasureDrag"] = 1920] = "EntityMeasureDrag";
38
+ InteractionPriority2[InteractionPriority2["EntityPositionDrag"] = 1910] = "EntityPositionDrag";
39
+ InteractionPriority2[InteractionPriority2["ContinuousDrag"] = 1900] = "ContinuousDrag";
40
+ InteractionPriority2[InteractionPriority2["ContextMenu"] = 1800] = "ContextMenu";
41
+ InteractionPriority2[InteractionPriority2["KeyboardShortcut"] = 1700] = "KeyboardShortcut";
42
+ InteractionPriority2[InteractionPriority2["ClickSelection"] = 1600] = "ClickSelection";
43
+ InteractionPriority2[InteractionPriority2["DoubleClickEdit"] = 1500] = "DoubleClickEdit";
44
+ InteractionPriority2[InteractionPriority2["MarqueeSelection"] = 1300] = "MarqueeSelection";
45
+ InteractionPriority2[InteractionPriority2["LassoSelection"] = 1250] = "LassoSelection";
46
+ InteractionPriority2[InteractionPriority2["LongPress"] = 1100] = "LongPress";
47
+ InteractionPriority2[InteractionPriority2["HoverTooltip"] = 1e3] = "HoverTooltip";
48
+ InteractionPriority2[InteractionPriority2["CanvasPan"] = 900] = "CanvasPan";
49
+ InteractionPriority2[InteractionPriority2["CanvasZoom"] = 800] = "CanvasZoom";
50
+ InteractionPriority2[InteractionPriority2["MultiTouchGesture"] = 700] = "MultiTouchGesture";
51
+ return InteractionPriority2;
52
+ })(InteractionPriority || {});
53
+ class InteractionManager {
54
+ constructor() {
55
+ __publicField$2(this, "interactions", /* @__PURE__ */ new Map());
56
+ }
57
+ /**
58
+ * 获取当前活动的交互
59
+ */
60
+ get activeInteraction() {
61
+ return Array.from(this.interactions.values()).find((interaction) => interaction.active);
62
+ }
63
+ /**
64
+ * 检查是否可以交互
65
+ * @param pluginId 插件ID
66
+ * @param type 交互类型
67
+ * @param autoStartPriority 自动启动优先级
68
+ * @returns 是否可以交互
69
+ */
70
+ canInteract(pluginId, type, autoStartPriority) {
71
+ var _a, _b;
72
+ const interaction = this.interactions.get(pluginId + type);
73
+ if (!interaction && autoStartPriority) {
74
+ this.startInteraction(pluginId, type, autoStartPriority);
75
+ }
76
+ return (_b = (_a = this.interactions.get(pluginId + type)) == null ? void 0 : _a.active) != null ? _b : false;
77
+ }
78
+ /**
79
+ * 启动交互
80
+ * @param pluginId 插件ID
81
+ * @param type 交互类型
82
+ * @param priority 优先级
83
+ * @returns 取消交互的函数
84
+ */
85
+ startInteraction(pluginId, type, priority) {
86
+ this.interactions.set(pluginId + type, { pluginId, type, priority });
87
+ this.updateActive();
88
+ return () => {
89
+ this.endInteraction(pluginId, type);
90
+ };
91
+ }
92
+ /**
93
+ * 结束交互
94
+ * @param pluginId 插件ID
95
+ * @param type 交互类型
96
+ */
97
+ endInteraction(pluginId, type) {
98
+ this.interactions.delete(pluginId + type);
99
+ this.updateActive();
100
+ }
101
+ updateActive() {
102
+ const activeInteraction = Array.from(this.interactions.values()).reduce((acc, interaction) => {
103
+ if (acc) {
104
+ return interaction.priority >= acc.priority ? interaction : acc;
105
+ }
106
+ return interaction;
107
+ }, null);
108
+ this.interactions.values().forEach((interaction) => {
109
+ interaction.active = interaction === activeInteraction;
110
+ });
111
+ }
112
+ }
113
+
114
+ var __defProp$2 = Object.defineProperty;
115
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
116
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
117
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
118
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
119
+ var __spreadValues$1 = (a, b) => {
120
+ for (var prop in b || (b = {}))
121
+ if (__hasOwnProp$1.call(b, prop))
122
+ __defNormalProp$2(a, prop, b[prop]);
123
+ if (__getOwnPropSymbols$1)
124
+ for (var prop of __getOwnPropSymbols$1(b)) {
125
+ if (__propIsEnum$1.call(b, prop))
126
+ __defNormalProp$2(a, prop, b[prop]);
127
+ }
128
+ return a;
129
+ };
130
+ function getLayerRenders(plugin) {
131
+ var _a;
132
+ const components = [];
133
+ let isRenderConsumed = false;
134
+ for (const [name, layer] of Object.entries((_a = Reflect.get(plugin, utils.getSymbol("layer"))) != null ? _a : {})) {
135
+ components.push(__spreadValues$1({
136
+ plugin: plugin.name,
137
+ name: `${plugin.name}:${name}`,
138
+ render: Reflect.get(plugin, name).bind(plugin)
139
+ }, layer));
140
+ if (name === "render") {
141
+ isRenderConsumed = true;
142
+ }
143
+ }
144
+ if (!isRenderConsumed && plugin.render) {
145
+ components.push({
146
+ plugin: plugin.name,
147
+ name: `${plugin.name}:render`,
148
+ layer: core.Layer.Foreground,
149
+ render: plugin.render.bind(plugin)
150
+ });
151
+ }
152
+ return components;
153
+ }
154
+
155
+ var __defProp$1 = Object.defineProperty;
156
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
157
+ var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
158
+ class Engine {
159
+ constructor(options) {
160
+ __publicField$1(this, "runtime");
161
+ __publicField$1(this, "interactionManager", new InteractionManager());
162
+ __publicField$1(this, "nodesManager");
163
+ __publicField$1(this, "edgesManager");
164
+ __publicField$1(this, "container$", new rxjs.BehaviorSubject(null));
165
+ __publicField$1(this, "nodes$", new rxjs.BehaviorSubject([]));
166
+ __publicField$1(this, "edges$", new rxjs.BehaviorSubject([]));
167
+ __publicField$1(this, "layers$", new rxjs.BehaviorSubject(/* @__PURE__ */ new Map()));
168
+ __publicField$1(this, "plugins$", new rxjs.BehaviorSubject([]));
169
+ __publicField$1(this, "nodeRenderers$", new rxjs.BehaviorSubject(/* @__PURE__ */ new Map()));
170
+ __publicField$1(this, "edgeRenderers$", new rxjs.BehaviorSubject(/* @__PURE__ */ new Map()));
171
+ __publicField$1(this, "_pluginDataContainer", {});
172
+ var _a;
173
+ this.runtime = (_a = options.runtime) != null ? _a : {};
174
+ this.container$.next(options.container);
175
+ this.nodesManager = new data.DataManager("nodes");
176
+ this.edgesManager = new data.DataManager("edges");
177
+ this.nodesManager.getDataList$().subscribe(this.nodes$);
178
+ this.edgesManager.getDataList$().subscribe(this.edges$);
179
+ this.init(options);
180
+ }
181
+ get container() {
182
+ return this.container$.value;
183
+ }
184
+ set container(value) {
185
+ this.container$.next(value);
186
+ }
187
+ get nodes() {
188
+ return this.nodes$.value;
189
+ }
190
+ get edges() {
191
+ return this.edges$.value;
192
+ }
193
+ get layers() {
194
+ return this.layers$.value;
195
+ }
196
+ get plugins() {
197
+ return this.plugins$.value;
198
+ }
199
+ init(options) {
200
+ const { plugins = [], nodes = [], edges = [] } = options;
201
+ for (const def of plugins) {
202
+ let plugin;
203
+ if (Array.isArray(def)) {
204
+ plugin = new def[0](...def.slice(1));
205
+ } else if (typeof def === "function") {
206
+ if (Object.is(Reflect.getPrototypeOf(def), core.BasePlugin)) {
207
+ plugin = new def();
208
+ } else {
209
+ plugin = def();
210
+ }
211
+ }
212
+ if (plugin) {
213
+ if (this._pluginDataContainer[plugin.name]) {
214
+ console.warn(`Plugin ${plugin.name} already registered, please check the plugin definition`);
215
+ continue;
216
+ }
217
+ this._pluginDataContainer[plugin.name] = {};
218
+ const engineSymbol = utils.getSymbol("engine");
219
+ if (!Reflect.has(plugin, engineSymbol)) {
220
+ Reflect.set(plugin, engineSymbol, new rxjs.BehaviorSubject(null));
221
+ }
222
+ Reflect.get(plugin, engineSymbol).next(this);
223
+ const currentPlugins = this.plugins$.value || [];
224
+ this.plugins$.next([...currentPlugins, plugin]);
225
+ const currentLayers = this.layers$.value;
226
+ getLayerRenders(plugin).forEach((component) => {
227
+ const effectiveLayer = this.calculateEffectiveLayer(component.layer, component.offset);
228
+ const layerComponents = currentLayers.get(effectiveLayer) || [];
229
+ currentLayers.set(effectiveLayer, [...layerComponents, component]);
230
+ });
231
+ this.layers$.next(currentLayers);
232
+ }
233
+ }
234
+ this.plugins$.complete();
235
+ this.plugins$.value.forEach((plugin) => {
236
+ var _a;
237
+ return (_a = plugin.onInit) == null ? void 0 : _a.call(plugin);
238
+ });
239
+ this.nodesManager.init(nodes);
240
+ this.edgesManager.init(edges);
241
+ }
242
+ calculateEffectiveLayer(layer, offset) {
243
+ if (offset === void 0)
244
+ return layer;
245
+ return Math.max((layer >> 1) + 1, Math.min(layer << 1, layer + offset));
246
+ }
247
+ getLayerComponents(layer) {
248
+ return this.layers$.value.get(layer) || [];
249
+ }
250
+ addNodePipe(pipe) {
251
+ this.nodesManager.addDataOperationPipe(pipe);
252
+ }
253
+ addEdgePipe(pipe) {
254
+ this.edgesManager.addDataOperationPipe(pipe);
255
+ }
256
+ dispatchNodeOperation(operation) {
257
+ this.nodesManager.dispatch(operation);
258
+ }
259
+ dispatchEdgeOperation(operation) {
260
+ this.edgesManager.dispatch(operation);
261
+ }
262
+ getNode(id) {
263
+ return this.nodesManager.getData(id);
264
+ }
265
+ getNodeDraft(id) {
266
+ return this.nodesManager.getCurrentDraftData(id);
267
+ }
268
+ getNodes() {
269
+ return this.nodesManager.getDataList();
270
+ }
271
+ getNodeRenderer(type) {
272
+ return this.nodeRenderers$.value.get(type);
273
+ }
274
+ getEdge(id) {
275
+ return this.edgesManager.getData(id);
276
+ }
277
+ getEdgeDraft(id) {
278
+ return this.edgesManager.getCurrentDraftData(id);
279
+ }
280
+ getEdges() {
281
+ return this.edgesManager.getDataList();
282
+ }
283
+ getEdgeRenderer(type) {
284
+ return this.edgeRenderers$.value.get(type);
285
+ }
286
+ registerPluginData(pluginName, property, data) {
287
+ lodashEs.set(this, ["_pluginDataContainer", pluginName, property], data);
288
+ }
289
+ registerNodeRenderer(type, renderer) {
290
+ const nodeRenderers = this.nodeRenderers$.value;
291
+ nodeRenderers.set(type, renderer);
292
+ this.nodeRenderers$.next(nodeRenderers);
293
+ }
294
+ registerEdgeRenderer(type, renderer) {
295
+ const edgeRenderers = this.edgeRenderers$.value;
296
+ edgeRenderers.set(type, renderer);
297
+ this.edgeRenderers$.next(edgeRenderers);
298
+ }
299
+ canInteract(pluginId, type, autoStartPriority) {
300
+ return this.interactionManager.canInteract(pluginId, type, autoStartPriority);
301
+ }
302
+ startInteraction(pluginId, type, priority) {
303
+ this.interactionManager.startInteraction(pluginId, type, priority);
304
+ }
305
+ endInteraction(pluginId, type) {
306
+ this.interactionManager.endInteraction(pluginId, type);
307
+ }
308
+ destroy() {
309
+ this.plugins$.value.forEach((plugin) => {
310
+ var _a;
311
+ (_a = plugin.onDestroy) == null ? void 0 : _a.call(plugin);
312
+ });
313
+ this.plugins$.next([]);
314
+ this.container$.complete();
315
+ this.layers$.complete();
316
+ this.nodes$.complete();
317
+ this.edges$.complete();
318
+ this.nodeRenderers$.complete();
319
+ this.edgeRenderers$.complete();
320
+ }
321
+ }
322
+
323
+ var __defProp = Object.defineProperty;
324
+ var __defProps = Object.defineProperties;
325
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
326
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
327
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
328
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
329
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
330
+ var __spreadValues = (a, b) => {
331
+ for (var prop in b || (b = {}))
332
+ if (__hasOwnProp.call(b, prop))
333
+ __defNormalProp(a, prop, b[prop]);
334
+ if (__getOwnPropSymbols)
335
+ for (var prop of __getOwnPropSymbols(b)) {
336
+ if (__propIsEnum.call(b, prop))
337
+ __defNormalProp(a, prop, b[prop]);
338
+ }
339
+ return a;
340
+ };
341
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
342
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
343
+ const _Runtime = class _Runtime {
344
+ constructor() {
345
+ /**
346
+ * 当前上下文
347
+ */
348
+ __publicField(this, "currentContext");
349
+ }
350
+ /**
351
+ * 在上下文中运行函数
352
+ * @param type 上下文类型
353
+ * @param handler 处理函数
354
+ * @param contextValue 上下文值
355
+ * @returns 处理函数的返回值
356
+ */
357
+ runInContext(type, handler, contextValue) {
358
+ this.currentContext = {
359
+ type,
360
+ value: contextValue
361
+ };
362
+ const result = handler();
363
+ this.currentContext = void 0;
364
+ return result;
365
+ }
366
+ /**
367
+ * 值处理器
368
+ */
369
+ getValue(engine, options) {
370
+ var _a, _b;
371
+ const { type: context, value: contextValue } = this.currentContext || {};
372
+ const getValue = ((_a = engine.runtime[context]) == null ? void 0 : _a.getValue) || _Runtime.DEFAULT_VALUE_HANDLER;
373
+ const value = getValue(
374
+ ((_b = options.matcher) == null ? void 0 : _b.call(options, engine)) || lodashEs.get(engine, options.paths),
375
+ __spreadProps(__spreadValues({}, options), { context: contextValue })
376
+ );
377
+ return value;
378
+ }
379
+ static getInstance() {
380
+ if (!this.instance) {
381
+ this.instance = new _Runtime();
382
+ }
383
+ return this.instance;
384
+ }
385
+ };
386
+ /**
387
+ * 获取 Runtime 实例
388
+ * @returns Runtime 实例
389
+ */
390
+ __publicField(_Runtime, "instance");
391
+ /**
392
+ * 默认值处理器
393
+ */
394
+ __publicField(_Runtime, "DEFAULT_VALUE_HANDLER", (value, { selector = rxjs.identity, context }) => selector(value instanceof rxjs.BehaviorSubject ? value.value : value, context));
395
+ let Runtime = _Runtime;
396
+
397
+ function use(hook, context) {
398
+ return Runtime.getInstance().runInContext("render", hook, context);
399
+ }
400
+
401
+ exports.BasePlugin = BasePlugin;
402
+ exports.Engine = Engine;
403
+ exports.InteractionManager = InteractionManager;
404
+ exports.InteractionPriority = InteractionPriority;
405
+ exports.Layer = Layer;
406
+ exports.Runtime = Runtime;
407
+ exports.getLayerRenders = getLayerRenders;
408
+ exports.use = use;
409
+ Object.prototype.hasOwnProperty.call(utils, '__proto__') &&
410
+ !Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
411
+ Object.defineProperty(exports, '__proto__', {
412
+ enumerable: true,
413
+ value: utils['__proto__']
414
+ });
415
+
416
+ Object.keys(utils).forEach(function (k) {
417
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = utils[k];
418
+ });