1 import {register as registerShortcuts} from "./shortcuts";
2 import {listen as listenForCommonEvents} from "./common-events";
3 import {scrollToQueryString, fixScrollForMobile} from "./scrolling";
4 import {listenForDragAndPaste} from "./drop-paste-handling";
6 import {getPlugin as getCodeeditorPlugin} from "./plugin-codeeditor";
7 import {getPlugin as getDrawioPlugin} from "./plugin-drawio";
8 import {getPlugin as getCustomhrPlugin} from "./plugins-customhr";
9 import {getPlugin as getImagemanagerPlugin} from "./plugins-imagemanager";
10 import {getPlugin as getAboutPlugin} from "./plugins-about";
12 const style_formats = [
13 {title: "Large Header", format: "h2", preview: 'color: blue;'},
14 {title: "Medium Header", format: "h3"},
15 {title: "Small Header", format: "h4"},
16 {title: "Tiny Header", format: "h5"},
17 {title: "Paragraph", format: "p", exact: true, classes: ''},
18 {title: "Blockquote", format: "blockquote"},
20 title: "Callouts", items: [
21 {title: "Information", format: 'calloutinfo'},
22 {title: "Success", format: 'calloutsuccess'},
23 {title: "Warning", format: 'calloutwarning'},
24 {title: "Danger", format: 'calloutdanger'}
30 codeeditor: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div'},
31 alignleft: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-left'},
32 aligncenter: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-center'},
33 alignright: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-right'},
34 calloutsuccess: {block: 'p', exact: true, attributes: {class: 'callout success'}},
35 calloutinfo: {block: 'p', exact: true, attributes: {class: 'callout info'}},
36 calloutwarning: {block: 'p', exact: true, attributes: {class: 'callout warning'}},
37 calloutdanger: {block: 'p', exact: true, attributes: {class: 'callout danger'}}
40 function file_picker_callback(callback, value, meta) {
42 // field_name, url, type, win
43 if (meta.filetype === 'file') {
44 window.EntitySelectorPopup.show(entity => {
45 callback(entity.link, {
52 if (meta.filetype === 'image') {
54 window.ImageManager.show(function (image) {
55 callback(image.url, {alt: image.name});
62 * @param {WysiwygConfigOptions} options
63 * @return {{toolbar: string, groupButtons: Object<string, Object>}}
65 function buildToolbar(options) {
66 const textDirPlugins = options.textDirection === 'rtl' ? 'ltr rtl' : '';
68 const groupButtons = {
72 items: 'strikethrough superscript subscript inlinecode removeformat'
77 items: 'outdent indent'
82 items: 'hr codeeditor drawio media'
89 'bold italic underline formatoverflow',
90 'forecolor backcolor',
91 'alignleft aligncenter alignright alignjustify',
92 'bullist numlist listoverflow',
94 'link table imagemanager-insert insertoverflow',
95 'code about fullscreen'
99 toolbar: toolbar.filter(row => Boolean(row)).join(' | '),
105 * @param {WysiwygConfigOptions} options
108 function gatherPlugins(options) {
125 options.textDirection === 'rtl' ? 'directionality' : '',
128 window.tinymce.PluginManager.add('codeeditor', getCodeeditorPlugin(options));
129 window.tinymce.PluginManager.add('customhr', getCustomhrPlugin(options));
130 window.tinymce.PluginManager.add('imagemanager', getImagemanagerPlugin(options));
131 window.tinymce.PluginManager.add('about', getAboutPlugin(options));
133 if (options.drawioUrl) {
134 window.tinymce.PluginManager.add('drawio', getDrawioPlugin(options));
135 plugins.push('drawio');
138 return plugins.filter(plugin => Boolean(plugin)).join(' ');
142 * Load custom HTML head content from the settings into the editor.
143 * TODO: We should be able to get this from current parent page?
144 * @param {Editor} editor
146 function loadCustomHeadContent(editor) {
147 window.$http.get(window.baseUrl('/custom-head-content')).then(resp => {
148 if (!resp.data) return;
149 let head = editor.getDoc().querySelector('head');
150 head.innerHTML += resp.data;
155 * @param {WysiwygConfigOptions} options
156 * @return {function(Editor)}
158 function getSetupCallback(options) {
159 return function(editor) {
160 editor.on('ExecCommand change input NodeChange ObjectResized', editorChange);
161 listenForCommonEvents(editor);
162 registerShortcuts(editor);
163 listenForDragAndPaste(editor, options);
165 editor.on('init', () => {
167 scrollToQueryString(editor);
168 fixScrollForMobile(editor);
169 window.editor = editor;
172 function editorChange() {
173 const content = editor.getContent();
174 if (options.darkMode) {
175 editor.contentDocument.documentElement.classList.add('dark-mode');
177 window.$events.emit('editor-html-change', content);
180 // TODO - Update to standardise across both editors
181 // Use events within listenForBookStackEditorEvents instead (Different event signature)
182 window.$events.listen('editor-html-update', html => {
183 editor.setContent(html);
184 editor.selection.select(editor.getBody(), true);
185 editor.selection.collapse(false);
189 // Custom handler hook
190 window.$events.emitPublic(options.containerElement, 'editor-tinymce::setup', {editor});
192 // Inline code format button
193 editor.ui.registry.addButton('inlinecode', {
194 tooltip: 'Inline code',
197 editor.execCommand('mceToggleFormat', false, 'code');
204 * @param {WysiwygConfigOptions} options
207 export function build(options) {
210 window.tinymce.addI18n(options.language, options.translationMap);
212 const {toolbar, groupButtons: toolBarGroupButtons} = buildToolbar(options);
214 // Return config object
218 selector: '#html-editor',
220 window.baseUrl('/dist/styles.css'),
223 skin: options.darkMode ? 'oxide-dark' : 'oxide',
224 body_class: 'page-content',
225 browser_spellcheck: true,
226 relative_urls: false,
227 language: options.language,
228 directionality: options.textDirection,
229 remove_script_host: false,
230 document_base_url: window.baseUrl('/'),
231 end_container_on_empty_block: true,
234 paste_data_images: false,
235 extended_valid_elements: 'pre[*],svg[*],div[drawio-diagram]',
236 automatic_uploads: false,
237 valid_children: "-div[p|h1|h2|h3|h4|h5|h6|blockquote],+div[pre],+div[img]",
238 plugins: gatherPlugins(options),
239 imagetools_toolbar: 'imageoptions',
242 content_style: `html, body, html.dark-mode {background: ${options.darkMode ? '#222' : '#fff'};} body {padding-left: 15px !important; padding-right: 15px !important; margin:0!important; margin-left:auto!important;margin-right:auto!important;}`,
244 style_formats_merge: false,
245 media_alt_source: false,
248 file_picker_types: 'file image',
249 file_picker_callback,
250 paste_preprocess(plugin, args) {
251 let content = args.content;
252 if (content.indexOf('<img src="file://') !== -1) {
256 init_instance_callback(editor) {
257 loadCustomHeadContent(editor);
260 for (const [key, config] of Object.entries(toolBarGroupButtons)) {
261 editor.ui.registry.addGroupToolbarButton(key, config);
263 getSetupCallback(options)(editor);
269 * @typedef {Object} WysiwygConfigOptions
270 * @property {Element} containerElement
271 * @property {string} language
272 * @property {boolean} darkMode
273 * @property {string} textDirection
274 * @property {string} drawioUrl
275 * @property {int} pageId
276 * @property {Object} translations
277 * @property {Object} translationMap