@portabletext/editor 1.54.2 → 1.54.4
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/index.cjs +98 -105
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +10 -2
- package/lib/index.d.ts +10 -2
- package/lib/index.js +98 -105
- package/lib/index.js.map +1 -1
- package/package.json +5 -5
- package/src/editor/Editable.tsx +4 -8
- package/src/editor/create-slate-editor.tsx +1 -0
- package/src/editor/editor-schema.ts +3 -0
- package/src/editor/plugins/createWithPlaceholderBlock.ts +14 -14
- package/src/editor/range-decorations-machine.ts +138 -157
- package/src/index.ts +1 -0
- package/src/types/editor.ts +2 -0
package/lib/index.d.cts
CHANGED
|
@@ -36,7 +36,7 @@ import {
|
|
|
36
36
|
TextareaHTMLAttributes,
|
|
37
37
|
} from 'react'
|
|
38
38
|
import type {Observable, Subject} from 'rxjs'
|
|
39
|
-
import {Descendant} from 'slate'
|
|
39
|
+
import {BaseRange, Descendant} from 'slate'
|
|
40
40
|
import type {Operation} from 'slate'
|
|
41
41
|
import type {DOMNode} from 'slate-dom'
|
|
42
42
|
import type {ReactEditor} from 'slate-react'
|
|
@@ -616,6 +616,10 @@ declare type CustomBehaviorEventType<
|
|
|
616
616
|
TType extends string = '',
|
|
617
617
|
> = TType extends '' ? `${TNamespace}` : `${TNamespace}.${TType}`
|
|
618
618
|
|
|
619
|
+
declare type DecoratedRange = BaseRange & {
|
|
620
|
+
rangeDecoration: RangeDecoration
|
|
621
|
+
}
|
|
622
|
+
|
|
619
623
|
/**
|
|
620
624
|
* @public
|
|
621
625
|
* A helper wrapper that adds editor support, such as autocomplete and type checking, for a schema definition.
|
|
@@ -3243,7 +3247,10 @@ declare type ExternalEditorEvent =
|
|
|
3243
3247
|
declare type ExtractNamespace<TType extends string> =
|
|
3244
3248
|
TType extends `${infer Namespace}.${string}` ? Namespace : TType
|
|
3245
3249
|
|
|
3246
|
-
|
|
3250
|
+
/**
|
|
3251
|
+
* @public
|
|
3252
|
+
*/
|
|
3253
|
+
export declare type FieldDefinition = {
|
|
3247
3254
|
name: string
|
|
3248
3255
|
type: 'string' | 'number' | 'boolean' | 'array' | 'object'
|
|
3249
3256
|
}
|
|
@@ -4133,6 +4140,7 @@ declare interface PortableTextSlateEditor extends ReactEditor {
|
|
|
4133
4140
|
isTextSpan: (value: unknown) => value is PortableTextSpan
|
|
4134
4141
|
isListBlock: (value: unknown) => value is PortableTextListBlock
|
|
4135
4142
|
value: Array<PortableTextBlock>
|
|
4143
|
+
decoratedRanges: Array<DecoratedRange>
|
|
4136
4144
|
decoratorState: Record<string, boolean | undefined>
|
|
4137
4145
|
markState: MarkState | undefined
|
|
4138
4146
|
/**
|
package/lib/index.d.ts
CHANGED
|
@@ -36,7 +36,7 @@ import {
|
|
|
36
36
|
TextareaHTMLAttributes,
|
|
37
37
|
} from 'react'
|
|
38
38
|
import type {Observable, Subject} from 'rxjs'
|
|
39
|
-
import {Descendant} from 'slate'
|
|
39
|
+
import {BaseRange, Descendant} from 'slate'
|
|
40
40
|
import type {Operation} from 'slate'
|
|
41
41
|
import type {DOMNode} from 'slate-dom'
|
|
42
42
|
import type {ReactEditor} from 'slate-react'
|
|
@@ -616,6 +616,10 @@ declare type CustomBehaviorEventType<
|
|
|
616
616
|
TType extends string = '',
|
|
617
617
|
> = TType extends '' ? `${TNamespace}` : `${TNamespace}.${TType}`
|
|
618
618
|
|
|
619
|
+
declare type DecoratedRange = BaseRange & {
|
|
620
|
+
rangeDecoration: RangeDecoration
|
|
621
|
+
}
|
|
622
|
+
|
|
619
623
|
/**
|
|
620
624
|
* @public
|
|
621
625
|
* A helper wrapper that adds editor support, such as autocomplete and type checking, for a schema definition.
|
|
@@ -3243,7 +3247,10 @@ declare type ExternalEditorEvent =
|
|
|
3243
3247
|
declare type ExtractNamespace<TType extends string> =
|
|
3244
3248
|
TType extends `${infer Namespace}.${string}` ? Namespace : TType
|
|
3245
3249
|
|
|
3246
|
-
|
|
3250
|
+
/**
|
|
3251
|
+
* @public
|
|
3252
|
+
*/
|
|
3253
|
+
export declare type FieldDefinition = {
|
|
3247
3254
|
name: string
|
|
3248
3255
|
type: 'string' | 'number' | 'boolean' | 'array' | 'object'
|
|
3249
3256
|
}
|
|
@@ -4133,6 +4140,7 @@ declare interface PortableTextSlateEditor extends ReactEditor {
|
|
|
4133
4140
|
isTextSpan: (value: unknown) => value is PortableTextSpan
|
|
4134
4141
|
isListBlock: (value: unknown) => value is PortableTextListBlock
|
|
4135
4142
|
value: Array<PortableTextBlock>
|
|
4143
|
+
decoratedRanges: Array<DecoratedRange>
|
|
4136
4144
|
decoratorState: Record<string, boolean | undefined>
|
|
4137
4145
|
markState: MarkState | undefined
|
|
4138
4146
|
/**
|
package/lib/index.js
CHANGED
|
@@ -4685,13 +4685,10 @@ function createWithPlaceholderBlock(editorActor) {
|
|
|
4685
4685
|
return;
|
|
4686
4686
|
}
|
|
4687
4687
|
if (op.type === "remove_node") {
|
|
4688
|
-
const
|
|
4689
|
-
|
|
4690
|
-
|
|
4691
|
-
|
|
4692
|
-
decorators: []
|
|
4693
|
-
})));
|
|
4694
|
-
}
|
|
4688
|
+
const blockIndex = op.path.at(0), isLonelyBlock = op.path.length === 1 && blockIndex === 0 && editor.children.length === 1, isBlockObject = op.node._type !== editorActor.getSnapshot().context.schema.block.name;
|
|
4689
|
+
isLonelyBlock && isBlockObject && (debug$b("Adding placeholder block"), Editor.insertNode(editor, editor.pteCreateTextBlock({
|
|
4690
|
+
decorators: []
|
|
4691
|
+
})));
|
|
4695
4692
|
}
|
|
4696
4693
|
apply2(op);
|
|
4697
4694
|
}, editor;
|
|
@@ -5218,7 +5215,7 @@ function createSlateEditor(config) {
|
|
|
5218
5215
|
relayActor: config.relayActor,
|
|
5219
5216
|
subscriptions: config.subscriptions
|
|
5220
5217
|
});
|
|
5221
|
-
KEY_TO_VALUE_ELEMENT.set(instance, {}), KEY_TO_SLATE_ELEMENT.set(instance, {}), instance.decoratorState = {}, instance.markState = void 0, instance.value = [createPlaceholderBlock(config.editorActor.getSnapshot().context)];
|
|
5218
|
+
KEY_TO_VALUE_ELEMENT.set(instance, {}), KEY_TO_SLATE_ELEMENT.set(instance, {}), instance.decoratedRanges = [], instance.decoratorState = {}, instance.markState = void 0, instance.value = [createPlaceholderBlock(config.editorActor.getSnapshot().context)];
|
|
5222
5219
|
const initialValue = toSlateValue(instance.value, {
|
|
5223
5220
|
schemaTypes: config.editorActor.getSnapshot().context.schema
|
|
5224
5221
|
});
|
|
@@ -10570,111 +10567,107 @@ const slateOperationCallback = ({
|
|
|
10570
10567
|
event
|
|
10571
10568
|
}) => event.type !== "range decorations updated" ? context.pendingRangeDecorations : event.rangeDecorations
|
|
10572
10569
|
}),
|
|
10573
|
-
"set up initial range decorations":
|
|
10574
|
-
|
|
10575
|
-
|
|
10576
|
-
|
|
10577
|
-
|
|
10578
|
-
|
|
10579
|
-
|
|
10580
|
-
|
|
10581
|
-
|
|
10582
|
-
newSelection: null,
|
|
10583
|
-
rangeDecoration,
|
|
10584
|
-
origin: "local"
|
|
10585
|
-
});
|
|
10586
|
-
continue;
|
|
10587
|
-
}
|
|
10588
|
-
rangeDecorationState.push({
|
|
10570
|
+
"set up initial range decorations": ({
|
|
10571
|
+
context
|
|
10572
|
+
}) => {
|
|
10573
|
+
const rangeDecorationState = [];
|
|
10574
|
+
for (const rangeDecoration of context.pendingRangeDecorations) {
|
|
10575
|
+
const slateRange = toSlateRange(rangeDecoration.selection, context.slateEditor);
|
|
10576
|
+
if (!Range.isRange(slateRange)) {
|
|
10577
|
+
rangeDecoration.onMoved?.({
|
|
10578
|
+
newSelection: null,
|
|
10589
10579
|
rangeDecoration,
|
|
10590
|
-
|
|
10580
|
+
origin: "local"
|
|
10591
10581
|
});
|
|
10582
|
+
continue;
|
|
10592
10583
|
}
|
|
10593
|
-
|
|
10584
|
+
rangeDecorationState.push({
|
|
10585
|
+
rangeDecoration,
|
|
10586
|
+
...slateRange
|
|
10587
|
+
});
|
|
10594
10588
|
}
|
|
10595
|
-
|
|
10596
|
-
|
|
10597
|
-
|
|
10598
|
-
|
|
10599
|
-
|
|
10600
|
-
|
|
10601
|
-
|
|
10602
|
-
|
|
10603
|
-
|
|
10604
|
-
|
|
10605
|
-
|
|
10606
|
-
|
|
10607
|
-
|
|
10608
|
-
|
|
10609
|
-
rangeDecoration,
|
|
10610
|
-
origin: "local"
|
|
10611
|
-
});
|
|
10612
|
-
continue;
|
|
10613
|
-
}
|
|
10614
|
-
rangeDecorationState.push({
|
|
10589
|
+
context.slateEditor.decoratedRanges = rangeDecorationState;
|
|
10590
|
+
},
|
|
10591
|
+
"update range decorations": ({
|
|
10592
|
+
context,
|
|
10593
|
+
event
|
|
10594
|
+
}) => {
|
|
10595
|
+
if (event.type !== "range decorations updated")
|
|
10596
|
+
return;
|
|
10597
|
+
const rangeDecorationState = [];
|
|
10598
|
+
for (const rangeDecoration of event.rangeDecorations) {
|
|
10599
|
+
const slateRange = toSlateRange(rangeDecoration.selection, context.slateEditor);
|
|
10600
|
+
if (!Range.isRange(slateRange)) {
|
|
10601
|
+
rangeDecoration.onMoved?.({
|
|
10602
|
+
newSelection: null,
|
|
10615
10603
|
rangeDecoration,
|
|
10616
|
-
|
|
10604
|
+
origin: "local"
|
|
10617
10605
|
});
|
|
10606
|
+
continue;
|
|
10618
10607
|
}
|
|
10619
|
-
|
|
10608
|
+
rangeDecorationState.push({
|
|
10609
|
+
rangeDecoration,
|
|
10610
|
+
...slateRange
|
|
10611
|
+
});
|
|
10620
10612
|
}
|
|
10621
|
-
|
|
10622
|
-
|
|
10623
|
-
|
|
10624
|
-
|
|
10625
|
-
|
|
10626
|
-
|
|
10627
|
-
|
|
10628
|
-
|
|
10629
|
-
|
|
10630
|
-
|
|
10631
|
-
|
|
10632
|
-
|
|
10633
|
-
|
|
10634
|
-
|
|
10635
|
-
|
|
10636
|
-
|
|
10637
|
-
|
|
10638
|
-
|
|
10639
|
-
|
|
10640
|
-
|
|
10641
|
-
|
|
10642
|
-
|
|
10613
|
+
context.slateEditor.decoratedRanges = rangeDecorationState;
|
|
10614
|
+
},
|
|
10615
|
+
"move range decorations": ({
|
|
10616
|
+
context,
|
|
10617
|
+
event
|
|
10618
|
+
}) => {
|
|
10619
|
+
if (event.type !== "slate operation")
|
|
10620
|
+
return;
|
|
10621
|
+
const rangeDecorationState = [];
|
|
10622
|
+
for (const decoratedRange of context.slateEditor.decoratedRanges) {
|
|
10623
|
+
const slateRange = toSlateRange(decoratedRange.rangeDecoration.selection, context.slateEditor);
|
|
10624
|
+
if (!Range.isRange(slateRange)) {
|
|
10625
|
+
decoratedRange.rangeDecoration.onMoved?.({
|
|
10626
|
+
newSelection: null,
|
|
10627
|
+
rangeDecoration: decoratedRange.rangeDecoration,
|
|
10628
|
+
origin: "local"
|
|
10629
|
+
});
|
|
10630
|
+
continue;
|
|
10631
|
+
}
|
|
10632
|
+
let newRange;
|
|
10633
|
+
if (newRange = moveRangeByOperation(slateRange, event.operation), newRange && newRange !== slateRange || newRange === null && slateRange) {
|
|
10634
|
+
const newRangeSelection = newRange ? slateRangeToSelection({
|
|
10635
|
+
schema: context.schema,
|
|
10636
|
+
editor: context.slateEditor,
|
|
10637
|
+
range: newRange
|
|
10638
|
+
}) : null;
|
|
10639
|
+
decoratedRange.rangeDecoration.onMoved?.({
|
|
10640
|
+
newSelection: newRangeSelection,
|
|
10641
|
+
rangeDecoration: decoratedRange.rangeDecoration,
|
|
10642
|
+
origin: "local"
|
|
10643
|
+
});
|
|
10644
|
+
}
|
|
10645
|
+
newRange !== null && rangeDecorationState.push({
|
|
10646
|
+
...newRange || slateRange,
|
|
10647
|
+
rangeDecoration: {
|
|
10648
|
+
...decoratedRange.rangeDecoration,
|
|
10649
|
+
selection: slateRangeToSelection({
|
|
10643
10650
|
schema: context.schema,
|
|
10644
10651
|
editor: context.slateEditor,
|
|
10645
10652
|
range: newRange
|
|
10646
|
-
})
|
|
10647
|
-
decoratedRange.rangeDecoration.onMoved?.({
|
|
10648
|
-
newSelection: newRangeSelection,
|
|
10649
|
-
rangeDecoration: decoratedRange.rangeDecoration,
|
|
10650
|
-
origin: "local"
|
|
10651
|
-
});
|
|
10653
|
+
})
|
|
10652
10654
|
}
|
|
10653
|
-
|
|
10654
|
-
...newRange || slateRange,
|
|
10655
|
-
rangeDecoration: {
|
|
10656
|
-
...decoratedRange.rangeDecoration,
|
|
10657
|
-
selection: slateRangeToSelection({
|
|
10658
|
-
schema: context.schema,
|
|
10659
|
-
editor: context.slateEditor,
|
|
10660
|
-
range: newRange
|
|
10661
|
-
})
|
|
10662
|
-
}
|
|
10663
|
-
});
|
|
10664
|
-
}
|
|
10665
|
-
return rangeDecorationState;
|
|
10655
|
+
});
|
|
10666
10656
|
}
|
|
10667
|
-
|
|
10657
|
+
context.slateEditor.decoratedRanges = rangeDecorationState;
|
|
10658
|
+
},
|
|
10668
10659
|
"assign readOnly": assign({
|
|
10669
10660
|
readOnly: ({
|
|
10670
10661
|
context,
|
|
10671
10662
|
event
|
|
10672
10663
|
}) => event.type !== "update read only" ? context.readOnly : event.readOnly
|
|
10673
10664
|
}),
|
|
10674
|
-
"
|
|
10675
|
-
|
|
10665
|
+
"update decorate": assign({
|
|
10666
|
+
decorate: ({
|
|
10676
10667
|
context
|
|
10677
|
-
}) =>
|
|
10668
|
+
}) => ({
|
|
10669
|
+
fn: createDecorate(context.schema, context.slateEditor)
|
|
10670
|
+
})
|
|
10678
10671
|
})
|
|
10679
10672
|
},
|
|
10680
10673
|
actors: {
|
|
@@ -10686,14 +10679,14 @@ const slateOperationCallback = ({
|
|
|
10686
10679
|
}) => context.pendingRangeDecorations.length > 0,
|
|
10687
10680
|
"has range decorations": ({
|
|
10688
10681
|
context
|
|
10689
|
-
}) => context.decoratedRanges.length > 0,
|
|
10682
|
+
}) => context.slateEditor.decoratedRanges.length > 0,
|
|
10690
10683
|
"has different decorations": ({
|
|
10691
10684
|
context,
|
|
10692
10685
|
event
|
|
10693
10686
|
}) => {
|
|
10694
10687
|
if (event.type !== "range decorations updated")
|
|
10695
10688
|
return !1;
|
|
10696
|
-
const existingRangeDecorations = context.decoratedRanges.map((decoratedRange) => ({
|
|
10689
|
+
const existingRangeDecorations = context.slateEditor.decoratedRanges.map((decoratedRange) => ({
|
|
10697
10690
|
anchor: decoratedRange.rangeDecoration.selection?.anchor,
|
|
10698
10691
|
focus: decoratedRange.rangeDecoration.selection?.focus
|
|
10699
10692
|
})), newRangeDecorations = event.rangeDecorations.map((rangeDecoration) => ({
|
|
@@ -10720,7 +10713,9 @@ const slateOperationCallback = ({
|
|
|
10720
10713
|
skipSetup: input.skipSetup,
|
|
10721
10714
|
schema: input.schema,
|
|
10722
10715
|
slateEditor: input.slateEditor,
|
|
10723
|
-
|
|
10716
|
+
decorate: {
|
|
10717
|
+
fn: createDecorate(input.schema, input.slateEditor)
|
|
10718
|
+
}
|
|
10724
10719
|
}),
|
|
10725
10720
|
invoke: {
|
|
10726
10721
|
src: "slate operation listener",
|
|
@@ -10741,7 +10736,7 @@ const slateOperationCallback = ({
|
|
|
10741
10736
|
always: [{
|
|
10742
10737
|
guard: and(["should skip setup", "has pending range decorations"]),
|
|
10743
10738
|
target: "ready",
|
|
10744
|
-
actions: ["set up initial range decorations", "
|
|
10739
|
+
actions: ["set up initial range decorations", "update decorate"]
|
|
10745
10740
|
}, {
|
|
10746
10741
|
guard: "should skip setup",
|
|
10747
10742
|
target: "ready"
|
|
@@ -10753,7 +10748,7 @@ const slateOperationCallback = ({
|
|
|
10753
10748
|
ready: [{
|
|
10754
10749
|
target: "ready",
|
|
10755
10750
|
guard: "has pending range decorations",
|
|
10756
|
-
actions: ["set up initial range decorations", "
|
|
10751
|
+
actions: ["set up initial range decorations", "update decorate"]
|
|
10757
10752
|
}, {
|
|
10758
10753
|
target: "ready"
|
|
10759
10754
|
}]
|
|
@@ -10765,7 +10760,7 @@ const slateOperationCallback = ({
|
|
|
10765
10760
|
"range decorations updated": {
|
|
10766
10761
|
target: ".idle",
|
|
10767
10762
|
guard: "has different decorations",
|
|
10768
|
-
actions: ["update range decorations", "
|
|
10763
|
+
actions: ["update range decorations", "update decorate"]
|
|
10769
10764
|
}
|
|
10770
10765
|
},
|
|
10771
10766
|
states: {
|
|
@@ -10787,9 +10782,9 @@ const slateOperationCallback = ({
|
|
|
10787
10782
|
}
|
|
10788
10783
|
}
|
|
10789
10784
|
});
|
|
10790
|
-
function createDecorate(
|
|
10785
|
+
function createDecorate(schema, slateEditor) {
|
|
10791
10786
|
return function([node, path]) {
|
|
10792
|
-
if (isEqualToEmptyEditor(
|
|
10787
|
+
if (isEqualToEmptyEditor(slateEditor.children, schema))
|
|
10793
10788
|
return [{
|
|
10794
10789
|
anchor: {
|
|
10795
10790
|
path: [0, 0],
|
|
@@ -10806,7 +10801,7 @@ function createDecorate(rangeDecorationActor) {
|
|
|
10806
10801
|
if (!Element$1.isElement(node) || node.children.length === 0)
|
|
10807
10802
|
return [];
|
|
10808
10803
|
const blockIndex = path.at(0);
|
|
10809
|
-
return blockIndex === void 0 ? [] :
|
|
10804
|
+
return blockIndex === void 0 ? [] : slateEditor.decoratedRanges.filter((decoratedRange) => Range.isCollapsed(decoratedRange) ? node.children.some((_, childIndex) => Path.equals(decoratedRange.anchor.path, [blockIndex, childIndex]) && Path.equals(decoratedRange.focus.path, [blockIndex, childIndex])) : Range.intersection(decoratedRange, {
|
|
10810
10805
|
anchor: {
|
|
10811
10806
|
path,
|
|
10812
10807
|
offset: 0
|
|
@@ -10861,9 +10856,7 @@ const debug = debugWithName("component:Editable"), PortableTextEditable = forwar
|
|
|
10861
10856
|
setup: "setting up"
|
|
10862
10857
|
})
|
|
10863
10858
|
}
|
|
10864
|
-
});
|
|
10865
|
-
useSelector(rangeDecorationsActor, (s_0) => s_0.context.updateCount);
|
|
10866
|
-
const decorate = useMemo(() => createDecorate(rangeDecorationsActor), [rangeDecorationsActor]);
|
|
10859
|
+
}), decorate = useSelector(rangeDecorationsActor, (s_0) => s_0.context.decorate?.fn);
|
|
10867
10860
|
useEffect(() => {
|
|
10868
10861
|
rangeDecorationsActor.send({
|
|
10869
10862
|
type: "update read only",
|