@portabletext/editor 1.34.1 → 1.35.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.
- package/lib/_chunks-cjs/behavior.core.cjs +57 -118
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/behavior.markdown.cjs +27 -67
- package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
- package/lib/_chunks-cjs/plugin.event-listener.cjs +53 -67
- package/lib/_chunks-cjs/plugin.event-listener.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.get-text-before.cjs +5 -7
- package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.is-active-style.cjs +22 -36
- package/lib/_chunks-cjs/selector.is-active-style.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs +68 -153
- package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs.map +1 -1
- package/lib/_chunks-cjs/util.block-offsets-to-selection.cjs.map +1 -1
- package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
- package/lib/_chunks-es/behavior.core.js +57 -118
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/behavior.markdown.js +27 -67
- package/lib/_chunks-es/behavior.markdown.js.map +1 -1
- package/lib/_chunks-es/plugin.event-listener.js +53 -67
- package/lib/_chunks-es/plugin.event-listener.js.map +1 -1
- package/lib/_chunks-es/selector.get-text-before.js +5 -7
- package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
- package/lib/_chunks-es/selector.is-active-style.js +22 -36
- package/lib/_chunks-es/selector.is-active-style.js.map +1 -1
- package/lib/_chunks-es/selector.is-at-the-start-of-block.js +68 -153
- package/lib/_chunks-es/selector.is-at-the-start-of-block.js.map +1 -1
- package/lib/_chunks-es/util.block-offsets-to-selection.js.map +1 -1
- package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
- package/lib/behaviors/index.cjs +18 -48
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +24 -13
- package/lib/behaviors/index.d.ts +24 -13
- package/lib/behaviors/index.js +18 -48
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.d.cts +16 -12
- package/lib/index.d.ts +16 -12
- package/lib/plugins/index.cjs +182 -186
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.d.cts +31 -23
- package/lib/plugins/index.d.ts +31 -23
- package/lib/plugins/index.js +182 -186
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.cjs +22 -50
- package/lib/selectors/index.cjs.map +1 -1
- package/lib/selectors/index.d.cts +6 -198
- package/lib/selectors/index.d.ts +6 -198
- package/lib/selectors/index.js +22 -50
- package/lib/selectors/index.js.map +1 -1
- package/lib/utils/index.cjs.map +1 -1
- package/lib/utils/index.d.cts +12 -5
- package/lib/utils/index.d.ts +12 -5
- package/lib/utils/index.js.map +1 -1
- package/package.json +7 -7
- package/src/behaviors/behavior.code-editor.ts +6 -6
- package/src/behaviors/behavior.core.annotations.ts +5 -4
- package/src/behaviors/behavior.core.block-objects.ts +17 -17
- package/src/behaviors/behavior.core.decorators.ts +12 -8
- package/src/behaviors/behavior.core.insert-break.ts +27 -29
- package/src/behaviors/behavior.core.lists.ts +19 -19
- package/src/behaviors/behavior.decorator-pair.ts +200 -0
- package/src/behaviors/behavior.default.ts +35 -30
- package/src/behaviors/behavior.emoji-picker.ts +12 -12
- package/src/behaviors/behavior.links.ts +7 -7
- package/src/behaviors/behavior.markdown.ts +41 -42
- package/src/behaviors/behavior.types.ts +14 -17
- package/src/behaviors/index.ts +0 -1
- package/src/converters/converter.json.ts +6 -6
- package/src/converters/converter.portable-text.deserialize.test.ts +28 -26
- package/src/converters/converter.portable-text.ts +6 -6
- package/src/converters/converter.text-html.deserialize.test.ts +17 -15
- package/src/converters/converter.text-html.serialize.test.ts +57 -53
- package/src/converters/converter.text-html.ts +14 -10
- package/src/converters/converter.text-plain.test.ts +17 -15
- package/src/converters/converter.text-plain.ts +15 -11
- package/src/converters/converter.types.ts +5 -5
- package/src/editor/editor-machine.ts +6 -1
- package/src/index.ts +3 -3
- package/src/internal-utils/get-text-to-emphasize.ts +29 -7
- package/src/plugins/plugin.decorator-shortcut.ts +235 -0
- package/src/plugins/plugin.markdown.tsx +56 -8
- package/src/plugins/plugin.one-line.tsx +17 -17
- package/src/selectors/selector.get-active-list-item.ts +4 -4
- package/src/selectors/selector.get-active-style.ts +6 -6
- package/src/selectors/selector.get-anchor-block.ts +5 -5
- package/src/selectors/selector.get-anchor-child.ts +5 -5
- package/src/selectors/selector.get-anchor-span.ts +2 -2
- package/src/selectors/selector.get-anchor-text-block.ts +2 -2
- package/src/selectors/selector.get-block-offsets.ts +8 -7
- package/src/selectors/selector.get-caret-word-selection.ts +19 -16
- package/src/selectors/selector.get-next-inline-object.ts +4 -4
- package/src/selectors/selector.get-previous-inline-object.ts +4 -4
- package/src/selectors/selector.get-selected-slice.ts +7 -4
- package/src/selectors/selector.get-selected-spans.ts +9 -9
- package/src/selectors/selector.get-selection-end-point.ts +5 -5
- package/src/selectors/selector.get-selection-start-point.ts +5 -5
- package/src/selectors/selector.get-selection-text.ts +2 -2
- package/src/selectors/selector.get-selection.ts +2 -2
- package/src/selectors/selector.get-text-before.ts +8 -8
- package/src/selectors/selector.get-trimmed-selection.ts +15 -13
- package/src/selectors/selector.get-value.ts +4 -4
- package/src/selectors/selector.is-at-the-end-of-block.ts +6 -3
- package/src/selectors/selector.is-at-the-start-of-block.ts +3 -3
- package/src/selectors/selector.is-overlapping-selection.ts +8 -6
- package/src/selectors/selector.is-selection-collapsed.ts +6 -5
- package/src/selectors/selector.is-selection-expanded.ts +2 -2
- package/src/selectors/selectors.ts +59 -59
- package/src/types/block-offset.ts +9 -0
- package/src/utils/index.ts +0 -1
- package/src/utils/util.block-offset.ts +1 -1
- package/src/utils/util.block-offsets-to-selection.ts +1 -1
- package/src/utils/util.child-selection-point-to-block-offset.ts +1 -1
- package/src/behaviors/behavior.markdown-emphasis.ts +0 -437
package/lib/index.d.cts
CHANGED
|
@@ -146,11 +146,13 @@ export declare type BehaviorActionIntend =
|
|
|
146
146
|
* @beta
|
|
147
147
|
*/
|
|
148
148
|
export declare type BehaviorActionIntendSet<TBehaviorEvent, TGuardResponse> = (
|
|
149
|
-
{
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
149
|
+
payload: {
|
|
150
|
+
/**
|
|
151
|
+
* @deprecated
|
|
152
|
+
* Use `snapshot` instead
|
|
153
|
+
*/
|
|
153
154
|
context: EditorContext
|
|
155
|
+
snapshot: EditorSnapshot
|
|
154
156
|
event: TBehaviorEvent
|
|
155
157
|
},
|
|
156
158
|
guardResponse: TGuardResponse,
|
|
@@ -167,11 +169,13 @@ export declare type BehaviorEvent =
|
|
|
167
169
|
/**
|
|
168
170
|
* @beta
|
|
169
171
|
*/
|
|
170
|
-
export declare type BehaviorGuard<TBehaviorEvent, TGuardResponse> = ({
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
172
|
+
export declare type BehaviorGuard<TBehaviorEvent, TGuardResponse> = (payload: {
|
|
173
|
+
/**
|
|
174
|
+
* @deprecated
|
|
175
|
+
* Use `snapshot` instead
|
|
176
|
+
*/
|
|
174
177
|
context: EditorContext
|
|
178
|
+
snapshot: EditorSnapshot
|
|
175
179
|
event: TBehaviorEvent
|
|
176
180
|
}) => TGuardResponse | false
|
|
177
181
|
|
|
@@ -359,10 +363,10 @@ export declare function defineSchema<
|
|
|
359
363
|
>(definition: TSchemaDefinition): TSchemaDefinition
|
|
360
364
|
|
|
361
365
|
declare type Deserializer<TMIMEType extends MIMEType> = ({
|
|
362
|
-
|
|
366
|
+
snapshot,
|
|
363
367
|
event,
|
|
364
368
|
}: {
|
|
365
|
-
|
|
369
|
+
snapshot: EditorSnapshot
|
|
366
370
|
event: PickFromUnion<ConverterEvent<TMIMEType>, 'type', 'deserialize'>
|
|
367
371
|
}) => PickFromUnion<
|
|
368
372
|
ConverterEvent<TMIMEType>,
|
|
@@ -20320,10 +20324,10 @@ export declare type SelectionChange = {
|
|
|
20320
20324
|
}
|
|
20321
20325
|
|
|
20322
20326
|
declare type Serializer<TMIMEType extends MIMEType> = ({
|
|
20323
|
-
|
|
20327
|
+
snapshot,
|
|
20324
20328
|
event,
|
|
20325
20329
|
}: {
|
|
20326
|
-
|
|
20330
|
+
snapshot: EditorSnapshot
|
|
20327
20331
|
event: PickFromUnion<ConverterEvent<TMIMEType>, 'type', 'serialize'>
|
|
20328
20332
|
}) => PickFromUnion<
|
|
20329
20333
|
ConverterEvent<TMIMEType>,
|
package/lib/index.d.ts
CHANGED
|
@@ -146,11 +146,13 @@ export declare type BehaviorActionIntend =
|
|
|
146
146
|
* @beta
|
|
147
147
|
*/
|
|
148
148
|
export declare type BehaviorActionIntendSet<TBehaviorEvent, TGuardResponse> = (
|
|
149
|
-
{
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
149
|
+
payload: {
|
|
150
|
+
/**
|
|
151
|
+
* @deprecated
|
|
152
|
+
* Use `snapshot` instead
|
|
153
|
+
*/
|
|
153
154
|
context: EditorContext
|
|
155
|
+
snapshot: EditorSnapshot
|
|
154
156
|
event: TBehaviorEvent
|
|
155
157
|
},
|
|
156
158
|
guardResponse: TGuardResponse,
|
|
@@ -167,11 +169,13 @@ export declare type BehaviorEvent =
|
|
|
167
169
|
/**
|
|
168
170
|
* @beta
|
|
169
171
|
*/
|
|
170
|
-
export declare type BehaviorGuard<TBehaviorEvent, TGuardResponse> = ({
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
172
|
+
export declare type BehaviorGuard<TBehaviorEvent, TGuardResponse> = (payload: {
|
|
173
|
+
/**
|
|
174
|
+
* @deprecated
|
|
175
|
+
* Use `snapshot` instead
|
|
176
|
+
*/
|
|
174
177
|
context: EditorContext
|
|
178
|
+
snapshot: EditorSnapshot
|
|
175
179
|
event: TBehaviorEvent
|
|
176
180
|
}) => TGuardResponse | false
|
|
177
181
|
|
|
@@ -359,10 +363,10 @@ export declare function defineSchema<
|
|
|
359
363
|
>(definition: TSchemaDefinition): TSchemaDefinition
|
|
360
364
|
|
|
361
365
|
declare type Deserializer<TMIMEType extends MIMEType> = ({
|
|
362
|
-
|
|
366
|
+
snapshot,
|
|
363
367
|
event,
|
|
364
368
|
}: {
|
|
365
|
-
|
|
369
|
+
snapshot: EditorSnapshot
|
|
366
370
|
event: PickFromUnion<ConverterEvent<TMIMEType>, 'type', 'deserialize'>
|
|
367
371
|
}) => PickFromUnion<
|
|
368
372
|
ConverterEvent<TMIMEType>,
|
|
@@ -20320,10 +20324,10 @@ export declare type SelectionChange = {
|
|
|
20320
20324
|
}
|
|
20321
20325
|
|
|
20322
20326
|
declare type Serializer<TMIMEType extends MIMEType> = ({
|
|
20323
|
-
|
|
20327
|
+
snapshot,
|
|
20324
20328
|
event,
|
|
20325
20329
|
}: {
|
|
20326
|
-
|
|
20330
|
+
snapshot: EditorSnapshot
|
|
20327
20331
|
event: PickFromUnion<ConverterEvent<TMIMEType>, 'type', 'serialize'>
|
|
20328
20332
|
}) => PickFromUnion<
|
|
20329
20333
|
ConverterEvent<TMIMEType>,
|
package/lib/plugins/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
var reactCompilerRuntime = require("react-compiler-runtime"), React = require("react"), plugin_eventListener = require("../_chunks-cjs/plugin.event-listener.cjs"), behavior_markdown = require("../_chunks-cjs/behavior.markdown.cjs"), react = require("@xstate/react"), isEqual = require("lodash/isEqual.js"), xstate = require("xstate"), selector_isAtTheStartOfBlock = require("../_chunks-cjs/selector.is-at-the-start-of-block.cjs"), util_sliceBlocks = require("../_chunks-cjs/util.slice-blocks.cjs"), util_blockOffsetsToSelection = require("../_chunks-cjs/util.block-offsets-to-selection.cjs"), utils_index = require("../utils/index.cjs"), selector_getTextBefore = require("../_chunks-cjs/selector.get-text-before.cjs"), behavior_core = require("../_chunks-cjs/behavior.core.cjs")
|
|
3
|
+
var reactCompilerRuntime = require("react-compiler-runtime"), React = require("react"), plugin_eventListener = require("../_chunks-cjs/plugin.event-listener.cjs"), jsxRuntime = require("react/jsx-runtime"), behavior_markdown = require("../_chunks-cjs/behavior.markdown.cjs"), react = require("@xstate/react"), isEqual = require("lodash/isEqual.js"), xstate = require("xstate"), selector_isAtTheStartOfBlock = require("../_chunks-cjs/selector.is-at-the-start-of-block.cjs"), util_sliceBlocks = require("../_chunks-cjs/util.slice-blocks.cjs"), util_blockOffsetsToSelection = require("../_chunks-cjs/util.block-offsets-to-selection.cjs"), utils_index = require("../utils/index.cjs"), selector_getTextBefore = require("../_chunks-cjs/selector.get-text-before.cjs"), behavior_core = require("../_chunks-cjs/behavior.core.cjs");
|
|
4
4
|
function _interopDefaultCompat(e) {
|
|
5
5
|
return e && typeof e == "object" && "default" in e ? e : { default: e };
|
|
6
6
|
}
|
|
@@ -26,114 +26,72 @@ const EditorRefPlugin = React__default.default.forwardRef((_, ref) => {
|
|
|
26
26
|
return $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = () => portableTextEditorRef.current, t1 = [], $[0] = t0, $[1] = t1) : (t0 = $[0], t1 = $[1]), React__default.default.useImperativeHandle(ref, t0, t1), null;
|
|
27
27
|
});
|
|
28
28
|
EditorRefPlugin.displayName = "EditorRefPlugin";
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return
|
|
29
|
+
function createPairRegex(char, amount) {
|
|
30
|
+
const prePrefix = `(?<!\\${char})`, prefix = `\\${char}`.repeat(Math.max(amount, 1)), postPrefix = "(?!\\s)", content = `([^${char}\\n]+?)`, preSuffix = "(?<!\\s)", suffix = `\\${char}`.repeat(Math.max(amount, 1)), postSuffix = `(?!\\${char})`;
|
|
31
|
+
return `${prePrefix}${prefix}${postPrefix}${content}${preSuffix}${suffix}${postSuffix}`;
|
|
32
32
|
}
|
|
33
|
-
function
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const $ = reactCompilerRuntime.c(10), editor = plugin_eventListener.useEditor();
|
|
38
|
-
let t0;
|
|
39
|
-
$[0] !== editor || $[1] !== props.config ? (t0 = props.config.boldDecorator?.({
|
|
40
|
-
schema: editor.getSnapshot().context.schema
|
|
41
|
-
}), $[0] = editor, $[1] = props.config, $[2] = t0) : t0 = $[2];
|
|
42
|
-
let t1;
|
|
43
|
-
$[3] !== editor || $[4] !== props.config ? (t1 = props.config.italicDecorator?.({
|
|
44
|
-
schema: editor.getSnapshot().context.schema
|
|
45
|
-
}), $[3] = editor, $[4] = props.config, $[5] = t1) : t1 = $[5];
|
|
46
|
-
let t2;
|
|
47
|
-
$[6] !== editor || $[7] !== t0 || $[8] !== t1 ? (t2 = {
|
|
48
|
-
input: {
|
|
49
|
-
editor,
|
|
50
|
-
boldDecorator: t0,
|
|
51
|
-
italicDecorator: t1
|
|
52
|
-
}
|
|
53
|
-
}, $[6] = editor, $[7] = t0, $[8] = t1, $[9] = t2) : t2 = $[9], react.useActorRef(emphasisMachine, t2);
|
|
54
|
-
}
|
|
55
|
-
const emphasisListener = ({
|
|
56
|
-
sendBack,
|
|
57
|
-
input
|
|
58
|
-
}) => input.editor.registerBehavior({
|
|
59
|
-
behavior: behavior_core.defineBehavior({
|
|
33
|
+
function createDecoratorPairBehavior(config) {
|
|
34
|
+
config.pair.amount < 1 && console.warn("The amount of characters in the pair should be greater than 0");
|
|
35
|
+
const pairRegex = createPairRegex(config.pair.char, config.pair.amount), regEx = new RegExp(`(${pairRegex})$`);
|
|
36
|
+
return behavior_core.defineBehavior({
|
|
60
37
|
on: "insert.text",
|
|
61
38
|
guard: ({
|
|
62
|
-
|
|
39
|
+
snapshot,
|
|
63
40
|
event
|
|
64
41
|
}) => {
|
|
65
|
-
|
|
66
|
-
if (boldDecorator === void 0 && italicDecorator === void 0)
|
|
42
|
+
if (config.pair.amount < 1)
|
|
67
43
|
return !1;
|
|
68
|
-
const
|
|
69
|
-
context
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
44
|
+
const decorator = config.decorator({
|
|
45
|
+
schema: snapshot.context.schema
|
|
46
|
+
});
|
|
47
|
+
if (decorator === void 0)
|
|
48
|
+
return !1;
|
|
49
|
+
const focusTextBlock = selector_isAtTheStartOfBlock.getFocusTextBlock(snapshot), selectionStartPoint = selector_isAtTheStartOfBlock.getSelectionStartPoint(snapshot), selectionStartOffset = selectionStartPoint ? util_sliceBlocks.spanSelectionPointToBlockOffset({
|
|
50
|
+
value: snapshot.context.value,
|
|
74
51
|
selectionPoint: selectionStartPoint
|
|
75
52
|
}) : void 0;
|
|
76
53
|
if (!focusTextBlock || !selectionStartOffset)
|
|
77
54
|
return !1;
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
suffixOffsets,
|
|
108
|
-
decorator: italicDecorator
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
const textToBold = getTextToBold(`${textBefore}${event.text}`);
|
|
112
|
-
if (textToBold !== void 0 && boldDecorator !== void 0) {
|
|
113
|
-
const prefixOffsets = {
|
|
114
|
-
anchor: {
|
|
115
|
-
path: focusTextBlock.path,
|
|
116
|
-
// Example: "foo **bar**".length - "**bar**".length = 4
|
|
117
|
-
offset: `${textBefore}${event.text}`.length - textToBold.length
|
|
118
|
-
},
|
|
119
|
-
focus: {
|
|
120
|
-
path: focusTextBlock.path,
|
|
121
|
-
// Example: "foo **bar**".length - "**bar**".length + 2 = 6
|
|
122
|
-
offset: `${textBefore}${event.text}`.length - textToBold.length + 2
|
|
123
|
-
}
|
|
124
|
-
}, prefixSelection = util_blockOffsetsToSelection.blockOffsetsToSelection({
|
|
125
|
-
value: context.value,
|
|
55
|
+
const newText = `${selector_getTextBefore.getBlockTextBefore(snapshot)}${event.text}`, textToDecorate = newText.match(regEx)?.at(0);
|
|
56
|
+
if (textToDecorate === void 0)
|
|
57
|
+
return !1;
|
|
58
|
+
const prefixOffsets = {
|
|
59
|
+
anchor: {
|
|
60
|
+
path: focusTextBlock.path,
|
|
61
|
+
// Example: "foo **bar**".length - "**bar**".length = 4
|
|
62
|
+
offset: newText.length - textToDecorate.length
|
|
63
|
+
},
|
|
64
|
+
focus: {
|
|
65
|
+
path: focusTextBlock.path,
|
|
66
|
+
// Example: "foo **bar**".length - "**bar**".length + "*".length * 2 = 6
|
|
67
|
+
offset: newText.length - textToDecorate.length + config.pair.char.length * config.pair.amount
|
|
68
|
+
}
|
|
69
|
+
}, suffixOffsets = {
|
|
70
|
+
anchor: {
|
|
71
|
+
path: focusTextBlock.path,
|
|
72
|
+
// Example: "foo **bar*|" (10) + "*".length - 2 = 9
|
|
73
|
+
offset: selectionStartOffset.offset + event.text.length - config.pair.char.length * config.pair.amount
|
|
74
|
+
},
|
|
75
|
+
focus: {
|
|
76
|
+
path: focusTextBlock.path,
|
|
77
|
+
// Example: "foo **bar*|" (10) + "*".length = 11
|
|
78
|
+
offset: selectionStartOffset.offset + event.text.length
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
if (prefixOffsets.focus.offset - prefixOffsets.anchor.offset > 1) {
|
|
82
|
+
const prefixSelection = util_blockOffsetsToSelection.blockOffsetsToSelection({
|
|
83
|
+
value: snapshot.context.value,
|
|
126
84
|
offsets: prefixOffsets
|
|
127
85
|
}), inlineObjectBeforePrefixFocus = selector_isAtTheStartOfBlock.getPreviousInlineObject({
|
|
128
86
|
context: {
|
|
129
|
-
...context,
|
|
87
|
+
...snapshot.context,
|
|
130
88
|
selection: prefixSelection ? {
|
|
131
89
|
anchor: prefixSelection.focus,
|
|
132
90
|
focus: prefixSelection.focus
|
|
133
91
|
} : null
|
|
134
92
|
}
|
|
135
93
|
}), inlineObjectBeforePrefixFocusOffset = inlineObjectBeforePrefixFocus ? utils_index.childSelectionPointToBlockOffset({
|
|
136
|
-
value: context.value,
|
|
94
|
+
value: snapshot.context.value,
|
|
137
95
|
selectionPoint: {
|
|
138
96
|
path: inlineObjectBeforePrefixFocus.path,
|
|
139
97
|
offset: 0
|
|
@@ -141,68 +99,95 @@ const emphasisListener = ({
|
|
|
141
99
|
}) : void 0;
|
|
142
100
|
if (inlineObjectBeforePrefixFocusOffset && inlineObjectBeforePrefixFocusOffset.offset > prefixOffsets.anchor.offset && inlineObjectBeforePrefixFocusOffset.offset < prefixOffsets.focus.offset)
|
|
143
101
|
return !1;
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
offset: selectionStartOffset.offset + event.text.length - 2
|
|
149
|
-
},
|
|
150
|
-
focus: {
|
|
151
|
-
path: focusTextBlock.path,
|
|
152
|
-
// Example: "foo **bar*|" (10) + "*".length = 11
|
|
153
|
-
offset: selectionStartOffset.offset + event.text.length
|
|
154
|
-
}
|
|
155
|
-
}, previousInlineObject = selector_isAtTheStartOfBlock.getPreviousInlineObject({
|
|
156
|
-
context
|
|
157
|
-
}), previousInlineObjectOffset = previousInlineObject ? utils_index.childSelectionPointToBlockOffset({
|
|
158
|
-
value: context.value,
|
|
102
|
+
}
|
|
103
|
+
if (suffixOffsets.focus.offset - suffixOffsets.anchor.offset > 1) {
|
|
104
|
+
const previousInlineObject = selector_isAtTheStartOfBlock.getPreviousInlineObject(snapshot), previousInlineObjectOffset = previousInlineObject ? utils_index.childSelectionPointToBlockOffset({
|
|
105
|
+
value: snapshot.context.value,
|
|
159
106
|
selectionPoint: {
|
|
160
107
|
path: previousInlineObject.path,
|
|
161
108
|
offset: 0
|
|
162
109
|
}
|
|
163
110
|
}) : void 0;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
suffixOffsets,
|
|
167
|
-
decorator: boldDecorator
|
|
168
|
-
};
|
|
111
|
+
if (previousInlineObjectOffset && previousInlineObjectOffset.offset > suffixOffsets.anchor.offset && previousInlineObjectOffset.offset < suffixOffsets.focus.offset)
|
|
112
|
+
return !1;
|
|
169
113
|
}
|
|
170
|
-
return
|
|
114
|
+
return {
|
|
115
|
+
prefixOffsets,
|
|
116
|
+
suffixOffsets,
|
|
117
|
+
decorator
|
|
118
|
+
};
|
|
171
119
|
},
|
|
172
|
-
actions: [
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
type: "delete.text",
|
|
190
|
-
...prefixOffsets
|
|
191
|
-
}, {
|
|
192
|
-
type: "decorator.remove",
|
|
193
|
-
decorator
|
|
194
|
-
}, {
|
|
195
|
-
type: "effect",
|
|
196
|
-
effect: () => {
|
|
197
|
-
sendBack({
|
|
198
|
-
type: "emphasis.add",
|
|
199
|
-
blockOffset: {
|
|
200
|
-
...suffixOffsets.anchor,
|
|
201
|
-
offset: suffixOffsets.anchor.offset - (prefixOffsets.focus.offset - prefixOffsets.anchor.offset)
|
|
120
|
+
actions: [
|
|
121
|
+
// Insert the text as usual in its own undo step
|
|
122
|
+
({
|
|
123
|
+
event
|
|
124
|
+
}) => [event],
|
|
125
|
+
(_, {
|
|
126
|
+
prefixOffsets,
|
|
127
|
+
suffixOffsets,
|
|
128
|
+
decorator
|
|
129
|
+
}) => [
|
|
130
|
+
// Decorate the text between the prefix and suffix
|
|
131
|
+
{
|
|
132
|
+
type: "decorator.add",
|
|
133
|
+
decorator,
|
|
134
|
+
offsets: {
|
|
135
|
+
anchor: prefixOffsets.focus,
|
|
136
|
+
focus: suffixOffsets.anchor
|
|
202
137
|
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
|
|
138
|
+
},
|
|
139
|
+
// Delete the suffix
|
|
140
|
+
{
|
|
141
|
+
type: "delete.text",
|
|
142
|
+
...suffixOffsets
|
|
143
|
+
},
|
|
144
|
+
// Delete the prefix
|
|
145
|
+
{
|
|
146
|
+
type: "delete.text",
|
|
147
|
+
...prefixOffsets
|
|
148
|
+
},
|
|
149
|
+
// Toggle the decorator off so the next inserted text isn't emphasized
|
|
150
|
+
{
|
|
151
|
+
type: "decorator.remove",
|
|
152
|
+
decorator
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
type: "effect",
|
|
156
|
+
effect: () => {
|
|
157
|
+
config.onDecorate({
|
|
158
|
+
...suffixOffsets.anchor,
|
|
159
|
+
offset: suffixOffsets.anchor.offset - (prefixOffsets.focus.offset - prefixOffsets.anchor.offset)
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
]
|
|
164
|
+
]
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
function DecoratorShortcutPlugin(config) {
|
|
168
|
+
const $ = reactCompilerRuntime.c(4), editor = plugin_eventListener.useEditor();
|
|
169
|
+
let t0;
|
|
170
|
+
return $[0] !== config.decorator || $[1] !== config.pair || $[2] !== editor ? (t0 = {
|
|
171
|
+
input: {
|
|
172
|
+
editor,
|
|
173
|
+
decorator: config.decorator,
|
|
174
|
+
pair: config.pair
|
|
175
|
+
}
|
|
176
|
+
}, $[0] = config.decorator, $[1] = config.pair, $[2] = editor, $[3] = t0) : t0 = $[3], react.useActorRef(decoratorPairMachine, t0), null;
|
|
177
|
+
}
|
|
178
|
+
const emphasisListener = ({
|
|
179
|
+
sendBack,
|
|
180
|
+
input
|
|
181
|
+
}) => input.editor.registerBehavior({
|
|
182
|
+
behavior: createDecoratorPairBehavior({
|
|
183
|
+
decorator: input.decorator,
|
|
184
|
+
pair: input.pair,
|
|
185
|
+
onDecorate: (offset) => {
|
|
186
|
+
sendBack({
|
|
187
|
+
type: "emphasis.add",
|
|
188
|
+
blockOffset: offset
|
|
189
|
+
});
|
|
190
|
+
}
|
|
206
191
|
})
|
|
207
192
|
}), selectionListenerCallback = ({
|
|
208
193
|
sendBack,
|
|
@@ -211,7 +196,7 @@ const emphasisListener = ({
|
|
|
211
196
|
behavior: behavior_core.defineBehavior({
|
|
212
197
|
on: "select",
|
|
213
198
|
guard: ({
|
|
214
|
-
|
|
199
|
+
snapshot,
|
|
215
200
|
event
|
|
216
201
|
}) => {
|
|
217
202
|
if (!event.selection)
|
|
@@ -219,10 +204,10 @@ const emphasisListener = ({
|
|
|
219
204
|
blockOffsets: void 0
|
|
220
205
|
};
|
|
221
206
|
const anchor = util_sliceBlocks.spanSelectionPointToBlockOffset({
|
|
222
|
-
value: context.value,
|
|
207
|
+
value: snapshot.context.value,
|
|
223
208
|
selectionPoint: event.selection.anchor
|
|
224
209
|
}), focus = util_sliceBlocks.spanSelectionPointToBlockOffset({
|
|
225
|
-
value: context.value,
|
|
210
|
+
value: snapshot.context.value,
|
|
226
211
|
selectionPoint: event.selection.focus
|
|
227
212
|
});
|
|
228
213
|
return !anchor || !focus ? {
|
|
@@ -263,7 +248,7 @@ const emphasisListener = ({
|
|
|
263
248
|
}
|
|
264
249
|
}]]
|
|
265
250
|
})
|
|
266
|
-
}),
|
|
251
|
+
}), decoratorPairMachine = xstate.setup({
|
|
267
252
|
types: {
|
|
268
253
|
context: {},
|
|
269
254
|
input: {},
|
|
@@ -275,13 +260,13 @@ const emphasisListener = ({
|
|
|
275
260
|
"selection listener": xstate.fromCallback(selectionListenerCallback)
|
|
276
261
|
}
|
|
277
262
|
}).createMachine({
|
|
278
|
-
id: "
|
|
263
|
+
id: "decorator pair",
|
|
279
264
|
context: ({
|
|
280
265
|
input
|
|
281
266
|
}) => ({
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
267
|
+
decorator: input.decorator,
|
|
268
|
+
editor: input.editor,
|
|
269
|
+
pair: input.pair
|
|
285
270
|
}),
|
|
286
271
|
initial: "idle",
|
|
287
272
|
states: {
|
|
@@ -291,9 +276,9 @@ const emphasisListener = ({
|
|
|
291
276
|
input: ({
|
|
292
277
|
context
|
|
293
278
|
}) => ({
|
|
279
|
+
decorator: context.decorator,
|
|
294
280
|
editor: context.editor,
|
|
295
|
-
|
|
296
|
-
italicDecorator: context.italicDecorator
|
|
281
|
+
pair: context.pair
|
|
297
282
|
})
|
|
298
283
|
}],
|
|
299
284
|
on: {
|
|
@@ -346,9 +331,7 @@ const emphasisListener = ({
|
|
|
346
331
|
});
|
|
347
332
|
function MarkdownPlugin(props) {
|
|
348
333
|
const editor = plugin_eventListener.useEditor();
|
|
349
|
-
return
|
|
350
|
-
config: props.config
|
|
351
|
-
}), React.useEffect(() => {
|
|
334
|
+
return React.useEffect(() => {
|
|
352
335
|
const unregisterBehaviors = behavior_markdown.createMarkdownBehaviors(props.config).map((behavior) => editor.registerBehavior({
|
|
353
336
|
behavior
|
|
354
337
|
}));
|
|
@@ -356,7 +339,36 @@ function MarkdownPlugin(props) {
|
|
|
356
339
|
for (const unregisterBehavior of unregisterBehaviors)
|
|
357
340
|
unregisterBehavior();
|
|
358
341
|
};
|
|
359
|
-
}, [editor, props.config]),
|
|
342
|
+
}, [editor, props.config]), /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
343
|
+
props.config.boldDecorator ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
344
|
+
/* @__PURE__ */ jsxRuntime.jsx(DecoratorShortcutPlugin, { decorator: props.config.boldDecorator, pair: {
|
|
345
|
+
char: "*",
|
|
346
|
+
amount: 2
|
|
347
|
+
} }),
|
|
348
|
+
/* @__PURE__ */ jsxRuntime.jsx(DecoratorShortcutPlugin, { decorator: props.config.boldDecorator, pair: {
|
|
349
|
+
char: "_",
|
|
350
|
+
amount: 2
|
|
351
|
+
} })
|
|
352
|
+
] }) : null,
|
|
353
|
+
props.config.codeDecorator ? /* @__PURE__ */ jsxRuntime.jsx(DecoratorShortcutPlugin, { decorator: props.config.codeDecorator, pair: {
|
|
354
|
+
char: "`",
|
|
355
|
+
amount: 1
|
|
356
|
+
} }) : null,
|
|
357
|
+
props.config.italicDecorator ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
358
|
+
/* @__PURE__ */ jsxRuntime.jsx(DecoratorShortcutPlugin, { decorator: props.config.italicDecorator, pair: {
|
|
359
|
+
char: "*",
|
|
360
|
+
amount: 1
|
|
361
|
+
} }),
|
|
362
|
+
/* @__PURE__ */ jsxRuntime.jsx(DecoratorShortcutPlugin, { decorator: props.config.italicDecorator, pair: {
|
|
363
|
+
char: "_",
|
|
364
|
+
amount: 1
|
|
365
|
+
} })
|
|
366
|
+
] }) : null,
|
|
367
|
+
props.config.strikeThroughDecorator ? /* @__PURE__ */ jsxRuntime.jsx(DecoratorShortcutPlugin, { decorator: props.config.strikeThroughDecorator, pair: {
|
|
368
|
+
char: "~",
|
|
369
|
+
amount: 2
|
|
370
|
+
} }) : null
|
|
371
|
+
] });
|
|
360
372
|
}
|
|
361
373
|
const oneLineBehaviors = [
|
|
362
374
|
/**
|
|
@@ -365,12 +377,8 @@ const oneLineBehaviors = [
|
|
|
365
377
|
*/
|
|
366
378
|
behavior_core.defineBehavior({
|
|
367
379
|
on: "insert.break",
|
|
368
|
-
guard: ({
|
|
369
|
-
context
|
|
370
|
-
}) => context.selection && selector_isAtTheStartOfBlock.isSelectionExpanded({
|
|
371
|
-
context
|
|
372
|
-
}) ? {
|
|
373
|
-
selection: context.selection
|
|
380
|
+
guard: (snapshot) => snapshot.context.selection && selector_isAtTheStartOfBlock.isSelectionExpanded(snapshot) ? {
|
|
381
|
+
selection: snapshot.context.selection
|
|
374
382
|
} : !1,
|
|
375
383
|
actions: [(_, {
|
|
376
384
|
selection
|
|
@@ -409,17 +417,11 @@ const oneLineBehaviors = [
|
|
|
409
417
|
behavior_core.defineBehavior({
|
|
410
418
|
on: "insert.block",
|
|
411
419
|
guard: ({
|
|
412
|
-
|
|
420
|
+
snapshot,
|
|
413
421
|
event
|
|
414
422
|
}) => {
|
|
415
|
-
const focusTextBlock = selector_isAtTheStartOfBlock.getFocusTextBlock(
|
|
416
|
-
|
|
417
|
-
}), selectionStartPoint = selector_isAtTheStartOfBlock.getSelectionStartPoint({
|
|
418
|
-
context
|
|
419
|
-
}), selectionEndPoint = selector_isAtTheStartOfBlock.getSelectionEndPoint({
|
|
420
|
-
context
|
|
421
|
-
});
|
|
422
|
-
if (!focusTextBlock || !utils_index.isTextBlock(context, event.block) || !selectionStartPoint || !selectionEndPoint)
|
|
423
|
+
const focusTextBlock = selector_isAtTheStartOfBlock.getFocusTextBlock(snapshot), selectionStartPoint = selector_isAtTheStartOfBlock.getSelectionStartPoint(snapshot), selectionEndPoint = selector_isAtTheStartOfBlock.getSelectionEndPoint(snapshot);
|
|
424
|
+
if (!focusTextBlock || !utils_index.isTextBlock(snapshot.context, event.block) || !selectionStartPoint || !selectionEndPoint)
|
|
423
425
|
return !1;
|
|
424
426
|
const blockStartPoint = util_sliceBlocks.getBlockStartPoint(focusTextBlock), blockEndPoint = util_sliceBlocks.getBlockEndPoint(focusTextBlock), newFocus = util_sliceBlocks.getBlockEndPoint({
|
|
425
427
|
node: event.block,
|
|
@@ -459,31 +461,25 @@ const oneLineBehaviors = [
|
|
|
459
461
|
behavior_core.defineBehavior({
|
|
460
462
|
on: "insert.block",
|
|
461
463
|
guard: ({
|
|
462
|
-
|
|
464
|
+
snapshot,
|
|
463
465
|
event
|
|
464
466
|
}) => {
|
|
465
|
-
const focusTextBlock = selector_isAtTheStartOfBlock.getFocusTextBlock(
|
|
466
|
-
|
|
467
|
-
}), selectionStartPoint = selector_isAtTheStartOfBlock.getSelectionStartPoint({
|
|
468
|
-
context
|
|
469
|
-
}), selectionEndPoint = selector_isAtTheStartOfBlock.getSelectionEndPoint({
|
|
470
|
-
context
|
|
471
|
-
});
|
|
472
|
-
if (!focusTextBlock || !utils_index.isTextBlock(context, event.block) || !selectionStartPoint || !selectionEndPoint)
|
|
467
|
+
const focusTextBlock = selector_isAtTheStartOfBlock.getFocusTextBlock(snapshot), selectionStartPoint = selector_isAtTheStartOfBlock.getSelectionStartPoint(snapshot), selectionEndPoint = selector_isAtTheStartOfBlock.getSelectionEndPoint(snapshot);
|
|
468
|
+
if (!focusTextBlock || !utils_index.isTextBlock(snapshot.context, event.block) || !selectionStartPoint || !selectionEndPoint)
|
|
473
469
|
return !1;
|
|
474
470
|
const blockBeforeStartPoint = utils_index.splitTextBlock({
|
|
475
|
-
context,
|
|
471
|
+
context: snapshot.context,
|
|
476
472
|
block: focusTextBlock.node,
|
|
477
473
|
point: selectionStartPoint
|
|
478
474
|
})?.before, blockAfterEndPoint = utils_index.splitTextBlock({
|
|
479
|
-
context,
|
|
475
|
+
context: snapshot.context,
|
|
480
476
|
block: focusTextBlock.node,
|
|
481
477
|
point: selectionEndPoint
|
|
482
478
|
})?.after;
|
|
483
479
|
if (!blockBeforeStartPoint || !blockAfterEndPoint)
|
|
484
480
|
return !1;
|
|
485
481
|
const targetBlock = utils_index.mergeTextBlocks({
|
|
486
|
-
context,
|
|
482
|
+
context: snapshot.context,
|
|
487
483
|
targetBlock: blockBeforeStartPoint,
|
|
488
484
|
incomingBlock: event.block
|
|
489
485
|
}), newFocus = util_sliceBlocks.getBlockEndPoint({
|
|
@@ -492,7 +488,7 @@ const oneLineBehaviors = [
|
|
|
492
488
|
_key: targetBlock._key
|
|
493
489
|
}]
|
|
494
490
|
}), mergedBlock = utils_index.mergeTextBlocks({
|
|
495
|
-
context,
|
|
491
|
+
context: snapshot.context,
|
|
496
492
|
targetBlock,
|
|
497
493
|
incomingBlock: blockAfterEndPoint
|
|
498
494
|
});
|