@portabletext/editor 1.6.0 → 1.6.1

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/lib/index.mjs CHANGED
@@ -6236,6 +6236,14 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
6236
6236
  return useMemo(() => /* @__PURE__ */ jsx("span", { ...attributes, ref: spanRef, children: content }, leaf._key), [leaf, attributes, content]);
6237
6237
  };
6238
6238
  Leaf.displayName = "Leaf";
6239
+ function withSyncRangeDecorations(slateEditor, syncRangeDecorations) {
6240
+ const originalApply = slateEditor.apply;
6241
+ return slateEditor.apply = (op) => {
6242
+ originalApply(op), op.type !== "set_selection" && syncRangeDecorations(op);
6243
+ }, () => {
6244
+ slateEditor.apply = originalApply;
6245
+ };
6246
+ }
6239
6247
  const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6240
6248
  position: "absolute",
6241
6249
  userSelect: "none",
@@ -6267,8 +6275,14 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6267
6275
  useImperativeHandle(forwardedRef, () => ref.current);
6268
6276
  const rangeDecorationsRef = useRef(rangeDecorations), editorActor = useContext(EditorActorContext), {
6269
6277
  schemaTypes
6270
- } = portableTextEditor, slateEditor = useSlate(), blockTypeName = schemaTypes.block.name, withInsertData = useMemo(() => createWithInsertData(editorActor, schemaTypes), [editorActor, schemaTypes]), withHotKeys = useMemo(() => createWithHotkeys(editorActor, portableTextEditor, hotkeys), [editorActor, hotkeys, portableTextEditor]);
6271
- useMemo(() => readOnly ? (debug("Editable is in read only mode"), withInsertData(slateEditor)) : (debug("Editable is in edit mode"), withInsertData(withHotKeys(slateEditor))), [readOnly, slateEditor, withHotKeys, withInsertData]);
6278
+ } = portableTextEditor, slateEditor = useSlate(), blockTypeName = schemaTypes.block.name;
6279
+ useMemo(() => {
6280
+ const withInsertData = createWithInsertData(editorActor, schemaTypes);
6281
+ if (readOnly)
6282
+ return debug("Editable is in read only mode"), withInsertData(slateEditor);
6283
+ const withHotKeys = createWithHotkeys(editorActor, portableTextEditor, hotkeys);
6284
+ return debug("Editable is in edit mode"), withInsertData(withHotKeys(slateEditor));
6285
+ }, [editorActor, hotkeys, portableTextEditor, readOnly, schemaTypes, slateEditor]);
6272
6286
  const renderElement = useCallback((eProps) => /* @__PURE__ */ jsx(Element, { ...eProps, readOnly, renderBlock, renderChild, renderListItem, renderStyle, schemaTypes, spellCheck }), [schemaTypes, spellCheck, readOnly, renderBlock, renderChild, renderListItem, renderStyle]), renderLeaf = useCallback((lProps) => {
6273
6287
  if (lProps.leaf._type === "span") {
6274
6288
  let rendered = /* @__PURE__ */ jsx(Leaf, { ...lProps, editorActor, schemaTypes, renderAnnotation, renderChild, renderDecorator, readOnly });
@@ -6343,16 +6357,15 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6343
6357
  }, [editorActor, restoreSelectionFromProps]), useEffect(() => {
6344
6358
  propsSelection && !hasInvalidValue && restoreSelectionFromProps();
6345
6359
  }, [hasInvalidValue, propsSelection, restoreSelectionFromProps]);
6346
- const originalApply = useMemo(() => slateEditor.apply, [slateEditor]), [syncedRangeDecorations, setSyncedRangeDecorations] = useState(!1);
6360
+ const [syncedRangeDecorations, setSyncedRangeDecorations] = useState(!1);
6347
6361
  useEffect(() => {
6348
6362
  syncedRangeDecorations || (setSyncedRangeDecorations(!0), syncRangeDecorations());
6349
6363
  }, [syncRangeDecorations, syncedRangeDecorations]), useEffect(() => {
6350
6364
  isEqual(rangeDecorations, rangeDecorationsRef.current) || syncRangeDecorations(), rangeDecorationsRef.current = rangeDecorations;
6351
- }, [rangeDecorations, syncRangeDecorations]), useEffect(() => (slateEditor.apply = (op) => {
6352
- originalApply(op), op.type !== "set_selection" && syncRangeDecorations(op);
6353
- }, () => {
6354
- slateEditor.apply = originalApply;
6355
- }), [originalApply, slateEditor, syncRangeDecorations]);
6365
+ }, [rangeDecorations, syncRangeDecorations]), useEffect(() => {
6366
+ const teardown = withSyncRangeDecorations(slateEditor, syncRangeDecorations);
6367
+ return () => teardown();
6368
+ }, [slateEditor, syncRangeDecorations]);
6356
6369
  const handleCopy = useCallback((event) => {
6357
6370
  onCopy && onCopy(event) !== void 0 && event.preventDefault();
6358
6371
  }, [onCopy]), handlePaste = useCallback((event_0) => {