@portabletext/editor 3.0.3 → 3.0.5

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.
@@ -24,17 +24,6 @@ function keepObjectEquality(
24
24
  return object
25
25
  }
26
26
 
27
- export function toSlateValue(
28
- value: PortableTextBlock[] | undefined,
29
- {schemaTypes}: {schemaTypes: EditorSchema},
30
- keyMap: Record<string, any> = {},
31
- ): Descendant[] {
32
- if (value && Array.isArray(value)) {
33
- return value.map((block) => toSlateBlock(block, {schemaTypes}, keyMap))
34
- }
35
- return []
36
- }
37
-
38
27
  export function toSlateBlock(
39
28
  block: PortableTextBlock,
40
29
  {schemaTypes}: {schemaTypes: EditorSchema},
@@ -141,51 +130,57 @@ export function fromSlateValue(
141
130
  textBlockType: string,
142
131
  keyMap: Record<string, PortableTextBlock | PortableTextChild> = {},
143
132
  ): PortableTextBlock[] {
144
- return value.map((block) => {
145
- const {_key, _type} = block
146
- if (!_key || !_type) {
147
- throw new Error('Not a valid block')
148
- }
149
- if (
150
- _type === textBlockType &&
151
- 'children' in block &&
152
- Array.isArray(block.children) &&
153
- _key
154
- ) {
155
- let hasInlines = false
156
- const children = block.children.map((child) => {
157
- const {_type: _cType} = child
158
- if ('value' in child && _cType !== 'span') {
159
- hasInlines = true
160
- const {
161
- value: v,
162
- _key: k,
163
- _type: t,
164
- __inline: _i,
165
- children: _c,
166
- ...rest
167
- } = child
168
- return keepObjectEquality(
169
- {...rest, ...v, _key: k as string, _type: t as string},
170
- keyMap,
171
- )
172
- }
173
- return child
174
- })
175
- if (!hasInlines) {
176
- return block as PortableTextBlock // Original object
133
+ return value.map((block) => fromSlateBlock(block, textBlockType, keyMap))
134
+ }
135
+
136
+ export function fromSlateBlock(
137
+ block: Descendant,
138
+ textBlockType: string,
139
+ keyMap: Record<string, PortableTextBlock | PortableTextChild> = {},
140
+ ) {
141
+ const {_key, _type} = block
142
+ if (!_key || !_type) {
143
+ throw new Error('Not a valid block')
144
+ }
145
+ if (
146
+ _type === textBlockType &&
147
+ 'children' in block &&
148
+ Array.isArray(block.children) &&
149
+ _key
150
+ ) {
151
+ let hasInlines = false
152
+ const children = block.children.map((child) => {
153
+ const {_type: _cType} = child
154
+ if ('value' in child && _cType !== 'span') {
155
+ hasInlines = true
156
+ const {
157
+ value: v,
158
+ _key: k,
159
+ _type: t,
160
+ __inline: _i,
161
+ children: _c,
162
+ ...rest
163
+ } = child
164
+ return keepObjectEquality(
165
+ {...rest, ...v, _key: k as string, _type: t as string},
166
+ keyMap,
167
+ )
177
168
  }
178
- return keepObjectEquality(
179
- {...block, children, _key, _type},
180
- keyMap,
181
- ) as PortableTextBlock
169
+ return child
170
+ })
171
+ if (!hasInlines) {
172
+ return block as PortableTextBlock // Original object
182
173
  }
183
- const blockValue = 'value' in block && block.value
184
174
  return keepObjectEquality(
185
- {_key, _type, ...(typeof blockValue === 'object' ? blockValue : {})},
175
+ {...block, children, _key, _type},
186
176
  keyMap,
187
177
  ) as PortableTextBlock
188
- })
178
+ }
179
+ const blockValue = 'value' in block && block.value
180
+ return keepObjectEquality(
181
+ {_key, _type, ...(typeof blockValue === 'object' ? blockValue : {})},
182
+ keyMap,
183
+ ) as PortableTextBlock
189
184
  }
190
185
 
191
186
  export function isEqualToEmptyEditor(
@@ -3,7 +3,7 @@ import {omit} from 'lodash'
3
3
  import {Editor, Transforms} from 'slate'
4
4
  import {KEY_TO_VALUE_ELEMENT} from '../editor/weakMaps'
5
5
  import {toSlateRange} from '../internal-utils/to-slate-range'
6
- import {fromSlateValue} from '../internal-utils/values'
6
+ import {fromSlateBlock} from '../internal-utils/values'
7
7
  import {parseBlock} from '../utils/parse-blocks'
8
8
  import type {BehaviorOperationImplementation} from './behavior.operations'
9
9
 
@@ -35,11 +35,11 @@ export const blockUnsetOperationImplementation: BehaviorOperationImplementation<
35
35
  throw new Error(`Unable to find block at ${JSON.stringify(operation.at)}`)
36
36
  }
37
37
 
38
- const parsedBlock = fromSlateValue(
39
- [block],
38
+ const parsedBlock = fromSlateBlock(
39
+ block,
40
40
  context.schema.block.name,
41
41
  KEY_TO_VALUE_ELEMENT.get(operation.editor),
42
- ).at(0)
42
+ )
43
43
 
44
44
  if (!parsedBlock) {
45
45
  throw new Error(`Unable to parse block at ${JSON.stringify(operation.at)}`)