@portabletext/editor 1.15.1 → 1.15.2

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 (60) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +494 -0
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -0
  3. package/lib/_chunks-cjs/selector.get-text-before.cjs +0 -231
  4. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
  5. package/lib/_chunks-cjs/selectors.cjs +234 -0
  6. package/lib/_chunks-cjs/selectors.cjs.map +1 -0
  7. package/lib/_chunks-es/behavior.core.js +496 -0
  8. package/lib/_chunks-es/behavior.core.js.map +1 -0
  9. package/lib/_chunks-es/selector.get-text-before.js +2 -233
  10. package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
  11. package/lib/_chunks-es/selectors.js +235 -0
  12. package/lib/_chunks-es/selectors.js.map +1 -0
  13. package/lib/behaviors/index.cjs +460 -0
  14. package/lib/behaviors/index.cjs.map +1 -0
  15. package/lib/behaviors/index.d.cts +875 -0
  16. package/lib/behaviors/index.d.ts +875 -0
  17. package/lib/behaviors/index.js +464 -0
  18. package/lib/behaviors/index.js.map +1 -0
  19. package/lib/index.cjs +38 -1008
  20. package/lib/index.cjs.map +1 -1
  21. package/lib/index.d.cts +17 -536
  22. package/lib/index.d.ts +17 -536
  23. package/lib/index.js +31 -1000
  24. package/lib/index.js.map +1 -1
  25. package/lib/selectors/index.cjs +16 -16
  26. package/lib/selectors/index.cjs.map +1 -1
  27. package/lib/selectors/index.js +3 -2
  28. package/lib/selectors/index.js.map +1 -1
  29. package/package.json +10 -4
  30. package/src/{editor/behavior → behavior-actions}/behavior.action-utils.insert-block.ts +2 -2
  31. package/src/{editor/behavior → behavior-actions}/behavior.action.insert-block-object.ts +1 -1
  32. package/src/{editor/behavior → behavior-actions}/behavior.action.insert-break.ts +1 -1
  33. package/src/{editor/behavior → behavior-actions}/behavior.action.insert-inline-object.ts +1 -1
  34. package/src/{editor/behavior → behavior-actions}/behavior.action.list-item.ts +2 -2
  35. package/src/{editor/behavior → behavior-actions}/behavior.action.text-block.set.ts +1 -1
  36. package/src/{editor/behavior → behavior-actions}/behavior.action.text-block.unset.ts +1 -1
  37. package/src/{editor/behavior → behavior-actions}/behavior.actions.ts +14 -14
  38. package/src/{editor/behavior → behavior-actions}/behavior.guards.ts +1 -1
  39. package/src/{editor/behavior → behaviors}/behavior.code-editor.ts +4 -4
  40. package/src/{editor/behavior → behaviors}/behavior.core.block-objects.ts +5 -5
  41. package/src/{editor/behavior → behaviors}/behavior.core.decorators.ts +6 -16
  42. package/src/{editor/behavior → behaviors}/behavior.core.lists.ts +15 -15
  43. package/src/{editor/behavior → behaviors}/behavior.links.ts +4 -4
  44. package/src/{editor/behavior → behaviors}/behavior.markdown.ts +15 -25
  45. package/src/{editor/behavior → behaviors}/behavior.types.ts +32 -39
  46. package/src/behaviors/index.ts +28 -0
  47. package/src/editor/create-editor.ts +26 -46
  48. package/src/editor/editor-machine.ts +14 -12
  49. package/src/editor/plugins/createWithEditableAPI.ts +3 -3
  50. package/src/editor/plugins/createWithPortableTextMarkModel.ts +1 -1
  51. package/src/editor/utils/utils.block-offset.ts +1 -1
  52. package/src/index.ts +11 -26
  53. package/src/selectors/selector.get-active-list-item.ts +1 -1
  54. package/src/selectors/selectors.ts +1 -1
  55. package/src/type-utils.ts +17 -0
  56. package/src/utils/__tests__/operationToPatches.test.ts +1 -1
  57. package/src/utils/__tests__/patchToOperations.test.ts +1 -1
  58. /package/src/{editor/behavior → behavior-actions}/behavior.action.insert-span.ts +0 -0
  59. /package/src/{editor/behavior → behavior-actions}/behavior.action.style.ts +0 -0
  60. /package/src/{editor/behavior → behaviors}/behavior.core.ts +0 -0
