slate-react 0.72.1 → 0.72.2-20211116213954

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
@@ -314,6 +314,13 @@ var EDITOR_TO_KEY_TO_ELEMENT = new WeakMap();
314
314
 
315
315
  var IS_READ_ONLY = new WeakMap();
316
316
  var IS_FOCUSED = new WeakMap();
317
+ var IS_COMPOSING = new WeakMap();
318
+ var IS_ON_COMPOSITION_END = new WeakMap();
319
+ /**
320
+ * Weak maps for saving text on composition stage.
321
+ */
322
+
323
+ var EDITOR_ON_COMPOSITION_TEXT = new WeakMap();
317
324
  /**
318
325
  * Weak map for associating the context `onChange` context with the plugin.
319
326
  */
@@ -2349,6 +2356,229 @@ var isDOMEventHandled = function isDOMEventHandled(event, handler) {
2349
2356
  return event.defaultPrevented;
2350
2357
  };
2351
2358
 
2359
+ /**
2360
+ * Returns the number of characters that are the same at the beginning of the
2361
+ * String.
2362
+ *
2363
+ * @param prev the previous text
2364
+ * @param next the next text
2365
+ * @returns the offset of the start of the difference; null if there is no difference
2366
+ */
2367
+
2368
+ function getDiffStart(prev, next) {
2369
+ var length = Math.min(prev.length, next.length);
2370
+
2371
+ for (var i = 0; i < length; i++) {
2372
+ if (prev.charAt(i) !== next.charAt(i)) return i;
2373
+ }
2374
+
2375
+ if (prev.length !== next.length) return length;
2376
+ return null;
2377
+ }
2378
+ /**
2379
+ * Returns the number of characters that are the same at the end of the String
2380
+ * up to `max`. Max prevents double-counting characters when there are
2381
+ * multiple duplicate characters around the diff area.
2382
+ *
2383
+ * @param prev the previous text
2384
+ * @param next the next text
2385
+ * @param max the max length to test.
2386
+ * @returns number of characters that are the same at the end of the string
2387
+ */
2388
+
2389
+
2390
+ function getDiffEnd(prev, next, max) {
2391
+ var prevLength = prev.length;
2392
+ var nextLength = next.length;
2393
+ var length = Math.min(prevLength, nextLength, max);
2394
+
2395
+ for (var i = 0; i < length; i++) {
2396
+ var prevChar = prev.charAt(prevLength - i - 1);
2397
+ var nextChar = next.charAt(nextLength - i - 1);
2398
+ if (prevChar !== nextChar) return i;
2399
+ }
2400
+
2401
+ if (prev.length !== next.length) return length;
2402
+ return null;
2403
+ }
2404
+ /**
2405
+ * Takes two strings and returns an object representing two offsets. The
2406
+ * first, `start` represents the number of characters that are the same at
2407
+ * the front of the String. The `end` represents the number of characters
2408
+ * that are the same at the end of the String.
2409
+ *
2410
+ * Returns null if they are identical.
2411
+ *
2412
+ * @param prev the previous text
2413
+ * @param next the next text
2414
+ * @returns the difference text range; null if there are no differences.
2415
+ */
2416
+
2417
+
2418
+ function getDiffOffsets(prev, next) {
2419
+ if (prev === next) return null;
2420
+ var start = getDiffStart(prev, next);
2421
+ if (start === null) return null;
2422
+ var maxEnd = Math.min(prev.length - start, next.length - start);
2423
+ var end = getDiffEnd(prev, next, maxEnd);
2424
+ if (end === null) return null;
2425
+ return {
2426
+ start: start,
2427
+ end: end
2428
+ };
2429
+ }
2430
+ /**
2431
+ * Takes a text string and returns a slice from the string at the given text range
2432
+ *
2433
+ * @param text the text
2434
+ * @param offsets the text range
2435
+ * @returns the text slice at text range
2436
+ */
2437
+
2438
+
2439
+ function sliceText(text, offsets) {
2440
+ return text.slice(offsets.start, text.length - offsets.end);
2441
+ }
2442
+ /**
2443
+ * Takes two strings and returns a smart diff that can be used to describe the
2444
+ * change in a way that can be used as operations like inserting, removing or
2445
+ * replacing text.
2446
+ *
2447
+ * @param prev the previous text
2448
+ * @param next the next text
2449
+ * @returns the text difference
2450
+ */
2451
+
2452
+
2453
+ function diffText(prev, next) {
2454
+ if (prev === undefined || next === undefined) return null;
2455
+ var offsets = getDiffOffsets(prev, next);
2456
+ if (offsets == null) return null;
2457
+ var insertText = sliceText(next, offsets);
2458
+ var removeText = sliceText(prev, offsets);
2459
+ return {
2460
+ start: offsets.start,
2461
+ end: prev.length - offsets.end,
2462
+ insertText: insertText,
2463
+ removeText: removeText
2464
+ };
2465
+ }
2466
+ function combineInsertedText(insertedText) {
2467
+ return insertedText.reduce(function (acc, _ref) {
2468
+ var text = _ref.text;
2469
+ return "".concat(acc).concat(text.insertText);
2470
+ }, '');
2471
+ }
2472
+ function getTextInsertion(editor, domNode) {
2473
+ var node = ReactEditor.toSlateNode(editor, domNode);
2474
+
2475
+ if (!slate.Text.isText(node)) {
2476
+ return undefined;
2477
+ }
2478
+
2479
+ var prevText = node.text;
2480
+ var nextText = domNode.textContent; // textContent will pad an extra \n when the textContent ends with an \n
2481
+
2482
+ if (nextText.endsWith('\n')) {
2483
+ nextText = nextText.slice(0, nextText.length - 1);
2484
+ } // If the text is no different, there is no diff.
2485
+
2486
+
2487
+ if (nextText !== prevText) {
2488
+ var textDiff = diffText(prevText, nextText);
2489
+
2490
+ if (textDiff !== null) {
2491
+ var textPath = ReactEditor.findPath(editor, node);
2492
+ return {
2493
+ text: textDiff,
2494
+ path: textPath
2495
+ };
2496
+ }
2497
+ }
2498
+
2499
+ return undefined;
2500
+ }
2501
+ function normalizeTextInsertionRange(editor, range, _ref2) {
2502
+ var path = _ref2.path,
2503
+ text = _ref2.text;
2504
+ var insertionRange = {
2505
+ anchor: {
2506
+ path: path,
2507
+ offset: text.start
2508
+ },
2509
+ focus: {
2510
+ path: path,
2511
+ offset: text.end
2512
+ }
2513
+ };
2514
+
2515
+ if (!range || !slate.Range.isCollapsed(range)) {
2516
+ return insertionRange;
2517
+ }
2518
+
2519
+ var insertText = text.insertText,
2520
+ removeText = text.removeText;
2521
+ var isSingleCharacterInsertion = insertText.length === 1 || removeText.length === 1;
2522
+ /**
2523
+ * This code handles edge cases that arise from text diffing when the
2524
+ * inserted or removed character is a single character, and the character
2525
+ * right before or after the anchor is the same as the one being inserted or
2526
+ * removed.
2527
+ *
2528
+ * Take this example: hello|o
2529
+ *
2530
+ * If another `o` is inserted at the selection's anchor in the example above,
2531
+ * it should be inserted at the anchor, but using text diffing, we actually
2532
+ * detect that the character was inserted after the second `o`:
2533
+ *
2534
+ * helloo[o]|
2535
+ *
2536
+ * Instead, in these very specific edge cases, we assume that the character
2537
+ * needs to be inserted after the anchor rather than where the diff was found:
2538
+ *
2539
+ * hello[o]|o
2540
+ */
2541
+
2542
+ if (isSingleCharacterInsertion && slate.Path.equals(range.anchor.path, path)) {
2543
+ var _Array$from = Array.from(slate.Editor.nodes(editor, {
2544
+ at: range,
2545
+ match: slate.Text.isText
2546
+ })),
2547
+ _Array$from2 = _slicedToArray(_Array$from, 1),
2548
+ _text = _Array$from2[0];
2549
+
2550
+ if (_text) {
2551
+ var _text2 = _slicedToArray(_text, 1),
2552
+ node = _text2[0];
2553
+
2554
+ var anchor = range.anchor;
2555
+ var characterBeforeAnchor = node.text[anchor.offset - 1];
2556
+ var characterAfterAnchor = node.text[anchor.offset];
2557
+
2558
+ if (insertText.length === 1 && insertText === characterAfterAnchor) {
2559
+ // Assume text should be inserted at the anchor
2560
+ return range;
2561
+ }
2562
+
2563
+ if (removeText.length === 1 && removeText === characterBeforeAnchor) {
2564
+ // Assume text should be removed right before the anchor
2565
+ return {
2566
+ anchor: {
2567
+ path: path,
2568
+ offset: anchor.offset - 1
2569
+ },
2570
+ focus: {
2571
+ path: path,
2572
+ offset: anchor.offset
2573
+ }
2574
+ };
2575
+ }
2576
+ }
2577
+ }
2578
+
2579
+ return insertionRange;
2580
+ }
2581
+
2352
2582
  var classCallCheck = createCommonjsModule(function (module) {
2353
2583
  function _classCallCheck(instance, Constructor) {
2354
2584
  if (!(instance instanceof Constructor)) {
@@ -2977,249 +3207,26 @@ var ReactEditor = {
2977
3207
  }
2978
3208
  };
2979
3209
 
2980
- /**
2981
- * Returns the number of characters that are the same at the beginning of the
2982
- * String.
2983
- *
2984
- * @param prev the previous text
2985
- * @param next the next text
2986
- * @returns the offset of the start of the difference; null if there is no difference
2987
- */
3210
+ function gatherMutationData(editor, mutations) {
3211
+ var addedNodes = [];
3212
+ var removedNodes = [];
3213
+ var insertedText = [];
3214
+ var characterDataMutations = [];
3215
+ mutations.forEach(function (mutation) {
3216
+ switch (mutation.type) {
3217
+ case 'childList':
3218
+ {
3219
+ if (mutation.addedNodes.length) {
3220
+ mutation.addedNodes.forEach(function (addedNode) {
3221
+ addedNodes.push(addedNode);
3222
+ });
3223
+ }
2988
3224
 
2989
- function getDiffStart(prev, next) {
2990
- var length = Math.min(prev.length, next.length);
2991
-
2992
- for (var i = 0; i < length; i++) {
2993
- if (prev.charAt(i) !== next.charAt(i)) return i;
2994
- }
2995
-
2996
- if (prev.length !== next.length) return length;
2997
- return null;
2998
- }
2999
- /**
3000
- * Returns the number of characters that are the same at the end of the String
3001
- * up to `max`. Max prevents double-counting characters when there are
3002
- * multiple duplicate characters around the diff area.
3003
- *
3004
- * @param prev the previous text
3005
- * @param next the next text
3006
- * @param max the max length to test.
3007
- * @returns number of characters that are the same at the end of the string
3008
- */
3009
-
3010
-
3011
- function getDiffEnd(prev, next, max) {
3012
- var prevLength = prev.length;
3013
- var nextLength = next.length;
3014
- var length = Math.min(prevLength, nextLength, max);
3015
-
3016
- for (var i = 0; i < length; i++) {
3017
- var prevChar = prev.charAt(prevLength - i - 1);
3018
- var nextChar = next.charAt(nextLength - i - 1);
3019
- if (prevChar !== nextChar) return i;
3020
- }
3021
-
3022
- if (prev.length !== next.length) return length;
3023
- return null;
3024
- }
3025
- /**
3026
- * Takes two strings and returns an object representing two offsets. The
3027
- * first, `start` represents the number of characters that are the same at
3028
- * the front of the String. The `end` represents the number of characters
3029
- * that are the same at the end of the String.
3030
- *
3031
- * Returns null if they are identical.
3032
- *
3033
- * @param prev the previous text
3034
- * @param next the next text
3035
- * @returns the difference text range; null if there are no differences.
3036
- */
3037
-
3038
-
3039
- function getDiffOffsets(prev, next) {
3040
- if (prev === next) return null;
3041
- var start = getDiffStart(prev, next);
3042
- if (start === null) return null;
3043
- var maxEnd = Math.min(prev.length - start, next.length - start);
3044
- var end = getDiffEnd(prev, next, maxEnd);
3045
- if (end === null) return null;
3046
- return {
3047
- start: start,
3048
- end: end
3049
- };
3050
- }
3051
- /**
3052
- * Takes a text string and returns a slice from the string at the given text range
3053
- *
3054
- * @param text the text
3055
- * @param offsets the text range
3056
- * @returns the text slice at text range
3057
- */
3058
-
3059
-
3060
- function sliceText(text, offsets) {
3061
- return text.slice(offsets.start, text.length - offsets.end);
3062
- }
3063
- /**
3064
- * Takes two strings and returns a smart diff that can be used to describe the
3065
- * change in a way that can be used as operations like inserting, removing or
3066
- * replacing text.
3067
- *
3068
- * @param prev the previous text
3069
- * @param next the next text
3070
- * @returns the text difference
3071
- */
3072
-
3073
-
3074
- function diffText(prev, next) {
3075
- if (prev === undefined || next === undefined) return null;
3076
- var offsets = getDiffOffsets(prev, next);
3077
- if (offsets == null) return null;
3078
- var insertText = sliceText(next, offsets);
3079
- var removeText = sliceText(prev, offsets);
3080
- return {
3081
- start: offsets.start,
3082
- end: prev.length - offsets.end,
3083
- insertText: insertText,
3084
- removeText: removeText
3085
- };
3086
- }
3087
- function combineInsertedText(insertedText) {
3088
- return insertedText.reduce(function (acc, _ref) {
3089
- var text = _ref.text;
3090
- return "".concat(acc).concat(text.insertText);
3091
- }, '');
3092
- }
3093
- function getTextInsertion(editor, domNode) {
3094
- var node = ReactEditor.toSlateNode(editor, domNode);
3095
-
3096
- if (!slate.Text.isText(node)) {
3097
- return undefined;
3098
- }
3099
-
3100
- var prevText = node.text;
3101
- var nextText = domNode.textContent; // textContent will pad an extra \n when the textContent ends with an \n
3102
-
3103
- if (nextText.endsWith('\n')) {
3104
- nextText = nextText.slice(0, nextText.length - 1);
3105
- } // If the text is no different, there is no diff.
3106
-
3107
-
3108
- if (nextText !== prevText) {
3109
- var textDiff = diffText(prevText, nextText);
3110
-
3111
- if (textDiff !== null) {
3112
- var textPath = ReactEditor.findPath(editor, node);
3113
- return {
3114
- text: textDiff,
3115
- path: textPath
3116
- };
3117
- }
3118
- }
3119
-
3120
- return undefined;
3121
- }
3122
- function normalizeTextInsertionRange(editor, range, _ref2) {
3123
- var path = _ref2.path,
3124
- text = _ref2.text;
3125
- var insertionRange = {
3126
- anchor: {
3127
- path: path,
3128
- offset: text.start
3129
- },
3130
- focus: {
3131
- path: path,
3132
- offset: text.end
3133
- }
3134
- };
3135
-
3136
- if (!range || !slate.Range.isCollapsed(range)) {
3137
- return insertionRange;
3138
- }
3139
-
3140
- var insertText = text.insertText,
3141
- removeText = text.removeText;
3142
- var isSingleCharacterInsertion = insertText.length === 1 || removeText.length === 1;
3143
- /**
3144
- * This code handles edge cases that arise from text diffing when the
3145
- * inserted or removed character is a single character, and the character
3146
- * right before or after the anchor is the same as the one being inserted or
3147
- * removed.
3148
- *
3149
- * Take this example: hello|o
3150
- *
3151
- * If another `o` is inserted at the selection's anchor in the example above,
3152
- * it should be inserted at the anchor, but using text diffing, we actually
3153
- * detect that the character was inserted after the second `o`:
3154
- *
3155
- * helloo[o]|
3156
- *
3157
- * Instead, in these very specific edge cases, we assume that the character
3158
- * needs to be inserted after the anchor rather than where the diff was found:
3159
- *
3160
- * hello[o]|o
3161
- */
3162
-
3163
- if (isSingleCharacterInsertion && slate.Path.equals(range.anchor.path, path)) {
3164
- var _Array$from = Array.from(slate.Editor.nodes(editor, {
3165
- at: range,
3166
- match: slate.Text.isText
3167
- })),
3168
- _Array$from2 = _slicedToArray(_Array$from, 1),
3169
- _text = _Array$from2[0];
3170
-
3171
- if (_text) {
3172
- var _text2 = _slicedToArray(_text, 1),
3173
- node = _text2[0];
3174
-
3175
- var anchor = range.anchor;
3176
- var characterBeforeAnchor = node.text[anchor.offset - 1];
3177
- var characterAfterAnchor = node.text[anchor.offset];
3178
-
3179
- if (insertText.length === 1 && insertText === characterAfterAnchor) {
3180
- // Assume text should be inserted at the anchor
3181
- return range;
3182
- }
3183
-
3184
- if (removeText.length === 1 && removeText === characterBeforeAnchor) {
3185
- // Assume text should be removed right before the anchor
3186
- return {
3187
- anchor: {
3188
- path: path,
3189
- offset: anchor.offset - 1
3190
- },
3191
- focus: {
3192
- path: path,
3193
- offset: anchor.offset
3194
- }
3195
- };
3196
- }
3197
- }
3198
- }
3199
-
3200
- return insertionRange;
3201
- }
3202
-
3203
- function gatherMutationData(editor, mutations) {
3204
- var addedNodes = [];
3205
- var removedNodes = [];
3206
- var insertedText = [];
3207
- var characterDataMutations = [];
3208
- mutations.forEach(function (mutation) {
3209
- switch (mutation.type) {
3210
- case 'childList':
3211
- {
3212
- if (mutation.addedNodes.length) {
3213
- mutation.addedNodes.forEach(function (addedNode) {
3214
- addedNodes.push(addedNode);
3215
- });
3216
- }
3217
-
3218
- mutation.removedNodes.forEach(function (removedNode) {
3219
- removedNodes.push(removedNode);
3220
- });
3221
- break;
3222
- }
3225
+ mutation.removedNodes.forEach(function (removedNode) {
3226
+ removedNodes.push(removedNode);
3227
+ });
3228
+ break;
3229
+ }
3223
3230
 
3224
3231
  case 'characterData':
3225
3232
  {
@@ -3412,7 +3419,15 @@ var AndroidInputManager = function AndroidInputManager(editor, restoreDOM) {
3412
3419
  this.insertText = function (insertedText) {
3413
3420
  var _this$editor = _this.editor,
3414
3421
  selection = _this$editor.selection,
3415
- marks = _this$editor.marks; // Insert the batched text diffs
3422
+ marks = _this$editor.marks; // If it is in composing or after `onCompositionend`, set `EDITOR_ON_COMPOSITION_TEXT` and return.
3423
+ // Text will be inserted on compositionend event.
3424
+
3425
+ if (IS_COMPOSING.get(_this.editor) || IS_ON_COMPOSITION_END.get(_this.editor)) {
3426
+ EDITOR_ON_COMPOSITION_TEXT.set(_this.editor, insertedText);
3427
+ IS_ON_COMPOSITION_END.set(_this.editor, false);
3428
+ return;
3429
+ } // Insert the batched text diffs
3430
+
3416
3431
 
3417
3432
  insertedText.forEach(function (insertion) {
3418
3433
  var text = insertion.text.insertText;
@@ -3688,7 +3703,10 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
3688
3703
  /**
3689
3704
  * Editable.
3690
3705
  */
3706
+ // https://github.com/facebook/draft-js/blob/main/src/component/handlers/composition/DraftEditorCompositionHandler.js#L41
3707
+ // When using keyboard English association function, conpositionEnd triggered too fast, resulting in after `insertText` still maintain association state.
3691
3708
 
3709
+ var RESOLVE_DELAY = 20;
3692
3710
  var AndroidEditable = function AndroidEditable(props) {
3693
3711
  var autoFocus = props.autoFocus,
3694
3712
  _props$decorate = props.decorate,
@@ -3709,7 +3727,13 @@ var AndroidEditable = function AndroidEditable(props) {
3709
3727
  Component = _props$as === void 0 ? 'div' : _props$as,
3710
3728
  attributes = _objectWithoutProperties(props, _excluded$1);
3711
3729
 
3712
- var editor = useSlate();
3730
+ var editor = useSlate(); // Rerender editor when composition status changed
3731
+
3732
+ var _useState = React.useState(false),
3733
+ _useState2 = _slicedToArray(_useState, 2),
3734
+ isComposing = _useState2[0],
3735
+ setIsComposing = _useState2[1];
3736
+
3713
3737
  var ref = React.useRef(null);
3714
3738
  var inputManager = useAndroidInputManager(ref); // Update internal state on each render.
3715
3739
 
@@ -3717,6 +3741,7 @@ var AndroidEditable = function AndroidEditable(props) {
3717
3741
 
3718
3742
  var state = React.useMemo(function () {
3719
3743
  return {
3744
+ isComposing: false,
3720
3745
  isUpdatingSelection: false,
3721
3746
  latestElement: null
3722
3747
  };
@@ -3742,7 +3767,7 @@ var AndroidEditable = function AndroidEditable(props) {
3742
3767
  var root = ReactEditor.findDocumentOrShadowRoot(editor);
3743
3768
  var domSelection = root.getSelection();
3744
3769
 
3745
- if (!domSelection || !ReactEditor.isFocused(editor)) {
3770
+ if (state.isComposing || !domSelection || !ReactEditor.isFocused(editor)) {
3746
3771
  return;
3747
3772
  }
3748
3773
 
@@ -3822,28 +3847,7 @@ var AndroidEditable = function AndroidEditable(props) {
3822
3847
  if (ref.current && autoFocus) {
3823
3848
  ref.current.focus();
3824
3849
  }
3825
- }, [autoFocus]); // Listen on the native `beforeinput` event to get real "Level 2" events. This
3826
- // is required because React's `beforeinput` is fake and never really attaches
3827
- // to the real event sadly. (2019/11/01)
3828
- // https://github.com/facebook/react/issues/11211
3829
-
3830
- var onDOMBeforeInput = React.useCallback(function (event) {
3831
- if (!readOnly && hasEditableTarget(editor, event.target) && !isDOMEventHandled(event, propsOnDOMBeforeInput)) {
3832
- inputManager.onUserInput();
3833
- }
3834
- }, [readOnly, propsOnDOMBeforeInput]); // Attach a native DOM event handler for `beforeinput` events, because React's
3835
- // built-in `onBeforeInput` is actually a leaky polyfill that doesn't expose
3836
- // real `beforeinput` events sadly... (2019/11/04)
3837
-
3838
- useIsomorphicLayoutEffect(function () {
3839
- var node = ref.current; // @ts-ignore The `beforeinput` event isn't recognized.
3840
-
3841
- node === null || node === void 0 ? void 0 : node.addEventListener('beforeinput', onDOMBeforeInput); // @ts-ignore The `beforeinput` event isn't recognized.
3842
-
3843
- return function () {
3844
- return node === null || node === void 0 ? void 0 : node.removeEventListener('beforeinput', onDOMBeforeInput);
3845
- };
3846
- }, [contentKey, propsOnDOMBeforeInput]); // Listen on the native `selectionchange` event to be able to update any time
3850
+ }, [autoFocus]); // Listen on the native `selectionchange` event to be able to update any time
3847
3851
  // the selection changes. This is required because React's `onSelect` is leaky
3848
3852
  // and non-standard so it doesn't fire until after a selection has been
3849
3853
  // released. This causes issues in situations where another change happens
@@ -3851,7 +3855,7 @@ var AndroidEditable = function AndroidEditable(props) {
3851
3855
 
3852
3856
  var onDOMSelectionChange = React.useCallback(throttle__default['default'](function () {
3853
3857
  try {
3854
- if (!state.isUpdatingSelection && !inputManager.isReconciling.current) {
3858
+ if (!state.isComposing && !state.isUpdatingSelection && !inputManager.isReconciling.current) {
3855
3859
  var root = ReactEditor.findDocumentOrShadowRoot(editor);
3856
3860
  var activeElement = root.activeElement;
3857
3861
  var el = ReactEditor.toDOMNode(editor, editor);
@@ -3885,7 +3889,35 @@ var AndroidEditable = function AndroidEditable(props) {
3885
3889
  }
3886
3890
  } catch (_unused2) {// Failed to update selection, likely due to reconciliation error
3887
3891
  }
3888
- }, 100), [readOnly]); // Attach a native DOM event handler for `selectionchange`, because React's
3892
+ }, 100), [readOnly]);
3893
+ var scheduleOnDOMSelectionChange = React.useMemo(function () {
3894
+ return debounce__default['default'](onDOMSelectionChange, 0);
3895
+ }, [onDOMSelectionChange]); // Listen on the native `beforeinput` event to get real "Level 2" events. This
3896
+ // is required because React's `beforeinput` is fake and never really attaches
3897
+ // to the real event sadly. (2019/11/01)
3898
+ // https://github.com/facebook/react/issues/11211
3899
+
3900
+ var onDOMBeforeInput = React.useCallback(function (event) {
3901
+ if (!readOnly && hasEditableTarget(editor, event.target) && !isDOMEventHandled(event, propsOnDOMBeforeInput)) {
3902
+ // Some IMEs/Chrome extensions like e.g. Grammarly set the selection immediately before
3903
+ // triggering a `beforeinput` expecting the change to be applied to the immediately before
3904
+ // set selection.
3905
+ scheduleOnDOMSelectionChange.flush();
3906
+ inputManager.onUserInput();
3907
+ }
3908
+ }, [readOnly, propsOnDOMBeforeInput]); // Attach a native DOM event handler for `beforeinput` events, because React's
3909
+ // built-in `onBeforeInput` is actually a leaky polyfill that doesn't expose
3910
+ // real `beforeinput` events sadly... (2019/11/04)
3911
+
3912
+ useIsomorphicLayoutEffect(function () {
3913
+ var node = ref.current; // @ts-ignore The `beforeinput` event isn't recognized.
3914
+
3915
+ node === null || node === void 0 ? void 0 : node.addEventListener('beforeinput', onDOMBeforeInput); // @ts-ignore The `beforeinput` event isn't recognized.
3916
+
3917
+ return function () {
3918
+ return node === null || node === void 0 ? void 0 : node.removeEventListener('beforeinput', onDOMBeforeInput);
3919
+ };
3920
+ }, [contentKey, propsOnDOMBeforeInput]); // Attach a native DOM event handler for `selectionchange`, because React's
3889
3921
  // built-in `onSelect` handler doesn't fire for all selection changes. It's a
3890
3922
  // leaky polyfill that only fires on keypresses or clicks. Instead, we want to
3891
3923
  // fire for any change to the selection inside the editor. (2019/11/04)
@@ -3893,14 +3925,14 @@ var AndroidEditable = function AndroidEditable(props) {
3893
3925
 
3894
3926
  useIsomorphicLayoutEffect(function () {
3895
3927
  var window = ReactEditor.getWindow(editor);
3896
- window.document.addEventListener('selectionchange', onDOMSelectionChange);
3928
+ window.document.addEventListener('selectionchange', scheduleOnDOMSelectionChange);
3897
3929
  return function () {
3898
- window.document.removeEventListener('selectionchange', onDOMSelectionChange);
3930
+ window.document.removeEventListener('selectionchange', scheduleOnDOMSelectionChange);
3899
3931
  };
3900
- });
3932
+ }, [scheduleOnDOMSelectionChange]);
3901
3933
  var decorations = decorate([editor, []]);
3902
3934
 
3903
- if (placeholder && editor.children.length === 1 && Array.from(slate.Node.texts(editor)).length === 1 && slate.Node.string(editor) === '') {
3935
+ if (placeholder && editor.children.length === 1 && Array.from(slate.Node.texts(editor)).length === 1 && slate.Node.string(editor) === '' && !isComposing) {
3904
3936
  var _decorations$push;
3905
3937
 
3906
3938
  var start = slate.Editor.start(editor, []);
@@ -4008,6 +4040,92 @@ var AndroidEditable = function AndroidEditable(props) {
4008
4040
 
4009
4041
  IS_FOCUSED["delete"](editor);
4010
4042
  }, [readOnly, attributes.onBlur]),
4043
+ onClick: React.useCallback(function (event) {
4044
+ if (!readOnly && hasTarget(editor, event.target) && !isEventHandled(event, attributes.onClick) && isDOMNode(event.target)) {
4045
+ var node = ReactEditor.toSlateNode(editor, event.target);
4046
+ var path = ReactEditor.findPath(editor, node); // At this time, the Slate document may be arbitrarily different,
4047
+ // because onClick handlers can change the document before we get here.
4048
+ // Therefore we must check that this path actually exists,
4049
+ // and that it still refers to the same node.
4050
+
4051
+ if (slate.Editor.hasPath(editor, path)) {
4052
+ var lookupNode = slate.Node.get(editor, path);
4053
+
4054
+ if (lookupNode === node) {
4055
+ var _start = slate.Editor.start(editor, path);
4056
+
4057
+ var end = slate.Editor.end(editor, path);
4058
+ var startVoid = slate.Editor["void"](editor, {
4059
+ at: _start
4060
+ });
4061
+ var endVoid = slate.Editor["void"](editor, {
4062
+ at: end
4063
+ });
4064
+
4065
+ if (startVoid && endVoid && slate.Path.equals(startVoid[1], endVoid[1])) {
4066
+ var range = slate.Editor.range(editor, _start);
4067
+ slate.Transforms.select(editor, range);
4068
+ }
4069
+ }
4070
+ }
4071
+ }
4072
+ }, [readOnly, attributes.onClick]),
4073
+ onCompositionEnd: React.useCallback(function (event) {
4074
+ if (hasEditableTarget(editor, event.target) && !isEventHandled(event, attributes.onCompositionEnd)) {
4075
+ scheduleOnDOMSelectionChange.flush();
4076
+ setTimeout(function () {
4077
+ state.isComposing && setIsComposing(false);
4078
+ state.isComposing = false;
4079
+ IS_COMPOSING.set(editor, false);
4080
+ IS_ON_COMPOSITION_END.set(editor, true);
4081
+ var insertedText = EDITOR_ON_COMPOSITION_TEXT.get(editor) || []; // `insertedText` is set in `MutationObserver` constructor.
4082
+ // If open phone keyboard association function, `CompositionEvent` will be triggered.
4083
+
4084
+ if (!insertedText.length) {
4085
+ return;
4086
+ }
4087
+
4088
+ EDITOR_ON_COMPOSITION_TEXT.set(editor, []);
4089
+ var selection = editor.selection,
4090
+ marks = editor.marks;
4091
+ insertedText.forEach(function (insertion) {
4092
+ var text = insertion.text.insertText;
4093
+ var at = normalizeTextInsertionRange(editor, selection, insertion);
4094
+
4095
+ if (marks) {
4096
+ var node = _objectSpread({
4097
+ text: text
4098
+ }, marks);
4099
+
4100
+ slate.Transforms.insertNodes(editor, node, {
4101
+ match: slate.Text.isText,
4102
+ at: at,
4103
+ select: true
4104
+ });
4105
+ editor.marks = null;
4106
+ } else {
4107
+ slate.Transforms.insertText(editor, text, {
4108
+ at: at
4109
+ });
4110
+ }
4111
+ });
4112
+ }, RESOLVE_DELAY);
4113
+ }
4114
+ }, [attributes.onCompositionEnd]),
4115
+ onCompositionUpdate: React.useCallback(function (event) {
4116
+ if (hasEditableTarget(editor, event.target) && !isEventHandled(event, attributes.onCompositionUpdate)) {
4117
+ !state.isComposing && setIsComposing(true);
4118
+ state.isComposing = true;
4119
+ IS_COMPOSING.set(editor, true);
4120
+ }
4121
+ }, [attributes.onCompositionUpdate]),
4122
+ onCompositionStart: React.useCallback(function (event) {
4123
+ if (hasEditableTarget(editor, event.target) && !isEventHandled(event, attributes.onCompositionStart)) {
4124
+ !state.isComposing && setIsComposing(true);
4125
+ state.isComposing = true;
4126
+ IS_COMPOSING.set(editor, true);
4127
+ }
4128
+ }, [attributes.onCompositionStart]),
4011
4129
  onPaste: React.useCallback(function (event) {
4012
4130
  // this will make application/x-slate-fragment exist when onPaste attributes is passed
4013
4131
  event.clipboardData = getClipboardData(event.clipboardData); // This unfortunately needs to be handled with paste events instead.