@portabletext/editor 2.5.0 → 2.6.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.
@@ -1,5 +1,5 @@
1
1
  import { Behavior, Editor, EditorEmittedEvent, EditorSchema } from "../_chunks-dts/behavior.types.action.js";
2
- import * as react21 from "react";
2
+ import * as react22 from "react";
3
3
  import React from "react";
4
4
  /**
5
5
  * @beta
@@ -181,7 +181,7 @@ type MarkdownPluginConfig = MarkdownBehaviorsConfig & {
181
181
  */
182
182
  declare function MarkdownPlugin(props: {
183
183
  config: MarkdownPluginConfig;
184
- }): react21.JSX.Element;
184
+ }): react22.JSX.Element;
185
185
  /**
186
186
  * @beta
187
187
  * Restrict the editor to one line. The plugin takes care of blocking
@@ -192,5 +192,5 @@ declare function MarkdownPlugin(props: {
192
192
  *
193
193
  * @deprecated Install the plugin from `@portabletext/plugin-one-line`
194
194
  */
195
- declare function OneLinePlugin(): react21.JSX.Element;
195
+ declare function OneLinePlugin(): react22.JSX.Element;
196
196
  export { BehaviorPlugin, DecoratorShortcutPlugin, EditorRefPlugin, EventListenerPlugin, MarkdownPlugin, type MarkdownPluginConfig, OneLinePlugin };
@@ -1,5 +1,5 @@
1
1
  import { BlockOffset, BlockPath, ChildPath, EditorContext, EditorSelection, EditorSelectionPoint } from "../_chunks-dts/behavior.types.action.cjs";
2
- import * as _sanity_types9 from "@sanity/types";
2
+ import * as _sanity_types8 from "@sanity/types";
3
3
  import { KeyedSegment, PortableTextBlock, PortableTextChild, PortableTextSpan, PortableTextTextBlock } from "@sanity/types";
4
4
  /**
5
5
  * @public
@@ -150,7 +150,7 @@ declare function mergeTextBlocks({
150
150
  context: Pick<EditorContext, 'keyGenerator' | 'schema'>;
151
151
  targetBlock: PortableTextTextBlock;
152
152
  incomingBlock: PortableTextTextBlock;
153
- }): PortableTextTextBlock<_sanity_types9.PortableTextObject | _sanity_types9.PortableTextSpan>;
153
+ }): PortableTextTextBlock<_sanity_types8.PortableTextObject | _sanity_types8.PortableTextSpan>;
154
154
  /**
155
155
  * @public
156
156
  */
@@ -1,5 +1,5 @@
1
1
  import { BlockOffset, BlockPath, ChildPath, EditorContext, EditorSelection, EditorSelectionPoint } from "../_chunks-dts/behavior.types.action.js";
2
- import * as _sanity_types9 from "@sanity/types";
2
+ import * as _sanity_types8 from "@sanity/types";
3
3
  import { KeyedSegment, PortableTextBlock, PortableTextChild, PortableTextSpan, PortableTextTextBlock } from "@sanity/types";