@@ -1,8 +1,9 @@
1
1
  import type {KeyedSegment, PortableTextTextBlock} from '@sanity/types'
2
2
  import type {TextUnit} from 'slate'
3
3
  import type {TextInsertTextOptions} from 'slate/dist/interfaces/transforms/text'
4
- import type {EditorSelection, PortableTextSlateEditor} from '../../types/editor'
5
- import type {EditorContext} from '../editor-snapshot'
4
+ import type {EditorContext} from '../editor/editor-snapshot'
5
+ import type {PickFromUnion} from '../type-utils'
6
+ import type {EditorSelection, PortableTextSlateEditor} from '../types/editor'
6
7
 
7
8
  /**
8
9
  * @alpha
@@ -116,20 +117,6 @@ export type NativeBehaviorEvent =
116
117
  data: DataTransfer
117
118
  }
118
119
 
119
- /**
120
- * @alpha
121
- */
122
- export type BehaviorGuard<
123
- TAnyBehaviorEvent extends BehaviorEvent,
124
- TGuardResponse,
125
- > = ({
126
- context,
127
- event,
128
- }: {
129
- context: EditorContext
130
- event: TAnyBehaviorEvent
131
- }) => TGuardResponse | false
132
-
133
120
  /**
134
121
  * @alpha
135
122
  */
@@ -238,32 +225,56 @@ export type BehaviorEvent = SyntheticBehaviorEvent | NativeBehaviorEvent
238
225
  * @alpha
239
226
  */
240
227
  export type Behavior<
241
- TAnyBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
228
+ TBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
242
229
  TGuardResponse = true,
243
230
  > = {
244
231
  /**
245
232
  * The internal editor event that triggers this behavior.
246
233
  */
247
- on: TAnyBehaviorEventType
234
+ on: TBehaviorEventType
248
235
  /**
249
236
  * Predicate function that determines if the behavior should be executed.
250
237
  * Returning a non-nullable value from the guard will pass the value to the
251
238
  * actions and execute them.
252
239
  */
253
240
  guard?: BehaviorGuard<
254
- PickFromUnion<BehaviorEvent, 'type', TAnyBehaviorEventType>,
241
+ PickFromUnion<BehaviorEvent, 'type', TBehaviorEventType>,
255
242
  TGuardResponse
256
243
  >
257
244
  /**
258
245
  * Array of behavior action sets.
259
246
  */
260
- actions: Array<BehaviorActionIntendSet<TGuardResponse>>
247
+ actions: Array<BehaviorActionIntendSet<TBehaviorEventType, TGuardResponse>>
261
248
  }
262
249
 
263
250
  /**
264
251
  * @alpha
265
252
  */
