@portabletext/editor 2.3.0 → 2.3.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.
Files changed (32) hide show
  1. package/lib/_chunks-dts/behavior.types.action.d.ts +122 -122
  2. package/lib/index.cjs +162 -2
  3. package/lib/index.cjs.map +1 -1
  4. package/lib/index.js +164 -4
  5. package/lib/index.js.map +1 -1
  6. package/lib/plugins/index.d.ts +3 -3
  7. package/lib/utils/index.d.cts +2 -2
  8. package/lib/utils/index.d.ts +2 -2
  9. package/package.json +1 -1
  10. package/src/behaviors/behavior.core.lists.ts +149 -1
  11. package/src/behaviors/behavior.core.ts +1 -0
  12. package/src/converters/converter.text-plain.test.ts +7 -5
  13. package/src/editor/__tests__/PortableTextEditor.test.tsx +5 -14
  14. package/src/editor/__tests__/PortableTextEditorTester.tsx +23 -77
  15. package/src/editor/__tests__/RangeDecorations.test.tsx +2 -9
  16. package/src/editor/__tests__/insert-block.test.tsx +7 -28
  17. package/src/editor/__tests__/self-solving.test.tsx +5 -17
  18. package/src/editor/create-editor.ts +4 -1
  19. package/src/editor/editor-schema.ts +36 -3
  20. package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +8 -15
  21. package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +7 -11
  22. package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +81 -63
  23. package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +2 -9
  24. package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +3 -7
  25. package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +17 -27
  26. package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +3 -7
  27. package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +4 -9
  28. package/src/internal-utils/__tests__/valueNormalization.test.tsx +3 -7
  29. package/src/internal-utils/__tests__/values.test.ts +5 -6
  30. package/src/internal-utils/operation-to-patches.test.ts +17 -12
  31. package/src/plugins/plugin.internal.portable-text-editor-ref.tsx +16 -0
  32. package/src/editor/__tests__/sync-value.test.tsx +0 -154
package/lib/index.cjs CHANGED
@@ -6259,6 +6259,126 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = behaviors_index.defineBehavior
6259
6259
  type: "delete.block",
6260
6260
  at: focusTextBlock.path
6261
6261
  })]]
