slate-react 0.89.0 → 0.90.0

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.
@@ -945,7 +945,6 @@
945
945
  var NODE_TO_ELEMENT = new WeakMap();
946
946
  var NODE_TO_KEY = new WeakMap();
947
947
  var EDITOR_TO_KEY_TO_ELEMENT = new WeakMap();
948
- var EDITOR_TO_STYLE_ELEMENT = new WeakMap();
949
948
  /**
950
949
  * Weak maps for storing editor-related state.
951
950
  */
@@ -2690,22 +2689,6 @@
2690
2689
  return editor;
2691
2690
  };
2692
2691
 
2693
- /**
2694
- * Returns a set of rules that use the `:where` selector if it is supported,
2695
- * otherwise it falls back to the provided selector on its own.
2696
- *
2697
- * The `:where` selector is used to give a selector a lower specificity,
2698
- * allowing the rule to be overridden by a user-defined stylesheet.
2699
- *
2700
- * Older browsers do not support the `:where` selector.
2701
- * If it is not supported, the selector will be used without `:where`,
2702
- * which means that the rule will have a higher specificity and a user-defined
2703
- * stylesheet will not be able to override it easily.
2704
- */
2705
- function whereIfSupported(selector, styles) {
2706
- return "@supports (selector(:where(".concat(selector, "))) {") + ":where(".concat(selector, ") { ").concat(styles, " }") + "}" + "@supports not (selector(:where(".concat(selector, "))) {") + "".concat(selector, " { ").concat(styles, " }") + "}";
2707
- }
2708
-
2709
2692
  /**
2710
2693
  * Individual leaves in a text node with unique formatting.
2711
2694
  */
@@ -2720,6 +2703,7 @@
2720
2703
  renderLeaf = _props$renderLeaf === void 0 ? function (props) {
2721
2704
  return /*#__PURE__*/React__default['default'].createElement(DefaultLeaf, Object.assign({}, props));
2722
2705
  } : _props$renderLeaf;
2706
+ var lastPlaceholderRef = React.useRef(null);
2723
2707
  var placeholderRef = React.useRef(null);
2724
2708
  var editor = useSlateStatic();
2725
2709
  var placeholderResizeObserver = React.useRef(null);
@@ -2746,32 +2730,23 @@
2746
2730
  } else if (placeholderEl) {
2747
2731
  // Create a new observer and observe the placeholder element.
2748
2732
  var ResizeObserver$1 = window.ResizeObserver || ResizeObserver;
2749
- placeholderResizeObserver.current = new ResizeObserver$1(function (_ref) {
2750
- var _ref2 = _slicedToArray(_ref, 1),
2751
- target = _ref2[0].target;
2752
-
2753
- var styleElement = EDITOR_TO_STYLE_ELEMENT.get(editor);
2754
-
2755
- if (styleElement) {
2756
- // Make the min-height the height of the placeholder.
2757
- var selector = "[data-slate-editor-id=\"".concat(editor.id, "\"]");
2758
- var styles = "min-height: ".concat(target.clientHeight, "px;");
2759
- styleElement.innerHTML = whereIfSupported(selector, styles);
2760
- }
2733
+ placeholderResizeObserver.current = new ResizeObserver$1(function () {
2734
+ // Force a re-render of the editor so its min-height can be updated
2735
+ // to the new height of the placeholder.
2736
+ var forceRender = EDITOR_TO_FORCE_RENDER.get(editor);
2737
+ forceRender === null || forceRender === void 0 ? void 0 : forceRender();
2761
2738
  });
2762
2739
  placeholderResizeObserver.current.observe(placeholderEl);
2763
2740
  }
2764
2741
 
2765
- if (!placeholderEl) {
2742
+ if (!placeholderEl && lastPlaceholderRef.current) {
2766
2743
  // No placeholder element, so no need for a resize observer.
2767
- var styleElement = EDITOR_TO_STYLE_ELEMENT.get(editor);
2768
-
2769
- if (styleElement) {
2770
- // No min-height if there is no placeholder.
2771
- styleElement.innerHTML = '';
2772
- }
2744
+ // Force a re-render of the editor so its min-height can be reset.
2745
+ var forceRender = EDITOR_TO_FORCE_RENDER.get(editor);
2746
+ forceRender === null || forceRender === void 0 ? void 0 : forceRender();
2773
2747
  }
2774
2748
 
2749
+ lastPlaceholderRef.current = placeholderRef.current;
2775
2750
  return function () {
2776
2751
  EDITOR_TO_PLACEHOLDER_ELEMENT["delete"](editor);
2777
2752
  };
@@ -5023,7 +4998,7 @@
5023
4998
  };
5024
4999
  }
5025
5000
 
5026
- var _excluded$1 = ["autoFocus", "decorate", "onDOMBeforeInput", "placeholder", "readOnly", "renderElement", "renderLeaf", "renderPlaceholder", "scrollSelectionIntoView", "style", "as"],
5001
+ var _excluded$1 = ["autoFocus", "decorate", "onDOMBeforeInput", "placeholder", "readOnly", "renderElement", "renderLeaf", "renderPlaceholder", "scrollSelectionIntoView", "style", "as", "disableDefaultStyles"],
5027
5002
  _excluded2 = ["text"];
5028
5003
 
5029
5004
  function _createForOfIteratorHelper$1(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$1(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
@@ -5038,15 +5013,15 @@
5038
5013
 
5039
5014
  var Children = function Children(props) {
5040
5015
  return /*#__PURE__*/React__default['default'].createElement(React__default['default'].Fragment, null, useChildren(props));
5041
- }; // The number of Editable components currently mounted.
5042
-
5043
-
5044
- var mountedCount = 0;
5016
+ };
5045
5017
  /**
5046
5018
  * Editable.
5047
5019
  */
5048
5020
 
5021
+
5049
5022
  var Editable = function Editable(props) {
5023
+ var _EDITOR_TO_PLACEHOLDE, _EDITOR_TO_PLACEHOLDE2;
5024
+
5050
5025
  var autoFocus = props.autoFocus,
5051
5026
  _props$decorate = props.decorate,
5052
5027
  decorate = _props$decorate === void 0 ? defaultDecorate : _props$decorate,
@@ -5063,9 +5038,11 @@
5063
5038
  _props$scrollSelectio = props.scrollSelectionIntoView,
5064
5039
  scrollSelectionIntoView = _props$scrollSelectio === void 0 ? defaultScrollSelectionIntoView : _props$scrollSelectio,
5065
5040
  _props$style = props.style,
5066
- style = _props$style === void 0 ? {} : _props$style,
5041
+ userStyle = _props$style === void 0 ? {} : _props$style,
5067
5042
  _props$as = props.as,
5068
5043
  Component = _props$as === void 0 ? 'div' : _props$as,
5044
+ _props$disableDefault = props.disableDefaultStyles,
5045
+ disableDefaultStyles = _props$disableDefault === void 0 ? false : _props$disableDefault,
5069
5046
  attributes = _objectWithoutProperties(props, _excluded$1);
5070
5047
 
5071
5048
  var editor = useSlate(); // Rerender editor when composition status changed
@@ -5669,39 +5646,7 @@
5669
5646
  EDITOR_TO_PENDING_INSERTION_MARKS["delete"](editor);
5670
5647
  });
5671
5648
  });
