@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.
- package/lib/_chunks-dts/index.d.ts +11 -11
- package/lib/index.js +74 -74
- package/lib/index.js.map +1 -1
- package/package.json +12 -12
- package/src/editor/PortableTextEditor.tsx +1 -1
- package/src/editor/create-slate-editor.tsx +6 -4
- package/src/editor/plugins/createWithEditableAPI.ts +17 -12
- package/src/internal-utils/__tests__/values.test.ts +85 -107
- package/src/internal-utils/applyPatch.ts +17 -11
- package/src/internal-utils/operation-to-patches.ts +20 -27
- package/src/internal-utils/slate-utils.ts +2 -2
- package/src/internal-utils/values.ts +46 -51
- package/src/operations/behavior.operation.block.unset.ts +4 -4
|
@@ -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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
)
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
-
{
|
|
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 {
|
|
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 =
|
|
39
|
-
|
|
38
|
+
const parsedBlock = fromSlateBlock(
|
|
39
|
+
block,
|
|
40
40
|
context.schema.block.name,
|
|
41
41
|
KEY_TO_VALUE_ELEMENT.get(operation.editor),
|
|
42
|
-
)
|
|
42
|
+
)
|
|
43
43
|
|
|
44
44
|
if (!parsedBlock) {
|
|
45
45
|
throw new Error(`Unable to parse block at ${JSON.stringify(operation.at)}`)
|