@portabletext/editor 1.35.0 → 1.35.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 (48) hide show
  1. package/lib/_chunks-cjs/{plugin.event-listener.cjs → editor-provider.cjs} +50 -20
  2. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -0
  3. package/lib/_chunks-es/{plugin.event-listener.js → editor-provider.js} +51 -21
  4. package/lib/_chunks-es/editor-provider.js.map +1 -0
  5. package/lib/behaviors/index.d.cts +19377 -210
  6. package/lib/behaviors/index.d.ts +19377 -210
  7. package/lib/index.cjs +81 -51
  8. package/lib/index.cjs.map +1 -1
  9. package/lib/index.d.cts +318 -47
  10. package/lib/index.d.ts +318 -47
  11. package/lib/index.js +35 -4
  12. package/lib/index.js.map +1 -1
  13. package/lib/plugins/index.cjs +22 -7
  14. package/lib/plugins/index.cjs.map +1 -1
  15. package/lib/plugins/index.d.cts +313 -2
  16. package/lib/plugins/index.d.ts +313 -2
  17. package/lib/plugins/index.js +18 -3
  18. package/lib/plugins/index.js.map +1 -1
  19. package/lib/selectors/index.d.cts +19504 -1
  20. package/lib/selectors/index.d.ts +19504 -1
  21. package/lib/utils/index.d.cts +19506 -2
  22. package/lib/utils/index.d.ts +19506 -2
  23. package/package.json +3 -3
  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/behaviors/behavior.decorator-pair.ts +1 -0
  27. package/src/converters/converter.portable-text.deserialize.test.ts +3 -7
  28. package/src/converters/converter.portable-text.ts +7 -1
  29. package/src/converters/converter.text-html.deserialize.test.ts +3 -7
  30. package/src/converters/converter.text-html.serialize.test.ts +5 -10
  31. package/src/converters/converter.text-plain.test.ts +4 -6
  32. package/src/editor/Editable.tsx +26 -0
  33. package/src/editor/editor-machine.ts +170 -147
  34. package/src/editor/editor-selector.ts +3 -0
  35. package/src/editor/editor-snapshot.ts +13 -0
  36. package/src/editor/mutation-machine.ts +2 -0
  37. package/src/editor-event-listener.tsx +30 -0
  38. package/src/index.ts +1 -1
  39. package/src/internal-utils/create-test-snapshot.ts +23 -0
  40. package/src/plugins/plugin.one-line.tsx +1 -1
  41. package/src/selectors/selector.get-active-annotations.test.ts +4 -13
  42. package/src/selectors/selector.get-caret-word-selection.test.ts +3 -7
  43. package/src/selectors/selector.get-selected-spans.test.ts +5 -9
  44. package/src/selectors/selector.get-selection-text.test.ts +5 -7
  45. package/src/selectors/selector.get-trimmed-selection.test.ts +3 -5
  46. package/src/selectors/selector.is-active-decorator.test.ts +5 -9
  47. package/lib/_chunks-cjs/plugin.event-listener.cjs.map +0 -1
  48. package/lib/_chunks-es/plugin.event-listener.js.map +0 -1
@@ -5,6 +5,7 @@ import {fromSlateValue} from '../internal-utils/values'
5
5
  import {KEY_TO_VALUE_ELEMENT} from '../internal-utils/weakMaps'
6
6
  import type {EditorSelection, PortableTextSlateEditor} from '../types/editor'
7
7
  import type {EditorSchema} from './define-schema'
8
+ import type {HasTag} from './editor-machine'
8
9
  import {getActiveDecorators} from './get-active-decorators'
9
10
 
10
11
  /**
@@ -24,6 +25,13 @@ export type EditorContext = {
24
25
  */
25
26
  export type EditorSnapshot = {
26
27
  context: EditorContext
28
+ /**
29
+ * @beta
30
+ * Do not rely on this externally
31
+ */
32
+ beta: {
33
+ hasTag: HasTag
34
+ }
27
35
  }
