@portabletext/editor 2.1.1 → 2.1.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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -75,14 +75,14 @@
75
75
  "lodash": "^4.17.21",
76
76
  "lodash.startcase": "^4.4.0",
77
77
  "react-compiler-runtime": "19.1.0-rc.2",
78
- "slate": "0.117.2",
78
+ "slate": "0.118.0",
79
79
  "slate-dom": "^0.117.4",
80
80
  "slate-react": "0.117.4",
81
81
  "use-effect-event": "^1.0.2",
82
82
  "xstate": "^5.20.1",
83
- "@portabletext/block-tools": "2.0.2",
84
- "@portabletext/keyboard-shortcuts": "1.1.1",
85
- "@portabletext/patches": "1.1.6"
83
+ "@portabletext/block-tools": "2.0.4",
84
+ "@portabletext/patches": "1.1.6",
85
+ "@portabletext/keyboard-shortcuts": "1.1.1"
86
86
  },
87
87
  "devDependencies": {
88
88
  "@portabletext/toolkit": "^2.0.17",
@@ -98,7 +98,7 @@
98
98
  "@types/react-dom": "^19.1.7",
99
99
  "@typescript-eslint/eslint-plugin": "^8.38.0",
100
100
  "@typescript-eslint/parser": "^8.38.0",
101
- "@vitejs/plugin-react": "^4.6.0",
101
+ "@vitejs/plugin-react": "^4.7.0",
102
102
  "@vitest/browser": "^3.2.4",
103
103
  "@vitest/coverage-istanbul": "^3.2.4",
104
104
  "babel-plugin-react-compiler": "19.1.0-rc.2",
@@ -112,7 +112,7 @@
112
112
  "vite": "^7.0.6",
113
113
  "vitest": "^3.2.4",
114
114
  "vitest-browser-react": "^1.0.1",
115
- "racejar": "1.2.11"
115
+ "racejar": "1.2.12"
116
116
  },
