1 import MarkdownIt from "markdown-it";
2 import mdTasksLists from 'markdown-it-task-lists';
3 import code from '../services/code';
4 import Clipboard from "../services/clipboard";
5 import {debounce} from "../services/util";
7 import DrawIO from "../services/drawio";
14 const pageEditor = document.getElementById('page-editor');
15 this.pageId = pageEditor.getAttribute('page-id');
16 this.textDirection = pageEditor.getAttribute('text-direction');
18 this.markdown = new MarkdownIt({html: true});
19 this.markdown.use(mdTasksLists, {label: true});
21 this.display = this.elem.querySelector('.markdown-display');
23 this.displayStylesLoaded = false;
24 this.input = this.elem.querySelector('textarea');
25 this.htmlInput = this.elem.querySelector('input[name=html]');
26 this.cm = code.markdownEditor(this.input);
28 this.onMarkdownScroll = this.onMarkdownScroll.bind(this);
30 this.display.addEventListener('load', () => {
31 this.displayDoc = this.display.contentDocument;
35 window.$events.emitPublic(elem, 'editor-markdown::setup', {
36 markdownIt: this.markdown,
37 displayEl: this.display,
38 codeMirrorInstance: this.cm,
46 // Prevent markdown display link click redirect
47 this.displayDoc.addEventListener('click', event => {
48 let isDblClick = Date.now() - lastClick < 300;
50 let link = event.target.closest('a');
52 event.preventDefault();
53 window.open(link.getAttribute('href'));
57 let drawing = event.target.closest('[drawio-diagram]');
58 if (drawing !== null && isDblClick) {
59 this.actionEditDrawing(drawing);
63 lastClick = Date.now();
67 this.elem.addEventListener('click', event => {
68 let button = event.target.closest('button[data-action]');
69 if (button === null) return;
71 let action = button.getAttribute('data-action');
72 if (action === 'insertImage') this.actionInsertImage();
73 if (action === 'insertLink') this.actionShowLinkSelector();
74 if (action === 'insertDrawing' && (event.ctrlKey || event.metaKey)) {
75 this.actionShowImageManager();
78 if (action === 'insertDrawing') this.actionStartDrawing();
81 // Mobile section toggling
82 this.elem.addEventListener('click', event => {
83 const toolbarLabel = event.target.closest('.editor-toolbar-label');
84 if (!toolbarLabel) return;
86 const currentActiveSections = this.elem.querySelectorAll('.markdown-editor-wrap');
87 for (let activeElem of currentActiveSections) {
88 activeElem.classList.remove('active');
91 toolbarLabel.closest('.markdown-editor-wrap').classList.add('active');
94 window.$events.listen('editor-markdown-update', value => {
95 this.cm.setValue(value);
96 this.updateAndRender();
99 this.codeMirrorSetup();
100 this.listenForBookStackEditorEvents();
102 // Scroll to text if needed.
103 const queryParams = (new URL(window.location)).searchParams;
104 const scrollText = queryParams.get('content-text');
106 this.scrollToText(scrollText);
110 // Update the input content and render the display.
112 const content = this.cm.getValue();
113 this.input.value = content;
114 const html = this.markdown.render(content);
115 window.$events.emit('editor-html-change', html);
116 window.$events.emit('editor-markdown-change', content);
119 this.displayDoc.body.className = 'page-content';
120 this.displayDoc.body.innerHTML = html;
121 this.htmlInput.value = html;
123 // Copy styles from page head and set custom styles for editor
124 this.loadStylesIntoDisplay();
127 loadStylesIntoDisplay() {
128 if (this.displayStylesLoaded) return;
129 this.displayDoc.documentElement.className = 'markdown-editor-display';
131 this.displayDoc.head.innerHTML = '';
132 const styles = document.head.querySelectorAll('style,link[rel=stylesheet]');
133 for (let style of styles) {
134 const copy = style.cloneNode(true);
135 this.displayDoc.head.appendChild(copy);
138 this.displayStylesLoaded = true;
141 onMarkdownScroll(lineCount) {
142 const elems = this.displayDoc.body.children;
143 if (elems.length <= lineCount) return;
145 const topElem = (lineCount === -1) ? elems[elems.length-1] : elems[lineCount];
146 topElem.scrollIntoView({ block: 'start', inline: 'nearest', behavior: 'smooth'});
151 const context = this;
154 // cm.setOption('direction', this.textDirection);
155 cm.setOption('direction', 'ltr'); // Will force to remain as ltr for now due to issues when HTML is in editor.
156 // Custom key commands
157 let metaKey = code.getMetaKey();
158 const extraKeys = {};
159 // Insert Image shortcut
160 extraKeys[`${metaKey}-Alt-I`] = function(cm) {
161 let selectedText = cm.getSelection();
162 let newText = ``;
163 let cursorPos = cm.getCursor('from');
164 cm.replaceSelection(newText);
165 cm.setCursor(cursorPos.line, cursorPos.ch + newText.length -1);
168 extraKeys[`${metaKey}-S`] = cm => {window.$events.emit('editor-save-draft')};
170 extraKeys[`${metaKey}-Enter`] = cm => {window.$events.emit('editor-save-page')};
171 // Show link selector
172 extraKeys[`Shift-${metaKey}-K`] = cm => {this.actionShowLinkSelector()};
174 extraKeys[`${metaKey}-K`] = cm => {insertLink()};
176 extraKeys[`${metaKey}-1`] = cm => {replaceLineStart('##');};
177 extraKeys[`${metaKey}-2`] = cm => {replaceLineStart('###');};
178 extraKeys[`${metaKey}-3`] = cm => {replaceLineStart('####');};
179 extraKeys[`${metaKey}-4`] = cm => {replaceLineStart('#####');};
180 extraKeys[`${metaKey}-5`] = cm => {replaceLineStart('');};
181 extraKeys[`${metaKey}-d`] = cm => {replaceLineStart('');};
182 extraKeys[`${metaKey}-6`] = cm => {replaceLineStart('>');};
183 extraKeys[`${metaKey}-q`] = cm => {replaceLineStart('>');};
184 extraKeys[`${metaKey}-7`] = cm => {wrapSelection('\n```\n', '\n```');};
185 extraKeys[`${metaKey}-8`] = cm => {wrapSelection('`', '`');};
186 extraKeys[`Shift-${metaKey}-E`] = cm => {wrapSelection('`', '`');};
187 extraKeys[`${metaKey}-9`] = cm => {wrapSelection('<p class="callout info">', '</p>');};
188 cm.setOption('extraKeys', extraKeys);
190 // Update data on content change
191 cm.on('change', (instance, changeObj) => {
192 this.updateAndRender();
195 const onScrollDebounced = debounce((instance) => {
196 // Thanks to http://liuhao.im/english/2015/11/10/the-sync-scroll-of-markdown-editor-in-javascript.html
197 let scroll = instance.getScrollInfo();
198 let atEnd = scroll.top + scroll.clientHeight === scroll.height;
200 this.onMarkdownScroll(-1);
204 let lineNum = instance.lineAtHeight(scroll.top, 'local');
205 let range = instance.getRange({line: 0, ch: null}, {line: lineNum, ch: null});
206 let parser = new DOMParser();
207 let doc = parser.parseFromString(this.markdown.render(range), 'text/html');
208 let totalLines = doc.documentElement.querySelectorAll('body > *');
209 this.onMarkdownScroll(totalLines.length);
212 // Handle scroll to sync display view
213 cm.on('scroll', instance => {
214 onScrollDebounced(instance);
217 // Handle image paste
218 cm.on('paste', (cm, event) => {
219 const clipboard = new Clipboard(event.clipboardData || event.dataTransfer);
221 // Don't handle the event ourselves if no items exist of contains table-looking data
222 if (!clipboard.hasItems() || clipboard.containsTabularData()) {
226 const images = clipboard.getImages();
227 for (const image of images) {
232 // Handle image & content drag n drop
233 cm.on('drop', (cm, event) => {
235 const templateId = event.dataTransfer.getData('bookstack/template');
237 const cursorPos = cm.coordsChar({left: event.pageX, top: event.pageY});
238 cm.setCursor(cursorPos);
239 event.preventDefault();
240 window.$http.get(`/templates/${templateId}`).then(resp => {
241 const content = resp.data.markdown || resp.data.html;
242 cm.replaceSelection(content);
246 const clipboard = new Clipboard(event.dataTransfer);
247 if (clipboard.hasItems()) {
248 const cursorPos = cm.coordsChar({left: event.pageX, top: event.pageY});
249 cm.setCursor(cursorPos);
250 event.stopPropagation();
251 event.preventDefault();
252 const images = clipboard.getImages();
253 for (const image of images) {
260 // Helper to replace editor content
261 function replaceContent(search, replace) {
262 let text = cm.getValue();
263 let cursor = cm.listSelections();
264 cm.setValue(text.replace(search, replace));
265 cm.setSelections(cursor);
268 // Helper to replace the start of the line
269 function replaceLineStart(newStart) {
270 let cursor = cm.getCursor();
271 let lineContent = cm.getLine(cursor.line);
272 let lineLen = lineContent.length;
273 let lineStart = lineContent.split(' ')[0];
275 // Remove symbol if already set
276 if (lineStart === newStart) {
277 lineContent = lineContent.replace(`${newStart} `, '');
278 cm.replaceRange(lineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
279 cm.setCursor({line: cursor.line, ch: cursor.ch - (newStart.length + 1)});
283 let alreadySymbol = /^[#>`]/.test(lineStart);
286 posDif = newStart.length - lineStart.length;
287 lineContent = lineContent.replace(lineStart, newStart).trim();
288 } else if (newStart !== '') {
289 posDif = newStart.length + 1;
290 lineContent = newStart + ' ' + lineContent;
292 cm.replaceRange(lineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
293 cm.setCursor({line: cursor.line, ch: cursor.ch + posDif});
296 function wrapLine(start, end) {
297 let cursor = cm.getCursor();
298 let lineContent = cm.getLine(cursor.line);
299 let lineLen = lineContent.length;
300 let newLineContent = lineContent;
302 if (lineContent.indexOf(start) === 0 && lineContent.slice(-end.length) === end) {
303 newLineContent = lineContent.slice(start.length, lineContent.length - end.length);
305 newLineContent = `${start}${lineContent}${end}`;
308 cm.replaceRange(newLineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
309 cm.setCursor({line: cursor.line, ch: cursor.ch + start.length});
312 function wrapSelection(start, end) {
313 let selection = cm.getSelection();
314 if (selection === '') return wrapLine(start, end);
316 let newSelection = selection;
320 if (selection.indexOf(start) === 0 && selection.slice(-end.length) === end) {
321 newSelection = selection.slice(start.length, selection.length - end.length);
322 endDiff = -(end.length + start.length);
324 newSelection = `${start}${selection}${end}`;
325 endDiff = start.length + end.length;
328 let selections = cm.listSelections()[0];
329 cm.replaceSelection(newSelection);
330 let headFirst = selections.head.ch <= selections.anchor.ch;
331 selections.head.ch += headFirst ? frontDiff : endDiff;
332 selections.anchor.ch += headFirst ? endDiff : frontDiff;
333 cm.setSelections([selections]);
336 // Handle image upload and add image into markdown content
337 function uploadImage(file) {
338 if (file === null || file.type.indexOf('image') !== 0) return;
342 let fileNameMatches = file.name.match(/\.(.+)$/);
343 if (fileNameMatches.length > 1) ext = fileNameMatches[1];
346 // Insert image into markdown
347 const id = "image-" + Math.random().toString(16).slice(2);
348 const placeholderImage = window.baseUrl(`/loading.gif#upload${id}`);
349 const selectedText = cm.getSelection();
350 const placeHolderText = ``;
351 const cursor = cm.getCursor();
352 cm.replaceSelection(placeHolderText);
353 cm.setCursor({line: cursor.line, ch: cursor.ch + selectedText.length + 3});
355 const remoteFilename = "image-" + Date.now() + "." + ext;
356 const formData = new FormData();
357 formData.append('file', file, remoteFilename);
358 formData.append('uploaded_to', context.pageId);
360 window.$http.post('/images/gallery', formData).then(resp => {
361 const newContent = `[](${resp.data.url})`;
362 replaceContent(placeHolderText, newContent);
364 window.$events.emit('error', trans('errors.image_upload_error'));
365 replaceContent(placeHolderText, selectedText);
370 function insertLink() {
371 let cursorPos = cm.getCursor('from');
372 let selectedText = cm.getSelection() || '';
373 let newText = `[${selectedText}]()`;
375 cm.replaceSelection(newText);
376 let cursorPosDiff = (selectedText === '') ? -3 : -1;
377 cm.setCursor(cursorPos.line, cursorPos.ch + newText.length+cursorPosDiff);
380 this.updateAndRender();
383 actionInsertImage() {
384 const cursorPos = this.cm.getCursor('from');
385 window.ImageManager.show(image => {
386 let selectedText = this.cm.getSelection();
387 let newText = "[](" + image.url + ")";
389 this.cm.replaceSelection(newText);
390 this.cm.setCursor(cursorPos.line, cursorPos.ch + newText.length);
394 actionShowImageManager() {
395 const cursorPos = this.cm.getCursor('from');
396 window.ImageManager.show(image => {
397 this.insertDrawing(image, cursorPos);
401 // Show the popup link selector and insert a link when finished
402 actionShowLinkSelector() {
403 const cursorPos = this.cm.getCursor('from');
404 window.EntitySelectorPopup.show(entity => {
405 let selectedText = this.cm.getSelection() || entity.name;
406 let newText = `[${selectedText}](${entity.link})`;
408 this.cm.replaceSelection(newText);
409 this.cm.setCursor(cursorPos.line, cursorPos.ch + newText.length);
413 // Show draw.io if enabled and handle save.
414 actionStartDrawing() {
415 if (document.querySelector('[drawio-enabled]').getAttribute('drawio-enabled') !== 'true') return;
416 let cursorPos = this.cm.getCursor('from');
419 return Promise.resolve('');
421 // let id = "image-" + Math.random().toString(16).slice(2);
422 // let loadingImage = window.baseUrl('/loading.gif');
425 uploaded_to: Number(document.getElementById('page-editor').getAttribute('page-id'))
428 window.$http.post(window.baseUrl('/images/drawio'), data).then(resp => {
429 this.insertDrawing(resp.data, cursorPos);
432 window.$events.emit('error', trans('errors.image_upload_error'));
438 insertDrawing(image, originalCursor) {
439 const newText = `<div drawio-diagram="${image.id}"><img src="${image.url}"></div>`;
441 this.cm.replaceSelection(newText);
442 this.cm.setCursor(originalCursor.line, originalCursor.ch + newText.length);
445 // Show draw.io if enabled and handle save.
446 actionEditDrawing(imgContainer) {
447 const drawingDisabled = document.querySelector('[drawio-enabled]').getAttribute('drawio-enabled') !== 'true';
448 if (drawingDisabled) {
452 const cursorPos = this.cm.getCursor('from');
453 const drawingId = imgContainer.getAttribute('drawio-diagram');
456 return DrawIO.load(drawingId);
461 uploaded_to: Number(document.getElementById('page-editor').getAttribute('page-id'))
464 window.$http.post(window.baseUrl(`/images/drawio`), data).then(resp => {
465 let newText = `<div drawio-diagram="${resp.data.id}"><img src="${resp.data.url}"></div>`;
466 let newContent = this.cm.getValue().split('\n').map(line => {
467 if (line.indexOf(`drawio-diagram="${drawingId}"`) !== -1) {
472 this.cm.setValue(newContent);
473 this.cm.setCursor(cursorPos);
477 window.$events.emit('error', trans('errors.image_upload_error'));
483 // Scroll to a specified text
484 scrollToText(searchText) {
489 const content = this.cm.getValue();
490 const lines = content.split(/\r?\n/);
491 let lineNumber = lines.findIndex(line => {
492 return line && line.indexOf(searchText) !== -1;
495 if (lineNumber === -1) {
499 this.cm.scrollIntoView({
503 // set the cursor location.
506 char: lines[lineNumber].length
510 listenForBookStackEditorEvents() {
512 function getContentToInsert({html, markdown}) {
513 return markdown || html;
516 // Replace editor content
517 window.$events.listen('editor::replace', (eventContent) => {
518 const markdown = getContentToInsert(eventContent);
519 this.cm.setValue(markdown);
522 // Append editor content
523 window.$events.listen('editor::append', (eventContent) => {
524 const cursorPos = this.cm.getCursor('from');
525 const markdown = getContentToInsert(eventContent);
526 const content = this.cm.getValue() + '\n' + markdown;
527 this.cm.setValue(content);
528 this.cm.setCursor(cursorPos.line, cursorPos.ch);
531 // Prepend editor content
532 window.$events.listen('editor::prepend', (eventContent) => {
533 const cursorPos = this.cm.getCursor('from');
534 const markdown = getContentToInsert(eventContent);
535 const content = markdown + '\n' + this.cm.getValue();
536 this.cm.setValue(content);
537 const prependLineCount = markdown.split('\n').length;
538 this.cm.setCursor(cursorPos.line + prependLineCount, cursorPos.ch);
543 export default MarkdownEditor ;