266
- export type BehaviorActionIntendSet<TGuardResponse = true> = (
253
+ export type BehaviorGuard<
254
+ TBehaviorEvent extends BehaviorEvent,
255
+ TGuardResponse,
256
+ > = ({
257
+ context,
258
+ event,
259
+ }: {
260
+ context: EditorContext
261
+ event: TBehaviorEvent
262
+ }) => TGuardResponse | false
263
+
264
+ /**
265
+ * @alpha
266
+ */
267
+ export type BehaviorActionIntendSet<
268
+ TBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
269
+ TGuardResponse = true,
270
+ > = (
271
+ {
272
+ context,
273
+ event,
274
+ }: {
275
+ context: EditorContext
276
+ event: PickFromUnion<BehaviorEvent, 'type', TBehaviorEventType>
277
+ },
267
278
  guardResponse: TGuardResponse,
268
279
  ) => Array<BehaviorActionIntend>
269
280
 
@@ -284,21 +295,3 @@ export type BlockOffset = {
284
295
  path: [KeyedSegment]
285
296
  offset: number
286
297
  }
287
-
288
- /**
289
- * @alpha
290
- */
291
- export type PickFromUnion<
292
- TUnion,
293
- TTagKey extends keyof TUnion,
294
- TPickedTags extends TUnion[TTagKey],
295
- > = TUnion extends Record<TTagKey, TPickedTags> ? TUnion : never
296
-
297
- /**
298
- * @alpha
299
- */
300
- export type OmitFromUnion<
301
- TUnion,
302
- TTagKey extends keyof TUnion,
303
- TOmittedTags extends TUnion[TTagKey],
304
- > = TUnion extends Record<TTagKey, TOmittedTags> ? never : TUnion
@@ -0,0 +1,28 @@
1
+ export type {EditorContext} from '../editor/editor-snapshot'
2
+ export type {PickFromUnion} from '../type-utils'
3
+ export type {
4
+ EditorSelection,
5
+ EditorSelectionPoint,
6
+ PortableTextMemberSchemaTypes,
7
+ } from '../types/editor'
8
+ export {
9
+ createCodeEditorBehaviors,
10
+ type CodeEditorBehaviorsConfig,
11
+ } from './behavior.code-editor'
12
+ export {coreBehavior, coreBehaviors} from './behavior.core'
13
+ export {createLinkBehaviors, type LinkBehaviorsConfig} from './behavior.links'
14
+ export {
15
+ createMarkdownBehaviors,
16
+ type MarkdownBehaviorsConfig,
17
+ } from './behavior.markdown'
18
+ export {
19
+ defineBehavior,
20
+ type Behavior,
21
+ type BehaviorActionIntend,
22
+ type BehaviorActionIntendSet,
23
+ type BehaviorEvent,
24
+ type BehaviorGuard,
25
+ type BlockOffset,
26
+ type NativeBehaviorEvent,
27
+ type SyntheticBehaviorEvent,
28
+ } from './behavior.types'
@@ -11,10 +11,11 @@ import {
11
11
  type EventObject,
12
12
  type Snapshot,
13
13
  } from 'xstate'
14
+ import type {Behavior} from '../behaviors/behavior.types'
15
+ import type {PickFromUnion} from '../type-utils'
14
16
  import type {EditableAPI} from '../types/editor'
15
17
  import {getPortableTextMemberSchemaTypes} from '../utils/getPortableTextMemberSchemaTypes'
16
18
  import {compileType} from '../utils/schema'
17
- import type {Behavior, PickFromUnion} from './behavior/behavior.types'
18
19
  import {createSlateEditor, type SlateEditor} from './create-slate-editor'
19
20
  import {compileSchemaDefinition, type SchemaDefinition} from './define-schema'
20
21
  import {
@@ -81,64 +82,21 @@ export type Editor = {
81
82
  }
82
83
  }
83
84
 
84
- /**
85
- * @internal
86
- */
87
85
  export function createEditor(config: EditorConfig): Editor {
88
86
  const editorActor = createActor(editorMachine, {
89
87
  input: editorConfigToMachineInput(config),
90
88
  })
91
-
92
89
  editorActor.start()
93
90
 
94
- const slateEditor = createSlateEditor({editorActor})
95
- const editable = createEditableAPI(slateEditor.instance, editorActor)
96
-
97
- return {
98
- send: (event) => {
99
- editorActor.send(event)
100
- },
101
- on: (event, listener) =>
102
- editorActor.on(
103
- event,
104
- // @ts-expect-error
105
- listener,
106
- ),
107
- _internal: {
108
- editable,
109
- editorActor,
110
- slateEditor,
111
- },
112
- }
91
+ return createEditorFromActor(editorActor)
113
92
  }
114
93
 
115
94
  export function useCreateEditor(config: EditorConfig): Editor {
116
95
  const editorActor = useActorRef(editorMachine, {
117
96
  input: editorConfigToMachineInput(config),
118
97
  })
119
- const editor: Editor = useMemo(() => {
120
- const slateEditor = createSlateEditor({editorActor})
121
- const editable = createEditableAPI(slateEditor.instance, editorActor)
122
-
123
- return {
124
- send: (event: EditorEvent) => {
125
- editorActor.send(event)
126
- },
127
- on: (event, listener) =>
128
- editorActor.on(
129
- event,
130
- // @ts-expect-error
131
- listener,
132
- ),
133
- _internal: {
134
- editable,
135
- editorActor,
136
- slateEditor,
137
- },
138
- }
139
- }, [editorActor])
140
98
 
141
- return editor
99
+ return useMemo(() => createEditorFromActor(editorActor), [editorActor])
142
100
  }
143
101
 
144
102
  function editorConfigToMachineInput(config: EditorConfig) {
@@ -157,3 +115,25 @@ function editorConfigToMachineInput(config: EditorConfig) {
157
115
  value: config.initialValue,
158
116
  } as const
159
117
  }
118
+
119
+ function createEditorFromActor(editorActor: EditorActor): Editor {
120
+ const slateEditor = createSlateEditor({editorActor})
121
+ const editable = createEditableAPI(slateEditor.instance, editorActor)
122
+
123
+ return {
124
+ send: (event) => {
125
+ editorActor.send(event)
126
+ },
127
+ on: (event, listener) =>
128
+ editorActor.on(
129
+ event,
130
+ // @ts-expect-error
131
+ listener,
132
+ ),
133
+ _internal: {
134
+ editable,
135
+ editorActor,
136
+ slateEditor,
137
+ },
138
+ }
139
+ }
@@ -10,6 +10,16 @@ import {
10
10
  setup,
11
11
  type ActorRefFrom,
12
12
  } from 'xstate'
13
+ import {performAction} from '../behavior-actions/behavior.actions'
14
+ import {coreBehaviors} from '../behaviors/behavior.core'
15
+ import type {
16
+ Behavior,
17
+ BehaviorAction,
18
+ BehaviorActionIntend,
19
+ NativeBehaviorEvent,
20
+ SyntheticBehaviorEvent,
21
+ } from '../behaviors/behavior.types'
22
+ import type {OmitFromUnion, PickFromUnion} from '../type-utils'
13
23
  import type {
14
24
  EditorSelection,
15
25
  InvalidValueResolution,
@@ -20,17 +30,6 @@ import debug from '../utils/debug'
20
30
  import {toPortableTextRange} from '../utils/ranges'
21
31
  import {fromSlateValue} from '../utils/values'
22
32
  import {KEY_TO_VALUE_ELEMENT} from '../utils/weakMaps'
23
- import {performAction} from './behavior/behavior.actions'
24
- import {coreBehaviors} from './behavior/behavior.core'
25
- import type {
26
- Behavior,
27
- BehaviorAction,
28
- BehaviorActionIntend,
29
- NativeBehaviorEvent,
30
- OmitFromUnion,
31
- PickFromUnion,
32
- SyntheticBehaviorEvent,
33
- } from './behavior/behavior.types'
34
33
  import type {EditorContext} from './editor-snapshot'
35
34
 
36
35
  export * from 'xstate/guards'
@@ -315,7 +314,10 @@ export const editorMachine = setup({
315
314
  }
316
315
 
317
316
  const actionIntendSets = eventBehavior.actions.map((actionSet) =>
318
- actionSet(shouldRun),
317
+ actionSet(
318
+ {context: editorContext, event: event.behaviorEvent},
319
+ shouldRun,
320
+ ),
319
321
  )
320
322
 
321
323
  for (const actionIntends of actionIntendSets) {
@@ -18,6 +18,9 @@ import {
18
18
  } from 'slate'
19
19
  import type {DOMNode} from 'slate-dom'
20
20
  import {ReactEditor} from 'slate-react'
21
+ import {isListItemActive} from '../../behavior-actions/behavior.action.list-item'
22
+ import {isStyleActive} from '../../behavior-actions/behavior.action.style'
23
+ import type {BehaviorActionImplementation} from '../../behavior-actions/behavior.actions'
21
24
  import type {
22
25
  EditableAPI,
23
26
  EditableAPIDeleteOptions,
@@ -31,9 +34,6 @@ import {
31
34
  KEY_TO_VALUE_ELEMENT,
32
35
  SLATE_TO_PORTABLE_TEXT_RANGE,
33
36
  } from '../../utils/weakMaps'
34
- import {isListItemActive} from '../behavior/behavior.action.list-item'
35
- import {isStyleActive} from '../behavior/behavior.action.style'
36
- import type {BehaviorActionImplementation} from '../behavior/behavior.actions'
37
37
  import type {EditorActor} from '../editor-machine'
38
38
  import {isDecoratorActive} from './createWithPortableTextMarkModel'
39
39
 
@@ -8,6 +8,7 @@ import {isPortableTextBlock, isPortableTextSpan} from '@portabletext/toolkit'
8
8
  import type {PortableTextObject, PortableTextSpan} from '@sanity/types'
9
9
  import {isEqual, uniq} from 'lodash'
10
10
  import {Editor, Element, Node, Path, Range, Text, Transforms} from 'slate'
11
+ import type {BehaviorActionImplementation} from '../../behavior-actions/behavior.actions'
11
12
  import type {
12
13
  PortableTextMemberSchemaTypes,
13
14
  PortableTextSlateEditor,
@@ -16,7 +17,6 @@ import {debugWithName} from '../../utils/debug'
16
17
  import {getNextSpan, getPreviousSpan} from '../../utils/sibling-utils'
17
18
  import {isChangingRemotely} from '../../utils/withChanges'
18
19
  import {isRedoing, isUndoing} from '../../utils/withUndoRedo'
19
- import type {BehaviorActionImplementation} from '../behavior/behavior.actions'
20
20
  import type {EditorActor} from '../editor-machine'
21
21
 
22
22
  const debug = debugWithName('plugin:withPortableTextMarkModel')
@@ -4,7 +4,7 @@ import {
4
4
  type KeyedSegment,
5
5
  type PortableTextBlock,
6
6
  } from '@sanity/types'
7
- import type {BlockOffset} from '../behavior/behavior.types'
7
+ import type {BlockOffset} from '../../behaviors/behavior.types'
8
8
 
9
9
  export function blockOffsetToSpanSelectionPoint({
10
10
  value,
package/src/index.ts CHANGED
@@ -1,31 +1,15 @@
1
1
  export type {Patch} from '@portabletext/patches'
2
2
  export type {PortableTextBlock, PortableTextChild} from '@sanity/types'
3
- export {
4
- createCodeEditorBehaviors,
5
- type CodeEditorBehaviorsConfig,
6
- } from './editor/behavior/behavior.code-editor'
7
- export {coreBehavior, coreBehaviors} from './editor/behavior/behavior.core'
8
- export {
9
- createLinkBehaviors,
10
- type LinkBehaviorsConfig,
11
- } from './editor/behavior/behavior.links'
12
- export {
13
- createMarkdownBehaviors,
14
- type MarkdownBehaviorsConfig,
15
- } from './editor/behavior/behavior.markdown'
16
- export {
17
- defineBehavior,
18
- type BehaviorEvent,
19
- type Behavior,
20
- type BehaviorActionIntend,
21
- type BehaviorActionIntendSet,
22
- type SyntheticBehaviorEvent,
23
- type BehaviorGuard,
24
- type BlockOffset,
25
- type NativeBehaviorEvent,
26
- type OmitFromUnion,
27
- type PickFromUnion,
28
- } from './editor/behavior/behavior.types'
3
+ export type {
4
+ Behavior,
5
+ BehaviorActionIntend,
6
+ BehaviorActionIntendSet,
7
+ BehaviorEvent,
8
+ BehaviorGuard,
9
+ BlockOffset,
10
+ NativeBehaviorEvent,
11
+ SyntheticBehaviorEvent,
12
+ } from './behaviors/index'
29
13
  export type {Editor, EditorConfig, EditorEvent} from './editor/create-editor'
30
14
  export type {SlateEditor} from './editor/create-slate-editor'
31
15
  export {
@@ -63,5 +47,6 @@ export {defaultKeyGenerator as keyGenerator} from './editor/key-generator'
63
47
  export type {AddedAnnotationPaths} from './editor/plugins/createWithEditableAPI'
64
48
  export {PortableTextEditor} from './editor/PortableTextEditor'
65
49
  export type {PortableTextEditorProps} from './editor/PortableTextEditor'
50
+ export type {OmitFromUnion, PickFromUnion} from './type-utils'
66
51
  export * from './types/editor'
67
52
  export type {HotkeyOptions} from './types/options'
@@ -1,5 +1,5 @@
1
1
  import type {PortableTextListBlock} from '@sanity/types'
2
- import {createGuards} from '../editor/behavior/behavior.guards'
2
+ import {createGuards} from '../behavior-actions/behavior.guards'
3
3
  import type {EditorSelector} from '../editor/editor-selector'
4
4
  import {getSelectedBlocks} from './selectors'
5
5
 
@@ -9,7 +9,7 @@ import {
9
9
  type PortableTextSpan,
10
10
  type PortableTextTextBlock,
11
11
  } from '@sanity/types'
12
- import {createGuards} from '../editor/behavior/behavior.guards'
12
+ import {createGuards} from '../behavior-actions/behavior.guards'
13
13
  import type {EditorSelector} from '../editor/editor-selector'
14
14
 
15
15
  /**
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @alpha
3
+ */
4
+ export type PickFromUnion<
5
+ TUnion,
6
+ TTagKey extends keyof TUnion,
7
+ TPickedTags extends TUnion[TTagKey],
8
+ > = TUnion extends Record<TTagKey, TPickedTags> ? TUnion : never
9
+
10
+ /**
11
+ * @alpha
12
+ */
13
+ export type OmitFromUnion<
14
+ TUnion,
15
+ TTagKey extends keyof TUnion,
16
+ TOmittedTags extends TUnion[TTagKey],
17
+ > = TUnion extends Record<TTagKey, TOmittedTags> ? never : TUnion
@@ -2,8 +2,8 @@ import type {PortableTextTextBlock} from '@sanity/types'
2
2
  import {createEditor, type Descendant} from 'slate'
3
3
  import {beforeEach, describe, expect, it} from 'vitest'
4
4
  import {createActor} from 'xstate'
5
+ import {coreBehaviors} from '../../behaviors/behavior.core'
5
6
  import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
6
- import {coreBehaviors} from '../../editor/behavior/behavior.core'
7
7
  import {editorMachine} from '../../editor/editor-machine'
8
8
  import {defaultKeyGenerator} from '../../editor/key-generator'
9
9
  import {withPlugins} from '../../editor/plugins/with-plugins'
@@ -2,8 +2,8 @@ import type {Patch} from '@portabletext/patches'
2
2
  import {createEditor, type Descendant} from 'slate'
3
3
  import {beforeEach, describe, expect, it} from 'vitest'
4
4
  import {createActor} from 'xstate'
5
+ import {coreBehaviors} from '../../behaviors/behavior.core'
5
6
  import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
6
- import {coreBehaviors} from '../../editor/behavior/behavior.core'
7
7
  import {editorMachine} from '../../editor/editor-machine'
8
8
  import {defaultKeyGenerator} from '../../editor/key-generator'
9
9
  import {withPlugins} from '../../editor/plugins/with-plugins'