@lexical/react 0.1.15 → 0.1.18

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 (87) hide show
  1. package/DEPRECATED_useLexicalAutoFormatter.dev.js +5 -729
  2. package/DEPRECATED_useLexicalAutoFormatter.prod.js +1 -19
  3. package/DEPRECATED_useLexicalCanShowPlaceholder.dev.js +1 -1
  4. package/DEPRECATED_useLexicalCanShowPlaceholder.prod.js +1 -1
  5. package/DEPRECATED_useLexicalCharacterLimit.dev.js +9 -10
  6. package/DEPRECATED_useLexicalCharacterLimit.prod.js +7 -7
  7. package/DEPRECATED_useLexicalDecorators.dev.js +1 -1
  8. package/DEPRECATED_useLexicalDecorators.prod.js +1 -1
  9. package/DEPRECATED_useLexicalEditorEvents.dev.js +1 -1
  10. package/DEPRECATED_useLexicalEditorEvents.prod.js +1 -1
  11. package/DEPRECATED_useLexicalHistory.dev.js +5 -307
  12. package/DEPRECATED_useLexicalHistory.prod.js +1 -7
  13. package/DEPRECATED_useLexicalList.dev.js +29 -25
  14. package/DEPRECATED_useLexicalList.prod.js +3 -1
  15. package/DEPRECATED_useLexicalPlainText.dev.js +8 -662
  16. package/DEPRECATED_useLexicalPlainText.prod.js +2 -14
  17. package/DEPRECATED_useLexicalRichText.dev.js +8 -747
  18. package/DEPRECATED_useLexicalRichText.prod.js +2 -17
  19. package/LICENSE +1 -1
  20. package/{withSubscriptions.prod.js → LexicalAutoFocusPlugin.d.ts} +3 -1
  21. package/{withSubscriptions.dev.js → LexicalAutoFocusPlugin.dev.js} +10 -5
  22. package/LexicalAutoFocusPlugin.js +9 -0
  23. package/{LexicalAutoFormatterPlugin.js.flow → LexicalAutoFocusPlugin.js.flow} +1 -1
  24. package/LexicalAutoFocusPlugin.prod.js +7 -0
  25. package/LexicalAutoLinkPlugin.dev.js +12 -15
  26. package/LexicalAutoLinkPlugin.prod.js +6 -6
  27. package/LexicalAutoScrollPlugin.d.ts +13 -0
  28. package/LexicalAutoScrollPlugin.dev.js +82 -0
  29. package/LexicalAutoScrollPlugin.js +9 -0
  30. package/{withSubscriptions.d.ts → LexicalAutoScrollPlugin.js.flow} +5 -5
  31. package/LexicalAutoScrollPlugin.prod.js +8 -0
  32. package/LexicalCharacterLimitPlugin.dev.js +9 -10
  33. package/LexicalCharacterLimitPlugin.prod.js +7 -7
  34. package/LexicalClearEditorPlugin.dev.js +15 -19
  35. package/LexicalClearEditorPlugin.prod.js +1 -1
  36. package/LexicalCollaborationPlugin.d.ts +6 -3
  37. package/LexicalCollaborationPlugin.dev.js +30 -41
  38. package/LexicalCollaborationPlugin.js.flow +7 -4
  39. package/LexicalCollaborationPlugin.prod.js +10 -9
  40. package/LexicalComposer.d.ts +2 -2
  41. package/LexicalComposer.dev.js +3 -19
  42. package/LexicalComposer.js.flow +2 -2
  43. package/LexicalComposer.prod.js +2 -3
  44. package/LexicalContentEditable.dev.js +3 -1
  45. package/LexicalContentEditable.prod.js +2 -2
  46. package/LexicalHashtagPlugin.dev.js +20 -94
  47. package/LexicalHashtagPlugin.prod.js +4 -7
  48. package/LexicalHistoryPlugin.dev.js +5 -307
  49. package/LexicalHistoryPlugin.prod.js +1 -7
  50. package/LexicalHorizontalRuleNode.d.ts +3 -1
  51. package/LexicalHorizontalRuleNode.dev.js +2 -0
  52. package/LexicalHorizontalRuleNode.js.flow +6 -2
  53. package/LexicalHorizontalRuleNode.prod.js +2 -2
  54. package/LexicalLinkPlugin.dev.js +19 -20
  55. package/LexicalLinkPlugin.prod.js +4 -3
  56. package/LexicalListPlugin.dev.js +29 -25
  57. package/LexicalListPlugin.prod.js +3 -2
  58. package/{LexicalAutoFormatterPlugin.d.ts → LexicalMarkdownShortcutPlugin.d.ts} +1 -1
  59. package/LexicalMarkdownShortcutPlugin.dev.js +42 -0
  60. package/LexicalMarkdownShortcutPlugin.js +9 -0
  61. package/{withSubscriptions.js.flow → LexicalMarkdownShortcutPlugin.js.flow} +1 -4
  62. package/LexicalMarkdownShortcutPlugin.prod.js +7 -0
  63. package/LexicalNestedComposer.dev.js +20 -15
  64. package/LexicalNestedComposer.prod.js +3 -3
  65. package/LexicalOnChangePlugin.dev.js +1 -1
  66. package/LexicalOnChangePlugin.prod.js +1 -1
  67. package/LexicalPlainTextPlugin.dev.js +6 -359
  68. package/LexicalPlainTextPlugin.prod.js +4 -11
  69. package/LexicalRichTextPlugin.dev.js +6 -444
  70. package/LexicalRichTextPlugin.prod.js +4 -12
  71. package/LexicalTablePlugin.dev.js +27 -30
  72. package/LexicalTablePlugin.prod.js +3 -3
  73. package/LexicalTreeView.dev.js +1 -1
  74. package/LexicalTreeView.prod.js +1 -1
  75. package/package.json +17 -13
  76. package/useLexicalIsTextContentEmpty.dev.js +1 -1
  77. package/useLexicalIsTextContentEmpty.prod.js +1 -1
  78. package/useLexicalNodeSelection.dev.js +1 -1
  79. package/useLexicalNodeSelection.prod.js +1 -1
  80. package/useLexicalTextEntity.d.ts +19 -0
  81. package/useLexicalTextEntity.dev.js +29 -0
  82. package/{withSubscriptions.js → useLexicalTextEntity.js} +2 -2
  83. package/useLexicalTextEntity.js.flow +18 -0
  84. package/useLexicalTextEntity.prod.js +7 -0
  85. package/LexicalAutoFormatterPlugin.dev.js +0 -766
  86. package/LexicalAutoFormatterPlugin.js +0 -9
  87. package/LexicalAutoFormatterPlugin.prod.js +0 -25
