@portabletext/editor 1.36.6 → 1.37.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.
Files changed (38) hide show
  1. package/lib/_chunks-cjs/behavior.markdown.cjs +1 -1
  2. package/lib/_chunks-cjs/editor-provider.cjs +63 -4
  3. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  4. package/lib/_chunks-es/behavior.markdown.js +1 -1
  5. package/lib/_chunks-es/editor-provider.js +67 -8
  6. package/lib/_chunks-es/editor-provider.js.map +1 -1
  7. package/lib/behaviors/index.d.cts +1 -0
  8. package/lib/behaviors/index.d.ts +1 -0
  9. package/lib/index.d.cts +1 -0
  10. package/lib/index.d.ts +1 -0
  11. package/lib/plugins/index.cjs +1 -1
  12. package/lib/plugins/index.d.cts +1 -0
  13. package/lib/plugins/index.d.ts +1 -0
  14. package/lib/plugins/index.js +1 -1
  15. package/lib/selectors/index.cjs +69 -14
  16. package/lib/selectors/index.cjs.map +1 -1
  17. package/lib/selectors/index.d.cts +17 -0
  18. package/lib/selectors/index.d.ts +17 -0
  19. package/lib/selectors/index.js +63 -8
  20. package/lib/selectors/index.js.map +1 -1
  21. package/lib/utils/index.d.cts +1 -0
  22. package/lib/utils/index.d.ts +1 -0
  23. package/package.json +7 -7
  24. package/src/behavior-actions/behavior.action.decorator.add.ts +1 -0
  25. package/src/behavior-actions/behavior.action.delete.text.ts +1 -0
  26. package/src/behavior-actions/behavior.action.delete.ts +1 -3
  27. package/src/behavior-actions/behavior.action.insert-blocks.ts +66 -1
  28. package/src/editor/editor-machine.ts +16 -3
  29. package/src/editor/editor-selector.ts +1 -0
  30. package/src/editor/editor-snapshot.ts +4 -0
  31. package/src/internal-utils/create-test-snapshot.ts +1 -0
  32. package/src/selectors/index.ts +2 -0
  33. package/src/selectors/selector.get-focus-inline-object.ts +21 -0
  34. package/src/selectors/selector.is-overlapping-selection.test.ts +171 -0
  35. package/src/selectors/selector.is-overlapping-selection.ts +108 -4
  36. package/src/selectors/selector.is-point-after-selection.ts +3 -1
  37. package/src/selectors/selector.is-point-before-selection.ts +3 -1
  38. package/src/selectors/selector.is-selecting-entire-blocks.ts +34 -0
@@ -1,6 +1,6 @@
1
1
  import { isPortableTextTextBlock } from "@sanity/types";
2
- import { isSelectionCollapsed, getFocusTextBlock, getFocusSpan, getPreviousInlineObject, getFocusBlock } from "./selector.is-at-the-start-of-block.js";
3
2
  import { spanSelectionPointToBlockOffset, getTextBlockText } from "./util.slice-blocks.js";
3
+ import { isSelectionCollapsed, getFocusTextBlock, getFocusSpan, getPreviousInlineObject, getFocusBlock } from "./selector.is-at-the-start-of-block.js";
4
4
  import { getBlockTextBefore } from "./selector.get-text-before.js";
5
5
  import { defineBehavior } from "./behavior.core.js";
