]> BookStack Code Mirror - bookstack/blob - resources/js/markdown/common-events.ts
MD Editor: Updated actions to use input interface
[bookstack] / resources / js / markdown / common-events.ts
1 import {MarkdownEditor} from "./index.mjs";
2
3 export interface HtmlOrMarkdown {
4     html: string;
5     markdown: string;
6 }
7
8 function getContentToInsert({html, markdown}: {html: string, markdown: string}): string {
9     return markdown || html;
10 }
11
12 export function listenToCommonEvents(editor: MarkdownEditor): void {
13     window.$events.listen('editor::replace', (eventContent: HtmlOrMarkdown) => {
14         const markdown = getContentToInsert(eventContent);
15         editor.actions.replaceContent(markdown);
16     });
17
18     window.$events.listen('editor::append', (eventContent: HtmlOrMarkdown) => {
19         const markdown = getContentToInsert(eventContent);
20         editor.actions.appendContent(markdown);
21     });
22
23     window.$events.listen('editor::prepend', (eventContent: HtmlOrMarkdown) => {
24         const markdown = getContentToInsert(eventContent);
25         editor.actions.prependContent(markdown);
26     });
27
28     window.$events.listen('editor::insert', (eventContent: HtmlOrMarkdown) => {
29         const markdown = getContentToInsert(eventContent);
30         editor.actions.insertContent(markdown);
31     });
32
33     window.$events.listen('editor::focus', () => {
34         editor.actions.focus();
35     });
36 }