slate-react 0.72.2 → 0.72.4-2022011114342

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
@@ -323,13 +323,6 @@ var EDITOR_TO_KEY_TO_ELEMENT = new WeakMap();
323
323
 
324
324
  var IS_READ_ONLY = new WeakMap();
325
325
  var IS_FOCUSED = new WeakMap();
326
- var IS_COMPOSING = new WeakMap();
327
- var IS_ON_COMPOSITION_END = new WeakMap();
328
- /**
329
- * Weak maps for saving text on composition stage.
330
- */
331
-
332
- var EDITOR_ON_COMPOSITION_TEXT = new WeakMap();
333
326
  /**
334
327
  * Weak map for associating the context `onChange` context with the plugin.
335
328
  */
@@ -1812,7 +1805,7 @@ var Editable$1 = function Editable(props) {
1812
1805
  distance: currentTextNode.text.length,
1813
1806
  reverse: true
1814
1807
  });
1815
- slate.Transforms.insertText(editor, text);
1808
+ slate.Editor.insertText(editor, text);
1816
1809
  });
1817
1810
  }
1818
1811
  }
@@ -2365,229 +2358,6 @@ var isDOMEventHandled = function isDOMEventHandled(event, handler) {
2365
2358
  return event.defaultPrevented;
2366
2359
  };
2367
2360
 
