slate-angular 1.7.3 → 1.9.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.
@@ -550,6 +550,34 @@
550
550
  }
551
551
  return text;
552
552
  };
553
+ /**
554
+ * Get x-slate-fragment attribute from data-slate-fragment
555
+ */
556
+ var catchSlateFragment = /data-slate-fragment="(.+?)"/m;
557
+ var getSlateFragmentAttribute = function (dataTransfer) {
558
+ var htmlData = dataTransfer.getData('text/html');
559
+ var _a = __read(htmlData.match(catchSlateFragment) || [], 2), fragment = _a[1];
560
+ return fragment;
561
+ };
562
+ /**
563
+ * Get the x-slate-fragment attribute that exist in text/html data
564
+ * and append it to the DataTransfer object
565
+ */
566
+ var getClipboardData = function (dataTransfer, clipboardFormatKey) {
567
+ if (clipboardFormatKey === void 0) { clipboardFormatKey = 'x-slate-fragment'; }
568
+ if (!dataTransfer.getData("application/" + clipboardFormatKey)) {
569
+ var fragment = getSlateFragmentAttribute(dataTransfer);
570
+ if (fragment) {
571
+ var clipboardData_1 = new DataTransfer();
572
+ dataTransfer.types.forEach(function (type) {
573
+ clipboardData_1.setData(type, dataTransfer.getData(type));
574
+ });
575
+ clipboardData_1.setData("application/" + clipboardFormatKey, fragment);
576
+ return clipboardData_1;
577
+ }
578
+ }
579
+ return dataTransfer;
580
+ };
553
581
 
554
582
  /**
555
583
  * An auto-incrementing identifier for keys.
@@ -571,6 +599,7 @@
571
599
  /iPad|iPhone|iPod/.test(navigator.userAgent) &&
572
600
  !window.MSStream;
573
601
  var IS_APPLE = typeof navigator !== 'undefined' && /Mac OS X/.test(navigator.userAgent);
602
+ var IS_ANDROID = typeof navigator !== 'undefined' && /Android/.test(navigator.userAgent);
574
603
  var IS_FIREFOX = typeof navigator !== 'undefined' &&
575
604
  /^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
576
605
  var IS_SAFARI = typeof navigator !== 'undefined' &&
@@ -582,6 +611,24 @@
582
611
  // Native beforeInput events don't work well with react on Chrome 75 and older, Chrome 76+ can use beforeInput
583
612
  var IS_CHROME_LEGACY = typeof navigator !== 'undefined' &&
584
613
  /Chrome?\/(?:[0-7][0-5]|[0-6][0-9])/i.test(navigator.userAgent);
614
+ // Firefox did not support `beforeInput` until `v87`.
615
+ var IS_FIREFOX_LEGACY = typeof navigator !== 'undefined' &&
616
+ /^(?!.*Seamonkey)(?=.*Firefox\/(?:[0-7][0-9]|[0-8][0-6])).*/i.test(navigator.userAgent);
617
+ // qq browser
618
+ var IS_QQBROWSER = typeof navigator !== 'undefined' && /.*QQBrowser/.test(navigator.userAgent);
619
+ // UC mobile browser
620
+ var IS_UC_MOBILE = typeof navigator !== 'undefined' && /.*UCBrowser/.test(navigator.userAgent);
621
+ // Wechat browser
622
+ var IS_WECHATBROWSER = typeof navigator !== 'undefined' && /.*Wechat/.test(navigator.userAgent);
623
+ // COMPAT: Firefox/Edge Legacy don't support the `beforeinput` event
624
+ // Chrome Legacy doesn't support `beforeinput` correctly
625
+ var HAS_BEFORE_INPUT_SUPPORT = !IS_CHROME_LEGACY &&
626
+ !IS_EDGE_LEGACY &&
627
+ // globalThis is undefined in older browsers
628
+ typeof globalThis !== 'undefined' &&
629
+ globalThis.InputEvent &&
630
+ // @ts-ignore The `getTargetRanges` property isn't recognized.
631
+ typeof globalThis.InputEvent.prototype.getTargetRanges === 'function';
585
632
 
