mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-01-10 02:57:36 +00:00
e50cd33277
Also: - Added svg loading support (dummy stub) for jest. - Updated headless test case due to node changes. - Split out editor change detected to where appropriate. - Added functions to help with testing, like mocking our context.
55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
import {$getSelection, LexicalEditor} from "lexical";
|
|
import {
|
|
appendHtmlToEditor,
|
|
focusEditor,
|
|
insertHtmlIntoEditor,
|
|
prependHtmlToEditor,
|
|
setEditorContentFromHtml
|
|
} from "../utils/actions";
|
|
|
|
type EditorEventContent = {
|
|
html: string;
|
|
markdown: string;
|
|
};
|
|
|
|
function getContentToInsert(eventContent: EditorEventContent): string {
|
|
return eventContent.html || '';
|
|
}
|
|
|
|
export function listen(editor: LexicalEditor): void {
|
|
window.$events.listen<EditorEventContent>('editor::replace', eventContent => {
|
|
const html = getContentToInsert(eventContent);
|
|
setEditorContentFromHtml(editor, html);
|
|
});
|
|
|
|
window.$events.listen<EditorEventContent>('editor::append', eventContent => {
|
|
const html = getContentToInsert(eventContent);
|
|
appendHtmlToEditor(editor, html);
|
|
});
|
|
|
|
window.$events.listen<EditorEventContent>('editor::prepend', eventContent => {
|
|
const html = getContentToInsert(eventContent);
|
|
prependHtmlToEditor(editor, html);
|
|
});
|
|
|
|
window.$events.listen<EditorEventContent>('editor::insert', eventContent => {
|
|
const html = getContentToInsert(eventContent);
|
|
insertHtmlIntoEditor(editor, html);
|
|
});
|
|
|
|
window.$events.listen<EditorEventContent>('editor::focus', () => {
|
|
focusEditor(editor);
|
|
});
|
|
|
|
let changeFromLoading = true;
|
|
editor.registerUpdateListener(({dirtyElements, dirtyLeaves, editorState, prevEditorState}) => {
|
|
// Emit change event to component system (for draft detection) on actual user content change
|
|
if (dirtyElements.size > 0 || dirtyLeaves.size > 0) {
|
|
if (changeFromLoading) {
|
|
changeFromLoading = false;
|
|
} else {
|
|
window.$events.emit('editor-html-change', '');
|
|
}
|
|
}
|
|
});
|
|
}
|