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