@portabletext/editor 1.11.2 → 1.12.0

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.
@@ -26,15 +26,12 @@ import type {
26
26
  } from '../../types/editor'
27
27
  import {debugWithName} from '../../utils/debug'
28
28
  import {toPortableTextRange, toSlateRange} from '../../utils/ranges'
29
- import {
30
- fromSlateValue,
31
- isEqualToEmptyEditor,
32
- toSlateValue,
33
- } from '../../utils/values'
29
+ import {fromSlateValue, toSlateValue} from '../../utils/values'
34
30
  import {
35
31
  KEY_TO_VALUE_ELEMENT,
36
32
  SLATE_TO_PORTABLE_TEXT_RANGE,
37
33
  } from '../../utils/weakMaps'
34
+ import {insertBlockObjectActionImplementation} from '../behavior/behavior.action.insert-block-object'
38
35
  import type {BehaviorActionImplementation} from '../behavior/behavior.actions'
39
36
  import type {EditorActor} from '../editor-machine'
40
37
  import {isDecoratorActive} from './createWithPortableTextMarkModel'
@@ -206,18 +203,35 @@ export function createEditableAPI(
206
203
  type: TSchemaType,
207
204
  value?: {[prop: string]: any},
208
205
  ): Path => {
209
- return insertBlockObjectActionImplementation({
206
+ insertBlockObjectActionImplementation({
210
207
  context: {
211
208
  keyGenerator: editorActor.getSnapshot().context.keyGenerator,
212
209
  schema: types,
213
210
  },
214
211
  action: {
215
212
  type: 'insert block object',
216
- name: type.name,
217
- value,
213
+ blockObject: {
214
+ name: type.name,
215
+ value,
216
+ },
217
+ placement: 'auto',
218
218
  editor,
219
219
  },
220
220
  })
221
+
222
+ editor.onChange()
223
+
224
+ return (
225
+ toPortableTextRange(
226
+ fromSlateValue(
227
+ editor.children,
228
+ types.block.name,
229
+ KEY_TO_VALUE_ELEMENT.get(editor),
230
+ ),
231
+ editor.selection,
232
+ types,
233
+ )?.focus.path ?? []
234
+ )
221
235
  },
222
236
  hasBlockStyle: (style: string): boolean => {
223
237
  try {
@@ -487,85 +501,6 @@ export function createEditableAPI(
487
501
  return editableApi
488
502
  }
489
503
 
490
- export const insertBlockObjectActionImplementation: BehaviorActionImplementation<
491
- 'insert block object',
492
- Path
493
- > = ({context, action}) => {
494
- const editor = action.editor
495
- const types = context.schema
496
- const block = toSlateValue(
497
- [
498
- {
499
- _key: context.keyGenerator(),
500
- _type: action.name,
501
- ...(action.value ? action.value : {}),
502
- },
503
- ],
504
- {schemaTypes: context.schema},
505
- )[0] as unknown as Node
506
-
507
- if (!editor.selection) {
508
- const lastBlock = Array.from(
509
- Editor.nodes(editor, {
510
- match: (n) => !Editor.isEditor(n),
511
- at: [],
512
- reverse: true,
513
- }),
514
- )[0]
515
-
516
- // If there is no selection, let's just insert the new block at the
517
- // end of the document
518
- Editor.insertNode(editor, block)
519
-
520
- if (lastBlock && isEqualToEmptyEditor([lastBlock[0]], types)) {
521
- // And if the last block was an empty text block, let's remove
522
- // that too
523
- Transforms.removeNodes(editor, {at: lastBlock[1]})
524
- }
525
-
526
- editor.onChange()
527
-
528
- return (
529
- toPortableTextRange(
530
- fromSlateValue(
531
- editor.children,
532
- types.block.name,
533
- KEY_TO_VALUE_ELEMENT.get(editor),
534
- ),
535
- editor.selection,
536
- types,
537
- )?.focus.path ?? []
538
- )
539
- }
540
-
541
- const focusBlock = Array.from(
542
- Editor.nodes(editor, {
543
- at: editor.selection.focus.path.slice(0, 1),
544
- match: (n) => n._type === types.block.name,
545
- }),
546
- )[0]
547
-
548
- Editor.insertNode(editor, block)
549
-
550
- if (focusBlock && isEqualToEmptyEditor([focusBlock[0]], types)) {
551
- Transforms.removeNodes(editor, {at: focusBlock[1]})
552
- }
553
-
554
- editor.onChange()
555
-
556
- return (
557
- toPortableTextRange(
558
- fromSlateValue(
559
- editor.children,
560
- types.block.name,
561
- KEY_TO_VALUE_ELEMENT.get(editor),
562
- ),
563
- editor.selection,
564
- types,
565
- )?.focus.path || []
566
- )
567
- }
568
-
569
504
  function isAnnotationActive({
570
505
  editor,
571
506
  annotation,
package/src/index.ts CHANGED
@@ -20,6 +20,7 @@ export {
20
20
  type OmitFromUnion,
21
21
  type PickFromUnion,
22
22
  } from './editor/behavior/behavior.types'
23
+ export type {BlockOffset} from './editor/behavior/behavior.utils.block-offset'
23
24
  export type {SlateEditor} from './editor/create-slate-editor'
24
25
  export {
25
26
  defineSchema,