@lexical/react 0.1.4 → 0.1.8
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_useLexical.dev.js +84 -0
- package/DEPRECATED_useLexical.js +9 -2
- package/DEPRECATED_useLexical.prod.js +8 -0
- package/DEPRECATED_useLexicalAutoFormatter.dev.js +642 -0
- package/DEPRECATED_useLexicalAutoFormatter.js +9 -12
- package/DEPRECATED_useLexicalAutoFormatter.prod.js +22 -0
- package/DEPRECATED_useLexicalCanShowPlaceholder.dev.js +136 -0
- package/DEPRECATED_useLexicalCanShowPlaceholder.js +9 -3
- package/DEPRECATED_useLexicalCanShowPlaceholder.prod.js +8 -0
- package/DEPRECATED_useLexicalCharacterLimit.dev.js +294 -0
- package/DEPRECATED_useLexicalCharacterLimit.js +9 -9
- package/DEPRECATED_useLexicalCharacterLimit.prod.js +14 -0
- package/DEPRECATED_useLexicalDecorators.dev.js +82 -0
- package/DEPRECATED_useLexicalDecorators.js +9 -2
- package/DEPRECATED_useLexicalDecorators.prod.js +8 -0
- package/DEPRECATED_useLexicalEditor.dev.js +52 -0
- package/DEPRECATED_useLexicalEditor.js +9 -1
- package/DEPRECATED_useLexicalEditor.prod.js +7 -0
- package/DEPRECATED_useLexicalEditorEvents.dev.js +96 -0
- package/DEPRECATED_useLexicalEditorEvents.js +9 -2
- package/DEPRECATED_useLexicalEditorEvents.prod.js +8 -0
- package/DEPRECATED_useLexicalHistory.dev.js +339 -0
- package/DEPRECATED_useLexicalHistory.js +9 -7
- package/DEPRECATED_useLexicalHistory.prod.js +13 -0
- package/DEPRECATED_useLexicalList.dev.js +64 -0
- package/DEPRECATED_useLexicalList.js +9 -12
- package/DEPRECATED_useLexicalList.prod.js +7 -0
- package/DEPRECATED_useLexicalPlainText.dev.js +755 -0
- package/DEPRECATED_useLexicalPlainText.js +9 -17
- package/DEPRECATED_useLexicalPlainText.prod.js +22 -0
- package/DEPRECATED_useLexicalRichText.dev.js +1326 -0
- package/DEPRECATED_useLexicalRichText.js +9 -30
- package/DEPRECATED_useLexicalRichText.prod.js +35 -0
- package/LexicalAutoFormatterPlugin.dev.js +645 -0
- package/LexicalAutoFormatterPlugin.js +9 -13
- package/LexicalAutoFormatterPlugin.prod.js +23 -0
- package/LexicalAutoLinkPlugin.dev.js +227 -0
- package/LexicalAutoLinkPlugin.js +9 -5
- package/LexicalAutoLinkPlugin.prod.js +12 -0
- package/LexicalBootstrapPlugin.dev.js +124 -0
- package/LexicalBootstrapPlugin.js +9 -0
- package/LexicalBootstrapPlugin.prod.js +8 -0
- package/LexicalCharacterLimitPlugin.dev.js +352 -0
- package/LexicalCharacterLimitPlugin.js +9 -10
- package/LexicalCharacterLimitPlugin.prod.js +14 -0
- package/LexicalCollaborationPlugin.dev.js +235 -0
- package/LexicalCollaborationPlugin.js +9 -8
- package/LexicalCollaborationPlugin.prod.js +14 -0
- package/LexicalComposer.dev.js +76 -0
- package/LexicalComposer.js +9 -2
- package/LexicalComposer.prod.js +9 -0
- package/LexicalComposerContext.dev.js +53 -0
- package/LexicalComposerContext.js +9 -1
- package/LexicalComposerContext.prod.js +7 -0
- package/LexicalContentEditable.dev.js +71 -0
- package/LexicalContentEditable.js +9 -3
- package/LexicalContentEditable.prod.js +9 -0
- package/LexicalHashtagPlugin.dev.js +152 -0
- package/LexicalHashtagPlugin.js +9 -4
- package/LexicalHashtagPlugin.prod.js +10 -0
- package/LexicalHistoryPlugin.dev.js +344 -0
- package/LexicalHistoryPlugin.js +9 -7
- package/LexicalHistoryPlugin.prod.js +13 -0
- package/LexicalHorizontalRulePlugin.dev.js +51 -0
- package/LexicalHorizontalRulePlugin.js +9 -1
- package/LexicalHorizontalRulePlugin.prod.js +7 -0
- package/LexicalLinkPlugin.dev.js +137 -0
- package/LexicalLinkPlugin.js +9 -3
- package/LexicalLinkPlugin.prod.js +9 -0
- package/LexicalListPlugin.dev.js +67 -0
- package/LexicalListPlugin.js +9 -12
- package/LexicalListPlugin.prod.js +8 -0
- package/LexicalNestedComposer.dev.js +60 -0
- package/LexicalNestedComposer.js +9 -1
- package/LexicalNestedComposer.prod.js +8 -0
- package/LexicalOnChangePlugin.dev.js +57 -0
- package/LexicalOnChangePlugin.js +9 -1
- package/LexicalOnChangePlugin.prod.js +7 -0
- package/LexicalPlainTextPlugin.dev.js +565 -0
- package/LexicalPlainTextPlugin.js +9 -13
- package/LexicalPlainTextPlugin.prod.js +17 -0
- package/LexicalRichTextPlugin.dev.js +1136 -0
- package/LexicalRichTextPlugin.js +9 -26
- package/LexicalRichTextPlugin.prod.js +31 -0
- package/LexicalTablePlugin.dev.js +95 -0
- package/LexicalTablePlugin.js +9 -4
- package/LexicalTablePlugin.prod.js +9 -0
- package/LexicalTreeView.dev.js +340 -0
- package/LexicalTreeView.js +9 -12
- package/LexicalTreeView.prod.js +19 -0
- package/README.md +11 -6
- package/package.json +4 -4
- package/useLexicalDecoratorMap.dev.js +83 -0
- package/useLexicalDecoratorMap.js +9 -1
- package/useLexicalDecoratorMap.prod.js +8 -0
- package/useLexicalIsTextContentEmpty.dev.js +84 -0
- package/useLexicalIsTextContentEmpty.js +9 -2
- package/useLexicalIsTextContentEmpty.prod.js +8 -0
- package/withSubscriptions.dev.js +23 -0
- package/withSubscriptions.js +9 -1
- package/withSubscriptions.prod.js +7 -0
|
@@ -0,0 +1,642 @@
|
|
|
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 list = require('@lexical/list');
|
|
10
|
+
var lexical = require('lexical');
|
|
11
|
+
var CodeNode = require('lexical/CodeNode');
|
|
12
|
+
var react = require('react');
|
|
13
|
+
var HeadingNode = require('lexical/HeadingNode');
|
|
14
|
+
var QuoteNode = require('lexical/QuoteNode');
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
18
|
+
*
|
|
19
|
+
* This source code is licensed under the MIT license found in the
|
|
20
|
+
* LICENSE file in the root directory of this source tree.
|
|
21
|
+
*
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
// Caution, this function creates a string and should not be used within a tight loop.
|
|
25
|
+
// Use $getNodeWithOffsetsFromJoinedTextNodesFromElementNode below to convert
|
|
26
|
+
// indexes in the return string back into their corresponding node and offsets.
|
|
27
|
+
|
|
28
|
+
function $joinTextNodesInElementNode(elementNode, separator, stopAt) {
|
|
29
|
+
let textContent = '';
|
|
30
|
+
const children = elementNode.getChildren();
|
|
31
|
+
const length = children.length;
|
|
32
|
+
|
|
33
|
+
for (let i = 0; i < length; ++i) {
|
|
34
|
+
const child = children[i];
|
|
35
|
+
|
|
36
|
+
if (lexical.$isTextNode(child)) {
|
|
37
|
+
const childTextContent = child.getTextContent();
|
|
38
|
+
|
|
39
|
+
if (child.is(stopAt.node)) {
|
|
40
|
+
if (stopAt.offset > childTextContent.length) {
|
|
41
|
+
{
|
|
42
|
+
throw Error(`Node ${child.__key} and selection point do not match.`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
textContent += child.getTextContent().substr(0, stopAt.offset);
|
|
47
|
+
break;
|
|
48
|
+
} else {
|
|
49
|
+
textContent += childTextContent;
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
textContent += separator;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return textContent;
|
|
57
|
+
} // This function converts the offsetInJoinedText to
|
|
58
|
+
// a node and offset result or null if not found.
|
|
59
|
+
// This function is to be used in conjunction with joinTextNodesInElementNode above.
|
|
60
|
+
// The joinedTextContent should be return value from joinTextNodesInElementNode.
|
|
61
|
+
//
|
|
62
|
+
// The offsetInJoinedText is relative to the entire string which
|
|
63
|
+
// itself is relevant to the parent ElementNode.
|
|
64
|
+
//
|
|
65
|
+
// Example:
|
|
66
|
+
// Given a Paragraph with 2 TextNodes. The first is Hello, the second is World.
|
|
67
|
+
// The joinedTextContent would be "HelloWorld"
|
|
68
|
+
// The offsetInJoinedText might be for the letter "e" = 1 or "r" = 7.
|
|
69
|
+
// The return values would be {TextNode1, 1} or {TextNode2,2}, respectively.
|
|
70
|
+
|
|
71
|
+
function $findNodeWithOffsetFromJoinedText(elementNode, joinedTextLength, offsetInJoinedText, separatorLength) {
|
|
72
|
+
const children = elementNode.getChildren();
|
|
73
|
+
const childrenLength = children.length;
|
|
74
|
+
let runningLength = 0;
|
|
75
|
+
|
|
76
|
+
for (let i = 0; i < childrenLength; ++i) {
|
|
77
|
+
if (runningLength >= joinedTextLength) {
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const child = children[i];
|
|
82
|
+
const childContentLength = lexical.$isTextNode(child) ? child.getTextContent().length : separatorLength;
|
|
83
|
+
const newRunningLength = runningLength + childContentLength;
|
|
84
|
+
|
|
85
|
+
if (runningLength <= offsetInJoinedText && offsetInJoinedText < newRunningLength && lexical.$isTextNode(child)) {
|
|
86
|
+
return {
|
|
87
|
+
node: child,
|
|
88
|
+
offset: offsetInJoinedText - runningLength
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
runningLength = newRunningLength;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
100
|
+
*
|
|
101
|
+
* This source code is licensed under the MIT license found in the
|
|
102
|
+
* LICENSE file in the root directory of this source tree.
|
|
103
|
+
*
|
|
104
|
+
*
|
|
105
|
+
*/
|
|
106
|
+
// from the prior and current EditorState.
|
|
107
|
+
// This is then used to determined if an auto format has been triggered.
|
|
108
|
+
|
|
109
|
+
const TRIGGER_STRING = '\u0020'; // The space key triggers markdown.
|
|
110
|
+
|
|
111
|
+
const TRIGGER_STRING_LENGTH = TRIGGER_STRING.length;
|
|
112
|
+
const SEPARATOR_BETWEEN_TEXT_AND_NON_TEXT_NODES = '\u0004'; // Select an unused unicode character to separate text and non-text nodes.
|
|
113
|
+
|
|
114
|
+
const autoFormatBase = {
|
|
115
|
+
nodeTransformationKind: null,
|
|
116
|
+
regEx: /(?:)/,
|
|
117
|
+
regExCaptureGroupsToDelete: null,
|
|
118
|
+
regExExpectedCaptureGroupCount: 1,
|
|
119
|
+
requiresParagraphStart: false
|
|
120
|
+
};
|
|
121
|
+
const paragraphStartBase = { ...autoFormatBase,
|
|
122
|
+
requiresParagraphStart: true
|
|
123
|
+
};
|
|
124
|
+
const markdownHeader1 = { ...paragraphStartBase,
|
|
125
|
+
nodeTransformationKind: 'paragraphH1',
|
|
126
|
+
regEx: /(?:# )/
|
|
127
|
+
};
|
|
128
|
+
const markdownHeader2 = { ...paragraphStartBase,
|
|
129
|
+
nodeTransformationKind: 'paragraphH2',
|
|
130
|
+
regEx: /(?:## )/
|
|
131
|
+
};
|
|
132
|
+
const markdownHeader3 = { ...paragraphStartBase,
|
|
133
|
+
nodeTransformationKind: 'paragraphH2',
|
|
134
|
+
regEx: /(?:### )/
|
|
135
|
+
};
|
|
136
|
+
const markdownBlockQuote = { ...paragraphStartBase,
|
|
137
|
+
nodeTransformationKind: 'paragraphBlockQuote',
|
|
138
|
+
regEx: /(?:> )/
|
|
139
|
+
};
|
|
140
|
+
const markdownUnorderedListDash = { ...paragraphStartBase,
|
|
141
|
+
nodeTransformationKind: 'paragraphUnorderedList',
|
|
142
|
+
regEx: /(?:- )/
|
|
143
|
+
};
|
|
144
|
+
const markdownUnorderedListAsterisk = { ...paragraphStartBase,
|
|
145
|
+
nodeTransformationKind: 'paragraphUnorderedList',
|
|
146
|
+
regEx: /(?:\* )/
|
|
147
|
+
};
|
|
148
|
+
const markdownCodeBlock = { ...paragraphStartBase,
|
|
149
|
+
nodeTransformationKind: 'paragraphCodeBlock',
|
|
150
|
+
regEx: /(?:``` )/
|
|
151
|
+
};
|
|
152
|
+
const markdownOrderedList = { ...paragraphStartBase,
|
|
153
|
+
nodeTransformationKind: 'paragraphOrderedList',
|
|
154
|
+
regEx: /^(\d+)\.\s/,
|
|
155
|
+
regExExpectedCaptureGroupCount: 2
|
|
156
|
+
/*e.g. '321. ' returns '321. ' & '321'*/
|
|
157
|
+
|
|
158
|
+
};
|
|
159
|
+
const markdownBold = { ...autoFormatBase,
|
|
160
|
+
nodeTransformationKind: 'textBold',
|
|
161
|
+
// regEx: /(\*)(?:\s*\b)(?:[^\*]*)(?:\b\s*)(\*\s)$/, // The $ will find the target at the end of the string.
|
|
162
|
+
regEx: /(\*)(\s*\b)([^\*]*)(\b\s*)(\*\s)$/,
|
|
163
|
+
// Remove the first and last capture groups. Remeber, the 0th capture group is the entire string.
|
|
164
|
+
// e.g. "*Hello* " requires removing both "*" as well as bolding "Hello".
|
|
165
|
+
regExCaptureGroupsToDelete: [1, 5],
|
|
166
|
+
// The $ will find the target at the end of the string.
|
|
167
|
+
regExExpectedCaptureGroupCount: 6
|
|
168
|
+
};
|
|
169
|
+
const allAutoFormatCriteriaForTextNodes = [markdownBold];
|
|
170
|
+
const allAutoFormatCriteria = [markdownHeader1, markdownHeader2, markdownHeader3, markdownBlockQuote, markdownUnorderedListDash, markdownUnorderedListAsterisk, markdownOrderedList, markdownCodeBlock, ...allAutoFormatCriteriaForTextNodes];
|
|
171
|
+
function getAllAutoFormatCriteriaForTextNodes() {
|
|
172
|
+
return allAutoFormatCriteriaForTextNodes;
|
|
173
|
+
}
|
|
174
|
+
function getAllAutoFormatCriteria() {
|
|
175
|
+
return allAutoFormatCriteria;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function getMatchResultContextWithRegEx(textToSearch, matchMustAppearAtStartOfString, matchMustAppearAtEndOfString, regEx, regExExpectedCaptureGroupCount, scanningContext) {
|
|
179
|
+
const matchResultContext = {
|
|
180
|
+
regExCaptureGroups: [],
|
|
181
|
+
triggerState: null
|
|
182
|
+
};
|
|
183
|
+
const regExMatches = textToSearch.match(regEx);
|
|
184
|
+
|
|
185
|
+
if (regExMatches !== null && regExMatches.length > 0 && regExMatches.length === regExExpectedCaptureGroupCount && (matchMustAppearAtStartOfString === false || regExMatches.index === 0) && (matchMustAppearAtEndOfString === false || regExMatches.index + regExMatches[0].length === textToSearch.length)) {
|
|
186
|
+
const captureGroupsCount = regExMatches.length;
|
|
187
|
+
let runningLength = regExMatches.index;
|
|
188
|
+
|
|
189
|
+
for (let captureGroupIndex = 0; captureGroupIndex < captureGroupsCount; captureGroupIndex++) {
|
|
190
|
+
const textContent = regExMatches[captureGroupIndex];
|
|
191
|
+
matchResultContext.regExCaptureGroups.push({
|
|
192
|
+
anchorTextNodeWithOffset: null,
|
|
193
|
+
focusTextNodeWithOffset: null,
|
|
194
|
+
offsetInParent: runningLength,
|
|
195
|
+
text: textContent,
|
|
196
|
+
textLength: textContent.length - (captureGroupIndex + 1 === captureGroupsCount ? TRIGGER_STRING_LENGTH : 0)
|
|
197
|
+
}); // The 0th capture group is special in that it's text contents is
|
|
198
|
+
// a join of all subsequent capture groups. So, skip this group
|
|
199
|
+
// when calculating the runningLength.
|
|
200
|
+
|
|
201
|
+
if (captureGroupIndex > 0) {
|
|
202
|
+
runningLength += textContent.length;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return matchResultContext;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function getMatchResultContextForParagraphs(autoFormatCriteria, scanningContext) {
|
|
213
|
+
const textNodeWithOffset = scanningContext.textNodeWithOffset; // At start of paragraph.
|
|
214
|
+
|
|
215
|
+
if (textNodeWithOffset.node.getPreviousSibling() === null) {
|
|
216
|
+
const textToSearch = scanningContext.textNodeWithOffset.node.getTextContent();
|
|
217
|
+
return getMatchResultContextWithRegEx(textToSearch, true, false, autoFormatCriteria.regEx, autoFormatCriteria.regExExpectedCaptureGroupCount);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function getMatchResultContextForText(autoFormatCriteria, scanningContext) {
|
|
224
|
+
if (scanningContext.joinedText == null) {
|
|
225
|
+
const parentNode = scanningContext.textNodeWithOffset.node.getParentOrThrow();
|
|
226
|
+
|
|
227
|
+
if (lexical.$isElementNode(parentNode)) {
|
|
228
|
+
if (scanningContext.joinedText == null) {
|
|
229
|
+
// Lazy calculate the text to search.
|
|
230
|
+
scanningContext.joinedText = $joinTextNodesInElementNode(parentNode, SEPARATOR_BETWEEN_TEXT_AND_NON_TEXT_NODES, scanningContext.textNodeWithOffset);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return getMatchResultContextWithRegEx(scanningContext.joinedText, false, true, autoFormatCriteria.regEx, autoFormatCriteria.regExExpectedCaptureGroupCount);
|
|
234
|
+
} else {
|
|
235
|
+
{
|
|
236
|
+
throw Error(`Expected node ${parentNode.__key} to to be a ElementNode.`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
} // This is a placeholder function for following PR's related to character based transformations.
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function getMatchResultContextForCriteria(autoFormatCriteria, scanningContext) {
|
|
246
|
+
if (autoFormatCriteria.requiresParagraphStart !== null && autoFormatCriteria.requiresParagraphStart === true) {
|
|
247
|
+
return getMatchResultContextForParagraphs(autoFormatCriteria, scanningContext);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return getMatchResultContextForText(autoFormatCriteria, scanningContext);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function getNewNodeForCriteria(autoFormatCriteria, matchResultContext, children) {
|
|
254
|
+
let newNode = null;
|
|
255
|
+
|
|
256
|
+
if (autoFormatCriteria.nodeTransformationKind != null) {
|
|
257
|
+
switch (autoFormatCriteria.nodeTransformationKind) {
|
|
258
|
+
case 'paragraphH1':
|
|
259
|
+
{
|
|
260
|
+
newNode = HeadingNode.$createHeadingNode('h1');
|
|
261
|
+
newNode.append(...children);
|
|
262
|
+
return newNode;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
case 'paragraphH2':
|
|
266
|
+
{
|
|
267
|
+
newNode = HeadingNode.$createHeadingNode('h2');
|
|
268
|
+
newNode.append(...children);
|
|
269
|
+
return newNode;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
case 'paragraphH3':
|
|
273
|
+
{
|
|
274
|
+
newNode = HeadingNode.$createHeadingNode('h3');
|
|
275
|
+
newNode.append(...children);
|
|
276
|
+
return newNode;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
case 'paragraphBlockQuote':
|
|
280
|
+
{
|
|
281
|
+
newNode = QuoteNode.$createQuoteNode();
|
|
282
|
+
newNode.append(...children);
|
|
283
|
+
return newNode;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
case 'paragraphUnorderedList':
|
|
287
|
+
{
|
|
288
|
+
newNode = list.$createListNode('ul');
|
|
289
|
+
const listItem = list.$createListItemNode();
|
|
290
|
+
listItem.append(...children);
|
|
291
|
+
newNode.append(listItem);
|
|
292
|
+
return newNode;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
case 'paragraphOrderedList':
|
|
296
|
+
{
|
|
297
|
+
const startAsString = matchResultContext.regExCaptureGroups.length > 1 ? matchResultContext.regExCaptureGroups[matchResultContext.regExCaptureGroups.length - 1].text : '1';
|
|
298
|
+
const start = parseInt(startAsString, 10);
|
|
299
|
+
newNode = list.$createListNode('ol', start);
|
|
300
|
+
const listItem = list.$createListItemNode();
|
|
301
|
+
listItem.append(...children);
|
|
302
|
+
newNode.append(listItem);
|
|
303
|
+
return newNode;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
case 'paragraphCodeBlock':
|
|
307
|
+
{
|
|
308
|
+
// Toggle code and paragraph nodes.
|
|
309
|
+
if (matchResultContext.triggerState != null && matchResultContext.triggerState.isCodeBlock) {
|
|
310
|
+
newNode = lexical.$createParagraphNode();
|
|
311
|
+
} else {
|
|
312
|
+
newNode = CodeNode.$createCodeNode();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
newNode.append(...children);
|
|
316
|
+
return newNode;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return newNode;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function updateTextNode(node, count) {
|
|
325
|
+
const textNode = node.spliceText(0, count, '', true);
|
|
326
|
+
|
|
327
|
+
if (textNode.getTextContent() === '') {
|
|
328
|
+
textNode.selectPrevious();
|
|
329
|
+
textNode.remove();
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function transformTextNodeForAutoFormatCriteria(scanningContext, autoFormatCriteria, matchResultContext) {
|
|
334
|
+
if (autoFormatCriteria.requiresParagraphStart) {
|
|
335
|
+
transformTextNodeForParagraphs(scanningContext, autoFormatCriteria, matchResultContext);
|
|
336
|
+
} else {
|
|
337
|
+
transformTextNodeForText(scanningContext, autoFormatCriteria, matchResultContext);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function transformTextNodeForParagraphs(scanningContext, autoFormatCriteria, matchResultContext) {
|
|
342
|
+
const textNodeWithOffset = scanningContext.textNodeWithOffset;
|
|
343
|
+
const element = textNodeWithOffset.node.getParentOrThrow();
|
|
344
|
+
const text = matchResultContext.regExCaptureGroups[0].text;
|
|
345
|
+
updateTextNode(textNodeWithOffset.node, text.length);
|
|
346
|
+
const elementNode = getNewNodeForCriteria(autoFormatCriteria, matchResultContext, element.getChildren());
|
|
347
|
+
|
|
348
|
+
if (elementNode !== null) {
|
|
349
|
+
element.replace(elementNode);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function transformTextNodeForText(scanningContext, autoFormatCriteria, matchResultContext) {
|
|
354
|
+
if (autoFormatCriteria.nodeTransformationKind != null) {
|
|
355
|
+
switch (autoFormatCriteria.nodeTransformationKind) {
|
|
356
|
+
case 'textBold':
|
|
357
|
+
{
|
|
358
|
+
matchResultContext.regExCaptureGroups = getCaptureGroupsByResolvingAllDetails(scanningContext, autoFormatCriteria, matchResultContext);
|
|
359
|
+
|
|
360
|
+
if (autoFormatCriteria.regExCaptureGroupsToDelete != null) {
|
|
361
|
+
// Remove unwanted text in reg ex patterh.
|
|
362
|
+
removeTextInCaptureGroups(autoFormatCriteria.regExCaptureGroupsToDelete, matchResultContext);
|
|
363
|
+
formatTextInCaptureGroupIndex('bold', 3, matchResultContext);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
} // Some Capture Group Details were left lazily unresolved as their calculation
|
|
371
|
+
// was not necessary during the scanning phase. Now that the nodeTransformationKind is
|
|
372
|
+
// known, the details may be fully resolved without incurring unwasted performance cost.
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
function getCaptureGroupsByResolvingAllDetails(scanningContext, autoFormatCriteria, matchResultContext) {
|
|
376
|
+
const textNodeWithOffset = scanningContext.textNodeWithOffset;
|
|
377
|
+
const regExCaptureGroups = matchResultContext.regExCaptureGroups;
|
|
378
|
+
const captureGroupsCount = regExCaptureGroups.length;
|
|
379
|
+
const parentElementNode = textNodeWithOffset.node.getParentOrThrow();
|
|
380
|
+
|
|
381
|
+
if (scanningContext.joinedText == null) {
|
|
382
|
+
{
|
|
383
|
+
throw Error(`joinedText was not calculated`);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const joinedTextLength = scanningContext.joinedText.length;
|
|
388
|
+
|
|
389
|
+
for (let captureGroupIndex = 1; captureGroupIndex < captureGroupsCount; captureGroupIndex++) {
|
|
390
|
+
const captureGroupDetail = regExCaptureGroups[captureGroupIndex];
|
|
391
|
+
captureGroupDetail.anchorTextNodeWithOffset = $findNodeWithOffsetFromJoinedText(parentElementNode, joinedTextLength, captureGroupDetail.offsetInParent, TRIGGER_STRING_LENGTH);
|
|
392
|
+
captureGroupDetail.focusTextNodeWithOffset = $findNodeWithOffsetFromJoinedText(parentElementNode, joinedTextLength, captureGroupDetail.offsetInParent + captureGroupDetail.textLength, TRIGGER_STRING_LENGTH);
|
|
393
|
+
|
|
394
|
+
if (captureGroupDetail.textLength < 0) {
|
|
395
|
+
{
|
|
396
|
+
throw Error(`Bad regEx pattern found for ${autoFormatCriteria.nodeTransformationKind}`);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
return regExCaptureGroups;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function removeTextInCaptureGroups(regExCaptureGroupsToDelete, matchResultContext) {
|
|
405
|
+
const regExCaptureGroups = matchResultContext.regExCaptureGroups;
|
|
406
|
+
const regExCaptureGroupsCount = regExCaptureGroups.length;
|
|
407
|
+
|
|
408
|
+
for (let i = regExCaptureGroupsToDelete.length - 1; i >= 0; i--) {
|
|
409
|
+
if (i < regExCaptureGroupsCount) {
|
|
410
|
+
const captureGroupIndex = regExCaptureGroupsToDelete[i];
|
|
411
|
+
const captureGroupDetail = regExCaptureGroups[captureGroupIndex];
|
|
412
|
+
const anchorTextNodeWithOffset = captureGroupDetail.anchorTextNodeWithOffset;
|
|
413
|
+
const focusTextNodeWithOffset = captureGroupDetail.focusTextNodeWithOffset;
|
|
414
|
+
|
|
415
|
+
if (anchorTextNodeWithOffset != null && focusTextNodeWithOffset != null && captureGroupDetail.textLength > 0) {
|
|
416
|
+
const newSelection = lexical.$createRangeSelection();
|
|
417
|
+
newSelection.anchor.set(anchorTextNodeWithOffset.node.getKey(), anchorTextNodeWithOffset.offset, 'text');
|
|
418
|
+
newSelection.focus.set(focusTextNodeWithOffset.node.getKey(), focusTextNodeWithOffset.offset, 'text');
|
|
419
|
+
lexical.$setSelection(newSelection);
|
|
420
|
+
const currentSelection = lexical.$getSelection();
|
|
421
|
+
|
|
422
|
+
if (currentSelection != null) {
|
|
423
|
+
currentSelection.removeText(); // Shift offsets for capture groups which are within the same node
|
|
424
|
+
|
|
425
|
+
if (anchorTextNodeWithOffset.node.getKey() === focusTextNodeWithOffset.node.getKey()) {
|
|
426
|
+
const delta = focusTextNodeWithOffset.offset - anchorTextNodeWithOffset.offset;
|
|
427
|
+
|
|
428
|
+
if (!(delta > 0)) {
|
|
429
|
+
throw Error(`Expected anchor and focus offsets to have ascending character order.`);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
shiftCaptureGroupOffsets(-delta, focusTextNodeWithOffset.offset, anchorTextNodeWithOffset.node, captureGroupIndex, matchResultContext);
|
|
433
|
+
} else {
|
|
434
|
+
const focusDelta = focusTextNodeWithOffset.offset;
|
|
435
|
+
|
|
436
|
+
if (focusDelta > 0) {
|
|
437
|
+
shiftCaptureGroupOffsets(-focusDelta, focusDelta, focusTextNodeWithOffset.node, captureGroupIndex, matchResultContext);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function shiftCaptureGroupOffsets(delta, applyAtOrAfterOffset, node, startingCaptureGroupIndex, matchResultContext) {
|
|
447
|
+
const regExCaptureGroups = matchResultContext.regExCaptureGroups;
|
|
448
|
+
const regExCaptureGroupsCount = regExCaptureGroups.length;
|
|
449
|
+
|
|
450
|
+
for (let captureGroupIndex = startingCaptureGroupIndex + 1; captureGroupIndex < regExCaptureGroupsCount; captureGroupIndex++) {
|
|
451
|
+
const captureGroupDetail = regExCaptureGroups[captureGroupIndex];
|
|
452
|
+
|
|
453
|
+
if (captureGroupDetail.anchorTextNodeWithOffset != null && captureGroupDetail.anchorTextNodeWithOffset.offset >= applyAtOrAfterOffset && captureGroupDetail.anchorTextNodeWithOffset.node.is(node)) {
|
|
454
|
+
captureGroupDetail.anchorTextNodeWithOffset.offset += delta;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (captureGroupDetail.focusTextNodeWithOffset != null && captureGroupDetail.focusTextNodeWithOffset.offset >= applyAtOrAfterOffset && captureGroupDetail.focusTextNodeWithOffset.node.is(node)) {
|
|
458
|
+
captureGroupDetail.focusTextNodeWithOffset.offset += delta;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function formatTextInCaptureGroupIndex(formatType, captureGroupIndex, matchResultContext) {
|
|
464
|
+
const regExCaptureGroups = matchResultContext.regExCaptureGroups;
|
|
465
|
+
const regExCaptureGroupsCount = regExCaptureGroups.length;
|
|
466
|
+
|
|
467
|
+
if (!(captureGroupIndex < regExCaptureGroupsCount)) {
|
|
468
|
+
throw Error(`The capture group count in the RegEx does match the actual capture group count.`);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const captureGroupDetail = regExCaptureGroups[captureGroupIndex];
|
|
472
|
+
const anchorTextNodeWithOffset = captureGroupDetail.anchorTextNodeWithOffset;
|
|
473
|
+
const focusTextNodeWithOffset = captureGroupDetail.focusTextNodeWithOffset;
|
|
474
|
+
|
|
475
|
+
if (anchorTextNodeWithOffset != null && focusTextNodeWithOffset != null && captureGroupDetail.textLength > 0) {
|
|
476
|
+
const newSelection = lexical.$createRangeSelection();
|
|
477
|
+
newSelection.anchor.set(anchorTextNodeWithOffset.node.getKey(), anchorTextNodeWithOffset.offset, 'text');
|
|
478
|
+
newSelection.focus.set(focusTextNodeWithOffset.node.getKey(), focusTextNodeWithOffset.offset, 'text');
|
|
479
|
+
lexical.$setSelection(newSelection);
|
|
480
|
+
const currentSelection = lexical.$getSelection();
|
|
481
|
+
|
|
482
|
+
if (currentSelection != null) {
|
|
483
|
+
currentSelection.formatText(formatType);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
490
|
+
*
|
|
491
|
+
* This source code is licensed under the MIT license found in the
|
|
492
|
+
* LICENSE file in the root directory of this source tree.
|
|
493
|
+
*
|
|
494
|
+
*
|
|
495
|
+
*/
|
|
496
|
+
|
|
497
|
+
function getCriteriaWithMatchResultContext(autoFormatCriteriaArray, currentTriggerState, scanningContext) {
|
|
498
|
+
const count = autoFormatCriteriaArray.length;
|
|
499
|
+
|
|
500
|
+
for (let i = 0; i < count; i++) {
|
|
501
|
+
const autoFormatCriteria = autoFormatCriteriaArray[i]; // Skip code block nodes, unless the nodeTransformationKind calls for toggling the code block.
|
|
502
|
+
|
|
503
|
+
if (currentTriggerState.isCodeBlock === false || autoFormatCriteria.nodeTransformationKind === 'paragraphCodeBlock') {
|
|
504
|
+
const matchResultContext = getMatchResultContextForCriteria(autoFormatCriteria, scanningContext);
|
|
505
|
+
|
|
506
|
+
if (matchResultContext != null) {
|
|
507
|
+
matchResultContext.triggerState = currentTriggerState;
|
|
508
|
+
return {
|
|
509
|
+
autoFormatCriteria: autoFormatCriteria,
|
|
510
|
+
matchResultContext
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
return {
|
|
517
|
+
autoFormatCriteria: null,
|
|
518
|
+
matchResultContext: null
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function getTextNodeForAutoFormatting(selection) {
|
|
523
|
+
if (selection == null) {
|
|
524
|
+
return null;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const node = selection.anchor.getNode();
|
|
528
|
+
|
|
529
|
+
if (!lexical.$isTextNode(node)) {
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
return {
|
|
534
|
+
node,
|
|
535
|
+
offset: selection.anchor.offset
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function updateAutoFormatting(editor, currentTriggerState) {
|
|
540
|
+
editor.update(() => {
|
|
541
|
+
const textNodeWithOffset = getTextNodeForAutoFormatting(lexical.$getSelection());
|
|
542
|
+
|
|
543
|
+
if (textNodeWithOffset === null) {
|
|
544
|
+
return;
|
|
545
|
+
} // Please see the declaration of ScanningContext for a detailed explanation.
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
const scanningContext = {
|
|
549
|
+
joinedText: null,
|
|
550
|
+
textNodeWithOffset
|
|
551
|
+
};
|
|
552
|
+
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 -.
|
|
553
|
+
currentTriggerState.isParentAListItemNode === false ? getAllAutoFormatCriteria() : getAllAutoFormatCriteriaForTextNodes(), currentTriggerState, scanningContext);
|
|
554
|
+
|
|
555
|
+
if (criteriaWithMatchResultContext.autoFormatCriteria === null || criteriaWithMatchResultContext.matchResultContext === null) {
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
transformTextNodeForAutoFormatCriteria(scanningContext, criteriaWithMatchResultContext.autoFormatCriteria, criteriaWithMatchResultContext.matchResultContext);
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
function shouldAttemptToAutoFormat(currentTriggerState, priorTriggerState) {
|
|
564
|
+
if (currentTriggerState == null || priorTriggerState == null) {
|
|
565
|
+
return false;
|
|
566
|
+
} // The below checks needs to execute relativey quickly, so perform the light-weight ones first.
|
|
567
|
+
// The substr check is a quick way to avoid autoformat parsing in that it looks for the autoformat
|
|
568
|
+
// trigger which is the trigger string (" ").
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
const triggerStringLength = TRIGGER_STRING.length;
|
|
572
|
+
const currentTextContentLength = currentTriggerState.textContent.length;
|
|
573
|
+
const triggerOffset = currentTriggerState.anchorOffset - triggerStringLength;
|
|
574
|
+
return 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;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function getTriggerState(editorState) {
|
|
578
|
+
let criteria = null;
|
|
579
|
+
editorState.read(() => {
|
|
580
|
+
const selection = lexical.$getSelection();
|
|
581
|
+
|
|
582
|
+
if (selection == null || !selection.isCollapsed()) {
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const node = selection.anchor.getNode();
|
|
587
|
+
const parentNode = node.getParent();
|
|
588
|
+
const isParentAListItemNode = parentNode !== null && list.$isListItemNode(parentNode);
|
|
589
|
+
const hasParentNode = parentNode !== null;
|
|
590
|
+
criteria = {
|
|
591
|
+
anchorOffset: selection.anchor.offset,
|
|
592
|
+
hasParentNode,
|
|
593
|
+
isCodeBlock: CodeNode.$isCodeNode(node),
|
|
594
|
+
isParentAListItemNode,
|
|
595
|
+
isSelectionCollapsed: selection.isCollapsed(),
|
|
596
|
+
isSimpleText: lexical.$isTextNode(node) && node.isSimpleText(),
|
|
597
|
+
nodeKey: node.getKey(),
|
|
598
|
+
textContent: node.getTextContent()
|
|
599
|
+
};
|
|
600
|
+
});
|
|
601
|
+
return criteria;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function useAutoFormatter(editor) {
|
|
605
|
+
react.useEffect(() => {
|
|
606
|
+
// The priorTriggerState is compared against the currentTriggerState to determine
|
|
607
|
+
// if the user has performed some typing event that warrants an auto format.
|
|
608
|
+
// For example, typing "#" and then " ", shoud trigger an format.
|
|
609
|
+
// However, given "#A B", where the user delets "A" should not.
|
|
610
|
+
let priorTriggerState = null;
|
|
611
|
+
editor.addListener('update', ({
|
|
612
|
+
tags
|
|
613
|
+
}) => {
|
|
614
|
+
// Examine historic so that we are not running autoformatting within markdown.
|
|
615
|
+
if (tags.has('historic') === false) {
|
|
616
|
+
const currentTriggerState = getTriggerState(editor.getEditorState());
|
|
617
|
+
|
|
618
|
+
if (shouldAttemptToAutoFormat(currentTriggerState, priorTriggerState) && currentTriggerState != null) {
|
|
619
|
+
updateAutoFormatting(editor, currentTriggerState);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
priorTriggerState = currentTriggerState;
|
|
623
|
+
} else {
|
|
624
|
+
priorTriggerState = null;
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
}, [editor]);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
632
|
+
*
|
|
633
|
+
* This source code is licensed under the MIT license found in the
|
|
634
|
+
* LICENSE file in the root directory of this source tree.
|
|
635
|
+
*
|
|
636
|
+
*
|
|
637
|
+
*/
|
|
638
|
+
function useLexicalAutoFormatter(editor) {
|
|
639
|
+
useAutoFormatter(editor);
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
module.exports = useLexicalAutoFormatter;
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
y.$createParagraphNode():l.$createCodeNode(),d.append(...e)}a=d;null!==a&&b.replace(a)}}})}
|
|
11
|
-
function K(f){let h=null;f.read(()=>{const b=t.$getSelection();if(null!=b&&b.isCollapsed()){var a=b.anchor.getNode(),c=a.getParent(),e=null!==c&&m.$isListItemNode(c);c=null!==c&&t.$isElementNode(c);h={anchorOffset:b.anchor.offset,isCodeBlock:l.$isCodeNode(a),isSelectionCollapsed:b.isCollapsed(),isSimpleText:t.$isTextNode(a)&&a.isSimpleText(),isParentAnElementNode:c,isParentAListItemNode:e,nodeKey:a.getKey(),textContent:a.getTextContent()}}});return h}
|
|
12
|
-
function L(f){u.useEffect(()=>{let h=null;f.addListener("update",({tags:b})=>{if(!1===b.has("historic")){b=K(f.getEditorState());var a=b;var c=h;if(null==a||null==c)a=!1;else{var e=a.textContent.length,d=a.anchorOffset-1;a=!0===a.isParentAnElementNode&&a.isSimpleText&&a.isSelectionCollapsed&&a.nodeKey===c.nodeKey&&a.anchorOffset!==c.anchorOffset&&0<=d&&d+1<=e&&" "===a.textContent.substr(d,1)&&a.textContent!==c.textContent}a&&null!=b&&H(f,b);h=b}else h=null})},[f])}module.exports=function(f){L(f)};
|
|
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
|
+
const DEPRECATED_useLexicalAutoFormatter = process.env.NODE_ENV === 'development' ? require('./DEPRECATED_useLexicalAutoFormatter.dev.js') : require('./DEPRECATED_useLexicalAutoFormatter.prod.js')
|
|
9
|
+
module.exports = DEPRECATED_useLexicalAutoFormatter;
|