28
36
 
29
37
  export function createEditorSnapshot({
@@ -31,11 +39,13 @@ export function createEditorSnapshot({
31
39
  editor,
32
40
  keyGenerator,
33
41
  schema,
42
+ hasTag,
34
43
  }: {
35
44
  converters: Array<Converter>
36
45
  editor: PortableTextSlateEditor
37
46
  keyGenerator: () => string
38
47
  schema: EditorSchema
48
+ hasTag: HasTag
39
49
  }) {
40
50
  const value = fromSlateValue(
41
51
  editor.children,
@@ -58,5 +68,8 @@ export function createEditorSnapshot({
58
68
 
59
69
  return {
60
70
  context,
71
+ beta: {
72
+ hasTag,
73
+ },
61
74
  } satisfies EditorSnapshot
62
75
  }
@@ -85,12 +85,14 @@ export const mutationMachine = setup({
85
85
  actions: ['emit mutation', 'clear pending patches'],
86
86
  },
87
87
  {
88
+ target: 'has pending patches',
88
89
  reenter: true,
89
90
  },
90
91
  ],
91
92
  },
92
93
  on: {
93
94
  patch: {
95
+ target: 'has pending patches',
94
96
  actions: ['defer patch'],
95
97
  reenter: true,
96
98
  },
@@ -0,0 +1,30 @@
1
+ import {useEffect} from 'react'
2
+ import {useEffectEvent} from 'use-effect-event'
3
+ import type {EditorEmittedEvent} from './editor/editor-machine'
4
+ import {useEditor} from './editor/editor-provider'
5
+
6
+ /**
7
+ * @public
8
+ * @deprecated
9
+ * This component has been renamed. Use `EventListenerPlugin` instead.
10
+ *
11
+ * ```
12
+ * import {EventListenerPlugin} from '@portabletext/editor/plugins'
13
+ * ```
14
+ */
15
+ export function EditorEventListener(props: {
16
+ on: (event: EditorEmittedEvent) => void
17
+ }) {
18
+ const editor = useEditor()
19
+ const on = useEffectEvent(props.on)
20
+
21
+ useEffect(() => {
22
+ const subscription = editor.on('*', on)
23
+
24
+ return () => {
25
+ subscription.unsubscribe()
26
+ }
27
+ }, [editor])
28
+
29
+ return null
30
+ }
package/src/index.ts CHANGED
@@ -41,7 +41,7 @@ export {defaultKeyGenerator as keyGenerator} from './editor/key-generator'
41
41
  export type {AddedAnnotationPaths} from './editor/plugins/createWithEditableAPI'
42
42
  export {PortableTextEditor} from './editor/PortableTextEditor'
43
43
  export type {PortableTextEditorProps} from './editor/PortableTextEditor'
44
- export {EventListenerPlugin as EditorEventListener} from './plugins/plugin.event-listener'
44
+ export {EditorEventListener} from './editor-event-listener'
45
45
  export type {BlockOffset} from './types/block-offset'
46
46
  export * from './types/editor'
47
47
  export type {PortableTextMemberSchemaTypes} from './types/editor'
@@ -0,0 +1,23 @@
1
+ import {compileSchemaDefinition, defineSchema} from '../editor/define-schema'
2
+ import type {EditorSnapshot} from '../selectors'
3
+ import {createTestKeyGenerator} from './test-key-generator'
4
+
5
+ export function createTestSnapshot(snapshot: {
6
+ context?: Partial<EditorSnapshot['context']>
7
+ beta?: Partial<EditorSnapshot['beta']>
8
+ }): EditorSnapshot {
9
+ return {
10
+ context: {
11
+ converters: snapshot.context?.converters ?? [],
12
+ schema:
13
+ snapshot.context?.schema ?? compileSchemaDefinition(defineSchema({})),
14
+ keyGenerator: snapshot.context?.keyGenerator ?? createTestKeyGenerator(),
15
+ activeDecorators: snapshot.context?.activeDecorators ?? [],
16
+ value: snapshot.context?.value ?? [],
17
+ selection: snapshot.context?.selection ?? null,
18
+ },
19
+ beta: {
20
+ hasTag: snapshot.beta?.hasTag ?? (() => false),
21
+ },
22
+ }
23
+ }
@@ -10,7 +10,7 @@ const oneLineBehaviors = [
10
10
  */
11
11
  defineBehavior({
12
12
  on: 'insert.break',
13
- guard: (snapshot) =>
13
+ guard: ({snapshot}) =>
14
14
  snapshot.context.selection && selectors.isSelectionExpanded(snapshot)
15
15
  ? {selection: snapshot.context.selection}
16
16
  : false,
@@ -1,25 +1,16 @@
1
1
  import type {PortableTextBlock} from '@sanity/types'
2
2
  import {expect, test} from 'vitest'
3
- import type {EditorSchema} from '../editor/define-schema'
4
- import type {EditorSnapshot} from '../editor/editor-snapshot'
5
- import {createTestKeyGenerator} from '../internal-utils/test-key-generator'
3
+ import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
6
4
  import type {EditorSelection} from '../utils'
7
5
  import {getActiveAnnotations} from './selector.get-active-annotations'
8
6
 
9
- function snapshot(
10
- value: Array<PortableTextBlock>,
11
- selection: EditorSelection,
12
- ): EditorSnapshot {
13
- return {
7
+ function snapshot(value: Array<PortableTextBlock>, selection: EditorSelection) {
8
+ return createTestSnapshot({
14
9
  context: {
15
- converters: [],
16
- schema: {} as EditorSchema,
17
- keyGenerator: createTestKeyGenerator(),
18
- activeDecorators: [],
19
10
  value,
20
11
  selection,
21
12
  },
22
- }
13
+ })
23
14
  }
24
15
 
25
16
  const link = {
@@ -1,7 +1,6 @@
1
1
  import type {PortableTextBlock} from '@sanity/types'
2
2
  import {describe, expect, test} from 'vitest'
3
- import {compileSchemaDefinition, defineSchema} from '../editor/define-schema'
4
- import type {EditorSnapshot} from '../editor/editor-snapshot'
3
+ import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
5
4
  import {createTestKeyGenerator} from '../internal-utils/test-key-generator'
6
5
  import type {EditorSelection} from '../utils'
7
6
  import {getCaretWordSelection} from './selector.get-caret-word-selection'
@@ -9,16 +8,13 @@ import {getCaretWordSelection} from './selector.get-caret-word-selection'
9
8
  const keyGenerator = createTestKeyGenerator()
10
9
 
11
10
  function snapshot(value: Array<PortableTextBlock>, selection: EditorSelection) {
12
- return {
11
+ return createTestSnapshot({
13
12
  context: {
14
13
  value,
15
14
  selection,
16
15
  keyGenerator,
17
- activeDecorators: [],
18
- converters: [],
19
- schema: compileSchemaDefinition(defineSchema({})),
20
16
  },
21
- } satisfies EditorSnapshot
17
+ })
22
18
  }
23
19
 
24
20
  describe(getCaretWordSelection.name, () => {
@@ -1,7 +1,7 @@
1
1
  import type {PortableTextBlock} from '@sanity/types'
2
2
  import {describe, expect, test} from 'vitest'
3
- import {getSelectedSpans, type EditorSchema, type EditorSelection} from '.'
4
- import type {EditorSnapshot} from '../editor/editor-snapshot'
3
+ import {getSelectedSpans, type EditorSelection} from '.'
4
+ import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
5
5
 
6
6
  const fooBar = {
7
7
  _type: 'block',
@@ -40,17 +40,13 @@ describe(getSelectedSpans.name, () => {
40
40
  function snapshot(
41
41
  value: Array<PortableTextBlock>,
42
42
  selection: EditorSelection,
43
- ): EditorSnapshot {
44
- return {
43
+ ) {
44
+ return createTestSnapshot({
45
45
  context: {
46
- converters: [],
47
- schema: {} as EditorSchema,
48
- keyGenerator: () => '',
49
- activeDecorators: [],
50
46
  value,
51
47
  selection,
52
48
  },
53
- }
49
+ })
54
50
  }
55
51
 
56
52
  test('selecting a single span', () => {
@@ -1,7 +1,8 @@
1
1
  import type {PortableTextBlock} from '@sanity/types'
2
2
  import {expect, test} from 'vitest'
3
- import type {EditorSelection, EditorSnapshot} from '.'
3
+ import type {EditorSelection} from '.'
4
4
  import {compileSchemaDefinition, defineSchema} from '../editor/define-schema'
5
+ import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
5
6
  import {getSelectionText} from './selector.get-selection-text'
6
7
 
7
8
  const brokenBlock = {
@@ -67,21 +68,18 @@ test(getSelectionText.name, () => {
67
68
  function snapshot(
68
69
  value: Array<PortableTextBlock>,
69
70
  selection: EditorSelection,
70
- ): EditorSnapshot {
71
- return {
71
+ ) {
72
+ return createTestSnapshot({
72
73
  context: {
73
- converters: [],
74
74
  schema: compileSchemaDefinition(
75
75
  defineSchema({
76
76
  inlineObjects: [{name: 'stock-ticker'}],
77
77
  }),
78
78
  ),
79
- keyGenerator: () => '',
80
- activeDecorators: [],
81
79
  value,
82
80
  selection,
83
81
  },
84
- }
82
+ })
85
83
  }
86
84
 
87
85
  expect(
@@ -1,7 +1,7 @@
1
1
  import type {PortableTextBlock} from '@sanity/types'
2
2
  import {describe, expect, test} from 'vitest'
3
3
  import {compileSchemaDefinition, defineSchema} from '../editor/define-schema'
4
- import type {EditorSnapshot} from '../editor/editor-snapshot'
4
+ import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
5
5
  import {parseBlock} from '../internal-utils/parse-blocks'
6
6
  import {createTestKeyGenerator} from '../internal-utils/test-key-generator'
7
7
  import type {EditorSelection} from '../types/editor'
@@ -20,10 +20,8 @@ function snapshot(
20
20
  }),
21
21
  )
22
22
 
23
- return {
23
+ return createTestSnapshot({
24
24
  context: {
25
- activeDecorators: [],
26
- converters: [],
27
25
  keyGenerator,
28
26
  schema,
29
27
  selection,
@@ -42,7 +40,7 @@ function snapshot(
42
40
  return parsedBlock ? [parsedBlock] : []
43
41
  }),
44
42
  },
45
- } satisfies EditorSnapshot
43
+ })
46
44
  }
47
45
 
48
46
  function createSpan(text: string, marks: Array<string> = []) {
@@ -1,16 +1,12 @@
1
1
  import {expect, test} from 'vitest'
2
- import type {EditorSchema, EditorSelection} from '.'
3
- import type {EditorSnapshot} from '../editor/editor-snapshot'
2
+ import type {EditorSelection} from '.'
3
+ import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
4
4
  import {isActiveDecorator} from './selector.is-active-decorator'
5
5
 
6
6
  test(isActiveDecorator.name, () => {
7
- function snapshot(selection: EditorSelection): EditorSnapshot {
8
- return {
7
+ function snapshot(selection: EditorSelection) {
8
+ return createTestSnapshot({
9
9
  context: {
10
- converters: [],
11
- schema: {} as EditorSchema,
12
- keyGenerator: () => '',
13
- activeDecorators: [],
14
10
  value: [
15
11
  {
16
12
  _type: '_block',
@@ -32,7 +28,7 @@ test(isActiveDecorator.name, () => {
32
28
  ],
33
29
  selection,
34
30
  },
35
- }
31
+ })
36
32
  }
37
33
 
38
34
  expect(