@portabletext/editor 1.49.13 → 1.50.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.
@@ -0,0 +1,202 @@
1
+ import type {PortableTextBlock} from '@sanity/types'
2
+ import {describe, expect, test} from 'vitest'
3
+ import type {EditorSelector} from '../editor/editor-selector'
4
+ import {defaultKeyGenerator} from '../editor/key-generator'
5
+ import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
6
+ import {getListState, type ListState} from './selector.get-list-state'
7
+
8
+ function blockObject(_key: string, name: string) {
9
+ return {
10
+ _key,
11
+ _type: name,
12
+ }
13
+ }
14
+
15
+ function textBlock(
16
+ _key: string,
17
+ {
18
+ listItem,
19
+ level,
20
+ }: {
21
+ listItem?: 'bullet' | 'number'
22
+ level?: number
23
+ },
24
+ ): PortableTextBlock {
25
+ return {
26
+ _key,
27
+ _type: 'block',
28
+ children: [
29
+ {
30
+ _key: defaultKeyGenerator(),
31
+ _type: 'span',
32
+ text: `${listItem}-${level}`,
33
+ },
34
+ ],
35
+ style: 'normal',
36
+ level,
37
+ listItem,
38
+ }
39
+ }
40
+
41
+ function createSnapshot(value: Array<PortableTextBlock>) {
42
+ return createTestSnapshot({
43
+ context: {
44
+ value,
45
+ },
46
+ })
47
+ }
48
+
49
+ function getListStates(
50
+ paths: Array<[{_key: string}]>,
51
+ ): EditorSelector<Array<ListState | undefined>> {
52
+ return (snapshot) => {
53
+ return paths.map((path) => getListState({path})(snapshot))
54
+ }
55
+ }
56
+ describe(getListState.name, () => {
57
+ test('empty', () => {
58
+ const snapshot = createSnapshot([])
59
+
60
+ expect(getListState({path: [{_key: 'k0'}]})(snapshot)).toEqual(undefined)
61
+ })
62
+
63
+ test('single list item', () => {
64
+ const snapshot = createSnapshot([
65
+ textBlock('k0', {listItem: 'number', level: 1}),
66
+ ])
67
+
68
+ expect(getListState({path: [{_key: 'k0'}]})(snapshot)).toEqual({
69
+ index: 1,
70
+ })
71
+ })
72
+
73
+ test('single indented list item', () => {
74
+ const snapshot = createSnapshot([
75
+ textBlock('k0', {listItem: 'number', level: 2}),
76
+ ])
77
+
78
+ expect(getListState({path: [{_key: 'k0'}]})(snapshot)).toEqual({
79
+ index: 1,
80
+ })
81
+ })
82
+
83
+ test('two lists broken up by a paragraph', () => {
84
+ const snapshot = createSnapshot([
85
+ textBlock('k0', {listItem: 'number', level: 1}),
86
+ textBlock('k1', {listItem: 'number', level: 1}),
87
+ textBlock('k2', {}),
88
+ textBlock('k3', {listItem: 'number', level: 1}),
89
+ textBlock('k4', {listItem: 'number', level: 1}),
90
+ ])
91
+
92
+ expect(
93
+ getListStates([
94
+ [{_key: 'k0'}],
95
+ [{_key: 'k1'}],
96
+ [{_key: 'k2'}],
97
+ [{_key: 'k3'}],
98
+ [{_key: 'k4'}],
99
+ ])(snapshot),
100
+ ).toEqual([{index: 1}, {index: 2}, undefined, {index: 1}, {index: 2}])
101
+ })
102
+
103
+ test('two lists broken up by an image', () => {
104
+ const snapshot = createSnapshot([
105
+ textBlock('k0', {listItem: 'number', level: 1}),
106
+ textBlock('k1', {listItem: 'number', level: 1}),
107
+ blockObject('k2', 'image'),
108
+ textBlock('k3', {listItem: 'number', level: 1}),
109
+ textBlock('k4', {listItem: 'number', level: 1}),
110
+ ])
111
+
112
+ expect(
113
+ getListStates([
114
+ [{_key: 'k0'}],
115
+ [{_key: 'k1'}],
116
+ [{_key: 'k2'}],
117
+ [{_key: 'k3'}],
118
+ [{_key: 'k4'}],
119
+ ])(snapshot),
120
+ ).toEqual([{index: 1}, {index: 2}, undefined, {index: 1}, {index: 2}])
121
+ })
122
+
123
+ test('numbered lists broken up by a bulleted list', () => {
124
+ const snapshot = createSnapshot([
125
+ textBlock('k0', {listItem: 'number', level: 1}),
126
+ textBlock('k1', {listItem: 'bullet', level: 1}),
127
+ textBlock('k2', {listItem: 'number', level: 1}),
128
+ ])
129
+
130
+ expect(
131
+ getListStates([[{_key: 'k0'}], [{_key: 'k1'}], [{_key: 'k2'}]])(snapshot),
132
+ ).toEqual([{index: 1}, {index: 1}, {index: 1}])
133
+ })
134
+
135
+ test('simple indented list', () => {
136
+ const snapshot = createSnapshot([
137
+ textBlock('k0', {listItem: 'number', level: 1}),
138
+ textBlock('k1', {listItem: 'number', level: 2}),
139
+ textBlock('k2', {listItem: 'number', level: 2}),
140
+ textBlock('k3', {listItem: 'number', level: 1}),
141
+ ])
142
+
143
+ expect(
144
+ getListStates([
145
+ [{_key: 'k0'}],
146
+ [{_key: 'k1'}],
147
+ [{_key: 'k2'}],
148
+ [{_key: 'k3'}],
149
+ ])(snapshot),
150
+ ).toEqual([{index: 1}, {index: 1}, {index: 2}, {index: 2}])
151
+ })
152
+
153
+ test('reverse indented list', () => {
154
+ const snapshot = createSnapshot([
155
+ textBlock('k0', {listItem: 'number', level: 2}),
156
+ textBlock('k1', {listItem: 'number', level: 1}),
157
+ textBlock('k2', {listItem: 'number', level: 2}),
158
+ ])
159
+
160
+ expect(
161
+ getListStates([[{_key: 'k0'}], [{_key: 'k1'}], [{_key: 'k2'}]])(snapshot),
162
+ ).toEqual([{index: 1}, {index: 1}, {index: 1}])
163
+ })
164
+
165
+ test('complex list', () => {
166
+ const snapshot = createSnapshot([
167
+ textBlock('k0', {listItem: 'number', level: 1}),
168
+ textBlock('k1', {listItem: 'number', level: 3}),
169
+ textBlock('k2', {listItem: 'number', level: 2}),
170
+ textBlock('k3', {listItem: 'number', level: 3}),
171
+ textBlock('k4', {listItem: 'number', level: 1}),
172
+ textBlock('k5', {listItem: 'number', level: 3}),
173
+ textBlock('k6', {listItem: 'number', level: 4}),
174
+ textBlock('k7', {listItem: 'number', level: 3}),
175
+ textBlock('k8', {listItem: 'number', level: 1}),
176
+ ])
177
+
178
+ expect(
179
+ getListStates([
180
+ [{_key: 'k0'}],
181
+ [{_key: 'k1'}],
182
+ [{_key: 'k2'}],
183
+ [{_key: 'k3'}],
184
+ [{_key: 'k4'}],
185
+ [{_key: 'k5'}],
186
+ [{_key: 'k6'}],
187
+ [{_key: 'k7'}],
188
+ [{_key: 'k8'}],
189
+ ])(snapshot),
190
+ ).toEqual([
191
+ {index: 1},
192
+ {index: 1},
193
+ {index: 1},
194
+ {index: 1},
195
+ {index: 2},
196
+ {index: 1},
197
+ {index: 1},
198
+ {index: 2},
199
+ {index: 3},
200
+ ])
201
+ })
202
+ })
@@ -0,0 +1,175 @@
1
+ import type {PortableTextTextBlock} from '@sanity/types'
2
+ import type {EditorSelector} from '../editor/editor-selector'
3
+ import {isTextBlock} from '../internal-utils/parse-blocks'
4
+ import type {BlockPath} from '../types/paths'
5
+ import {getFocusTextBlock, getPreviousBlock} from './selectors'
6
+
7
+ /**
8
+ * @beta
9
+ * Represents the List state of a block.
10
+ */
11
+ export type ListState = {
12
+ index: number
13
+ }
14
+
15
+ /**
16
+ * @beta
17
+ * Given the `path` of a block, this selector will return the `ListState` of
18
+ * the block.
19
+ */
20
+ export function getListState({
21
+ path,
22
+ }: {
23
+ path: BlockPath
24
+ }): EditorSelector<ListState | undefined> {
25
+ return (snapshot) => {
26
+ const selection = {
27
+ anchor: {
28
+ path,
29
+ offset: 0,
30
+ },
31
+ focus: {
32
+ path,
33
+ offset: 0,
34
+ },
35
+ }
36
+
37
+ const focusTextBlock = getFocusTextBlock({
38
+ ...snapshot,
39
+ context: {
40
+ ...snapshot.context,
41
+ selection,
42
+ },
43
+ })
44
+
45
+ if (!focusTextBlock) {
46
+ return undefined
47
+ }
48
+
49
+ if (
50
+ focusTextBlock.node.listItem === undefined ||
51
+ focusTextBlock.node.level === undefined
52
+ ) {
53
+ return undefined
54
+ }
55
+
56
+ const previousListItem = getPreviousListItem({
57
+ listItem: focusTextBlock.node.listItem,
58
+ level: focusTextBlock.node.level,
59
+ })({
60
+ ...snapshot,
61
+ context: {
62
+ ...snapshot.context,
63
+ selection,
64
+ },
65
+ })
66
+
67
+ if (!previousListItem) {
68
+ return {
69
+ index: 1,
70
+ }
71
+ }
72
+
73
+ if (previousListItem.node.listItem !== focusTextBlock.node.listItem) {
74
+ return {
75
+ index: 1,
76
+ }
77
+ }
78
+
79
+ if (
80
+ previousListItem.node.level !== undefined &&
81
+ previousListItem.node.level < focusTextBlock.node.level
82
+ ) {
83
+ return {
84
+ index: 1,
85
+ }
86
+ }
87
+
88
+ const previousListItemListState = getListState({
89
+ path: previousListItem.path,
90
+ })(snapshot)
91
+
92
+ if (previousListItemListState === undefined) {
93
+ return {
94
+ index: 1,
95
+ }
96
+ }
97
+
98
+ return {
99
+ index: previousListItemListState.index + 1,
100
+ }
101
+ }
102
+ }
103
+
104
+ function getPreviousListItem({
105
+ listItem,
106
+ level,
107
+ }: {
108
+ listItem: string
109
+ level: number
110
+ }): EditorSelector<
111
+ | {
112
+ node: PortableTextTextBlock
113
+ path: [{_key: string}]
114
+ }
115
+ | undefined
116
+ > {
117
+ return (snapshot) => {
118
+ const previousBlock = getPreviousBlock({
119
+ ...snapshot,
120
+ context: {
121
+ ...snapshot.context,
122
+ },
123
+ })
124
+
125
+ if (!previousBlock) {
126
+ return undefined
127
+ }
128
+
129
+ if (!isTextBlock(snapshot.context, previousBlock.node)) {
130
+ return undefined
131
+ }
132
+
133
+ if (
134
+ previousBlock.node.listItem === undefined ||
135
+ previousBlock.node.level === undefined
136
+ ) {
137
+ return undefined
138
+ }
139
+
140
+ if (previousBlock.node.listItem !== listItem) {
141
+ return undefined
142
+ }
143
+
144
+ if (previousBlock.node.level === level) {
145
+ return {
146
+ node: previousBlock.node,
147
+ path: previousBlock.path,
148
+ }
149
+ }
150
+
151
+ if (previousBlock.node.level < level) {
152
+ return undefined
153
+ }
154
+
155
+ return getPreviousListItem({
156
+ listItem,
157
+ level,
158
+ })({
159
+ ...snapshot,
160
+ context: {
161
+ ...snapshot.context,
162
+ selection: {
163
+ anchor: {
164
+ path: previousBlock.path,
165
+ offset: 0,
166
+ },
167
+ focus: {
168
+ path: previousBlock.path,
169
+ offset: 0,
170
+ },
171
+ },
172
+ },
173
+ })
174
+ }
175
+ }
@@ -29,6 +29,7 @@ import type {DOMNode} from 'slate-dom'
29
29
  import type {ReactEditor} from 'slate-react'
30
30
  import type {PortableTextEditableProps} from '../editor/Editable'
31
31
  import type {PortableTextEditor} from '../editor/PortableTextEditor'
32
+ import type {BlockPath} from './paths'
32
33
 
33
34
  /** @beta */
34
35
  export interface EditableAPIDeleteOptions {
@@ -373,7 +374,7 @@ export interface BlockRenderProps {
373
374
  focused: boolean
374
375
  level?: number
375
376
  listItem?: string
376
- path: Path
377
+ path: BlockPath
377
378
  selected: boolean
378
379
  style?: string
379
380
  schemaType: ObjectSchemaType
@@ -0,0 +1 @@
1
+ export type BlockPath = [{_key: string}]