@@ -6,589 +6,9 @@
6
6
  */
7
7
  'use strict';
8
8
 
9
- var list = require('@lexical/list');
10
- var lexical = require('lexical');
11
- var CodeNode = require('lexical/CodeNode');
12
- var react = require('react');
9
+ var markdown = require('@lexical/markdown');
13
10
  var LexicalHorizontalRuleNode = require('@lexical/react/LexicalHorizontalRuleNode');
14
- var text = require('@lexical/text');
15
- var HeadingNode = require('lexical/HeadingNode');
16
- var QuoteNode = require('lexical/QuoteNode');
17
-
18
- /**
19
- * Copyright (c) Meta Platforms, Inc. and affiliates.
20
- *
21
- * This source code is licensed under the MIT license found in the
22
- * LICENSE file in the root directory of this source tree.
23
- *
24
- *
25
- */
26
- /*
27
- How to add a new syntax to capture and transform.
28
- 1. Create a new enumeration by adding to AutoFormatKind.
29
- 2. Add a new criteria with a regEx pattern. See markdownStrikethrough as an example.
30
- 3. Add your block criteria (e.g. '# ') to allAutoFormatCriteria or
31
- your text criteria (e.g. *MyItalic*) to allAutoFormatCriteriaForTextNodes.
32
- 4. Add your Lexical block specific transforming code here: transformTextNodeForText.
33
- Add your Lexical text specific transforming code here: transformTextNodeForText.
34
- */
35
- // The trigger state helps to capture EditorState information
36
- // from the prior and current EditorState.
37
- // This is then used to determined if an auto format has been triggered.
38
-
39
- const TRIGGER_STRING = '\u0020'; // The space key triggers markdown.
40
-
41
- const SEPARATOR_BETWEEN_TEXT_AND_NON_TEXT_NODES = '\u0004'; // Select an unused unicode character to separate text and non-text nodes.
42
-
43
- const SEPARATOR_LENGTH = SEPARATOR_BETWEEN_TEXT_AND_NON_TEXT_NODES.length; // Todo: speed up performance by having non-capture group variations of the regex.
44
-
45
- const autoFormatBase = {
46
- autoFormatKind: null,
47
- regEx: /(?:)/,
48
- requiresParagraphStart: false
49
- };
50
- const paragraphStartBase = { ...autoFormatBase,
51
- requiresParagraphStart: true
52
- };
53
- const markdownHeader1 = { ...paragraphStartBase,
54
- autoFormatKind: 'paragraphH1',
55
- regEx: /^(?:# )/
56
- };
57
- const markdownHeader2 = { ...paragraphStartBase,
58
- autoFormatKind: 'paragraphH2',
59
- regEx: /^(?:## )/
60
- };
61
- const markdownHeader3 = { ...paragraphStartBase,
62
- autoFormatKind: 'paragraphH2',
63
- regEx: /^(?:### )/
64
- };
65
- const markdownBlockQuote = { ...paragraphStartBase,
66
- autoFormatKind: 'paragraphBlockQuote',
67
- regEx: /^(?:> )/
68
- };
69
- const markdownUnorderedListDash = { ...paragraphStartBase,
70
- autoFormatKind: 'paragraphUnorderedList',
71
- regEx: /^(?:- )/
72
- };
73
- const markdownUnorderedListAsterisk = { ...paragraphStartBase,
74
- autoFormatKind: 'paragraphUnorderedList',
75
- regEx: /^(?:\* )/
76
- };
77
- const markdownCodeBlock = { ...paragraphStartBase,
78
- autoFormatKind: 'paragraphCodeBlock',
79
- regEx: /^(```)([a-z]*)( )/
80
- };
81
- const markdownOrderedList = { ...paragraphStartBase,
82
- autoFormatKind: 'paragraphOrderedList',
83
- regEx: /^(\d+)\.\s/
84
- };
85
- const markdownHorizontalRule = { ...paragraphStartBase,
86
- autoFormatKind: 'horizontalRule',
87
- regEx: /^(?:\*\*\* )/
88
- };
89
- const markdownHorizontalRuleUsingDashes = { ...paragraphStartBase,
90
- autoFormatKind: 'horizontalRule',
91
- regEx: /^(?:--- )/
92
- };
93
- const markdownItalic = { ...autoFormatBase,
94
- autoFormatKind: 'italic',
95
- regEx: /(\*)(\s*\b)([^\*]*)(\b\s*)(\*)(\s)$/
96
- };
97
- const markdownBold = { ...autoFormatBase,
98
- autoFormatKind: 'bold',
99
- regEx: /(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*)(\s)$/
100
- };
101
- const markdownBoldWithUnderlines = { ...autoFormatBase,
102
- autoFormatKind: 'bold',
103
- regEx: /(__)(\s*)([^__]*)(\s*)(__)(\s)$/
104
- };
105
- const markdownBoldItalic = { ...autoFormatBase,
106
- autoFormatKind: 'bold_italic',
107
- regEx: /(\*\*\*)(\s*\b)([^\*\*\*]*)(\b\s*)(\*\*\*)(\s)$/
108
- }; // Markdown does not support underline, but we can allow folks to use
109
- // the HTML tags for underline.
110
-
111
- const fakeMarkdownUnderline = { ...autoFormatBase,
112
- autoFormatKind: 'underline',
113
- regEx: /(\<u\>)(\s*\b)([^\<]*)(\b\s*)(\<\/u\>)(\s)$/
114
- };
115
- const markdownStrikethrough = { ...autoFormatBase,
116
- autoFormatKind: 'strikethrough',
117
- regEx: /(~~)(\s*\b)([^~~]*)(\b\s*)(~~)(\s)$/
118
- };
119
- const markdownLink = { ...autoFormatBase,
120
- autoFormatKind: 'link',
121
- regEx: /(\[)(.+)(\]\()([^ ]+)(?: \"(?:.+)\")?(\))(\s)$/
122
- };
123
- const allAutoFormatCriteriaForTextNodes = [markdownBoldItalic, markdownItalic, markdownBold, markdownBoldWithUnderlines, fakeMarkdownUnderline, markdownStrikethrough, markdownLink];
124
- const allAutoFormatCriteria = [markdownHeader1, markdownHeader2, markdownHeader3, markdownBlockQuote, markdownUnorderedListDash, markdownUnorderedListAsterisk, markdownOrderedList, markdownCodeBlock, markdownHorizontalRule, markdownHorizontalRuleUsingDashes, ...allAutoFormatCriteriaForTextNodes];
125
- function getAllAutoFormatCriteriaForTextNodes() {
126
- return allAutoFormatCriteriaForTextNodes;
127
- }
128
- function getAllAutoFormatCriteria() {
129
- return allAutoFormatCriteria;
130
- }
131
- function getInitialScanningContext(editor, textNodeWithOffset, triggerState) {
132
- return {
133
- autoFormatCriteria: {
134
- autoFormatKind: 'noTransformation',
135
- regEx: /(?:)/,
136
- // Empty reg ex will do until the precise criteria is discovered.
137
- requiresParagraphStart: null
138
- },
139
- editor,
140
- joinedText: null,
141
- patternMatchResults: {
142
- regExCaptureGroups: []
143
- },
144
- textNodeWithOffset,
145
- triggerState
146
- };
147
- }
148
-
149
- function getPatternMatchResultsWithRegEx(textToSearch, matchMustAppearAtStartOfString, matchMustAppearAtEndOfString, regEx) {
150
- const patternMatchResults = {
151
- regExCaptureGroups: []
152
- };
153
- const regExMatches = textToSearch.match(regEx);
154
-
155
- if (regExMatches !== null && regExMatches.length > 0 && (matchMustAppearAtStartOfString === false || regExMatches.index === 0) && (matchMustAppearAtEndOfString === false || regExMatches.index + regExMatches[0].length === textToSearch.length)) {
156
- const captureGroupsCount = regExMatches.length;
157
- let runningLength = regExMatches.index;
158
-
159
- for (let captureGroupIndex = 0; captureGroupIndex < captureGroupsCount; captureGroupIndex++) {
160
- const textContent = regExMatches[captureGroupIndex];
161
- patternMatchResults.regExCaptureGroups.push({
162
- offsetInParent: runningLength,
163
- text: textContent
164
- }); // The 0th capture group is special in that it's text contents is
165
- // a join of all subsequent capture groups. So, skip this group
166
- // when calculating the runningLength.
167
-
168
- if (captureGroupIndex > 0) {
169
- runningLength += textContent.length;
170
- }
171
- }
172
-
173
- return patternMatchResults;
174
- }
175
-
176
- return null;
177
- }
178
-
179
- function getPatternMatchResultsForParagraphs(autoFormatCriteria, scanningContext) {
180
- const textNodeWithOffset = scanningContext.textNodeWithOffset; // At start of paragraph.
181
-
182
- if (textNodeWithOffset.node.getPreviousSibling() === null) {
183
- const textToSearch = scanningContext.textNodeWithOffset.node.getTextContent();
184
- return getPatternMatchResultsWithRegEx(textToSearch, true, false, autoFormatCriteria.regEx);
185
- }
186
-
187
- return null;
188
- }
189
-
190
- function getPatternMatchResultsForText(autoFormatCriteria, scanningContext) {
191
- if (scanningContext.joinedText == null) {
192
- const parentNode = getParent(scanningContext);
193
-
194
- if (lexical.$isElementNode(parentNode)) {
195
- if (scanningContext.joinedText == null) {
196
- // Lazy calculate the text to search.
197
- scanningContext.joinedText = text.$joinTextNodesInElementNode(parentNode, SEPARATOR_BETWEEN_TEXT_AND_NON_TEXT_NODES, scanningContext.textNodeWithOffset);
198
- }
199
- } else {
200
- {
201
- throw Error(`Expected node ${parentNode.__key} to to be a ElementNode.`);
202
- }
203
- }
204
- }
205
-
206
- return getPatternMatchResultsWithRegEx(scanningContext.joinedText, false, true, autoFormatCriteria.regEx);
207
- }
208
-
209
- function getPatternMatchResultsForCriteria(autoFormatCriteria, scanningContext) {
210
- if (autoFormatCriteria.requiresParagraphStart !== null && autoFormatCriteria.requiresParagraphStart === true) {
211
- return getPatternMatchResultsForParagraphs(autoFormatCriteria, scanningContext);
212
- }
213
-
214
- return getPatternMatchResultsForText(autoFormatCriteria, scanningContext);
215
- }
216
-
217
- function getNewNodeForCriteria(scanningContext, element) {
218
- let newNode = null;
219
- const children = element.getChildren();
220
- const autoFormatCriteria = scanningContext.autoFormatCriteria;
221
- const patternMatchResults = scanningContext.patternMatchResults;
222
-
223
- if (autoFormatCriteria.autoFormatKind != null) {
224
- switch (autoFormatCriteria.autoFormatKind) {
225
- case 'paragraphH1':
226
- {
227
- newNode = HeadingNode.$createHeadingNode('h1');
228
- newNode.append(...children);
229
- return newNode;
230
- }
231
-
232
- case 'paragraphH2':
233
- {
234
- newNode = HeadingNode.$createHeadingNode('h2');
235
- newNode.append(...children);
236
- return newNode;
237
- }
238
-
239
- case 'paragraphH3':
240
- {
241
- newNode = HeadingNode.$createHeadingNode('h3');
242
- newNode.append(...children);
243
- return newNode;
244
- }
245
-
246
- case 'paragraphBlockQuote':
247
- {
248
- newNode = QuoteNode.$createQuoteNode();
249
- newNode.append(...children);
250
- return newNode;
251
- }
252
-
253
- case 'paragraphUnorderedList':
254
- {
255
- newNode = list.$createListNode('ul');
256
- const listItem = list.$createListItemNode();
257
- listItem.append(...children);
258
- newNode.append(listItem);
259
- return newNode;
260
- }
261
-
262
- case 'paragraphOrderedList':
263
- {
264
- const startAsString = patternMatchResults.regExCaptureGroups.length > 1 ? patternMatchResults.regExCaptureGroups[patternMatchResults.regExCaptureGroups.length - 1].text : '1';
265
- const start = parseInt(startAsString, 10);
266
- newNode = list.$createListNode('ol', start);
267
- const listItem = list.$createListItemNode();
268
- listItem.append(...children);
269
- newNode.append(listItem);
270
- return newNode;
271
- }
272
-
273
- case 'paragraphCodeBlock':
274
- {
275
- // Toggle code and paragraph nodes.
276
- if (scanningContext.triggerState != null && scanningContext.triggerState.isCodeBlock) {
277
- newNode = lexical.$createParagraphNode();
278
- } else {
279
- newNode = CodeNode.$createCodeNode();
280
- const codingLanguage = patternMatchResults.regExCaptureGroups.length >= 3 ? patternMatchResults.regExCaptureGroups[2].text : null;
281
-
282
- if (codingLanguage != null && codingLanguage.length > 0) {
283
- newNode.setLanguage(codingLanguage);
284
- }
285
- }
286
-
287
- newNode.append(...children);
288
- return newNode;
289
- }
290
-
291
- case 'horizontalRule':
292
- {
293
- // return null for newNode. Insert the HR here.
294
- const horizontalRuleNode = LexicalHorizontalRuleNode.$createHorizontalRuleNode();
295
- element.insertBefore(horizontalRuleNode);
296
- break;
297
- }
298
- }
299
- }
300
-
301
- return newNode;
302
- }
303
-
304
- function transformTextNodeForAutoFormatCriteria(scanningContext) {
305
- if (scanningContext.autoFormatCriteria.requiresParagraphStart) {
306
- transformTextNodeForParagraphs(scanningContext);
307
- } else {
308
- transformTextNodeForText(scanningContext);
309
- }
310
- }
311
-
312
- function transformTextNodeForParagraphs(scanningContext) {
313
- const textNodeWithOffset = scanningContext.textNodeWithOffset;
314
- const element = textNodeWithOffset.node.getParentOrThrow();
315
- const text = scanningContext.patternMatchResults.regExCaptureGroups[0].text; // Remove the text which we matched.
316
-
317
- const textNode = textNodeWithOffset.node.spliceText(0, text.length, '', true);
318
-
319
- if (textNode.getTextContent() === '') {
320
- textNode.selectPrevious();
321
- textNode.remove();
322
- } // Transform the current element kind to the new element kind.
323
-
324
-
325
- const elementNode = getNewNodeForCriteria(scanningContext, element);
326
-
327
- if (elementNode !== null) {
328
- element.replace(elementNode);
329
- }
330
- }
331
-
332
- function transformTextNodeForText(scanningContext) {
333
- const autoFormatCriteria = scanningContext.autoFormatCriteria;
334
-
335
- if (autoFormatCriteria.autoFormatKind != null) {
336
- const formatting = getTextFormatType(autoFormatCriteria.autoFormatKind);
337
-
338
- if (formatting != null) {
339
- transformTextNodeWithFormatting(formatting, scanningContext);
340
- return;
341
- }
342
-
343
- if (autoFormatCriteria.autoFormatKind === 'link') {
344
- transformTextNodeWithLink(scanningContext);
345
- }
346
- }
347
- }
348
-
349
- function transformTextNodeWithFormatting(formatting, scanningContext) {
350
- const patternMatchResults = scanningContext.patternMatchResults;
351
- const groupCount = patternMatchResults.regExCaptureGroups.length;
352
-
353
- if (groupCount !== 7) {
354
- // For BIUS and similar formats which have a pattern + text + pattern:
355
- // given '*italic* ' below are the capture groups by index:
356
- // 0. '*italic* '
357
- // 1. '*'
358
- // 2. whitespace // typically this is "".
359
- // 3. 'italic'
360
- // 4. whitespace // typicallly this is "".
361
- // 5. '*'
362
- // 6. ' '
363
- return;
364
- } // Remove unwanted text in reg ex pattern.
365
- // Remove group 5.
366
-
367
-
368
- removeTextByCaptureGroups(5, 5, scanningContext); // Remove group 1.
369
-
370
- removeTextByCaptureGroups(1, 1, scanningContext); // Apply the formatting.
371
-
372
- formatTextInCaptureGroupIndex(formatting, 3, scanningContext); // Place caret at end of final capture group.
373
-
374
- selectAfterFinalCaptureGroup(scanningContext);
375
- }
376
-
377
- function transformTextNodeWithLink(scanningContext) {
378
- const patternMatchResults = scanningContext.patternMatchResults;
379
- const regExCaptureGroups = patternMatchResults.regExCaptureGroups;
380
- const groupCount = regExCaptureGroups.length;
381
-
382
- if (groupCount !== 7) {
383
- // For links and similar formats which have: pattern + text + pattern + pattern2 text2 + pattern2:
384
- // Given '[title](url) ', below are the capture groups by index:
385
- // 0. '[title](url) '
386
- // 1. '['
387
- // 2. 'title'
388
- // 3. ']('
389
- // 4. 'url'
390
- // 5. ')'
391
- // 6. ' '
392
- return;
393
- }
394
-
395
- const title = regExCaptureGroups[2].text;
396
- const url = regExCaptureGroups[4].text;
397
-
398
- if (title.length === 0 || url.length === 0) {
399
- return;
400
- } // Remove the initial pattern through to the final pattern.
401
-
402
-
403
- removeTextByCaptureGroups(1, 5, scanningContext);
404
- insertTextPriorToCaptureGroup(1, // Insert at the beginning of the meaningful capture groups, namely index 1. Index 0 refers to the whole matched string.
405
- title, scanningContext);
406
- const newSelectionForLink = createSelectionWithCaptureGroups(1, 1, false, true, scanningContext);
407
-
408
- if (newSelectionForLink == null) {
409
- return;
410
- }
411
-
412
- lexical.$setSelection(newSelectionForLink);
413
- scanningContext.editor.execCommand('toggleLink', url); // Place caret at end of final capture group.
414
-
415
- selectAfterFinalCaptureGroup(scanningContext);
416
- } // Below are lower level helper functions.
417
-
418
-
419
- function getParent(scanningContext) {
420
- return scanningContext.textNodeWithOffset.node.getParentOrThrow();
421
- }
422
-
423
- function getJoinedTextLength(patternMatchResults) {
424
- const groupCount = patternMatchResults.regExCaptureGroups.length;
425
-
426
- if (groupCount < 2) {
427
- // Ignore capture group 0, as regEx defaults the 0th one to the entire matched string.
428
- return 0;
429
- }
430
-
431
- const lastGroupIndex = groupCount - 1;
432
- return patternMatchResults.regExCaptureGroups[lastGroupIndex].offsetInParent + patternMatchResults.regExCaptureGroups[lastGroupIndex].text.length;
433
- }
434
-
435
- function getTextFormatType(autoFormatKind) {
436
- switch (autoFormatKind) {
437
- case 'italic':
438
- case 'bold':
439
- case 'underline':
440
- case 'strikethrough':
441
- return [autoFormatKind];
442
-
443
- case 'bold_italic':
444
- {
445
- return ['bold', 'italic'];
446
- }
447
- }
448
-
449
- return null;
450
- }
451
-
452
- function createSelectionWithCaptureGroups(anchorCaptureGroupIndex, focusCaptureGroupIndex, startAtEndOfAnchor, finishAtEndOfFocus, scanningContext) {
453
- const patternMatchResults = scanningContext.patternMatchResults;
454
- const regExCaptureGroups = patternMatchResults.regExCaptureGroups;
455
- const regExCaptureGroupsCount = regExCaptureGroups.length;
456
-
457
- if (anchorCaptureGroupIndex >= regExCaptureGroupsCount || focusCaptureGroupIndex >= regExCaptureGroupsCount) {
458
- return null;
459
- }
460
-
461
- const parentElementNode = getParent(scanningContext);
462
- const joinedTextLength = getJoinedTextLength(patternMatchResults);
463
- const anchorCaptureGroupDetail = regExCaptureGroups[anchorCaptureGroupIndex];
464
- const focusCaptureGroupDetail = regExCaptureGroups[focusCaptureGroupIndex];
465
- const anchorLocation = startAtEndOfAnchor ? anchorCaptureGroupDetail.offsetInParent + anchorCaptureGroupDetail.text.length : anchorCaptureGroupDetail.offsetInParent;
466
- const focusLocation = finishAtEndOfFocus ? focusCaptureGroupDetail.offsetInParent + focusCaptureGroupDetail.text.length : focusCaptureGroupDetail.offsetInParent;
467
- const anchorTextNodeWithOffset = text.$findNodeWithOffsetFromJoinedText(anchorLocation, joinedTextLength, SEPARATOR_LENGTH, parentElementNode);
468
- const focusTextNodeWithOffset = text.$findNodeWithOffsetFromJoinedText(focusLocation, joinedTextLength, SEPARATOR_LENGTH, parentElementNode);
469
-
470
- if (anchorTextNodeWithOffset == null || focusTextNodeWithOffset == null) {
471
- return null;
472
- }
473
-
474
- const selection = lexical.$createRangeSelection();
475
- selection.anchor.set(anchorTextNodeWithOffset.node.getKey(), anchorTextNodeWithOffset.offset, 'text');
476
- selection.focus.set(focusTextNodeWithOffset.node.getKey(), focusTextNodeWithOffset.offset, 'text');
477
- return selection;
478
- }
479
-
480
- function removeTextByCaptureGroups(anchorCaptureGroupIndex, focusCaptureGroupIndex, scanningContext) {
481
- const patternMatchResults = scanningContext.patternMatchResults;
482
- const regExCaptureGroups = patternMatchResults.regExCaptureGroups;
483
- const newSelection = createSelectionWithCaptureGroups(anchorCaptureGroupIndex, focusCaptureGroupIndex, false, true, scanningContext);
484
-
485
- if (newSelection != null) {
486
- lexical.$setSelection(newSelection);
487
- const currentSelection = lexical.$getSelection();
488
-
489
- if (currentSelection != null && lexical.$isRangeSelection(currentSelection) && currentSelection.isCollapsed() === false) {
490
- currentSelection.removeText(); // Shift all group offsets and clear out group text.
491
-
492
- let runningLength = 0;
493
- const groupCount = regExCaptureGroups.length;
494
-
495
- for (let i = anchorCaptureGroupIndex; i < groupCount; i++) {
496
- const captureGroupDetail = regExCaptureGroups[i];
497
-
498
- if (i > anchorCaptureGroupIndex) {
499
- captureGroupDetail.offsetInParent -= runningLength;
500
- }
501
-
502
- if (i <= focusCaptureGroupIndex) {
503
- runningLength += captureGroupDetail.text.length;
504
- captureGroupDetail.text = '';
505
- }
506
- }
507
- }
508
- }
509
- }
510
-
511
- function insertTextPriorToCaptureGroup(captureGroupIndex, text, scanningContext) {
512
- const patternMatchResults = scanningContext.patternMatchResults;
513
- const regExCaptureGroups = patternMatchResults.regExCaptureGroups;
514
- const regExCaptureGroupsCount = regExCaptureGroups.length;
515
-
516
- if (captureGroupIndex >= regExCaptureGroupsCount) {
517
- return;
518
- }
519
-
520
- const captureGroupDetail = regExCaptureGroups[captureGroupIndex];
521
- const newCaptureGroupDetail = {
522
- offsetInParent: captureGroupDetail.offsetInParent,
523
- text
524
- };
525
- const newSelection = createSelectionWithCaptureGroups(captureGroupIndex, captureGroupIndex, false, false, scanningContext);
526
-
527
- if (newSelection != null) {
528
- lexical.$setSelection(newSelection);
529
- const currentSelection = lexical.$getSelection();
530
-
531
- if (currentSelection != null && lexical.$isRangeSelection(currentSelection) && currentSelection.isCollapsed()) {
532
- currentSelection.insertText(newCaptureGroupDetail.text); // Update the capture groups.
533
-
534
- regExCaptureGroups.splice(captureGroupIndex, 0, newCaptureGroupDetail);
535
- const textLength = newCaptureGroupDetail.text.length;
536
- const newGroupCount = regExCaptureGroups.length;
537
-
538
- for (let i = captureGroupIndex + 1; i < newGroupCount; i++) {
539
- const currentCaptureGroupDetail = regExCaptureGroups[i];
540
- currentCaptureGroupDetail.offsetInParent += textLength;
541
- }
542
- }
543
- }
544
- }
545
-
546
- function formatTextInCaptureGroupIndex(formatTypes, captureGroupIndex, scanningContext) {
547
- const patternMatchResults = scanningContext.patternMatchResults;
548
- const regExCaptureGroups = patternMatchResults.regExCaptureGroups;
549
- const regExCaptureGroupsCount = regExCaptureGroups.length;
550
-
551
- if (!(captureGroupIndex < regExCaptureGroupsCount)) {
552
- throw Error(`The capture group count in the RegEx does match the actual capture group count.`);
553
- }
554
-
555
- const captureGroupDetail = regExCaptureGroups[captureGroupIndex];
556
-
557
- if (captureGroupDetail.text.length === 0) {
558
- return;
559
- }
560
-
561
- const newSelection = createSelectionWithCaptureGroups(captureGroupIndex, captureGroupIndex, false, true, scanningContext);
562
-
563
- if (newSelection != null) {
564
- lexical.$setSelection(newSelection);
565
- const currentSelection = lexical.$getSelection();
566
-
567
- if (lexical.$isRangeSelection(currentSelection)) {
568
- for (let i = 0; i < formatTypes.length; i++) {
569
- currentSelection.formatText(formatTypes[i]);
570
- }
571
- }
572
- }
573
- } // Place caret at end of final capture group.
574
-
575
-
576
- function selectAfterFinalCaptureGroup(scanningContext) {
577
- const patternMatchResults = scanningContext.patternMatchResults;
578
- const groupCount = patternMatchResults.regExCaptureGroups.length;
579
-
580
- if (groupCount < 2) {
581
- // Ignore capture group 0, as regEx defaults the 0th one to the entire matched string.
582
- return;
583
- }
584
-
585
- const lastGroupIndex = groupCount - 1;
586
- const newSelection = createSelectionWithCaptureGroups(lastGroupIndex, lastGroupIndex, true, true, scanningContext);
587
-
588
- if (newSelection != null) {
589
- lexical.$setSelection(newSelection);
590
- }
591
- }
11
+ var react = require('react');
592
12
 
593
13
  /**
594
14
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -598,153 +18,9 @@ function selectAfterFinalCaptureGroup(scanningContext) {
598
18
  *
599
19
  *
600
20
  */
601
-
602
- function getCriteriaWithPatternMatchResults(autoFormatCriteriaArray, scanningContext) {
603
- const currentTriggerState = scanningContext.triggerState;
604
- const count = autoFormatCriteriaArray.length;
605
-
606
- for (let i = 0; i < count; i++) {
607
- const autoFormatCriteria = autoFormatCriteriaArray[i]; // Skip code block nodes, unless the autoFormatKind calls for toggling the code block.
608
-
609
- if (currentTriggerState != null && currentTriggerState.isCodeBlock === false || autoFormatCriteria.autoFormatKind === 'paragraphCodeBlock') {
610
- const patternMatchResults = getPatternMatchResultsForCriteria(autoFormatCriteria, scanningContext);
611
-
612
- if (patternMatchResults != null) {
613
- return {
614
- autoFormatCriteria: autoFormatCriteria,
615
- patternMatchResults
616
- };
617
- }
618
- }
619
- }
620
-
621
- return {
622
- autoFormatCriteria: null,
623
- patternMatchResults: null
624
- };
625
- }
626
-
627
- function getTextNodeForAutoFormatting(selection) {
628
- if (!lexical.$isRangeSelection(selection)) {
629
- return null;
630
- }
631
-
632
- const node = selection.anchor.getNode();
633
-
634
- if (!lexical.$isTextNode(node)) {
635
- return null;
636
- }
637
-
638
- return {
639
- node,
640
- offset: selection.anchor.offset
641
- };
642
- }
643
-
644
- function updateAutoFormatting(editor, scanningContext) {
645
- editor.update(() => {
646
- transformTextNodeForAutoFormatCriteria(scanningContext);
647
- }, {
648
- tag: 'history-push'
649
- });
650
- }
651
-
652
- function findScanningContextWithValidMatch(editor, currentTriggerState) {
653
- let scanningContext = null;
654
- editor.getEditorState().read(() => {
655
- const textNodeWithOffset = getTextNodeForAutoFormatting(lexical.$getSelection());
656
-
657
- if (textNodeWithOffset === null) {
658
- return;
659
- } // Please see the declaration of ScanningContext for a detailed explanation.
660
-
661
-
662
- const initialScanningContext = getInitialScanningContext(editor, textNodeWithOffset, currentTriggerState);
663
- const criteriaWithPatternMatchResults = getCriteriaWithPatternMatchResults( // Do not apply paragraph node changes like blockQuote or H1 to listNodes. Also, do not attempt to transform a list into a list using * or -.
664
- currentTriggerState.isParentAListItemNode === false ? getAllAutoFormatCriteria() : getAllAutoFormatCriteriaForTextNodes(), initialScanningContext);
665
-
666
- if (criteriaWithPatternMatchResults.autoFormatCriteria === null || criteriaWithPatternMatchResults.patternMatchResults === null) {
667
- return;
668
- }
669
-
670
- scanningContext = initialScanningContext; // Lazy fill-in the particular format criteria and any matching result information.
671
-
672
- scanningContext.autoFormatCriteria = criteriaWithPatternMatchResults.autoFormatCriteria;
673
- scanningContext.patternMatchResults = criteriaWithPatternMatchResults.patternMatchResults;
674
- });
675
- return scanningContext;
676
- }
677
-
678
- function findScanningContext(editor, currentTriggerState, priorTriggerState) {
679
- if (currentTriggerState == null || priorTriggerState == null) {
680
- return null;
681
- } // The below checks needs to execute relativey quickly, so perform the light-weight ones first.
682
- // The substr check is a quick way to avoid autoformat parsing in that it looks for the autoformat
683
- // trigger which is the trigger string (" ").
684
-
685
-
686
- const triggerStringLength = TRIGGER_STRING.length;
687
- const currentTextContentLength = currentTriggerState.textContent.length;
688
- const triggerOffset = currentTriggerState.anchorOffset - triggerStringLength;
689
-
690
- if ((currentTriggerState.hasParentNode === true && currentTriggerState.isSimpleText && currentTriggerState.isSelectionCollapsed && currentTriggerState.nodeKey === priorTriggerState.nodeKey && currentTriggerState.anchorOffset !== priorTriggerState.anchorOffset && triggerOffset >= 0 && triggerOffset + triggerStringLength <= currentTextContentLength && currentTriggerState.textContent.substr(triggerOffset, triggerStringLength) === TRIGGER_STRING && currentTriggerState.textContent !== priorTriggerState.textContent) === false) {
691
- return null;
692
- }
693
-
694
- return findScanningContextWithValidMatch(editor, currentTriggerState);
695
- }
696
-
697
- function getTriggerState(editorState) {
698
- let criteria = null;
699
- editorState.read(() => {
700
- const selection = lexical.$getSelection();
701
-
702
- if (!lexical.$isRangeSelection(selection) || !selection.isCollapsed()) {
703
- return;
704
- }
705
-
706
- const node = selection.anchor.getNode();
707
- const parentNode = node.getParent();
708
- const isParentAListItemNode = parentNode !== null && list.$isListItemNode(parentNode);
709
- const hasParentNode = parentNode !== null;
710
- criteria = {
711
- anchorOffset: selection.anchor.offset,
712
- hasParentNode,
713
- isCodeBlock: CodeNode.$isCodeNode(node),
714
- isParentAListItemNode,
715
- isSelectionCollapsed: selection.isCollapsed(),
716
- isSimpleText: lexical.$isTextNode(node) && node.isSimpleText(),
717
- nodeKey: node.getKey(),
718
- textContent: node.getTextContent()
719
- };
720
- });
721
- return criteria;
722
- }
723
-
724
- function useAutoFormatter(editor) {
21
+ function useMarkdownShortcuts(editor) {
725
22
  react.useEffect(() => {
726
- // The priorTriggerState is compared against the currentTriggerState to determine
727
- // if the user has performed some typing event that warrants an auto format.
728
- // For example, typing "#" and then " ", shoud trigger an format.
729
- // However, given "#A B", where the user delets "A" should not.
730
- let priorTriggerState = null;
731
- return editor.addListener('update', ({
732
- tags
733
- }) => {
734
- // Examine historic so that we are not running autoformatting within markdown.
735
- if (tags.has('historic') === false) {
736
- const currentTriggerState = getTriggerState(editor.getEditorState());
737
- const scanningContext = currentTriggerState == null ? null : findScanningContext(editor, currentTriggerState, priorTriggerState);
738
-
739
- if (scanningContext != null) {
740
- updateAutoFormatting(editor, scanningContext);
741
- }
742
-
743
- priorTriggerState = currentTriggerState;
744
- } else {
745
- priorTriggerState = null;
746
- }
747
- });
23
+ return markdown.registerMarkdownShortcuts(editor, LexicalHorizontalRuleNode.$createHorizontalRuleNode);
748
24
  }, [editor]);
749
25
  }
750
26
 
@@ -757,7 +33,7 @@ function useAutoFormatter(editor) {
757
33
  *
758
34
  */
759
35
  function useLexicalAutoFormatter(editor) {
760
- useAutoFormatter(editor);
36
+ useMarkdownShortcuts(editor);
761
37
  }
762
38
 
763
39
  module.exports = useLexicalAutoFormatter;