@portabletext/plugin-markdown-shortcuts 1.0.16 → 1.1.1

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/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { jsxs, Fragment, jsx } from "react/jsx-runtime";
2
+ import { c } from "react/compiler-runtime";
2
3
  import { useEditor } from "@portabletext/editor";
3
4
  import { CharacterPairDecoratorPlugin } from "@portabletext/plugin-character-pair-decorator";
4
5
  import { useEffect } from "react";
@@ -8,7 +9,10 @@ import * as utils from "@portabletext/editor/utils";
8
9
  function createMarkdownBehaviors(config) {
9
10
  const automaticBlockquoteOnSpace = defineBehavior({
10
11
  on: "insert.text",
11
- guard: ({ snapshot, event }) => {
12
+ guard: ({
13
+ snapshot,
14
+ event
15
+ }) => {
12
16
  if (event.text !== " ")
13
17
  return !1;
14
18
  const selectionCollapsed = selectors.isSelectionCollapsed(snapshot), focusTextBlock = selectors.getFocusTextBlock(snapshot), focusSpan = selectors.getFocusSpan(snapshot);
@@ -17,11 +21,11 @@ function createMarkdownBehaviors(config) {
17
21
  const previousInlineObject = selectors.getPreviousInlineObject(snapshot), blockOffset = utils.spanSelectionPointToBlockOffset({
18
22
  context: snapshot.context,
19
23
  selectionPoint: {
20
- path: [
21
- { _key: focusTextBlock.node._key },
22
- "children",
23
- { _key: focusSpan.node._key }
24
- ],
24
+ path: [{
25
+ _key: focusTextBlock.node._key
26
+ }, "children", {
27
+ _key: focusSpan.node._key
28
+ }],
25
29
  offset: snapshot.context.selection?.focus.offset ?? 0
26
30
  }
27
31
  });
@@ -30,44 +34,46 @@ function createMarkdownBehaviors(config) {
30
34
  const blockText = utils.getTextBlockText(focusTextBlock.node), caretAtTheEndOfQuote = blockOffset.offset === 1, looksLikeMarkdownQuote = /^>/.test(blockText), blockquoteStyle = config.blockquoteStyle?.({
31
35
  schema: snapshot.context.schema
32
36
  });
33
- return caretAtTheEndOfQuote && looksLikeMarkdownQuote && blockquoteStyle !== void 0 ? { focusTextBlock, style: blockquoteStyle } : !1;
37
+ return caretAtTheEndOfQuote && looksLikeMarkdownQuote && blockquoteStyle !== void 0 ? {
38
+ focusTextBlock,
39
+ style: blockquoteStyle
40
+ } : !1;
34
41
  },
35
- actions: [
36
- () => [
37
- execute({
38
- type: "insert.text",
39
- text: " "
40
- })
41
- ],
42
- (_, { focusTextBlock, style }) => [
43
- execute({
44
- type: "block.unset",
45
- props: ["listItem", "level"],
46
- at: focusTextBlock.path
47
- }),
48
- execute({
49
- type: "block.set",
50
- props: { style },
51
- at: focusTextBlock.path
52
- }),
53
- execute({
54
- type: "delete.text",
55
- at: {
56
- anchor: {
57
- path: focusTextBlock.path,
58
- offset: 0
59
- },
60
- focus: {
61
- path: focusTextBlock.path,
62
- offset: 2
63
- }
64
- }
65
- })
66
- ]
67
- ]
42
+ actions: [() => [execute({
43
+ type: "insert.text",
44
+ text: " "
45
+ })], (_, {
46
+ focusTextBlock,
47
+ style
48
+ }) => [execute({
49
+ type: "block.unset",
50
+ props: ["listItem", "level"],
51
+ at: focusTextBlock.path
52
+ }), execute({
53
+ type: "block.set",
54
+ props: {
55
+ style
56
+ },
57
+ at: focusTextBlock.path
58
+ }), execute({
59
+ type: "delete.text",
60
+ at: {
61
+ anchor: {
62
+ path: focusTextBlock.path,
63
+ offset: 0
64
+ },
65
+ focus: {
66
+ path: focusTextBlock.path,
67
+ offset: 2
68
+ }
69
+ }
70
+ })]]
68
71
  }), automaticHr = defineBehavior({
69
72
  on: "insert.text",
70
- guard: ({ snapshot, event }) => {
73
+ guard: ({
74
+ snapshot,
75
+ event
76
+ }) => {
71
77
  const hrCharacter = event.text === "-" ? "-" : event.text === "*" ? "*" : event.text === "_" ? "_" : void 0;
72
78
  if (hrCharacter === void 0)
73
79
  return !1;
@@ -86,81 +92,91 @@ function createMarkdownBehaviors(config) {
86
92
  offset: 3
87
93
  }
88
94
  };
89
- return !previousInlineObject && textBefore === `${hrCharacter}${hrCharacter}` ? { hrObject, focusBlock, hrCharacter, hrBlockOffsets } : !1;
95
+ return !previousInlineObject && textBefore === `${hrCharacter}${hrCharacter}` ? {
96
+ hrObject,
97
+ focusBlock,
98
+ hrCharacter,
99
+ hrBlockOffsets
100
+ } : !1;
90
101
  },
91
- actions: [
92
- (_, { hrCharacter }) => [
93
- execute({
94
- type: "insert.text",
95
- text: hrCharacter
96
- })
97
- ],
98
- (_, { hrObject, hrBlockOffsets }) => [
99
- execute({
100
- type: "insert.block",
101
- block: {
102
- _type: hrObject.name,
103
- ...hrObject.value ?? {}
104
- },
105
- placement: "before",
106
- select: "none"
107
- }),
108
- execute({
109
- type: "delete.text",
110
- at: hrBlockOffsets
111
- })
112
- ]
113
- ]
102
+ actions: [(_, {
103
+ hrCharacter
104
+ }) => [execute({
105
+ type: "insert.text",
106
+ text: hrCharacter
107
+ })], (_, {
108
+ hrObject,
109
+ hrBlockOffsets
110
+ }) => [execute({
111
+ type: "insert.block",
112
+ block: {
113
+ _type: hrObject.name,
114
+ ...hrObject.value ?? {}
115
+ },
116
+ placement: "before",
117
+ select: "none"
118
+ }), execute({
119
+ type: "delete.text",
120
+ at: hrBlockOffsets
121
+ })]]
114
122
  }), automaticHrOnPaste = defineBehavior({
115
123
  on: "clipboard.paste",
116
- guard: ({ snapshot, event }) => {
124
+ guard: ({
125
+ snapshot,
126
+ event
127
+ }) => {
117
128
  const text = event.originEvent.dataTransfer.getData("text/plain"), hrRegExp = /^(---)$|(___)$|(\*\*\*)$/, hrCharacters = text.match(hrRegExp)?.[0], hrObject = config.horizontalRuleObject?.({
118
129
  schema: snapshot.context.schema
119
130
  }), focusBlock = selectors.getFocusBlock(snapshot), focusTextBlock = selectors.getFocusTextBlock(snapshot);
120
- return !hrCharacters || !hrObject || !focusBlock ? !1 : { hrCharacters, hrObject, focusBlock, focusTextBlock };
131
+ return !hrCharacters || !hrObject || !focusBlock ? !1 : {
132
+ hrCharacters,
133
+ hrObject,
134
+ focusBlock,
135
+ focusTextBlock
136
+ };
121
137
  },
122
- actions: [
123
- (_, { hrCharacters }) => [
124
- execute({
125
- type: "insert.text",
126
- text: hrCharacters
127
- })
128
- ],
129
- ({ snapshot }, { hrObject, focusBlock, focusTextBlock }) => focusTextBlock ? [
130
- execute({
131
- type: "insert.block",
132
- block: {
133
- _type: snapshot.context.schema.block.name,
134
- children: focusTextBlock.node.children
135
- },
136
- placement: "after"
137
- }),
138
- execute({
139
- type: "insert.block",
140
- block: {
141
- _type: hrObject.name,
142
- ...hrObject.value ?? {}
143
- },
144
- placement: "after"
145
- }),
146
- execute({
147
- type: "delete.block",
148
- at: focusBlock.path
149
- })
150
- ] : [
151
- execute({
152
- type: "insert.block",
153
- block: {
154
- _type: hrObject.name,
155
- ...hrObject.value ?? {}
156
- },
157
- placement: "after"
158
- })
159
- ]
160
- ]
138
+ actions: [(_, {
139
+ hrCharacters
140
+ }) => [execute({
141
+ type: "insert.text",
142
+ text: hrCharacters
143
+ })], ({
144
+ snapshot
145
+ }, {
146
+ hrObject,
147
+ focusBlock,
148
+ focusTextBlock
149
+ }) => focusTextBlock ? [execute({
150
+ type: "insert.block",
151
+ block: {
152
+ _type: snapshot.context.schema.block.name,
153
+ children: focusTextBlock.node.children
154
+ },
155
+ placement: "after"
156
+ }), execute({
157
+ type: "insert.block",
158
+ block: {
159
+ _type: hrObject.name,
160
+ ...hrObject.value ?? {}
161
+ },
162
+ placement: "after"
163
+ }), execute({
164
+ type: "delete.block",
165
+ at: focusBlock.path
166
+ })] : [execute({
167
+ type: "insert.block",
168
+ block: {
169
+ _type: hrObject.name,
170
+ ...hrObject.value ?? {}
171
+ },
172
+ placement: "after"
173
+ })]]
161
174
  }), automaticHeadingOnSpace = defineBehavior({
162
175
  on: "insert.text",
163
- guard: ({ snapshot, event }) => {
176
+ guard: ({
177
+ snapshot,
178
+ event
179
+ }) => {
164
180
  if (event.text !== " ")
165
181
  return !1;
166
182
  const selectionCollapsed = selectors.isSelectionCollapsed(snapshot), focusTextBlock = selectors.getFocusTextBlock(snapshot), focusSpan = selectors.getFocusSpan(snapshot);
@@ -169,11 +185,11 @@ function createMarkdownBehaviors(config) {
169
185
  const blockOffset = utils.spanSelectionPointToBlockOffset({
170
186
  context: snapshot.context,
171
187
  selectionPoint: {
172
- path: [
173
- { _key: focusTextBlock.node._key },
174
- "children",
175
- { _key: focusSpan.node._key }
176
- ],
188
+ path: [{
189
+ _key: focusTextBlock.node._key
190
+ }, "children", {
191
+ _key: focusSpan.node._key
192
+ }],
177
193
  offset: snapshot.context.selection?.focus.offset ?? 0
178
194
  }
179
195
  });
@@ -182,64 +198,77 @@ function createMarkdownBehaviors(config) {
182
198
  const previousInlineObject = selectors.getPreviousInlineObject(snapshot), blockText = utils.getTextBlockText(focusTextBlock.node), markdownHeadingSearch = /^#+/.exec(blockText), level = markdownHeadingSearch ? markdownHeadingSearch[0].length : void 0, caretAtTheEndOfHeading = blockOffset.offset === level;
183
199
  if (previousInlineObject || !caretAtTheEndOfHeading)
184
200
  return !1;
185
- const style = level !== void 0 ? config.headingStyle?.({ schema: snapshot.context.schema, level }) : void 0;
201
+ const style = level !== void 0 ? config.headingStyle?.({
202
+ schema: snapshot.context.schema,
203
+ level
204
+ }) : void 0;
186
205
  return level !== void 0 && style !== void 0 ? {
187
206
  focusTextBlock,
188
207
  style,
189
208
  level
190
209
  } : !1;
191
210
  },
192
- actions: [
193
- ({ event }) => [execute(event)],
194
- (_, { focusTextBlock, style, level }) => [
195
- execute({
196
- type: "block.unset",
197
- props: ["listItem", "level"],
198
- at: focusTextBlock.path
199
- }),
200
- execute({
201
- type: "block.set",
202
- props: { style },
203
- at: focusTextBlock.path
204
- }),
205
- execute({
206
- type: "delete.text",
207
- at: {
208
- anchor: {
209
- path: focusTextBlock.path,
210
- offset: 0
211
- },
212
- focus: {
213
- path: focusTextBlock.path,
214
- offset: level + 1
215
- }
216
- }
217
- })
218
- ]
219
- ]
211
+ actions: [({
212
+ event
213
+ }) => [execute(event)], (_, {
214
+ focusTextBlock,
215
+ style,
216
+ level
217
+ }) => [execute({
218
+ type: "block.unset",
219
+ props: ["listItem", "level"],
220
+ at: focusTextBlock.path
221
+ }), execute({
222
+ type: "block.set",
223
+ props: {
224
+ style
225
+ },
226
+ at: focusTextBlock.path
227
+ }), execute({
228
+ type: "delete.text",
229
+ at: {
230
+ anchor: {
231
+ path: focusTextBlock.path,
232
+ offset: 0
233
+ },
234
+ focus: {
235
+ path: focusTextBlock.path,
236
+ offset: level + 1
237
+ }
238
+ }
239
+ })]]
220
240
  }), clearStyleOnBackspace = defineBehavior({
221
241
  on: "delete.backward",
222
- guard: ({ snapshot }) => {
242
+ guard: ({
243
+ snapshot
244
+ }) => {
223
245
  const selectionCollapsed = selectors.isSelectionCollapsed(snapshot), focusTextBlock = selectors.getFocusTextBlock(snapshot), focusSpan = selectors.getFocusSpan(snapshot);
224
246
  if (!selectionCollapsed || !focusTextBlock || !focusSpan)
225
247
  return !1;
226
248
  const atTheBeginningOfBLock = focusTextBlock.node.children[0]._key === focusSpan.node._key && snapshot.context.selection?.focus.offset === 0, defaultStyle = config.defaultStyle?.({
227
249
  schema: snapshot.context.schema
228
250
  });
229
- return atTheBeginningOfBLock && defaultStyle && focusTextBlock.node.style !== defaultStyle ? { defaultStyle, focusTextBlock } : !1;
251
+ return atTheBeginningOfBLock && defaultStyle && focusTextBlock.node.style !== defaultStyle ? {
252
+ defaultStyle,
253
+ focusTextBlock
254
+ } : !1;
230
255
  },
231
- actions: [
232
- (_, { defaultStyle, focusTextBlock }) => [
233
- execute({
234
- type: "block.set",
235
- props: { style: defaultStyle },
236
- at: focusTextBlock.path
237
- })
238
- ]
239
- ]
256
+ actions: [(_, {
257
+ defaultStyle,
258
+ focusTextBlock
259
+ }) => [execute({
260
+ type: "block.set",
261
+ props: {
262
+ style: defaultStyle
263
+ },
264
+ at: focusTextBlock.path
265
+ })]]
240
266
  }), automaticListOnSpace = defineBehavior({
241
267
  on: "insert.text",
242
- guard: ({ snapshot, event }) => {
268
+ guard: ({
269
+ snapshot,
270
+ event
271
+ }) => {
243
272
  if (event.text !== " ")
244
273
  return !1;
245
274
  const selectionCollapsed = selectors.isSelectionCollapsed(snapshot), focusTextBlock = selectors.getFocusTextBlock(snapshot), focusSpan = selectors.getFocusSpan(snapshot);
@@ -248,11 +277,11 @@ function createMarkdownBehaviors(config) {
248
277
  const previousInlineObject = selectors.getPreviousInlineObject(snapshot), blockOffset = utils.spanSelectionPointToBlockOffset({
249
278
  context: snapshot.context,
250
279
  selectionPoint: {
251
- path: [
252
- { _key: focusTextBlock.node._key },
253
- "children",
254
- { _key: focusSpan.node._key }
255
- ],
280
+ path: [{
281
+ _key: focusTextBlock.node._key
282
+ }, "children", {
283
+ _key: focusSpan.node._key
284
+ }],
256
285
  offset: snapshot.context.selection?.focus.offset ?? 0
257
286
  }
258
287
  });
@@ -280,101 +309,106 @@ function createMarkdownBehaviors(config) {
280
309
  style: defaultStyle
281
310
  } : !1;
282
311
  },
283
- actions: [
284
- ({ event }) => [execute(event)],
285
- (_, { focusTextBlock, style, listItem, listItemLength }) => [
286
- execute({
287
- type: "block.set",
288
- props: {
289
- listItem,
290
- level: 1,
291
- style
292
- },
293
- at: focusTextBlock.path
294
- }),
295
- execute({
296
- type: "delete.text",
297
- at: {
298
- anchor: {
299
- path: focusTextBlock.path,
300
- offset: 0
301
- },
302
- focus: {
303
- path: focusTextBlock.path,
304
- offset: listItemLength + 1
305
- }
306
- }
307
- })
308
- ]
309
- ]
312
+ actions: [({
313
+ event
314
+ }) => [execute(event)], (_, {
315
+ focusTextBlock,
316
+ style,
317
+ listItem,
318
+ listItemLength
319
+ }) => [execute({
320
+ type: "block.set",
321
+ props: {
322
+ listItem,
323
+ level: 1,
324
+ style
325
+ },
326
+ at: focusTextBlock.path
327
+ }), execute({
328
+ type: "delete.text",
329
+ at: {
330
+ anchor: {
331
+ path: focusTextBlock.path,
332
+ offset: 0
333
+ },
334
+ focus: {
335
+ path: focusTextBlock.path,
336
+ offset: listItemLength + 1
337
+ }
338
+ }
339
+ })]]
310
340
  });
311
- return [
312
- automaticBlockquoteOnSpace,
313
- automaticHeadingOnSpace,
314
- automaticHr,
315
- automaticHrOnPaste,
316
- clearStyleOnBackspace,
317
- automaticListOnSpace
318
- ];
341
+ return [automaticBlockquoteOnSpace, automaticHeadingOnSpace, automaticHr, automaticHrOnPaste, clearStyleOnBackspace, automaticListOnSpace];
319
342
  }
320
- function MarkdownShortcutsPlugin(props) {
321
- const editor = useEditor();
322
- return useEffect(() => {
323
- const unregisterBehaviors = createMarkdownBehaviors(props).map(
324
- (behavior) => editor.registerBehavior({ behavior })
325
- );
343
+ function MarkdownShortcutsPlugin(t0) {
344
+ const $ = c(22), {
345
+ blockquoteStyle,
346
+ boldDecorator,
347
+ codeDecorator,
348
+ defaultStyle,
349
+ headingStyle,
350
+ horizontalRuleObject,
351
+ italicDecorator,
352
+ orderedList,
353
+ strikeThroughDecorator,
354
+ unorderedList
355
+ } = t0, editor = useEditor();
356
+ let t1, t2;
357
+ $[0] !== blockquoteStyle || $[1] !== defaultStyle || $[2] !== editor || $[3] !== headingStyle || $[4] !== horizontalRuleObject || $[5] !== orderedList || $[6] !== unorderedList ? (t1 = () => {
358
+ const unregisterBehaviors = createMarkdownBehaviors({
359
+ blockquoteStyle,
360
+ defaultStyle,
361
+ headingStyle,
362
+ horizontalRuleObject,
363
+ orderedList,
364
+ unorderedList
365
+ }).map((behavior) => editor.registerBehavior({
366
+ behavior
367
+ }));
326
368
  return () => {
327
369
  for (const unregisterBehavior of unregisterBehaviors)
328
370
  unregisterBehavior();
329
371
  };
330
- }, [editor, props]), /* @__PURE__ */ jsxs(Fragment, { children: [
331
- props.boldDecorator ? /* @__PURE__ */ jsxs(Fragment, { children: [
332
- /* @__PURE__ */ jsx(
333
- CharacterPairDecoratorPlugin,
334
- {
335
- decorator: props.boldDecorator,
336
- pair: { char: "*", amount: 2 }
337
- }
338
- ),
339
- /* @__PURE__ */ jsx(
340
- CharacterPairDecoratorPlugin,
341
- {
342
- decorator: props.boldDecorator,
343
- pair: { char: "_", amount: 2 }
344
- }
345
- )
346
- ] }) : null,
347
- props.codeDecorator ? /* @__PURE__ */ jsx(
348
- CharacterPairDecoratorPlugin,
349
- {
350
- decorator: props.codeDecorator,
351
- pair: { char: "`", amount: 1 }
352
- }
353
- ) : null,
354
- props.italicDecorator ? /* @__PURE__ */ jsxs(Fragment, { children: [
355
- /* @__PURE__ */ jsx(
356
- CharacterPairDecoratorPlugin,
357
- {
358
- decorator: props.italicDecorator,
359
- pair: { char: "*", amount: 1 }
360
- }
361
- ),
362
- /* @__PURE__ */ jsx(
363
- CharacterPairDecoratorPlugin,
364
- {
365
- decorator: props.italicDecorator,
366
- pair: { char: "_", amount: 1 }
367
- }
368
- )
369
- ] }) : null,
370
- props.strikeThroughDecorator ? /* @__PURE__ */ jsx(
371
- CharacterPairDecoratorPlugin,
372
- {
373
- decorator: props.strikeThroughDecorator,
374
- pair: { char: "~", amount: 2 }
375
- }
376
- ) : null
377
- ] });
372
+ }, t2 = [blockquoteStyle, defaultStyle, editor, headingStyle, horizontalRuleObject, orderedList, unorderedList], $[0] = blockquoteStyle, $[1] = defaultStyle, $[2] = editor, $[3] = headingStyle, $[4] = horizontalRuleObject, $[5] = orderedList, $[6] = unorderedList, $[7] = t1, $[8] = t2) : (t1 = $[7], t2 = $[8]), useEffect(t1, t2);
373
+ let t3;
374
+ $[9] !== boldDecorator ? (t3 = boldDecorator ? /* @__PURE__ */ jsxs(Fragment, { children: [
375
+ /* @__PURE__ */ jsx(CharacterPairDecoratorPlugin, { decorator: boldDecorator, pair: {
376
+ char: "*",
377
+ amount: 2
378
+ } }),
379
+ /* @__PURE__ */ jsx(CharacterPairDecoratorPlugin, { decorator: boldDecorator, pair: {
380
+ char: "_",
381
+ amount: 2
382
+ } })
383
+ ] }) : null, $[9] = boldDecorator, $[10] = t3) : t3 = $[10];
384
+ let t4;
385
+ $[11] !== codeDecorator ? (t4 = codeDecorator ? /* @__PURE__ */ jsx(CharacterPairDecoratorPlugin, { decorator: codeDecorator, pair: {
386
+ char: "`",
387
+ amount: 1
388
+ } }) : null, $[11] = codeDecorator, $[12] = t4) : t4 = $[12];
389
+ let t5;
390
+ $[13] !== italicDecorator ? (t5 = italicDecorator ? /* @__PURE__ */ jsxs(Fragment, { children: [
391
+ /* @__PURE__ */ jsx(CharacterPairDecoratorPlugin, { decorator: italicDecorator, pair: {
392
+ char: "*",
393
+ amount: 1
394
+ } }),
395
+ /* @__PURE__ */ jsx(CharacterPairDecoratorPlugin, { decorator: italicDecorator, pair: {
396
+ char: "_",
397
+ amount: 1
398
+ } })
399
+ ] }) : null, $[13] = italicDecorator, $[14] = t5) : t5 = $[14];
400
+ let t6;
401
+ $[15] !== strikeThroughDecorator ? (t6 = strikeThroughDecorator ? /* @__PURE__ */ jsx(CharacterPairDecoratorPlugin, { decorator: strikeThroughDecorator, pair: {
402
+ char: "~",
403
+ amount: 2
404
+ } }) : null, $[15] = strikeThroughDecorator, $[16] = t6) : t6 = $[16];
405
+ let t7;
406
+ return $[17] !== t3 || $[18] !== t4 || $[19] !== t5 || $[20] !== t6 ? (t7 = /* @__PURE__ */ jsxs(Fragment, { children: [
407
+ t3,
408
+ t4,
409
+ t5,
410
+ t6
411
+ ] }), $[17] = t3, $[18] = t4, $[19] = t5, $[20] = t6, $[21] = t7) : t7 = $[21], t7;
378
412
  }
379
413
  export {
380
414
  MarkdownShortcutsPlugin