@portabletext/editor 2.17.2 → 2.18.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.
Files changed (43) hide show
  1. package/lib/_chunks-dts/{behavior.types.action.d.ts → index.d.ts} +9 -9
  2. package/lib/behaviors/index.d.ts +1 -1
  3. package/lib/index.d.ts +1 -1
  4. package/lib/plugins/index.d.ts +2 -2
  5. package/lib/selectors/index.d.ts +2 -2
  6. package/lib/utils/index.d.ts +1 -1
  7. package/package.json +14 -25
  8. package/src/test/vitest/step-definitions.tsx +2 -0
  9. package/src/test/vitest/test-editor.tsx +18 -4
  10. package/lib/_chunks-cjs/selector.get-selection-text.cjs +0 -89
  11. package/lib/_chunks-cjs/selector.get-selection-text.cjs.map +0 -1
  12. package/lib/_chunks-cjs/selector.get-text-before.cjs +0 -34
  13. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +0 -1
  14. package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs +0 -875
  15. package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs.map +0 -1
  16. package/lib/_chunks-cjs/use-editor.cjs +0 -28
  17. package/lib/_chunks-cjs/use-editor.cjs.map +0 -1
  18. package/lib/_chunks-cjs/util.child-selection-point-to-block-offset.cjs +0 -79
  19. package/lib/_chunks-cjs/util.child-selection-point-to-block-offset.cjs.map +0 -1
  20. package/lib/_chunks-cjs/util.get-text-block-text.cjs +0 -473
  21. package/lib/_chunks-cjs/util.get-text-block-text.cjs.map +0 -1
  22. package/lib/_chunks-cjs/util.is-empty-text-block.cjs +0 -38
  23. package/lib/_chunks-cjs/util.is-empty-text-block.cjs.map +0 -1
  24. package/lib/_chunks-cjs/util.merge-text-blocks.cjs +0 -23
  25. package/lib/_chunks-cjs/util.merge-text-blocks.cjs.map +0 -1
  26. package/lib/_chunks-cjs/util.slice-text-block.cjs +0 -63
  27. package/lib/_chunks-cjs/util.slice-text-block.cjs.map +0 -1
  28. package/lib/_chunks-dts/behavior.types.action.d.cts +0 -3650
  29. package/lib/behaviors/index.cjs +0 -35
  30. package/lib/behaviors/index.cjs.map +0 -1
  31. package/lib/behaviors/index.d.cts +0 -2
  32. package/lib/index.cjs +0 -12952
  33. package/lib/index.cjs.map +0 -1
  34. package/lib/index.d.cts +0 -2
  35. package/lib/plugins/index.cjs +0 -826
  36. package/lib/plugins/index.cjs.map +0 -1
  37. package/lib/plugins/index.d.cts +0 -196
  38. package/lib/selectors/index.cjs +0 -243
  39. package/lib/selectors/index.cjs.map +0 -1
  40. package/lib/selectors/index.d.cts +0 -357
  41. package/lib/utils/index.cjs +0 -97
  42. package/lib/utils/index.cjs.map +0 -1
  43. package/lib/utils/index.d.cts +0 -186
