@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.
- package/lib/_chunks-dts/behavior.types.action.d.ts +122 -122
- package/lib/index.cjs +162 -2
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +164 -4
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.ts +3 -3
- package/lib/utils/index.d.cts +2 -2
- package/lib/utils/index.d.ts +2 -2
- package/package.json +1 -1
- package/src/behaviors/behavior.core.lists.ts +149 -1
- package/src/behaviors/behavior.core.ts +1 -0
- package/src/converters/converter.text-plain.test.ts +7 -5
- package/src/editor/__tests__/PortableTextEditor.test.tsx +5 -14
- package/src/editor/__tests__/PortableTextEditorTester.tsx +23 -77
- package/src/editor/__tests__/RangeDecorations.test.tsx +2 -9
- package/src/editor/__tests__/insert-block.test.tsx +7 -28
- package/src/editor/__tests__/self-solving.test.tsx +5 -17
- package/src/editor/create-editor.ts +4 -1
- package/src/editor/editor-schema.ts +36 -3
- package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +8 -15
- package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +7 -11
- package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +81 -63
- package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +2 -9
- package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +3 -7
- package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +17 -27
- package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +3 -7
- package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +4 -9
- package/src/internal-utils/__tests__/valueNormalization.test.tsx +3 -7
- package/src/internal-utils/__tests__/values.test.ts +5 -6
- package/src/internal-utils/operation-to-patches.test.ts +17 -12
- package/src/plugins/plugin.internal.portable-text-editor-ref.tsx +16 -0
- package/src/editor/__tests__/sync-value.test.tsx +0 -154
package/lib/index.js
CHANGED
|
@@ -11,8 +11,8 @@ import { DOMEditor, isDOMNode, EDITOR_TO_PENDING_SELECTION } from "slate-dom";
|
|
|
11
11
|
import { getBlockStartPoint, getBlockKeyFromSelectionPoint, isTextBlock, getChildKeyFromSelectionPoint, blockOffsetToSpanSelectionPoint, isSpan, parseBlock, parseAnnotation, parseInlineObject, isKeyedSegment, isListBlock, isTypedObject, getSelectionStartPoint, getSelectionEndPoint, getTextBlockText, parseBlocks } from "./_chunks-es/util.slice-blocks.js";
|
|
12
12
|
import { getBlockEndPoint, isSelectionCollapsed, isEqualSelectionPoints, isEmptyTextBlock } from "./_chunks-es/util.is-selection-collapsed.js";
|
|
13
13
|
import isEqual from "lodash/isEqual.js";
|
|
14
|
-
import { isSelectionCollapsed as isSelectionCollapsed$1, getFocusTextBlock, getFocusSpan as getFocusSpan$1, isSelectionExpanded, getFocusBlock as getFocusBlock$1, getSelectedValue, getFocusChild as getFocusChild$1 } from "./_chunks-es/selector.is-selection-expanded.js";
|
|
15
|
-
import { getFocusInlineObject, getSelectedBlocks, getSelectionStartBlock as getSelectionStartBlock$1, getSelectionEndBlock as getSelectionEndBlock$1, isOverlappingSelection, isSelectingEntireBlocks, getMarkState, getActiveDecorators, getActiveAnnotationsMarks, getTrimmedSelection, getCaretWordSelection, getFocusBlockObject, getPreviousBlock, getNextBlock, isAtTheEndOfBlock, isAtTheStartOfBlock, getFirstBlock as getFirstBlock$1, getLastBlock as getLastBlock$1, getFocusListBlock, isActiveAnnotation, isActiveDecorator, getSelectedTextBlocks, isActiveListItem, isActiveStyle, getActiveAnnotations } from "./_chunks-es/selector.is-selecting-entire-blocks.js";
|
|
14
|
+
import { isSelectionCollapsed as isSelectionCollapsed$1, getFocusTextBlock, getFocusSpan as getFocusSpan$1, isSelectionExpanded, getFocusBlock as getFocusBlock$1, getSelectedValue, getSelectionStartPoint as getSelectionStartPoint$1, getFocusChild as getFocusChild$1 } from "./_chunks-es/selector.is-selection-expanded.js";
|
|
15
|
+
import { getFocusInlineObject, getSelectedBlocks, getSelectionStartBlock as getSelectionStartBlock$1, getSelectionEndBlock as getSelectionEndBlock$1, isOverlappingSelection, isSelectingEntireBlocks, getMarkState, getActiveDecorators, getActiveAnnotationsMarks, getTrimmedSelection, getCaretWordSelection, getFocusBlockObject, getPreviousBlock, getNextBlock, isAtTheEndOfBlock, isAtTheStartOfBlock, getFirstBlock as getFirstBlock$1, getLastBlock as getLastBlock$1, getFocusListBlock, getSelectionEndPoint as getSelectionEndPoint$1, isActiveAnnotation, isActiveDecorator, getSelectedTextBlocks, isActiveListItem, isActiveStyle, getActiveAnnotations } from "./_chunks-es/selector.is-selecting-entire-blocks.js";
|
|
16
16
|
import getRandomValues from "get-random-values-esm";
|
|
17
17
|
import { defineBehavior, forward, raise, effect } from "./behaviors/index.js";
|
|
18
18
|
import uniq from "lodash/uniq.js";
|
|
@@ -6288,6 +6288,126 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = defineBehavior({
|
|
|
6288
6288
|
type: "delete.block",
|
|
6289
6289
|
at: focusTextBlock.path
|
|
6290
6290
|
})]]
|
|
6291
|
+
}), deletingListFromStart = defineBehavior({
|
|
6292
|
+
on: "delete",
|
|
6293
|
+
guard: ({
|
|
6294
|
+
snapshot,
|
|
6295
|
+
event
|
|
6296
|
+
}) => {
|
|
6297
|
+
const blocksToDelete = getSelectedBlocks({
|
|
6298
|
+
...snapshot,
|
|
6299
|
+
context: {
|
|
6300
|
+
...snapshot.context,
|
|
6301
|
+
selection: event.at
|
|
6302
|
+
}
|
|
6303
|
+
});
|
|
6304
|
+
if (blocksToDelete.length < 2)
|
|
6305
|
+
return !1;
|
|
6306
|
+
const startBlock = blocksToDelete.at(0)?.node, middleBlocks = blocksToDelete.slice(1, -1), endBlock = blocksToDelete.at(-1)?.node;
|
|
6307
|
+
if (!isListBlock(snapshot.context, startBlock) || !isListBlock(snapshot.context, endBlock))
|
|
6308
|
+
return !1;
|
|
6309
|
+
const deleteStartPoint = getSelectionStartPoint$1({
|
|
6310
|
+
context: {
|
|
6311
|
+
...snapshot.context,
|
|
6312
|
+
selection: event.at
|
|
6313
|
+
}
|
|
6314
|
+
}), deleteEndPoint = getSelectionEndPoint$1({
|
|
6315
|
+
context: {
|
|
6316
|
+
...snapshot.context,
|
|
6317
|
+
selection: event.at
|
|
6318
|
+
}
|
|
6319
|
+
});
|
|
6320
|
+
if (!deleteStartPoint || !deleteEndPoint)
|
|
6321
|
+
return !1;
|
|
6322
|
+
const startBlockStartPoint = getBlockStartPoint({
|
|
6323
|
+
context: snapshot.context,
|
|
6324
|
+
block: {
|
|
6325
|
+
node: startBlock,
|
|
6326
|
+
path: [{
|
|
6327
|
+
_key: startBlock._key
|
|
6328
|
+
}]
|
|
6329
|
+
}
|
|
6330
|
+
});
|
|
6331
|
+
if (!isEqualSelectionPoints(deleteStartPoint, startBlockStartPoint))
|
|
6332
|
+
return !1;
|
|
6333
|
+
const startBlockEndPoint = getBlockEndPoint({
|
|
6334
|
+
context: snapshot.context,
|
|
6335
|
+
block: {
|
|
6336
|
+
node: startBlock,
|
|
6337
|
+
path: [{
|
|
6338
|
+
_key: startBlock._key
|
|
6339
|
+
}]
|
|
6340
|
+
}
|
|
6341
|
+
}), endBlockEndPoint = getBlockEndPoint({
|
|
6342
|
+
context: snapshot.context,
|
|
6343
|
+
block: {
|
|
6344
|
+
node: endBlock,
|
|
6345
|
+
path: [{
|
|
6346
|
+
_key: endBlock._key
|
|
6347
|
+
}]
|
|
6348
|
+
}
|
|
6349
|
+
}), slicedEndBlock = sliceTextBlock({
|
|
6350
|
+
context: {
|
|
6351
|
+
schema: snapshot.context.schema,
|
|
6352
|
+
selection: {
|
|
6353
|
+
anchor: deleteEndPoint,
|
|
6354
|
+
focus: endBlockEndPoint
|
|
6355
|
+
}
|
|
6356
|
+
},
|
|
6357
|
+
block: endBlock
|
|
6358
|
+
});
|
|
6359
|
+
return {
|
|
6360
|
+
startBlockStartPoint,
|
|
6361
|
+
startBlockEndPoint,
|
|
6362
|
+
middleBlocks,
|
|
6363
|
+
endBlock,
|
|
6364
|
+
slicedEndBlock
|
|
6365
|
+
};
|
|
6366
|
+
},
|
|
6367
|
+
actions: [(_, {
|
|
6368
|
+
startBlockStartPoint,
|
|
6369
|
+
startBlockEndPoint,
|
|
6370
|
+
middleBlocks,
|
|
6371
|
+
endBlock,
|
|
6372
|
+
slicedEndBlock
|
|
6373
|
+
}) => [
|
|
6374
|
+
// All block in between can safely be deleted.
|
|
6375
|
+
...middleBlocks.map((block) => raise({
|
|
6376
|
+
type: "delete.block",
|
|
6377
|
+
at: block.path
|
|
6378
|
+
})),
|
|
6379
|
+
// The last block is deleted as well.
|
|
6380
|
+
raise({
|
|
6381
|
+
type: "delete.block",
|
|
6382
|
+
at: [{
|
|
6383
|
+
_key: endBlock._key
|
|
6384
|
+
}]
|
|
6385
|
+
}),
|
|
6386
|
+
// But in case the delete operation didn't reach all the way to the end
|
|
6387
|
+
// of it, we first place the caret at the end of the start block...
|
|
6388
|
+
raise({
|
|
6389
|
+
type: "select",
|
|
6390
|
+
at: {
|
|
6391
|
+
anchor: startBlockEndPoint,
|
|
6392
|
+
focus: startBlockEndPoint
|
|
6393
|
+
}
|
|
6394
|
+
}),
|
|
6395
|
+
// ...and insert the rest of the end block at the end of it.
|
|
6396
|
+
raise({
|
|
6397
|
+
type: "insert.block",
|
|
6398
|
+
block: slicedEndBlock,
|
|
6399
|
+
placement: "auto",
|
|
6400
|
+
select: "none"
|
|
6401
|
+
}),
|
|
6402
|
+
// And finally, we delete the original text of the start block.
|
|
6403
|
+
raise({
|
|
6404
|
+
type: "delete",
|
|
6405
|
+
at: {
|
|
6406
|
+
anchor: startBlockStartPoint,
|
|
6407
|
+
focus: startBlockEndPoint
|
|
6408
|
+
}
|
|
6409
|
+
})
|
|
6410
|
+
]]
|
|
6291
6411
|
}), clearListOnEnter = defineBehavior({
|
|
6292
6412
|
on: "insert.break",
|
|
6293
6413
|
guard: ({
|
|
@@ -6452,13 +6572,14 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = defineBehavior({
|
|
|
6452
6572
|
unindentListOnBackspace,
|
|
6453
6573
|
mergeTextIntoListOnDelete,
|
|
6454
6574
|
mergeTextIntoListOnBackspace,
|
|
6575
|
+
deletingListFromStart,
|
|
6455
6576
|
clearListOnEnter,
|
|
6456
6577
|
indentListOnTab,
|
|
6457
6578
|
unindentListOnShiftTab,
|
|
6458
6579
|
inheritListLevel,
|
|
6459
6580
|
inheritListItem,
|
|
6460
6581
|
inheritListProperties
|
|
6461
|
-
}, 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) => ({
|
|
6582
|
+
}, 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) => ({
|
|
6462
6583
|
behavior,
|
|
6463
6584
|
priority: corePriority
|
|
6464
6585
|
})), abstractAnnotationBehaviors = [defineBehavior({
|
|
@@ -8467,6 +8588,45 @@ function legacySchemaToEditorSchema(schema) {
|
|
|
8467
8588
|
}))
|
|
8468
8589
|
};
|
|
8469
8590
|
}
|
|
8591
|
+
function compileSchemaDefinition(definition) {
|
|
8592
|
+
const styles = (definition.styles ?? []).map((style) => ({
|
|
8593
|
+
...style,
|
|
8594
|
+
value: style.name
|
|
8595
|
+
}));
|
|
8596
|
+
return {
|
|
8597
|
+
block: {
|
|
8598
|
+
name: "block"
|
|
8599
|
+
},
|
|
8600
|
+
span: {
|
|
8601
|
+
name: "span"
|
|
8602
|
+
},
|
|
8603
|
+
styles: styles.some((style) => style.value === "normal") ? styles : [{
|
|
8604
|
+
value: "normal",
|
|
8605
|
+
name: "normal",
|
|
8606
|
+
title: "Normal"
|
|
8607
|
+
}, ...styles],
|
|
8608
|
+
lists: (definition.lists ?? []).map((list) => ({
|
|
8609
|
+
...list,
|
|
8610
|
+
value: list.name
|
|
8611
|
+
})),
|
|
8612
|
+
decorators: (definition.decorators ?? []).map((decorator) => ({
|
|
8613
|
+
...decorator,
|
|
8614
|
+
value: decorator.name
|
|
8615
|
+
})),
|
|
8616
|
+
annotations: (definition.annotations ?? []).map((annotation) => ({
|
|
8617
|
+
...annotation,
|
|
8618
|
+
fields: annotation.fields ?? []
|
|
8619
|
+
})),
|
|
8620
|
+
blockObjects: (definition.blockObjects ?? []).map((blockObject) => ({
|
|
8621
|
+
...blockObject,
|
|
8622
|
+
fields: blockObject.fields ?? []
|
|
8623
|
+
})),
|
|
8624
|
+
inlineObjects: (definition.inlineObjects ?? []).map((inlineObject) => ({
|
|
8625
|
+
...inlineObject,
|
|
8626
|
+
fields: inlineObject.fields ?? []
|
|
8627
|
+
}))
|
|
8628
|
+
};
|
|
8629
|
+
}
|
|
8470
8630
|
function compileSchemaDefinitionToLegacySchema(definition) {
|
|
8471
8631
|
const blockObjects = definition?.blockObjects?.map((blockObject) => defineType({
|
|
8472
8632
|
type: "object",
|
|
@@ -10189,7 +10349,7 @@ function editorConfigToMachineInput(config) {
|
|
|
10189
10349
|
};
|
|
10190
10350
|
}
|
|
10191
10351
|
function compileSchemasFromEditorConfig(config) {
|
|
10192
|
-
const legacySchema = config.schemaDefinition ? compileSchemaDefinitionToLegacySchema(config.schemaDefinition) : createLegacySchema(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)), schema = legacySchemaToEditorSchema(legacySchema);
|
|
10352
|
+
const legacySchema = config.schemaDefinition ? compileSchemaDefinitionToLegacySchema(config.schemaDefinition) : createLegacySchema(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)), schema = config.schemaDefinition ? compileSchemaDefinition(config.schemaDefinition) : legacySchemaToEditorSchema(legacySchema);
|
|
10193
10353
|
return {
|
|
10194
10354
|
legacySchema,
|
|
10195
10355
|
schema
|