5672
- React.useEffect(function () {
5673
- mountedCount++;
5674
-
5675
- if (mountedCount === 1) {
5676
- // Set global default styles for editors.
5677
- var defaultStylesElement = document.createElement('style');
5678
- defaultStylesElement.setAttribute('data-slate-default-styles', 'true');
5679
- var selector = '[data-slate-editor]';
5680
- var defaultStyles = // Allow positioning relative to the editable element.
5681
- "position: relative;" + // Prevent the default outline styles.
5682
- "outline: none;" + // Preserve adjacent whitespace and new lines.
5683
- "white-space: pre-wrap;" + // Allow words to break if they are too long.
5684
- "word-wrap: break-word;";
5685
- defaultStylesElement.innerHTML = whereIfSupported(selector, defaultStyles);
5686
- document.head.appendChild(defaultStylesElement);
5687
- }
5688
-
5689
- return function () {
5690
- var _document$querySelect;
5691
-
5692
- mountedCount--;
5693
- if (mountedCount <= 0) (_document$querySelect = document.querySelector('style[data-slate-default-styles]')) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect.remove();
5694
- };
5695
- }, []);
5696
- React.useEffect(function () {
5697
- var styleElement = document.createElement('style');
5698
- document.head.appendChild(styleElement);
5699
- EDITOR_TO_STYLE_ELEMENT.set(editor, styleElement);
5700
- return function () {
5701
- styleElement.remove();
5702
- EDITOR_TO_STYLE_ELEMENT["delete"](editor);
5703
- };
5704
- }, []);
5649
+ var placeholderHeight = (_EDITOR_TO_PLACEHOLDE = EDITOR_TO_PLACEHOLDER_ELEMENT.get(editor)) === null || _EDITOR_TO_PLACEHOLDE === void 0 ? void 0 : (_EDITOR_TO_PLACEHOLDE2 = _EDITOR_TO_PLACEHOLDE.getBoundingClientRect()) === null || _EDITOR_TO_PLACEHOLDE2 === void 0 ? void 0 : _EDITOR_TO_PLACEHOLDE2.height;
5705
5650
  return /*#__PURE__*/React__default['default'].createElement(ReadOnlyContext.Provider, {
5706
5651
  value: readOnly
5707
5652
  }, /*#__PURE__*/React__default['default'].createElement(DecorateContext.Provider, {
@@ -5722,7 +5667,6 @@
5722
5667
  autoCorrect: HAS_BEFORE_INPUT_SUPPORT || !CAN_USE_DOM ? attributes.autoCorrect : 'false',
5723
5668
  autoCapitalize: HAS_BEFORE_INPUT_SUPPORT || !CAN_USE_DOM ? attributes.autoCapitalize : 'false',
5724
5669
  "data-slate-editor": true,
5725
- "data-slate-editor-id": editor.id,
5726
5670
  "data-slate-node": "value",
5727
5671
  // explicitly set this
5728
5672
  contentEditable: !readOnly,
@@ -5732,7 +5676,18 @@
5732
5676
  zindex: -1,
5733
5677
  suppressContentEditableWarning: true,
5734
5678
  ref: ref,
5735
- style: style,
5679
+ style: _objectSpread$1(_objectSpread$1({}, disableDefaultStyles ? {} : _objectSpread$1({
5680
+ // Allow positioning relative to the editable element.
5681
+ position: 'relative',
5682
+ // Prevent the default outline styles.
5683
+ outline: 'none',
5684
+ // Preserve adjacent whitespace and new lines.
5685
+ whiteSpace: 'pre-wrap',
5686
+ // Allow words to break if they are too long.
5687
+ wordWrap: 'break-word'
5688
+ }, placeholderHeight ? {
5689
+ minHeight: placeholderHeight
5690
+ } : {})), userStyle),
5736
5691
  onBeforeInput: React.useCallback(function (event) {
5737
5692
  // COMPAT: Certain browsers don't support the `beforeinput` event, so we
5738
5693
  // fall back to React's leaky polyfill instead just for it. It