@portabletext/plugin-character-pair-decorator 1.1.18 → 2.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/plugin-character-pair-decorator",
3
- "version": "1.1.18",
3
+ "version": "2.0.0",
4
4
  "description": "Automatically match a pair of characters and decorate the text in between",
5
5
  "keywords": [
6
6
  "portabletext",
@@ -25,14 +25,11 @@
25
25
  "exports": {
26
26
  ".": {
27
27
  "source": "./src/index.ts",
28
- "import": "./dist/index.js",
29
- "require": "./dist/index.cjs",
30
28
  "default": "./dist/index.js"
31
29
  },
32
30
  "./package.json": "./package.json"
33
31
  },
34
- "main": "./dist/index.cjs",
35
- "module": "./dist/index.js",
32
+ "main": "./dist/index.js",
36
33
  "types": "./dist/index.d.ts",
37
34
  "files": [
38
35
  "src",
@@ -52,11 +49,14 @@
52
49
  "typescript": "5.9.3",
53
50
  "typescript-eslint": "^8.46.1",
54
51
  "vitest": "^4.0.5",
55
- "@portabletext/editor": "^2.17.2"
52
+ "@portabletext/editor": "^2.18.0"
56
53
  },
57
54
  "peerDependencies": {
58
55
  "react": "^19",
59
- "@portabletext/editor": "^2.17.2"
56
+ "@portabletext/editor": "^2.18.0"
57
+ },
58
+ "engines": {
59
+ "node": ">=20.19 <22 || >=22.12"
60
60
  },
61
61
  "scripts": {
62
62
  "build": "pkg-utils build --strict --check --clean",
package/dist/index.cjs DELETED
@@ -1,321 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var compilerRuntime = require("react/compiler-runtime"), editor = require("@portabletext/editor"), behaviors = require("@portabletext/editor/behaviors"), utils = require("@portabletext/editor/utils"), react = require("@xstate/react"), remeda = require("remeda"), xstate = require("xstate"), selectors = require("@portabletext/editor/selectors");
4
- function _interopNamespaceCompat(e) {
5
- if (e && typeof e == "object" && "default" in e) return e;
6
- var n = /* @__PURE__ */ Object.create(null);
7
- return e && Object.keys(e).forEach(function(k) {
8
- if (k !== "default") {
9
- var d = Object.getOwnPropertyDescriptor(e, k);
10
- Object.defineProperty(n, k, d.get ? d : {
11
- enumerable: !0,
12
- get: function() {
13
- return e[k];
14
- }
15
- });
16
- }
17
- }), n.default = e, Object.freeze(n);
18
- }
19
- var utils__namespace = /* @__PURE__ */ _interopNamespaceCompat(utils), selectors__namespace = /* @__PURE__ */ _interopNamespaceCompat(selectors);
20
- function createCharacterPairRegex(char, amount) {
21
- 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})`;
22
- return `${prePrefix}${prefix}${postPrefix}${content}${preSuffix}${suffix}${postSuffix}`;
23
- }
24
- function createCharacterPairDecoratorBehavior(config) {
25
- config.pair.amount < 1 && console.warn("The amount of characters in the pair should be greater than 0");
26
- const pairRegex = createCharacterPairRegex(config.pair.char, config.pair.amount), regEx = new RegExp(`(${pairRegex})$`);
27
- return behaviors.defineBehavior({
28
- on: "insert.text",
29
- guard: ({
30
- snapshot,
31
- event
32
- }) => {
33
- if (config.pair.amount < 1)
34
- return !1;
35
- const decorator = config.decorator({
36
- schema: snapshot.context.schema
37
- });
38
- if (decorator === void 0)
39
- return !1;
40
- const focusTextBlock = selectors__namespace.getFocusTextBlock(snapshot), selectionStartPoint = selectors__namespace.getSelectionStartPoint(snapshot), selectionStartOffset = selectionStartPoint ? utils__namespace.spanSelectionPointToBlockOffset({
41
- context: snapshot.context,
42
- selectionPoint: selectionStartPoint
43
- }) : void 0;
44
- if (!focusTextBlock || !selectionStartOffset)
45
- return !1;
46
- const newText = `${selectors__namespace.getBlockTextBefore(snapshot)}${event.text}`, textToDecorate = newText.match(regEx)?.at(0);
47
- if (textToDecorate === void 0)
48
- return !1;
49
- const prefixOffsets = {
50
- anchor: {
51
- path: focusTextBlock.path,
52
- // Example: "foo **bar**".length - "**bar**".length = 4
53
- offset: newText.length - textToDecorate.length
54
- },
55
- focus: {
56
- path: focusTextBlock.path,
57
- // Example: "foo **bar**".length - "**bar**".length + "*".length * 2 = 6
58
- offset: newText.length - textToDecorate.length + config.pair.char.length * config.pair.amount
59
- }
60
- }, suffixOffsets = {
61
- anchor: {
62
- path: focusTextBlock.path,
63
- // Example: "foo **bar*|" (10) + "*".length - 2 = 9
64
- offset: selectionStartOffset.offset + event.text.length - config.pair.char.length * config.pair.amount
65
- },
66
- focus: {
67
- path: focusTextBlock.path,
68
- // Example: "foo **bar*|" (10) + "*".length = 11
69
- offset: selectionStartOffset.offset + event.text.length
70
- }
71
- };
72
- if (prefixOffsets.focus.offset - prefixOffsets.anchor.offset > 1) {
73
- const prefixSelection = utils__namespace.blockOffsetsToSelection({
74
- context: snapshot.context,
75
- offsets: prefixOffsets
76
- }), inlineObjectBeforePrefixFocus = selectors__namespace.getPreviousInlineObject({
77
- ...snapshot,
78
- context: {
79
- ...snapshot.context,
80
- selection: prefixSelection ? {
81
- anchor: prefixSelection.focus,
82
- focus: prefixSelection.focus
83
- } : null
84
- }
85
- }), inlineObjectBeforePrefixFocusOffset = inlineObjectBeforePrefixFocus ? utils__namespace.childSelectionPointToBlockOffset({
86
- context: snapshot.context,
87
- selectionPoint: {
88
- path: inlineObjectBeforePrefixFocus.path,
89
- offset: 0
90
- }
91
- }) : void 0;
92
- if (inlineObjectBeforePrefixFocusOffset && inlineObjectBeforePrefixFocusOffset.offset > prefixOffsets.anchor.offset && inlineObjectBeforePrefixFocusOffset.offset < prefixOffsets.focus.offset)
93
- return !1;
94
- }
95
- if (suffixOffsets.focus.offset - suffixOffsets.anchor.offset > 1) {
96
- const previousInlineObject = selectors__namespace.getPreviousInlineObject(snapshot), previousInlineObjectOffset = previousInlineObject ? utils__namespace.childSelectionPointToBlockOffset({
97
- context: snapshot.context,
98
- selectionPoint: {
99
- path: previousInlineObject.path,
100
- offset: 0
101
- }
102
- }) : void 0;
103
- if (previousInlineObjectOffset && previousInlineObjectOffset.offset > suffixOffsets.anchor.offset && previousInlineObjectOffset.offset < suffixOffsets.focus.offset)
104
- return !1;
105
- }
106
- return {
107
- prefixOffsets,
108
- suffixOffsets,
109
- decorator
110
- };
111
- },
112
- actions: [
113
- // Insert the text as usual in its own undo step
114
- ({
115
- event
116
- }) => [behaviors.execute(event)],
117
- (_, {
118
- prefixOffsets,
119
- suffixOffsets,
120
- decorator
121
- }) => [
122
- // Decorate the text between the prefix and suffix
123
- behaviors.execute({
124
- type: "decorator.add",
125
- decorator,
126
- at: {
127
- anchor: prefixOffsets.focus,
128
- focus: suffixOffsets.anchor
129
- }
130
- }),
131
- // Delete the suffix
132
- behaviors.execute({
133
- type: "delete.text",
134
- at: suffixOffsets
135
- }),
136
- // Delete the prefix
137
- behaviors.execute({
138
- type: "delete.text",
139
- at: prefixOffsets
140
- }),
141
- // Toggle the decorator off so the next inserted text isn't emphasized
142
- behaviors.execute({
143
- type: "decorator.remove",
144
- decorator
145
- }),
146
- behaviors.effect(() => {
147
- config.onDecorate({
148
- ...suffixOffsets.anchor,
149
- offset: suffixOffsets.anchor.offset - (prefixOffsets.focus.offset - prefixOffsets.anchor.offset)
150
- });
151
- })
152
- ]
153
- ]
154
- });
155
- }
156
- function CharacterPairDecoratorPlugin(config) {
157
- const $ = compilerRuntime.c(4), editor$1 = editor.useEditor();
158
- let t0;
159
- return $[0] !== config.decorator || $[1] !== config.pair || $[2] !== editor$1 ? (t0 = {
160
- input: {
161
- editor: editor$1,
162
- decorator: config.decorator,
163
- pair: config.pair
164
- }
165
- }, $[0] = config.decorator, $[1] = config.pair, $[2] = editor$1, $[3] = t0) : t0 = $[3], react.useActorRef(decoratorPairMachine, t0), null;
166
- }
167
- const decorateListener = ({
168
- sendBack,
169
- input
170
- }) => input.editor.registerBehavior({
171
- behavior: createCharacterPairDecoratorBehavior({
172
- decorator: input.decorator,
173
- pair: input.pair,
174
- onDecorate: (offset) => {
175
- sendBack({
176
- type: "decorator.add",
177
- blockOffset: offset
178
- });
179
- }
180
- })
181
- }), selectionListenerCallback = ({
182
- sendBack,
183
- input
184
- }) => input.editor.registerBehavior({
185
- behavior: behaviors.defineBehavior({
186
- on: "select",
187
- guard: ({
188
- snapshot,
189
- event
190
- }) => {
191
- if (!event.at)
192
- return {
193
- blockOffsets: void 0
194
- };
195
- const anchor = utils__namespace.spanSelectionPointToBlockOffset({
196
- context: snapshot.context,
197
- selectionPoint: event.at.anchor
198
- }), focus = utils__namespace.spanSelectionPointToBlockOffset({
199
- context: snapshot.context,
200
- selectionPoint: event.at.focus
201
- });
202
- return !anchor || !focus ? {
203
- blockOffsets: void 0
204
- } : {
205
- blockOffsets: {
206
- anchor,
207
- focus
208
- }
209
- };
210
- },
211
- actions: [({
212
- event
213
- }, {
214
- blockOffsets
215
- }) => [{
216
- type: "effect",
217
- effect: () => {
218
- sendBack({
219
- type: "selection",
220
- blockOffsets
221
- });
222
- }
223
- }, behaviors.forward(event)]]
224
- })
225
- }), deleteBackwardListenerCallback = ({
226
- sendBack,
227
- input
228
- }) => input.editor.registerBehavior({
229
- behavior: behaviors.defineBehavior({
230
- on: "delete.backward",
231
- actions: [() => [behaviors.execute({
232
- type: "history.undo"
233
- }), behaviors.effect(() => {
234
- sendBack({
235
- type: "delete.backward"
236
- });
237
- })]]
238
- })
239
- }), decoratorPairMachine = xstate.setup({
240
- types: {
241
- context: {},
242
- input: {},
243
- events: {}
244
- },
245
- actors: {
246
- "decorate listener": xstate.fromCallback(decorateListener),
247
- "delete.backward listener": xstate.fromCallback(deleteBackwardListenerCallback),
248
- "selection listener": xstate.fromCallback(selectionListenerCallback)
249
- }
250
- }).createMachine({
251
- id: "decorator pair",
252
- context: ({
253
- input
254
- }) => ({
255
- decorator: input.decorator,
256
- editor: input.editor,
257
- pair: input.pair
258
- }),
259
- initial: "idle",
260
- states: {
261
- idle: {
262
- invoke: [{
263
- src: "decorate listener",
264
- input: ({
265
- context
266
- }) => ({
267
- decorator: context.decorator,
268
- editor: context.editor,
269
- pair: context.pair
270
- })
271
- }],
272
- on: {
273
- "decorator.add": {
274
- target: "decorator added",
275
- actions: xstate.assign({
276
- offsetAfterDecorator: ({
277
- event
278
- }) => event.blockOffset
279
- })
280
- }
281
- }
282
- },
283
- "decorator added": {
284
- exit: [xstate.assign({
285
- offsetAfterDecorator: void 0
286
- })],
287
- invoke: [{
288
- src: "selection listener",
289
- input: ({
290
- context
291
- }) => ({
292
- editor: context.editor
293
- })
294
- }, {
295
- src: "delete.backward listener",
296
- input: ({
297
- context
298
- }) => ({
299
- editor: context.editor
300
- })
301
- }],
302
- on: {
303
- selection: {
304
- target: "idle",
305
- guard: ({
306
- context,
307
- event
308
- }) => !remeda.isDeepEqual({
309
- anchor: context.offsetAfterDecorator,
310
- focus: context.offsetAfterDecorator
311
- }, event.blockOffsets)
312
- },
313
- "delete.backward": {
314
- target: "idle"
315
- }
316
- }
317
- }
318
- }
319
- });
320
- exports.CharacterPairDecoratorPlugin = CharacterPairDecoratorPlugin;
321
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/regex.character-pair.ts","../src/behavior.character-pair-decorator.ts","../src/plugin.character-pair-decorator.ts"],"sourcesContent":["export function createCharacterPairRegex(char: string, amount: number) {\n // Negative lookbehind: Ensures that the matched sequence is not preceded by the same character\n const prePrefix = `(?<!\\\\${char})`\n\n // Repeats the character `amount` times\n const prefix = `\\\\${char}`.repeat(Math.max(amount, 1))\n\n // Negative lookahead: Ensures that the opening pair (**, *, etc.) is not followed by a space\n const postPrefix = `(?!\\\\s)`\n\n // Captures the content inside the pair\n const content = `([^${char}\\\\n]+?)`\n\n // Negative lookbehind: Ensures that the content is not followed by a space\n const preSuffix = `(?<!\\\\s)`\n\n // Repeats the character `amount` times\n const suffix = `\\\\${char}`.repeat(Math.max(amount, 1))\n\n // Negative lookahead: Ensures that the matched sequence is not followed by the same character\n const postSuffix = `(?!\\\\${char})`\n\n return `${prePrefix}${prefix}${postPrefix}${content}${preSuffix}${suffix}${postSuffix}`\n}\n","import type {BlockOffset, EditorSchema} from '@portabletext/editor'\nimport {defineBehavior, effect, execute} from '@portabletext/editor/behaviors'\nimport * as selectors from '@portabletext/editor/selectors'\nimport * as utils from '@portabletext/editor/utils'\nimport {createCharacterPairRegex} from './regex.character-pair'\n\nexport function createCharacterPairDecoratorBehavior(config: {\n decorator: ({schema}: {schema: EditorSchema}) => string | undefined\n pair: {char: string; amount: number}\n onDecorate: (offset: BlockOffset) => void\n}) {\n if (config.pair.amount < 1) {\n console.warn(\n `The amount of characters in the pair should be greater than 0`,\n )\n }\n\n const pairRegex = createCharacterPairRegex(\n config.pair.char,\n config.pair.amount,\n )\n const regEx = new RegExp(`(${pairRegex})$`)\n\n return defineBehavior({\n on: 'insert.text',\n guard: ({snapshot, event}) => {\n if (config.pair.amount < 1) {\n return false\n }\n\n const decorator = config.decorator({schema: snapshot.context.schema})\n\n if (decorator === undefined) {\n return false\n }\n\n const focusTextBlock = selectors.getFocusTextBlock(snapshot)\n const selectionStartPoint = selectors.getSelectionStartPoint(snapshot)\n const selectionStartOffset = selectionStartPoint\n ? utils.spanSelectionPointToBlockOffset({\n context: snapshot.context,\n selectionPoint: selectionStartPoint,\n })\n : undefined\n\n if (!focusTextBlock || !selectionStartOffset) {\n return false\n }\n\n const textBefore = selectors.getBlockTextBefore(snapshot)\n const newText = `${textBefore}${event.text}`\n const textToDecorate = newText.match(regEx)?.at(0)\n\n if (textToDecorate === undefined) {\n return false\n }\n\n const prefixOffsets = {\n anchor: {\n path: focusTextBlock.path,\n // Example: \"foo **bar**\".length - \"**bar**\".length = 4\n offset: newText.length - textToDecorate.length,\n },\n focus: {\n path: focusTextBlock.path,\n // Example: \"foo **bar**\".length - \"**bar**\".length + \"*\".length * 2 = 6\n offset:\n newText.length -\n textToDecorate.length +\n config.pair.char.length * config.pair.amount,\n },\n }\n\n const suffixOffsets = {\n anchor: {\n path: focusTextBlock.path,\n // Example: \"foo **bar*|\" (10) + \"*\".length - 2 = 9\n offset:\n selectionStartOffset.offset +\n event.text.length -\n config.pair.char.length * config.pair.amount,\n },\n focus: {\n path: focusTextBlock.path,\n // Example: \"foo **bar*|\" (10) + \"*\".length = 11\n offset: selectionStartOffset.offset + event.text.length,\n },\n }\n\n // If the prefix is more than one character, then we need to check if\n // there is an inline object inside it\n if (prefixOffsets.focus.offset - prefixOffsets.anchor.offset > 1) {\n const prefixSelection = utils.blockOffsetsToSelection({\n context: snapshot.context,\n offsets: prefixOffsets,\n })\n const inlineObjectBeforePrefixFocus = selectors.getPreviousInlineObject(\n {\n ...snapshot,\n context: {\n ...snapshot.context,\n selection: prefixSelection\n ? {\n anchor: prefixSelection.focus,\n focus: prefixSelection.focus,\n }\n : null,\n },\n },\n )\n const inlineObjectBeforePrefixFocusOffset =\n inlineObjectBeforePrefixFocus\n ? utils.childSelectionPointToBlockOffset({\n context: snapshot.context,\n selectionPoint: {\n path: inlineObjectBeforePrefixFocus.path,\n offset: 0,\n },\n })\n : undefined\n\n if (\n inlineObjectBeforePrefixFocusOffset &&\n inlineObjectBeforePrefixFocusOffset.offset >\n prefixOffsets.anchor.offset &&\n inlineObjectBeforePrefixFocusOffset.offset <\n prefixOffsets.focus.offset\n ) {\n return false\n }\n }\n\n // If the suffix is more than one character, then we need to check if\n // there is an inline object inside it\n if (suffixOffsets.focus.offset - suffixOffsets.anchor.offset > 1) {\n const previousInlineObject = selectors.getPreviousInlineObject(snapshot)\n const previousInlineObjectOffset = previousInlineObject\n ? utils.childSelectionPointToBlockOffset({\n context: snapshot.context,\n selectionPoint: {\n path: previousInlineObject.path,\n offset: 0,\n },\n })\n : undefined\n\n if (\n previousInlineObjectOffset &&\n previousInlineObjectOffset.offset > suffixOffsets.anchor.offset &&\n previousInlineObjectOffset.offset < suffixOffsets.focus.offset\n ) {\n return false\n }\n }\n\n return {\n prefixOffsets,\n suffixOffsets,\n decorator,\n }\n },\n actions: [\n // Insert the text as usual in its own undo step\n ({event}) => [execute(event)],\n (_, {prefixOffsets, suffixOffsets, decorator}) => [\n // Decorate the text between the prefix and suffix\n execute({\n type: 'decorator.add',\n decorator,\n at: {\n anchor: prefixOffsets.focus,\n focus: suffixOffsets.anchor,\n },\n }),\n // Delete the suffix\n execute({\n type: 'delete.text',\n at: suffixOffsets,\n }),\n // Delete the prefix\n execute({\n type: 'delete.text',\n at: prefixOffsets,\n }),\n // Toggle the decorator off so the next inserted text isn't emphasized\n execute({\n type: 'decorator.remove',\n decorator,\n }),\n effect(() => {\n config.onDecorate({\n ...suffixOffsets.anchor,\n offset:\n suffixOffsets.anchor.offset -\n (prefixOffsets.focus.offset - prefixOffsets.anchor.offset),\n })\n }),\n ],\n ],\n })\n}\n","import type {BlockOffset, Editor, EditorSchema} from '@portabletext/editor'\nimport {useEditor} from '@portabletext/editor'\nimport {\n defineBehavior,\n effect,\n execute,\n forward,\n} from '@portabletext/editor/behaviors'\nimport * as utils from '@portabletext/editor/utils'\nimport {useActorRef} from '@xstate/react'\nimport {isDeepEqual} from 'remeda'\nimport {\n assign,\n fromCallback,\n setup,\n type AnyEventObject,\n type CallbackLogicFunction,\n} from 'xstate'\nimport {createCharacterPairDecoratorBehavior} from './behavior.character-pair-decorator'\n\n/**\n * @beta\n */\nexport function CharacterPairDecoratorPlugin(config: {\n decorator: ({schema}: {schema: EditorSchema}) => string | undefined\n pair: {char: string; amount: number}\n}) {\n const editor = useEditor()\n\n useActorRef(decoratorPairMachine, {\n input: {\n editor,\n decorator: config.decorator,\n pair: config.pair,\n },\n })\n\n return null\n}\n\ntype DecoratorPairEvent =\n | {\n type: 'decorator.add'\n blockOffset: BlockOffset\n }\n | {\n type: 'selection'\n blockOffsets?: {\n anchor: BlockOffset\n focus: BlockOffset\n }\n }\n | {\n type: 'delete.backward'\n }\n\nconst decorateListener: CallbackLogicFunction<\n AnyEventObject,\n DecoratorPairEvent,\n {\n decorator: ({schema}: {schema: EditorSchema}) => string | undefined\n editor: Editor\n pair: {char: string; amount: number}\n }\n> = ({sendBack, input}) => {\n const unregister = input.editor.registerBehavior({\n behavior: createCharacterPairDecoratorBehavior({\n decorator: input.decorator,\n pair: input.pair,\n onDecorate: (offset) => {\n sendBack({type: 'decorator.add', blockOffset: offset})\n },\n }),\n })\n\n return unregister\n}\n\nconst selectionListenerCallback: CallbackLogicFunction<\n AnyEventObject,\n DecoratorPairEvent,\n {editor: Editor}\n> = ({sendBack, input}) => {\n const unregister = input.editor.registerBehavior({\n behavior: defineBehavior({\n on: 'select',\n guard: ({snapshot, event}) => {\n if (!event.at) {\n return {blockOffsets: undefined}\n }\n\n const anchor = utils.spanSelectionPointToBlockOffset({\n context: snapshot.context,\n selectionPoint: event.at.anchor,\n })\n const focus = utils.spanSelectionPointToBlockOffset({\n context: snapshot.context,\n selectionPoint: event.at.focus,\n })\n\n if (!anchor || !focus) {\n return {blockOffsets: undefined}\n }\n\n return {\n blockOffsets: {\n anchor,\n focus,\n },\n }\n },\n actions: [\n ({event}, {blockOffsets}) => [\n {\n type: 'effect',\n effect: () => {\n sendBack({type: 'selection', blockOffsets})\n },\n },\n forward(event),\n ],\n ],\n }),\n })\n\n return unregister\n}\n\nconst deleteBackwardListenerCallback: CallbackLogicFunction<\n AnyEventObject,\n DecoratorPairEvent,\n {editor: Editor}\n> = ({sendBack, input}) => {\n const unregister = input.editor.registerBehavior({\n behavior: defineBehavior({\n on: 'delete.backward',\n actions: [\n () => [\n execute({\n type: 'history.undo',\n }),\n effect(() => {\n sendBack({type: 'delete.backward'})\n }),\n ],\n ],\n }),\n })\n\n return unregister\n}\n\nconst decoratorPairMachine = setup({\n types: {\n context: {} as {\n decorator: ({schema}: {schema: EditorSchema}) => string | undefined\n editor: Editor\n offsetAfterDecorator?: BlockOffset\n pair: {char: string; amount: number}\n },\n input: {} as {\n decorator: ({schema}: {schema: EditorSchema}) => string | undefined\n editor: Editor\n pair: {char: string; amount: number}\n },\n events: {} as DecoratorPairEvent,\n },\n actors: {\n 'decorate listener': fromCallback(decorateListener),\n 'delete.backward listener': fromCallback(deleteBackwardListenerCallback),\n 'selection listener': fromCallback(selectionListenerCallback),\n },\n}).createMachine({\n id: 'decorator pair',\n context: ({input}) => ({\n decorator: input.decorator,\n editor: input.editor,\n pair: input.pair,\n }),\n initial: 'idle',\n states: {\n 'idle': {\n invoke: [\n {\n src: 'decorate listener',\n input: ({context}) => ({\n decorator: context.decorator,\n editor: context.editor,\n pair: context.pair,\n }),\n },\n ],\n on: {\n 'decorator.add': {\n target: 'decorator added',\n actions: assign({\n offsetAfterDecorator: ({event}) => event.blockOffset,\n }),\n },\n },\n },\n 'decorator added': {\n exit: [\n assign({\n offsetAfterDecorator: undefined,\n }),\n ],\n invoke: [\n {\n src: 'selection listener',\n input: ({context}) => ({editor: context.editor}),\n },\n {\n src: 'delete.backward listener',\n input: ({context}) => ({editor: context.editor}),\n },\n ],\n on: {\n 'selection': {\n target: 'idle',\n guard: ({context, event}) => {\n const selectionChanged = !isDeepEqual(\n {\n anchor: context.offsetAfterDecorator,\n focus: context.offsetAfterDecorator,\n },\n event.blockOffsets,\n )\n\n return selectionChanged\n },\n },\n 'delete.backward': {\n target: 'idle',\n },\n },\n },\n },\n})\n"],"names":["createCharacterPairRegex","char","amount","prePrefix","prefix","repeat","Math","max","postPrefix","content","preSuffix","suffix","postSuffix","createCharacterPairDecoratorBehavior","config","pair","console","warn","pairRegex","regEx","RegExp","defineBehavior","on","guard","snapshot","event","decorator","schema","context","undefined","focusTextBlock","selectors","getFocusTextBlock","selectionStartPoint","getSelectionStartPoint","selectionStartOffset","utils","spanSelectionPointToBlockOffset","selectionPoint","newText","getBlockTextBefore","text","textToDecorate","match","at","prefixOffsets","anchor","path","offset","length","focus","suffixOffsets","prefixSelection","blockOffsetsToSelection","offsets","inlineObjectBeforePrefixFocus","getPreviousInlineObject","selection","inlineObjectBeforePrefixFocusOffset","childSelectionPointToBlockOffset","previousInlineObject","previousInlineObjectOffset","actions","execute","_","type","effect","onDecorate","CharacterPairDecoratorPlugin","$","_c","editor","useEditor","t0","input","useActorRef","decoratorPairMachine","decorateListener","sendBack","registerBehavior","behavior","blockOffset","selectionListenerCallback","blockOffsets","forward","deleteBackwardListenerCallback","setup","types","events","actors","fromCallback","createMachine","id","initial","states","invoke","src","target","assign","offsetAfterDecorator","exit","isDeepEqual"],"mappings":";;;;;;;;;;;;;;;;;;;AAAO,SAASA,yBAAyBC,MAAcC,QAAgB;AAErE,QAAMC,YAAY,SAASF,IAAI,KAGzBG,SAAS,KAAKH,IAAI,GAAGI,OAAOC,KAAKC,IAAIL,QAAQ,CAAC,CAAC,GAG/CM,aAAa,WAGbC,UAAU,MAAMR,IAAI,WAGpBS,YAAY,YAGZC,SAAS,KAAKV,IAAI,GAAGI,OAAOC,KAAKC,IAAIL,QAAQ,CAAC,CAAC,GAG/CU,aAAa,QAAQX,IAAI;AAE/B,SAAO,GAAGE,SAAS,GAAGC,MAAM,GAAGI,UAAU,GAAGC,OAAO,GAAGC,SAAS,GAAGC,MAAM,GAAGC,UAAU;AACvF;ACjBO,SAASC,qCAAqCC,QAIlD;AACGA,SAAOC,KAAKb,SAAS,KACvBc,QAAQC,KACN,+DACF;AAGF,QAAMC,YAAYlB,yBAChBc,OAAOC,KAAKd,MACZa,OAAOC,KAAKb,MACd,GACMiB,QAAQ,IAAIC,OAAO,IAAIF,SAAS,IAAI;AAE1C,SAAOG,yBAAe;AAAA,IACpBC,IAAI;AAAA,IACJC,OAAOA,CAAC;AAAA,MAACC;AAAAA,MAAUC;AAAAA,IAAAA,MAAW;AAC5B,UAAIX,OAAOC,KAAKb,SAAS;AACvB,eAAO;AAGT,YAAMwB,YAAYZ,OAAOY,UAAU;AAAA,QAACC,QAAQH,SAASI,QAAQD;AAAAA,MAAAA,CAAO;AAEpE,UAAID,cAAcG;AAChB,eAAO;AAGT,YAAMC,iBAAiBC,qBAAUC,kBAAkBR,QAAQ,GACrDS,sBAAsBF,qBAAUG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ACRnC,SAASoC,OAAAA,OAAO;AAAA,YACdC,sBAAsBA,CAAC;AAAA,cAAC1E;AAAAA,YAAAA,MAAWA,MAAMwD;AAAAA,UAAAA,CAC1C;AAAA,QAAA;AAAA,MACH;AAAA,IACF;AAAA,IAEF,mBAAmB;AAAA,MACjBmB,MAAM,CACJF,OAAAA,OAAO;AAAA,QACLC,sBAAsBtE;AAAAA,MAAAA,CACvB,CAAC;AAAA,MAEJkE,QAAQ,CACN;AAAA,QACEC,KAAK;AAAA,QACLtB,OAAOA,CAAC;AAAA,UAAC9C;AAAAA,QAAAA,OAAc;AAAA,UAAC2C,QAAQ3C,QAAQ2C;AAAAA,QAAAA;AAAAA,MAAM,GAEhD;AAAA,QACEyB,KAAK;AAAA,QACLtB,OAAOA,CAAC;AAAA,UAAC9C;AAAAA,QAAAA,OAAc;AAAA,UAAC2C,QAAQ3C,QAAQ2C;AAAAA,QAAAA;AAAAA,MAAM,CAC/C;AAAA,MAEHjD,IAAI;AAAA,QACF,WAAa;AAAA,UACX2E,QAAQ;AAAA,UACR1E,OAAOA,CAAC;AAAA,YAACK;AAAAA,YAASH;AAAAA,UAAAA,MACS,CAAC4E,OAAAA,YACxB;AAAA,YACEvD,QAAQlB,QAAQuE;AAAAA,YAChBjD,OAAOtB,QAAQuE;AAAAA,UAAAA,GAEjB1E,MAAM0D,YACR;AAAA,QAAA;AAAA,QAKJ,mBAAmB;AAAA,UACjBc,QAAQ;AAAA,QAAA;AAAA,MACV;AAAA,IACF;AAAA,EACF;AAEJ,CAAC;;"}
package/dist/index.d.cts DELETED
@@ -1,14 +0,0 @@
1
- import type {EditorSchema} from '@portabletext/editor'
2
-
3
- /**
4
- * @beta
5
- */
6
- export declare function CharacterPairDecoratorPlugin(config: {
7
- decorator: ({schema}: {schema: EditorSchema}) => string | undefined
8
- pair: {
9
- char: string
10
- amount: number
11
- }
12
- }): null
13
-
14
- export {}