slate-react 0.86.0 → 0.87.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
@@ -291,6 +291,7 @@ var ELEMENT_TO_NODE = new WeakMap();
291
291
  var NODE_TO_ELEMENT = new WeakMap();
292
292
  var NODE_TO_KEY = new WeakMap();
293
293
  var EDITOR_TO_KEY_TO_ELEMENT = new WeakMap();
294
+ var EDITOR_TO_STYLE_ELEMENT = new WeakMap();
294
295
  /**
295
296
  * Weak maps for storing editor-related state.
296
297
  */
@@ -1504,18 +1505,55 @@ var Leaf = function Leaf(props) {
1504
1505
  } : _props$renderLeaf;
1505
1506
  var placeholderRef = React.useRef(null);
1506
1507
  var editor = useSlateStatic();
1508
+ var placeholderResizeObserver = React.useRef(null);
1509
+ React.useEffect(function () {
1510
+ return function () {
1511
+ if (placeholderResizeObserver.current) {
1512
+ placeholderResizeObserver.current.disconnect();
1513
+ }
1514
+ };
1515
+ }, []);
1507
1516
  React.useEffect(function () {
1508
1517
  var placeholderEl = placeholderRef === null || placeholderRef === void 0 ? void 0 : placeholderRef.current;
1509
- var editorEl = ReactEditor.toDOMNode(editor, editor);
1510
1518
 
1511
- if (!placeholderEl || !editorEl) {
1512
- return;
1519
+ if (placeholderEl) {
1520
+ EDITOR_TO_PLACEHOLDER_ELEMENT.set(editor, placeholderEl);
1521
+ } else {
1522
+ EDITOR_TO_PLACEHOLDER_ELEMENT["delete"](editor);
1523
+ }
1524
+
1525
+ if (placeholderResizeObserver.current) {
1526
+ // Update existing observer.
1527
+ placeholderResizeObserver.current.disconnect();
1528
+ if (placeholderEl) placeholderResizeObserver.current.observe(placeholderEl);
1529
+ } else if (placeholderEl) {
1530
+ // Create a new observer and observe the placeholder element.
1531
+ placeholderResizeObserver.current = new ResizeObserver(function (_ref) {
1532
+ var _ref2 = _slicedToArray(_ref, 1),
1533
+ target = _ref2[0].target;
1534
+
1535
+ var styleElement = EDITOR_TO_STYLE_ELEMENT.get(editor);
1536
+
1537
+ if (styleElement) {
1538
+ // Make the min-height the height of the placeholder.
1539
+ var minHeight = "".concat(target.clientHeight, "px");
1540
+ styleElement.innerHTML = ":where([data-slate-editor-id=\"".concat(editor.id, "\"]) { min-height: ").concat(minHeight, "; }");
1541
+ }
1542
+ });
1543
+ placeholderResizeObserver.current.observe(placeholderEl);
1544
+ }
1545
+
1546
+ if (!placeholderEl) {
1547
+ // No placeholder element, so no need for a resize observer.
1548
+ var styleElement = EDITOR_TO_STYLE_ELEMENT.get(editor);
1549
+
1550
+ if (styleElement) {
1551
+ // No min-height if there is no placeholder.
1552
+ styleElement.innerHTML = '';
1553
+ }
1513
1554
  }
1514
1555
 
1515
- editorEl.style.minHeight = "".concat(placeholderEl.clientHeight, "px");
1516
- EDITOR_TO_PLACEHOLDER_ELEMENT.set(editor, placeholderEl);
1517
1556
  return function () {
1518
- editorEl.style.minHeight = 'auto';
1519
1557
  EDITOR_TO_PLACEHOLDER_ELEMENT["delete"](editor);
1520
1558
  };
1521
1559
  }, [placeholderRef, leaf]);
@@ -3525,12 +3563,14 @@ function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) {
3525
3563
 
3526
3564
  var Children = function Children(props) {
3527
3565
  return /*#__PURE__*/React__default['default'].createElement(React__default['default'].Fragment, null, useChildren(props));
3528
- };
3566
+ }; // The number of Editable components currently mounted.
3567
+
3568
+
3569
+ var mountedCount = 0;
3529
3570
  /**
3530
3571
  * Editable.
3531
3572
  */
