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.
@@ -977,13 +977,6 @@
977
977
 
978
978
  var IS_READ_ONLY = new WeakMap();
979
979
  var IS_FOCUSED = new WeakMap();
980
- var IS_COMPOSING = new WeakMap();
981
- var IS_ON_COMPOSITION_END = new WeakMap();
982
- /**
983
- * Weak maps for saving text on composition stage.
984
- */
985
-
986
- var EDITOR_ON_COMPOSITION_TEXT = new WeakMap();
987
980
  /**
988
981
  * Weak map for associating the context `onChange` context with the plugin.
989
982
  */
@@ -2722,7 +2715,7 @@
2722
2715
  distance: currentTextNode.text.length,
2723
2716
  reverse: true
2724
2717
  });
2725
- slate.Transforms.insertText(editor, text);
2718
+ slate.Editor.insertText(editor, text);
2726
2719
  });
2727
2720
  }
2728
2721
  }
@@ -3275,229 +3268,6 @@
3275
3268
  return event.defaultPrevented;
3276
3269
  };
3277
3270
 
3278
- /**
3279
- * Returns the number of characters that are the same at the beginning of the
3280
- * String.
3281
- *
3282
- * @param prev the previous text
3283
- * @param next the next text
3284
- * @returns the offset of the start of the difference; null if there is no difference
3285
- */
3286
-
3287
- function getDiffStart(prev, next) {
3288
- var length = Math.min(prev.length, next.length);
3289
-
3290
- for (var i = 0; i < length; i++) {
3291
- if (prev.charAt(i) !== next.charAt(i)) return i;
3292
- }
3293
-
3294
- if (prev.length !== next.length) return length;
3295
- return null;
3296
- }
3297
- /**
3298
- * Returns the number of characters that are the same at the end of the String
3299
- * up to `max`. Max prevents double-counting characters when there are
3300
- * multiple duplicate characters around the diff area.
3301
- *
3302
- * @param prev the previous text
3303
- * @param next the next text
3304
- * @param max the max length to test.
3305
- * @returns number of characters that are the same at the end of the string
3306
- */
3307
-
3308
-
3309
- function getDiffEnd(prev, next, max) {
3310
- var prevLength = prev.length;
3311
- var nextLength = next.length;
3312
- var length = Math.min(prevLength, nextLength, max);
3313
-
3314
- for (var i = 0; i < length; i++) {
3315
- var prevChar = prev.charAt(prevLength - i - 1);
3316
- var nextChar = next.charAt(nextLength - i - 1);
3317
- if (prevChar !== nextChar) return i;
3318
- }
3319
-
3320
- if (prev.length !== next.length) return length;
3321
- return null;
3322
- }
3323
- /**
3324
- * Takes two strings and returns an object representing two offsets. The
3325
- * first, `start` represents the number of characters that are the same at
3326
- * the front of the String. The `end` represents the number of characters
3327
- * that are the same at the end of the String.
3328
- *
3329
- * Returns null if they are identical.
3330
- *
3331
- * @param prev the previous text
3332
- * @param next the next text
3333
- * @returns the difference text range; null if there are no differences.
3334
- */
3335
-
3336
-
3337
- function getDiffOffsets(prev, next) {
3338
- if (prev === next) return null;
3339
- var start = getDiffStart(prev, next);
3340
- if (start === null) return null;
3341
- var maxEnd = Math.min(prev.length - start, next.length - start);
3342
- var end = getDiffEnd(prev, next, maxEnd);
3343
- if (end === null) return null;
3344
- return {
3345
- start: start,
3346
- end: end
3347
- };
3348
- }
3349
- /**
3350
- * Takes a text string and returns a slice from the string at the given text range
3351
- *
3352
- * @param text the text
3353
- * @param offsets the text range
3354
- * @returns the text slice at text range
3355
- */
3356
-
3357
-
3358
- function sliceText(text, offsets) {
3359
- return text.slice(offsets.start, text.length - offsets.end);
3360
- }
3361
- /**
3362
- * Takes two strings and returns a smart diff that can be used to describe the
3363
- * change in a way that can be used as operations like inserting, removing or
3364
- * replacing text.
3365
- *
3366
- * @param prev the previous text
3367
- * @param next the next text
3368
- * @returns the text difference
3369
- */
3370
-
3371
-
3372
- function diffText(prev, next) {
3373
- if (prev === undefined || next === undefined) return null;
3374
- var offsets = getDiffOffsets(prev, next);
3375
- if (offsets == null) return null;
3376
- var insertText = sliceText(next, offsets);
3377
- var removeText = sliceText(prev, offsets);
3378
- return {
3379
- start: offsets.start,
3380
- end: prev.length - offsets.end,
3381
- insertText: insertText,
3382
- removeText: removeText
3383
- };
3384
- }
3385
- function combineInsertedText(insertedText) {
3386
- return insertedText.reduce(function (acc, _ref) {
3387
- var text = _ref.text;
3388
- return "".concat(acc).concat(text.insertText);
3389
- }, '');
3390
- }
3391
- function getTextInsertion(editor, domNode) {
3392
- var node = ReactEditor.toSlateNode(editor, domNode);
3393
-
3394
- if (!slate.Text.isText(node)) {
3395
- return undefined;
3396
- }
3397
-
3398
- var prevText = node.text;
3399
- var nextText = domNode.textContent; // textContent will pad an extra \n when the textContent ends with an \n
3400
-
3401
- if (nextText.endsWith('\n')) {
3402
- nextText = nextText.slice(0, nextText.length - 1);
3403
- } // If the text is no different, there is no diff.
3404
-
3405
-
3406
- if (nextText !== prevText) {
3407
- var textDiff = diffText(prevText, nextText);
3408
-
3409
- if (textDiff !== null) {
3410
- var textPath = ReactEditor.findPath(editor, node);
3411
- return {
3412
- text: textDiff,
3413
- path: textPath
3414
- };
3415
- }
3416
- }
3417
-
3418
- return undefined;
3419
- }
3420
- function normalizeTextInsertionRange(editor, range, _ref2) {
3421
- var path = _ref2.path,
3422
- text = _ref2.text;
3423
- var insertionRange = {
3424
- anchor: {
3425
- path: path,
3426
- offset: text.start
3427
- },
3428
- focus: {
3429
- path: path,
3430
- offset: text.end
3431
- }
3432
- };
3433
-
3434
- if (!range || !slate.Range.isCollapsed(range)) {
3435
- return insertionRange;
3436
- }
3437
-
3438
- var insertText = text.insertText,
3439
- removeText = text.removeText;
3440
- var isSingleCharacterInsertion = insertText.length === 1 || removeText.length === 1;
3441
- /**
3442
- * This code handles edge cases that arise from text diffing when the
3443
- * inserted or removed character is a single character, and the character
3444
- * right before or after the anchor is the same as the one being inserted or
3445
- * removed.
3446
- *
3447
- * Take this example: hello|o
3448
- *
3449
- * If another `o` is inserted at the selection's anchor in the example above,
3450
- * it should be inserted at the anchor, but using text diffing, we actually
3451
- * detect that the character was inserted after the second `o`:
3452
- *
3453
- * helloo[o]|
3454
- *
3455
- * Instead, in these very specific edge cases, we assume that the character
3456
- * needs to be inserted after the anchor rather than where the diff was found:
3457
- *
3458
- * hello[o]|o
3459
- */
3460
-
3461
- if (isSingleCharacterInsertion && slate.Path.equals(range.anchor.path, path)) {
3462
- var _Array$from = Array.from(slate.Editor.nodes(editor, {
3463
- at: range,
3464
- match: slate.Text.isText
3465
- })),
3466
- _Array$from2 = _slicedToArray(_Array$from, 1),
3467
- _text = _Array$from2[0];
3468
-
3469
- if (_text) {
3470
- var _text2 = _slicedToArray(_text, 1),
3471
- node = _text2[0];
3472
-
3473
- var anchor = range.anchor;
3474
- var characterBeforeAnchor = node.text[anchor.offset - 1];
3475
- var characterAfterAnchor = node.text[anchor.offset];
3476
-
3477
- if (insertText.length === 1 && insertText === characterAfterAnchor) {
3478
- // Assume text should be inserted at the anchor
3479
- return range;
3480
- }
3481
-
3482
- if (removeText.length === 1 && removeText === characterBeforeAnchor) {
3483
- // Assume text should be removed right before the anchor
3484
- return {
3485
- anchor: {
3486
- path: path,
3487
- offset: anchor.offset - 1
3488
- },
3489
- focus: {
3490
- path: path,
3491
- offset: anchor.offset
3492
- }
3493
- };
3494
- }
3495
- }
3496
- }
3497
-
3498
- return insertionRange;
3499
- }
3500
-
3501
3271
  var classCallCheck = createCommonjsModule(function (module) {
3502
3272
  function _classCallCheck(instance, Constructor) {
3503
3273
  if (!(instance instanceof Constructor)) {
@@ -4126,6 +3896,229 @@
4126
3896
  }
4127
3897
  };
4128
3898
 
3899
+ /**
3900
+ * Returns the number of characters that are the same at the beginning of the
3901
+ * String.
3902
+ *
3903
+ * @param prev the previous text
3904
+ * @param next the next text
3905
+ * @returns the offset of the start of the difference; null if there is no difference
3906
+ */
3907
+
3908
+ function getDiffStart(prev, next) {
3909
+ var length = Math.min(prev.length, next.length);
3910
+
3911
+ for (var i = 0; i < length; i++) {
3912
+ if (prev.charAt(i) !== next.charAt(i)) return i;
3913
+ }
3914
+
3915
+ if (prev.length !== next.length) return length;
3916
+ return null;
3917
+ }
3918
+ /**
3919
+ * Returns the number of characters that are the same at the end of the String
3920
+ * up to `max`. Max prevents double-counting characters when there are
3921
+ * multiple duplicate characters around the diff area.
3922
+ *
3923
+ * @param prev the previous text
3924
+ * @param next the next text
3925
+ * @param max the max length to test.
3926
+ * @returns number of characters that are the same at the end of the string
3927
+ */
3928
+
3929
+
3930
+ function getDiffEnd(prev, next, max) {
3931
+ var prevLength = prev.length;
3932
+ var nextLength = next.length;
3933
+ var length = Math.min(prevLength, nextLength, max);
3934
+
3935
+ for (var i = 0; i < length; i++) {
3936
+ var prevChar = prev.charAt(prevLength - i - 1);
3937
+ var nextChar = next.charAt(nextLength - i - 1);
3938
+ if (prevChar !== nextChar) return i;
3939
+ }
3940
+
3941
+ if (prev.length !== next.length) return length;
3942
+ return null;
3943
+ }
3944
+ /**
3945
+ * Takes two strings and returns an object representing two offsets. The
3946
+ * first, `start` represents the number of characters that are the same at
3947
+ * the front of the String. The `end` represents the number of characters
3948
+ * that are the same at the end of the String.
3949
+ *
3950
+ * Returns null if they are identical.
3951
+ *
3952
+ * @param prev the previous text
3953
+ * @param next the next text
3954
+ * @returns the difference text range; null if there are no differences.
3955
+ */
3956
+
3957
+
3958
+ function getDiffOffsets(prev, next) {
3959
+ if (prev === next) return null;
3960
+ var start = getDiffStart(prev, next);
3961
+ if (start === null) return null;
3962
+ var maxEnd = Math.min(prev.length - start, next.length - start);
3963
+ var end = getDiffEnd(prev, next, maxEnd);
3964
+ if (end === null) return null;
3965
+ return {
3966
+ start: start,
3967
+ end: end
3968
+ };
3969
+ }
3970
+ /**
3971
+ * Takes a text string and returns a slice from the string at the given text range
3972
+ *
3973
+ * @param text the text
3974
+ * @param offsets the text range
3975
+ * @returns the text slice at text range
3976
+ */
3977
+
3978
+
3979
+ function sliceText(text, offsets) {
3980
+ return text.slice(offsets.start, text.length - offsets.end);
3981
+ }
3982
+ /**
3983
+ * Takes two strings and returns a smart diff that can be used to describe the
3984
+ * change in a way that can be used as operations like inserting, removing or
3985
+ * replacing text.
3986
+ *
3987
+ * @param prev the previous text
3988
+ * @param next the next text
3989
+ * @returns the text difference
3990
+ */
3991
+
3992
+
3993
+ function diffText(prev, next) {
3994
+ if (prev === undefined || next === undefined) return null;
3995
+ var offsets = getDiffOffsets(prev, next);
3996
+ if (offsets == null) return null;
3997
+ var insertText = sliceText(next, offsets);
3998
+ var removeText = sliceText(prev, offsets);
3999
+ return {
4000
+ start: offsets.start,
4001
+ end: prev.length - offsets.end,
4002
+ insertText: insertText,
4003
+ removeText: removeText
4004
+ };
4005
+ }
4006
+ function combineInsertedText(insertedText) {
4007
+ return insertedText.reduce(function (acc, _ref) {
4008
+ var text = _ref.text;
4009
+ return "".concat(acc).concat(text.insertText);
4010
+ }, '');
4011
+ }
4012
+ function getTextInsertion(editor, domNode) {
4013
+ var node = ReactEditor.toSlateNode(editor, domNode);
4014
+
4015
+ if (!slate.Text.isText(node)) {
4016
+ return undefined;
4017
+ }
4018
+
4019
+ var prevText = node.text;
4020
+ var nextText = domNode.textContent; // textContent will pad an extra \n when the textContent ends with an \n
4021
+
4022
+ if (nextText.endsWith('\n')) {
4023
+ nextText = nextText.slice(0, nextText.length - 1);
4024
+ } // If the text is no different, there is no diff.
4025
+
4026
+
4027
+ if (nextText !== prevText) {
4028
+ var textDiff = diffText(prevText, nextText);
4029
+
4030
+ if (textDiff !== null) {
4031
+ var textPath = ReactEditor.findPath(editor, node);
4032
+ return {
4033
+ text: textDiff,
4034
+ path: textPath
4035
+ };
4036
+ }
4037
+ }
4038
+
4039
+ return undefined;
4040
+ }
4041
+ function normalizeTextInsertionRange(editor, range, _ref2) {
4042
+ var path = _ref2.path,
4043
+ text = _ref2.text;
4044
+ var insertionRange = {
4045
+ anchor: {
4046
+ path: path,
4047
+ offset: text.start
4048
+ },
4049
+ focus: {
4050
+ path: path,
4051
+ offset: text.end
4052
+ }
4053
+ };
4054
+
4055
+ if (!range || !slate.Range.isCollapsed(range)) {
4056
+ return insertionRange;
4057
+ }
4058
+
4059
+ var insertText = text.insertText,
4060
+ removeText = text.removeText;
4061
+ var isSingleCharacterInsertion = insertText.length === 1 || removeText.length === 1;
4062
+ /**
4063
+ * This code handles edge cases that arise from text diffing when the
4064
+ * inserted or removed character is a single character, and the character
4065
+ * right before or after the anchor is the same as the one being inserted or
4066
+ * removed.
4067
+ *
4068
+ * Take this example: hello|o
4069
+ *
4070
+ * If another `o` is inserted at the selection's anchor in the example above,
4071
+ * it should be inserted at the anchor, but using text diffing, we actually
4072
+ * detect that the character was inserted after the second `o`:
4073
+ *
4074
+ * helloo[o]|
4075
+ *
4076
+ * Instead, in these very specific edge cases, we assume that the character
4077
+ * needs to be inserted after the anchor rather than where the diff was found:
4078
+ *
4079
+ * hello[o]|o
4080
+ */
4081
+
4082
+ if (isSingleCharacterInsertion && slate.Path.equals(range.anchor.path, path)) {
4083
+ var _Array$from = Array.from(slate.Editor.nodes(editor, {
4084
+ at: range,
4085
+ match: slate.Text.isText
4086
+ })),
4087
+ _Array$from2 = _slicedToArray(_Array$from, 1),
4088
+ _text = _Array$from2[0];
4089
+
4090
+ if (_text) {
4091
+ var _text2 = _slicedToArray(_text, 1),
4092
+ node = _text2[0];
4093
+
4094
+ var anchor = range.anchor;
4095
+ var characterBeforeAnchor = node.text[anchor.offset - 1];
4096
+ var characterAfterAnchor = node.text[anchor.offset];
4097
+
4098
+ if (insertText.length === 1 && insertText === characterAfterAnchor) {
4099
+ // Assume text should be inserted at the anchor
4100
+ return range;
4101
+ }
4102
+
4103
+ if (removeText.length === 1 && removeText === characterBeforeAnchor) {
4104
+ // Assume text should be removed right before the anchor
4105
+ return {
4106
+ anchor: {
4107
+ path: path,
4108
+ offset: anchor.offset - 1
4109
+ },
4110
+ focus: {
4111
+ path: path,
4112
+ offset: anchor.offset
4113
+ }
4114
+ };
4115
+ }
4116
+ }
4117
+ }
4118
+
4119
+ return insertionRange;
4120
+ }
4121
+
4129
4122
  function gatherMutationData(editor, mutations) {
4130
4123
  var addedNodes = [];
4131
4124
  var removedNodes = [];
@@ -4338,15 +4331,7 @@
4338
4331
  this.insertText = function (insertedText) {
4339
4332
  var _this$editor = _this.editor,
4340
4333
  selection = _this$editor.selection,
4341
- marks = _this$editor.marks; // If it is in composing or after `onCompositionend`, set `EDITOR_ON_COMPOSITION_TEXT` and return.
4342
- // Text will be inserted on compositionend event.
4343
-
4344
- if (IS_COMPOSING.get(_this.editor) || IS_ON_COMPOSITION_END.get(_this.editor)) {
4345
- EDITOR_ON_COMPOSITION_TEXT.set(_this.editor, insertedText);
4346
- IS_ON_COMPOSITION_END.set(_this.editor, false);
4347
- return;
4348
- } // Insert the batched text diffs
4349
-
4334
+ marks = _this$editor.marks; // Insert the batched text diffs
4350
4335
 
4351
4336
  insertedText.forEach(function (insertion) {
4352
4337
  var text = insertion.text.insertText;
@@ -4364,9 +4349,8 @@
4364
4349
  });
4365
4350
  _this.editor.marks = null;
4366
4351
  } else {
4367
- slate.Transforms.insertText(_this.editor, text, {
4368
- at: at
4369
- });
4352
+ slate.Transforms.setSelection(_this.editor, at);
4353
+ slate.Editor.insertText(_this.editor, text);
4370
4354
  }
4371
4355
  });
4372
4356
  };