6
6
  function createMarkdownBehaviors(config) {
@@ -5,7 +5,7 @@ import { ReactEditor, withReact, Slate } from "slate-react";
5
5
  import { useSelector, useActorRef } from "@xstate/react";
6
6
  import debug$e from "debug";
7
7
  import isEqual from "lodash/isEqual.js";
8
- import { Editor, Element, Range, Point, Text, Operation, Transforms, Path, Node, insertText, select, deleteFragment, deleteForward, createEditor as createEditor$1 } from "slate";
8
+ import { Editor, Element, Range, Point, Text, Operation, Transforms, Path, Node, insertText, deleteForward, createEditor as createEditor$1 } from "slate";
9
9
  import { setup, emit, enqueueActions, assign, assertEvent, fromCallback, stateIn, and, not, createActor } from "xstate";
10
10
  import { unset, set, setIfMissing, insert, diffMatchPatch as diffMatchPatch$1, applyAll } from "@portabletext/patches";
11
11
  import { defineType, defineField, isKeySegment, isPortableTextTextBlock, isPortableTextSpan as isPortableTextSpan$1, isPortableTextListBlock } from "@sanity/types";
@@ -3065,6 +3065,7 @@ const decoratorAddActionImplementation = ({
3065
3065
  activeDecorators: [],
3066
3066
  converters: [],
3067
3067
  keyGenerator: context.keyGenerator,
3068
+ readOnly: !1,
3068
3069
  schema: context.schema,
3069
3070
  selection: newSelection,
3070
3071
  value: newValue
@@ -4259,7 +4260,9 @@ const blockSetBehaviorActionImplementation = ({
4259
4260
  const range = toSlateRange(action.selection, action.editor);
4260
4261
  if (!range)
4261
4262
  throw new Error(`Failed to get Slate Range for selection ${JSON.stringify(action.selection)}`);
4262
- select(action.editor, range), deleteFragment(action.editor);
4263
+ action.editor.delete({
4264
+ at: range
4265
+ });
4263
4266
  }, deleteTextActionImplementation = ({
4264
4267
  context,
4265
4268
  action
@@ -4282,6 +4285,7 @@ const blockSetBehaviorActionImplementation = ({
4282
4285
  schema: context.schema,
4283
4286
  keyGenerator: context.keyGenerator,
4284
4287
  activeDecorators: [],
4288
+ readOnly: !1,
4285
4289
  value,
4286
4290
  selection
4287
4291
  }
@@ -4347,6 +4351,44 @@ const blockSetBehaviorActionImplementation = ({
4347
4351
  const nextPath = [focusPath[0] + 1];
4348
4352
  Transforms.insertNodes(action.editor, fragment, {
4349
4353
  at: nextPath
4354
+ });
4355
+ const [nextBlock, nextBlockPath] = Editor.node(action.editor, Path.next(focusPath), {
4356
+ depth: 1
4357
+ }), nextChild = Node.child(nextBlock, 0);
4358
+ !action.editor.isTextSpan(nextChild) && Transforms.insertNodes(action.editor, {
4359
+ _key: context.keyGenerator(),
4360
+ _type: "span",
4361
+ text: "",
4362
+ marks: []
4363
+ }, {
4364
+ at: [nextBlockPath[0], 0]
4365
+ }), Transforms.select(action.editor, {
4366
+ anchor: {
4367
+ path: [nextPath[0], 0],
4368
+ offset: 0
4369
+ },
4370
+ focus: {
4371
+ path: [nextPath[0], 0],
4372
+ offset: 0
4373
+ }
4374
+ });
4375
+ return;
4376
+ }
4377
+ if (!action.editor.isTextBlock(focusBlock)) {
4378
+ const nextPath = [focusPath[0] + 1];
4379
+ Transforms.insertNodes(action.editor, fragment, {
4380
+ at: nextPath
4381
+ });
4382
+ const [nextBlock, nextBlockPath] = Editor.node(action.editor, Path.next(focusPath), {
4383
+ depth: 1
4384
+ }), nextChild = Node.child(nextBlock, 0);
4385
+ !action.editor.isTextSpan(nextChild) && Transforms.insertNodes(action.editor, {
4386
+ _key: context.keyGenerator(),
4387
+ _type: "span",
4388
+ text: "",
4389
+ marks: []
4390
+ }, {
4391
+ at: [nextBlockPath[0], 0]
4350
4392
  }), Transforms.select(action.editor, {
4351
4393
  anchor: {
4352
4394
  path: [nextPath[0], 0],
@@ -5053,7 +5095,7 @@ function createWithEventListeners(editorActor, subscriptions) {
5053
5095
  insertBreak,
5054
5096
  insertData,
5055
5097
  insertText: insertText2,
5056
- select: select2,
5098
+ select,
5057
5099
  setFragmentData
5058
5100
  } = editor;
5059
5101
  return editor.deleteBackward = (unit) => {
@@ -5168,7 +5210,7 @@ function createWithEventListeners(editorActor, subscriptions) {
5168
5210
  });
5169
5211
  }, editor.select = (location) => {
5170
5212
  if (isApplyingBehaviorActions(editor)) {
5171
- select2(location);
5213
+ select(location);
5172
5214
  return;
5173
5215
  }
5174
5216
  const range = Editor.range(editor, location);
@@ -5180,7 +5222,7 @@ function createWithEventListeners(editorActor, subscriptions) {
5180
5222
  },
5181
5223
  editor,
5182
5224
  defaultActionCallback: () => {
5183
- select2(location);
5225
+ select(location);
5184
5226
  }
5185
5227
  });
5186
5228
  }, editor.setFragmentData = (dataTransfer, originEvent) => {
@@ -6082,6 +6124,7 @@ function createEditorSnapshot({
6082
6124
  converters,
6083
6125
  editor,
6084
6126
  keyGenerator,
6127
+ readOnly,
6085
6128
  schema,
6086
6129
  hasTag
6087
6130
  }) {
@@ -6094,6 +6137,7 @@ function createEditorSnapshot({
6094
6137
  }),
6095
6138
  converters,
6096
6139
  keyGenerator,
6140
+ readOnly,
6097
6141
  schema,
6098
6142
  selection,
6099
6143
  value
@@ -6201,7 +6245,10 @@ const editorMachine = setup({
6201
6245
  withApplyingBehaviorActions(event.editor, () => {
6202
6246
  try {
6203
6247
  performAction({
6204
- context,
6248
+ context: {
6249
+ keyGenerator: context.keyGenerator,
6250
+ schema: context.schema
6251
+ },
6205
6252
  action: defaultAction
6206
6253
  });
6207
6254
  } catch (error) {
@@ -6214,6 +6261,9 @@ const editorMachine = setup({
6214
6261
  converters: [...context.converters],
6215
6262
  editor: event.editor,
6216
6263
  keyGenerator: context.keyGenerator,
6264
+ readOnly: self.getSnapshot().matches({
6265
+ "edit mode": "read only"
6266
+ }),
6217
6267
  schema: context.schema,
6218
6268
  hasTag: (tag) => self.getSnapshot().hasTag(tag)
6219
6269
  });
@@ -6252,7 +6302,10 @@ const editorMachine = setup({
6252
6302
  };
6253
6303
  try {
6254
6304
  performAction({
6255
- context,
6305
+ context: {
6306
+ keyGenerator: context.keyGenerator,
6307
+ schema: context.schema
6308
+ },
6256
6309
  action: internalAction
6257
6310
  });
6258
6311
  } catch (error) {
@@ -6282,7 +6335,10 @@ const editorMachine = setup({
6282
6335
  withApplyingBehaviorActions(event.editor, () => {
6283
6336
  try {
6284
6337
  performAction({
6285
- context,
6338
+ context: {
6339
+ keyGenerator: context.keyGenerator,
6340
+ schema: context.schema
6341
+ },
6286
6342
  action: defaultAction
6287
6343
  });
6288
6344
  } catch (error) {
@@ -6673,6 +6729,9 @@ function getEditorSnapshot({
6673
6729
  slateEditorInstance
6674
6730
  }),
6675
6731
  keyGenerator: editorActorSnapshot.context.keyGenerator,
6732
+ readOnly: editorActorSnapshot.matches({
6733
+ "edit mode": "read only"
6734
+ }),
6676
6735
  schema: editorActorSnapshot.context.schema,
6677
6736
  selection: editorActorSnapshot.context.selection,
6678
6737
  value: getValue({