diff --git a/app/assets/javascripts/markdown_editor.js b/app/assets/javascripts/markdown_editor.js new file mode 100644 index 00000000..a9354843 --- /dev/null +++ b/app/assets/javascripts/markdown_editor.js @@ -0,0 +1,77 @@ +/** + * ToastUi editor initializer + * + * This script transforms form textareas created with + * "PagedownFormBuilder" into ToastUi markdown editors. + * + */ + +const initializeMarkdownEditors = () => { + const editors = document.querySelectorAll( + '[data-behavior="markdown-editor-widget"]' + ); + + editors.forEach((editor) => { + const formInput = document.querySelector(`#${editor.dataset.id}`); + if (!editor || !formInput) return; + + const toastEditor = new ToastUi({ + el: editor, + theme: window.getCurrentTheme(), + initialValue: formInput.value, + placeholder: formInput.placeholder, + extendedAutolinks: true, + linkAttributes: { + target: "_blank", + }, + previewHighlight: false, + height: "400px", + autofocus: false, + usageStatistics: false, + language: I18n.locale, + toolbarItems: [ + ["heading", "bold", "italic"], + ["link", "quote", "code", "codeblock"], + ["ul", "ol"], + ], + initialEditType: "markdown", + events: { + change: () => { + // Keep real form