@portabletext/editor 2.12.1 → 2.12.3

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.
@@ -1,5 +1,5 @@
1
1
  import { Behavior, Editor, EditorEmittedEvent, EditorSchema } from "../_chunks-dts/behavior.types.action.cjs";
2
- import * as react22 from "react";
2
+ import * as react12 from "react";
3
3
  import React from "react";
4
4
  /**
5
5
  * @beta
@@ -181,7 +181,7 @@ type MarkdownPluginConfig = MarkdownBehaviorsConfig & {
181
181
  */
182
182
  declare function MarkdownPlugin(props: {
183
183
  config: MarkdownPluginConfig;
184
- }): react22.JSX.Element;
184
+ }): react12.JSX.Element;
185
185
  /**
186
186
  * @beta
187
187
  * Restrict the editor to one line. The plugin takes care of blocking
@@ -192,5 +192,5 @@ declare function MarkdownPlugin(props: {
192
192
  *
193
193
  * @deprecated Install the plugin from `@portabletext/plugin-one-line`
194
194
  */
195
- declare function OneLinePlugin(): react22.JSX.Element;
195
+ declare function OneLinePlugin(): react12.JSX.Element;
196
196
  export { BehaviorPlugin, DecoratorShortcutPlugin, EditorRefPlugin, EventListenerPlugin, MarkdownPlugin, type MarkdownPluginConfig, OneLinePlugin };
@@ -1,5 +1,5 @@
1
1
  import { BlockOffset, BlockPath, ChildPath, EditorContext, EditorSelection, EditorSelectionPoint } from "../_chunks-dts/behavior.types.action.cjs";
2
- import * as _sanity_types8 from "@sanity/types";
2
+ import * as _sanity_types9 from "@sanity/types";
3
3
  import { KeyedSegment, PortableTextBlock, PortableTextTextBlock } from "@sanity/types";
4
4
  import { isSpan, isTextBlock } from "@portabletext/schema";
