@antv/g-plugin-dragndrop 1.0.5-alpha.1 → 1.1.1
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/DragndropPlugin.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +238 -144
- package/dist/index.js +238 -144
- package/dist/index.umd.js +238 -144
- package/dist/index.umd.min.js +2 -2
- package/dist/tokens.d.ts +18 -0
- package/dist/tokens.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -5,6 +5,32 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var g = require('@antv/g');
|
|
6
6
|
var tslib = require('tslib');
|
|
7
7
|
|
|
8
|
+
function ownKeys(object, enumerableOnly) {
|
|
9
|
+
var keys = Object.keys(object);
|
|
10
|
+
|
|
11
|
+
if (Object.getOwnPropertySymbols) {
|
|
12
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
13
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
14
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
15
|
+
})), keys.push.apply(keys, symbols);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return keys;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function _objectSpread2(target) {
|
|
22
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
23
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
|
24
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
|
25
|
+
_defineProperty(target, key, source[key]);
|
|
26
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
|
27
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return target;
|
|
32
|
+
}
|
|
33
|
+
|
|
8
34
|
function _regeneratorRuntime() {
|
|
9
35
|
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
|
|
10
36
|
|
|
@@ -386,22 +412,44 @@ function _asyncToGenerator(fn) {
|
|
|
386
412
|
};
|
|
387
413
|
}
|
|
388
414
|
|
|
389
|
-
function
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
415
|
+
function _classCallCheck(instance, Constructor) {
|
|
416
|
+
if (!(instance instanceof Constructor)) {
|
|
417
|
+
throw new TypeError("Cannot call a class as a function");
|
|
418
|
+
}
|
|
419
|
+
}
|
|
393
420
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
421
|
+
function _defineProperties(target, props) {
|
|
422
|
+
for (var i = 0; i < props.length; i++) {
|
|
423
|
+
var descriptor = props[i];
|
|
424
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
425
|
+
descriptor.configurable = true;
|
|
426
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
427
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
400
430
|
|
|
401
|
-
|
|
402
|
-
|
|
431
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
432
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
433
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
434
|
+
Object.defineProperty(Constructor, "prototype", {
|
|
435
|
+
writable: false
|
|
436
|
+
});
|
|
437
|
+
return Constructor;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function _defineProperty(obj, key, value) {
|
|
441
|
+
if (key in obj) {
|
|
442
|
+
Object.defineProperty(obj, key, {
|
|
443
|
+
value: value,
|
|
444
|
+
enumerable: true,
|
|
445
|
+
configurable: true,
|
|
446
|
+
writable: true
|
|
447
|
+
});
|
|
448
|
+
} else {
|
|
449
|
+
obj[key] = value;
|
|
450
|
+
}
|
|
403
451
|
|
|
404
|
-
return
|
|
452
|
+
return obj;
|
|
405
453
|
}
|
|
406
454
|
|
|
407
455
|
var DragndropPluginOptions = g.Syringe.defineToken('DragndropPluginOptions');
|
|
@@ -411,137 +459,177 @@ var _class;
|
|
|
411
459
|
var DragndropPlugin_1;
|
|
412
460
|
var DragndropPlugin = DragndropPlugin_1 = (_class = /*#__PURE__*/function () {
|
|
413
461
|
function DragndropPlugin() {
|
|
462
|
+
_classCallCheck(this, DragndropPlugin);
|
|
463
|
+
|
|
414
464
|
this.sceneGraphService = void 0;
|
|
415
465
|
this.renderingContext = void 0;
|
|
416
466
|
this.dragndropPluginOptions = void 0;
|
|
417
467
|
}
|
|
418
468
|
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
case 7:
|
|
450
|
-
elemBelow = _context.sent;
|
|
451
|
-
target.style.pointerEvents = pointerEventsOldValue;
|
|
452
|
-
|
|
453
|
-
if (elemBelow) {
|
|
454
|
-
_context.next = 11;
|
|
455
|
-
break;
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
return _context.abrupt("return");
|
|
459
|
-
|
|
460
|
-
case 11:
|
|
461
|
-
droppableBelow = (elemBelow === null || elemBelow === void 0 ? void 0 : elemBelow.closest('[droppable=true]')) || null;
|
|
462
|
-
|
|
463
|
-
if (currentDroppable !== droppableBelow) {
|
|
464
|
-
if (currentDroppable) {
|
|
465
|
-
// null when we were not over a droppable before this event
|
|
466
|
-
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/Document/dragleave_event
|
|
467
|
-
event.type = 'dragleave';
|
|
468
|
-
event.target = currentDroppable;
|
|
469
|
-
currentDroppable.dispatchEvent(event);
|
|
469
|
+
_createClass(DragndropPlugin, [{
|
|
470
|
+
key: "apply",
|
|
471
|
+
value: function apply(renderingService) {
|
|
472
|
+
var document = this.renderingContext.root.ownerDocument; // TODO: should we add an option like `draggable` to Canvas
|
|
473
|
+
|
|
474
|
+
var canvas = document.defaultView;
|
|
475
|
+
var _this$dragndropPlugin = this.dragndropPluginOptions,
|
|
476
|
+
overlap = _this$dragndropPlugin.overlap,
|
|
477
|
+
isDocumentDraggable = _this$dragndropPlugin.isDocumentDraggable,
|
|
478
|
+
isDocumentDroppable = _this$dragndropPlugin.isDocumentDroppable,
|
|
479
|
+
dragstartDistanceThreshold = _this$dragndropPlugin.dragstartDistanceThreshold,
|
|
480
|
+
dragstartTimeThreshold = _this$dragndropPlugin.dragstartTimeThreshold;
|
|
481
|
+
|
|
482
|
+
var handlePointerdown = function handlePointerdown(event) {
|
|
483
|
+
var target = event.target;
|
|
484
|
+
var isDocument = target === document;
|
|
485
|
+
|
|
486
|
+
if (isDocument && isDocumentDraggable || target.getAttribute && target.getAttribute('draggable')) {
|
|
487
|
+
// @ts-ignore
|
|
488
|
+
var onMouseMove = /*#__PURE__*/function () {
|
|
489
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(event) {
|
|
490
|
+
var timeElapsed, distanceMoved, pointerEventsOldValue, point, elemBelow, droppableBelow;
|
|
491
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
492
|
+
while (1) {
|
|
493
|
+
switch (_context.prev = _context.next) {
|
|
494
|
+
case 0:
|
|
495
|
+
if (dragstartTriggered) {
|
|
496
|
+
_context.next = 8;
|
|
497
|
+
break;
|
|
470
498
|
}
|
|
471
499
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
500
|
+
timeElapsed = event.timeStamp - dragstartTimeStamp;
|
|
501
|
+
distanceMoved = Math.sqrt(Math.pow(event.canvasX - dragstartCanvasCoordinates[0], 2) + Math.pow(event.canvasY - dragstartCanvasCoordinates[1], 2)); // check thresholds
|
|
502
|
+
|
|
503
|
+
if (!(timeElapsed <= dragstartTimeThreshold || distanceMoved <= dragstartDistanceThreshold)) {
|
|
504
|
+
_context.next = 5;
|
|
505
|
+
break;
|
|
477
506
|
}
|
|
478
507
|
|
|
479
|
-
|
|
508
|
+
return _context.abrupt("return");
|
|
509
|
+
|
|
510
|
+
case 5:
|
|
511
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/Document/dragstart_event
|
|
512
|
+
event.type = 'dragstart';
|
|
513
|
+
target.dispatchEvent(event);
|
|
514
|
+
dragstartTriggered = true;
|
|
515
|
+
|
|
516
|
+
case 8:
|
|
517
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/Document/drag_event
|
|
518
|
+
event.type = 'drag';
|
|
519
|
+
target.dispatchEvent(event);
|
|
480
520
|
|
|
481
|
-
if (
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
event.type = 'dragover';
|
|
485
|
-
event.target = currentDroppable;
|
|
486
|
-
currentDroppable.dispatchEvent(event);
|
|
521
|
+
if (isDocument) {
|
|
522
|
+
_context.next = 20;
|
|
523
|
+
break;
|
|
487
524
|
}
|
|
488
|
-
}
|
|
489
525
|
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
526
|
+
// prevent from picking the dragging element
|
|
527
|
+
pointerEventsOldValue = target.style.pointerEvents;
|
|
528
|
+
target.style.pointerEvents = 'none';
|
|
529
|
+
point = overlap === 'pointer' ? [event.canvasX, event.canvasY] : target.getBounds().center;
|
|
530
|
+
_context.next = 16;
|
|
531
|
+
return document.elementFromPoint(point[0], point[1]);
|
|
532
|
+
|
|
533
|
+
case 16:
|
|
534
|
+
elemBelow = _context.sent;
|
|
535
|
+
target.style.pointerEvents = pointerEventsOldValue;
|
|
536
|
+
droppableBelow = (elemBelow === null || elemBelow === void 0 ? void 0 : elemBelow.closest('[droppable=true]')) || (isDocumentDroppable ? document : null);
|
|
537
|
+
|
|
538
|
+
if (currentDroppable !== droppableBelow) {
|
|
539
|
+
if (currentDroppable) {
|
|
540
|
+
// null when we were not over a droppable before this event
|
|
541
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/Document/dragleave_event
|
|
542
|
+
event.type = 'dragleave';
|
|
543
|
+
event.target = currentDroppable;
|
|
544
|
+
currentDroppable.dispatchEvent(event);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
if (droppableBelow) {
|
|
548
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/Document/dragleave_event
|
|
549
|
+
event.type = 'dragenter';
|
|
550
|
+
event.target = droppableBelow;
|
|
551
|
+
droppableBelow.dispatchEvent(event);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
currentDroppable = droppableBelow;
|
|
555
|
+
|
|
556
|
+
if (currentDroppable) {
|
|
557
|
+
// null if we're not coming over a droppable now
|
|
558
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/Document/dragover_event
|
|
559
|
+
event.type = 'dragover';
|
|
560
|
+
event.target = currentDroppable;
|
|
561
|
+
currentDroppable.dispatchEvent(event);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
case 20:
|
|
566
|
+
case "end":
|
|
567
|
+
return _context.stop();
|
|
568
|
+
}
|
|
493
569
|
}
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
}));
|
|
570
|
+
}, _callee);
|
|
571
|
+
}));
|
|
497
572
|
|
|
498
|
-
|
|
499
|
-
|
|
573
|
+
return function onMouseMove(_x) {
|
|
574
|
+
return _ref.apply(this, arguments);
|
|
575
|
+
};
|
|
576
|
+
}();
|
|
577
|
+
|
|
578
|
+
// delay triggering dragstart event
|
|
579
|
+
var dragstartTriggered = false;
|
|
580
|
+
var dragstartTimeStamp = event.timeStamp;
|
|
581
|
+
var dragstartCanvasCoordinates = [event.canvasX, event.canvasY];
|
|
582
|
+
var currentDroppable = null;
|
|
583
|
+
canvas.addEventListener('pointermove', onMouseMove);
|
|
584
|
+
|
|
585
|
+
var stopDragging = function stopDragging() {
|
|
586
|
+
if (dragstartTriggered) {
|
|
587
|
+
// drop should fire before dragend
|
|
588
|
+
// @see https://javascript.tutorialink.com/is-there-a-defined-ordering-between-dragend-and-drop-events/
|
|
589
|
+
if (currentDroppable) {
|
|
590
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/Document/drop_event
|
|
591
|
+
event.type = 'drop';
|
|
592
|
+
event.target = currentDroppable;
|
|
593
|
+
currentDroppable.dispatchEvent(event);
|
|
594
|
+
} // @see https://developer.mozilla.org/zh-CN/docs/Web/API/Document/dragend_event
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
event.type = 'dragend';
|
|
598
|
+
target.dispatchEvent(event);
|
|
599
|
+
dragstartTriggered = false;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
canvas.removeEventListener('pointermove', onMouseMove);
|
|
500
603
|
};
|
|
501
|
-
}();
|
|
502
|
-
|
|
503
|
-
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/Document/dragstart_event
|
|
504
|
-
event.type = 'dragstart';
|
|
505
|
-
target.dispatchEvent(event);
|
|
506
|
-
var currentDroppable = null;
|
|
507
|
-
document.addEventListener('pointermove', onMouseMove);
|
|
508
|
-
target.addEventListener('pointerup', function () {
|
|
509
|
-
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/Document/dragend_event
|
|
510
|
-
event.type = 'dragend';
|
|
511
|
-
target.dispatchEvent(event);
|
|
512
|
-
|
|
513
|
-
if (currentDroppable) {
|
|
514
|
-
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/Document/drop_event
|
|
515
|
-
event.type = 'drop';
|
|
516
|
-
event.target = currentDroppable;
|
|
517
|
-
currentDroppable.dispatchEvent(event);
|
|
518
|
-
}
|
|
519
604
|
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
605
|
+
target.addEventListener('pointerup', stopDragging, {
|
|
606
|
+
once: true
|
|
607
|
+
});
|
|
608
|
+
target.addEventListener('pointerupoutside', stopDragging, {
|
|
609
|
+
once: true
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
};
|
|
526
613
|
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
614
|
+
renderingService.hooks.init.tapPromise(DragndropPlugin_1.tag, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
615
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
616
|
+
while (1) {
|
|
617
|
+
switch (_context2.prev = _context2.next) {
|
|
618
|
+
case 0:
|
|
619
|
+
canvas.addEventListener('pointerdown', handlePointerdown);
|
|
533
620
|
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
621
|
+
case 1:
|
|
622
|
+
case "end":
|
|
623
|
+
return _context2.stop();
|
|
624
|
+
}
|
|
537
625
|
}
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
}
|
|
544
|
-
};
|
|
626
|
+
}, _callee2);
|
|
627
|
+
})));
|
|
628
|
+
renderingService.hooks.destroy.tap(DragndropPlugin_1.tag, function () {
|
|
629
|
+
canvas.removeEventListener('pointerdown', handlePointerdown);
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
}]);
|
|
545
633
|
|
|
546
634
|
return DragndropPlugin;
|
|
547
635
|
}(), _class.tag = 'Dragndrop', _class);
|
|
@@ -560,31 +648,37 @@ var containerModule = g.Module(function (register) {
|
|
|
560
648
|
register(DragndropPlugin);
|
|
561
649
|
});
|
|
562
650
|
var Plugin = /*#__PURE__*/function () {
|
|
563
|
-
function Plugin(
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
651
|
+
function Plugin() {
|
|
652
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
653
|
+
|
|
654
|
+
_classCallCheck(this, Plugin);
|
|
567
655
|
|
|
568
656
|
this.options = void 0;
|
|
569
657
|
this.name = 'dragndrop';
|
|
570
658
|
this.options = options;
|
|
571
659
|
}
|
|
572
660
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
}
|
|
661
|
+
_createClass(Plugin, [{
|
|
662
|
+
key: "init",
|
|
663
|
+
value: function init(container) {
|
|
664
|
+
container.register(DragndropPluginOptions, {
|
|
665
|
+
useValue: _objectSpread2({
|
|
666
|
+
overlap: 'pointer',
|
|
667
|
+
isDocumentDraggable: false,
|
|
668
|
+
isDocumentDroppable: false,
|
|
669
|
+
dragstartDistanceThreshold: 0,
|
|
670
|
+
dragstartTimeThreshold: 0
|
|
671
|
+
}, this.options)
|
|
672
|
+
});
|
|
673
|
+
container.load(containerModule, true);
|
|
674
|
+
}
|
|
675
|
+
}, {
|
|
676
|
+
key: "destroy",
|
|
677
|
+
value: function destroy(container) {
|
|
678
|
+
container.remove(DragndropPluginOptions);
|
|
679
|
+
container.unload(containerModule);
|
|
680
|
+
}
|
|
681
|
+
}]);
|
|
588
682
|
|
|
589
683
|
return Plugin;
|
|
590
684
|
}();
|