From 9c71c6667a5285d66756eae7a5a94e8ef2615c6b Mon Sep 17 00:00:00 2001 From: Julia Casamitjana <62883011+JuliaCasamitjana@users.noreply.github.com> Date: Thu, 11 Apr 2024 10:34:37 +0200 Subject: [PATCH] Install and use ToastUi markdown editor Replace all usages of pagedown-bootstrap editor with the new editor. Add styles to ensure the editor preview matches the final output. --- app/assets/javascripts/markdown_editor.js | 77 ++++++++++++ app/assets/stylesheets/base.css.scss | 43 +++++-- app/helpers/pagedown_form_builder.rb | 34 ++--- app/javascript/toast-ui.js | 7 ++ app/javascript/toast-ui.scss | 71 +++++++++++ app/views/exercises/_form.html.slim | 4 +- app/views/proxy_exercises/_form.html.slim | 9 +- app/views/tips/_form.html.slim | 9 +- package.json | 1 + yarn.lock | 144 +++++++++++++++++++++- 10 files changed, 362 insertions(+), 37 deletions(-) create mode 100644 app/assets/javascripts/markdown_editor.js create mode 100644 app/javascript/toast-ui.js create mode 100644 app/javascript/toast-ui.scss 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