4
4
  /**
5
5
  * @public
@@ -150,7 +150,7 @@ declare function mergeTextBlocks({
150
150
  context: Pick<EditorContext, 'keyGenerator' | 'schema'>;
151
151
  targetBlock: PortableTextTextBlock;
152
152
  incomingBlock: PortableTextTextBlock;
153
- }): PortableTextTextBlock<_sanity_types9.PortableTextObject | _sanity_types9.PortableTextSpan>;
153
+ }): PortableTextTextBlock<_sanity_types8.PortableTextObject | _sanity_types8.PortableTextSpan>;
154
154
  /**
155
155
  * @public
156
156
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "2.5.0",
3
+ "version": "2.6.1",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -79,10 +79,10 @@
79
79
  "slate-dom": "^0.117.4",
80
80
  "slate-react": "0.117.4",
81
81
  "xstate": "^5.20.2",
82
- "@portabletext/block-tools": "^3.3.1",
83
- "@portabletext/patches": "^1.1.8",
82
+ "@portabletext/block-tools": "^3.3.2",
84
83
  "@portabletext/keyboard-shortcuts": "^1.1.1",
85
- "@portabletext/schema": "^1.0.1"
84
+ "@portabletext/patches": "^1.1.8",
85
+ "@portabletext/schema": "^1.1.0"
86
86
  },
87
87
  "devDependencies": {
88
88
  "@sanity/diff-match-patch": "^3.2.0",
@@ -111,11 +111,11 @@
111
111
  "vite": "^7.1.3",
112
112
  "vitest": "^3.2.4",
113
113
  "vitest-browser-react": "^1.0.1",
114
- "@portabletext/sanity-bridge": "1.1.3",
114
+ "@portabletext/sanity-bridge": "1.1.4",
115
115
  "racejar": "1.2.14"
116
116
  },
117
117
  "peerDependencies": {
118
- "@portabletext/sanity-bridge": "^1.0.0",
118
+ "@portabletext/sanity-bridge": "^1.1.4",
119
119
  "@sanity/schema": "^4.5.0",
120
120
  "@sanity/types": "^4.5.0",
121
121
  "react": "^18.3 || ^19",
@@ -61,6 +61,7 @@ export type PortableTextEditableProps = Omit<
61
61
  TextareaHTMLAttributes<HTMLDivElement>,
62
62
  'onPaste' | 'onCopy' | 'onBeforeInput'
63
63
  > & {
64
+ ref?: React.RefObject<HTMLDivElement>
64
65
  hotkeys?: HotkeyOptions
65
66
  onBeforeInput?: (event: InputEvent) => void
66
67
  onPaste?: OnPasteFn
@@ -280,6 +280,95 @@ describe(parseBlock.name, () => {
280
280
  style: 'normal',
281
281
  })
282
282
  })
283
+
284
+ describe('validating custom fields', () => {
285
+ test('none defined', () => {
286
+ expect(
287
+ parseBlock({
288
+ block: {_type: 'block', map: {}},
289
+ context: {
290
+ keyGenerator: createTestKeyGenerator(),
291
+ schema: compileSchema(defineSchema({})),
292
+ },
293
+ options: {refreshKeys: false, validateFields: true},
294
+ }),
295
+ ).toEqual({
296
+ _type: 'block',
297
+ _key: 'k0',
298
+ children: [
299
+ {
300
+ _key: 'k1',
301
+ _type: 'span',
302
+ text: '',
303
+ marks: [],
304
+ },
305
+ ],
306
+ markDefs: [],
307
+ style: 'normal',
308
+ })
309
+ })
310
+
311
+ test('field defined', () => {
312
+ expect(
313
+ parseBlock({
314
+ block: {_type: 'block', map: {}},
315
+ context: {
316
+ keyGenerator: createTestKeyGenerator(),
317
+ schema: compileSchema(
318
+ defineSchema({
319
+ block: {fields: [{name: 'map', type: 'object'}]},
320
+ }),
321
+ ),
322
+ },
323
+ options: {refreshKeys: false, validateFields: true},
324
+ }),
325
+ ).toEqual({
326
+ _type: 'block',
327
+ _key: 'k0',
328
+ map: {},
329
+ children: [
330
+ {
331
+ _key: 'k1',
332
+ _type: 'span',
333
+ text: '',
334
+ marks: [],
335
+ },
336
+ ],
337
+ markDefs: [],
338
+ style: 'normal',
339
+ })
340
+ })
341
+
342
+ test('different field defined', () => {
343
+ expect(
344
+ parseBlock({
345
+ block: {_type: 'block', foo: {}},
346
+ context: {
347
+ keyGenerator: createTestKeyGenerator(),
348
+ schema: compileSchema(
349
+ defineSchema({
350
+ block: {fields: [{name: 'map', type: 'object'}]},
351
+ }),
352
+ ),
353
+ },
354
+ options: {refreshKeys: false, validateFields: true},
355
+ }),
356
+ ).toEqual({
357
+ _type: 'block',
358
+ _key: 'k0',
359
+ children: [
360
+ {
361
+ _key: 'k1',
362
+ _type: 'span',
363
+ text: '',
364
+ marks: [],
365
+ },
366
+ ],
367
+ markDefs: [],
368
+ style: 'normal',
369
+ })
370
+ })
371
+ })
283
372
  })
284
373
  })
285
374
 
@@ -129,14 +129,22 @@ export function parseTextBlock({
129
129
 
130
130
  for (const key of Object.keys(block)) {
131
131
  if (
132
- key !== '_type' &&
133
- key !== '_key' &&
134
- key !== 'children' &&
135
- key !== 'markDefs' &&
136
- key !== 'style' &&
137
- key !== 'listItem' &&
138
- key !== 'level'
132
+ key === '_type' ||
133
+ key === '_key' ||
134
+ key === 'children' ||
135
+ key === 'markDefs' ||
136
+ key === 'style' ||
137
+ key === 'listItem' ||
138
+ key === 'level'
139
139
  ) {
140
+ continue
141
+ }
142
+
143
+ if (options.validateFields) {
144
+ if (context.schema.block.fields?.some((field) => field.name === key)) {
145
+ customFields[key] = block[key]
146
+ }
147
+ } else {
140
148
  customFields[key] = block[key]
141
149
  }
142
150
  }
@@ -219,7 +227,7 @@ export function parseTextBlock({
219
227
  },
220
228
  ],
221
229
  markDefs,
222
- ...(options.validateFields ? {} : customFields),
230
+ ...customFields,
223
231
  }
224
232
 
225
233
  if (