@portabletext/editor 1.50.7 → 1.50.8

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.
@@ -3,7 +3,7 @@ import {describe, expect, test} from 'vitest'
3
3
  import type {EditorSelector} from '../editor/editor-selector'
4
4
  import {defaultKeyGenerator} from '../editor/key-generator'
5
5
  import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
6
- import {getListState, type ListState} from './selector.get-list-state'
6
+ import {getListIndex} from './selector.get-list-state'
7
7
 
8
8
  function blockObject(_key: string, name: string) {
9
9
  return {
@@ -48,16 +48,17 @@ function createSnapshot(value: Array<PortableTextBlock>) {
48
48
 
49
49
  function getListStates(
50
50
  paths: Array<[{_key: string}]>,
51
- ): EditorSelector<Array<ListState | undefined>> {
51
+ ): EditorSelector<Array<number | undefined>> {
52
52
  return (snapshot) => {
53
- return paths.map((path) => getListState({path})(snapshot))
53
+ return paths.map((path) => getListIndex({path})(snapshot))
54
54
  }
55
55
  }
56
- describe(getListState.name, () => {
56
+
57
+ describe(getListIndex.name, () => {
57
58
  test('empty', () => {
58
59
  const snapshot = createSnapshot([])
59
60
 
60
- expect(getListState({path: [{_key: 'k0'}]})(snapshot)).toEqual(undefined)
61
+ expect(getListIndex({path: [{_key: 'k0'}]})(snapshot)).toEqual(undefined)
61
62
  })
62
63
 
63
64
  test('single list item', () => {
@@ -65,9 +66,7 @@ describe(getListState.name, () => {
65
66
  textBlock('k0', {listItem: 'number', level: 1}),
66
67
  ])
67
68
 
68
- expect(getListState({path: [{_key: 'k0'}]})(snapshot)).toEqual({
69
- index: 1,
70
- })
69
+ expect(getListIndex({path: [{_key: 'k0'}]})(snapshot)).toEqual(1)
71
70
  })
72
71
 
73
72
  test('single indented list item', () => {
@@ -75,9 +74,7 @@ describe(getListState.name, () => {
75
74
  textBlock('k0', {listItem: 'number', level: 2}),
76
75
  ])
77
76
 
78
- expect(getListState({path: [{_key: 'k0'}]})(snapshot)).toEqual({
79
- index: 1,
80
- })
77
+ expect(getListIndex({path: [{_key: 'k0'}]})(snapshot)).toEqual(1)
81
78
  })
82
79
 
83
80
  test('two lists broken up by a paragraph', () => {
@@ -97,7 +94,7 @@ describe(getListState.name, () => {
97
94
  [{_key: 'k3'}],
98
95
  [{_key: 'k4'}],
99
96
  ])(snapshot),
100
- ).toEqual([{index: 1}, {index: 2}, undefined, {index: 1}, {index: 2}])
97
+ ).toEqual([1, 2, undefined, 1, 2])
101
98
  })
102
99
 
103
100
  test('two lists broken up by an image', () => {
@@ -117,7 +114,7 @@ describe(getListState.name, () => {
117
114
  [{_key: 'k3'}],
118
115
  [{_key: 'k4'}],
119
116
  ])(snapshot),
120
- ).toEqual([{index: 1}, {index: 2}, undefined, {index: 1}, {index: 2}])
117
+ ).toEqual([1, 2, undefined, 1, 2])
121
118
  })
122
119
 
123
120
  test('numbered lists broken up by a bulleted list', () => {
@@ -129,7 +126,7 @@ describe(getListState.name, () => {
129
126
 
130
127
  expect(
131
128
  getListStates([[{_key: 'k0'}], [{_key: 'k1'}], [{_key: 'k2'}]])(snapshot),
132
- ).toEqual([{index: 1}, {index: 1}, {index: 1}])
129
+ ).toEqual([1, 1, 1])
133
130
  })
134
131
 
135
132
  test('simple indented list', () => {
@@ -147,7 +144,7 @@ describe(getListState.name, () => {
147
144
  [{_key: 'k2'}],
148
145
  [{_key: 'k3'}],
149
146
  ])(snapshot),
150
- ).toEqual([{index: 1}, {index: 1}, {index: 2}, {index: 2}])
147
+ ).toEqual([1, 1, 2, 2])
151
148
  })
152
149
 
153
150
  test('reverse indented list', () => {
@@ -159,7 +156,7 @@ describe(getListState.name, () => {
159
156
 
160
157
  expect(
161
158
  getListStates([[{_key: 'k0'}], [{_key: 'k1'}], [{_key: 'k2'}]])(snapshot),
162
- ).toEqual([{index: 1}, {index: 1}, {index: 1}])
159
+ ).toEqual([1, 1, 1])
163
160
  })
164
161
 
165
162
  test('complex list', () => {
@@ -187,16 +184,6 @@ describe(getListState.name, () => {
187
184
  [{_key: 'k7'}],
188
185
  [{_key: 'k8'}],
189
186
  ])(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
- ])
187
+ ).toEqual([1, 1, 1, 1, 2, 1, 1, 2, 3])
201
188
  })
202
189
  })
@@ -6,22 +6,14 @@ import {getFocusTextBlock, getPreviousBlock} from './selectors'
6
6
 
7
7
  /**
8
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
9
+ * Given the `path` of a block, this selector will return the "list index" of
18
10
  * the block.
19
11
  */
20
- export function getListState({
12
+ export function getListIndex({
21
13
  path,
22
14
  }: {
23
15
  path: BlockPath
24
- }): EditorSelector<ListState | undefined> {
16
+ }): EditorSelector<number | undefined> {
25
17
  return (snapshot) => {
26
18
  const selection = {
27
19
  anchor: {
@@ -65,39 +57,29 @@ export function getListState({
65
57
  })
66
58
 
67
59
  if (!previousListItem) {
68
- return {
69
- index: 1,
70
- }
60
+ return 1
71
61
  }
72
62
 
73
63
  if (previousListItem.node.listItem !== focusTextBlock.node.listItem) {
74
- return {
75
- index: 1,
76
- }
64
+ return 1
77
65
  }
78
66
 
79
67
  if (
80
68
  previousListItem.node.level !== undefined &&
81
69
  previousListItem.node.level < focusTextBlock.node.level
82
70
  ) {
83
- return {
84
- index: 1,
85
- }
71
+ return 1
86
72
  }
87
73
 
88
- const previousListItemListState = getListState({
74
+ const previousListItemListState = getListIndex({
89
75
  path: previousListItem.path,
90
76
  })(snapshot)
91
77
 
92
78
  if (previousListItemListState === undefined) {
93
- return {
94
- index: 1,
95
- }
79
+ return 1
96
80
  }
97
81
 
98
- return {
99
- index: previousListItemListState.index + 1,
100
- }
82
+ return previousListItemListState + 1
101
83
  }
102
84
  }
103
85