@knotx/plugins-selection 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,193 @@
1
+ 'use strict';
2
+
3
+ const core = require('@knotx/core');
4
+ const decorators = require('@knotx/decorators');
5
+ const pluginsBaseRender = require('@knotx/plugins-base-render');
6
+ const SelectionArea = require('@viselect/vanilla');
7
+ const rxjs = require('rxjs');
8
+
9
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
10
+
11
+ const SelectionArea__default = /*#__PURE__*/_interopDefaultCompat(SelectionArea);
12
+
13
+ var __create = Object.create;
14
+ var __defProp = Object.defineProperty;
15
+ var __defProps = Object.defineProperties;
16
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
17
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
18
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
19
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
20
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
21
+ var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
22
+ var __typeError = (msg) => {
23
+ throw TypeError(msg);
24
+ };
25
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
26
+ var __spreadValues = (a, b) => {
27
+ for (var prop in b || (b = {}))
28
+ if (__hasOwnProp.call(b, prop))
29
+ __defNormalProp(a, prop, b[prop]);
30
+ if (__getOwnPropSymbols)
31
+ for (var prop of __getOwnPropSymbols(b)) {
32
+ if (__propIsEnum.call(b, prop))
33
+ __defNormalProp(a, prop, b[prop]);
34
+ }
35
+ return a;
36
+ };
37
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
38
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
39
+ var __decoratorStart = (base) => {
40
+ var _a2;
41
+ return [, , , __create((_a2 = base == null ? void 0 : base[__knownSymbol("metadata")]) != null ? _a2 : null)];
42
+ };
43
+ var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
44
+ var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
45
+ var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
46
+ var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
47
+ var __runInitializers = (array, flags, self, value) => {
48
+ for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
49
+ return value;
50
+ };
51
+ var __decorateElement = (array, flags, name, decorators, target, extra) => {
52
+ var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
53
+ var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
54
+ var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
55
+ var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
56
+ return __privateGet(this, extra);
57
+ }, set [name](x) {
58
+ return __privateSet(this, extra, x);
59
+ } }, name));
60
+ k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
61
+ for (var i = decorators.length - 1; i >= 0; i--) {
62
+ ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
63
+ if (k) {
64
+ ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
65
+ if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
66
+ if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
67
+ }
68
+ it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
69
+ if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
70
+ else if (typeof it !== "object" || it === null) __typeError("Object expected");
71
+ else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
72
+ }
73
+ return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
74
+ };
75
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
76
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
77
+ var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
78
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
79
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
80
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
81
+ var _destroy_dec, _init_dec, _canInteract_dec, _endInteraction_dec, _startInteraction_dec, _registerPluginData_dec, _a, _init;
82
+ const _Selection = class _Selection extends (_a = core.BasePlugin, _registerPluginData_dec = [decorators.inject.registerPluginData()], _startInteraction_dec = [decorators.inject.startInteraction()], _endInteraction_dec = [decorators.inject.endInteraction()], _canInteract_dec = [decorators.inject.canInteract()], _init_dec = [decorators.OnInit], _destroy_dec = [decorators.OnDestroy], _a) {
83
+ constructor(options = {
84
+ boundaries: [`.${core.bem("canvas", "wrapper")}`]
85
+ }) {
86
+ super();
87
+ this.options = options;
88
+ __runInitializers(_init, 5, this);
89
+ __publicField(this, "name", "selection");
90
+ __publicField(this, "selection");
91
+ __publicField(this, "selectedNodeIds$", new rxjs.BehaviorSubject([]));
92
+ __publicField(this, "registerPluginData", __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this);
93
+ __publicField(this, "startInteraction", __runInitializers(_init, 12, this)), __runInitializers(_init, 15, this);
94
+ __publicField(this, "endInteraction", __runInitializers(_init, 16, this)), __runInitializers(_init, 19, this);
95
+ __publicField(this, "canInteract", __runInitializers(_init, 20, this)), __runInitializers(_init, 23, this);
96
+ }
97
+ init() {
98
+ this.registerPluginData("selection", "selectedNodeIds", this.selectedNodeIds$);
99
+ this.createSelection();
100
+ }
101
+ createSelection() {
102
+ const nodeClassName = core.addBemModifier(pluginsBaseRender.BaseRender.NODE_WRAPPER_CLASSNAME, "selected");
103
+ const selectionAreaClass = _Selection.SELECTION_AREA_CLASSNAME;
104
+ const selectables = [
105
+ `.${pluginsBaseRender.BaseRender.NODE_WRAPPER_CLASSNAME}`
106
+ ];
107
+ const endSelect = () => {
108
+ this.endInteraction(this.pluginId, "select");
109
+ };
110
+ this.selection = new SelectionArea__default(__spreadProps(__spreadValues({}, this.options), { selectionAreaClass, selectables })).on("start", ({ store, event, selection }) => {
111
+ if (!(event == null ? void 0 : event.ctrlKey) && !(event == null ? void 0 : event.metaKey)) {
112
+ store.stored.forEach((el) => {
113
+ if (el.className.includes(pluginsBaseRender.BaseRender.NODE_WRAPPER_CLASSNAME)) {
114
+ el.classList.remove(nodeClassName);
115
+ }
116
+ });
117
+ selection.clearSelection();
118
+ this.selectedNodeIds$.next([]);
119
+ }
120
+ this.startInteraction(this.pluginId, "select", core.InteractionPriority.MarqueeSelection);
121
+ }).on("move", ({ store: { changed: { added, removed } }, selection }) => {
122
+ if (!this.canInteract(this.pluginId, "select", core.InteractionPriority.MarqueeSelection)) {
123
+ selection.cancel();
124
+ endSelect();
125
+ return;
126
+ }
127
+ const addedNodeIds = [];
128
+ added.forEach((el) => {
129
+ if (el.className.includes(pluginsBaseRender.BaseRender.NODE_WRAPPER_CLASSNAME)) {
130
+ el.classList.add(nodeClassName);
131
+ const nodeId = el.getAttribute("data-node-id");
132
+ if (nodeId)
133
+ addedNodeIds.push(nodeId);
134
+ }
135
+ });
136
+ const removedNodeIds = [];
137
+ removed.forEach((el) => {
138
+ if (el.className.includes(pluginsBaseRender.BaseRender.NODE_WRAPPER_CLASSNAME)) {
139
+ el.classList.remove(nodeClassName);
140
+ const nodeId = el.getAttribute("data-node-id");
141
+ if (nodeId)
142
+ removedNodeIds.push(nodeId);
143
+ }
144
+ });
145
+ this.updateSelection(addedNodeIds, removedNodeIds);
146
+ }).on("stop", endSelect);
147
+ }
148
+ // 更新选中的节点和组
149
+ updateSelection(addedNodeIds = [], removedNodeIds = []) {
150
+ const currentNodeIds = this.selectedNodeIds$.getValue();
151
+ const newNodeIds = [
152
+ ...currentNodeIds.filter((id) => !removedNodeIds.includes(id)),
153
+ ...addedNodeIds.filter((id) => !currentNodeIds.includes(id))
154
+ ];
155
+ this.selectedNodeIds$.next(newNodeIds);
156
+ }
157
+ // 获取选中的节点ID
158
+ getSelectedNodeIds() {
159
+ return this.selectedNodeIds$.getValue();
160
+ }
161
+ // 选择节点
162
+ selectNodes(nodeIds, clearPrevious = true) {
163
+ if (clearPrevious) {
164
+ this.selectedNodeIds$.next(nodeIds);
165
+ } else {
166
+ const currentNodeIds = this.selectedNodeIds$.getValue();
167
+ this.selectedNodeIds$.next([
168
+ ...currentNodeIds,
169
+ ...nodeIds.filter((id) => !currentNodeIds.includes(id))
170
+ ]);
171
+ }
172
+ }
173
+ // 清除选择
174
+ clearSelection() {
175
+ this.selectedNodeIds$.next([]);
176
+ }
177
+ destroy() {
178
+ var _a2;
179
+ (_a2 = this.selection) == null ? void 0 : _a2.destroy();
180
+ }
181
+ };
182
+ _init = __decoratorStart(_a);
183
+ __decorateElement(_init, 1, "init", _init_dec, _Selection);
184
+ __decorateElement(_init, 1, "destroy", _destroy_dec, _Selection);
185
+ __decorateElement(_init, 5, "registerPluginData", _registerPluginData_dec, _Selection);
186
+ __decorateElement(_init, 5, "startInteraction", _startInteraction_dec, _Selection);
187
+ __decorateElement(_init, 5, "endInteraction", _endInteraction_dec, _Selection);
188
+ __decorateElement(_init, 5, "canInteract", _canInteract_dec, _Selection);
189
+ __decoratorMetadata(_init, _Selection);
190
+ __publicField(_Selection, "SELECTION_AREA_CLASSNAME", core.bem("selection-area"));
191
+ let Selection = _Selection;
192
+
193
+ exports.Selection = Selection;
@@ -0,0 +1,31 @@
1
+ import { PartialSelectionOptions } from '@viselect/vanilla';
2
+ import { BasePlugin } from '@knotx/core';
3
+
4
+ declare module '@knotx/core' {
5
+ interface PluginData {
6
+ selection: {
7
+ selectedNodeIds: string[];
8
+ };
9
+ }
10
+ }
11
+ declare class Selection extends BasePlugin<'selection'> {
12
+ private options;
13
+ name: "selection";
14
+ private selection;
15
+ private selectedNodeIds$;
16
+ private registerPluginData;
17
+ private startInteraction;
18
+ private endInteraction;
19
+ private canInteract;
20
+ constructor(options?: PartialSelectionOptions);
21
+ protected init(): void;
22
+ private createSelection;
23
+ private updateSelection;
24
+ getSelectedNodeIds(): string[];
25
+ selectNodes(nodeIds: string[], clearPrevious?: boolean): void;
26
+ clearSelection(): void;
27
+ destroy(): void;
28
+ static SELECTION_AREA_CLASSNAME: string;
29
+ }
30
+
31
+ export { Selection };
@@ -0,0 +1,31 @@
1
+ import { PartialSelectionOptions } from '@viselect/vanilla';
2
+ import { BasePlugin } from '@knotx/core';
3
+
4
+ declare module '@knotx/core' {
5
+ interface PluginData {
6
+ selection: {
7
+ selectedNodeIds: string[];
8
+ };
9
+ }
10
+ }
11
+ declare class Selection extends BasePlugin<'selection'> {
12
+ private options;
13
+ name: "selection";
14
+ private selection;
15
+ private selectedNodeIds$;
16
+ private registerPluginData;
17
+ private startInteraction;
18
+ private endInteraction;
19
+ private canInteract;
20
+ constructor(options?: PartialSelectionOptions);
21
+ protected init(): void;
22
+ private createSelection;
23
+ private updateSelection;
24
+ getSelectedNodeIds(): string[];
25
+ selectNodes(nodeIds: string[], clearPrevious?: boolean): void;
26
+ clearSelection(): void;
27
+ destroy(): void;
28
+ static SELECTION_AREA_CLASSNAME: string;
29
+ }
30
+
31
+ export { Selection };
@@ -0,0 +1,31 @@
1
+ import { PartialSelectionOptions } from '@viselect/vanilla';
2
+ import { BasePlugin } from '@knotx/core';
3
+
4
+ declare module '@knotx/core' {
5
+ interface PluginData {
6
+ selection: {
7
+ selectedNodeIds: string[];
8
+ };
9
+ }
10
+ }
11
+ declare class Selection extends BasePlugin<'selection'> {
12
+ private options;
13
+ name: "selection";
14
+ private selection;
15
+ private selectedNodeIds$;
16
+ private registerPluginData;
17
+ private startInteraction;
18
+ private endInteraction;
19
+ private canInteract;
20
+ constructor(options?: PartialSelectionOptions);
21
+ protected init(): void;
22
+ private createSelection;
23
+ private updateSelection;
24
+ getSelectedNodeIds(): string[];
25
+ selectNodes(nodeIds: string[], clearPrevious?: boolean): void;
26
+ clearSelection(): void;
27
+ destroy(): void;
28
+ static SELECTION_AREA_CLASSNAME: string;
29
+ }
30
+
31
+ export { Selection };
package/dist/index.mjs ADDED
@@ -0,0 +1,187 @@
1
+ import { bem, addBemModifier, InteractionPriority, BasePlugin } from '@knotx/core';
2
+ import { inject, OnInit, OnDestroy } from '@knotx/decorators';
3
+ import { BaseRender } from '@knotx/plugins-base-render';
4
+ import SelectionArea from '@viselect/vanilla';
5
+ import { BehaviorSubject } from 'rxjs';
6
+
7
+ var __create = Object.create;
8
+ var __defProp = Object.defineProperty;
9
+ var __defProps = Object.defineProperties;
10
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
11
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
12
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
15
+ var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
16
+ var __typeError = (msg) => {
17
+ throw TypeError(msg);
18
+ };
19
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
20
+ var __spreadValues = (a, b) => {
21
+ for (var prop in b || (b = {}))
22
+ if (__hasOwnProp.call(b, prop))
23
+ __defNormalProp(a, prop, b[prop]);
24
+ if (__getOwnPropSymbols)
25
+ for (var prop of __getOwnPropSymbols(b)) {
26
+ if (__propIsEnum.call(b, prop))
27
+ __defNormalProp(a, prop, b[prop]);
28
+ }
29
+ return a;
30
+ };
31
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
32
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
33
+ var __decoratorStart = (base) => {
34
+ var _a2;
35
+ return [, , , __create((_a2 = base == null ? void 0 : base[__knownSymbol("metadata")]) != null ? _a2 : null)];
36
+ };
37
+ var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
38
+ var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
39
+ var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
40
+ var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
41
+ var __runInitializers = (array, flags, self, value) => {
42
+ for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
43
+ return value;
44
+ };
45
+ var __decorateElement = (array, flags, name, decorators, target, extra) => {
46
+ var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
47
+ var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
48
+ var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
49
+ var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
50
+ return __privateGet(this, extra);
51
+ }, set [name](x) {
52
+ return __privateSet(this, extra, x);
53
+ } }, name));
54
+ k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
55
+ for (var i = decorators.length - 1; i >= 0; i--) {
56
+ ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
57
+ if (k) {
58
+ ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
59
+ if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
60
+ if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
61
+ }
62
+ it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
63
+ if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
64
+ else if (typeof it !== "object" || it === null) __typeError("Object expected");
65
+ else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
66
+ }
67
+ return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
68
+ };
69
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
70
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
71
+ var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
72
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
73
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
74
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
75
+ var _destroy_dec, _init_dec, _canInteract_dec, _endInteraction_dec, _startInteraction_dec, _registerPluginData_dec, _a, _init;
76
+ const _Selection = class _Selection extends (_a = BasePlugin, _registerPluginData_dec = [inject.registerPluginData()], _startInteraction_dec = [inject.startInteraction()], _endInteraction_dec = [inject.endInteraction()], _canInteract_dec = [inject.canInteract()], _init_dec = [OnInit], _destroy_dec = [OnDestroy], _a) {
77
+ constructor(options = {
78
+ boundaries: [`.${bem("canvas", "wrapper")}`]
79
+ }) {
80
+ super();
81
+ this.options = options;
82
+ __runInitializers(_init, 5, this);
83
+ __publicField(this, "name", "selection");
84
+ __publicField(this, "selection");
85
+ __publicField(this, "selectedNodeIds$", new BehaviorSubject([]));
86
+ __publicField(this, "registerPluginData", __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this);
87
+ __publicField(this, "startInteraction", __runInitializers(_init, 12, this)), __runInitializers(_init, 15, this);
88
+ __publicField(this, "endInteraction", __runInitializers(_init, 16, this)), __runInitializers(_init, 19, this);
89
+ __publicField(this, "canInteract", __runInitializers(_init, 20, this)), __runInitializers(_init, 23, this);
90
+ }
91
+ init() {
92
+ this.registerPluginData("selection", "selectedNodeIds", this.selectedNodeIds$);
93
+ this.createSelection();
94
+ }
95
+ createSelection() {
96
+ const nodeClassName = addBemModifier(BaseRender.NODE_WRAPPER_CLASSNAME, "selected");
97
+ const selectionAreaClass = _Selection.SELECTION_AREA_CLASSNAME;
98
+ const selectables = [
99
+ `.${BaseRender.NODE_WRAPPER_CLASSNAME}`
100
+ ];
101
+ const endSelect = () => {
102
+ this.endInteraction(this.pluginId, "select");
103
+ };
104
+ this.selection = new SelectionArea(__spreadProps(__spreadValues({}, this.options), { selectionAreaClass, selectables })).on("start", ({ store, event, selection }) => {
105
+ if (!(event == null ? void 0 : event.ctrlKey) && !(event == null ? void 0 : event.metaKey)) {
106
+ store.stored.forEach((el) => {
107
+ if (el.className.includes(BaseRender.NODE_WRAPPER_CLASSNAME)) {
108
+ el.classList.remove(nodeClassName);
109
+ }
110
+ });
111
+ selection.clearSelection();
112
+ this.selectedNodeIds$.next([]);
113
+ }
114
+ this.startInteraction(this.pluginId, "select", InteractionPriority.MarqueeSelection);
115
+ }).on("move", ({ store: { changed: { added, removed } }, selection }) => {
116
+ if (!this.canInteract(this.pluginId, "select", InteractionPriority.MarqueeSelection)) {
117
+ selection.cancel();
118
+ endSelect();
119
+ return;
120
+ }
121
+ const addedNodeIds = [];
122
+ added.forEach((el) => {
123
+ if (el.className.includes(BaseRender.NODE_WRAPPER_CLASSNAME)) {
124
+ el.classList.add(nodeClassName);
125
+ const nodeId = el.getAttribute("data-node-id");
126
+ if (nodeId)
127
+ addedNodeIds.push(nodeId);
128
+ }
129
+ });
130
+ const removedNodeIds = [];
131
+ removed.forEach((el) => {
132
+ if (el.className.includes(BaseRender.NODE_WRAPPER_CLASSNAME)) {
133
+ el.classList.remove(nodeClassName);
134
+ const nodeId = el.getAttribute("data-node-id");
135
+ if (nodeId)
136
+ removedNodeIds.push(nodeId);
137
+ }
138
+ });
139
+ this.updateSelection(addedNodeIds, removedNodeIds);
140
+ }).on("stop", endSelect);
141
+ }
142
+ // 更新选中的节点和组
143
+ updateSelection(addedNodeIds = [], removedNodeIds = []) {
144
+ const currentNodeIds = this.selectedNodeIds$.getValue();
145
+ const newNodeIds = [
146
+ ...currentNodeIds.filter((id) => !removedNodeIds.includes(id)),
147
+ ...addedNodeIds.filter((id) => !currentNodeIds.includes(id))
148
+ ];
149
+ this.selectedNodeIds$.next(newNodeIds);
150
+ }
151
+ // 获取选中的节点ID
152
+ getSelectedNodeIds() {
153
+ return this.selectedNodeIds$.getValue();
154
+ }
155
+ // 选择节点
156
+ selectNodes(nodeIds, clearPrevious = true) {
157
+ if (clearPrevious) {
158
+ this.selectedNodeIds$.next(nodeIds);
159
+ } else {
160
+ const currentNodeIds = this.selectedNodeIds$.getValue();
161
+ this.selectedNodeIds$.next([
162
+ ...currentNodeIds,
163
+ ...nodeIds.filter((id) => !currentNodeIds.includes(id))
164
+ ]);
165
+ }
166
+ }
167
+ // 清除选择
168
+ clearSelection() {
169
+ this.selectedNodeIds$.next([]);
170
+ }
171
+ destroy() {
172
+ var _a2;
173
+ (_a2 = this.selection) == null ? void 0 : _a2.destroy();
174
+ }
175
+ };
176
+ _init = __decoratorStart(_a);
177
+ __decorateElement(_init, 1, "init", _init_dec, _Selection);
178
+ __decorateElement(_init, 1, "destroy", _destroy_dec, _Selection);
179
+ __decorateElement(_init, 5, "registerPluginData", _registerPluginData_dec, _Selection);
180
+ __decorateElement(_init, 5, "startInteraction", _startInteraction_dec, _Selection);
181
+ __decorateElement(_init, 5, "endInteraction", _endInteraction_dec, _Selection);
182
+ __decorateElement(_init, 5, "canInteract", _canInteract_dec, _Selection);
183
+ __decoratorMetadata(_init, _Selection);
184
+ __publicField(_Selection, "SELECTION_AREA_CLASSNAME", bem("selection-area"));
185
+ let Selection = _Selection;
186
+
187
+ export { Selection };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@knotx/plugins-selection",
3
+ "type": "module",
4
+ "version": "0.0.0",
5
+ "description": "Selection Plugin for Knotx",
6
+ "author": "boenfu",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/boenfu/knotx#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/boenfu/knotx.git",
12
+ "directory": "packages/plugins-selection"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "sideEffects": false,
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.mjs",
22
+ "require": "./dist/index.cjs"
23
+ }
24
+ },
25
+ "main": "./dist/index.cjs",
26
+ "module": "./dist/index.mjs",
27
+ "types": "./dist/index.d.ts",
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "dependencies": {
32
+ "@viselect/vanilla": "^3.9.0",
33
+ "rxjs": "^7.8.1",
34
+ "@knotx/core": "0.0.0",
35
+ "@knotx/decorators": "0.0.0",
36
+ "@knotx/plugins-base-render": "0.0.0"
37
+ },
38
+ "devDependencies": {
39
+ "@knotx/eslint-config": "0.0.0",
40
+ "@knotx/typescript-config": "0.0.0"
41
+ },
42
+ "scripts": {
43
+ "build": "unbuild --failOnWarn=false",
44
+ "dev": "unbuild --stub",
45
+ "lint": "eslint .",
46
+ "typecheck": "tsc --noEmit"
47
+ }
48
+ }