2368
- /**
2369
- * Returns the number of characters that are the same at the beginning of the
2370
- * String.
2371
- *
2372
- * @param prev the previous text
2373
- * @param next the next text
2374
- * @returns the offset of the start of the difference; null if there is no difference
2375
- */
2376
-
2377
- function getDiffStart(prev, next) {
2378
- var length = Math.min(prev.length, next.length);
2379
-
2380
- for (var i = 0; i < length; i++) {
2381
- if (prev.charAt(i) !== next.charAt(i)) return i;
2382
- }
2383
-
2384
- if (prev.length !== next.length) return length;
2385
- return null;
2386
- }
2387
- /**
2388
- * Returns the number of characters that are the same at the end of the String
2389
- * up to `max`. Max prevents double-counting characters when there are
2390
- * multiple duplicate characters around the diff area.
2391
- *
2392
- * @param prev the previous text
2393
- * @param next the next text
2394
- * @param max the max length to test.
2395
- * @returns number of characters that are the same at the end of the string
2396
- */
2397
-
2398
-
2399
- function getDiffEnd(prev, next, max) {
2400
- var prevLength = prev.length;
2401
- var nextLength = next.length;
2402
- var length = Math.min(prevLength, nextLength, max);
2403
-
2404
- for (var i = 0; i < length; i++) {
2405
- var prevChar = prev.charAt(prevLength - i - 1);
2406
- var nextChar = next.charAt(nextLength - i - 1);
2407
- if (prevChar !== nextChar) return i;
2408
- }
2409
-
2410
- if (prev.length !== next.length) return length;
2411
- return null;
2412
- }
2413
- /**
2414
- * Takes two strings and returns an object representing two offsets. The
2415
- * first, `start` represents the number of characters that are the same at
2416
- * the front of the String. The `end` represents the number of characters
2417
- * that are the same at the end of the String.
2418
- *
2419
- * Returns null if they are identical.
2420
- *
2421
- * @param prev the previous text
2422
- * @param next the next text
2423
- * @returns the difference text range; null if there are no differences.
2424
- */
2425
-
2426
-
2427
- function getDiffOffsets(prev, next) {
2428
- if (prev === next) return null;
2429
- var start = getDiffStart(prev, next);
2430
- if (start === null) return null;
2431
- var maxEnd = Math.min(prev.length - start, next.length - start);
2432
- var end = getDiffEnd(prev, next, maxEnd);
2433
- if (end === null) return null;
2434
- return {
2435
- start: start,
2436
- end: end
2437
- };
2438
- }
2439
- /**
2440
- * Takes a text string and returns a slice from the string at the given text range
2441
- *
2442
- * @param text the text
2443
- * @param offsets the text range
2444
- * @returns the text slice at text range
2445
- */
2446
-
2447
-
2448
- function sliceText(text, offsets) {
2449
- return text.slice(offsets.start, text.length - offsets.end);
2450
- }
2451
- /**
2452
- * Takes two strings and returns a smart diff that can be used to describe the
2453
- * change in a way that can be used as operations like inserting, removing or
2454
- * replacing text.
2455
- *
2456
- * @param prev the previous text
2457
- * @param next the next text
2458
- * @returns the text difference
2459
- */
2460
-
2461
-
2462
- function diffText(prev, next) {
2463
- if (prev === undefined || next === undefined) return null;
2464
- var offsets = getDiffOffsets(prev, next);
2465
- if (offsets == null) return null;
2466
- var insertText = sliceText(next, offsets);
2467
- var removeText = sliceText(prev, offsets);
2468
- return {
2469
- start: offsets.start,
2470
- end: prev.length - offsets.end,
2471
- insertText: insertText,
2472
- removeText: removeText
2473
- };
2474
- }
2475
- function combineInsertedText(insertedText) {
2476
- return insertedText.reduce(function (acc, _ref) {
2477
- var text = _ref.text;
2478
- return "".concat(acc).concat(text.insertText);
2479
- }, '');
2480
- }
2481
- function getTextInsertion(editor, domNode) {
2482
- var node = ReactEditor.toSlateNode(editor, domNode);
2483
-
2484
- if (!slate.Text.isText(node)) {
2485
- return undefined;
2486
- }
2487
-
2488
- var prevText = node.text;
2489
- var nextText = domNode.textContent; // textContent will pad an extra \n when the textContent ends with an \n
2490
-
2491
- if (nextText.endsWith('\n')) {
2492
- nextText = nextText.slice(0, nextText.length - 1);
2493
- } // If the text is no different, there is no diff.
2494
-
2495
-
2496
- if (nextText !== prevText) {
2497
- var textDiff = diffText(prevText, nextText);
2498
-
2499
- if (textDiff !== null) {
2500
- var textPath = ReactEditor.findPath(editor, node);
2501
- return {
2502
- text: textDiff,
2503
- path: textPath
2504
- };
2505
- }
2506
- }
2507
-
2508
- return undefined;
2509
- }
2510
- function normalizeTextInsertionRange(editor, range, _ref2) {
2511
- var path = _ref2.path,
2512
- text = _ref2.text;
2513
- var insertionRange = {
2514
- anchor: {
2515
- path: path,
2516
- offset: text.start
2517
- },
2518
- focus: {
2519
- path: path,
2520
- offset: text.end
2521
- }
2522
- };
2523
-
2524
- if (!range || !slate.Range.isCollapsed(range)) {
2525
- return insertionRange;
2526
- }
2527
-
2528
- var insertText = text.insertText,
2529
- removeText = text.removeText;
2530
- var isSingleCharacterInsertion = insertText.length === 1 || removeText.length === 1;
2531
- /**
2532
- * This code handles edge cases that arise from text diffing when the
2533
- * inserted or removed character is a single character, and the character
2534
- * right before or after the anchor is the same as the one being inserted or
2535
- * removed.
2536
- *
2537
- * Take this example: hello|o
2538
- *
2539
- * If another `o` is inserted at the selection's anchor in the example above,
2540
- * it should be inserted at the anchor, but using text diffing, we actually
2541
- * detect that the character was inserted after the second `o`:
2542
- *
2543
- * helloo[o]|
2544
- *
2545
- * Instead, in these very specific edge cases, we assume that the character
2546
- * needs to be inserted after the anchor rather than where the diff was found:
2547
- *
2548
- * hello[o]|o
2549
- */
2550
-
2551
- if (isSingleCharacterInsertion && slate.Path.equals(range.anchor.path, path)) {
2552
- var _Array$from = Array.from(slate.Editor.nodes(editor, {
2553
- at: range,
2554
- match: slate.Text.isText
2555
- })),
2556
- _Array$from2 = _slicedToArray(_Array$from, 1),
2557
- _text = _Array$from2[0];
2558
-
2559
- if (_text) {
2560
- var _text2 = _slicedToArray(_text, 1),
2561
- node = _text2[0];
2562
-
2563
- var anchor = range.anchor;
2564
- var characterBeforeAnchor = node.text[anchor.offset - 1];
2565
- var characterAfterAnchor = node.text[anchor.offset];
2566
-
2567
- if (insertText.length === 1 && insertText === characterAfterAnchor) {
2568
- // Assume text should be inserted at the anchor
2569
- return range;
2570
- }
2571
-
2572
- if (removeText.length === 1 && removeText === characterBeforeAnchor) {
2573
- // Assume text should be removed right before the anchor
2574
- return {
2575
- anchor: {
2576
- path: path,
2577
- offset: anchor.offset - 1
2578
- },
2579
- focus: {
2580
- path: path,
2581
- offset: anchor.offset
2582
- }
2583
- };
2584
- }
2585
- }
2586
- }
2587
-
2588
- return insertionRange;
2589
- }
2590
-
2591
2361
  var classCallCheck = createCommonjsModule(function (module) {
2592
2362
  function _classCallCheck(instance, Constructor) {
2593
2363
  if (!(instance instanceof Constructor)) {
@@ -3216,6 +2986,229 @@ var ReactEditor = {
3216
2986
  }
3217
2987
  };
3218
2988
 
2989
+ /**
2990
+ * Returns the number of characters that are the same at the beginning of the
2991
+ * String.
2992
+ *
2993
+ * @param prev the previous text
2994
+ * @param next the next text
2995
+ * @returns the offset of the start of the difference; null if there is no difference
2996
+ */
2997
+
2998
+ function getDiffStart(prev, next) {
2999
+ var length = Math.min(prev.length, next.length);
3000
+
3001
+ for (var i = 0; i < length; i++) {
3002
+ if (prev.charAt(i) !== next.charAt(i)) return i;
3003
+ }
3004
+
3005
+ if (prev.length !== next.length) return length;
3006
+ return null;
3007
+ }
3008
+ /**
3009
+ * Returns the number of characters that are the same at the end of the String
3010
+ * up to `max`. Max prevents double-counting characters when there are
3011
+ * multiple duplicate characters around the diff area.
3012
+ *
3013
+ * @param prev the previous text
3014
+ * @param next the next text
3015
+ * @param max the max length to test.
3016
+ * @returns number of characters that are the same at the end of the string
3017
+ */
3018
+
3019
+
3020
+ function getDiffEnd(prev, next, max) {
3021
+ var prevLength = prev.length;
3022
+ var nextLength = next.length;
3023
+ var length = Math.min(prevLength, nextLength, max);
3024
+
3025
+ for (var i = 0; i < length; i++) {
3026
+ var prevChar = prev.charAt(prevLength - i - 1);
3027
+ var nextChar = next.charAt(nextLength - i - 1);
3028
+ if (prevChar !== nextChar) return i;
3029
+ }
3030
+
3031
+ if (prev.length !== next.length) return length;
3032
+ return null;
3033
+ }
3034
+ /**
3035
+ * Takes two strings and returns an object representing two offsets. The
3036
+ * first, `start` represents the number of characters that are the same at
3037
+ * the front of the String. The `end` represents the number of characters
3038
+ * that are the same at the end of the String.
3039
+ *
3040
+ * Returns null if they are identical.
3041
+ *
3042
+ * @param prev the previous text
3043
+ * @param next the next text
3044
+ * @returns the difference text range; null if there are no differences.
3045
+ */
3046
+
3047
+
3048
+ function getDiffOffsets(prev, next) {
3049
+ if (prev === next) return null;
3050
+ var start = getDiffStart(prev, next);
3051
+ if (start === null) return null;
3052
+ var maxEnd = Math.min(prev.length - start, next.length - start);
3053
+ var end = getDiffEnd(prev, next, maxEnd);
3054
+ if (end === null) return null;
3055
+ return {
3056
+ start: start,
3057
+ end: end
3058
+ };
3059
+ }
3060
+ /**
3061
+ * Takes a text string and returns a slice from the string at the given text range
3062
+ *
3063
+ * @param text the text
3064
+ * @param offsets the text range
3065
+ * @returns the text slice at text range
3066
+ */
3067
+
3068
+
3069
+ function sliceText(text, offsets) {
3070
+ return text.slice(offsets.start, text.length - offsets.end);
3071
+ }
3072
+ /**
3073
+ * Takes two strings and returns a smart diff that can be used to describe the
3074
+ * change in a way that can be used as operations like inserting, removing or
3075
+ * replacing text.
3076
+ *
3077
+ * @param prev the previous text
3078
+ * @param next the next text
3079
+ * @returns the text difference
3080
+ */
3081
+
3082
+
3083
+ function diffText(prev, next) {
3084
+ if (prev === undefined || next === undefined) return null;
3085
+ var offsets = getDiffOffsets(prev, next);
3086
+ if (offsets == null) return null;
3087
+ var insertText = sliceText(next, offsets);
3088
+ var removeText = sliceText(prev, offsets);
3089
+ return {
3090
+ start: offsets.start,
3091
+ end: prev.length - offsets.end,
3092
+ insertText: insertText,
3093
+ removeText: removeText
3094
+ };
3095
+ }
3096
+ function combineInsertedText(insertedText) {
3097
+ return insertedText.reduce(function (acc, _ref) {
3098
+ var text = _ref.text;
3099
+ return "".concat(acc).concat(text.insertText);
3100
+ }, '');
3101
+ }
3102
+ function getTextInsertion(editor, domNode) {
3103
+ var node = ReactEditor.toSlateNode(editor, domNode);
3104
+
3105
+ if (!slate.Text.isText(node)) {
3106
+ return undefined;
3107
+ }
3108
+
3109
+ var prevText = node.text;
3110
+ var nextText = domNode.textContent; // textContent will pad an extra \n when the textContent ends with an \n
3111
+
3112
+ if (nextText.endsWith('\n')) {
3113
+ nextText = nextText.slice(0, nextText.length - 1);
3114
+ } // If the text is no different, there is no diff.
3115
+
3116
+
3117
+ if (nextText !== prevText) {
3118
+ var textDiff = diffText(prevText, nextText);
3119
+
3120
+ if (textDiff !== null) {
3121
+ var textPath = ReactEditor.findPath(editor, node);
3122
+ return {
3123
+ text: textDiff,
3124
+ path: textPath
3125
+ };
3126
+ }
3127
+ }
3128
+
3129
+ return undefined;
3130
+ }
3131
+ function normalizeTextInsertionRange(editor, range, _ref2) {
3132
+ var path = _ref2.path,
3133
+ text = _ref2.text;
3134
+ var insertionRange = {
3135
+ anchor: {
3136
+ path: path,
3137
+ offset: text.start
3138
+ },
3139
+ focus: {
3140
+ path: path,
3141
+ offset: text.end
3142
+ }
3143
+ };
3144
+
3145
+ if (!range || !slate.Range.isCollapsed(range)) {
3146
+ return insertionRange;
3147
+ }
3148
+
3149
+ var insertText = text.insertText,
3150
+ removeText = text.removeText;
3151
+ var isSingleCharacterInsertion = insertText.length === 1 || removeText.length === 1;
3152
+ /**
3153
+ * This code handles edge cases that arise from text diffing when the
3154
+ * inserted or removed character is a single character, and the character
3155
+ * right before or after the anchor is the same as the one being inserted or
3156
+ * removed.
3157
+ *
3158
+ * Take this example: hello|o
3159
+ *
3160
+ * If another `o` is inserted at the selection's anchor in the example above,
3161
+ * it should be inserted at the anchor, but using text diffing, we actually
3162
+ * detect that the character was inserted after the second `o`:
3163
+ *
3164
+ * helloo[o]|
3165
+ *
3166
+ * Instead, in these very specific edge cases, we assume that the character
3167
+ * needs to be inserted after the anchor rather than where the diff was found:
3168
+ *
3169
+ * hello[o]|o
3170
+ */
3171
+
3172
+ if (isSingleCharacterInsertion && slate.Path.equals(range.anchor.path, path)) {
3173
+ var _Array$from = Array.from(slate.Editor.nodes(editor, {
3174
+ at: range,
3175
+ match: slate.Text.isText
3176
+ })),
3177
+ _Array$from2 = _slicedToArray(_Array$from, 1),
3178
+ _text = _Array$from2[0];
3179
+
3180
+ if (_text) {
3181
+ var _text2 = _slicedToArray(_text, 1),
3182
+ node = _text2[0];
3183
+
3184
+ var anchor = range.anchor;
3185
+ var characterBeforeAnchor = node.text[anchor.offset - 1];
3186
+ var characterAfterAnchor = node.text[anchor.offset];
3187
+
3188
+ if (insertText.length === 1 && insertText === characterAfterAnchor) {
3189
+ // Assume text should be inserted at the anchor
3190
+ return range;
3191
+ }
3192
+
3193
+ if (removeText.length === 1 && removeText === characterBeforeAnchor) {
3194
+ // Assume text should be removed right before the anchor
3195
+ return {
3196
+ anchor: {
3197
+ path: path,
3198
+ offset: anchor.offset - 1
3199
+ },
3200
+ focus: {
3201
+ path: path,
3202
+ offset: anchor.offset
3203
+ }
3204
+ };
3205
+ }
3206
+ }
3207
+ }
3208
+
3209
+ return insertionRange;
3210
+ }
3211
+
3219
3212
  function gatherMutationData(editor, mutations) {
3220
3213
  var addedNodes = [];
3221
3214
  var removedNodes = [];
@@ -3428,15 +3421,7 @@ var AndroidInputManager = function AndroidInputManager(editor, restoreDOM) {
3428
3421
  this.insertText = function (insertedText) {
3429
3422
  var _this$editor = _this.editor,
3430
3423
  selection = _this$editor.selection,
3431
- marks = _this$editor.marks; // If it is in composing or after `onCompositionend`, set `EDITOR_ON_COMPOSITION_TEXT` and return.
3432
- // Text will be inserted on compositionend event.
3433
-
3434
- if (IS_COMPOSING.get(_this.editor) || IS_ON_COMPOSITION_END.get(_this.editor)) {
3435
- EDITOR_ON_COMPOSITION_TEXT.set(_this.editor, insertedText);
3436
- IS_ON_COMPOSITION_END.set(_this.editor, false);
3437
- return;
3438
- } // Insert the batched text diffs
3439
-
3424
+ marks = _this$editor.marks; // Insert the batched text diffs
3440
3425
 
3441
3426
  insertedText.forEach(function (insertion) {
3442
3427
  var text = insertion.text.insertText;
@@ -3454,9 +3439,8 @@ var AndroidInputManager = function AndroidInputManager(editor, restoreDOM) {
3454
3439
  });
3455
3440
  _this.editor.marks = null;
3456
3441
  } else {
3457
- slate.Transforms.insertText(_this.editor, text, {
3458
- at: at
3459
- });
3442
+ slate.Transforms.setSelection(_this.editor, at);
3443
+ slate.Editor.insertText(_this.editor, text);
3460
3444
  }
3461
3445
  });
3462
3446
  };
@@ -4085,39 +4069,6 @@ var AndroidEditable = function AndroidEditable(props) {
4085
4069
  setTimeout(function () {
4086
4070
  state.isComposing && setIsComposing(false);
4087
4071
  state.isComposing = false;
4088
- IS_COMPOSING.set(editor, false);
4089
- IS_ON_COMPOSITION_END.set(editor, true);
4090
- var insertedText = EDITOR_ON_COMPOSITION_TEXT.get(editor) || []; // `insertedText` is set in `MutationObserver` constructor.
4091
- // If open phone keyboard association function, `CompositionEvent` will be triggered.
4092
-
4093
- if (!insertedText.length) {
4094
- return;
4095
- }
4096
-
4097
- EDITOR_ON_COMPOSITION_TEXT.set(editor, []);
4098
- var selection = editor.selection,
4099
- marks = editor.marks;
4100
- insertedText.forEach(function (insertion) {
4101
- var text = insertion.text.insertText;
4102
- var at = normalizeTextInsertionRange(editor, selection, insertion);
4103
-
4104
- if (marks) {
4105
- var node = _objectSpread({
4106
- text: text
4107
- }, marks);
4108
-
4109
- slate.Transforms.insertNodes(editor, node, {
4110
- match: slate.Text.isText,
4111
- at: at,
4112
- select: true
4113
- });
4114
- editor.marks = null;
4115
- } else {
4116
- slate.Transforms.insertText(editor, text, {
4117
- at: at
4118
- });
4119
- }
4120
- });
4121
4072
  }, RESOLVE_DELAY);
4122
4073
  }
4123
4074
  }, [attributes.onCompositionEnd]),
@@ -4125,14 +4076,12 @@ var AndroidEditable = function AndroidEditable(props) {
4125
4076
  if (hasEditableTarget(editor, event.target) && !isEventHandled(event, attributes.onCompositionUpdate)) {
4126
4077
  !state.isComposing && setIsComposing(true);
4127
4078
  state.isComposing = true;
4128
- IS_COMPOSING.set(editor, true);
4129
4079
  }
4130
4080
  }, [attributes.onCompositionUpdate]),
