File tree Expand file tree Collapse file tree 4 files changed +27
-20
lines changed
packages/ui/src/lib/richText Expand file tree Collapse file tree 4 files changed +27
-20
lines changed Original file line number Diff line number Diff line change 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 ,
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 }
Original file line number Diff line number Diff line change 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 ,
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 >
Original file line number Diff line number Diff line change 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 ,
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 ;
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments