slate-react 0.88.2 → 0.89.0-20230315644

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/index.js CHANGED
@@ -8,6 +8,7 @@ var throttle = require('lodash/throttle');
8
8
  var React = require('react');
9
9
  var scrollIntoView = require('scroll-into-view-if-needed');
10
10
  var slate = require('slate');
11
+ var resizeObserver = require('@juggle/resize-observer');
11
12
  var isHotkey = require('is-hotkey');
12
13
  var ReactDOM = require('react-dom');
13
14
 
@@ -990,7 +991,7 @@ var ReactEditor = {
990
991
  // next or previous node, depending on which side the `x` and `y`
991
992
  // coordinates are closest to.
992
993
 
993
- if (slate.Editor.isVoid(editor, node)) {
994
+ if (slate.Element.isElement(node) && slate.Editor.isVoid(editor, node)) {
994
995
  var rect = target.getBoundingClientRect();
995
996
  var isPrev = editor.isInline(node) ? x - rect.left < rect.left + rect.width - x : y - rect.top < rect.top + rect.height - y;
996
997
  var edge = slate.Editor.point(editor, path, {
@@ -1232,6 +1233,46 @@ var ReactEditor = {
1232
1233
  if (!focus) {
1233
1234
  return null;
1234
1235
  }
1236
+ /**
1237
+ * suppose we have this document:
1238
+ *
1239
+ * { type: 'paragraph',
1240
+ * children: [
1241
+ * { text: 'foo ' },
1242
+ * { text: 'bar' },
1243
+ * { text: ' baz' }
1244
+ * ]
1245
+ * }
1246
+ *
1247
+ * a double click on "bar" on chrome will create this range:
1248
+ *
1249
+ * anchor -> [0,1] offset 0
1250
+ * focus -> [0,1] offset 3
1251
+ *
1252
+ * while on firefox will create this range:
1253
+ *
1254
+ * anchor -> [0,0] offset 4
1255
+ * focus -> [0,2] offset 0
1256
+ *
1257
+ * let's try to fix it...
1258
+ */
1259
+
1260
+
1261
+ if (IS_FIREFOX && !isCollapsed && anchorNode !== focusNode) {
1262
+ var isEnd = slate.Editor.isEnd(editor, anchor, anchor.path);
1263
+ var isStart = slate.Editor.isStart(editor, focus, focus.path);
1264
+
1265
+ if (isEnd) {
1266
+ var after = slate.Editor.after(editor, anchor); // Editor.after() might return undefined
1267
+
1268
+ anchor = after || anchor;
1269
+ }
1270
+
1271
+ if (isStart) {
1272
+ var before = slate.Editor.before(editor, focus);
1273
+ focus = before || focus;
1274
+ }
1275
+ }
1235
1276
 
1236
1277
  var range = {
1237
1278
  anchor: anchor,
@@ -1287,7 +1328,7 @@ var ReactEditor = {
1287
1328
  isTargetInsideNonReadonlyVoid: function isTargetInsideNonReadonlyVoid(editor, target) {
1288
1329
  if (IS_READ_ONLY.get(editor)) return false;
1289
1330
  var slateNode = ReactEditor.hasTarget(editor, target) && ReactEditor.toSlateNode(editor, target);
1290
- return slate.Editor.isVoid(editor, slateNode);
1331
+ return slate.Element.isElement(slateNode) && slate.Editor.isVoid(editor, slateNode);
1291
1332
  },
1292
1333
 
1293
1334
  /**
@@ -1497,6 +1538,22 @@ var useSlateStatic = function useSlateStatic() {
1497
1538
  return editor;
1498
1539
  };
1499
1540
 
1541
+ /**
1542
+ * Returns a set of rules that use the `:where` selector if it is supported,
1543
+ * otherwise it falls back to the provided selector on its own.
1544
+ *
1545
+ * The `:where` selector is used to give a selector a lower specificity,
1546
+ * allowing the rule to be overridden by a user-defined stylesheet.
1547
+ *
1548
+ * Older browsers do not support the `:where` selector.
1549
+ * If it is not supported, the selector will be used without `:where`,
1550
+ * which means that the rule will have a higher specificity and a user-defined
1551
+ * stylesheet will not be able to override it easily.
1552
+ */
1553
+ function whereIfSupported(selector, styles) {
1554
+ return "@supports (selector(:where(".concat(selector, "))) {") + ":where(".concat(selector, ") { ").concat(styles, " }") + "}" + "@supports not (selector(:where(".concat(selector, "))) {") + "".concat(selector, " { ").concat(styles, " }") + "}";
1555
+ }
1556
+
1500
1557
  /**
1501
1558
  * Individual leaves in a text node with unique formatting.
1502
1559
  */
@@ -1536,6 +1593,7 @@ var Leaf = function Leaf(props) {
1536
1593
  if (placeholderEl) placeholderResizeObserver.current.observe(placeholderEl);
1537
1594
  } else if (placeholderEl) {
1538
1595
  // Create a new observer and observe the placeholder element.
1596
+ var ResizeObserver = window.ResizeObserver || resizeObserver.ResizeObserver;
1539
1597
  placeholderResizeObserver.current = new ResizeObserver(function (_ref) {
1540
1598
  var _ref2 = _slicedToArray(_ref, 1),
1541
1599
  target = _ref2[0].target;
@@ -1544,8 +1602,9 @@ var Leaf = function Leaf(props) {
1544
1602
 
1545
1603
  if (styleElement) {
1546
1604
  // Make the min-height the height of the placeholder.
1547
- var minHeight = "".concat(target.clientHeight, "px");
1548
- styleElement.innerHTML = ":where([data-slate-editor-id=\"".concat(editor.id, "\"]) { min-height: ").concat(minHeight, "; }");
1605
+ var selector = "[data-slate-editor-id=\"".concat(editor.id, "\"]");
1606
+ var styles = "min-height: ".concat(target.clientHeight, "px;");
1607
+ styleElement.innerHTML = whereIfSupported(selector, styles);
1549
1608
  }
1550
1609
  });
1551
1610
  placeholderResizeObserver.current.observe(placeholderEl);
@@ -2465,7 +2524,7 @@ function normalizePoint(editor, point) {
2465
2524
 
2466
2525
  var parentBlock = slate.Editor.above(editor, {
2467
2526
  match: function match(n) {
2468
- return slate.Editor.isBlock(editor, n);
2527
+ return slate.Element.isElement(n) && slate.Editor.isBlock(editor, n);
2469
2528
  },
2470
2529
  at: path
2471
2530
  });
@@ -3918,7 +3977,7 @@ var Editable = function Editable(props) {
3918
3977
  var block = slate.Editor.above(editor, {
3919
3978
  at: anchor.path,
3920
3979
  match: function match(n) {
3921
- return slate.Editor.isBlock(editor, n);
3980
+ return slate.Element.isElement(n) && slate.Editor.isBlock(editor, n);
3922
3981
  }
3923
3982
  });
3924
3983
 
@@ -4209,12 +4268,13 @@ var Editable = function Editable(props) {
4209
4268
  // Set global default styles for editors.
4210
4269
  var defaultStylesElement = document.createElement('style');
4211
4270
  defaultStylesElement.setAttribute('data-slate-default-styles', 'true');
4212
- defaultStylesElement.innerHTML = // :where is used to give these rules lower specificity so user stylesheets can override them.
4213
- ":where([data-slate-editor]) {" + // Allow positioning relative to the editable element.
4271
+ var selector = '[data-slate-editor]';
4272
+ var defaultStyles = // Allow positioning relative to the editable element.
4214
4273
  "position: relative;" + // Prevent the default outline styles.
4215
4274
  "outline: none;" + // Preserve adjacent whitespace and new lines.
4216
4275
  "white-space: pre-wrap;" + // Allow words to break if they are too long.
4217
- "word-wrap: break-word;" + "}";
4276
+ "word-wrap: break-word;";
4277
+ defaultStylesElement.innerHTML = whereIfSupported(selector, defaultStyles);
4218
4278
  document.head.appendChild(defaultStylesElement);
4219
4279
  }
4220
4280
 
@@ -4370,12 +4430,12 @@ var Editable = function Editable(props) {
4370
4430
  if (event.detail === TRIPLE_CLICK && path.length >= 1) {
4371
4431
  var blockPath = path;
4372
4432
 
4373
- if (!slate.Editor.isBlock(editor, node)) {
4433
+ if (!(slate.Element.isElement(node) && slate.Editor.isBlock(editor, node))) {
4374
4434
  var _block$;
4375
4435
 
4376
4436
  var block = slate.Editor.above(editor, {
4377
4437
  match: function match(n) {
4378
- return slate.Editor.isBlock(editor, n);
4438
+ return slate.Element.isElement(n) && slate.Editor.isBlock(editor, n);
4379
4439
  },
4380
4440
  at: path
4381
4441
  });
@@ -4471,7 +4531,7 @@ var Editable = function Editable(props) {
4471
4531
 
4472
4532
  var inline = slate.Editor.above(editor, {
4473
4533
  match: function match(n) {
4474
- return slate.Editor.isInline(editor, n);
4534
+ return slate.Element.isElement(n) && slate.Editor.isInline(editor, n);
4475
4535
  },
4476
4536
  mode: 'highest'
4477
4537
  });
@@ -4523,7 +4583,7 @@ var Editable = function Editable(props) {
4523
4583
  // default, and calling `preventDefault` hides the cursor.
4524
4584
  var node = ReactEditor.toSlateNode(editor, event.target);
4525
4585
 
4526
- if (slate.Editor.isVoid(editor, node)) {
4586
+ if (slate.Element.isElement(node) && slate.Editor.isVoid(editor, node)) {
4527
4587
  event.preventDefault();
4528
4588
  }
4529
4589
  }
@@ -4532,7 +4592,7 @@ var Editable = function Editable(props) {
4532
4592
  if (!readOnly && ReactEditor.hasTarget(editor, event.target) && !isEventHandled(event, attributes.onDragStart)) {
4533
4593
  var node = ReactEditor.toSlateNode(editor, event.target);
4534
4594
  var path = ReactEditor.findPath(editor, node);
4535
- var voidMatch = slate.Editor.isVoid(editor, node) || slate.Editor["void"](editor, {
4595
+ var voidMatch = slate.Element.isElement(node) && slate.Editor.isVoid(editor, node) || slate.Editor["void"](editor, {
4536
4596
  at: path,
4537
4597
  voids: true
4538
4598
  }); // If starting a drag on a void node, make sure it is selected
@@ -5186,8 +5246,8 @@ var Slate = function Slate(props) {
5186
5246
  });
5187
5247
  handleSelectorChange(editor);
5188
5248
  }, [onChange]);
5189
- EDITOR_TO_ON_CHANGE.set(editor, onContextChange);
5190
5249
  React.useEffect(function () {
5250
+ EDITOR_TO_ON_CHANGE.set(editor, onContextChange);
5191
5251
  return function () {
5192
5252
  EDITOR_TO_ON_CHANGE.set(editor, function () {});
5193
5253
  unmountRef.current = true;
@@ -5388,7 +5448,7 @@ var withReact = function withReact(editor) {
5388
5448
  if (e.selection && slate.Range.isCollapsed(e.selection)) {
5389
5449
  var parentBlockEntry = slate.Editor.above(e, {
5390
5450
  match: function match(n) {
5391
- return slate.Editor.isBlock(e, n);
5451
+ return slate.Element.isElement(n) && slate.Editor.isBlock(e, n);
5392
5452
  },
5393
5453
  at: e.selection
5394
5454
  });