Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(richtext): export a run of tabs in linear time (#7093)

Cindy Zhang92779d3fbd0f
diff --git a/packages/richtext/src/markdownSource.ts b/packages/richtext/src/markdownSource.ts
index 3f9d102a9e..9fccddf817 100644
--- a/packages/richtext/src/markdownSource.ts
+++ b/packages/richtext/src/markdownSource.ts
@@ -746,13 +746,16 @@ function markedView(
return view;
}
if ($isElementNode(node) && !$isCodeNode(node)) {
- const children = node
- .getChildren()
- .map(child => markedView(child, token, placeholders));
+ // Read once: each read walks every child, so reading per child would
+ // cost the square of their number — a run of tabs is one node per tab.
+ const original = node.getChildren();
+ const children = original.map(child =>
+ markedView(child, token, placeholders),
+ );
// Each view's siblings are the views beside it, so a text view sees a
// plugin node's text view as text with formats, not as a gap.
children.forEach((child, index) => {
- if (child !== node.getChildren()[index]) {
+ if (child !== original[index]) {
child.getPreviousSibling = <T extends LexicalNode>() =>
(children[index - 1] ?? null) as T | null;
child.getNextSibling = <T extends LexicalNode>() =>
diff --git a/packages/richtext/src/markdownTabRuns.test.ts b/packages/richtext/src/markdownTabRuns.test.ts
new file mode 100644
index 0000000000..a4b4c78043
--- /dev/null
+++ b/packages/richtext/src/markdownTabRuns.test.ts
@@ -0,0 +1,75 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+import {describe, expect, it} from 'vitest';
+import {createHeadlessEditor} from '@lexical/headless';
+import {$getRoot, $isTextNode} from 'lexical';
+import {DEFAULT_NODES} from './editorNodes';
+import {DEFAULT_TRANSFORMERS} from './markdownTable';
+import {
+ $exportMarkdownKeepingSource,
+ importMarkdownKeepingSource,
+} from './markdownSource';
+
+/** Imports `markdown`, appends `x` to its last text, and exports it. */
+function editAndExport(markdown: string): string {
+ const editor = createHeadlessEditor({
+ nodes: [...DEFAULT_NODES],
+ onError(error) {
+ throw error;
+ },
+ });
+ importMarkdownKeepingSource(editor, markdown, [...DEFAULT_TRANSFORMERS]);
+ editor.update(
+ () => {
+ const last = $getRoot().getLastDescendant();
+ if (!$isTextNode(last)) {
+ throw new Error('No text to edit');
+ }
+ last.setTextContent(`${last.getTextContent()}x`);
+ },
+ {discrete: true},
+ );
+ return editor
+ .getEditorState()
+ .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));
+}
+
+/**
+ * The least CPU time of three runs of `run`, in milliseconds. CPU time, not
+ * elapsed time: on a loaded test machine, other work stretches elapsed time
+ * but not the time the editor spends computing.
+ */
+function leastCpuTime(run: () => void): number {
+ run();
+ let least = Number.POSITIVE_INFINITY;
+ for (let round = 0; round < 3; round++) {
+ const started = process.cpuUsage();
+ run();
+ const used = process.cpuUsage(started);
+ least = Math.min(least, (used.user + used.system) / 1000);
+ }
+ return least;
+}
+
+const TABS = '\t'.repeat(8_000);
+
+describe('a run of tabs, one editor node per tab', () => {
+ it('keeps every tab through an edit', () => {
+ expect(editAndExport(`a${TABS}b\n`)).toBe(`a${TABS}bx\n`);
+ });
+
+ it.each([
+ ['plain text', `a${TABS}b\n`],
+ ['a list item', `- a${TABS}b\n`],
+ ['bold text', `**a${TABS}b**\n`],
+ ['link text', `[a${TABS}b](u)\n`],
+ ['a table cell', `| h |\n| - |\n| a${TABS}b |\n`],
+ ])(
+ 'imports, edits, and exports %s with 8,000 tabs within 1 s',
+ (_, markdown) => {
+ // Alone each takes about 0.1 s; reading a node's children once per child
+ // took over a minute.
+ expect(leastCpuTime(() => editAndExport(markdown))).toBeLessThan(1000);
+ },
+ );
+});