slate-react 0.57.2 → 0.57.3

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.
@@ -6,6 +6,7 @@ import { DOMNode, DOMPoint, DOMRange, DOMSelection, DOMStaticRange } from '../ut
6
6
  */
7
7
  export interface ReactEditor extends Editor {
8
8
  insertData: (data: DataTransfer) => void;
9
+ setFragmentData: (data: DataTransfer) => void;
9
10
  }
10
11
  export declare const ReactEditor: {
11
12
  /**
@@ -46,6 +47,10 @@ export declare const ReactEditor: {
46
47
  * Insert data from a `DataTransfer` into the editor.
47
48
  */
48
49
  insertData(editor: ReactEditor, data: DataTransfer): void;
50
+ /**
51
+ * Sets data from the currently selected fragment on a `DataTransfer`.
52
+ */
53
+ setFragmentData(editor: ReactEditor, data: DataTransfer): void;
49
54
  /**
50
55
  * Find the native DOM element from a Slate node.
51
56
  */
@@ -1 +1 @@
1
- {"version":3,"file":"react-editor.d.ts","sourceRoot":"","sources":["../packages/slate-react/src/plugin/react-editor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAc,MAAM,OAAO,CAAA;AAEpE,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AAWlC,OAAO,EAEL,OAAO,EACP,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,cAAc,EAGf,MAAM,cAAc,CAAA;AAErB;;GAEG;AAEH,MAAM,WAAW,WAAY,SAAQ,MAAM;IACzC,UAAU,EAAE,CAAC,IAAI,EAAE,YAAY,KAAK,IAAI,CAAA;CACzC;AAED,eAAO,MAAM,WAAW;IACtB;;OAEG;;IAaH;;OAEG;;IAgCH;;OAEG;;IAMH;;OAEG;;IAMH;;OAEG;;IAWH;;OAEG;;IAWH;;OAEG;;IAeH;;OAEG;;;;IAuCH;;OAEG;;IAMH;;OAEG;;IAgBH;;OAEG;;IAkDH;;OAEG;;IAiBH;;OAEG;;IAkBH;;OAEG;;IAgEH;;OAEG;;IA2EH;;OAEG;;CAkDJ,CAAA"}
1
+ {"version":3,"file":"react-editor.d.ts","sourceRoot":"","sources":["../packages/slate-react/src/plugin/react-editor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAA0B,MAAM,OAAO,CAAA;AAEhF,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AAWlC,OAAO,EAEL,OAAO,EACP,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,cAAc,EAGf,MAAM,cAAc,CAAA;AAErB;;GAEG;AAEH,MAAM,WAAW,WAAY,SAAQ,MAAM;IACzC,UAAU,EAAE,CAAC,IAAI,EAAE,YAAY,KAAK,IAAI,CAAA;IACxC,eAAe,EAAE,CAAC,IAAI,EAAE,YAAY,KAAK,IAAI,CAAA;CAC9C;AAED,eAAO,MAAM,WAAW;IACtB;;OAEG;;IAaH;;OAEG;;IAgCH;;OAEG;;IAMH;;OAEG;;IAMH;;OAEG;;IAWH;;OAEG;;IAWH;;OAEG;;IAeH;;OAEG;;;;IAuCH;;OAEG;;IAMH;;OAEG;;IAMH;;OAEG;;IAgBH;;OAEG;;IAkDH;;OAEG;;IA+BH;;OAEG;;IAkBH;;OAEG;;IAgEH;;OAEG;;IA2EH;;OAEG;;CAkDJ,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"with-react.d.ts","sourceRoot":"","sources":["../packages/slate-react/src/plugin/with-react.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAqC,MAAM,OAAO,CAAA;AAEjE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAI5C;;GAEG;AAEH,eAAO,MAAM,SAAS,kDA2FrB,CAAA"}
1
+ {"version":3,"file":"with-react.d.ts","sourceRoot":"","sources":["../packages/slate-react/src/plugin/with-react.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAA4C,MAAM,OAAO,CAAA;AAExE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAK5C;;GAEG;AAEH,eAAO,MAAM,SAAS,kDAgLrB,CAAA"}
@@ -1113,6 +1113,35 @@
1113
1113
 
1114
1114
  return child;
1115
1115
  };
1116
+ /**
1117
+ * Get a plaintext representation of the content of a node, accounting for block
1118
+ * elements which get a newline appended.
1119
+ *
1120
+ * The domNode must be attached to the DOM.
1121
+ */
1122
+
1123
+ var getPlainText = function getPlainText(domNode) {
1124
+ var text = '';
1125
+
1126
+ if (isDOMText(domNode) && domNode.nodeValue) {
1127
+ return domNode.nodeValue;
1128
+ }
1129
+
1130
+ if (isDOMElement(domNode)) {
1131
+ for (var _i = 0, _Array$from = Array.from(domNode.childNodes); _i < _Array$from.length; _i++) {
1132
+ var childNode = _Array$from[_i];
1133
+ text += getPlainText(childNode);
1134
+ }
1135
+
1136
+ var display = getComputedStyle(domNode).getPropertyValue('display');
1137
+
1138
+ if (display === 'block' || display === 'list' || domNode.tagName === 'BR') {
1139
+ text += '\n';
1140
+ }
1141
+ }
1142
+
1143
+ return text;
1144
+ };
1116
1145
 
1117
1146
  var ReactEditor = {
1118
1147
  /**
@@ -1251,6 +1280,13 @@
1251
1280
  editor.insertData(data);
1252
1281
  },
1253
1282
 
1283
+ /**
1284
+ * Sets data from the currently selected fragment on a `DataTransfer`.
1285
+ */
1286
+ setFragmentData: function setFragmentData(editor, data) {
1287
+ editor.setFragmentData(data);
1288
+ },
1289
+
1254
1290
  /**
1255
1291
  * Find the native DOM element from a Slate node.
1256
1292
  */
@@ -1327,13 +1363,30 @@
1327
1363
  toDOMRange: function toDOMRange(editor, range) {
1328
1364
  var anchor = range.anchor,
1329
1365
  focus = range.focus;
1366
+ var isBackward = slate.Range.isBackward(range);
1330
1367
  var domAnchor = ReactEditor.toDOMPoint(editor, anchor);
1331
1368
  var domFocus = slate.Range.isCollapsed(range) ? domAnchor : ReactEditor.toDOMPoint(editor, focus);
1332
1369
  var domRange = window.document.createRange();
1333
- var start = slate.Range.isBackward(range) ? domFocus : domAnchor;
1334
- var end = slate.Range.isBackward(range) ? domAnchor : domFocus;
1335
- domRange.setStart(start[0], start[1]);
1336
- domRange.setEnd(end[0], end[1]);
1370
+
1371
+ var _ref = isBackward ? domFocus : domAnchor,
1372
+ _ref2 = slicedToArray(_ref, 2),
1373
+ startNode = _ref2[0],
1374
+ startOffset = _ref2[1];
1375
+
1376
+ var _ref3 = isBackward ? domAnchor : domFocus,
1377
+ _ref4 = slicedToArray(_ref3, 2),
1378
+ endNode = _ref4[0],
1379
+ endOffset = _ref4[1]; // A slate Point at zero-width Leaf always has an offset of 0 but a native DOM selection at
1380
+ // zero-width node has an offset of 1 so we have to check if we are in a zero-width node and
1381
+ // adjust the offset accordingly.
1382
+
1383
+
1384
+ var startEl = isDOMElement(startNode) ? startNode : startNode.parentElement;
1385
+ var isStartAtZeroWidth = !!startEl.getAttribute('data-slate-zero-width');
1386
+ var endEl = isDOMElement(endNode) ? endNode : endNode.parentElement;
1387
+ var isEndAtZeroWidth = !!endEl.getAttribute('data-slate-zero-width');
1388
+ domRange.setStart(startNode, isStartAtZeroWidth ? 1 : startOffset);
1389
+ domRange.setEnd(endNode, isEndAtZeroWidth ? 1 : endOffset);
1337
1390
  return domRange;
1338
1391
  },
1339
1392
 
@@ -1611,6 +1664,11 @@
1611
1664
  setKey(key + 1);
1612
1665
  }, [key, onChange]);
1613
1666
  EDITOR_TO_ON_CHANGE.set(editor, onContextChange);
1667
+ React.useEffect(function () {
1668
+ return function () {
1669
+ EDITOR_TO_ON_CHANGE.set(editor, function () {});
1670
+ };
1671
+ }, []);
1614
1672
  return React__default.createElement(SlateContext.Provider, {
1615
1673
  value: context
1616
1674
  }, React__default.createElement(EditorContext.Provider, {
@@ -28344,6 +28402,95 @@
28344
28402
  }
28345
28403
  };
28346
28404
 
28405
+ e.setFragmentData = function (data) {
28406
+ var selection = e.selection;
28407
+
28408
+ if (!selection) {
28409
+ return;
28410
+ }
28411
+
28412
+ var _Range$edges = slate.Range.edges(selection),
28413
+ _Range$edges2 = slicedToArray(_Range$edges, 2),
28414
+ start = _Range$edges2[0],
28415
+ end = _Range$edges2[1];
28416
+
28417
+ var startVoid = slate.Editor["void"](e, {
28418
+ at: start.path
28419
+ });
28420
+ var endVoid = slate.Editor["void"](e, {
28421
+ at: end.path
28422
+ });
28423
+
28424
+ if (slate.Range.isCollapsed(selection) && !startVoid) {
28425
+ return;
28426
+ } // Create a fake selection so that we can add a Base64-encoded copy of the
28427
+ // fragment to the HTML, to decode on future pastes.
28428
+
28429
+
28430
+ var domRange = ReactEditor.toDOMRange(e, selection);
28431
+ var contents = domRange.cloneContents();
28432
+ var attach = contents.childNodes[0]; // Make sure attach is non-empty, since empty nodes will not get copied.
28433
+
28434
+ contents.childNodes.forEach(function (node) {
28435
+ if (node.textContent && node.textContent.trim() !== '') {
28436
+ attach = node;
28437
+ }
28438
+ }); // COMPAT: If the end node is a void node, we need to move the end of the
28439
+ // range from the void node's spacer span, to the end of the void node's
28440
+ // content, since the spacer is before void's content in the DOM.
28441
+
28442
+ if (endVoid) {
28443
+ var _endVoid = slicedToArray(endVoid, 1),
28444
+ voidNode = _endVoid[0];
28445
+
28446
+ var r = domRange.cloneRange();
28447
+ var domNode = ReactEditor.toDOMNode(e, voidNode);
28448
+ r.setEndAfter(domNode);
28449
+ contents = r.cloneContents();
28450
+ } // COMPAT: If the start node is a void node, we need to attach the encoded
28451
+ // fragment to the void node's content node instead of the spacer, because
28452
+ // attaching it to empty `<div>/<span>` nodes will end up having it erased by
28453
+ // most browsers. (2018/04/27)
28454
+
28455
+
28456
+ if (startVoid) {
28457
+ attach = contents.querySelector('[data-slate-spacer]');
28458
+ } // Remove any zero-width space spans from the cloned DOM so that they don't
28459
+ // show up elsewhere when pasted.
28460
+
28461
+
28462
+ Array.from(contents.querySelectorAll('[data-slate-zero-width]')).forEach(function (zw) {
28463
+ var isNewline = zw.getAttribute('data-slate-zero-width') === 'n';
28464
+ zw.textContent = isNewline ? '\n' : '';
28465
+ }); // Set a `data-slate-fragment` attribute on a non-empty node, so it shows up
28466
+ // in the HTML, and can be used for intra-Slate pasting. If it's a text
28467
+ // node, wrap it in a `<span>` so we have something to set an attribute on.
28468
+
28469
+ if (isDOMText(attach)) {
28470
+ var span = document.createElement('span'); // COMPAT: In Chrome and Safari, if we don't add the `white-space` style
28471
+ // then leading and trailing spaces will be ignored. (2017/09/21)
28472
+
28473
+ span.style.whiteSpace = 'pre';
28474
+ span.appendChild(attach);
28475
+ contents.appendChild(span);
28476
+ attach = span;
28477
+ }
28478
+
28479
+ var fragment = e.getFragment();
28480
+ var string = JSON.stringify(fragment);
28481
+ var encoded = window.btoa(encodeURIComponent(string));
28482
+ attach.setAttribute('data-slate-fragment', encoded);
28483
+ data.setData('application/x-slate-fragment', encoded); // Add the content to a <div> so that we can get its inner HTML.
28484
+
28485
+ var div = document.createElement('div');
28486
+ div.appendChild(contents);
28487
+ div.setAttribute('hidden', 'true');
28488
+ document.body.appendChild(div);
28489
+ data.setData('text/html', div.innerHTML);
28490
+ data.setData('text/plain', getPlainText(div));
28491
+ document.body.removeChild(div);
28492
+ };
28493
+
28347
28494
  e.insertData = function (data) {
28348
28495
  var fragment = data.getData('application/x-slate-fragment');
28349
28496
 
@@ -29244,11 +29391,10 @@
29244
29391
 
29245
29392
  if (!selection && !hasDomSelection) {
29246
29393
  return;
29247
- }
29394
+ } // If the DOM selection is already correct, we're done.
29248
29395
 
29249
- var newDomRange = selection && ReactEditor.toDOMRange(editor, selection); // If the DOM selection is already correct, we're done.
29250
29396
 
29251
- if (hasDomSelection && newDomRange && isRangeEqual(domSelection.getRangeAt(0), newDomRange)) {
29397
+ if (hasDomSelection && selection && slate.Range.equals(ReactEditor.toSlateRange(editor, domSelection), selection)) {
29252
29398
  return;
29253
29399
  } // Otherwise the DOM selection is out of sync, so update it.
29254
29400
 
@@ -29256,6 +29402,7 @@
29256
29402
  var el = ReactEditor.toDOMNode(editor, editor);
29257
29403
  state.isUpdatingSelection = true;
29258
29404
  domSelection.removeAllRanges();
29405
+ var newDomRange = selection && ReactEditor.toDOMRange(editor, selection);
29259
29406
 
29260
29407
  if (newDomRange) {
29261
29408
  domSelection.addRange(newDomRange);
@@ -29455,7 +29602,6 @@
29455
29602
  var activeElement = window.document.activeElement;
29456
29603
  var el = ReactEditor.toDOMNode(editor, editor);
29457
29604
  var domSelection = window.getSelection();
29458
- var domRange = domSelection && domSelection.rangeCount > 0 && domSelection.getRangeAt(0);
29459
29605
 
29460
29606
  if (activeElement === el) {
29461
29607
  state.latestElement = activeElement;
@@ -29464,8 +29610,8 @@
29464
29610
  IS_FOCUSED["delete"](editor);
29465
29611
  }
29466
29612
 
29467
- if (domRange && hasEditableTarget(editor, domRange.startContainer) && hasEditableTarget(editor, domRange.endContainer)) {
29468
- var range = ReactEditor.toSlateRange(editor, domRange);
29613
+ if (domSelection && hasEditableTarget(editor, domSelection.anchorNode) && hasEditableTarget(editor, domSelection.focusNode)) {
29614
+ var range = ReactEditor.toSlateRange(editor, domSelection);
29469
29615
  slate.Transforms.select(editor, range);
29470
29616
  } else {
29471
29617
  slate.Transforms.deselect(editor);
@@ -29605,13 +29751,13 @@
29605
29751
  onCopy: React.useCallback(function (event) {
29606
29752
  if (hasEditableTarget(editor, event.target) && !isEventHandled(event, attributes.onCopy)) {
29607
29753
  event.preventDefault();
29608
- setFragmentData(event.clipboardData, editor);
29754
+ ReactEditor.setFragmentData(editor, event.clipboardData);
29609
29755
  }
29610
29756
  }, [attributes.onCopy]),
29611
29757
  onCut: React.useCallback(function (event) {
29612
29758
  if (!readOnly && hasEditableTarget(editor, event.target) && !isEventHandled(event, attributes.onCut)) {
29613
29759
  event.preventDefault();
29614
- setFragmentData(event.clipboardData, editor);
29760
+ ReactEditor.setFragmentData(editor, event.clipboardData);
29615
29761
  var selection = editor.selection;
29616
29762
 
29617
29763
  if (selection && slate.Range.isExpanded(selection)) {
@@ -29645,7 +29791,7 @@
29645
29791
  slate.Transforms.select(editor, range);
29646
29792
  }
29647
29793
 
29648
- setFragmentData(event.dataTransfer, editor);
29794
+ ReactEditor.setFragmentData(editor, event.dataTransfer);
29649
29795
  }
29650
29796
  }, [attributes.onDragStart]),
29651
29797
  onDrop: React.useCallback(function (event) {
@@ -29924,14 +30070,6 @@
29924
30070
  var defaultDecorate = function defaultDecorate() {
29925
30071
  return [];
29926
30072
  };
29927
- /**
29928
- * Check if two DOM range objects are equal.
29929
- */
29930
-
29931
-
29932
- var isRangeEqual = function isRangeEqual(a, b) {
29933
- return a.startContainer === b.startContainer && a.startOffset === b.startOffset && a.endContainer === b.endContainer && a.endOffset === b.endOffset || a.startContainer === b.endContainer && a.startOffset === b.endOffset && a.endContainer === b.startContainer && a.endOffset === b.startOffset;
29934
- };
29935
30073
  /**
29936
30074
  * Check if the target is in the editor.
29937
30075
  */
@@ -29976,129 +30114,6 @@
29976
30114
  handler(event);
29977
30115
  return event.defaultPrevented;
29978
30116
  };
29979
- /**
29980
- * Set the currently selected fragment to the clipboard.
29981
- */
29982
-
29983
-
29984
- var setFragmentData = function setFragmentData(dataTransfer, editor) {
29985
- var selection = editor.selection;
29986
-
29987
- if (!selection) {
29988
- return;
29989
- }
29990
-
29991
- var _Range$edges = slate.Range.edges(selection),
29992
- _Range$edges2 = slicedToArray(_Range$edges, 2),
29993
- start = _Range$edges2[0],
29994
- end = _Range$edges2[1];
29995
-
29996
- var startVoid = slate.Editor["void"](editor, {
29997
- at: start.path
29998
- });
29999
- var endVoid = slate.Editor["void"](editor, {
30000
- at: end.path
30001
- });
30002
-
30003
- if (slate.Range.isCollapsed(selection) && !startVoid) {
30004
- return;
30005
- } // Create a fake selection so that we can add a Base64-encoded copy of the
30006
- // fragment to the HTML, to decode on future pastes.
30007
-
30008
-
30009
- var domRange = ReactEditor.toDOMRange(editor, selection);
30010
- var contents = domRange.cloneContents();
30011
- var attach = contents.childNodes[0]; // Make sure attach is non-empty, since empty nodes will not get copied.
30012
-
30013
- contents.childNodes.forEach(function (node) {
30014
- if (node.textContent && node.textContent.trim() !== '') {
30015
- attach = node;
30016
- }
30017
- }); // COMPAT: If the end node is a void node, we need to move the end of the
30018
- // range from the void node's spacer span, to the end of the void node's
30019
- // content, since the spacer is before void's content in the DOM.
30020
-
30021
- if (endVoid) {
30022
- var _endVoid = slicedToArray(endVoid, 1),
30023
- voidNode = _endVoid[0];
30024
-
30025
- var r = domRange.cloneRange();
30026
- var domNode = ReactEditor.toDOMNode(editor, voidNode);
30027
- r.setEndAfter(domNode);
30028
- contents = r.cloneContents();
30029
- } // COMPAT: If the start node is a void node, we need to attach the encoded
30030
- // fragment to the void node's content node instead of the spacer, because
30031
- // attaching it to empty `<div>/<span>` nodes will end up having it erased by
30032
- // most browsers. (2018/04/27)
30033
-
30034
-
30035
- if (startVoid) {
30036
- attach = contents.querySelector('[data-slate-spacer]');
30037
- } // Remove any zero-width space spans from the cloned DOM so that they don't
30038
- // show up elsewhere when pasted.
30039
-
30040
-
30041
- Array.from(contents.querySelectorAll('[data-slate-zero-width]')).forEach(function (zw) {
30042
- var isNewline = zw.getAttribute('data-slate-zero-width') === 'n';
30043
- zw.textContent = isNewline ? '\n' : '';
30044
- }); // Set a `data-slate-fragment` attribute on a non-empty node, so it shows up
30045
- // in the HTML, and can be used for intra-Slate pasting. If it's a text
30046
- // node, wrap it in a `<span>` so we have something to set an attribute on.
30047
-
30048
- if (isDOMText(attach)) {
30049
- var span = document.createElement('span'); // COMPAT: In Chrome and Safari, if we don't add the `white-space` style
30050
- // then leading and trailing spaces will be ignored. (2017/09/21)
30051
-
30052
- span.style.whiteSpace = 'pre';
30053
- span.appendChild(attach);
30054
- contents.appendChild(span);
30055
- attach = span;
30056
- }
30057
-
30058
- var fragment = slate.Node.fragment(editor, selection);
30059
- var string = JSON.stringify(fragment);
30060
- var encoded = window.btoa(encodeURIComponent(string));
30061
- attach.setAttribute('data-slate-fragment', encoded);
30062
- dataTransfer.setData('application/x-slate-fragment', encoded); // Add the content to a <div> so that we can get its inner HTML.
30063
-
30064
- var div = document.createElement('div');
30065
- div.appendChild(contents);
30066
- div.setAttribute('hidden', 'true');
30067
- document.body.appendChild(div);
30068
- dataTransfer.setData('text/html', div.innerHTML);
30069
- dataTransfer.setData('text/plain', getPlainText(div));
30070
- document.body.removeChild(div);
30071
- };
30072
- /**
30073
- * Get a plaintext representation of the content of a node, accounting for block
30074
- * elements which get a newline appended.
30075
- *
30076
- * The domNode must be attached to the DOM.
30077
- */
30078
-
30079
-
30080
- var getPlainText = function getPlainText(domNode) {
30081
- var text = '';
30082
-
30083
- if (isDOMText(domNode) && domNode.nodeValue) {
30084
- return domNode.nodeValue;
30085
- }
30086
-
30087
- if (isDOMElement(domNode)) {
30088
- for (var _i = 0, _Array$from = Array.from(domNode.childNodes); _i < _Array$from.length; _i++) {
30089
- var childNode = _Array$from[_i];
30090
- text += getPlainText(childNode);
30091
- }
30092
-
30093
- var display = getComputedStyle(domNode).getPropertyValue('display');
30094
-
30095
- if (display === 'block' || display === 'list' || domNode.tagName === 'BR') {
30096
- text += '\n';
30097
- }
30098
- }
30099
-
30100
- return text;
30101
- };
30102
30117
 
30103
30118
  exports.DefaultElement = DefaultElement;
30104
30119
  exports.DefaultLeaf = DefaultLeaf;