@@ -1,826 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var reactCompilerRuntime = require("react-compiler-runtime"), React = require("react"), useEditor = require("../_chunks-cjs/use-editor.cjs"), react = require("@xstate/react"), isEqual = require("lodash/isEqual.js"), xstate = require("xstate"), selector_getSelectionText = require("../_chunks-cjs/selector.get-selection-text.cjs"), selector_getTextBefore = require("../_chunks-cjs/selector.get-text-before.cjs"), util_getTextBlockText = require("../_chunks-cjs/util.get-text-block-text.cjs"), util_childSelectionPointToBlockOffset = require("../_chunks-cjs/util.child-selection-point-to-block-offset.cjs"), behaviors_index = require("../behaviors/index.cjs"), jsxRuntime = require("react/jsx-runtime"), schema = require("@portabletext/schema"), util_mergeTextBlocks = require("../_chunks-cjs/util.merge-text-blocks.cjs");
4
- function _interopDefaultCompat(e) {
5
- return e && typeof e == "object" && "default" in e ? e : { default: e };
6
- }
7
- var React__default = /* @__PURE__ */ _interopDefaultCompat(React), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual);
8
- function BehaviorPlugin(props) {
9
- const $ = reactCompilerRuntime.c(4), editor = useEditor.useEditor();
10
- let t0, t1;
11
- return $[0] !== editor || $[1] !== props.behaviors ? (t0 = () => {
12
- const unregisterBehaviors = props.behaviors.map((behavior) => editor.registerBehavior({
13
- behavior
14
- }));
15
- return () => {
16
- unregisterBehaviors.forEach(_temp);
17
- };
18
- }, t1 = [editor, props.behaviors], $[0] = editor, $[1] = props.behaviors, $[2] = t0, $[3] = t1) : (t0 = $[2], t1 = $[3]), React.useEffect(t0, t1), null;
19
- }
20
- function _temp(unregister) {
21
- unregister();
22
- }
23
- function createPairRegex(char, amount) {
24
- 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})`;
25
- return `${prePrefix}${prefix}${postPrefix}${content}${preSuffix}${suffix}${postSuffix}`;
26
- }
27
- function createDecoratorPairBehavior(config) {
28
- config.pair.amount < 1 && console.warn("The amount of characters in the pair should be greater than 0");
29
- const pairRegex = createPairRegex(config.pair.char, config.pair.amount), regEx = new RegExp(`(${pairRegex})$`);
30
- return behaviors_index.defineBehavior({
31
- on: "insert.text",
32
- guard: ({
33
- snapshot,
34
- event
35
- }) => {
36
- if (config.pair.amount < 1)
37
- return !1;
38
- const decorator = config.decorator({
39
- schema: snapshot.context.schema
40
- });
41
- if (decorator === void 0)
42
- return !1;
43
- const focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot), selectionStartPoint = selector_getSelectionText.getSelectionStartPoint(snapshot), selectionStartOffset = selectionStartPoint ? util_getTextBlockText.spanSelectionPointToBlockOffset({
44
- context: {
45
- schema: snapshot.context.schema,
46
- value: snapshot.context.value
47
- },
48
- selectionPoint: selectionStartPoint
49
- }) : void 0;
50
- if (!focusTextBlock || !selectionStartOffset)
51
- return !1;
52
- const newText = `${selector_getTextBefore.getBlockTextBefore(snapshot)}${event.text}`, textToDecorate = newText.match(regEx)?.at(0);
53
- if (textToDecorate === void 0)
54
- return !1;
55
- const prefixOffsets = {
56
- anchor: {
57
- path: focusTextBlock.path,
58
- // Example: "foo **bar**".length - "**bar**".length = 4
59
- offset: newText.length - textToDecorate.length
60
- },
61
- focus: {
62
- path: focusTextBlock.path,
63
- // Example: "foo **bar**".length - "**bar**".length + "*".length * 2 = 6
64
- offset: newText.length - textToDecorate.length + config.pair.char.length * config.pair.amount
65
- }
66
- }, suffixOffsets = {
67
- anchor: {
68
- path: focusTextBlock.path,
69
- // Example: "foo **bar*|" (10) + "*".length - 2 = 9
70
- offset: selectionStartOffset.offset + event.text.length - config.pair.char.length * config.pair.amount
71
- },
72
- focus: {
73
- path: focusTextBlock.path,
74
- // Example: "foo **bar*|" (10) + "*".length = 11
75
- offset: selectionStartOffset.offset + event.text.length
76
- }
77
- };
78
- if (prefixOffsets.focus.offset - prefixOffsets.anchor.offset > 1) {
79
- const prefixSelection = util_childSelectionPointToBlockOffset.blockOffsetsToSelection({
80
- context: snapshot.context,
81
- offsets: prefixOffsets
82
- }), inlineObjectBeforePrefixFocus = selector_getSelectionText.getPreviousInlineObject({
83
- ...snapshot,
84
- context: {
85
- ...snapshot.context,
86
- selection: prefixSelection ? {
87
- anchor: prefixSelection.focus,
88
- focus: prefixSelection.focus
89
- } : null
90
- }
91
- }), inlineObjectBeforePrefixFocusOffset = inlineObjectBeforePrefixFocus ? util_childSelectionPointToBlockOffset.childSelectionPointToBlockOffset({
92
- context: {
93
- schema: snapshot.context.schema,
94
- value: snapshot.context.value
95
- },
96
- selectionPoint: {
97
- path: inlineObjectBeforePrefixFocus.path,
98
- offset: 0
99
- }
100
- }) : void 0;
101
- if (inlineObjectBeforePrefixFocusOffset && inlineObjectBeforePrefixFocusOffset.offset > prefixOffsets.anchor.offset && inlineObjectBeforePrefixFocusOffset.offset < prefixOffsets.focus.offset)
102
- return !1;
103
- }
104
- if (suffixOffsets.focus.offset - suffixOffsets.anchor.offset > 1) {
105
- const previousInlineObject = selector_getSelectionText.getPreviousInlineObject(snapshot), previousInlineObjectOffset = previousInlineObject ? util_childSelectionPointToBlockOffset.childSelectionPointToBlockOffset({
106
- context: {
107
- schema: snapshot.context.schema,
108
- value: snapshot.context.value
109
- },
110
- selectionPoint: {
111
- path: previousInlineObject.path,
112
- offset: 0
113
- }
114
- }) : void 0;
115
- if (previousInlineObjectOffset && previousInlineObjectOffset.offset > suffixOffsets.anchor.offset && previousInlineObjectOffset.offset < suffixOffsets.focus.offset)
116
- return !1;
117
- }
118
- return {
119
- prefixOffsets,
120
- suffixOffsets,
121
- decorator
122
- };
123
- },
124
- actions: [
125
- // Insert the text as usual in its own undo step
126
- ({
127
- event
128
- }) => [behaviors_index.execute(event)],
129
- (_, {
130
- prefixOffsets,
131
- suffixOffsets,
132
- decorator
133
- }) => [
134
- // Decorate the text between the prefix and suffix
135
- behaviors_index.execute({
136
- type: "decorator.add",
137
- decorator,
138
- at: {
139
- anchor: prefixOffsets.focus,
140
- focus: suffixOffsets.anchor
141
- }
142
- }),
143
- // Delete the suffix
144
- behaviors_index.execute({
145
- type: "delete.text",
146
- at: suffixOffsets
147
- }),
148
- // Delete the prefix
149
- behaviors_index.execute({
150
- type: "delete.text",
151
- at: prefixOffsets
152
- }),
153
- // Toggle the decorator off so the next inserted text isn't emphasized
154
- behaviors_index.execute({
155
- type: "decorator.remove",
156
- decorator
157
- }),
158
- behaviors_index.effect(() => {
159
- config.onDecorate({
160
- ...suffixOffsets.anchor,
161
- offset: suffixOffsets.anchor.offset - (prefixOffsets.focus.offset - prefixOffsets.anchor.offset)
162
- });
163
- })
164
- ]
165
- ]
166
- });
167
- }
168
- function DecoratorShortcutPlugin(config) {
169
- const $ = reactCompilerRuntime.c(4), editor = useEditor.useEditor();
170
- let t0;
171
- return $[0] !== config.decorator || $[1] !== config.pair || $[2] !== editor ? (t0 = {
172
- input: {
173
- editor,
174
- decorator: config.decorator,
175
- pair: config.pair
176
- }
177
- }, $[0] = config.decorator, $[1] = config.pair, $[2] = editor, $[3] = t0) : t0 = $[3], react.useActorRef(decoratorPairMachine, t0), null;
178
- }
179
- const emphasisListener = ({
180
- sendBack,
181
- input
182
- }) => input.editor.registerBehavior({
183
- behavior: createDecoratorPairBehavior({
184
- decorator: input.decorator,
185
- pair: input.pair,
186
- onDecorate: (offset) => {
187
- sendBack({
188
- type: "emphasis.add",
189
- blockOffset: offset
190
- });
191
- }
192
- })
193
- }), selectionListenerCallback = ({
194
- sendBack,
195
- input
196
- }) => input.editor.registerBehavior({
197
- behavior: behaviors_index.defineBehavior({
198
- on: "select",
199
- guard: ({
200
- snapshot,
201
- event
202
- }) => {
203
- if (!event.at)
204
- return {
205
- blockOffsets: void 0
206
- };
207
- const anchor = util_getTextBlockText.spanSelectionPointToBlockOffset({
208
- context: snapshot.context,
209
- selectionPoint: event.at.anchor
210
- }), focus = util_getTextBlockText.spanSelectionPointToBlockOffset({
211
- context: snapshot.context,
212
- selectionPoint: event.at.focus
213
- });
214
- return !anchor || !focus ? {
215
- blockOffsets: void 0
216
- } : {
217
- blockOffsets: {
218
- anchor,
219
- focus
220
- }
221
- };
222
- },
223
- actions: [({
224
- event
225
- }, {
226
- blockOffsets
227
- }) => [{
228
- type: "effect",
229
- effect: () => {
230
- sendBack({
231
- type: "selection",
232
- blockOffsets
233
- });
234
- }
235
- }, behaviors_index.forward(event)]]
236
- })
237
- }), deleteBackwardListenerCallback = ({
238
- sendBack,
239
- input
240
- }) => input.editor.registerBehavior({
241
- behavior: behaviors_index.defineBehavior({
242
- on: "delete.backward",
243
- actions: [() => [behaviors_index.execute({
244
- type: "history.undo"
245
- }), behaviors_index.effect(() => {
246
- sendBack({
247
- type: "delete.backward"
248
- });
249
- })]]
250
- })
251
- }), decoratorPairMachine = xstate.setup({
252
- types: {
253
- context: {},
254
- input: {},
255
- events: {}
256
- },
257
- actors: {
258
- "emphasis listener": xstate.fromCallback(emphasisListener),
259
- "delete.backward listener": xstate.fromCallback(deleteBackwardListenerCallback),
260
- "selection listener": xstate.fromCallback(selectionListenerCallback)
261
- }
262
- }).createMachine({
263
- id: "decorator pair",
264
- context: ({
265
- input
266
- }) => ({
267
- decorator: input.decorator,
268
- editor: input.editor,
269
- pair: input.pair
270
- }),
271
- initial: "idle",
272
- states: {
273
- idle: {
274
- invoke: [{
275
- src: "emphasis listener",
276
- input: ({
277
- context
278
- }) => ({
279
- decorator: context.decorator,
280
- editor: context.editor,
281
- pair: context.pair
282
- })
283
- }],
284
- on: {
285
- "emphasis.add": {
286
- target: "emphasis added",
287
- actions: xstate.assign({
288
- offsetAfterEmphasis: ({
289
- event
290
- }) => event.blockOffset
291
- })
292
- }
293
- }
294
- },
295
- "emphasis added": {
296
- exit: [xstate.assign({
297
- offsetAfterEmphasis: void 0
298
- })],
299
- invoke: [{
300
- src: "selection listener",
301
- input: ({
302
- context
303
- }) => ({
304
- editor: context.editor
305
- })
306
- }, {
307
- src: "delete.backward listener",
308
- input: ({
309
- context
310
- }) => ({
311
- editor: context.editor
312
- })
313
- }],
314
- on: {
315
- selection: {
316
- target: "idle",
317
- guard: ({
318
- context,
319
- event
320
- }) => !isEqual__default.default({
321
- anchor: context.offsetAfterEmphasis,
322
- focus: context.offsetAfterEmphasis
323
- }, event.blockOffsets)
324
- },
325
- "delete.backward": {
326
- target: "idle"
327
- }
328
- }
329
- }
330
- }
331
- }), EditorRefPlugin = React__default.default.forwardRef((_, ref) => {
332
- const $ = reactCompilerRuntime.c(2), editor = useEditor.useEditor(), portableTextEditorRef = React__default.default.useRef(editor);
333
- let t0, t1;
334
- 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;
335
- });
336
- EditorRefPlugin.displayName = "EditorRefPlugin";
337
- function EventListenerPlugin(props) {
338
- const $ = reactCompilerRuntime.c(4), editor = useEditor.useEditor();
339
- let t0, t1;
340
- return $[0] !== editor || $[1] !== props.on ? (t0 = () => {
341
- const subscription = editor.on("*", props.on);
342
- return () => {
343
- subscription.unsubscribe();
344
- };
345
- }, t1 = [editor, props.on], $[0] = editor, $[1] = props.on, $[2] = t0, $[3] = t1) : (t0 = $[2], t1 = $[3]), React.useEffect(t0, t1), null;
346
- }
347
- function createMarkdownBehaviors(config) {
348
- const automaticBlockquoteOnSpace = behaviors_index.defineBehavior({
349
- on: "insert.text",
350
- guard: ({
351
- snapshot,
352
- event
353
- }) => {
354
- if (event.text !== " ")
355
- return !1;
356
- const selectionCollapsed = selector_getSelectionText.isSelectionCollapsed(snapshot), focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot), focusSpan = selector_getSelectionText.getFocusSpan(snapshot);
357
- if (!selectionCollapsed || !focusTextBlock || !focusSpan)
358
- return !1;
359
- const previousInlineObject = selector_getSelectionText.getPreviousInlineObject(snapshot), blockOffset = util_getTextBlockText.spanSelectionPointToBlockOffset({
360
- context: snapshot.context,
361
- selectionPoint: {
362
- path: [{
363
- _key: focusTextBlock.node._key
364
- }, "children", {
365
- _key: focusSpan.node._key
366
- }],
367
- offset: snapshot.context.selection?.focus.offset ?? 0
368
- }
369
- });
370
- if (previousInlineObject || !blockOffset)
371
- return !1;
372
- const blockText = util_getTextBlockText.getTextBlockText(focusTextBlock.node), caretAtTheEndOfQuote = blockOffset.offset === 1, looksLikeMarkdownQuote = /^>/.test(blockText), blockquoteStyle = config.blockquoteStyle?.(snapshot.context);
373
- return caretAtTheEndOfQuote && looksLikeMarkdownQuote && blockquoteStyle !== void 0 ? {
374
- focusTextBlock,
375
- style: blockquoteStyle
376
- } : !1;
377
- },
378
- actions: [() => [behaviors_index.execute({
379
- type: "insert.text",
380
- text: " "
381
- })], (_, {
382
- focusTextBlock,
383
- style
384
- }) => [behaviors_index.execute({
385
- type: "block.unset",
386
- props: ["listItem", "level"],
387
- at: focusTextBlock.path
388
- }), behaviors_index.execute({
389
- type: "block.set",
390
- props: {
391
- style
392
- },
393
- at: focusTextBlock.path
394
- }), behaviors_index.execute({
395
- type: "delete.text",
396
- at: {
397
- anchor: {
398
- path: focusTextBlock.path,
399
- offset: 0
400
- },
401
- focus: {
402
- path: focusTextBlock.path,
403
- offset: 2
404
- }
405
- }
406
- })]]
407
- }), automaticHr = behaviors_index.defineBehavior({
408
- on: "insert.text",
409
- guard: ({
410
- snapshot,
411
- event
412
- }) => {
413
- const hrCharacter = event.text === "-" ? "-" : event.text === "*" ? "*" : event.text === "_" ? "_" : void 0;
414
- if (hrCharacter === void 0)
415
- return !1;
416
- const hrObject = config.horizontalRuleObject?.(snapshot.context), focusBlock = selector_getSelectionText.getFocusTextBlock(snapshot), selectionCollapsed = selector_getSelectionText.isSelectionCollapsed(snapshot);
417
- if (!hrObject || !focusBlock || !selectionCollapsed)
418
- return !1;
419
- const previousInlineObject = selector_getSelectionText.getPreviousInlineObject(snapshot), textBefore = selector_getTextBefore.getBlockTextBefore(snapshot), hrBlockOffsets = {
420
- anchor: {
421
- path: focusBlock.path,
422
- offset: 0
423
- },
424
- focus: {
425
- path: focusBlock.path,
426
- offset: 3
427
- }
428
- };
429
- return !previousInlineObject && textBefore === `${hrCharacter}${hrCharacter}` ? {
430
- hrObject,
431
- focusBlock,
432
- hrCharacter,
433
- hrBlockOffsets
434
- } : !1;
435
- },
436
- actions: [(_, {
437
- hrCharacter
438
- }) => [behaviors_index.execute({
439
- type: "insert.text",
440
- text: hrCharacter
441
- })], (_, {
442
- hrObject,
443
- hrBlockOffsets
444
- }) => [behaviors_index.execute({
445
- type: "insert.block",
446
- placement: "before",
447
- block: {
448
- _type: hrObject.name,
449
- ...hrObject.value ?? {}
450
- }
451
- }), behaviors_index.execute({
452
- type: "delete.text",
453
- at: hrBlockOffsets
454
- })]]
455
- }), automaticHrOnPaste = behaviors_index.defineBehavior({
456
- on: "clipboard.paste",
457
- guard: ({
458
- snapshot,
459
- event
460
- }) => {
461
- const text = event.originEvent.dataTransfer.getData("text/plain"), hrRegExp = /^(---)$|(___)$|(\*\*\*)$/, hrCharacters = text.match(hrRegExp)?.[0], hrObject = config.horizontalRuleObject?.(snapshot.context), focusBlock = selector_getSelectionText.getFocusBlock(snapshot);
462
- return !hrCharacters || !hrObject || !focusBlock ? !1 : {
463
- hrCharacters,
464
- hrObject,
465
- focusBlock
466
- };
467
- },
468
- actions: [(_, {
469
- hrCharacters
470
- }) => [behaviors_index.execute({
471
- type: "insert.text",
472
- text: hrCharacters
473
- })], ({
474
- snapshot
475
- }, {
476
- hrObject,
477
- focusBlock
478
- }) => schema.isTextBlock(snapshot.context, focusBlock.node) ? [behaviors_index.execute({
479
- type: "insert.block",
480
- block: {
481
- _type: snapshot.context.schema.block.name,
482
- children: focusBlock.node.children
483
- },
484
- placement: "after"
485
- }), behaviors_index.execute({
486
- type: "insert.block",
487
- block: {
488
- _type: hrObject.name,
489
- ...hrObject.value ?? {}
490
- },
491
- placement: "after"
492
- }), behaviors_index.execute({
493
- type: "delete.block",
494
- at: focusBlock.path
495
- })] : [behaviors_index.execute({
496
- type: "insert.block",
497
- block: {
498
- _type: hrObject.name,
499
- ...hrObject.value ?? {}
500
- },
501
- placement: "after"
502
- })]]
503
- }), automaticHeadingOnSpace = behaviors_index.defineBehavior({
504
- on: "insert.text",
505
- guard: ({
506
- snapshot,
507
- event
508
- }) => {
509
- if (event.text !== " ")
510
- return !1;
511
- const selectionCollapsed = selector_getSelectionText.isSelectionCollapsed(snapshot), focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot), focusSpan = selector_getSelectionText.getFocusSpan(snapshot);
512
- if (!selectionCollapsed || !focusTextBlock || !focusSpan)
513
- return !1;
514
- const blockOffset = util_getTextBlockText.spanSelectionPointToBlockOffset({
515
- context: snapshot.context,
516
- selectionPoint: {
517
- path: [{
518
- _key: focusTextBlock.node._key
519
- }, "children", {
520
- _key: focusSpan.node._key
521
- }],
522
- offset: snapshot.context.selection?.focus.offset ?? 0
523
- }
524
- });
525
- if (!blockOffset)
526
- return !1;
527
- const previousInlineObject = selector_getSelectionText.getPreviousInlineObject(snapshot), blockText = util_getTextBlockText.getTextBlockText(focusTextBlock.node), markdownHeadingSearch = /^#+/.exec(blockText), level = markdownHeadingSearch ? markdownHeadingSearch[0].length : void 0, caretAtTheEndOfHeading = blockOffset.offset === level;
528
- if (previousInlineObject || !caretAtTheEndOfHeading)
529
- return !1;
530
- const style = level !== void 0 ? config.headingStyle?.({
531
- schema: snapshot.context.schema,
532
- level
533
- }) : void 0;
534
- return level !== void 0 && style !== void 0 ? {
535
- focusTextBlock,
536
- style,
537
- level
538
- } : !1;
539
- },
540
- actions: [({
541
- event
542
- }) => [behaviors_index.execute(event)], (_, {
543
- focusTextBlock,
544
- style,
545
- level
546
- }) => [behaviors_index.execute({
547
- type: "block.unset",
548
- props: ["listItem", "level"],
549
- at: focusTextBlock.path
550
- }), behaviors_index.execute({
551
- type: "block.set",
552
- props: {
553
- style
554
- },
555
- at: focusTextBlock.path
556
- }), behaviors_index.execute({
557
- type: "delete.text",
558
- at: {
559
- anchor: {
560
- path: focusTextBlock.path,
561
- offset: 0
562
- },
563
- focus: {
564
- path: focusTextBlock.path,
565
- offset: level + 1
566
- }
567
- }
568
- })]]
569
- }), clearStyleOnBackspace = behaviors_index.defineBehavior({
570
- on: "delete.backward",
571
- guard: ({
572
- snapshot
573
- }) => {
574
- const selectionCollapsed = selector_getSelectionText.isSelectionCollapsed(snapshot), focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot), focusSpan = selector_getSelectionText.getFocusSpan(snapshot);
575
- if (!selectionCollapsed || !focusTextBlock || !focusSpan)
576
- return !1;
577
- const atTheBeginningOfBLock = focusTextBlock.node.children[0]._key === focusSpan.node._key && snapshot.context.selection?.focus.offset === 0, defaultStyle = config.defaultStyle?.(snapshot.context);
578
- return atTheBeginningOfBLock && defaultStyle && focusTextBlock.node.style !== defaultStyle ? {
579
- defaultStyle,
580
- focusTextBlock
581
- } : !1;
582
- },
583
- actions: [(_, {
584
- defaultStyle,
585
- focusTextBlock
586
- }) => [behaviors_index.execute({
587
- type: "block.set",
588
- props: {
589
- style: defaultStyle
590
- },
591
- at: focusTextBlock.path
592
- })]]
593
- }), automaticListOnSpace = behaviors_index.defineBehavior({
594
- on: "insert.text",
595
- guard: ({
596
- snapshot,
597
- event
598
- }) => {
599
- if (event.text !== " ")
600
- return !1;
601
- const selectionCollapsed = selector_getSelectionText.isSelectionCollapsed(snapshot), focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot), focusSpan = selector_getSelectionText.getFocusSpan(snapshot);
602
- if (!selectionCollapsed || !focusTextBlock || !focusSpan)
603
- return !1;
604
- const previousInlineObject = selector_getSelectionText.getPreviousInlineObject(snapshot), blockOffset = util_getTextBlockText.spanSelectionPointToBlockOffset({
605
- context: snapshot.context,
606
- selectionPoint: {
607
- path: [{
608
- _key: focusTextBlock.node._key
609
- }, "children", {
610
- _key: focusSpan.node._key
611
- }],
612
- offset: snapshot.context.selection?.focus.offset ?? 0
613
- }
614
- });
615
- if (previousInlineObject || !blockOffset)
616
- return !1;
617
- const blockText = util_getTextBlockText.getTextBlockText(focusTextBlock.node), defaultStyle = config.defaultStyle?.(snapshot.context), looksLikeUnorderedList = /^(-|\*)/.test(blockText), unorderedListStyle = config.unorderedListStyle?.(snapshot.context), caretAtTheEndOfUnorderedList = blockOffset.offset === 1;
618
- if (defaultStyle && caretAtTheEndOfUnorderedList && looksLikeUnorderedList && unorderedListStyle !== void 0)
619
- return {
620
- focusTextBlock,
621
- listItem: unorderedListStyle,
622
- listItemLength: 1,
623
- style: defaultStyle
624
- };
625
- const looksLikeOrderedList = /^1\./.test(blockText), orderedListStyle = config.orderedListStyle?.(snapshot.context), caretAtTheEndOfOrderedList = blockOffset.offset === 2;
626
- return defaultStyle && caretAtTheEndOfOrderedList && looksLikeOrderedList && orderedListStyle !== void 0 ? {
627
- focusTextBlock,
628
- listItem: orderedListStyle,
629
- listItemLength: 2,
630
- style: defaultStyle
631
- } : !1;
632
- },
633
- actions: [({
634
- event
635
- }) => [behaviors_index.execute(event)], (_, {
636
- focusTextBlock,
637
- style,
638
- listItem,
639
- listItemLength
640
- }) => [behaviors_index.execute({
641
- type: "block.set",
642
- props: {
643
- listItem,
644
- level: 1,
645
- style
646
- },
647
- at: focusTextBlock.path
648
- }), behaviors_index.execute({
649
- type: "delete.text",
650
- at: {
651
- anchor: {
652
- path: focusTextBlock.path,
653
- offset: 0
654
- },
655
- focus: {
656
- path: focusTextBlock.path,
657
- offset: listItemLength + 1
658
- }
659
- }
660
- })]]
661
- });
662
- return [automaticBlockquoteOnSpace, automaticHeadingOnSpace, automaticHr, automaticHrOnPaste, clearStyleOnBackspace, automaticListOnSpace];
663
- }
664
- function MarkdownPlugin(props) {
665
- const $ = reactCompilerRuntime.c(17), editor = useEditor.useEditor();
666
- let t0, t1;
667
- $[0] !== editor || $[1] !== props.config ? (t0 = () => {
668
- const unregisterBehaviors = createMarkdownBehaviors(props.config).map((behavior) => editor.registerBehavior({
669
- behavior
670
- }));
671
- return () => {
672
- for (const unregisterBehavior of unregisterBehaviors)
673
- unregisterBehavior();
674
- };
675
- }, t1 = [editor, props.config], $[0] = editor, $[1] = props.config, $[2] = t0, $[3] = t1) : (t0 = $[2], t1 = $[3]), React.useEffect(t0, t1);
676
- let t2;
677
- $[4] !== props.config.boldDecorator ? (t2 = props.config.boldDecorator ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
678
- /* @__PURE__ */ jsxRuntime.jsx(DecoratorShortcutPlugin, { decorator: props.config.boldDecorator, pair: {
679
- char: "*",
680
- amount: 2
681
- } }),
682
- /* @__PURE__ */ jsxRuntime.jsx(DecoratorShortcutPlugin, { decorator: props.config.boldDecorator, pair: {
683
- char: "_",
684
- amount: 2
685
- } })
686
- ] }) : null, $[4] = props.config.boldDecorator, $[5] = t2) : t2 = $[5];
687
- let t3;
688
- $[6] !== props.config.codeDecorator ? (t3 = props.config.codeDecorator ? /* @__PURE__ */ jsxRuntime.jsx(DecoratorShortcutPlugin, { decorator: props.config.codeDecorator, pair: {
689
- char: "`",
690
- amount: 1
691
- } }) : null, $[6] = props.config.codeDecorator, $[7] = t3) : t3 = $[7];
692
- let t4;
693
- $[8] !== props.config.italicDecorator ? (t4 = props.config.italicDecorator ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
694
- /* @__PURE__ */ jsxRuntime.jsx(DecoratorShortcutPlugin, { decorator: props.config.italicDecorator, pair: {
695
- char: "*",
696
- amount: 1
697
- } }),
698
- /* @__PURE__ */ jsxRuntime.jsx(DecoratorShortcutPlugin, { decorator: props.config.italicDecorator, pair: {
699
- char: "_",
700
- amount: 1
701
- } })
702
- ] }) : null, $[8] = props.config.italicDecorator, $[9] = t4) : t4 = $[9];
703
- let t5;
704
- $[10] !== props.config.strikeThroughDecorator ? (t5 = props.config.strikeThroughDecorator ? /* @__PURE__ */ jsxRuntime.jsx(DecoratorShortcutPlugin, { decorator: props.config.strikeThroughDecorator, pair: {
705
- char: "~",
706
- amount: 2
707
- } }) : null, $[10] = props.config.strikeThroughDecorator, $[11] = t5) : t5 = $[11];
708
- let t6;
709
- return $[12] !== t2 || $[13] !== t3 || $[14] !== t4 || $[15] !== t5 ? (t6 = /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
710
- t2,
711
- t3,
712
- t4,
713
- t5
714
- ] }), $[12] = t2, $[13] = t3, $[14] = t4, $[15] = t5, $[16] = t6) : t6 = $[16], t6;
715
- }
716
- const oneLineBehaviors = [
717
- /**
718
- * Hitting Enter on an expanded selection should just delete that selection
719
- * without causing a line break.
720
- */
721
- behaviors_index.defineBehavior({
722
- on: "insert.break",
723
- guard: ({
724
- snapshot
725
- }) => snapshot.context.selection && selector_getSelectionText.isSelectionExpanded(snapshot) ? {
726
- selection: snapshot.context.selection
727
- } : !1,
728
- actions: [(_, {
729
- selection
730
- }) => [behaviors_index.execute({
731
- type: "delete",
732
- at: selection
733
- })]]
734
- }),
735
- /**
736
- * All other cases of `insert.break` should be aborted.
737
- */
738
- behaviors_index.defineBehavior({
739
- on: "insert.break",
740
- actions: []
741
- }),
742
- /**
743
- * `insert.block` `before` or `after` is not allowed in a one-line editor.
744
- */
745
- behaviors_index.defineBehavior({
746
- on: "insert.block",
747
- guard: ({
748
- event
749
- }) => event.placement === "before" || event.placement === "after",
750
- actions: []
751
- }),
752
- /**
753
- * An ordinary `insert.block` is acceptable if it's a text block. In that
754
- * case it will get merged into the existing text block.
755
- */
756
- behaviors_index.defineBehavior({
757
- on: "insert.block",
758
- guard: ({
759
- snapshot,
760
- event
761
- }) => !(!selector_getSelectionText.getFocusTextBlock(snapshot) || !schema.isTextBlock(snapshot.context, event.block)),
762
- actions: [({
763
- event
764
- }) => [behaviors_index.execute({
765
- type: "insert.block",
766
- block: event.block,
767
- placement: "auto",
768
- select: "end"
769
- })]]
770
- }),
771
- /**
772
- * Fallback Behavior to avoid `insert.block` in case the Behaviors above all
773
- * end up with a falsy guard.
774
- */
775
- behaviors_index.defineBehavior({
776
- on: "insert.block",
777
- actions: []
778
- }),
779
- /**
780
- * If multiple blocks are inserted, then the non-text blocks are filtered out
781
- * and the text blocks are merged into one block
782
- */
783
- behaviors_index.defineBehavior({
784
- on: "insert.blocks",
785
- guard: ({
786
- snapshot,
787
- event
788
- }) => {
789
- const textBlocks = event.blocks.filter((block) => schema.isTextBlock(snapshot.context, block));
790
- return textBlocks.length === 0 ? !1 : textBlocks.reduce((targetBlock, incomingBlock) => util_mergeTextBlocks.mergeTextBlocks({
791
- context: snapshot.context,
792
- targetBlock,
793
- incomingBlock
794
- }));
795
- },
796
- actions: [
797
- // `insert.block` is raised so the Behavior above can handle the
798
- // insertion
799
- (_, block) => [behaviors_index.raise({
800
- type: "insert.block",
801
- block,
802
- placement: "auto"
803
- })]
804
- ]
805
- }),
806
- /**
807
- * Fallback Behavior to avoid `insert.blocks` in case the Behavior above
808
- * ends up with a falsy guard.
809
- */
810
- behaviors_index.defineBehavior({
811
- on: "insert.blocks",
812
- actions: []
813
- })
814
- ];
815
- function OneLinePlugin() {
816
- const $ = reactCompilerRuntime.c(1);
817
- let t0;
818
- return $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = /* @__PURE__ */ jsxRuntime.jsx(BehaviorPlugin, { behaviors: oneLineBehaviors }), $[0] = t0) : t0 = $[0], t0;
819
- }
820
- exports.BehaviorPlugin = BehaviorPlugin;
821
- exports.DecoratorShortcutPlugin = DecoratorShortcutPlugin;
822
- exports.EditorRefPlugin = EditorRefPlugin;
823
- exports.EventListenerPlugin = EventListenerPlugin;
824
- exports.MarkdownPlugin = MarkdownPlugin;
825
- exports.OneLinePlugin = OneLinePlugin;
826
- //# sourceMappingURL=index.cjs.map