117
117
  "peerDependencies": {
118
118
  "@sanity/schema": "^4.2.0",
@@ -5,7 +5,15 @@ import {defineBehavior} from './behavior.types.behavior'
5
5
  export const abstractListItemBehaviors = [
6
6
  defineBehavior({
7
7
  on: 'list item.add',
8
- guard: ({snapshot}) => {
8
+ guard: ({snapshot, event}) => {
9
+ if (
10
+ !snapshot.context.schema.lists.some(
11
+ (list) => list.name === event.listItem,
12
+ )
13
+ ) {
14
+ return false
15
+ }
16
+
9
17
  const selectedTextBlocks = getSelectedTextBlocks(snapshot)
10
18
 
11
19
  return {selectedTextBlocks}
@@ -17,7 +25,7 @@ export const abstractListItemBehaviors = [
17
25
  type: 'block.set',
18
26
  at: block.path,
19
27
  props: {
20
- level: 1,
28
+ level: block.node.level ?? 1,
21
29
  listItem: event.listItem,
22
30
  },
23
31
  }),
@@ -29,11 +29,11 @@ import type {
29
29
  import {createInternalEditor, type InternalEditor} from './create-editor'
30
30
  import {EditorActorContext} from './editor-actor-context'
31
31
  import type {EditorActor} from './editor-machine'
32
+ import {eventToChange} from './event-to-change'
32
33
  import {PortableTextEditorContext} from './hooks/usePortableTextEditor'
33
34
  import type {MutationActor} from './mutation-machine'
34
35
  import {RelayActorContext} from './relay-actor-context'
35
36
  import type {RelayActor} from './relay-machine'
36
- import {eventToChange} from './route-events-to-changes'
37
37
  import type {SyncActor} from './sync-machine'
38
38
 
39
39
  const debug = debugWithName('component:PortableTextEditor')
@@ -7,13 +7,13 @@ import useConstant from '../internal-utils/use-constant'
7
7
  import {createInternalEditor} from './create-editor'
8
8
  import {EditorActorContext} from './editor-actor-context'
9
9
  import {EditorContext} from './editor-context'
10
+ import {eventToChange} from './event-to-change'
10
11
  import {PortableTextEditorContext} from './hooks/usePortableTextEditor'
11
12
  import {
12
13
  PortableTextEditor,
13
14
  type PortableTextEditorProps,
14
15
  } from './PortableTextEditor'
15
16
  import {RelayActorContext} from './relay-actor-context'
16
- import {RouteEventsToChanges} from './route-events-to-changes'
17
17
 
18
18
  /**
19
19
  * @public
@@ -58,6 +58,18 @@ export function EditorProvider(props: EditorProviderProps) {
58
58
  unsubscribers.push(subscription())
59
59
  }
60
60
 
61
+ const relayActorSubscription = internalEditor.actors.relayActor.on(
62
+ '*',
63
+ (event) => {
64
+ const change = eventToChange(event)
65
+
66
+ if (change) {
67
+ portableTextEditor.change$.next(change)
68
+ }
69
+ },
70
+ )
71
+ unsubscribers.push(relayActorSubscription.unsubscribe)
72
+
61
73
  internalEditor.actors.editorActor.start()
62
74
  internalEditor.actors.mutationActor.start()
63
75
  internalEditor.actors.relayActor.start()
@@ -73,16 +85,10 @@ export function EditorProvider(props: EditorProviderProps) {
73
85
  stopActor(internalEditor.actors.relayActor)
74
86
  stopActor(internalEditor.actors.syncActor)
75
87
  }
76
- }, [internalEditor])
88
+ }, [internalEditor, portableTextEditor])
77
89
 
78
90
  return (
79
91
  <EditorContext.Provider value={internalEditor.editor}>
80
- <RouteEventsToChanges
81
- relayActor={internalEditor.actors.relayActor}
82
- onChange={(change) => {
83
- portableTextEditor.change$.next(change)
84
- }}
85
- />
86
92
  <EditorActorContext.Provider value={internalEditor.actors.editorActor}>
87
93
  <RelayActorContext.Provider value={internalEditor.actors.relayActor}>
88
94
  <Slate
@@ -0,0 +1,45 @@
1
+ import type {EditorChange} from '../types/editor'
2
+ import type {InternalEditorEmittedEvent} from './relay-machine'
3
+
4
+ export function eventToChange(
5
+ event: InternalEditorEmittedEvent,
6
+ ): EditorChange | undefined {
7
+ switch (event.type) {
8
+ case 'blurred': {
9
+ return {type: 'blur', event: event.event}
10
+ }
11
+ case 'patch':
12
+ return event
13
+ case 'loading': {
14
+ return {type: 'loading', isLoading: true}
15
+ }
16
+ case 'done loading': {
17
+ return {type: 'loading', isLoading: false}
18
+ }
19
+ case 'focused': {
20
+ return {type: 'focus', event: event.event}
21
+ }
22
+ case 'value changed': {
23
+ return {type: 'value', value: event.value}
24
+ }
25
+ case 'invalid value': {
26
+ return {
27
+ type: 'invalidValue',
28
+ resolution: event.resolution,
29
+ value: event.value,
30
+ }
31
+ }
32
+ case 'mutation': {
33
+ return event
34
+ }
35
+ case 'ready': {
36
+ return event
37
+ }
38
+ case 'selection': {
39
+ return event
40
+ }
41
+ case 'unset': {
42
+ return event
43
+ }
44
+ }
45
+ }
@@ -0,0 +1,19 @@
1
+ import {useEffect} from 'react'
2
+ import {usePortableTextEditor} from '../editor/hooks/usePortableTextEditor'
3
+ import type {EditorChange} from '../types/editor'
4
+
5
+ export function InternalChange$Plugin(props: {
6
+ onChange: (change: EditorChange) => void
7
+ }) {
8
+ const change$ = usePortableTextEditor().change$
9
+
10
+ useEffect(() => {
11
+ const subscription = change$.subscribe(props.onChange)
12
+
13
+ return () => {
14
+ subscription.unsubscribe()
15
+ }
16
+ }, [change$, props.onChange])
17
+
18
+ return null
19
+ }
@@ -1,75 +0,0 @@
1
- import {useEffect} from 'react'
2
- import {useEffectEvent} from 'use-effect-event'
3
- import type {EditorChange} from '../types/editor'
4
- import type {InternalEditorEmittedEvent, RelayActor} from './relay-machine'
5
-
6
- export function RouteEventsToChanges(props: {
7
- relayActor: RelayActor
8
- onChange: (change: EditorChange) => void
9
- }) {
10
- // We want to ensure that _when_ `props.onChange` is called, it uses the current value.
11
- // But we don't want to have the `useEffect` run setup + teardown + setup every time the prop might change, as that's unnecessary.
12
- // So we use our own polyfill that lets us use an upcoming React hook that solves this exact problem.
13
- // https://19.react.dev/learn/separating-events-from-effects#declaring-an-effect-event
14
- const handleChange = useEffectEvent((change: EditorChange) =>
15
- props.onChange(change),
16
- )
17
-
18
- useEffect(() => {
19
- const sub = props.relayActor.on('*', (event) => {
20
- const change = eventToChange(event)
21
-
22
- if (change) {
23
- handleChange(change)
24
- }
25
- })
26
- return () => {
27
- sub.unsubscribe()
28
- }
29
- }, [props.relayActor])
30
-
31
- return null
32
- }
33
-
34
- export function eventToChange(
35
- event: InternalEditorEmittedEvent,
36
- ): EditorChange | undefined {
37
- switch (event.type) {
38
- case 'blurred': {
39
- return {type: 'blur', event: event.event}
40
- }
41
- case 'patch':
42
- return event
43
- case 'loading': {
44
- return {type: 'loading', isLoading: true}
45
- }
46
- case 'done loading': {
47
- return {type: 'loading', isLoading: false}
48
- }
49
- case 'focused': {
50
- return {type: 'focus', event: event.event}
51
- }
52
- case 'value changed': {
53
- return {type: 'value', value: event.value}
54
- }
55
- case 'invalid value': {
56
- return {
57
- type: 'invalidValue',
58
- resolution: event.resolution,
59
- value: event.value,
60
- }
61
- }
62
- case 'mutation': {
63
- return event
64
- }
65
- case 'ready': {
66
- return event
67
- }
68
- case 'selection': {
69
- return event
70
- }
71
- case 'unset': {
72
- return event
73
- }
74
- }
75
- }