@portabletext/editor 1.40.0 → 1.40.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.40.0",
3
+ "version": "1.40.2",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -85,7 +85,7 @@
85
85
  "devDependencies": {
86
86
  "@portabletext/toolkit": "^2.0.17",
87
87
  "@sanity/diff-match-patch": "^3.2.0",
88
- "@sanity/pkg-utils": "^7.1.0",
88
+ "@sanity/pkg-utils": "^7.1.1",
89
89
  "@sanity/schema": "^3.79.0",
90
90
  "@sanity/types": "^3.79.0",
91
91
  "@testing-library/jest-dom": "^6.6.3",
@@ -22,6 +22,15 @@ export const converterPortableText = defineConverter({
22
22
  selection,
23
23
  })
24
24
 
25
+ if (blocks.length === 0) {
26
+ return {
27
+ type: 'serialization.failure',
28
+ mimeType: 'application/x-portable-text',
29
+ reason: 'No blocks serialized',
30
+ originEvent: event.originEvent,
31
+ }
32
+ }
33
+
25
34
  return {
26
35
  type: 'serialization.success',
27
36
  data: JSON.stringify(blocks),
@@ -128,7 +128,7 @@ test(converterTextPlain.serialize.name, () => {
128
128
  },
129
129
  }),
130
130
  ).toMatchObject({
131
- data: 'foobar\n\n[Object]\n\nbaz',
131
+ data: 'foobar\n\nbaz',
132
132
  })
133
133
 
134
134
  expect(
@@ -152,7 +152,7 @@ test(converterTextPlain.serialize.name, () => {
152
152
  },
153
153
  }),
154
154
  ).toMatchObject({
155
- data: '[Object]',
155
+ data: '',
156
156
  })
157
157
 
158
158
  expect(
@@ -182,7 +182,7 @@ test(converterTextPlain.serialize.name, () => {
182
182
  },
183
183
  }),
184
184
  ).toMatchObject({
185
- data: 'foobar\n\n[Image]\n\nbaz',
185
+ data: 'foobar\n\nbaz',
186
186
  })
187
187
 
188
188
  expect(
@@ -206,7 +206,7 @@ test(converterTextPlain.serialize.name, () => {
206
206
  },
207
207
  }),
208
208
  ).toMatchObject({
209
- data: 'fizz[Object]buzz',
209
+ data: 'fizzbuzz',
210
210
  })
211
211
 
212
212
  expect(
@@ -236,6 +236,6 @@ test(converterTextPlain.serialize.name, () => {
236
236
  },
237
237
  }),
238
238
  ).toMatchObject({
239
- data: 'fizz[Stock Ticker]buzz',
239
+ data: 'fizzbuzz',
240
240
  })
241
241
  })
@@ -25,28 +25,21 @@ export const converterTextPlain = defineConverter({
25
25
 
26
26
  const data = blocks
27
27
  .map((block) => {
28
- if (isPortableTextTextBlock(block)) {
29
- return block.children
30
- .map((child) => {
31
- if (child._type === snapshot.context.schema.span.name) {
32
- return child.text
33
- }
34
-
35
- return `[${
36
- snapshot.context.schema.inlineObjects.find(
37
- (inlineObjectType) => inlineObjectType.name === child._type,
38
- )?.title ?? 'Object'
39
- }]`
40
- })
41
- .join('')
28
+ if (!isPortableTextTextBlock(block)) {
29
+ return ''
42
30
  }
43
31
 
44
- return `[${
45
- snapshot.context.schema.blockObjects.find(
46
- (blockObjectType) => blockObjectType.name === block._type,
47
- )?.title ?? 'Object'
48
- }]`
32
+ return block.children
33
+ .map((child) => {
34
+ if (child._type === snapshot.context.schema.span.name) {
35
+ return child.text
36
+ }
37
+
38
+ return ''
39
+ })
40
+ .join('')
49
41
  })
42
+ .filter((block) => block !== '')
50
43
  .join('\n\n')
51
44
 
52
45
  return {
@@ -419,15 +419,10 @@ export const Element: FunctionComponent<ElementProps> = ({
419
419
  }
420
420
 
421
421
  return (
422
- <div
423
- key={element._key}
424
- {...attributes}
425
- className={className}
426
- draggable={!readOnly}
427
- >
422
+ <div key={element._key} {...attributes} className={className}>
428
423
  {dragPositionBlock === 'start' ? <DropIndicator /> : null}
429
424
  {children}
430
- <div ref={blockRef} contentEditable={false}>
425
+ <div ref={blockRef} contentEditable={false} draggable={!readOnly}>
431
426
  {renderedBlockFromProps ? (
432
427
  renderedBlockFromProps
433
428
  ) : (
@@ -122,12 +122,9 @@ export function createWithEventListeners(
122
122
  editorActor.send({
123
123
  type: 'behavior event',
124
124
  behaviorEvent: {
125
- type: 'deserialize',
125
+ type: 'input.*',
126
126
  originEvent: {
127
- type: 'input.*',
128
- originEvent: {
129
- dataTransfer,
130
- },
127
+ dataTransfer,
131
128
  },
132
129
  },
133
130
  editor,
@@ -215,4 +215,37 @@ describe(getSelectedSpans.name, () => {
215
215
  },
216
216
  ])
217
217
  })
218
+
219
+ test('selecting from block object to empty span', () => {
220
+ expect(
221
+ getSelectedSpans(
222
+ snapshot(
223
+ [
224
+ image,
225
+ baz,
226
+ {
227
+ _key: 'b4',
228
+ _type: 'block',
229
+ children: [{_key: 's4', _type: 'span', text: ''}],
230
+ },
231
+ ],
232
+ {
233
+ anchor: {
234
+ path: [{_key: 'b2'}],
235
+ offset: 0,
236
+ },
237
+ focus: {
238
+ path: [{_key: 'b4'}, 'children', {_key: 's4'}],
239
+ offset: 0,
240
+ },
241
+ },
242
+ ),
243
+ ),
244
+ ).toEqual([
245
+ {
246
+ node: {_key: 's3', _type: 'span', text: 'baz'},
247
+ path: [{_key: 'b3'}, 'children', {_key: 's3'}],
248
+ },
249
+ ])
250
+ })
218
251
  })
@@ -50,7 +50,13 @@ export const getSelectedSpans: EditorSelector<
50
50
  ? endPoint.path[2]._key
51
51
  : undefined
52
52
 
53
+ let startBlockFound = false
54
+
53
55
  for (const block of snapshot.context.value) {
56
+ if (block._key === startBlockKey) {
57
+ startBlockFound = true
58
+ }
59
+
54
60
  if (!isPortableTextTextBlock(block)) {
55
61
  continue
56
62
  }
@@ -126,7 +132,7 @@ export const getSelectedSpans: EditorSelector<
126
132
  break
127
133
  }
128
134
 
129
- if (selectedSpans.length > 0) {
135
+ if (startBlockFound) {
130
136
  for (const child of block.children) {
131
137
  if (!isPortableTextSpan(child)) {
132
138
  continue
@@ -1,13 +1,25 @@
1
1
  import {expect, test} from 'vitest'
2
- import type {EditorSelection} from '..'
2
+ import type {EditorSelection, PortableTextBlock} from '..'
3
3
  import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
4
4
  import {isActiveDecorator} from './selector.is-active-decorator'
5
5
 
6
6
  test(isActiveDecorator.name, () => {
7
- function snapshot(selection: EditorSelection) {
7
+ function snapshot(
8
+ value: Array<PortableTextBlock>,
9
+ selection: EditorSelection,
10
+ ) {
8
11
  return createTestSnapshot({
9
12
  context: {
10
- value: [
13
+ value,
14
+ selection,
15
+ },
16
+ })
17
+ }
18
+
19
+ expect(
20
+ isActiveDecorator('strong')(
21
+ snapshot(
22
+ [
11
23
  {
12
24
  _type: '_block',
13
25
  _key: 'b1',
@@ -26,37 +38,98 @@ test(isActiveDecorator.name, () => {
26
38
  ],
27
39
  },
28
40
  ],
29
- selection,
30
- },
31
- })
32
- }
41
+ {
42
+ anchor: {
43
+ path: [{_key: 'b1'}, 'children', {_key: 's1'}],
44
+ offset: 0,
45
+ },
46
+ focus: {
47
+ path: [{_key: 'b1'}, 'children', {_key: 's1'}],
48
+ offset: 3,
49
+ },
50
+ },
51
+ ),
52
+ ),
53
+ ).toBe(true)
33
54
 
34
55
  expect(
35
56
  isActiveDecorator('strong')(
36
- snapshot({
37
- anchor: {
38
- path: [{_key: 'b1'}, 'children', {_key: 's1'}],
39
- offset: 0,
40
- },
41
- focus: {
42
- path: [{_key: 'b1'}, 'children', {_key: 's1'}],
43
- offset: 3,
57
+ snapshot(
58
+ [
59
+ {
60
+ _type: '_block',
61
+ _key: 'b1',
62
+ children: [
63
+ {
64
+ _type: 'span',
65
+ _key: 's1',
66
+ text: 'foo',
67
+ marks: ['strong'],
68
+ },
69
+ {
70
+ _type: 'span',
71
+ _key: 's2',
72
+ text: 'bar',
73
+ },
74
+ ],
75
+ },
76
+ ],
77
+ {
78
+ anchor: {
79
+ path: [{_key: 'b1'}, 'children', {_key: 's1'}],
80
+ offset: 2,
81
+ },
82
+ focus: {
83
+ path: [{_key: 'b1'}, 'children', {_key: 's2'}],
84
+ offset: 3,
85
+ },
44
86
  },
45
- }),
87
+ ),
46
88
  ),
47
- ).toBe(true)
89
+ ).toBe(false)
90
+
48
91
  expect(
49
92
  isActiveDecorator('strong')(
50
- snapshot({
51
- anchor: {
52
- path: [{_key: 'b1'}, 'children', {_key: 's1'}],
53
- offset: 2,
54
- },
55
- focus: {
56
- path: [{_key: 'b1'}, 'children', {_key: 's2'}],
57
- offset: 3,
93
+ snapshot(
94
+ [
95
+ {_key: 'b0', _type: 'image'},
96
+ {
97
+ _type: '_block',
98
+ _key: 'b1',
99
+ children: [
100
+ {
101
+ _type: 'span',
102
+ _key: 's1',
103
+ text: 'foo',
104
+ marks: ['strong'],
105
+ },
106
+ {
107
+ _type: 'span',
108
+ _key: 's2',
109
+ text: 'bar',
110
+ marks: ['strong'],
111
+ },
112
+ ],
113
+ },
114
+ {
115
+ _key: 'b2',
116
+ _type: 'block',
117
+ children: [
118
+ {_key: 's3', _type: 'span', text: '', marks: ['strong']},
119
+ ],
120
+ },
121
+ ],
122
+ {
123
+ anchor: {
124
+ path: [{_key: 'b0'}],
125
+ offset: 0,
126
+ },
127
+ focus: {
128
+ path: [{_key: 'b2'}, 'children', {_key: 's3'}],
129
+ offset: 0,
130
+ },
58
131
  },
59
- }),
132
+ ),
60
133
  ),
61
- ).toBe(false)
134
+ ).toBe(true)
62
135
  })