@knotx/plugins-drag 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,274 @@
1
+ 'use strict';
2
+
3
+ const core = require('@knotx/core');
4
+ const decorators = require('@knotx/decorators');
5
+ const interact = require('interactjs');
6
+ const rxjs = require('rxjs');
7
+ const operators = require('rxjs/operators');
8
+ require('@knotx/plugins-canvas');
9
+
10
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
11
+
12
+ const interact__default = /*#__PURE__*/_interopDefaultCompat(interact);
13
+
14
+ var __create = Object.create;
15
+ var __defProp = Object.defineProperty;
16
+ var __defProps = Object.defineProperties;
17
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
18
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
19
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
20
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
21
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
22
+ var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
23
+ var __typeError = (msg) => {
24
+ throw TypeError(msg);
25
+ };
26
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
27
+ var __spreadValues = (a, b) => {
28
+ for (var prop in b || (b = {}))
29
+ if (__hasOwnProp.call(b, prop))
30
+ __defNormalProp(a, prop, b[prop]);
31
+ if (__getOwnPropSymbols)
32
+ for (var prop of __getOwnPropSymbols(b)) {
33
+ if (__propIsEnum.call(b, prop))
34
+ __defNormalProp(a, prop, b[prop]);
35
+ }
36
+ return a;
37
+ };
38
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
39
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
40
+ var __decoratorStart = (base) => {
41
+ var _a2;
42
+ return [, , , __create((_a2 = base == null ? void 0 : base[__knownSymbol("metadata")]) != null ? _a2 : null)];
43
+ };
44
+ var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
45
+ var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
46
+ var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
47
+ var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
48
+ var __runInitializers = (array, flags, self, value) => {
49
+ 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);
50
+ return value;
51
+ };
52
+ var __decorateElement = (array, flags, name, decorators, target, extra) => {
53
+ var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
54
+ var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
55
+ var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
56
+ var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
57
+ return __privateGet(this, extra);
58
+ }, set [name](x) {
59
+ return __privateSet(this, extra, x);
60
+ } }, name));
61
+ k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
62
+ for (var i = decorators.length - 1; i >= 0; i--) {
63
+ ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
64
+ if (k) {
65
+ ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
66
+ if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
67
+ if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
68
+ }
69
+ 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;
70
+ if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
71
+ else if (typeof it !== "object" || it === null) __typeError("Object expected");
72
+ else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
73
+ }
74
+ return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
75
+ };
76
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
77
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
78
+ var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
79
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
80
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
81
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
82
+ var _destroy_dec, _init_dec, _registerOptionsTransformer_dec, _options_dec, _canvasScale_dec, _canInteract_dec, _endInteraction_dec, _startInteraction_dec, _dispatchNodeOperation_dec, _getNode_dec, _a, _init;
83
+ const DEFAULT_OPTIONS = {
84
+ inertia: {
85
+ enabled: true
86
+ },
87
+ modifiers: [
88
+ interact__default.modifiers.restrictRect({
89
+ restriction: `.${core.bem("canvas", "wrapper")}`,
90
+ endOnly: true
91
+ })
92
+ ]
93
+ };
94
+ class Drag extends (_a = core.BasePlugin, _getNode_dec = [decorators.inject.getNode()], _dispatchNodeOperation_dec = [decorators.inject.dispatchNodeOperation()], _startInteraction_dec = [decorators.inject.startInteraction()], _endInteraction_dec = [decorators.inject.endInteraction()], _canInteract_dec = [decorators.inject.canInteract()], _canvasScale_dec = [decorators.inject.canvas.transform((state) => state.scale)], _options_dec = [decorators.register("options")], _registerOptionsTransformer_dec = [decorators.register("registerOptionsTransformer")], _init_dec = [decorators.OnInit], _destroy_dec = [decorators.OnDestroy], _a) {
95
+ constructor() {
96
+ super(...arguments);
97
+ __runInitializers(_init, 5, this);
98
+ __publicField(this, "name", "drag");
99
+ __publicField(this, "getNode", __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this);
100
+ __publicField(this, "dispatchNodeOperation", __runInitializers(_init, 12, this)), __runInitializers(_init, 15, this);
101
+ __publicField(this, "startInteraction", __runInitializers(_init, 16, this)), __runInitializers(_init, 19, this);
102
+ __publicField(this, "endInteraction", __runInitializers(_init, 20, this)), __runInitializers(_init, 23, this);
103
+ __publicField(this, "canInteract", __runInitializers(_init, 24, this)), __runInitializers(_init, 27, this);
104
+ __publicField(this, "canvasScale", __runInitializers(_init, 28, this, 1)), __runInitializers(_init, 31, this);
105
+ __publicField(this, "subscription");
106
+ __publicField(this, "transformers$", new rxjs.BehaviorSubject([]));
107
+ __publicField(this, "currentInteractable");
108
+ __publicField(this, "options", __runInitializers(_init, 32, this, DEFAULT_OPTIONS)), __runInitializers(_init, 35, this);
109
+ __publicField(this, "registerOptionsTransformer", __runInitializers(_init, 36, this, (transformer) => {
110
+ const transformers = this.transformers$.value;
111
+ transformers.push(transformer);
112
+ this.transformers$.next(transformers);
113
+ return () => {
114
+ const currentTransformers = this.transformers$.value;
115
+ const index = currentTransformers.indexOf(transformer);
116
+ if (index > -1) {
117
+ currentTransformers.splice(index, 1);
118
+ this.transformers$.next(currentTransformers);
119
+ }
120
+ };
121
+ })), __runInitializers(_init, 39, this);
122
+ }
123
+ /**
124
+ * 应用所有转换器,生成最终的配置
125
+ */
126
+ applyTransformers(nodeId) {
127
+ const node = this.getNode(nodeId);
128
+ if (!node)
129
+ return this.options;
130
+ const context = {
131
+ nodeId,
132
+ node,
133
+ defaultOptions: this.options,
134
+ currentOptions: this.options
135
+ };
136
+ return this.transformers$.value.reduce(
137
+ (options, transformer) => {
138
+ var _a2;
139
+ context.currentOptions = options;
140
+ return (_a2 = transformer(context)) != null ? _a2 : options;
141
+ },
142
+ this.options
143
+ );
144
+ }
145
+ /**
146
+ * 更新 interactable 的配置
147
+ */
148
+ updateInteractableOptions(event) {
149
+ var _a2;
150
+ if (!this.currentInteractable)
151
+ return;
152
+ const target = ((_a2 = event.__set) == null ? void 0 : _a2.currentTarget) || event.target;
153
+ const nodeId = target.getAttribute("data-node-id");
154
+ if (!nodeId)
155
+ return;
156
+ const options = this.applyTransformers(nodeId);
157
+ this.currentInteractable.draggable(options);
158
+ }
159
+ init() {
160
+ const dragStart$ = new rxjs.Subject();
161
+ const dragMove$ = new rxjs.Subject();
162
+ const dragEnd$ = new rxjs.Subject();
163
+ const end = () => {
164
+ var _a2;
165
+ dragEnd$.next();
166
+ (_a2 = this.currentInteractable) == null ? void 0 : _a2.draggable(this.options);
167
+ this.endInteraction(this.pluginId, "position");
168
+ };
169
+ this.currentInteractable = interact__default(`.${core.bem("node", "wrapper")}[data-node-id]`).draggable(__spreadProps(__spreadValues({}, this.options), {
170
+ listeners: {
171
+ start: (event) => {
172
+ const target = event.target;
173
+ const nodeId = target.getAttribute("data-node-id");
174
+ if (!nodeId)
175
+ return;
176
+ dragStart$.next({
177
+ nodeId
178
+ });
179
+ this.startInteraction(this.pluginId, "position", core.InteractionPriority.EntityPositionDrag);
180
+ },
181
+ move: (event) => {
182
+ if (!this.canInteract(this.pluginId, "position")) {
183
+ end();
184
+ return;
185
+ }
186
+ const scale = this.canvasScale;
187
+ dragMove$.next({
188
+ dx: event.dx / scale,
189
+ dy: event.dy / scale
190
+ });
191
+ },
192
+ // 拖拽结束
193
+ end
194
+ }
195
+ })).on("down", (event) => this.updateInteractableOptions(event));
196
+ this.options = __spreadValues({}, this.currentInteractable.draggable());
197
+ this.subscription = dragStart$.pipe(
198
+ // 对每次拖拽开始事件,创建一个新的拖拽流程
199
+ operators.switchMap((startEvent) => {
200
+ var _a2;
201
+ const draftId = `${this.pluginId}-${startEvent.nodeId}`;
202
+ const { nodeId } = startEvent;
203
+ const startPosition = (_a2 = this.getNode(nodeId)) == null ? void 0 : _a2.position;
204
+ const currentPosition = { x: (startPosition == null ? void 0 : startPosition.x) || 0, y: (startPosition == null ? void 0 : startPosition.y) || 0 };
205
+ this.dispatchNodeOperation({
206
+ type: "startDraft",
207
+ draftId
208
+ });
209
+ return dragMove$.pipe(
210
+ operators.skip(1),
211
+ operators.map((moveEvent) => {
212
+ currentPosition.x += moveEvent.dx;
213
+ currentPosition.y += moveEvent.dy;
214
+ return {
215
+ nodeId,
216
+ position: currentPosition
217
+ };
218
+ }),
219
+ operators.tap((moveData) => {
220
+ this.dispatchNodeOperation({
221
+ type: "draftOperation",
222
+ draftId,
223
+ operation: {
224
+ type: "update",
225
+ id: moveData.nodeId,
226
+ data: {
227
+ position: {
228
+ x: currentPosition.x,
229
+ y: currentPosition.y
230
+ }
231
+ }
232
+ }
233
+ });
234
+ }),
235
+ operators.takeUntil(
236
+ dragEnd$.pipe(
237
+ operators.tap(() => {
238
+ this.dispatchNodeOperation({
239
+ type: "commitDraft",
240
+ draftId
241
+ });
242
+ })
243
+ )
244
+ )
245
+ );
246
+ })
247
+ ).subscribe();
248
+ this.subscription.add(() => {
249
+ var _a2;
250
+ (_a2 = this.currentInteractable) == null ? void 0 : _a2.unset();
251
+ dragStart$.complete();
252
+ dragMove$.complete();
253
+ dragEnd$.complete();
254
+ });
255
+ }
256
+ destroy() {
257
+ var _a2;
258
+ (_a2 = this.subscription) == null ? void 0 : _a2.unsubscribe();
259
+ }
260
+ }
261
+ _init = __decoratorStart(_a);
262
+ __decorateElement(_init, 1, "init", _init_dec, Drag);
263
+ __decorateElement(_init, 1, "destroy", _destroy_dec, Drag);
264
+ __decorateElement(_init, 5, "getNode", _getNode_dec, Drag);
265
+ __decorateElement(_init, 5, "dispatchNodeOperation", _dispatchNodeOperation_dec, Drag);
266
+ __decorateElement(_init, 5, "startInteraction", _startInteraction_dec, Drag);
267
+ __decorateElement(_init, 5, "endInteraction", _endInteraction_dec, Drag);
268
+ __decorateElement(_init, 5, "canInteract", _canInteract_dec, Drag);
269
+ __decorateElement(_init, 5, "canvasScale", _canvasScale_dec, Drag);
270
+ __decorateElement(_init, 5, "options", _options_dec, Drag);
271
+ __decorateElement(_init, 5, "registerOptionsTransformer", _registerOptionsTransformer_dec, Drag);
272
+ __decoratorMetadata(_init, Drag);
273
+
274
+ exports.Drag = Drag;
@@ -0,0 +1,45 @@
1
+ import { DraggableOptions as DraggableOptions$1 } from '@interactjs/actions/drag/plugin';
2
+ import { NodeData, BasePlugin, NodeOperation, InteractionPriority } from '@knotx/core';
3
+
4
+ type DraggableOptions = Omit<DraggableOptions$1, 'listeners'>;
5
+ interface DragTransformerContext {
6
+ nodeId: string;
7
+ node: NodeData;
8
+ defaultOptions: DraggableOptions;
9
+ currentOptions: DraggableOptions;
10
+ }
11
+ type DragOptionsTransformer = (context: DragTransformerContext) => DraggableOptions | undefined;
12
+ declare module '@knotx/core' {
13
+ interface PluginData {
14
+ drag: {
15
+ options: DraggableOptions;
16
+ registerOptionsTransformer: (transformer: DragOptionsTransformer) => () => void;
17
+ };
18
+ }
19
+ }
20
+ declare class Drag extends BasePlugin {
21
+ name: "drag";
22
+ getNode: (id: string) => NodeData | undefined;
23
+ dispatchNodeOperation: (operation: NodeOperation) => void;
24
+ startInteraction: (pluginId: string, type: string, priority: InteractionPriority) => void;
25
+ endInteraction: (pluginId: string, type: string) => void;
26
+ canInteract: (pluginId: string, type: string, autoStartPriority?: InteractionPriority) => boolean;
27
+ canvasScale: number;
28
+ private subscription?;
29
+ private transformers$;
30
+ private currentInteractable?;
31
+ options: DraggableOptions;
32
+ registerOptionsTransformer: (transformer: DragOptionsTransformer) => () => void;
33
+ /**
34
+ * 应用所有转换器,生成最终的配置
35
+ */
36
+ private applyTransformers;
37
+ /**
38
+ * 更新 interactable 的配置
39
+ */
40
+ private updateInteractableOptions;
41
+ init(): void;
42
+ destroy(): void;
43
+ }
44
+
45
+ export { Drag, type DragOptionsTransformer, type DragTransformerContext, type DraggableOptions };
@@ -0,0 +1,45 @@
1
+ import { DraggableOptions as DraggableOptions$1 } from '@interactjs/actions/drag/plugin';
2
+ import { NodeData, BasePlugin, NodeOperation, InteractionPriority } from '@knotx/core';
3
+
4
+ type DraggableOptions = Omit<DraggableOptions$1, 'listeners'>;
5
+ interface DragTransformerContext {
6
+ nodeId: string;
7
+ node: NodeData;
8
+ defaultOptions: DraggableOptions;
9
+ currentOptions: DraggableOptions;
10
+ }
11
+ type DragOptionsTransformer = (context: DragTransformerContext) => DraggableOptions | undefined;
12
+ declare module '@knotx/core' {
13
+ interface PluginData {
14
+ drag: {
15
+ options: DraggableOptions;
16
+ registerOptionsTransformer: (transformer: DragOptionsTransformer) => () => void;
17
+ };
18
+ }
19
+ }
20
+ declare class Drag extends BasePlugin {
21
+ name: "drag";
22
+ getNode: (id: string) => NodeData | undefined;
23
+ dispatchNodeOperation: (operation: NodeOperation) => void;
24
+ startInteraction: (pluginId: string, type: string, priority: InteractionPriority) => void;
25
+ endInteraction: (pluginId: string, type: string) => void;
26
+ canInteract: (pluginId: string, type: string, autoStartPriority?: InteractionPriority) => boolean;
27
+ canvasScale: number;
28
+ private subscription?;
29
+ private transformers$;
30
+ private currentInteractable?;
31
+ options: DraggableOptions;
32
+ registerOptionsTransformer: (transformer: DragOptionsTransformer) => () => void;
33
+ /**
34
+ * 应用所有转换器,生成最终的配置
35
+ */
36
+ private applyTransformers;
37
+ /**
38
+ * 更新 interactable 的配置
39
+ */
40
+ private updateInteractableOptions;
41
+ init(): void;
42
+ destroy(): void;
43
+ }
44
+
45
+ export { Drag, type DragOptionsTransformer, type DragTransformerContext, type DraggableOptions };
@@ -0,0 +1,45 @@
1
+ import { DraggableOptions as DraggableOptions$1 } from '@interactjs/actions/drag/plugin';
2
+ import { NodeData, BasePlugin, NodeOperation, InteractionPriority } from '@knotx/core';
3
+
4
+ type DraggableOptions = Omit<DraggableOptions$1, 'listeners'>;
5
+ interface DragTransformerContext {
6
+ nodeId: string;
7
+ node: NodeData;
8
+ defaultOptions: DraggableOptions;
9
+ currentOptions: DraggableOptions;
10
+ }
11
+ type DragOptionsTransformer = (context: DragTransformerContext) => DraggableOptions | undefined;
12
+ declare module '@knotx/core' {
13
+ interface PluginData {
14
+ drag: {
15
+ options: DraggableOptions;
16
+ registerOptionsTransformer: (transformer: DragOptionsTransformer) => () => void;
17
+ };
18
+ }
19
+ }
20
+ declare class Drag extends BasePlugin {
21
+ name: "drag";
22
+ getNode: (id: string) => NodeData | undefined;
23
+ dispatchNodeOperation: (operation: NodeOperation) => void;
24
+ startInteraction: (pluginId: string, type: string, priority: InteractionPriority) => void;
25
+ endInteraction: (pluginId: string, type: string) => void;
26
+ canInteract: (pluginId: string, type: string, autoStartPriority?: InteractionPriority) => boolean;
27
+ canvasScale: number;
28
+ private subscription?;
29
+ private transformers$;
30
+ private currentInteractable?;
31
+ options: DraggableOptions;
32
+ registerOptionsTransformer: (transformer: DragOptionsTransformer) => () => void;
33
+ /**
34
+ * 应用所有转换器,生成最终的配置
35
+ */
36
+ private applyTransformers;
37
+ /**
38
+ * 更新 interactable 的配置
39
+ */
40
+ private updateInteractableOptions;
41
+ init(): void;
42
+ destroy(): void;
43
+ }
44
+
45
+ export { Drag, type DragOptionsTransformer, type DragTransformerContext, type DraggableOptions };
package/dist/index.mjs ADDED
@@ -0,0 +1,268 @@
1
+ import { bem, BasePlugin, InteractionPriority } from '@knotx/core';
2
+ import { inject, register, OnInit, OnDestroy } from '@knotx/decorators';
3
+ import interact from 'interactjs';
4
+ import { BehaviorSubject, Subject } from 'rxjs';
5
+ import { switchMap, skip, map, tap, takeUntil } from 'rxjs/operators';
6
+ import '@knotx/plugins-canvas';
7
+
8
+ var __create = Object.create;
9
+ var __defProp = Object.defineProperty;
10
+ var __defProps = Object.defineProperties;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
13
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
14
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
15
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
16
+ var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
17
+ var __typeError = (msg) => {
18
+ throw TypeError(msg);
19
+ };
20
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
21
+ var __spreadValues = (a, b) => {
22
+ for (var prop in b || (b = {}))
23
+ if (__hasOwnProp.call(b, prop))
24
+ __defNormalProp(a, prop, b[prop]);
25
+ if (__getOwnPropSymbols)
26
+ for (var prop of __getOwnPropSymbols(b)) {
27
+ if (__propIsEnum.call(b, prop))
28
+ __defNormalProp(a, prop, b[prop]);
29
+ }
30
+ return a;
31
+ };
32
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
33
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
34
+ var __decoratorStart = (base) => {
35
+ var _a2;
36
+ return [, , , __create((_a2 = base == null ? void 0 : base[__knownSymbol("metadata")]) != null ? _a2 : null)];
37
+ };
38
+ var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
39
+ var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
40
+ var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
41
+ var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
42
+ var __runInitializers = (array, flags, self, value) => {
43
+ 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);
44
+ return value;
45
+ };
46
+ var __decorateElement = (array, flags, name, decorators, target, extra) => {
47
+ var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
48
+ var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
49
+ var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
50
+ var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
51
+ return __privateGet(this, extra);
52
+ }, set [name](x) {
53
+ return __privateSet(this, extra, x);
54
+ } }, name));
55
+ k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
56
+ for (var i = decorators.length - 1; i >= 0; i--) {
57
+ ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
58
+ if (k) {
59
+ ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
60
+ if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
61
+ if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
62
+ }
63
+ 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;
64
+ if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
65
+ else if (typeof it !== "object" || it === null) __typeError("Object expected");
66
+ else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
67
+ }
68
+ return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
69
+ };
70
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
71
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
72
+ var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
73
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
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
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
76
+ var _destroy_dec, _init_dec, _registerOptionsTransformer_dec, _options_dec, _canvasScale_dec, _canInteract_dec, _endInteraction_dec, _startInteraction_dec, _dispatchNodeOperation_dec, _getNode_dec, _a, _init;
77
+ const DEFAULT_OPTIONS = {
78
+ inertia: {
79
+ enabled: true
80
+ },
81
+ modifiers: [
82
+ interact.modifiers.restrictRect({
83
+ restriction: `.${bem("canvas", "wrapper")}`,
84
+ endOnly: true
85
+ })
86
+ ]
87
+ };
88
+ class Drag extends (_a = BasePlugin, _getNode_dec = [inject.getNode()], _dispatchNodeOperation_dec = [inject.dispatchNodeOperation()], _startInteraction_dec = [inject.startInteraction()], _endInteraction_dec = [inject.endInteraction()], _canInteract_dec = [inject.canInteract()], _canvasScale_dec = [inject.canvas.transform((state) => state.scale)], _options_dec = [register("options")], _registerOptionsTransformer_dec = [register("registerOptionsTransformer")], _init_dec = [OnInit], _destroy_dec = [OnDestroy], _a) {
89
+ constructor() {
90
+ super(...arguments);
91
+ __runInitializers(_init, 5, this);
92
+ __publicField(this, "name", "drag");
93
+ __publicField(this, "getNode", __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this);
94
+ __publicField(this, "dispatchNodeOperation", __runInitializers(_init, 12, this)), __runInitializers(_init, 15, this);
95
+ __publicField(this, "startInteraction", __runInitializers(_init, 16, this)), __runInitializers(_init, 19, this);
96
+ __publicField(this, "endInteraction", __runInitializers(_init, 20, this)), __runInitializers(_init, 23, this);
97
+ __publicField(this, "canInteract", __runInitializers(_init, 24, this)), __runInitializers(_init, 27, this);
98
+ __publicField(this, "canvasScale", __runInitializers(_init, 28, this, 1)), __runInitializers(_init, 31, this);
99
+ __publicField(this, "subscription");
100
+ __publicField(this, "transformers$", new BehaviorSubject([]));
101
+ __publicField(this, "currentInteractable");
102
+ __publicField(this, "options", __runInitializers(_init, 32, this, DEFAULT_OPTIONS)), __runInitializers(_init, 35, this);
103
+ __publicField(this, "registerOptionsTransformer", __runInitializers(_init, 36, this, (transformer) => {
104
+ const transformers = this.transformers$.value;
105
+ transformers.push(transformer);
106
+ this.transformers$.next(transformers);
107
+ return () => {
108
+ const currentTransformers = this.transformers$.value;
109
+ const index = currentTransformers.indexOf(transformer);
110
+ if (index > -1) {
111
+ currentTransformers.splice(index, 1);
112
+ this.transformers$.next(currentTransformers);
113
+ }
114
+ };
115
+ })), __runInitializers(_init, 39, this);
116
+ }
117
+ /**
118
+ * 应用所有转换器,生成最终的配置
119
+ */
120
+ applyTransformers(nodeId) {
121
+ const node = this.getNode(nodeId);
122
+ if (!node)
123
+ return this.options;
124
+ const context = {
125
+ nodeId,
126
+ node,
127
+ defaultOptions: this.options,
128
+ currentOptions: this.options
129
+ };
130
+ return this.transformers$.value.reduce(
131
+ (options, transformer) => {
132
+ var _a2;
133
+ context.currentOptions = options;
134
+ return (_a2 = transformer(context)) != null ? _a2 : options;
135
+ },
136
+ this.options
137
+ );
138
+ }
139
+ /**
140
+ * 更新 interactable 的配置
141
+ */
142
+ updateInteractableOptions(event) {
143
+ var _a2;
144
+ if (!this.currentInteractable)
145
+ return;
146
+ const target = ((_a2 = event.__set) == null ? void 0 : _a2.currentTarget) || event.target;
147
+ const nodeId = target.getAttribute("data-node-id");
148
+ if (!nodeId)
149
+ return;
150
+ const options = this.applyTransformers(nodeId);
151
+ this.currentInteractable.draggable(options);
152
+ }
153
+ init() {
154
+ const dragStart$ = new Subject();
155
+ const dragMove$ = new Subject();
156
+ const dragEnd$ = new Subject();
157
+ const end = () => {
158
+ var _a2;
159
+ dragEnd$.next();
160
+ (_a2 = this.currentInteractable) == null ? void 0 : _a2.draggable(this.options);
161
+ this.endInteraction(this.pluginId, "position");
162
+ };
163
+ this.currentInteractable = interact(`.${bem("node", "wrapper")}[data-node-id]`).draggable(__spreadProps(__spreadValues({}, this.options), {
164
+ listeners: {
165
+ start: (event) => {
166
+ const target = event.target;
167
+ const nodeId = target.getAttribute("data-node-id");
168
+ if (!nodeId)
169
+ return;
170
+ dragStart$.next({
171
+ nodeId
172
+ });
173
+ this.startInteraction(this.pluginId, "position", InteractionPriority.EntityPositionDrag);
174
+ },
175
+ move: (event) => {
176
+ if (!this.canInteract(this.pluginId, "position")) {
177
+ end();
178
+ return;
179
+ }
180
+ const scale = this.canvasScale;
181
+ dragMove$.next({
182
+ dx: event.dx / scale,
183
+ dy: event.dy / scale
184
+ });
185
+ },
186
+ // 拖拽结束
187
+ end
188
+ }
189
+ })).on("down", (event) => this.updateInteractableOptions(event));
190
+ this.options = __spreadValues({}, this.currentInteractable.draggable());
191
+ this.subscription = dragStart$.pipe(
192
+ // 对每次拖拽开始事件,创建一个新的拖拽流程
193
+ switchMap((startEvent) => {
194
+ var _a2;
195
+ const draftId = `${this.pluginId}-${startEvent.nodeId}`;
196
+ const { nodeId } = startEvent;
197
+ const startPosition = (_a2 = this.getNode(nodeId)) == null ? void 0 : _a2.position;
198
+ const currentPosition = { x: (startPosition == null ? void 0 : startPosition.x) || 0, y: (startPosition == null ? void 0 : startPosition.y) || 0 };
199
+ this.dispatchNodeOperation({
200
+ type: "startDraft",
201
+ draftId
202
+ });
203
+ return dragMove$.pipe(
204
+ skip(1),
205
+ map((moveEvent) => {
206
+ currentPosition.x += moveEvent.dx;
207
+ currentPosition.y += moveEvent.dy;
208
+ return {
209
+ nodeId,
210
+ position: currentPosition
211
+ };
212
+ }),
213
+ tap((moveData) => {
214
+ this.dispatchNodeOperation({
215
+ type: "draftOperation",
216
+ draftId,
217
+ operation: {
218
+ type: "update",
219
+ id: moveData.nodeId,
220
+ data: {
221
+ position: {
222
+ x: currentPosition.x,
223
+ y: currentPosition.y
224
+ }
225
+ }
226
+ }
227
+ });
228
+ }),
229
+ takeUntil(
230
+ dragEnd$.pipe(
231
+ tap(() => {
232
+ this.dispatchNodeOperation({
233
+ type: "commitDraft",
234
+ draftId
235
+ });
236
+ })
237
+ )
238
+ )
239
+ );
240
+ })
241
+ ).subscribe();
242
+ this.subscription.add(() => {
243
+ var _a2;
244
+ (_a2 = this.currentInteractable) == null ? void 0 : _a2.unset();
245
+ dragStart$.complete();
246
+ dragMove$.complete();
247
+ dragEnd$.complete();
248
+ });
249
+ }
250
+ destroy() {
251
+ var _a2;
252
+ (_a2 = this.subscription) == null ? void 0 : _a2.unsubscribe();
253
+ }
254
+ }
255
+ _init = __decoratorStart(_a);
256
+ __decorateElement(_init, 1, "init", _init_dec, Drag);
257
+ __decorateElement(_init, 1, "destroy", _destroy_dec, Drag);
258
+ __decorateElement(_init, 5, "getNode", _getNode_dec, Drag);
259
+ __decorateElement(_init, 5, "dispatchNodeOperation", _dispatchNodeOperation_dec, Drag);
260
+ __decorateElement(_init, 5, "startInteraction", _startInteraction_dec, Drag);
261
+ __decorateElement(_init, 5, "endInteraction", _endInteraction_dec, Drag);
262
+ __decorateElement(_init, 5, "canInteract", _canInteract_dec, Drag);
263
+ __decorateElement(_init, 5, "canvasScale", _canvasScale_dec, Drag);
264
+ __decorateElement(_init, 5, "options", _options_dec, Drag);
265
+ __decorateElement(_init, 5, "registerOptionsTransformer", _registerOptionsTransformer_dec, Drag);
266
+ __decoratorMetadata(_init, Drag);
267
+
268
+ export { Drag };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@knotx/plugins-drag",
3
+ "type": "module",
4
+ "version": "0.0.0",
5
+ "description": "Drag 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-drag"
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
+ "@interactjs/actions": "^1.10.27",
33
+ "@interactjs/core": "^1.10.27",
34
+ "@interactjs/modifiers": "^1.10.27",
35
+ "@interactjs/types": "^1.10.27",
36
+ "interactjs": "^1.10.27",
37
+ "rxjs": "^7.8.1",
38
+ "@knotx/decorators": "0.0.0",
39
+ "@knotx/plugins-canvas": "0.0.0",
40
+ "@knotx/core": "0.0.0"
41
+ },
42
+ "devDependencies": {
43
+ "@knotx/jsx": "0.0.0",
44
+ "@knotx/build-config": "0.0.0",
45
+ "@knotx/eslint-config": "0.0.0",
46
+ "@knotx/typescript-config": "0.0.0"
47
+ },
48
+ "scripts": {
49
+ "build": "unbuild --failOnWarn=false",
50
+ "dev": "unbuild --stub",
51
+ "lint": "eslint .",
52
+ "typecheck": "tsc --noEmit"
53
+ }
54
+ }