@@ -4995,39 +4979,6 @@
4995
4979
  setTimeout(function () {
4996
4980
  state.isComposing && setIsComposing(false);
4997
4981
  state.isComposing = false;
4998
- IS_COMPOSING.set(editor, false);
4999
- IS_ON_COMPOSITION_END.set(editor, true);
5000
- var insertedText = EDITOR_ON_COMPOSITION_TEXT.get(editor) || []; // `insertedText` is set in `MutationObserver` constructor.
5001
- // If open phone keyboard association function, `CompositionEvent` will be triggered.
5002
-
5003
- if (!insertedText.length) {
5004
- return;
5005
- }
5006
-
5007
- EDITOR_ON_COMPOSITION_TEXT.set(editor, []);
5008
- var selection = editor.selection,
5009
- marks = editor.marks;
5010
- insertedText.forEach(function (insertion) {
5011
- var text = insertion.text.insertText;
5012
- var at = normalizeTextInsertionRange(editor, selection, insertion);
5013
-
5014
- if (marks) {
5015
- var node = _objectSpread({
5016
- text: text
5017
- }, marks);
5018
-
5019
- slate.Transforms.insertNodes(editor, node, {
5020
- match: slate.Text.isText,
5021
- at: at,
5022
- select: true
5023
- });
5024
- editor.marks = null;
5025
- } else {
5026
- slate.Transforms.insertText(editor, text, {
5027
- at: at
5028
- });
5029
- }
5030
- });
5031
4982
  }, RESOLVE_DELAY);
