@portabletext/editor 1.34.1 → 1.35.1
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-cjs/behavior.core.cjs +57 -118
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/behavior.markdown.cjs +27 -67
- package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
- package/lib/_chunks-cjs/{plugin.event-listener.cjs → editor-provider.cjs} +101 -87
- package/lib/_chunks-cjs/editor-provider.cjs.map +1 -0
- package/lib/_chunks-cjs/selector.get-text-before.cjs +5 -7
- package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.is-active-style.cjs +22 -36
- package/lib/_chunks-cjs/selector.is-active-style.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs +68 -153
- package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs.map +1 -1
- package/lib/_chunks-cjs/util.block-offsets-to-selection.cjs.map +1 -1
- package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
- package/lib/_chunks-es/behavior.core.js +57 -118
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/behavior.markdown.js +27 -67
- package/lib/_chunks-es/behavior.markdown.js.map +1 -1
- package/lib/_chunks-es/{plugin.event-listener.js → editor-provider.js} +102 -88
- package/lib/_chunks-es/editor-provider.js.map +1 -0
- package/lib/_chunks-es/selector.get-text-before.js +5 -7
- package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
- package/lib/_chunks-es/selector.is-active-style.js +22 -36
- package/lib/_chunks-es/selector.is-active-style.js.map +1 -1
- package/lib/_chunks-es/selector.is-at-the-start-of-block.js +68 -153
- package/lib/_chunks-es/selector.is-at-the-start-of-block.js.map +1 -1
- package/lib/_chunks-es/util.block-offsets-to-selection.js.map +1 -1
- package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
- package/lib/behaviors/index.cjs +18 -48
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +19392 -214
- package/lib/behaviors/index.d.ts +19392 -214
- package/lib/behaviors/index.js +18 -48
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +81 -51
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +334 -59
- package/lib/index.d.ts +334 -59
- package/lib/index.js +35 -4
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.cjs +200 -189
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.d.cts +344 -25
- package/lib/plugins/index.d.ts +344 -25
- package/lib/plugins/index.js +198 -187
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.cjs +22 -50
- package/lib/selectors/index.cjs.map +1 -1
- package/lib/selectors/index.d.cts +19485 -174
- package/lib/selectors/index.d.ts +19485 -174
- package/lib/selectors/index.js +22 -50
- package/lib/selectors/index.js.map +1 -1
- package/lib/utils/index.cjs.map +1 -1
- package/lib/utils/index.d.cts +19518 -7
- package/lib/utils/index.d.ts +19518 -7
- package/lib/utils/index.js.map +1 -1
- package/package.json +7 -7
- package/src/behavior-actions/behavior.action.decorator.add.ts +1 -0
- package/src/behavior-actions/behavior.action.delete.text.ts +1 -0
- package/src/behaviors/behavior.code-editor.ts +6 -6
- package/src/behaviors/behavior.core.annotations.ts +5 -4
- package/src/behaviors/behavior.core.block-objects.ts +17 -17
- package/src/behaviors/behavior.core.decorators.ts +12 -8
- package/src/behaviors/behavior.core.insert-break.ts +27 -29
- package/src/behaviors/behavior.core.lists.ts +19 -19
- package/src/behaviors/behavior.decorator-pair.ts +201 -0
- package/src/behaviors/behavior.default.ts +35 -30
- package/src/behaviors/behavior.emoji-picker.ts +12 -12
- package/src/behaviors/behavior.links.ts +7 -7
- package/src/behaviors/behavior.markdown.ts +41 -42
- package/src/behaviors/behavior.types.ts +14 -17
- package/src/behaviors/index.ts +0 -1
- package/src/converters/converter.json.ts +6 -6
- package/src/converters/converter.portable-text.deserialize.test.ts +27 -29
- package/src/converters/converter.portable-text.ts +13 -7
- package/src/converters/converter.text-html.deserialize.test.ts +16 -18
- package/src/converters/converter.text-html.serialize.test.ts +56 -57
- package/src/converters/converter.text-html.ts +14 -10
- package/src/converters/converter.text-plain.test.ts +17 -17
- package/src/converters/converter.text-plain.ts +15 -11
- package/src/converters/converter.types.ts +5 -5
- package/src/editor/Editable.tsx +26 -0
- package/src/editor/editor-machine.ts +170 -142
- package/src/editor/editor-selector.ts +3 -0
- package/src/editor/editor-snapshot.ts +13 -0
- package/src/editor-event-listener.tsx +30 -0
- package/src/index.ts +3 -3
- package/src/internal-utils/create-test-snapshot.ts +23 -0
- package/src/internal-utils/get-text-to-emphasize.ts +29 -7
- package/src/plugins/plugin.decorator-shortcut.ts +235 -0
- package/src/plugins/plugin.markdown.tsx +56 -8
- package/src/plugins/plugin.one-line.tsx +17 -17
- package/src/selectors/selector.get-active-annotations.test.ts +4 -13
- package/src/selectors/selector.get-active-list-item.ts +4 -4
- package/src/selectors/selector.get-active-style.ts +6 -6
- package/src/selectors/selector.get-anchor-block.ts +5 -5
- package/src/selectors/selector.get-anchor-child.ts +5 -5
- package/src/selectors/selector.get-anchor-span.ts +2 -2
- package/src/selectors/selector.get-anchor-text-block.ts +2 -2
- package/src/selectors/selector.get-block-offsets.ts +8 -7
- package/src/selectors/selector.get-caret-word-selection.test.ts +3 -7
- package/src/selectors/selector.get-caret-word-selection.ts +19 -16
- package/src/selectors/selector.get-next-inline-object.ts +4 -4
- package/src/selectors/selector.get-previous-inline-object.ts +4 -4
- package/src/selectors/selector.get-selected-slice.ts +7 -4
- package/src/selectors/selector.get-selected-spans.test.ts +5 -9
- package/src/selectors/selector.get-selected-spans.ts +9 -9
- package/src/selectors/selector.get-selection-end-point.ts +5 -5
- package/src/selectors/selector.get-selection-start-point.ts +5 -5
- package/src/selectors/selector.get-selection-text.test.ts +5 -7
- package/src/selectors/selector.get-selection-text.ts +2 -2
- package/src/selectors/selector.get-selection.ts +2 -2
- package/src/selectors/selector.get-text-before.ts +8 -8
- package/src/selectors/selector.get-trimmed-selection.test.ts +3 -5
- package/src/selectors/selector.get-trimmed-selection.ts +15 -13
- package/src/selectors/selector.get-value.ts +4 -4
- package/src/selectors/selector.is-active-decorator.test.ts +5 -9
- package/src/selectors/selector.is-at-the-end-of-block.ts +6 -3
- package/src/selectors/selector.is-at-the-start-of-block.ts +3 -3
- package/src/selectors/selector.is-overlapping-selection.ts +8 -6
- package/src/selectors/selector.is-selection-collapsed.ts +6 -5
- package/src/selectors/selector.is-selection-expanded.ts +2 -2
- package/src/selectors/selectors.ts +59 -59
- package/src/types/block-offset.ts +9 -0
- package/src/utils/index.ts +0 -1
- package/src/utils/util.block-offset.ts +1 -1
- package/src/utils/util.block-offsets-to-selection.ts +1 -1
- package/src/utils/util.child-selection-point-to-block-offset.ts +1 -1
- package/lib/_chunks-cjs/plugin.event-listener.cjs.map +0 -1
- package/lib/_chunks-es/plugin.event-listener.js.map +0 -1
- package/src/behaviors/behavior.markdown-emphasis.ts +0 -437
|
@@ -4,19 +4,17 @@ import {
|
|
|
4
4
|
defineSchema,
|
|
5
5
|
type SchemaDefinition,
|
|
6
6
|
} from '../editor/define-schema'
|
|
7
|
-
import {
|
|
7
|
+
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
8
8
|
import {converterTextHtml} from './converter.text-html'
|
|
9
9
|
import {coreConverters} from './converters.core'
|
|
10
10
|
|
|
11
|
-
function
|
|
12
|
-
return {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
value: [],
|
|
19
|
-
}
|
|
11
|
+
function createSnapshot(schema: SchemaDefinition) {
|
|
12
|
+
return createTestSnapshot({
|
|
13
|
+
context: {
|
|
14
|
+
converters: coreConverters,
|
|
15
|
+
schema: compileSchemaDefinition(schema),
|
|
16
|
+
},
|
|
17
|
+
})
|
|
20
18
|
}
|
|
21
19
|
|
|
22
20
|
const decoratedParagraph =
|
|
@@ -31,7 +29,7 @@ describe(converterTextHtml.deserialize.name, () => {
|
|
|
31
29
|
test('paragraph with unknown decorators', () => {
|
|
32
30
|
expect(
|
|
33
31
|
converterTextHtml.deserialize({
|
|
34
|
-
|
|
32
|
+
snapshot: createSnapshot(defineSchema({})),
|
|
35
33
|
event: {
|
|
36
34
|
type: 'deserialize',
|
|
37
35
|
data: decoratedParagraph,
|
|
@@ -60,7 +58,7 @@ describe(converterTextHtml.deserialize.name, () => {
|
|
|
60
58
|
test('paragraph with known decorators', () => {
|
|
61
59
|
expect(
|
|
62
60
|
converterTextHtml.deserialize({
|
|
63
|
-
|
|
61
|
+
snapshot: createSnapshot(
|
|
64
62
|
defineSchema({
|
|
65
63
|
decorators: [{name: 'strong'}, {name: 'em'}, {name: 'code'}],
|
|
66
64
|
}),
|
|
@@ -105,7 +103,7 @@ describe(converterTextHtml.deserialize.name, () => {
|
|
|
105
103
|
test('image', () => {
|
|
106
104
|
expect(
|
|
107
105
|
converterTextHtml.deserialize({
|
|
108
|
-
|
|
106
|
+
snapshot: createSnapshot(
|
|
109
107
|
defineSchema({
|
|
110
108
|
blockObjects: [{name: 'image'}],
|
|
111
109
|
}),
|
|
@@ -123,7 +121,7 @@ describe(converterTextHtml.deserialize.name, () => {
|
|
|
123
121
|
test('paragraph with unknown link', () => {
|
|
124
122
|
expect(
|
|
125
123
|
converterTextHtml.deserialize({
|
|
126
|
-
|
|
124
|
+
snapshot: createSnapshot(defineSchema({})),
|
|
127
125
|
event: {
|
|
128
126
|
type: 'deserialize',
|
|
129
127
|
data: paragraphWithLink,
|
|
@@ -152,7 +150,7 @@ describe(converterTextHtml.deserialize.name, () => {
|
|
|
152
150
|
test('paragraph with known link', () => {
|
|
153
151
|
expect(
|
|
154
152
|
converterTextHtml.deserialize({
|
|
155
|
-
|
|
153
|
+
snapshot: createSnapshot(
|
|
156
154
|
defineSchema({
|
|
157
155
|
annotations: [{name: 'link'}],
|
|
158
156
|
}),
|
|
@@ -197,7 +195,7 @@ describe(converterTextHtml.deserialize.name, () => {
|
|
|
197
195
|
test('unordered list', () => {
|
|
198
196
|
expect(
|
|
199
197
|
converterTextHtml.deserialize({
|
|
200
|
-
|
|
198
|
+
snapshot: createSnapshot(
|
|
201
199
|
defineSchema({
|
|
202
200
|
lists: [{name: 'bullet'}],
|
|
203
201
|
}),
|
|
@@ -248,7 +246,7 @@ describe(converterTextHtml.deserialize.name, () => {
|
|
|
248
246
|
test('ordered list', () => {
|
|
249
247
|
expect(
|
|
250
248
|
converterTextHtml.deserialize({
|
|
251
|
-
|
|
249
|
+
snapshot: createSnapshot(
|
|
252
250
|
defineSchema({
|
|
253
251
|
lists: [{name: 'number'}],
|
|
254
252
|
}),
|
|
@@ -299,7 +297,7 @@ describe(converterTextHtml.deserialize.name, () => {
|
|
|
299
297
|
test('nested list', () => {
|
|
300
298
|
expect(
|
|
301
299
|
converterTextHtml.deserialize({
|
|
302
|
-
|
|
300
|
+
snapshot: createSnapshot(
|
|
303
301
|
defineSchema({
|
|
304
302
|
lists: [{name: 'bullet'}, {name: 'number'}],
|
|
305
303
|
}),
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
defineSchema,
|
|
6
6
|
type SchemaDefinition,
|
|
7
7
|
} from '../editor/define-schema'
|
|
8
|
-
import {
|
|
8
|
+
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
9
9
|
import type {EditorSelection} from '../utils'
|
|
10
10
|
import {converterTextHtml} from './converter.text-html'
|
|
11
11
|
import {coreConverters} from './converters.core'
|
|
@@ -75,22 +75,22 @@ const paragraphWithInlineBlock: PortableTextTextBlock = {
|
|
|
75
75
|
],
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
function
|
|
79
|
-
return {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
78
|
+
function createSnapshot(schema: SchemaDefinition, selection: EditorSelection) {
|
|
79
|
+
return createTestSnapshot({
|
|
80
|
+
context: {
|
|
81
|
+
converters: coreConverters,
|
|
82
|
+
schema: compileSchemaDefinition(schema),
|
|
83
|
+
selection,
|
|
84
|
+
value: [decoratedParagraph, image, b2, paragraphWithInlineBlock],
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
describe(converterTextHtml.serialize.name, () => {
|
|
90
90
|
test('paragraph with decorators', () => {
|
|
91
91
|
expect(
|
|
92
92
|
converterTextHtml.serialize({
|
|
93
|
-
|
|
93
|
+
snapshot: createSnapshot(defineSchema({}), {
|
|
94
94
|
anchor: {
|
|
95
95
|
path: [
|
|
96
96
|
{_key: decoratedParagraph._key},
|
|
@@ -121,7 +121,7 @@ describe(converterTextHtml.serialize.name, () => {
|
|
|
121
121
|
test('image', () => {
|
|
122
122
|
expect(
|
|
123
123
|
converterTextHtml.serialize({
|
|
124
|
-
|
|
124
|
+
snapshot: createSnapshot(defineSchema({}), {
|
|
125
125
|
anchor: {
|
|
126
126
|
path: [{_key: image._key}],
|
|
127
127
|
offset: 0,
|
|
@@ -144,7 +144,7 @@ describe(converterTextHtml.serialize.name, () => {
|
|
|
144
144
|
test('inline object', () => {
|
|
145
145
|
expect(
|
|
146
146
|
converterTextHtml.serialize({
|
|
147
|
-
|
|
147
|
+
snapshot: createSnapshot(defineSchema({}), {
|
|
148
148
|
anchor: {
|
|
149
149
|
path: [
|
|
150
150
|
{_key: paragraphWithInlineBlock._key},
|
|
@@ -175,52 +175,51 @@ describe(converterTextHtml.serialize.name, () => {
|
|
|
175
175
|
test('lists', () => {
|
|
176
176
|
expect(
|
|
177
177
|
converterTextHtml.serialize({
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
offset: 3,
|
|
178
|
+
snapshot: createTestSnapshot({
|
|
179
|
+
context: {
|
|
180
|
+
converters: coreConverters,
|
|
181
|
+
value: [
|
|
182
|
+
{
|
|
183
|
+
_key: 'k0',
|
|
184
|
+
_type: 'block',
|
|
185
|
+
children: [
|
|
186
|
+
{
|
|
187
|
+
_key: 'k1',
|
|
188
|
+
_type: 'span',
|
|
189
|
+
marks: [],
|
|
190
|
+
text: 'foo',
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
level: 1,
|
|
194
|
+
listItem: 'number',
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
_key: 'k2',
|
|
198
|
+
_type: 'block',
|
|
199
|
+
children: [
|
|
200
|
+
{
|
|
201
|
+
_key: 'k3',
|
|
202
|
+
_type: 'span',
|
|
203
|
+
marks: [],
|
|
204
|
+
text: 'bar',
|
|
205
|
+
},
|
|
206
|
+
],
|
|
207
|
+
level: 2,
|
|
208
|
+
listItem: 'bullet',
|
|
209
|
+
},
|
|
210
|
+
],
|
|
211
|
+
selection: {
|
|
212
|
+
anchor: {
|
|
213
|
+
path: [{_key: 'k0'}, 'children', {_key: 'k1'}],
|
|
214
|
+
offset: 0,
|
|
215
|
+
},
|
|
216
|
+
focus: {
|
|
217
|
+
path: [{_key: 'k2'}, 'children', {_key: 'k3'}],
|
|
218
|
+
offset: 3,
|
|
219
|
+
},
|
|
221
220
|
},
|
|
222
221
|
},
|
|
223
|
-
},
|
|
222
|
+
}),
|
|
224
223
|
event: {
|
|
225
224
|
type: 'serialize',
|
|
226
225
|
originEvent: 'unknown',
|
|
@@ -6,8 +6,8 @@ import {defineConverter} from './converter.types'
|
|
|
6
6
|
|
|
7
7
|
export const converterTextHtml = defineConverter({
|
|
8
8
|
mimeType: 'text/html',
|
|
9
|
-
serialize: ({
|
|
10
|
-
if (!context.selection) {
|
|
9
|
+
serialize: ({snapshot, event}) => {
|
|
10
|
+
if (!snapshot.context.selection) {
|
|
11
11
|
return {
|
|
12
12
|
type: 'serialization.failure',
|
|
13
13
|
mimeType: 'text/html',
|
|
@@ -17,8 +17,8 @@ export const converterTextHtml = defineConverter({
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
const blocks = sliceBlocks({
|
|
20
|
-
blocks: context.value,
|
|
21
|
-
selection: context.selection,
|
|
20
|
+
blocks: snapshot.context.value,
|
|
21
|
+
selection: snapshot.context.selection,
|
|
22
22
|
})
|
|
23
23
|
|
|
24
24
|
const html = toHTML(blocks, {
|
|
@@ -45,12 +45,16 @@ export const converterTextHtml = defineConverter({
|
|
|
45
45
|
originEvent: event.originEvent,
|
|
46
46
|
}
|
|
47
47
|
},
|
|
48
|
-
deserialize: ({
|
|
49
|
-
const blocks = htmlToBlocks(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
deserialize: ({snapshot, event}) => {
|
|
49
|
+
const blocks = htmlToBlocks(
|
|
50
|
+
event.data,
|
|
51
|
+
snapshot.context.schema.portableText,
|
|
52
|
+
{
|
|
53
|
+
keyGenerator: snapshot.context.keyGenerator,
|
|
54
|
+
unstable_whitespaceOnPasteMode:
|
|
55
|
+
snapshot.context.schema.block.options.unstable_whitespaceOnPasteMode,
|
|
56
|
+
},
|
|
57
|
+
) as Array<PortableTextBlock>
|
|
54
58
|
|
|
55
59
|
return {
|
|
56
60
|
type: 'deserialization.success',
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
defineSchema,
|
|
6
6
|
type SchemaDefinition,
|
|
7
7
|
} from '../editor/define-schema'
|
|
8
|
-
import
|
|
8
|
+
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
9
9
|
import type {EditorSelection} from '../utils'
|
|
10
10
|
import {converterTextPlain} from './converter.text-plain'
|
|
11
11
|
import {coreConverters} from './converters.core'
|
|
@@ -65,27 +65,27 @@ const b4: PortableTextTextBlock = {
|
|
|
65
65
|
],
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
function
|
|
68
|
+
function createSnapshot({
|
|
69
69
|
schema,
|
|
70
70
|
selection,
|
|
71
71
|
}: {
|
|
72
72
|
schema: SchemaDefinition
|
|
73
73
|
selection: EditorSelection
|
|
74
|
-
})
|
|
75
|
-
return {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
74
|
+
}) {
|
|
75
|
+
return createTestSnapshot({
|
|
76
|
+
context: {
|
|
77
|
+
converters: coreConverters,
|
|
78
|
+
schema: compileSchemaDefinition(schema),
|
|
79
|
+
selection,
|
|
80
|
+
value: [b1, b2, b3, b4],
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
test(converterTextPlain.serialize.name, () => {
|
|
86
86
|
expect(
|
|
87
87
|
converterTextPlain.serialize({
|
|
88
|
-
|
|
88
|
+
snapshot: createSnapshot({
|
|
89
89
|
schema: defineSchema({}),
|
|
90
90
|
selection: {
|
|
91
91
|
anchor: {
|
|
@@ -109,7 +109,7 @@ test(converterTextPlain.serialize.name, () => {
|
|
|
109
109
|
|
|
110
110
|
expect(
|
|
111
111
|
converterTextPlain.serialize({
|
|
112
|
-
|
|
112
|
+
snapshot: createSnapshot({
|
|
113
113
|
schema: defineSchema({}),
|
|
114
114
|
selection: {
|
|
115
115
|
anchor: {
|
|
@@ -133,7 +133,7 @@ test(converterTextPlain.serialize.name, () => {
|
|
|
133
133
|
|
|
134
134
|
expect(
|
|
135
135
|
converterTextPlain.serialize({
|
|
136
|
-
|
|
136
|
+
snapshot: createSnapshot({
|
|
137
137
|
schema: defineSchema({}),
|
|
138
138
|
selection: {
|
|
139
139
|
anchor: {
|
|
@@ -157,7 +157,7 @@ test(converterTextPlain.serialize.name, () => {
|
|
|
157
157
|
|
|
158
158
|
expect(
|
|
159
159
|
converterTextPlain.serialize({
|
|
160
|
-
|
|
160
|
+
snapshot: createSnapshot({
|
|
161
161
|
schema: defineSchema({
|
|
162
162
|
blockObjects: [
|
|
163
163
|
{
|
|
@@ -187,7 +187,7 @@ test(converterTextPlain.serialize.name, () => {
|
|
|
187
187
|
|
|
188
188
|
expect(
|
|
189
189
|
converterTextPlain.serialize({
|
|
190
|
-
|
|
190
|
+
snapshot: createSnapshot({
|
|
191
191
|
schema: defineSchema({}),
|
|
192
192
|
selection: {
|
|
193
193
|
anchor: {
|
|
@@ -211,7 +211,7 @@ test(converterTextPlain.serialize.name, () => {
|
|
|
211
211
|
|
|
212
212
|
expect(
|
|
213
213
|
converterTextPlain.serialize({
|
|
214
|
-
|
|
214
|
+
snapshot: createSnapshot({
|
|
215
215
|
schema: defineSchema({
|
|
216
216
|
inlineObjects: [
|
|
217
217
|
{
|
|
@@ -5,8 +5,8 @@ import {defineConverter} from './converter.types'
|
|
|
5
5
|
|
|
6
6
|
export const converterTextPlain = defineConverter({
|
|
7
7
|
mimeType: 'text/plain',
|
|
8
|
-
serialize: ({
|
|
9
|
-
if (!context.selection) {
|
|
8
|
+
serialize: ({snapshot, event}) => {
|
|
9
|
+
if (!snapshot.context.selection) {
|
|
10
10
|
return {
|
|
11
11
|
type: 'serialization.failure',
|
|
12
12
|
mimeType: 'text/plain',
|
|
@@ -16,8 +16,8 @@ export const converterTextPlain = defineConverter({
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
const blocks = sliceBlocks({
|
|
19
|
-
blocks: context.value,
|
|
20
|
-
selection: context.selection,
|
|
19
|
+
blocks: snapshot.context.value,
|
|
20
|
+
selection: snapshot.context.selection,
|
|
21
21
|
})
|
|
22
22
|
|
|
23
23
|
const data = blocks
|
|
@@ -25,12 +25,12 @@ export const converterTextPlain = defineConverter({
|
|
|
25
25
|
if (isPortableTextTextBlock(block)) {
|
|
26
26
|
return block.children
|
|
27
27
|
.map((child) => {
|
|
28
|
-
if (child._type === context.schema.span.name) {
|
|
28
|
+
if (child._type === snapshot.context.schema.span.name) {
|
|
29
29
|
return child.text
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
return `[${
|
|
33
|
-
context.schema.inlineObjects.find(
|
|
33
|
+
snapshot.context.schema.inlineObjects.find(
|
|
34
34
|
(inlineObjectType) => inlineObjectType.name === child._type,
|
|
35
35
|
)?.title ?? 'Object'
|
|
36
36
|
}]`
|
|
@@ -39,7 +39,7 @@ export const converterTextPlain = defineConverter({
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
return `[${
|
|
42
|
-
context.schema.blockObjects.find(
|
|
42
|
+
snapshot.context.schema.blockObjects.find(
|
|
43
43
|
(blockObjectType) => blockObjectType.name === block._type,
|
|
44
44
|
)?.title ?? 'Object'
|
|
45
45
|
}]`
|
|
@@ -53,7 +53,7 @@ export const converterTextPlain = defineConverter({
|
|
|
53
53
|
originEvent: event.originEvent,
|
|
54
54
|
}
|
|
55
55
|
},
|
|
56
|
-
deserialize: ({
|
|
56
|
+
deserialize: ({snapshot, event}) => {
|
|
57
57
|
const html = escapeHtml(event.data)
|
|
58
58
|
.split(/\n{2,}/)
|
|
59
59
|
.map((line) =>
|
|
@@ -63,9 +63,13 @@ export const converterTextPlain = defineConverter({
|
|
|
63
63
|
|
|
64
64
|
const textToHtml = `<html><body>${html}</body></html>`
|
|
65
65
|
|
|
66
|
-
const blocks = htmlToBlocks(
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
const blocks = htmlToBlocks(
|
|
67
|
+
textToHtml,
|
|
68
|
+
snapshot.context.schema.portableText,
|
|
69
|
+
{
|
|
70
|
+
keyGenerator: snapshot.context.keyGenerator,
|
|
71
|
+
},
|
|
72
|
+
) as Array<PortableTextBlock>
|
|
69
73
|
|
|
70
74
|
return {
|
|
71
75
|
type: 'deserialization.success',
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type {PortableTextBlock} from '@sanity/types'
|
|
2
|
-
import type {
|
|
2
|
+
import type {EditorSnapshot} from '../editor/editor-snapshot'
|
|
3
3
|
import type {MIMEType} from '../internal-utils/mime-type'
|
|
4
4
|
import type {PickFromUnion} from '../type-utils'
|
|
5
5
|
|
|
@@ -48,10 +48,10 @@ export type ConverterEvent<TMIMEType extends MIMEType = MIMEType> =
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
export type Serializer<TMIMEType extends MIMEType> = ({
|
|
51
|
-
|
|
51
|
+
snapshot,
|
|
52
52
|
event,
|
|
53
53
|
}: {
|
|
54
|
-
|
|
54
|
+
snapshot: EditorSnapshot
|
|
55
55
|
event: PickFromUnion<ConverterEvent<TMIMEType>, 'type', 'serialize'>
|
|
56
56
|
}) => PickFromUnion<
|
|
57
57
|
ConverterEvent<TMIMEType>,
|
|
@@ -60,10 +60,10 @@ export type Serializer<TMIMEType extends MIMEType> = ({
|
|
|
60
60
|
>
|
|
61
61
|
|
|
62
62
|
export type Deserializer<TMIMEType extends MIMEType> = ({
|
|
63
|
-
|
|
63
|
+
snapshot,
|
|
64
64
|
event,
|
|
65
65
|
}: {
|
|
66
|
-
|
|
66
|
+
snapshot: EditorSnapshot
|
|
67
67
|
event: PickFromUnion<ConverterEvent<TMIMEType>, 'type', 'deserialize'>
|
|
68
68
|
}) => PickFromUnion<
|
|
69
69
|
ConverterEvent<TMIMEType>,
|
package/src/editor/Editable.tsx
CHANGED
|
@@ -806,6 +806,25 @@ export const PortableTextEditable = forwardRef<
|
|
|
806
806
|
setEditableElement(ref.current)
|
|
807
807
|
}, [slateEditor, ref])
|
|
808
808
|
|
|
809
|
+
useEffect(() => {
|
|
810
|
+
const window = ReactEditor.getWindow(slateEditor)
|
|
811
|
+
|
|
812
|
+
const onDragEnd = () => {
|
|
813
|
+
editorActor.send({type: 'dragend'})
|
|
814
|
+
}
|
|
815
|
+
const onDrop = () => {
|
|
816
|
+
editorActor.send({type: 'drop'})
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
window.document.addEventListener('dragend', onDragEnd)
|
|
820
|
+
window.document.addEventListener('drop', onDrop)
|
|
821
|
+
|
|
822
|
+
return () => {
|
|
823
|
+
window.document.removeEventListener('dragend', onDragEnd)
|
|
824
|
+
window.document.removeEventListener('drop', onDrop)
|
|
825
|
+
}
|
|
826
|
+
}, [slateEditor, editorActor])
|
|
827
|
+
|
|
809
828
|
if (!portableTextEditor) {
|
|
810
829
|
return null
|
|
811
830
|
}
|
|
@@ -819,6 +838,13 @@ export const PortableTextEditable = forwardRef<
|
|
|
819
838
|
onCopy={handleCopy}
|
|
820
839
|
onClick={handleClick}
|
|
821
840
|
onDOMBeforeInput={handleOnBeforeInput}
|
|
841
|
+
onDragStart={(event) => {
|
|
842
|
+
props.onDragStart?.(event)
|
|
843
|
+
|
|
844
|
+
if (!event.isDefaultPrevented() && !event.isPropagationStopped()) {
|
|
845
|
+
editorActor.send({type: 'dragstart'})
|
|
846
|
+
}
|
|
847
|
+
}}
|
|
822
848
|
onFocus={handleOnFocus}
|
|
823
849
|
onKeyDown={handleKeyDown}
|
|
824
850
|
onKeyUp={handleKeyUp}
|