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