4131
4081
  onCompositionStart: React.useCallback(function (event) {
4132
4082
  if (hasEditableTarget(editor, event.target) && !isEventHandled(event, attributes.onCompositionStart)) {
4133
4083
  !state.isComposing && setIsComposing(true);
4134
4084
  state.isComposing = true;
4135
- IS_COMPOSING.set(editor, true);
4136
4085
  }
4137
4086
  }, [attributes.onCompositionStart]),
4138
4087
  onPaste: React.useCallback(function (event) {
@@ -4218,7 +4167,9 @@ var Slate = function Slate(props) {
4218
4167
  });
4219
4168
  useIsomorphicLayoutEffect(function () {
4220
4169
  var fn = function fn() {
4221
- return setIsFocused(ReactEditor.isFocused(editor));
4170
+ setTimeout(function () {
4171
+ setIsFocused(ReactEditor.isFocused(editor));
4172
+ }, 0);
4222
4173
  };
4223
4174
 
4224
4175
  document.addEventListener('focus', fn, true);
@@ -4228,7 +4179,9 @@ var Slate = function Slate(props) {
4228
4179
  }, []);
4229
4180
  useIsomorphicLayoutEffect(function () {
4230
4181
  var fn = function fn() {
4231
- return setIsFocused(ReactEditor.isFocused(editor));
4182
+ setTimeout(function () {
4183
+ setIsFocused(ReactEditor.isFocused(editor));
4184
+ }, 0);
4232
4185
  };
4233
4186
 
4234
4187
  document.addEventListener('blur', fn, true);