Skip to content

Commit 695e208

Browse files
committed
Refactor rich text editor functions to share text export function
Extract common text export logic from multiple rich text editor functions into a single shared export function to reduce code duplication and improve maintainability.
1 parent 56e25fc commit 695e208

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

packages/ui/src/lib/richText/RichTextEditor.svelte

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import OnChangePlugin, { type OnChangeCallback } from '$lib/richText/plugins/onChange.svelte';
1212
import OnInput, { type OnInputCallback } from '$lib/richText/plugins/onInput.svelte';
1313
import { insertTextAtCaret, setEditorText } from '$lib/richText/selection';
14+
import { exportPlaintext } from '$lib/richText/utils/export';
1415
import {
1516
COMMAND_PRIORITY_CRITICAL,
1617
$getRoot as getRoot,
@@ -173,11 +174,7 @@
173174
editor.read(() => {
174175
// Using `root.getTextContent()` adds extra blank lines between paragraphs, since
175176
// normally paragraphs have a bottom margin (that we removed).
176-
const root = getRoot();
177-
const children = root.getChildren();
178-
const paragraphTexts = children.map((child) => child.getTextContent());
179-
const text = paragraphTexts.join('\n');
180-
resolve(text);
177+
resolve(exportPlaintext(getRoot()));
181178
});
182179
});
183180
}

packages/ui/src/lib/richText/plugins/onChange.svelte

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<script lang="ts">
1010
import { getEditor } from '$lib/richText/context';
1111
import { getEditorTextAfterAnchor, getEditorTextUpToAnchor } from '$lib/richText/selection';
12+
import { exportPlaintext } from '$lib/richText/utils/export';
1213
import { $getRoot as getRoot } from 'lexical';
1314
import {
1415
BLUR_COMMAND,
@@ -32,28 +33,22 @@
3233
BLUR_COMMAND,
3334
() => {
3435
editor.read(() => {
35-
const currentText = getRoot().getTextContent();
36-
if (currentText === text) {
37-
return;
38-
}
39-
36+
const currentText = exportPlaintext(getRoot());
37+
if (currentText === text) return;
4038
text = currentText;
39+
4140
const selection = getSelection();
4241
if (!isRangeSelection(selection)) {
4342
return;
4443
}
4544
4645
const textUpToAnchor = getEditorTextUpToAnchor(selection);
4746
const textAfterAnchor = getEditorTextAfterAnchor(selection);
48-
onChange?.(text, textUpToAnchor, textAfterAnchor);
47+
onChange?.(exportPlaintext(getRoot()), textUpToAnchor, textAfterAnchor);
4948
});
5049
return false;
5150
},
5251
COMMAND_PRIORITY_NORMAL
5352
);
5453
});
55-
56-
export function getText(): string | undefined {
57-
return text;
58-
}
5954
</script>

packages/ui/src/lib/richText/plugins/onInput.svelte

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<script lang="ts">
1010
import { getEditor } from '$lib/richText/context';
1111
import { getEditorTextAfterAnchor, getEditorTextUpToAnchor } from '$lib/richText/selection';
12+
import { exportPlaintext } from '$lib/richText/utils/export';
1213
import {
1314
$getRoot as getRoot,
1415
$getSelection as getSelection,
@@ -37,12 +38,10 @@
3738
}
3839
3940
editorState.read(() => {
40-
const currentText = getRoot().getTextContent();
41-
if (currentText === text) {
42-
return;
43-
}
44-
41+
const currentText = exportPlaintext(getRoot());
42+
if (currentText === text) return;
4543
text = currentText;
44+
4645
const selection = getSelection();
4746
if (!isRangeSelection(selection)) {
4847
return;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { RootNode } from 'lexical';
2+
3+
/**
4+
* We keep this export function in a separate package accessible to both plugins
5+
* as well as the core editor functionality.
6+
*
7+
* TODO: Restructure the richText code so we don't need this separate `utils` package.
8+
*/
9+
export function exportPlaintext(root: RootNode): string {
10+
// Using `root.getTextContent()` adds extra blank lines between paragraphs, since
11+
// normally paragraphs have a bottom margin (that we removed).
12+
const children = root.getChildren();
13+
const paragraphTexts = children.map((child) => child.getTextContent());
14+
const text = paragraphTexts.join('\n');
15+
return text;
16+
}

0 commit comments

Comments
 (0)