586
633
  var FAKE_LEFT_BLOCK_CARD_OFFSET = -1;
587
634
  var FAKE_RIGHT_BLOCK_CARD_OFFSET = -2;
@@ -664,27 +711,16 @@
664
711
  throw new Error("Unable to find the path for Slate node: " + JSON.stringify(node));
665
712
  },
666
713
  /**
667
- * Find the DOM node that implements DocumentOrShadowRoot for the editor.
668
- */
714
+ * Find the DOM node that implements DocumentOrShadowRoot for the editor.
715
+ */
669
716
  findDocumentOrShadowRoot: function (editor) {
670
717
  var el = AngularEditor.toDOMNode(editor, editor);
671
718
  var root = el.getRootNode();
672
- // The below exception will always be thrown for iframes because the document inside an iframe
673
- // does not inherit it's prototype from the parent document, therefore we return early
674
- if (el.ownerDocument !== document) {
675
- return el.ownerDocument;
676
- }
677
- if (!(root instanceof Document || root instanceof ShadowRoot)) {
678
- throw new Error("Unable to find DocumentOrShadowRoot for editor element: " + el);
679
- }
680
- // COMPAT: Only Chrome implements the DocumentOrShadowRoot mixin for
681
- // ShadowRoot; other browsers still implement it on the Document
682
- // interface. (2020/08/08)
683
- // https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot#Properties
684
- if (root.getSelection === undefined && el.ownerDocument !== null) {
685
- return el.ownerDocument;
686
- }
687
- return root;
719
+ if ((root instanceof Document || root instanceof ShadowRoot) &&
720
+ root.getSelection != null) {
721
+ return root;
722
+ }
723
+ return el.ownerDocument;
688
724
  },
689
725
  /**
690
726
  * Check if the editor is focused.
@@ -782,6 +818,18 @@
782
818
  insertData: function (editor, data) {
783
819
  editor.insertData(data);
784
820
  },
821
+ /**
822
+ * Insert fragment data from a `DataTransfer` into the editor.
823
+ */
824
+ insertFragmentData: function (editor, data) {
825
+ return editor.insertFragmentData(data);
826
+ },
827
+ /**
828
+ * Insert text data from a `DataTransfer` into the editor.
829
+ */
830
+ insertTextData: function (editor, data) {
831
+ return editor.insertTextData(data);
832
+ },
785
833
  /**
786
834
  * onKeydown hook.
787
835
  */
@@ -797,8 +845,8 @@
797
845
  /**
798
846
  * Sets data from the currently selected fragment on a `DataTransfer`.
799
847
  */
