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.
package/dist/index.js CHANGED
@@ -292,7 +292,6 @@ var ELEMENT_TO_NODE = new WeakMap();
292
292
  var NODE_TO_ELEMENT = new WeakMap();
293
293
  var NODE_TO_KEY = new WeakMap();
294
294
  var EDITOR_TO_KEY_TO_ELEMENT = new WeakMap();
295
- var EDITOR_TO_STYLE_ELEMENT = new WeakMap();
296
295
  /**
297
296
  * Weak maps for storing editor-related state.
298
297
  */
@@ -1538,22 +1537,6 @@ var useSlateStatic = function useSlateStatic() {
1538
1537
  return editor;
1539
1538
  };
1540
1539
 
1541
- /**
1542
- * Returns a set of rules that use the `:where` selector if it is supported,
1543
- * otherwise it falls back to the provided selector on its own.
1544
- *
1545
- * The `:where` selector is used to give a selector a lower specificity,
1546
- * allowing the rule to be overridden by a user-defined stylesheet.
1547
- *
1548
- * Older browsers do not support the `:where` selector.
1549
- * If it is not supported, the selector will be used without `:where`,
1550
- * which means that the rule will have a higher specificity and a user-defined
1551
- * stylesheet will not be able to override it easily.
1552
- */
1553
- function whereIfSupported(selector, styles) {
1554
- return "@supports (selector(:where(".concat(selector, "))) {") + ":where(".concat(selector, ") { ").concat(styles, " }") + "}" + "@supports not (selector(:where(".concat(selector, "))) {") + "".concat(selector, " { ").concat(styles, " }") + "}";
1555
- }
1556
-
1557
1540
  /**
1558
1541
  * Individual leaves in a text node with unique formatting.
1559
1542
  */
@@ -1568,6 +1551,7 @@ var Leaf = function Leaf(props) {
1568
1551
  renderLeaf = _props$renderLeaf === void 0 ? function (props) {
1569
1552
  return /*#__PURE__*/React__default['default'].createElement(DefaultLeaf, Object.assign({}, props));
1570
1553
  } : _props$renderLeaf;
1554
+ var lastPlaceholderRef = React.useRef(null);
1571
1555
  var placeholderRef = React.useRef(null);
1572
1556
  var editor = useSlateStatic();
1573
1557
  var placeholderResizeObserver = React.useRef(null);
@@ -1594,32 +1578,23 @@ var Leaf = function Leaf(props) {
1594
1578
  } else if (placeholderEl) {
1595
1579
  // Create a new observer and observe the placeholder element.
1596
1580
  var ResizeObserver = window.ResizeObserver || resizeObserver.ResizeObserver;
1597
- placeholderResizeObserver.current = new ResizeObserver(function (_ref) {
1598
- var _ref2 = _slicedToArray(_ref, 1),
1599
- target = _ref2[0].target;
1600
-
1601
- var styleElement = EDITOR_TO_STYLE_ELEMENT.get(editor);
1602
-
1603
- if (styleElement) {
1604
- // Make the min-height the height of the placeholder.
1605
- var selector = "[data-slate-editor-id=\"".concat(editor.id, "\"]");
1606
- var styles = "min-height: ".concat(target.clientHeight, "px;");
1607
- styleElement.innerHTML = whereIfSupported(selector, styles);
1608
- }
1581
+ placeholderResizeObserver.current = new ResizeObserver(function () {
1582
+ // Force a re-render of the editor so its min-height can be updated
1583
+ // to the new height of the placeholder.
1584
+ var forceRender = EDITOR_TO_FORCE_RENDER.get(editor);
1585
+ forceRender === null || forceRender === void 0 ? void 0 : forceRender();
1609
1586
  });
1610
1587
  placeholderResizeObserver.current.observe(placeholderEl);
1611
1588
  }
1612
1589
 
1613
- if (!placeholderEl) {
1590
+ if (!placeholderEl && lastPlaceholderRef.current) {
1614
1591
  // No placeholder element, so no need for a resize observer.
1615
- var styleElement = EDITOR_TO_STYLE_ELEMENT.get(editor);
1616
-
1617
- if (styleElement) {
1618
- // No min-height if there is no placeholder.
1619
- styleElement.innerHTML = '';
1620
- }
1592
+ // Force a re-render of the editor so its min-height can be reset.
1593
+ var forceRender = EDITOR_TO_FORCE_RENDER.get(editor);
1594
+ forceRender === null || forceRender === void 0 ? void 0 : forceRender();
1621
1595
  }
1622
1596
 
1597
+ lastPlaceholderRef.current = placeholderRef.current;
1623
1598
  return function () {
1624
1599
  EDITOR_TO_PLACEHOLDER_ELEMENT["delete"](editor);
1625
1600
  };
@@ -3615,7 +3590,7 @@ function useTrackUserInput() {
3615
3590
  };
3616
3591
  }
3617
3592
 
3618
- var _excluded$1 = ["autoFocus", "decorate", "onDOMBeforeInput", "placeholder", "readOnly", "renderElement", "renderLeaf", "renderPlaceholder", "scrollSelectionIntoView", "style", "as"],
3593
+ var _excluded$1 = ["autoFocus", "decorate", "onDOMBeforeInput", "placeholder", "readOnly", "renderElement", "renderLeaf", "renderPlaceholder", "scrollSelectionIntoView", "style", "as", "disableDefaultStyles"],
3619
3594
  _excluded2 = ["text"];
3620
3595
 
3621
3596
  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; } } }; }
