|
| 1 | +/** |
| 2 | + * Reduced Jodit Editor for Frontend FAQ Submissions |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 5 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 6 | + * obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | + * |
| 8 | + * @package phpMyFAQ |
| 9 | + * @author Thorsten Rinne <[email protected]> |
| 10 | + * @copyright 2025 phpMyFAQ Team |
| 11 | + * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 12 | + * @link https://www.phpmyfaq.de |
| 13 | + * @since 2025-12-22 |
| 14 | + */ |
| 15 | + |
| 16 | +import { Jodit } from 'jodit'; |
| 17 | + |
| 18 | +import 'jodit/esm/plugins/clean-html/clean-html.js'; |
| 19 | +import 'jodit/esm/plugins/clipboard/clipboard.js'; |
| 20 | +import 'jodit/esm/plugins/delete/delete.js'; |
| 21 | +import 'jodit/esm/plugins/fullsize/fullsize.js'; |
| 22 | +import 'jodit/esm/plugins/image/image.js'; |
| 23 | +import 'jodit/esm/plugins/indent/indent.js'; |
| 24 | +import 'jodit/esm/plugins/justify/justify.js'; |
| 25 | +import 'jodit/esm/plugins/paste-from-word/paste-from-word.js'; |
| 26 | +import 'jodit/esm/plugins/preview/preview.js'; |
| 27 | +import 'jodit/esm/plugins/resizer/resizer.js'; |
| 28 | +import 'jodit/esm/plugins/select/select.js'; |
| 29 | +import 'jodit/esm/plugins/source/source.js'; |
| 30 | + |
| 31 | +let joditEditorInstance: any = null; |
| 32 | + |
| 33 | +export const getJoditEditor = () => joditEditorInstance; |
| 34 | + |
| 35 | +/** |
| 36 | + * Renders a reduced Jodit editor for the FAQ add form |
| 37 | + * This is a simplified version compared to the admin editor with: |
| 38 | + * - Basic formatting only |
| 39 | + * - No file uploads (security) |
| 40 | + * - No advanced features (tables, media, custom plugins) |
| 41 | + */ |
| 42 | +export const renderFaqEditor = () => { |
| 43 | + const answerField = document.getElementById('answer') as HTMLTextAreaElement | null; |
| 44 | + if (!answerField) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // Detect browser color scheme preference (dark/light) |
| 49 | + const prefersDark = window.matchMedia('(prefers-color-scheme: dark)'); |
| 50 | + |
| 51 | + const joditEditor = Jodit.make(answerField, { |
| 52 | + zIndex: 0, |
| 53 | + readonly: false, |
| 54 | + beautifyHTML: false, |
| 55 | + sourceEditor: 'area', |
| 56 | + activeButtonsInReadOnly: ['source', 'fullsize', 'preview'], |
| 57 | + toolbarButtonSize: 'middle', |
| 58 | + theme: prefersDark.matches ? 'dark' : 'default', |
| 59 | + saveModeInStorage: false, |
| 60 | + spellcheck: true, |
| 61 | + editorClassName: false, |
| 62 | + triggerChangeEvent: true, |
| 63 | + width: 'auto', |
| 64 | + height: 'auto', |
| 65 | + minHeight: 300, |
| 66 | + direction: '', |
| 67 | + language: 'auto', |
| 68 | + debugLanguage: false, |
| 69 | + tabIndex: -1, |
| 70 | + toolbar: true, |
| 71 | + enter: 'p', |
| 72 | + defaultMode: 1, // MODE_WYSIWYG |
| 73 | + useSplitMode: false, |
| 74 | + askBeforePasteFromWord: true, |
| 75 | + processPasteFromWord: true, |
| 76 | + defaultActionOnPasteFromWord: 'insert_clear_html', |
| 77 | + colors: { |
| 78 | + greyscale: [ |
| 79 | + '#000000', |
| 80 | + '#434343', |
| 81 | + '#666666', |
| 82 | + '#999999', |
| 83 | + '#B7B7B7', |
| 84 | + '#CCCCCC', |
| 85 | + '#D9D9D9', |
| 86 | + '#EFEFEF', |
| 87 | + '#F3F3F3', |
| 88 | + '#FFFFFF', |
| 89 | + ], |
| 90 | + palette: [ |
| 91 | + '#980000', |
| 92 | + '#FF0000', |
| 93 | + '#FF9900', |
| 94 | + '#FFFF00', |
| 95 | + '#00F0F0', |
| 96 | + '#00FFFF', |
| 97 | + '#4A86E8', |
| 98 | + '#0000FF', |
| 99 | + '#9900FF', |
| 100 | + '#FF00FF', |
| 101 | + ], |
| 102 | + }, |
| 103 | + colorPickerDefaultTab: 'background', |
| 104 | + imageDefaultWidth: 300, |
| 105 | + removeButtons: [], |
| 106 | + disablePlugins: ['file', 'video', 'media', 'table'], |
| 107 | + extraPlugins: [], |
| 108 | + extraButtons: [], |
| 109 | + buttons: [ |
| 110 | + 'source', |
| 111 | + '|', |
| 112 | + 'bold', |
| 113 | + 'italic', |
| 114 | + 'underline', |
| 115 | + 'strikethrough', |
| 116 | + '|', |
| 117 | + 'ul', |
| 118 | + 'ol', |
| 119 | + '|', |
| 120 | + 'outdent', |
| 121 | + 'indent', |
| 122 | + '|', |
| 123 | + 'left', |
| 124 | + 'center', |
| 125 | + 'right', |
| 126 | + '|', |
| 127 | + 'link', |
| 128 | + 'image', |
| 129 | + '|', |
| 130 | + 'undo', |
| 131 | + 'redo', |
| 132 | + '|', |
| 133 | + 'eraser', |
| 134 | + 'fullsize', |
| 135 | + 'preview', |
| 136 | + ], |
| 137 | + events: {}, |
| 138 | + textIcons: false, |
| 139 | + uploader: { |
| 140 | + url: '', // Disabled for frontend security |
| 141 | + }, |
| 142 | + filebrowser: { |
| 143 | + ajax: { |
| 144 | + url: '', |
| 145 | + }, |
| 146 | + }, |
| 147 | + }); |
| 148 | + |
| 149 | + const setJoditTheme = (theme: 'dark' | 'default'): void => { |
| 150 | + (joditEditor as any).options.theme = theme; |
| 151 | + |
| 152 | + const container: HTMLDivElement = joditEditor.container; |
| 153 | + container.classList.remove('jodit_theme_default', 'jodit_theme_dark'); |
| 154 | + container.classList.add(theme === 'dark' ? 'jodit_theme_dark' : 'jodit_theme_default'); |
| 155 | + }; |
| 156 | + |
| 157 | + const applyTheme = (): void => { |
| 158 | + setJoditTheme(prefersDark.matches ? 'dark' : 'default'); |
| 159 | + }; |
| 160 | + |
| 161 | + if (typeof prefersDark.addEventListener === 'function') { |
| 162 | + prefersDark.addEventListener('change', applyTheme); |
| 163 | + } else if (typeof (prefersDark as any).addListener === 'function') { |
| 164 | + (prefersDark as any).addListener(applyTheme); |
| 165 | + } |
| 166 | + |
| 167 | + const applyThemeFromAttribute = (): void => { |
| 168 | + const themeAttr: string | null = document.documentElement.getAttribute('data-bs-theme'); |
| 169 | + setJoditTheme(themeAttr === 'dark' ? 'dark' : 'default'); |
| 170 | + }; |
| 171 | + |
| 172 | + const themeObserver = new MutationObserver((mutations): void => { |
| 173 | + for (const m of mutations) { |
| 174 | + if (m.type === 'attributes' && m.attributeName === 'data-bs-theme') { |
| 175 | + applyThemeFromAttribute(); |
| 176 | + } |
| 177 | + } |
| 178 | + }); |
| 179 | + themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['data-bs-theme'] }); |
| 180 | + |
| 181 | + window.addEventListener('storage', (e: StorageEvent): void => { |
| 182 | + if (e.key === 'pmf-theme') { |
| 183 | + applyThemeFromAttribute(); |
| 184 | + } |
| 185 | + }); |
| 186 | + |
| 187 | + applyThemeFromAttribute(); |
| 188 | + |
| 189 | + joditEditorInstance = joditEditor; |
| 190 | +}; |
0 commit comments