800
- setFragmentData: function (editor, data) {
801
- editor.setFragmentData(data);
848
+ setFragmentData: function (editor, data, originEvent) {
849
+ editor.setFragmentData(data, originEvent);
802
850
  },
803
851
  deleteCutData: function (editor) {
804
852
  editor.deleteCutData();
@@ -1063,7 +1111,9 @@
1063
1111
  // composition the ASCII characters will be prepended to the zero-width
1064
1112
  // space, so subtract 1 from the offset to account for the zero-width
1065
1113
  // space character.
1066
- if (domNode_1 && offset === domNode_1.textContent.length && parentNode && parentNode.hasAttribute('data-slate-zero-width')) {
1114
+ if (domNode_1 &&
1115
+ offset === domNode_1.textContent.length &&
1116
+ (parentNode && parentNode.hasAttribute('data-slate-zero-width'))) {
1067
1117
  offset--;
1068
1118
  }
1069
1119
  }
@@ -1519,7 +1569,7 @@
1519
1569
  // in the HTML, and can be used for intra-Slate pasting. If it's a text
1520
1570
  // node, wrap it in a `<span>` so we have something to set an attribute on.
1521
1571
  if (isDOMText(attach)) {
1522
- var span = document.createElement('span');
1572
+ var span = attach.ownerDocument.createElement('span');
1523
1573
  // COMPAT: In Chrome and Safari, if we don't add the `white-space` style
1524
1574
  // then leading and trailing spaces will be ignored. (2017/09/21)
1525
1575
  span.style.whiteSpace = 'pre';
@@ -1533,13 +1583,14 @@
1533
1583
  attach.setAttribute('data-slate-fragment', encoded);
1534
1584
  data.setData("application/" + clipboardFormatKey, encoded);
1535
1585
  // Add the content to a <div> so that we can get its inner HTML.
1536
- var div = document.createElement('div');
1586
+ var div = contents.ownerDocument.createElement('div');
1537
1587
  div.appendChild(contents);
1538
1588
  div.setAttribute('hidden', 'true');
1539
- document.body.appendChild(div);
1589
+ contents.ownerDocument.body.appendChild(div);
1540
1590
  data.setData('text/html', div.innerHTML);
1541
1591
  data.setData('text/plain', getPlainText(div));
1542
- document.body.removeChild(div);
1592
+ contents.ownerDocument.body.removeChild(div);
1593
+ return data;
1543
1594
  };
1544
1595
  e.deleteCutData = function () {
1545
1596
  var selection = editor.selection;
@@ -1556,14 +1607,26 @@
1556
1607
  }
1557
1608
  };
1558
1609
  e.insertData = function (data) {
1559
- var e_5, _a;
1560
- var fragment = data.getData("application/" + clipboardFormatKey);
1610
+ if (!e.insertFragmentData(data)) {
1611
+ e.insertTextData(data);
1612
+ }
1613
+ };
1614
+ e.insertFragmentData = function (data) {
1615
+ /**
1616
+ * Checking copied fragment from application/x-slate-fragment or data-slate-fragment
1617
+ */
1618
+ var fragment = data.getData("application/" + clipboardFormatKey) ||
1619
+ getSlateFragmentAttribute(data);
1561
1620
  if (fragment) {
1562
1621
  var decoded = decodeURIComponent(window.atob(fragment));
1563
1622
  var parsed = JSON.parse(decoded);
1564
1623
  e.insertFragment(parsed);
1565
- return;
1624
+ return true;
1566
1625
  }
1626
+ return false;
1627
+ };
1628
+ e.insertTextData = function (data) {
1629
+ var e_5, _a;
1567
1630
  var text = data.getData('text/plain');
1568
1631
  if (text) {
1569
1632
  var lines = text.split(/\r\n|\r|\n/);
@@ -1585,7 +1648,9 @@
1585
1648
  }
1586
1649
  finally { if (e_5) throw e_5.error; }
1587
1650
  }
1651
+ return true;
1588
1652
  }
1653
+ return false;
1589
1654
  };
1590
1655
  e.onKeydown = function () { };
1591
1656
  e.onClick = function () { };
@@ -1963,12 +2028,21 @@
1963
2028
  enumerable: false,
1964
2029
  configurable: true
1965
2030
  });
2031
+ Object.defineProperty(SlateBlockCardComponent.prototype, "centerContainerElement", {
2032
+ get: function () {
2033
+ return this.centerContianer.nativeElement;
2034
+ },
2035
+ enumerable: false,
2036
+ configurable: true
2037
+ });
1966
2038
  SlateBlockCardComponent.prototype.ngOnInit = function () {
1967
- var fragment = document.createDocumentFragment();
1968
- fragment.append.apply(fragment, __spreadArray([], __read(this.centerRootNodes)));
1969
- this.centerContianer.nativeElement.appendChild(fragment);
2039
+ this.append();
1970
2040
  this.nativeElement.classList.add("slate-block-card");
1971
2041
  };
2042
+ SlateBlockCardComponent.prototype.append = function () {
2043
+ var _this = this;
2044
+ this.centerRootNodes.forEach(function (rootNode) { return !_this.centerContainerElement.contains(rootNode) && _this.centerContainerElement.appendChild(rootNode); });
2045
+ };
1972
2046
  SlateBlockCardComponent.prototype.initializeCenter = function (rootNodes) {
1973
2047
  this.centerRootNodes = rootNodes;
1974
2048
  };
@@ -2079,6 +2153,11 @@
2079
2153
  }
2080
2154
  }
2081
2155
  };
2156
+ ViewContainerItem.prototype.appendBlockCardElement = function () {
2157
+ if (this.blockCardComponentRef) {
2158
+ this.blockCardComponentRef.instance.append();
2159
+ }
2160
+ };
2082
2161
  return ViewContainerItem;
2083
2162
  }());
2084
2163
  ViewContainerItem.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.7", ngImport: i0__namespace, type: ViewContainerItem, deps: [{ token: i0__namespace.ViewContainerRef }, { token: i0__namespace.ComponentFactoryResolver }], target: i0__namespace.ɵɵFactoryTarget.Directive });
@@ -2178,35 +2257,28 @@
2178
2257
  this.cdr.markForCheck();
2179
2258
  };
2180
2259
  BaseLeafComponent.prototype.renderPlaceholder = function () {
2181
- var _a, _b, _c;
2182
2260
  // issue-1: IME input was interrupted
2183
2261
  // issue-2: IME input focus jumping
2184
- // Issue occurs when the placeholder node is removed (in leaf span)
2185
- // So add a placeholder span to the block element root node
2262
+ // Issue occurs when the span node of the placeholder is before the slateString span node
2186
2263
  if (this.context.leaf['placeholder']) {
2187
2264
  if (!this.placeholderElement) {
2188
2265
  this.placeholderElement = document.createElement('span');
2189
2266
  this.placeholderElement.innerText = this.context.leaf['placeholder'];
2190
2267
  this.placeholderElement.contentEditable = 'false';
2191
2268
  this.placeholderElement.setAttribute('data-slate-placeholder', 'true');
2192
- (_a = this.nativeElement.closest('[data-slate-node="element"]')) === null || _a === void 0 ? void 0 : _a.classList.add('element-placeholder');
2193
- (_b = this.nativeElement.closest('[data-slate-node="element"]')) === null || _b === void 0 ? void 0 : _b.appendChild(this.placeholderElement);
2269
+ this.nativeElement.classList.add('leaf-with-placeholder');
2270
+ this.nativeElement.appendChild(this.placeholderElement);
2194
2271
  }
2195
2272
  }
2196
2273
  else {
2197
- if (this.placeholderElement) {
2198
- this.placeholderElement.remove();
2199
- this.placeholderElement = null;
2200
- (_c = this.nativeElement.closest('[data-slate-node="element"]')) === null || _c === void 0 ? void 0 : _c.classList.remove('element-placeholder');
2201
- }
2274
+ this.destroyPlaceholder();
2202
2275
  }
2203
2276
  };
2204
2277
  BaseLeafComponent.prototype.destroyPlaceholder = function () {
2205
- var _a, _b;
2206
2278
  if (this.placeholderElement) {
2207
2279
  this.placeholderElement.remove();
2208
2280
  this.placeholderElement = null;
2209
- (_b = (_a = this.nativeElement) === null || _a === void 0 ? void 0 : _a.closest('[data-slate-node="element"]')) === null || _b === void 0 ? void 0 : _b.classList.remove('element-placeholder');
2281
+ this.nativeElement.classList.remove('leaf-with-placeholder');
2210
2282
  }
2211
2283
  };
2212
2284
  return BaseLeafComponent;
@@ -2448,6 +2520,8 @@
2448
2520
  });
2449
2521
  }
2450
2522
  }
2523
+ // Solve the block-card DOMElement loss when moving nodes
2524
+ record.item.appendBlockCardElement();
2451
2525
  };
2452
2526
  return ViewContainer;
2453
2527
  }());
@@ -2967,13 +3041,6 @@
2967
3041
  }] } });
2968
3042
 
2969
3043
  var timeDebug = Debug__default["default"]('slate-angular-time');
2970
- // COMPAT: Firefox/Edge Legacy don't support the `beforeinput` event
2971
- // Chrome Legacy doesn't support `beforeinput` correctly
2972
- var HAS_BEFORE_INPUT_SUPPORT = !IS_CHROME_LEGACY &&
2973
- !IS_EDGE_LEGACY &&
2974
- globalThis.InputEvent &&
2975
- // @ts-ignore The `getTargetRanges` property isn't recognized.
2976
- typeof globalThis.InputEvent.prototype.getTargetRanges === 'function';
2977
3044
  // not correctly clipboardData on beforeinput
2978
3045
  var forceOnDOMPaste = IS_SAFARI;
2979
3046
  var SlateEditableComponent = /** @class */ (function () {
@@ -3033,6 +3100,9 @@
3033
3100
  this.initializeContext();
3034
3101
  // remove unused DOM, just keep templateComponent instance
3035
3102
  this.templateElementRef.nativeElement.remove();
3103
+ // add browser class
3104
+ var browserClass = IS_FIREFOX ? 'firefox' : (IS_SAFARI ? 'safari' : '');
3105
+ browserClass && this.elementRef.nativeElement.classList.add(browserClass);
3036
3106
  };
3037
3107
  SlateEditableComponent.prototype.ngOnChanges = function (simpleChanges) {
3038
3108
  if (!this.initialized) {
@@ -3411,7 +3481,10 @@
3411
3481
  case 'insertFromYank':
3412
3482
  case 'insertReplacementText':
3413
3483
  case 'insertText': {
3414
- if (data instanceof DataTransfer) {
3484
+ // use a weak comparison instead of 'instanceof' to allow
3485
+ // programmatic access of paste events coming from external windows
3486
+ // like cypress where cy.window does not work realibly
3487
+ if ((data === null || data === void 0 ? void 0 : data.constructor.name) === 'DataTransfer') {
3415
3488
  AngularEditor.insertData(editor, data);
3416
3489
  }
3417
3490
  else if (typeof data === 'string') {
@@ -3523,13 +3596,13 @@
3523
3596
  var isOutsideSlate = !hasStringTarget(window.getSelection()) && isTargetInsideVoid(this.editor, event.target);
3524
3597
  if (!isOutsideSlate && hasTarget(this.editor, event.target) && !this.readonly && !this.isDOMEventHandled(event, this.copy)) {
3525
3598
  event.preventDefault();
3526
- AngularEditor.setFragmentData(this.editor, event.clipboardData);
3599
+ AngularEditor.setFragmentData(this.editor, event.clipboardData, 'copy');
3527
3600
  }
3528
3601
  };
3529
3602
  SlateEditableComponent.prototype.onDOMCut = function (event) {
3530
3603
  if (!this.readonly && hasEditableTarget(this.editor, event.target) && !this.isDOMEventHandled(event, this.cut)) {
3531
3604
  event.preventDefault();
3532
- AngularEditor.setFragmentData(this.editor, event.clipboardData);
3605
+ AngularEditor.setFragmentData(this.editor, event.clipboardData, 'cut');
3533
3606
  var selection = this.editor.selection;
3534
3607
  if (selection) {
3535
3608
  AngularEditor.deleteCutData(this.editor);
@@ -3548,7 +3621,7 @@
3548
3621
  }
3549
3622
  };
3550
3623
  SlateEditableComponent.prototype.onDOMDragStart = function (event) {
3551
- if (hasTarget(this.editor, event.target) && !this.isDOMEventHandled(event, this.dragStart)) {
3624
+ if (!this.readonly && hasTarget(this.editor, event.target) && !this.isDOMEventHandled(event, this.dragStart)) {
3552
3625
  var node = AngularEditor.toSlateNode(this.editor, event.target);
3553
3626
  var path = AngularEditor.findPath(this.editor, node);
3554
3627
  var voidMatch = slate.Editor.isVoid(this.editor, node) ||
@@ -3560,7 +3633,7 @@
3560
3633
  slate.Transforms.select(this.editor, range);
3561
3634
  }
3562
3635
  this.isDraggingInternally = true;
3563
- AngularEditor.setFragmentData(this.editor, event.dataTransfer);
3636
+ AngularEditor.setFragmentData(this.editor, event.dataTransfer, 'drag');
3564
3637
  }
3565
3638
  };
3566
3639
  SlateEditableComponent.prototype.onDOMDrop = function (event) {
@@ -4137,6 +4210,8 @@
4137
4210
  exports.ELEMENT_TO_NODE = ELEMENT_TO_NODE;
4138
4211
  exports.FAKE_LEFT_BLOCK_CARD_OFFSET = FAKE_LEFT_BLOCK_CARD_OFFSET;
4139
4212
  exports.FAKE_RIGHT_BLOCK_CARD_OFFSET = FAKE_RIGHT_BLOCK_CARD_OFFSET;
4213
+ exports.HAS_BEFORE_INPUT_SUPPORT = HAS_BEFORE_INPUT_SUPPORT;
4214
+ exports.IS_ANDROID = IS_ANDROID;
4140
4215
  exports.IS_APPLE = IS_APPLE;
4141
4216
  exports.IS_CHROME = IS_CHROME;
4142
4217
  exports.IS_CHROME_LEGACY = IS_CHROME_LEGACY;
@@ -4144,10 +4219,14 @@
4144
4219
  exports.IS_DRAGGING = IS_DRAGGING;
4145
4220
  exports.IS_EDGE_LEGACY = IS_EDGE_LEGACY;
4146
4221
  exports.IS_FIREFOX = IS_FIREFOX;
4222
+ exports.IS_FIREFOX_LEGACY = IS_FIREFOX_LEGACY;
4147
4223
  exports.IS_FOCUSED = IS_FOCUSED;
4148
4224
  exports.IS_IOS = IS_IOS;
4225
+ exports.IS_QQBROWSER = IS_QQBROWSER;
4149
4226
  exports.IS_READONLY = IS_READONLY;
4150
4227
  exports.IS_SAFARI = IS_SAFARI;
4228
+ exports.IS_UC_MOBILE = IS_UC_MOBILE;
4229
+ exports.IS_WECHATBROWSER = IS_WECHATBROWSER;
4151
4230
  exports.KEY_TO_ELEMENT = KEY_TO_ELEMENT;
4152
4231
  exports.Key = Key;
4153
4232
  exports.NODE_TO_ELEMENT = NODE_TO_ELEMENT;
@@ -4163,10 +4242,12 @@
4163
4242
  exports.SlateStringComponent = SlateStringComponent;
4164
4243
  exports.check = check;
4165
4244
  exports.getCardTargetAttribute = getCardTargetAttribute;
4245
+ exports.getClipboardData = getClipboardData;
4166
4246
  exports.getDefaultView = getDefaultView;
4167
4247
  exports.getEditableChild = getEditableChild;
4168
4248
  exports.getEditableChildAndIndex = getEditableChildAndIndex;
4169
4249
  exports.getPlainText = getPlainText;
4250
+ exports.getSlateFragmentAttribute = getSlateFragmentAttribute;
4170
4251
  exports.hasBeforeContextChange = hasBeforeContextChange;
4171
4252
  exports.hasBlockCard = hasBlockCard;
4172
4253
  exports.hasBlockCardWithNode = hasBlockCardWithNode;