3532
3573
 
3533
-
3534
3574
  var Editable = function Editable(props) {
3535
3575
  var autoFocus = props.autoFocus,
3536
3576
  _props$decorate = props.decorate,
@@ -4154,6 +4194,38 @@ var Editable = function Editable(props) {
4154
4194
  EDITOR_TO_PENDING_INSERTION_MARKS["delete"](editor);
4155
4195
  });
4156
4196
  });
4197
+ React.useEffect(function () {
4198
+ mountedCount++;
4199
+
4200
+ if (mountedCount === 1) {
4201
+ // Set global default styles for editors.
4202
+ var defaultStylesElement = document.createElement('style');
4203
+ defaultStylesElement.setAttribute('data-slate-default-styles', 'true');
4204
+ defaultStylesElement.innerHTML = // :where is used to give these rules lower specificity so user stylesheets can override them.
4205
+ ":where([data-slate-editor]) {" + // Allow positioning relative to the editable element.
4206
+ "position: relative;" + // Prevent the default outline styles.
4207
+ "outline: none;" + // Preserve adjacent whitespace and new lines.
4208
+ "white-space: pre-wrap;" + // Allow words to break if they are too long.
4209
+ "word-wrap: break-word;" + "}";
4210
+ document.head.appendChild(defaultStylesElement);
4211
+ }
4212
+
4213
+ return function () {
4214
+ var _document$querySelect;
4215
+
4216
+ mountedCount--;
4217
+ if (mountedCount <= 0) (_document$querySelect = document.querySelector('style[data-slate-default-styles]')) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect.remove();
4218
+ };
4219
+ }, []);
4220
+ React.useEffect(function () {
4221
+ var styleElement = document.createElement('style');
4222
+ document.head.appendChild(styleElement);
4223
+ EDITOR_TO_STYLE_ELEMENT.set(editor, styleElement);
4224
+ return function () {
4225
+ styleElement.remove();
4226
+ EDITOR_TO_STYLE_ELEMENT["delete"](editor);
4227
+ };
4228
+ }, []);
4157
4229
  return /*#__PURE__*/React__default['default'].createElement(ReadOnlyContext.Provider, {
4158
4230
  value: readOnly
4159
4231
  }, /*#__PURE__*/React__default['default'].createElement(DecorateContext.Provider, {
@@ -4174,6 +4246,7 @@ var Editable = function Editable(props) {
4174
4246
  autoCorrect: HAS_BEFORE_INPUT_SUPPORT || !CAN_USE_DOM ? attributes.autoCorrect : 'false',
4175
4247
  autoCapitalize: HAS_BEFORE_INPUT_SUPPORT || !CAN_USE_DOM ? attributes.autoCapitalize : 'false',
4176
4248
  "data-slate-editor": true,
4249
+ "data-slate-editor-id": editor.id,
4177
4250
  "data-slate-node": "value",
4178
4251
  // explicitly set this
4179
4252
  contentEditable: !readOnly,
@@ -4183,16 +4256,7 @@ var Editable = function Editable(props) {
4183
4256
  zindex: -1,
4184
4257
  suppressContentEditableWarning: true,
4185
4258
  ref: ref,
4186
- style: _objectSpread$1({
4187
- // Allow positioning relative to the editable element.
4188
- position: 'relative',
4189
- // Prevent the default outline styles.
4190
- outline: 'none',
4191
- // Preserve adjacent whitespace and new lines.
4192
- whiteSpace: 'pre-wrap',
4193
- // Allow words to break if they are too long.
4194
- wordWrap: 'break-word'
4195
- }, style),
4259
+ style: style,
4196
4260
  onBeforeInput: React.useCallback(function (event) {
4197
4261
  // COMPAT: Certain browsers don't support the `beforeinput` event, so we
4198
4262
  // fall back to React's leaky polyfill instead just for it. It