slate-react 0.126.0 → 0.126.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.
package/dist/index.js CHANGED
@@ -7,8 +7,8 @@ var React = require('react');
7
7
  var scrollIntoView = require('scroll-into-view-if-needed');
8
8
  var slate = require('slate');
9
9
  var slateDom = require('slate-dom');
10
- var resizeObserver = require('@juggle/resize-observer');
11
10
  var ReactDOM = require('react-dom');
11
+ var resizeObserver = require('@juggle/resize-observer');
12
12
 
13
13
  function unwrapExports (x) {
14
14
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
@@ -231,6 +231,14 @@ function _objectSpread$8(e) { for (var r = 1; r < arguments.length; r++) { var t
231
231
  var RESOLVE_DELAY = 25;
232
232
  // Time with no user interaction before the current user action is considered as done.
233
233
  var FLUSH_DELAY = 200;
234
+ // Time with no composition input after which a composition that never ended is
235
+ // treated as stale. Long enough that pausing mid-word does not cut a live
236
+ // composition short.
237
+ var COMPOSITION_IDLE_TIMEOUT = 5000;
238
+ // How often a deferred flush re-checks whether the composition is still live.
239
+ // Only bounds how quickly the value catches up with a composition that ended
240
+ // without firing `compositionend`; one that ends normally flushes immediately.
241
+ var COMPOSITION_RECHECK_DELAY = 50;
234
242
  // Replace with `const debug = console.log` to debug
235
243
  var debug = function debug() {};
236
244
  // Type guard to check if a value is a DataTransfer
@@ -245,6 +253,8 @@ function createAndroidInputManager(_ref) {
245
253
  var compositionEndTimeoutId = null;
246
254
  var flushTimeoutId = null;
247
255
  var actionTimeoutId = null;
256
+ var lastCompositionActivity = 0;
257
+ var compositionCleared = false;
248
258
  var idCounter = 0;
249
259
  var insertPositionHint = false;
250
260
  var applyPendingSelection = function applyPendingSelection() {
@@ -276,6 +286,43 @@ function createAndroidInputManager(_ref) {
276
286
  }
277
287
  action.run();
278
288
  };
289
+ // `compositionend` is not fired reliably in every browser, which Slate
290
+ // already works around on keydown. Deferring flushes on a composition that
291
+ // never ends would strand the typed text outside the value, so a composition
292
+ // that has gone quiet, or that has lost focus, no longer holds flushes back.
293
+ var isCompositionLive = function isCompositionLive() {
294
+ if (!slateDom.IS_COMPOSING.get(editor) || compositionCleared) {
295
+ return false;
296
+ }
297
+ try {
298
+ var editable = ReactEditor.toDOMNode(editor, editor);
299
+ var activeElement = ReactEditor.getWindow(editor).document.activeElement;
300
+ if (activeElement !== editable && !editable.contains(activeElement)) {
301
+ return false;
302
+ }
303
+ } catch (_unused) {
304
+ // Fall back to the idle check below if the editable can't be resolved.
305
+ }
306
+ return Date.now() - lastCompositionActivity < COMPOSITION_IDLE_TIMEOUT;
307
+ };
308
+ // A leaf that Slate models as empty renders as a zero-width string, which on
309
+ // Android is a `<br>` rather than a text node. When the IME composes the
310
+ // first character into such a leaf, the browser creates a text node for the
311
+ // composition. Applying the pending diffs re-renders that leaf as a text
312
+ // leaf, which unmounts the zero-width span and takes the text node the IME is
313
+ // composing in with it, silently cancelling the composition.
314
+ var hasPendingDiffsInEmptyLeaf = function hasPendingDiffsInEmptyLeaf() {
315
+ var _EDITOR_TO_PENDING_DI;
316
+ return !!((_EDITOR_TO_PENDING_DI = slateDom.EDITOR_TO_PENDING_DIFFS.get(editor)) !== null && _EDITOR_TO_PENDING_DI !== void 0 && _EDITOR_TO_PENDING_DI.some(function (_ref2) {
317
+ var path = _ref2.path;
318
+ try {
319
+ return slate.Node.leaf(editor, path).text.length === 0;
320
+ } catch (_unused2) {
321
+ // The path may no longer resolve if the editor changed underneath us.
322
+ return false;
323
+ }
324
+ }));
325
+ };
279
326
  var flush = function flush() {
280
327
  if (flushTimeoutId) {
281
328
  clearTimeout(flushTimeoutId);
@@ -285,6 +332,15 @@ function createAndroidInputManager(_ref) {
285
332
  clearTimeout(actionTimeoutId);
286
333
  actionTimeoutId = null;
287
334
  }
335
+ // Defer flushing until the composition ends, so that the re-render that
336
+ // would replace the composing text node cannot happen mid-composition.
337
+ // Composing into a leaf that already has text is unaffected: applying the
338
+ // diff there only updates `textContent`, so the value still updates on
339
+ // every `compositionupdate`.
340
+ if (isCompositionLive() && hasPendingDiffsInEmptyLeaf()) {
341
+ flushTimeoutId = setTimeout(flush, COMPOSITION_RECHECK_DELAY);
342
+ return;
343
+ }
288
344
  if (!hasPendingDiffs() && !hasPendingAction()) {
289
345
  applyPendingSelection();
290
346
  return;
@@ -305,8 +361,8 @@ function createAndroidInputManager(_ref) {
305
361
  debug('flush', slateDom.EDITOR_TO_PENDING_ACTION.get(editor), slateDom.EDITOR_TO_PENDING_DIFFS.get(editor));
306
362
  var scheduleSelectionChange = hasPendingDiffs();
307
363
  var diff;
308
- while (diff = (_EDITOR_TO_PENDING_DI = slateDom.EDITOR_TO_PENDING_DIFFS.get(editor)) === null || _EDITOR_TO_PENDING_DI === void 0 ? void 0 : _EDITOR_TO_PENDING_DI[0]) {
309
- var _EDITOR_TO_PENDING_DI, _EDITOR_TO_PENDING_DI2;
364
+ while (diff = (_EDITOR_TO_PENDING_DI2 = slateDom.EDITOR_TO_PENDING_DIFFS.get(editor)) === null || _EDITOR_TO_PENDING_DI2 === void 0 ? void 0 : _EDITOR_TO_PENDING_DI2[0]) {
365
+ var _EDITOR_TO_PENDING_DI2, _EDITOR_TO_PENDING_DI3;
310
366
  var pendingMarks = slateDom.EDITOR_TO_PENDING_INSERTION_MARKS.get(editor);
311
367
  if (pendingMarks !== undefined) {
312
368
  slateDom.EDITOR_TO_PENDING_INSERTION_MARKS["delete"](editor);
@@ -326,8 +382,8 @@ function createAndroidInputManager(_ref) {
326
382
  }
327
383
  // Remove diff only after we have applied it to account for it when transforming
328
384
  // pending ranges.
329
- slateDom.EDITOR_TO_PENDING_DIFFS.set(editor, (_EDITOR_TO_PENDING_DI2 = slateDom.EDITOR_TO_PENDING_DIFFS.get(editor)) === null || _EDITOR_TO_PENDING_DI2 === void 0 ? void 0 : _EDITOR_TO_PENDING_DI2.filter(function (_ref2) {
330
- var id = _ref2.id;
385
+ slateDom.EDITOR_TO_PENDING_DIFFS.set(editor, (_EDITOR_TO_PENDING_DI3 = slateDom.EDITOR_TO_PENDING_DIFFS.get(editor)) === null || _EDITOR_TO_PENDING_DI3 === void 0 ? void 0 : _EDITOR_TO_PENDING_DI3.filter(function (_ref3) {
386
+ var id = _ref3.id;
331
387
  return id !== diff.id;
332
388
  }));
333
389
  if (!slateDom.verifyDiffState(editor, diff)) {
@@ -371,13 +427,35 @@ function createAndroidInputManager(_ref) {
371
427
  if (compositionEndTimeoutId) {
372
428
  clearTimeout(compositionEndTimeoutId);
373
429
  }
430
+ // Diffs deferred by `flush` have to be applied synchronously here. IMEs
431
+ // that compose one syllable at a time (Hangul, kana) start the next
432
+ // composition in the same tick as this event, so an asynchronous render
433
+ // would replace the text node that composition has already started in.
434
+ if (hasPendingDiffsInEmptyLeaf() && !flushing) {
435
+ ReactDOM.flushSync(function () {
436
+ slateDom.IS_COMPOSING.set(editor, false);
437
+ flush();
438
+ });
439
+ updatePlaceholderVisibility();
440
+ return;
441
+ }
374
442
  compositionEndTimeoutId = setTimeout(function () {
375
443
  slateDom.IS_COMPOSING.set(editor, false);
376
444
  flush();
445
+ if (compositionCleared) {
446
+ var _EDITOR_TO_FORCE_REND;
447
+ // A cancelled composition can take the empty leaf's text node with
448
+ // it. A forced render lets RestoreDOM put the leaf's DOM back, so the
449
+ // next composition starts from a clean state instead of a bare <br>.
450
+ (_EDITOR_TO_FORCE_REND = slateDom.EDITOR_TO_FORCE_RENDER.get(editor)) === null || _EDITOR_TO_FORCE_REND === void 0 || _EDITOR_TO_FORCE_REND();
451
+ }
452
+ updatePlaceholderVisibility();
377
453
  }, RESOLVE_DELAY);
378
454
  };
379
455
  var handleCompositionStart = function handleCompositionStart(_event) {
380
456
  slateDom.IS_COMPOSING.set(editor, true);
457
+ lastCompositionActivity = Date.now();
458
+ compositionCleared = false;
381
459
  if (compositionEndTimeoutId) {
382
460
  clearTimeout(compositionEndTimeoutId);
383
461
  compositionEndTimeoutId = null;
@@ -396,8 +474,8 @@ function createAndroidInputManager(_ref) {
396
474
  placeholderElement.style.removeProperty('display');
397
475
  };
398
476
  var storeDiff = function storeDiff(path, diff) {
399
- var _EDITOR_TO_PENDING_DI3;
400
- var pendingDiffs = (_EDITOR_TO_PENDING_DI3 = slateDom.EDITOR_TO_PENDING_DIFFS.get(editor)) !== null && _EDITOR_TO_PENDING_DI3 !== void 0 ? _EDITOR_TO_PENDING_DI3 : [];
477
+ var _EDITOR_TO_PENDING_DI4;
478
+ var pendingDiffs = (_EDITOR_TO_PENDING_DI4 = slateDom.EDITOR_TO_PENDING_DIFFS.get(editor)) !== null && _EDITOR_TO_PENDING_DI4 !== void 0 ? _EDITOR_TO_PENDING_DI4 : [];
401
479
  slateDom.EDITOR_TO_PENDING_DIFFS.set(editor, pendingDiffs);
402
480
  var target = slate.Node.leaf(editor, path);
403
481
  var idx = pendingDiffs.findIndex(function (change) {
@@ -426,8 +504,8 @@ function createAndroidInputManager(_ref) {
426
504
  });
427
505
  };
428
506
  var scheduleAction = function scheduleAction(run) {
429
- var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
430
- at = _ref3.at;
507
+ var _ref4 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
508
+ at = _ref4.at;
431
509
  insertPositionHint = false;
432
510
  slateDom.EDITOR_TO_PENDING_SELECTION["delete"](editor);
433
511
  scheduleOnDOMSelectionChange.cancel();
@@ -454,6 +532,34 @@ function createAndroidInputManager(_ref) {
454
532
  return;
455
533
  }
456
534
  var type = event.inputType;
535
+ if (type === 'insertCompositionText' || type === 'deleteCompositionText') {
536
+ lastCompositionActivity = Date.now();
537
+ if (event.data) {
538
+ compositionCleared = false;
539
+ } else {
540
+ // The IME threw away what it was composing. The deferred text never
541
+ // reached the value, so the value is already in the desired state -
542
+ // drop the deferral instead of applying and re-deleting it, which
543
+ // would churn the DOM mid-composition and can make Android close the
544
+ // keyboard.
545
+ compositionCleared = true;
546
+ var deferredDiffs = slateDom.EDITOR_TO_PENDING_DIFFS.get(editor);
547
+ if (deferredDiffs !== null && deferredDiffs !== void 0 && deferredDiffs.length) {
548
+ var allInEmptyLeaves = deferredDiffs.every(function (_ref5) {
549
+ var path = _ref5.path;
550
+ try {
551
+ return slate.Node.leaf(editor, path).text.length === 0;
552
+ } catch (_unused3) {
553
+ return false;
554
+ }
555
+ });
556
+ if (allInEmptyLeaves) {
557
+ slateDom.EDITOR_TO_PENDING_DIFFS.set(editor, []);
558
+ slateDom.EDITOR_TO_PENDING_SELECTION["delete"](editor);
559
+ }
560
+ }
561
+ }
562
+ }
457
563
  var targetRange = null;
458
564
  var data = event.dataTransfer || event.data || undefined;
459
565
  if (insertPositionHint !== false && type !== 'insertText' && type !== 'insertCompositionText') {
@@ -827,8 +933,8 @@ function createAndroidInputManager(_ref) {
827
933
  return !!slateDom.EDITOR_TO_PENDING_ACTION.get(editor);
828
934
  };
829
935
  var hasPendingDiffs = function hasPendingDiffs() {
830
- var _EDITOR_TO_PENDING_DI4;
831
- return !!((_EDITOR_TO_PENDING_DI4 = slateDom.EDITOR_TO_PENDING_DIFFS.get(editor)) !== null && _EDITOR_TO_PENDING_DI4 !== void 0 && _EDITOR_TO_PENDING_DI4.length);
936
+ var _EDITOR_TO_PENDING_DI5;
937
+ return !!((_EDITOR_TO_PENDING_DI5 = slateDom.EDITOR_TO_PENDING_DIFFS.get(editor)) !== null && _EDITOR_TO_PENDING_DI5 !== void 0 && _EDITOR_TO_PENDING_DI5.length);
832
938
  };
833
939
  var hasPendingChanges = function hasPendingChanges() {
834
940
  return hasPendingAction() || hasPendingDiffs();
@@ -883,10 +989,10 @@ function createAndroidInputManager(_ref) {
883
989
  if (mutations.some(function (mutation) {
884
990
  return slateDom.isTrackedMutation(editor, mutation, mutations);
885
991
  })) {
886
- var _EDITOR_TO_FORCE_REND;
992
+ var _EDITOR_TO_FORCE_REND2;
887
993
  // Cause a re-render to restore the dom state if we encounter tracked mutations without
888
994
  // a corresponding pending action.
889
- (_EDITOR_TO_FORCE_REND = slateDom.EDITOR_TO_FORCE_RENDER.get(editor)) === null || _EDITOR_TO_FORCE_REND === void 0 || _EDITOR_TO_FORCE_REND();
995
+ (_EDITOR_TO_FORCE_REND2 = slateDom.EDITOR_TO_FORCE_RENDER.get(editor)) === null || _EDITOR_TO_FORCE_REND2 === void 0 || _EDITOR_TO_FORCE_REND2();
890
996
  }
891
997
  };
892
998
  return {
@@ -1034,6 +1140,7 @@ var TextString = function TextString(props) {
1034
1140
  var text = props.text,
1035
1141
  _props$isTrailing = props.isTrailing,
1036
1142
  isTrailing = _props$isTrailing === void 0 ? false : _props$isTrailing;
1143
+ var editor = useSlateStatic();
1037
1144
  var ref = React.useRef(null);
1038
1145
  var getTextContent = function getTextContent() {
1039
1146
  return "".concat(text !== null && text !== void 0 ? text : '').concat(isTrailing ? '\n' : '');
@@ -1053,6 +1160,18 @@ var TextString = function TextString(props) {
1053
1160
  // null coalescing text to make sure we're not outputing "null" as a string in the extreme case it is nullish at runtime
1054
1161
  var textWithTrailing = getTextContent();
1055
1162
  if (ref.current && ref.current.textContent !== textWithTrailing) {
1163
+ // On Android the DOM intentionally runs ahead of the value while the IME
1164
+ // composes, since input is buffered as pending diffs. Writing
1165
+ // `textContent` here would replace the text node the composition lives in
1166
+ // and cancel it; the pending diffs reconcile this span when the
1167
+ // composition ends.
1168
+ if (slateDom.IS_ANDROID && slateDom.IS_COMPOSING.get(editor)) {
1169
+ var _ReactEditor$getWindo;
1170
+ var composingNode = (_ReactEditor$getWindo = ReactEditor.getWindow(editor).getSelection()) === null || _ReactEditor$getWindo === void 0 ? void 0 : _ReactEditor$getWindo.anchorNode;
1171
+ if (composingNode && ref.current.contains(composingNode)) {
1172
+ return;
1173
+ }
1174
+ }
1056
1175
  ref.current.textContent = textWithTrailing;
1057
1176
  }
1058
1177
  // intentionally not specifying dependencies, so that this effect runs on every render
@@ -1095,7 +1214,13 @@ var ZeroWidthString = function ZeroWidthString(props) {
1095
1214
  // be because accepting an IME suggestion when at the start of a block (no
1096
1215
  // preceding \uFEFF) removes one or more DOM elements that `toSlateRange`
1097
1216
  // depends on. (https://github.com/ianstormtaylor/slate/issues/5703)
1098
- return /*#__PURE__*/React.createElement("span", _objectSpread$6({}, attributes), !slateDom.IS_ANDROID || !isLineBreak ? "\uFEFF" : null, isLineBreak ? /*#__PURE__*/React.createElement("br", null) : null);
1217
+ // A leaf with no text node at all forces the IME to create one when the
1218
+ // first character is composed there, and no re-render can then touch the
1219
+ // leaf without cancelling the composition. Rendering the zero-width space on
1220
+ // Android too gives the composition a React-owned text node to live in; the
1221
+ // wrapper keeps its data-slate-zero-width attributes, so selection mapping
1222
+ // is unchanged.
1223
+ return /*#__PURE__*/React.createElement("span", _objectSpread$6({}, attributes), "\uFEFF", isLineBreak ? /*#__PURE__*/React.createElement("br", null) : null);
1099
1224
  };
1100
1225
 
1101
1226
  function ownKeys$5(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }