@lexical/react 0.1.14 → 0.1.17

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