@knotx/plugins-selection 0.3.2 → 0.3.4

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/dist/index.cjs CHANGED
@@ -73,8 +73,28 @@ var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use
73
73
  var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
74
74
  var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
75
75
  var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
76
- var _destroy_dec, _clearSelection_dec, _getSelected_dec, _init_dec, _interaction_dec, _registerRules_dec, _selected_dec, _a, _init;
77
- class Selection extends (_a = core.BasePlugin, _selected_dec = [decorators.register("selected")], _registerRules_dec = [decorators.register("registerRules")], _interaction_dec = [decorators.inject.interaction()], _init_dec = [decorators.OnInit], _getSelected_dec = [decorators.tool("Get selected items", {})], _clearSelection_dec = [decorators.tool("Clear selection", {})], _destroy_dec = [decorators.OnDestroy], _a) {
76
+ var _destroy_dec, _clearSelection_dec, _select_dec, _getSelected_dec, _init_dec, _interaction_dec, _setElementOverlap_dec, _setClearSelectionBeforeStart_dec, _clearSelectionBeforeStart_dec, _setRuleEnabled_dec, _registerRules_dec, _selected_dec, _a, _init;
77
+ class Selection extends (_a = core.BasePlugin, _selected_dec = [decorators.register("selected")], _registerRules_dec = [decorators.register("registerRules")], _setRuleEnabled_dec = [decorators.register("setRuleEnabled")], _clearSelectionBeforeStart_dec = [decorators.observable()], _setClearSelectionBeforeStart_dec = [decorators.register("setClearSelectionBeforeStart")], _setElementOverlap_dec = [decorators.register("setElementOverlap")], _interaction_dec = [decorators.inject.interaction()], _init_dec = [decorators.OnInit], _getSelected_dec = [decorators.tool("Get selected items", {})], _select_dec = [decorators.tool("Items that match the registered rules will be selected, manifested as the item showing a selected style.", {
78
+ type: "array",
79
+ items: {
80
+ type: "object",
81
+ properties: {
82
+ type: {
83
+ type: "string",
84
+ description: "item type such as node, group, etc."
85
+ },
86
+ id: {
87
+ type: "string"
88
+ }
89
+ },
90
+ additionalProperties: false,
91
+ required: [
92
+ "id",
93
+ "type"
94
+ ]
95
+ },
96
+ $schema: "http://json-schema.org/draft-07/schema#"
97
+ })], _clearSelection_dec = [decorators.tool("Clear selection", {})], _destroy_dec = [decorators.OnDestroy], _a) {
78
98
  constructor() {
79
99
  super(...arguments);
80
100
  __runInitializers(_init, 5, this);
@@ -89,17 +109,33 @@ class Selection extends (_a = core.BasePlugin, _selected_dec = [decorators.regis
89
109
  __publicField(this, "selected", __runInitializers(_init, 8, this, [])), __runInitializers(_init, 11, this);
90
110
  __publicField(this, "registerRules", __runInitializers(_init, 12, this, (rules) => {
91
111
  rules.forEach((rule) => {
92
- this.ruleMap.set(rule.className, rule);
112
+ this.ruleMap.set(rule.type, rule);
93
113
  });
94
114
  return () => {
95
115
  rules.forEach((rule) => {
96
- if (this.ruleMap.get(rule.className) === rule) {
97
- this.ruleMap.delete(rule.className);
116
+ if (this.ruleMap.get(rule.type) === rule) {
117
+ this.ruleMap.delete(rule.type);
98
118
  }
99
119
  });
100
120
  };
101
121
  })), __runInitializers(_init, 15, this);
102
- __publicField(this, "interaction", __runInitializers(_init, 16, this)), __runInitializers(_init, 19, this);
122
+ __publicField(this, "setRuleEnabled", __runInitializers(_init, 16, this, (type, enabled) => {
123
+ const rule = this.ruleMap.get(type);
124
+ if (rule) {
125
+ rule.enabled = enabled;
126
+ }
127
+ })), __runInitializers(_init, 19, this);
128
+ __publicField(this, "clearSelectionBeforeStart", __runInitializers(_init, 20, this, true)), __runInitializers(_init, 23, this);
129
+ __publicField(this, "setClearSelectionBeforeStart", __runInitializers(_init, 24, this, (clearSelectionBeforeStart) => {
130
+ this.clearSelectionBeforeStart = clearSelectionBeforeStart;
131
+ })), __runInitializers(_init, 27, this);
132
+ __publicField(this, "setElementOverlap", __runInitializers(_init, 28, this, (overlap) => {
133
+ try {
134
+ this.selection._options.behaviour.overlap = overlap;
135
+ } catch (e) {
136
+ }
137
+ })), __runInitializers(_init, 31, this);
138
+ __publicField(this, "interaction", __runInitializers(_init, 32, this)), __runInitializers(_init, 35, this);
103
139
  __publicField(this, "onBeforeStart", (_) => {
104
140
  });
105
141
  __publicField(this, "onBeforeDrag", () => {
@@ -110,13 +146,16 @@ class Selection extends (_a = core.BasePlugin, _selected_dec = [decorators.regis
110
146
  var _a2;
111
147
  this.interaction.start(this.pluginId, "select", core.InteractionPriority.MarqueeSelection);
112
148
  if (!(event == null ? void 0 : event.ctrlKey) && !(event == null ? void 0 : event.metaKey) && !(event == null ? void 0 : event.shiftKey)) {
113
- store.stored.forEach((element) => {
114
- const rule = this.getElementRule(element);
115
- if (rule == null ? void 0 : rule.activeClassName) {
116
- element.classList.remove(rule.activeClassName);
117
- }
118
- });
119
- (_a2 = this.selection) == null ? void 0 : _a2.clearSelection(true, true);
149
+ if (this.clearSelectionBeforeStart) {
150
+ store.stored.forEach((element) => {
151
+ const rule = this.getElementRule(element);
152
+ if (rule == null ? void 0 : rule.activeClassName) {
153
+ element.classList.remove(rule.activeClassName);
154
+ }
155
+ element.removeAttribute("data-selected");
156
+ });
157
+ (_a2 = this.selection) == null ? void 0 : _a2.clearSelection(true, true);
158
+ }
120
159
  }
121
160
  });
122
161
  __publicField(this, "onMove", ({ store: { stored, changed: { added, removed } }, selection }) => {
@@ -151,6 +190,7 @@ class Selection extends (_a = core.BasePlugin, _selected_dec = [decorators.regis
151
190
  if (rule.activeClassName) {
152
191
  element.classList.remove(rule.activeClassName);
153
192
  }
193
+ element.removeAttribute("data-selected");
154
194
  });
155
195
  added.forEach((element) => {
156
196
  const rule = this.getElementRule(element);
@@ -160,7 +200,7 @@ class Selection extends (_a = core.BasePlugin, _selected_dec = [decorators.regis
160
200
  const type = rule.type;
161
201
  const id = rule.idGetter(element);
162
202
  for (const [, rule2] of this.ruleMap) {
163
- if (rule2.onSelected) {
203
+ if (rule2.enabled !== false && rule2.onSelected) {
164
204
  if (!rule2.onSelected(__spreadValues({ element, type, id }, context))) {
165
205
  selection.deselect(element, true);
166
206
  return;
@@ -170,6 +210,7 @@ class Selection extends (_a = core.BasePlugin, _selected_dec = [decorators.regis
170
210
  if (rule.activeClassName) {
171
211
  element.classList.add(rule.activeClassName);
172
212
  }
213
+ element.setAttribute("data-selected", "true");
173
214
  });
174
215
  });
175
216
  __publicField(this, "onStop", ({ event }) => {
@@ -185,7 +226,7 @@ class Selection extends (_a = core.BasePlugin, _selected_dec = [decorators.regis
185
226
  __publicField(this, "updateSelected", () => {
186
227
  var _a2;
187
228
  const selected = [];
188
- for (const element of ((_a2 = this.selection) == null ? void 0 : _a2.getSelection()) || []) {
229
+ for (const element of new Set(((_a2 = this.selection) == null ? void 0 : _a2.getSelection()) || [])) {
189
230
  const rule = this.getElementRule(element);
190
231
  if (rule) {
191
232
  selected.push({ type: rule.type, id: rule.idGetter(element) });
@@ -204,10 +245,10 @@ class Selection extends (_a = core.BasePlugin, _selected_dec = [decorators.regis
204
245
  activeClassName: core.bem("node", "wrapper", "selected"),
205
246
  idGetter: (element) => element.getAttribute("data-node-id") || ""
206
247
  };
207
- this.ruleMap.set(nodeRule.className, nodeRule);
248
+ this.ruleMap.set(nodeRule.type, nodeRule);
208
249
  }
209
250
  for (const rule of config.rules || []) {
210
- this.ruleMap.set(rule.className, rule);
251
+ this.ruleMap.set(rule.type, rule);
211
252
  }
212
253
  if (config.enableDrag !== void 0) {
213
254
  this.enableDrag = config.enableDrag;
@@ -220,12 +261,12 @@ class Selection extends (_a = core.BasePlugin, _selected_dec = [decorators.regis
220
261
  // container,
221
262
  boundaries: [container],
222
263
  selectionAreaClass: this.selectionAreaClassName,
223
- selectables: Array.from(this.ruleMap.keys()).map((className) => `.${className}`)
264
+ selectables: Array.from(new Set(Array.from(this.ruleMap.values()).map(({ className }) => `.${className}`)))
224
265
  }).on("beforedrag", this.onBeforeDrag).on("beforestart", this.onBeforeStart).on("start", this.onStart).on("move", this.onMove).on("stop", this.onStop);
225
266
  }
226
267
  getElementRule(element) {
227
- for (const [className, rule] of this.ruleMap) {
228
- if (element.classList.contains(className)) {
268
+ for (const [, rule] of this.ruleMap) {
269
+ if (rule.enabled !== false && element.classList.contains(rule.className)) {
229
270
  return rule;
230
271
  }
231
272
  }
@@ -234,6 +275,17 @@ class Selection extends (_a = core.BasePlugin, _selected_dec = [decorators.regis
234
275
  getSelected() {
235
276
  return this.selected;
236
277
  }
278
+ select(items) {
279
+ var _a2;
280
+ const selectors = [];
281
+ for (const item of items) {
282
+ const rule = this.ruleMap.get(item.type);
283
+ if (rule) {
284
+ selectors.push(`.${rule.className}[data-${rule.dataSelectorType || item.type}-id="${item.id}"]`);
285
+ }
286
+ }
287
+ (_a2 = this.selection) == null ? void 0 : _a2.select(selectors);
288
+ }
237
289
  clearSelection() {
238
290
  var _a2;
239
291
  (_a2 = this.selection) == null ? void 0 : _a2.clearSelection(true, false);
@@ -247,10 +299,15 @@ class Selection extends (_a = core.BasePlugin, _selected_dec = [decorators.regis
247
299
  _init = __decoratorStart(_a);
248
300
  __decorateElement(_init, 1, "init", _init_dec, Selection);
249
301
  __decorateElement(_init, 1, "getSelected", _getSelected_dec, Selection);
302
+ __decorateElement(_init, 1, "select", _select_dec, Selection);
250
303
  __decorateElement(_init, 1, "clearSelection", _clearSelection_dec, Selection);
251
304
  __decorateElement(_init, 1, "destroy", _destroy_dec, Selection);
252
305
  __decorateElement(_init, 5, "selected", _selected_dec, Selection);
253
306
  __decorateElement(_init, 5, "registerRules", _registerRules_dec, Selection);
307
+ __decorateElement(_init, 5, "setRuleEnabled", _setRuleEnabled_dec, Selection);
308
+ __decorateElement(_init, 5, "clearSelectionBeforeStart", _clearSelectionBeforeStart_dec, Selection);
309
+ __decorateElement(_init, 5, "setClearSelectionBeforeStart", _setClearSelectionBeforeStart_dec, Selection);
310
+ __decorateElement(_init, 5, "setElementOverlap", _setElementOverlap_dec, Selection);
254
311
  __decorateElement(_init, 5, "interaction", _interaction_dec, Selection);
255
312
  __decoratorMetadata(_init, Selection);
256
313
 
package/dist/index.d.cts CHANGED
@@ -1,3 +1,4 @@
1
+ import { OverlapMode } from '@viselect/vanilla';
1
2
  import { BasePlugin } from '@knotx/core';
2
3
 
3
4
  interface SelectionSelectedItem {
@@ -9,10 +10,30 @@ declare module '@knotx/core' {
9
10
  selection: {
10
11
  selected: SelectionSelectedItem[];
11
12
  registerRules: (rules: SelectionRule[]) => () => void;
13
+ /**
14
+ * 启用/禁用规则
15
+ * @param type
16
+ * @param enabled
17
+ * @returns
18
+ */
19
+ setRuleEnabled: (type: string, enabled: boolean) => void;
20
+ /**
21
+ * 是否在开始选择前清除选择
22
+ * @param isClearSelectionBeforeStart
23
+ * @returns
24
+ */
25
+ setClearSelectionBeforeStart: (isClearSelectionBeforeStart: boolean) => void;
26
+ /**
27
+ * 设置元素在选中状态下被二次选中时的行为
28
+ * @param overlap
29
+ * @returns
30
+ */
31
+ setElementOverlap: (overlap: OverlapMode) => void;
12
32
  };
13
33
  }
14
34
  interface PluginTools {
15
35
  selection: {
36
+ select: (items: SelectionSelectedItem[]) => void;
16
37
  getSelected: () => SelectionSelectedItem[];
17
38
  clearSelection: () => void;
18
39
  };
@@ -41,6 +62,16 @@ interface SelectionRule {
41
62
  select: (elements: Element[]) => void;
42
63
  deselect: (elements: Element[]) => void;
43
64
  }) => boolean;
65
+ /**
66
+ * 数据选择器类型,用于匹配 dom 元素的 data-${type}-id 属性
67
+ * default: `type
68
+ */
69
+ dataSelectorType?: string;
70
+ /**
71
+ * 是否启用规则
72
+ * default: true
73
+ */
74
+ enabled?: boolean;
44
75
  }
45
76
  interface SelectionConfig {
46
77
  selectionAreaClassName?: string;
@@ -70,6 +101,10 @@ declare class Selection extends BasePlugin<'selection', SelectionConfig> {
70
101
  private updateFrameId;
71
102
  selected: SelectionSelectedItem[];
72
103
  registerRules: (rules: SelectionRule[]) => () => void;
104
+ setRuleEnabled: (type: string, enabled: boolean) => void;
105
+ clearSelectionBeforeStart: boolean;
106
+ setClearSelectionBeforeStart: (clearSelectionBeforeStart: boolean) => void;
107
+ setElementOverlap: (overlap: OverlapMode) => void;
73
108
  private interaction;
74
109
  init(config?: SelectionConfig): void;
75
110
  private createSelection;
@@ -81,6 +116,7 @@ declare class Selection extends BasePlugin<'selection', SelectionConfig> {
81
116
  private getElementRule;
82
117
  private updateSelected;
83
118
  getSelected(): SelectionSelectedItem[];
119
+ select(items: SelectionSelectedItem[]): void;
84
120
  clearSelection(): void;
85
121
  destroy(): void;
86
122
  }
package/dist/index.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import { OverlapMode } from '@viselect/vanilla';
1
2
  import { BasePlugin } from '@knotx/core';
2
3
 
3
4
  interface SelectionSelectedItem {
@@ -9,10 +10,30 @@ declare module '@knotx/core' {
9
10
  selection: {
10
11
  selected: SelectionSelectedItem[];
11
12
  registerRules: (rules: SelectionRule[]) => () => void;
13
+ /**
14
+ * 启用/禁用规则
15
+ * @param type
16
+ * @param enabled
17
+ * @returns
18
+ */
19
+ setRuleEnabled: (type: string, enabled: boolean) => void;
20
+ /**
21
+ * 是否在开始选择前清除选择
22
+ * @param isClearSelectionBeforeStart
23
+ * @returns
24
+ */
25
+ setClearSelectionBeforeStart: (isClearSelectionBeforeStart: boolean) => void;
26
+ /**
27
+ * 设置元素在选中状态下被二次选中时的行为
28
+ * @param overlap
29
+ * @returns
30
+ */
31
+ setElementOverlap: (overlap: OverlapMode) => void;
12
32
  };
13
33
  }
14
34
  interface PluginTools {
15
35
  selection: {
36
+ select: (items: SelectionSelectedItem[]) => void;
16
37
  getSelected: () => SelectionSelectedItem[];
17
38
  clearSelection: () => void;
18
39
  };
@@ -41,6 +62,16 @@ interface SelectionRule {
41
62
  select: (elements: Element[]) => void;
42
63
  deselect: (elements: Element[]) => void;
43
64
  }) => boolean;
65
+ /**
66
+ * 数据选择器类型,用于匹配 dom 元素的 data-${type}-id 属性
67
+ * default: `type
68
+ */
69
+ dataSelectorType?: string;
70
+ /**
71
+ * 是否启用规则
72
+ * default: true
73
+ */
74
+ enabled?: boolean;
44
75
  }
45
76
  interface SelectionConfig {
46
77
  selectionAreaClassName?: string;
@@ -70,6 +101,10 @@ declare class Selection extends BasePlugin<'selection', SelectionConfig> {
70
101
  private updateFrameId;
71
102
  selected: SelectionSelectedItem[];
72
103
  registerRules: (rules: SelectionRule[]) => () => void;
104
+ setRuleEnabled: (type: string, enabled: boolean) => void;
105
+ clearSelectionBeforeStart: boolean;
106
+ setClearSelectionBeforeStart: (clearSelectionBeforeStart: boolean) => void;
107
+ setElementOverlap: (overlap: OverlapMode) => void;
73
108
  private interaction;
74
109
  init(config?: SelectionConfig): void;
75
110
  private createSelection;
@@ -81,6 +116,7 @@ declare class Selection extends BasePlugin<'selection', SelectionConfig> {
81
116
  private getElementRule;
82
117
  private updateSelected;
83
118
  getSelected(): SelectionSelectedItem[];
119
+ select(items: SelectionSelectedItem[]): void;
84
120
  clearSelection(): void;
85
121
  destroy(): void;
86
122
  }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { OverlapMode } from '@viselect/vanilla';
1
2
  import { BasePlugin } from '@knotx/core';
2
3
 
3
4
  interface SelectionSelectedItem {
@@ -9,10 +10,30 @@ declare module '@knotx/core' {
9
10
  selection: {
10
11
  selected: SelectionSelectedItem[];
11
12
  registerRules: (rules: SelectionRule[]) => () => void;
13
+ /**
14
+ * 启用/禁用规则
15
+ * @param type
16
+ * @param enabled
17
+ * @returns
18
+ */
19
+ setRuleEnabled: (type: string, enabled: boolean) => void;
20
+ /**
21
+ * 是否在开始选择前清除选择
22
+ * @param isClearSelectionBeforeStart
23
+ * @returns
24
+ */
25
+ setClearSelectionBeforeStart: (isClearSelectionBeforeStart: boolean) => void;
26
+ /**
27
+ * 设置元素在选中状态下被二次选中时的行为
28
+ * @param overlap
29
+ * @returns
30
+ */
31
+ setElementOverlap: (overlap: OverlapMode) => void;
12
32
  };
13
33
  }
14
34
  interface PluginTools {
15
35
  selection: {
36
+ select: (items: SelectionSelectedItem[]) => void;
16
37
  getSelected: () => SelectionSelectedItem[];
17
38
  clearSelection: () => void;
18
39
  };
@@ -41,6 +62,16 @@ interface SelectionRule {
41
62
  select: (elements: Element[]) => void;
42
63
  deselect: (elements: Element[]) => void;
43
64
  }) => boolean;
65
+ /**
66
+ * 数据选择器类型,用于匹配 dom 元素的 data-${type}-id 属性
67
+ * default: `type
68
+ */
69
+ dataSelectorType?: string;
70
+ /**
71
+ * 是否启用规则
72
+ * default: true
73
+ */
74
+ enabled?: boolean;
44
75
  }
45
76
  interface SelectionConfig {
46
77
  selectionAreaClassName?: string;
@@ -70,6 +101,10 @@ declare class Selection extends BasePlugin<'selection', SelectionConfig> {
70
101
  private updateFrameId;
71
102
  selected: SelectionSelectedItem[];
72
103
  registerRules: (rules: SelectionRule[]) => () => void;
104
+ setRuleEnabled: (type: string, enabled: boolean) => void;
105
+ clearSelectionBeforeStart: boolean;
106
+ setClearSelectionBeforeStart: (clearSelectionBeforeStart: boolean) => void;
107
+ setElementOverlap: (overlap: OverlapMode) => void;
73
108
  private interaction;
74
109
  init(config?: SelectionConfig): void;
75
110
  private createSelection;
@@ -81,6 +116,7 @@ declare class Selection extends BasePlugin<'selection', SelectionConfig> {
81
116
  private getElementRule;
82
117
  private updateSelected;
83
118
  getSelected(): SelectionSelectedItem[];
119
+ select(items: SelectionSelectedItem[]): void;
84
120
  clearSelection(): void;
85
121
  destroy(): void;
86
122
  }
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { bemSelector, bem, BasePlugin, InteractionPriority } from '@knotx/core';
2
- import { register, inject, tool, OnInit, OnDestroy } from '@knotx/decorators';
2
+ import { register, observable, inject, tool, OnInit, OnDestroy } from '@knotx/decorators';
3
3
  import SelectionArea from '@viselect/vanilla';
4
4
 
5
5
  var __create = Object.create;
@@ -67,8 +67,28 @@ var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use
67
67
  var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
68
68
  var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
69
69
  var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
70
- var _destroy_dec, _clearSelection_dec, _getSelected_dec, _init_dec, _interaction_dec, _registerRules_dec, _selected_dec, _a, _init;
71
- class Selection extends (_a = BasePlugin, _selected_dec = [register("selected")], _registerRules_dec = [register("registerRules")], _interaction_dec = [inject.interaction()], _init_dec = [OnInit], _getSelected_dec = [tool("Get selected items", {})], _clearSelection_dec = [tool("Clear selection", {})], _destroy_dec = [OnDestroy], _a) {
70
+ var _destroy_dec, _clearSelection_dec, _select_dec, _getSelected_dec, _init_dec, _interaction_dec, _setElementOverlap_dec, _setClearSelectionBeforeStart_dec, _clearSelectionBeforeStart_dec, _setRuleEnabled_dec, _registerRules_dec, _selected_dec, _a, _init;
71
+ class Selection extends (_a = BasePlugin, _selected_dec = [register("selected")], _registerRules_dec = [register("registerRules")], _setRuleEnabled_dec = [register("setRuleEnabled")], _clearSelectionBeforeStart_dec = [observable()], _setClearSelectionBeforeStart_dec = [register("setClearSelectionBeforeStart")], _setElementOverlap_dec = [register("setElementOverlap")], _interaction_dec = [inject.interaction()], _init_dec = [OnInit], _getSelected_dec = [tool("Get selected items", {})], _select_dec = [tool("Items that match the registered rules will be selected, manifested as the item showing a selected style.", {
72
+ type: "array",
73
+ items: {
74
+ type: "object",
75
+ properties: {
76
+ type: {
77
+ type: "string",
78
+ description: "item type such as node, group, etc."
79
+ },
80
+ id: {
81
+ type: "string"
82
+ }
83
+ },
84
+ additionalProperties: false,
85
+ required: [
86
+ "id",
87
+ "type"
88
+ ]
89
+ },
90
+ $schema: "http://json-schema.org/draft-07/schema#"
91
+ })], _clearSelection_dec = [tool("Clear selection", {})], _destroy_dec = [OnDestroy], _a) {
72
92
  constructor() {
73
93
  super(...arguments);
74
94
  __runInitializers(_init, 5, this);
@@ -83,17 +103,33 @@ class Selection extends (_a = BasePlugin, _selected_dec = [register("selected")]
83
103
  __publicField(this, "selected", __runInitializers(_init, 8, this, [])), __runInitializers(_init, 11, this);
84
104
  __publicField(this, "registerRules", __runInitializers(_init, 12, this, (rules) => {
85
105
  rules.forEach((rule) => {
86
- this.ruleMap.set(rule.className, rule);
106
+ this.ruleMap.set(rule.type, rule);
87
107
  });
88
108
  return () => {
89
109
  rules.forEach((rule) => {
90
- if (this.ruleMap.get(rule.className) === rule) {
91
- this.ruleMap.delete(rule.className);
110
+ if (this.ruleMap.get(rule.type) === rule) {
111
+ this.ruleMap.delete(rule.type);
92
112
  }
93
113
  });
94
114
  };
95
115
  })), __runInitializers(_init, 15, this);
96
- __publicField(this, "interaction", __runInitializers(_init, 16, this)), __runInitializers(_init, 19, this);
116
+ __publicField(this, "setRuleEnabled", __runInitializers(_init, 16, this, (type, enabled) => {
117
+ const rule = this.ruleMap.get(type);
118
+ if (rule) {
119
+ rule.enabled = enabled;
120
+ }
121
+ })), __runInitializers(_init, 19, this);
122
+ __publicField(this, "clearSelectionBeforeStart", __runInitializers(_init, 20, this, true)), __runInitializers(_init, 23, this);
123
+ __publicField(this, "setClearSelectionBeforeStart", __runInitializers(_init, 24, this, (clearSelectionBeforeStart) => {
124
+ this.clearSelectionBeforeStart = clearSelectionBeforeStart;
125
+ })), __runInitializers(_init, 27, this);
126
+ __publicField(this, "setElementOverlap", __runInitializers(_init, 28, this, (overlap) => {
127
+ try {
128
+ this.selection._options.behaviour.overlap = overlap;
129
+ } catch (e) {
130
+ }
131
+ })), __runInitializers(_init, 31, this);
132
+ __publicField(this, "interaction", __runInitializers(_init, 32, this)), __runInitializers(_init, 35, this);
97
133
  __publicField(this, "onBeforeStart", (_) => {
98
134
  });
99
135
  __publicField(this, "onBeforeDrag", () => {
@@ -104,13 +140,16 @@ class Selection extends (_a = BasePlugin, _selected_dec = [register("selected")]
104
140
  var _a2;
105
141
  this.interaction.start(this.pluginId, "select", InteractionPriority.MarqueeSelection);
106
142
  if (!(event == null ? void 0 : event.ctrlKey) && !(event == null ? void 0 : event.metaKey) && !(event == null ? void 0 : event.shiftKey)) {
107
- store.stored.forEach((element) => {
108
- const rule = this.getElementRule(element);
109
- if (rule == null ? void 0 : rule.activeClassName) {
110
- element.classList.remove(rule.activeClassName);
111
- }
112
- });
113
- (_a2 = this.selection) == null ? void 0 : _a2.clearSelection(true, true);
143
+ if (this.clearSelectionBeforeStart) {
144
+ store.stored.forEach((element) => {
145
+ const rule = this.getElementRule(element);
146
+ if (rule == null ? void 0 : rule.activeClassName) {
147
+ element.classList.remove(rule.activeClassName);
148
+ }
149
+ element.removeAttribute("data-selected");
150
+ });
151
+ (_a2 = this.selection) == null ? void 0 : _a2.clearSelection(true, true);
152
+ }
114
153
  }
115
154
  });
116
155
  __publicField(this, "onMove", ({ store: { stored, changed: { added, removed } }, selection }) => {
@@ -145,6 +184,7 @@ class Selection extends (_a = BasePlugin, _selected_dec = [register("selected")]
145
184
  if (rule.activeClassName) {
146
185
  element.classList.remove(rule.activeClassName);
147
186
  }
187
+ element.removeAttribute("data-selected");
148
188
  });
149
189
  added.forEach((element) => {
150
190
  const rule = this.getElementRule(element);
@@ -154,7 +194,7 @@ class Selection extends (_a = BasePlugin, _selected_dec = [register("selected")]
154
194
  const type = rule.type;
155
195
  const id = rule.idGetter(element);
156
196
  for (const [, rule2] of this.ruleMap) {
157
- if (rule2.onSelected) {
197
+ if (rule2.enabled !== false && rule2.onSelected) {
158
198
  if (!rule2.onSelected(__spreadValues({ element, type, id }, context))) {
159
199
  selection.deselect(element, true);
160
200
  return;
@@ -164,6 +204,7 @@ class Selection extends (_a = BasePlugin, _selected_dec = [register("selected")]
164
204
  if (rule.activeClassName) {
165
205
  element.classList.add(rule.activeClassName);
166
206
  }
207
+ element.setAttribute("data-selected", "true");
167
208
  });
168
209
  });
169
210
  __publicField(this, "onStop", ({ event }) => {
@@ -179,7 +220,7 @@ class Selection extends (_a = BasePlugin, _selected_dec = [register("selected")]
179
220
  __publicField(this, "updateSelected", () => {
180
221
  var _a2;
181
222
  const selected = [];
182
- for (const element of ((_a2 = this.selection) == null ? void 0 : _a2.getSelection()) || []) {
223
+ for (const element of new Set(((_a2 = this.selection) == null ? void 0 : _a2.getSelection()) || [])) {
183
224
  const rule = this.getElementRule(element);
184
225
  if (rule) {
185
226
  selected.push({ type: rule.type, id: rule.idGetter(element) });
@@ -198,10 +239,10 @@ class Selection extends (_a = BasePlugin, _selected_dec = [register("selected")]
198
239
  activeClassName: bem("node", "wrapper", "selected"),
199
240
  idGetter: (element) => element.getAttribute("data-node-id") || ""
200
241
  };
201
- this.ruleMap.set(nodeRule.className, nodeRule);
242
+ this.ruleMap.set(nodeRule.type, nodeRule);
202
243
  }
203
244
  for (const rule of config.rules || []) {
204
- this.ruleMap.set(rule.className, rule);
245
+ this.ruleMap.set(rule.type, rule);
205
246
  }
206
247
  if (config.enableDrag !== void 0) {
207
248
  this.enableDrag = config.enableDrag;
@@ -214,12 +255,12 @@ class Selection extends (_a = BasePlugin, _selected_dec = [register("selected")]
214
255
  // container,
215
256
  boundaries: [container],
216
257
  selectionAreaClass: this.selectionAreaClassName,
217
- selectables: Array.from(this.ruleMap.keys()).map((className) => `.${className}`)
258
+ selectables: Array.from(new Set(Array.from(this.ruleMap.values()).map(({ className }) => `.${className}`)))
218
259
  }).on("beforedrag", this.onBeforeDrag).on("beforestart", this.onBeforeStart).on("start", this.onStart).on("move", this.onMove).on("stop", this.onStop);
219
260
  }
220
261
  getElementRule(element) {
221
- for (const [className, rule] of this.ruleMap) {
222
- if (element.classList.contains(className)) {
262
+ for (const [, rule] of this.ruleMap) {
263
+ if (rule.enabled !== false && element.classList.contains(rule.className)) {
223
264
  return rule;
224
265
  }
225
266
  }
@@ -228,6 +269,17 @@ class Selection extends (_a = BasePlugin, _selected_dec = [register("selected")]
228
269
  getSelected() {
229
270
  return this.selected;
230
271
  }
272
+ select(items) {
273
+ var _a2;
274
+ const selectors = [];
275
+ for (const item of items) {
276
+ const rule = this.ruleMap.get(item.type);
277
+ if (rule) {
278
+ selectors.push(`.${rule.className}[data-${rule.dataSelectorType || item.type}-id="${item.id}"]`);
279
+ }
280
+ }
281
+ (_a2 = this.selection) == null ? void 0 : _a2.select(selectors);
282
+ }
231
283
  clearSelection() {
232
284
  var _a2;
233
285
  (_a2 = this.selection) == null ? void 0 : _a2.clearSelection(true, false);
@@ -241,10 +293,15 @@ class Selection extends (_a = BasePlugin, _selected_dec = [register("selected")]
241
293
  _init = __decoratorStart(_a);
242
294
  __decorateElement(_init, 1, "init", _init_dec, Selection);
243
295
  __decorateElement(_init, 1, "getSelected", _getSelected_dec, Selection);
296
+ __decorateElement(_init, 1, "select", _select_dec, Selection);
244
297
  __decorateElement(_init, 1, "clearSelection", _clearSelection_dec, Selection);
245
298
  __decorateElement(_init, 1, "destroy", _destroy_dec, Selection);
246
299
  __decorateElement(_init, 5, "selected", _selected_dec, Selection);
247
300
  __decorateElement(_init, 5, "registerRules", _registerRules_dec, Selection);
301
+ __decorateElement(_init, 5, "setRuleEnabled", _setRuleEnabled_dec, Selection);
302
+ __decorateElement(_init, 5, "clearSelectionBeforeStart", _clearSelectionBeforeStart_dec, Selection);
303
+ __decorateElement(_init, 5, "setClearSelectionBeforeStart", _setClearSelectionBeforeStart_dec, Selection);
304
+ __decorateElement(_init, 5, "setElementOverlap", _setElementOverlap_dec, Selection);
248
305
  __decorateElement(_init, 5, "interaction", _interaction_dec, Selection);
249
306
  __decoratorMetadata(_init, Selection);
250
307
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knotx/plugins-selection",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "description": "Selection Plugin for Knotx",
5
5
  "author": "boenfu",
6
6
  "license": "MIT",
@@ -30,13 +30,13 @@
30
30
  "dependencies": {
31
31
  "@viselect/vanilla": "^3.9.0",
32
32
  "rxjs": "^7.8.1",
33
- "@knotx/core": "0.3.2",
34
- "@knotx/decorators": "0.3.2"
33
+ "@knotx/core": "0.3.4",
34
+ "@knotx/decorators": "0.3.4"
35
35
  },
36
36
  "devDependencies": {
37
- "@knotx/build-config": "0.3.2",
38
- "@knotx/eslint-config": "0.3.2",
39
- "@knotx/typescript-config": "0.3.2"
37
+ "@knotx/build-config": "0.3.4",
38
+ "@knotx/eslint-config": "0.3.4",
39
+ "@knotx/typescript-config": "0.3.4"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "unbuild",