-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexical_rich_text_controller.ts
75 lines (63 loc) · 2.1 KB
/
lexical_rich_text_controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Controller } from "@hotwired/stimulus"
import { $getRoot, $insertNodes, createEditor, CreateEditorArgs, LexicalEditor } from 'lexical';
import {$generateHtmlFromNodes, $generateNodesFromDOM} from '@lexical/html';
import { registerRichText } from "@lexical/rich-text";
export default class LexicalController extends Controller {
static targets = ["editor", "input"]
/**
* The element to render the editor within.
*/
declare readonly editorTarget: HTMLElement;
/**
* The input field to output the rendered from the editor HTML to.
*/
declare readonly inputTarget: HTMLInputElement;
editor: LexicalEditor;
removeRichTextListener: () => void;
removeUpdateListener: () => void;
connect() {
this.initEditor()
this.loadInitialState()
this.removeRichTextListener = registerRichText(this.editor)
this.registerUpdateListener()
}
initEditor() {
this.editor = createEditor(this.config());
this.editor.setRootElement(this.editorTarget);
}
/**
* Load the HTML markup into the editor from the input field value.
*/
loadInitialState() {
this.editor.update(() => {
// In the browser you can use the native DOMParser API to parse the HTML string.
const parser = new DOMParser();
const dom = parser.parseFromString(this.inputTarget.value, "text/html");
// Once you have the DOM instance it's easy to generate LexicalNodes.
const nodes = $generateNodesFromDOM(this.editor, dom);
// Select the root
$getRoot().select();
// Insert them at a selection.
$insertNodes(nodes);
});
}
registerUpdateListener() {
this.removeUpdateListener = this.editor.registerUpdateListener(({editorState}) => {
editorState.read(() => {
const htmlString = $generateHtmlFromNodes(this.editor, null);
this.inputTarget.value = htmlString;
})
})
}
/**
* The configuration to pass to `createEditor`.
* @returns The configuration object.
*/
config(): CreateEditorArgs {
return {}
}
disconnect() {
this.removeRichTextListener()
this.removeUpdateListener()
}
}