5
5
  /**
@@ -143,7 +143,7 @@ declare function mergeTextBlocks({
143
143
  context: Pick<EditorContext, 'keyGenerator' | 'schema'>;
144
144
  targetBlock: PortableTextTextBlock;
145
145
  incomingBlock: PortableTextTextBlock;
146
- }): PortableTextTextBlock<_sanity_types8.PortableTextObject | _sanity_types8.PortableTextSpan>;
146
+ }): PortableTextTextBlock<_sanity_types9.PortableTextObject | _sanity_types9.PortableTextSpan>;
147
147
  /**
148
148
  * @public
149
149
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "2.12.1",
3
+ "version": "2.12.3",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -84,11 +84,11 @@
84
84
  "slate": "0.118.1",
85
85
  "slate-dom": "^0.118.1",
86
86
  "slate-react": "0.117.4",
87
- "xstate": "^5.21.0",
88
- "@portabletext/block-tools": "^3.5.6",
87
+ "xstate": "^5.22.0",
88
+ "@portabletext/schema": "^1.2.0",
89
+ "@portabletext/block-tools": "^3.5.7",
89
90
  "@portabletext/keyboard-shortcuts": "^1.1.1",
90
- "@portabletext/patches": "^1.1.8",
91
- "@portabletext/schema": "^1.2.0"
91
+ "@portabletext/patches": "^1.1.8"
92
92
  },
93
93
  "devDependencies": {
94
94
  "@sanity/diff-match-patch": "^3.2.0",
@@ -116,12 +116,12 @@
116
116
  "vite": "^7.1.3",
117
117
  "vitest": "^3.2.4",
118
118
  "vitest-browser-react": "^1.0.1",
119
- "@portabletext/sanity-bridge": "1.1.10",
119
+ "@portabletext/sanity-bridge": "1.1.11",
120
120
  "@portabletext/test": "^0.0.0",
121
- "racejar": "1.2.15"
121
+ "racejar": "1.3.0"
122
122
  },
123
123
  "peerDependencies": {
124
- "@portabletext/sanity-bridge": "^1.1.10",
124
+ "@portabletext/sanity-bridge": "^1.1.11",
125
125
  "@sanity/schema": "^4.9.0",
126
126
  "@sanity/types": "^4.9.0",
127
127
  "react": "^18.3 || ^19",
@@ -6,6 +6,7 @@ import {
6
6
  getFocusInlineObject,
7
7
  getPreviousBlock,
8
8
  isSelectionCollapsed,
9
+ isSelectionExpanded,
9
10
  } from '../selectors'
10
11
  import {getBlockEndPoint, isEmptyTextBlock} from '../utils'
11
12
  import {raise} from './behavior.types.action'
@@ -37,6 +38,18 @@ export const abstractKeyboardBehaviors = [
37
38
  actions: [() => [raise({type: 'insert.break'})]],
38
39
  }),
39
40
 
41
+ /**
42
+ * On Firefox, Enter might collapse the selection. To mitigate this, we
43
+ * `raise` an `insert.break` event manually.
44
+ */
45
+ defineBehavior({
46
+ on: 'keyboard.keydown',
47
+ guard: ({snapshot, event}) =>
48
+ defaultKeyboardShortcuts.break.guard(event.originEvent) &&
49
+ isSelectionExpanded(snapshot),
50
+ actions: [() => [raise({type: 'insert.break'})]],
51
+ }),
52
+
40
53
  /**
41
54
  * On WebKit, Shift+Enter results in an `insertParagraph` input event rather
42
55
  * than an `insertLineBreak` input event. This Behavior makes sure we catch
@@ -172,7 +172,11 @@ export function performEvent({
172
172
  nativeEventPrevented = true
173
173
  }
174
174
 
175
+ let actionSetIndex = -1
176
+
175
177
  for (const actionSet of eventBehavior.actions) {
178
+ actionSetIndex++
179
+
176
180
  const actionsSnapshot = getSnapshot()
177
181
 
178
182
  let actions: Array<BehaviorAction> = []
@@ -205,7 +209,17 @@ export function performEvent({
205
209
 
206
210
  let undoStepCreated = false
207
211
 
208
- if (actions.some((action) => action.type === 'execute')) {
212
+ if (actionSetIndex > 0) {
213
+ // Since there are multiple action sets
214
+ createUndoStep(editor)
215
+
216
+ undoStepCreated = true
217
+ }
218
+
219
+ if (
220
+ !undoStepCreated &&
221
+ actions.some((action) => action.type === 'execute')
222
+ ) {
209
223
  // Since at least one action is about to `execute` changes in the editor,
210
224
  // we set up a new undo step.
211
225
  // All actions performed recursively from now will be squashed into this
@@ -942,28 +942,8 @@ export const PortableTextEditable = forwardRef<
942
942
  } else if (forwardedRef) {
943
943
  forwardedRef.current = node
944
944
  }
945
-
946
- if (node) {
947
- // Observe mutations (child list and subtree) to this component's DOM,
948
- // and make sure the editor selection is valid when that happens.
949
- const mutationObserver = new MutationObserver(() => {
950
- validateSelection(slateEditor, node)
951
- })
952
-
953
- mutationObserver.observe(node, {
954
- attributeOldValue: false,
955
- attributes: false,
956
- characterData: false,
957
- childList: true,
958
- subtree: true,
959
- })
960
-
961
- return () => {
962
- mutationObserver.disconnect()
963
- }
964
- }
965
945
  },
966
- [forwardedRef, slateEditor],
946
+ [forwardedRef],
967
947
  )
968
948
 
969
949
  if (!portableTextEditor) {
@@ -1007,72 +987,3 @@ export const PortableTextEditable = forwardRef<
1007
987
  })
1008
988
 
1009
989
  PortableTextEditable.displayName = 'ForwardRef(PortableTextEditable)'
1010
-
1011
- // This function will handle unexpected DOM changes inside the Editable rendering,
1012
- // and make sure that we can maintain a stable slateEditor.selection when that happens.
1013
- //
1014
- // For example, if this Editable is rendered inside something that might re-render
1015
- // this component (hidden contexts) while the user is still actively changing the
1016
- // contentEditable, this could interfere with the intermediate DOM selection,
1017
- // which again could be picked up by ReactEditor's event listeners.
1018
- // If that range is invalid at that point, the slate.editorSelection could be
1019
- // set either wrong, or invalid, to which slateEditor will throw exceptions
1020
- // that are impossible to recover properly from or result in a wrong selection.
1021
- //
1022
- // Also the other way around, when the ReactEditor will try to create a DOM Range
1023
- // from the current slateEditor.selection, it may throw unrecoverable errors
1024
- // if the current editor.selection is invalid according to the DOM.
1025
- // If this is the case, default to selecting the top of the document, if the
1026
- // user already had a selection.
1027
- function validateSelection(slateEditor: Editor, activeElement: HTMLDivElement) {
1028
- if (!slateEditor.selection) {
1029
- return
1030
- }
1031
-
1032
- let root: Document | ShadowRoot | undefined
1033
-
1034
- try {
1035
- root = ReactEditor.findDocumentOrShadowRoot(slateEditor)
1036
- } catch {}
1037
-
1038
- if (!root) {
1039
- // The editor has most likely been unmounted
1040
- return
1041
- }
1042
-
1043
- // Return if the editor isn't the active element
1044
- if (activeElement !== root.activeElement) {
1045
- return
1046
- }
1047
- const window = ReactEditor.getWindow(slateEditor)
1048
- const domSelection = window.getSelection()
1049
- if (!domSelection || domSelection.rangeCount === 0) {
1050
- return
1051
- }
1052
- const existingDOMRange = domSelection.getRangeAt(0)
1053
- try {
1054
- const newDOMRange = ReactEditor.toDOMRange(
1055
- slateEditor,
1056
- slateEditor.selection,
1057
- )
1058
- if (
1059
- newDOMRange.startOffset !== existingDOMRange.startOffset ||
1060
- newDOMRange.endOffset !== existingDOMRange.endOffset
1061
- ) {
1062
- debug('DOM range out of sync, validating selection')
1063
- // Remove all ranges temporary
1064
- domSelection?.removeAllRanges()
1065
- // Set the correct range
1066
- domSelection.addRange(newDOMRange)
1067
- }
1068
- } catch {
1069
- debug(`Could not resolve selection, selecting top document`)
1070
- // Deselect the editor
1071
- Transforms.deselect(slateEditor)
1072
- // Select top document if there is a top block to select
1073
- if (slateEditor.children.length > 0) {
1074
- Transforms.select(slateEditor, [0, 0])
1075
- }
1076
- slateEditor.onChange()
1077
- }
1078
- }
@@ -0,0 +1,76 @@
1
+ import {compileSchema, defineSchema} from '@portabletext/schema'
2
+ import {createTestKeyGenerator} from '@portabletext/test'
3
+ import {describe, expect, test} from 'vitest'
4
+ import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
5
+ import {getMarkState} from './selector.get-mark-state'
6
+
7
+ describe(getMarkState.name, () => {
8
+ test('Scenario: Caret after annotated decorator', () => {
9
+ const keyGenerator = createTestKeyGenerator()
10
+ const blockKey = keyGenerator()
11
+ const fooSpanKey = keyGenerator()
12
+ const barSpanKey = keyGenerator()
13
+ const bazSpanKey = keyGenerator()
14
+ const linkKey = keyGenerator()
15
+ const snapshot = createTestSnapshot({
16
+ context: {
17
+ keyGenerator,
18
+ value: [
19
+ {
20
+ _type: 'block',
21
+ _key: blockKey,
22
+ children: [
23
+ {
24
+ _type: 'span',
25
+ _key: fooSpanKey,
26
+ text: 'foo ',
27
+ },
28
+ {
29
+ _type: 'span',
30
+ _key: barSpanKey,
31
+ text: 'bar',
32
+ marks: [linkKey, 'strong'],
33
+ },
34
+ {
35
+ _type: 'span',
36
+ _key: bazSpanKey,
37
+ text: ' baz',
38
+ },
39
+ ],
40
+ markDefs: [
41
+ {
42
+ _type: 'link',
43
+ _key: linkKey,
44
+ href: 'https://portabletext.org',
45
+ },
46
+ ],
47
+ style: 'normal',
48
+ },
49
+ ],
50
+ selection: {
51
+ anchor: {
52
+ path: [{_key: blockKey}, 'children', {_key: bazSpanKey}],
53
+ offset: 0,
54
+ },
55
+ focus: {
56
+ path: [{_key: blockKey}, 'children', {_key: bazSpanKey}],
57
+ offset: 0,
58
+ },
59
+ backward: false,
60
+ },
61
+ schema: compileSchema(
62
+ defineSchema({
63
+ annotations: [{name: 'link'}],
64
+ decorators: [{name: 'strong'}],
65
+ }),
66
+ ),
67
+ },
68
+ })
69
+
70
+ expect(getMarkState(snapshot)).toEqual({
71
+ state: 'changed',
72
+ marks: [],
73
+ previousMarks: [linkKey, 'strong'],
74
+ })
75
+ })
76
+ })
@@ -164,7 +164,7 @@ export const getMarkState: EditorSelector<MarkState | undefined> = (
164
164
  if (previousSpanHasAnnotations) {
165
165
  return {
166
166
  state: 'changed',
167
- previousMarks: marks,
167
+ previousMarks: previousSpan.node.marks ?? [],
168
168
  marks: [],
169
169
  }
170
170
  } else {
@@ -70,7 +70,7 @@ const parameterType = {
70
70
  }),
71
71
  tersePt: createParameterType<Array<string>>({
72
72
  name: 'terse-pt',
73
- matcher: /"([a-z-,#>:\\n \d|{}'"‘’“”?]*)"/u,
73
+ matcher: /"([a-z-,#>:\\n \d|{}'"‘’“”?—]*)"/u,
74
74
  type: Array,
75
75
  transform: parseTersePtString,
76
76
  }),
@@ -61,14 +61,7 @@ export const stepDefinitions = [
61
61
  }),
62
62
 
63
63
  Given('the editor is focused', async (context: Context) => {
64
- context.editor.send({
65
- type: 'focus',
66
- })
67
-
68
- await vi.waitFor(() => {
69
- const selection = context.editor.getSnapshot().context.selection
70
- expect(selection).not.toBeNull()
71
- })
64
+ await userEvent.click(context.locator)
72
65
  }),
73
66
 
74
67
  Given(
@@ -181,6 +174,9 @@ export const stepDefinitions = [
181
174
  When('{string} is typed', async (context: Context, text: string) => {
182
175
  await userEvent.type(context.locator, text)
183
176
  }),
177
+ When('{string} is inserted', (context: Context, text: string) => {
178
+ context.editor.send({type: 'insert.text', text})
179
+ }),
184
180
  Then(
185
181
  '{terse-pt} is in block {key}',
186
182
  (context: Context, text: Array<string>, key: string) => {