6262
+ }), deletingListFromStart = behaviors_index.defineBehavior({
6263
+ on: "delete",
6264
+ guard: ({
6265
+ snapshot,
6266
+ event
6267
+ }) => {
6268
+ const blocksToDelete = selector_isSelectingEntireBlocks.getSelectedBlocks({
6269
+ ...snapshot,
6270
+ context: {
6271
+ ...snapshot.context,
6272
+ selection: event.at
6273
+ }
6274
+ });
6275
+ if (blocksToDelete.length < 2)
6276
+ return !1;
6277
+ const startBlock = blocksToDelete.at(0)?.node, middleBlocks = blocksToDelete.slice(1, -1), endBlock = blocksToDelete.at(-1)?.node;
6278
+ if (!util_sliceBlocks.isListBlock(snapshot.context, startBlock) || !util_sliceBlocks.isListBlock(snapshot.context, endBlock))
6279
+ return !1;
6280
+ const deleteStartPoint = selector_isSelectionExpanded.getSelectionStartPoint({
6281
+ context: {
6282
+ ...snapshot.context,
6283
+ selection: event.at
6284
+ }
6285
+ }), deleteEndPoint = selector_isSelectingEntireBlocks.getSelectionEndPoint({
6286
+ context: {
6287
+ ...snapshot.context,
6288
+ selection: event.at
6289
+ }
6290
+ });
6291
+ if (!deleteStartPoint || !deleteEndPoint)
6292
+ return !1;
6293
+ const startBlockStartPoint = util_sliceBlocks.getBlockStartPoint({
6294
+ context: snapshot.context,
6295
+ block: {
6296
+ node: startBlock,
6297
+ path: [{
6298
+ _key: startBlock._key
6299
+ }]
6300
+ }
6301
+ });
6302
+ if (!util_isSelectionCollapsed.isEqualSelectionPoints(deleteStartPoint, startBlockStartPoint))
6303
+ return !1;
6304
+ const startBlockEndPoint = util_isSelectionCollapsed.getBlockEndPoint({
6305
+ context: snapshot.context,
6306
+ block: {
6307
+ node: startBlock,
6308
+ path: [{
6309
+ _key: startBlock._key
6310
+ }]
6311
+ }
6312
+ }), endBlockEndPoint = util_isSelectionCollapsed.getBlockEndPoint({
6313
+ context: snapshot.context,
6314
+ block: {
6315
+ node: endBlock,
6316
+ path: [{
6317
+ _key: endBlock._key
6318
+ }]
6319
+ }
6320
+ }), slicedEndBlock = util_sliceTextBlock.sliceTextBlock({
6321
+ context: {
6322
+ schema: snapshot.context.schema,
6323
+ selection: {
6324
+ anchor: deleteEndPoint,
6325
+ focus: endBlockEndPoint
6326
+ }
6327
+ },
6328
+ block: endBlock
6329
+ });
6330
+ return {
6331
+ startBlockStartPoint,
6332
+ startBlockEndPoint,
6333
+ middleBlocks,
6334
+ endBlock,
6335
+ slicedEndBlock
6336
+ };
6337
+ },
6338
+ actions: [(_, {
6339
+ startBlockStartPoint,
6340
+ startBlockEndPoint,
6341
+ middleBlocks,
6342
+ endBlock,
6343
+ slicedEndBlock
6344
+ }) => [
6345
+ // All block in between can safely be deleted.
6346
+ ...middleBlocks.map((block) => behaviors_index.raise({
6347
+ type: "delete.block",
6348
+ at: block.path
6349
+ })),
6350
+ // The last block is deleted as well.
6351
+ behaviors_index.raise({
6352
+ type: "delete.block",
6353
+ at: [{
6354
+ _key: endBlock._key
6355
+ }]
6356
+ }),
6357
+ // But in case the delete operation didn't reach all the way to the end
6358
+ // of it, we first place the caret at the end of the start block...
6359
+ behaviors_index.raise({
6360
+ type: "select",
6361
+ at: {
6362
+ anchor: startBlockEndPoint,
6363
+ focus: startBlockEndPoint
6364
+ }
6365
+ }),
6366
+ // ...and insert the rest of the end block at the end of it.
6367
+ behaviors_index.raise({
6368
+ type: "insert.block",
6369
+ block: slicedEndBlock,
6370
+ placement: "auto",
6371
+ select: "none"
6372
+ }),
6373
+ // And finally, we delete the original text of the start block.
6374
+ behaviors_index.raise({
6375
+ type: "delete",
6376
+ at: {
6377
+ anchor: startBlockStartPoint,
6378
+ focus: startBlockEndPoint
6379
+ }
6380
+ })
6381
+ ]]
6262
6382
  }), clearListOnEnter = behaviors_index.defineBehavior({
6263
6383
  on: "insert.break",
6264
6384
  guard: ({
@@ -6423,13 +6543,14 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = behaviors_index.defineBehavior
6423
6543
  unindentListOnBackspace,
6424
6544
  mergeTextIntoListOnDelete,
6425
6545
  mergeTextIntoListOnBackspace,
6546
+ deletingListFromStart,
6426
6547
  clearListOnEnter,
6427
6548
  indentListOnTab,
6428
6549
  unindentListOnShiftTab,
6429
6550
  inheritListLevel,
6430
6551
  inheritListItem,
6431
6552
  inheritListProperties
6432
- }, coreBehaviorsConfig = [coreAnnotationBehaviors.addAnnotationOnCollapsedSelection, coreDecoratorBehaviors.strongShortcut, coreDecoratorBehaviors.emShortcut, coreDecoratorBehaviors.underlineShortcut, coreDecoratorBehaviors.codeShortcut, ...coreDndBehaviors, coreBlockObjectBehaviors.clickingAboveLonelyBlockObject, coreBlockObjectBehaviors.clickingBelowLonelyBlockObject, coreBlockObjectBehaviors.arrowDownOnLonelyBlockObject, coreBlockObjectBehaviors.arrowUpOnLonelyBlockObject, coreBlockObjectBehaviors.breakingBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockAfterBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockBeforeBlockObject, coreListBehaviors.clearListOnBackspace, coreListBehaviors.unindentListOnBackspace, coreListBehaviors.mergeTextIntoListOnDelete, coreListBehaviors.mergeTextIntoListOnBackspace, coreListBehaviors.clearListOnEnter, coreListBehaviors.indentListOnTab, coreListBehaviors.unindentListOnShiftTab, coreListBehaviors.inheritListLevel, coreListBehaviors.inheritListItem, coreListBehaviors.inheritListProperties, coreInsertBreakBehaviors.breakingAtTheEndOfTextBlock, coreInsertBreakBehaviors.breakingAtTheStartOfTextBlock, coreInsertBreakBehaviors.breakingEntireDocument, coreInsertBreakBehaviors.breakingEntireBlocks, coreInsertBreakBehaviors.breakingInlineObject].map((behavior) => ({
6553
+ }, coreBehaviorsConfig = [coreAnnotationBehaviors.addAnnotationOnCollapsedSelection, coreDecoratorBehaviors.strongShortcut, coreDecoratorBehaviors.emShortcut, coreDecoratorBehaviors.underlineShortcut, coreDecoratorBehaviors.codeShortcut, ...coreDndBehaviors, coreBlockObjectBehaviors.clickingAboveLonelyBlockObject, coreBlockObjectBehaviors.clickingBelowLonelyBlockObject, coreBlockObjectBehaviors.arrowDownOnLonelyBlockObject, coreBlockObjectBehaviors.arrowUpOnLonelyBlockObject, coreBlockObjectBehaviors.breakingBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockAfterBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockBeforeBlockObject, coreListBehaviors.clearListOnBackspace, coreListBehaviors.unindentListOnBackspace, coreListBehaviors.mergeTextIntoListOnDelete, coreListBehaviors.mergeTextIntoListOnBackspace, coreListBehaviors.deletingListFromStart, coreListBehaviors.clearListOnEnter, coreListBehaviors.indentListOnTab, coreListBehaviors.unindentListOnShiftTab, coreListBehaviors.inheritListLevel, coreListBehaviors.inheritListItem, coreListBehaviors.inheritListProperties, coreInsertBreakBehaviors.breakingAtTheEndOfTextBlock, coreInsertBreakBehaviors.breakingAtTheStartOfTextBlock, coreInsertBreakBehaviors.breakingEntireDocument, coreInsertBreakBehaviors.breakingEntireBlocks, coreInsertBreakBehaviors.breakingInlineObject].map((behavior) => ({
6433
6554
  behavior,
6434
6555
  priority: corePriority
6435
6556
  })), abstractAnnotationBehaviors = [behaviors_index.defineBehavior({
@@ -8438,6 +8559,45 @@ function legacySchemaToEditorSchema(schema2) {
8438
8559
  }))
8439
8560
  };
8440
8561
  }
8562
+ function compileSchemaDefinition(definition) {
8563
+ const styles = (definition.styles ?? []).map((style) => ({
8564
+ ...style,
8565
+ value: style.name
8566
+ }));
8567
+ return {
8568
+ block: {
8569
+ name: "block"
8570
+ },
8571
+ span: {
8572
+ name: "span"
8573
+ },
8574
+ styles: styles.some((style) => style.value === "normal") ? styles : [{
8575
+ value: "normal",
8576
+ name: "normal",
8577
+ title: "Normal"
8578
+ }, ...styles],
8579
+ lists: (definition.lists ?? []).map((list) => ({
8580
+ ...list,
8581
+ value: list.name
8582
+ })),
8583
+ decorators: (definition.decorators ?? []).map((decorator) => ({
8584
+ ...decorator,
8585
+ value: decorator.name
8586
+ })),
8587
+ annotations: (definition.annotations ?? []).map((annotation) => ({
8588
+ ...annotation,
8589
+ fields: annotation.fields ?? []
8590
+ })),
8591
+ blockObjects: (definition.blockObjects ?? []).map((blockObject) => ({
8592
+ ...blockObject,
8593
+ fields: blockObject.fields ?? []
8594
+ })),
8595
+ inlineObjects: (definition.inlineObjects ?? []).map((inlineObject) => ({
8596
+ ...inlineObject,
8597
+ fields: inlineObject.fields ?? []
8598
+ }))
8599
+ };
8600
+ }
8441
8601
  function compileSchemaDefinitionToLegacySchema(definition) {
8442
8602
  const blockObjects = definition?.blockObjects?.map((blockObject) => types.defineType({
8443
8603
  type: "object",
@@ -10160,7 +10320,7 @@ function editorConfigToMachineInput(config) {
10160
10320
  };
10161
10321
  }
10162
10322
  function compileSchemasFromEditorConfig(config) {
10163
- const legacySchema = config.schemaDefinition ? compileSchemaDefinitionToLegacySchema(config.schemaDefinition) : createLegacySchema(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)), schema2 = legacySchemaToEditorSchema(legacySchema);
10323
+ const legacySchema = config.schemaDefinition ? compileSchemaDefinitionToLegacySchema(config.schemaDefinition) : createLegacySchema(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)), schema2 = config.schemaDefinition ? compileSchemaDefinition(config.schemaDefinition) : legacySchemaToEditorSchema(legacySchema);
10164
10324
  return {
10165
10325
  legacySchema,
10166
10326
  schema: schema2