@@ -3630,15 +3605,15 @@ function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) {
3630
3605
 
3631
3606
  var Children = function Children(props) {
3632
3607
  return /*#__PURE__*/React__default['default'].createElement(React__default['default'].Fragment, null, useChildren(props));
3633
- }; // The number of Editable components currently mounted.
3634
-
3635
-
3636
- var mountedCount = 0;
3608
+ };
3637
3609
  /**
3638
3610
  * Editable.
3639
3611
  */
3640
3612
 
3613
+
3641
3614
  var Editable = function Editable(props) {
3615
+ var _EDITOR_TO_PLACEHOLDE, _EDITOR_TO_PLACEHOLDE2;
3616
+
3642
3617
  var autoFocus = props.autoFocus,
3643
3618
  _props$decorate = props.decorate,
3644
3619
  decorate = _props$decorate === void 0 ? defaultDecorate : _props$decorate,
@@ -3655,9 +3630,11 @@ var Editable = function Editable(props) {
3655
3630
  _props$scrollSelectio = props.scrollSelectionIntoView,
3656
3631
  scrollSelectionIntoView = _props$scrollSelectio === void 0 ? defaultScrollSelectionIntoView : _props$scrollSelectio,
3657
3632
  _props$style = props.style,
3658
- style = _props$style === void 0 ? {} : _props$style,
3633
+ userStyle = _props$style === void 0 ? {} : _props$style,
3659
3634
  _props$as = props.as,
3660
3635
  Component = _props$as === void 0 ? 'div' : _props$as,
3636
+ _props$disableDefault = props.disableDefaultStyles,
3637
+ disableDefaultStyles = _props$disableDefault === void 0 ? false : _props$disableDefault,
3661
3638
  attributes = _objectWithoutProperties(props, _excluded$1);
3662
3639
 
3663
3640
  var editor = useSlate(); // Rerender editor when composition status changed
@@ -4261,39 +4238,7 @@ var Editable = function Editable(props) {
4261
4238
  EDITOR_TO_PENDING_INSERTION_MARKS["delete"](editor);
4262
4239
  });
4263
4240
  });
4264
- React.useEffect(function () {
4265
- mountedCount++;
4266
-
4267
- if (mountedCount === 1) {
4268
- // Set global default styles for editors.
4269
- var defaultStylesElement = document.createElement('style');
4270
- defaultStylesElement.setAttribute('data-slate-default-styles', 'true');
4271
- var selector = '[data-slate-editor]';
4272
- var defaultStyles = // Allow positioning relative to the editable element.
4273
- "position: relative;" + // Prevent the default outline styles.
4274
- "outline: none;" + // Preserve adjacent whitespace and new lines.
4275
- "white-space: pre-wrap;" + // Allow words to break if they are too long.
4276
- "word-wrap: break-word;";
4277
- defaultStylesElement.innerHTML = whereIfSupported(selector, defaultStyles);
4278
- document.head.appendChild(defaultStylesElement);
4279
- }
4280
-
4281
- return function () {
4282
- var _document$querySelect;
4283
-
4284
- mountedCount--;
4285
- if (mountedCount <= 0) (_document$querySelect = document.querySelector('style[data-slate-default-styles]')) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect.remove();
4286
- };
4287
- }, []);
4288
- React.useEffect(function () {
4289
- var styleElement = document.createElement('style');
4290
- document.head.appendChild(styleElement);
4291
- EDITOR_TO_STYLE_ELEMENT.set(editor, styleElement);
4292
- return function () {
4293
- styleElement.remove();
4294
- EDITOR_TO_STYLE_ELEMENT["delete"](editor);
4295
- };
4296
- }, []);
4241
+ 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;
4297
4242
  return /*#__PURE__*/React__default['default'].createElement(ReadOnlyContext.Provider, {
4298
4243
  value: readOnly
4299
4244
  }, /*#__PURE__*/React__default['default'].createElement(DecorateContext.Provider, {
@@ -4314,7 +4259,6 @@ var Editable = function Editable(props) {
4314
4259
  autoCorrect: HAS_BEFORE_INPUT_SUPPORT || !CAN_USE_DOM ? attributes.autoCorrect : 'false',
4315
4260
  autoCapitalize: HAS_BEFORE_INPUT_SUPPORT || !CAN_USE_DOM ? attributes.autoCapitalize : 'false',
4316
4261
  "data-slate-editor": true,
4317
- "data-slate-editor-id": editor.id,
4318
4262
  "data-slate-node": "value",
4319
4263
  // explicitly set this
4320
4264
  contentEditable: !readOnly,
@@ -4324,7 +4268,18 @@ var Editable = function Editable(props) {
4324
4268
  zindex: -1,
4325
4269
  suppressContentEditableWarning: true,
4326
4270
  ref: ref,
4327
- style: style,
4271
+ style: _objectSpread$1(_objectSpread$1({}, disableDefaultStyles ? {} : _objectSpread$1({
4272
+ // Allow positioning relative to the editable element.
4273
+ position: 'relative',
4274
+ // Prevent the default outline styles.
4275
+ outline: 'none',
4276
+ // Preserve adjacent whitespace and new lines.
4277
+ whiteSpace: 'pre-wrap',
4278
+ // Allow words to break if they are too long.
4279
+ wordWrap: 'break-word'
4280
+ }, placeholderHeight ? {
4281
+ minHeight: placeholderHeight
4282
+ } : {})), userStyle),
4328
4283
  onBeforeInput: React.useCallback(function (event) {
4329
4284
  // COMPAT: Certain browsers don't support the `beforeinput` event, so we
4330
4285
  // fall back to React's leaky polyfill instead just for it. It