5032
4983
  }
5033
4984
  }, [attributes.onCompositionEnd]),
@@ -5035,14 +4986,12 @@
5035
4986
  if (hasEditableTarget(editor, event.target) && !isEventHandled(event, attributes.onCompositionUpdate)) {
5036
4987
  !state.isComposing && setIsComposing(true);
5037
4988
  state.isComposing = true;
5038
- IS_COMPOSING.set(editor, true);
5039
4989
  }
5040
4990
  }, [attributes.onCompositionUpdate]),
5041
4991
  onCompositionStart: React.useCallback(function (event) {
5042
4992
  if (hasEditableTarget(editor, event.target) && !isEventHandled(event, attributes.onCompositionStart)) {
5043
4993
  !state.isComposing && setIsComposing(true);
5044
4994
  state.isComposing = true;
5045
- IS_COMPOSING.set(editor, true);
5046
4995
  }
5047
4996
  }, [attributes.onCompositionStart]),
5048
4997
  onPaste: React.useCallback(function (event) {
@@ -5128,7 +5077,9 @@
5128
5077
  });
5129
5078
  useIsomorphicLayoutEffect(function () {
5130
5079
  var fn = function fn() {
5131
- return setIsFocused(ReactEditor.isFocused(editor));
5080
+ setTimeout(function () {
5081
+ setIsFocused(ReactEditor.isFocused(editor));
5082
+ }, 0);
5132
5083
  };
5133
5084
 
5134
5085
  document.addEventListener('focus', fn, true);
@@ -5138,7 +5089,9 @@
5138
5089
  }, []);
5139
5090
  useIsomorphicLayoutEffect(function () {
5140
5091
  var fn = function fn() {
5141
- return setIsFocused(ReactEditor.isFocused(editor));
5092
+ setTimeout(function () {
5093
+ setIsFocused(ReactEditor.isFocused(editor));
5094
+ }, 0);
5142
5095
  };
5143
5096
 
5144
5097
  document.addEventListener('blur', fn, true);