1 import MarkdownIt from "markdown-it";
2 import mdTasksLists from 'markdown-it-task-lists';
3 import Clipboard from "../services/clipboard";
4 import {debounce} from "../services/util";
5 import {patchDomFromHtmlString} from "../services/vdom";
6 import DrawIO from "../services/drawio";
13 this.pageId = this.$opts.pageId;
14 this.textDirection = this.$opts.textDirection;
15 this.imageUploadErrorText = this.$opts.imageUploadErrorText;
16 this.serverUploadLimitText = this.$opts.serverUploadLimitText;
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');
28 const cmLoadPromise = window.importVersioned('code').then(Code => {
29 this.cm = Code.markdownEditor(this.input);
34 this.onMarkdownScroll = this.onMarkdownScroll.bind(this);
36 const displayLoad = () => {
37 this.displayDoc = this.display.contentDocument;
38 this.init(cmLoadPromise);
41 if (this.display.contentDocument.readyState === 'complete') {
44 this.display.addEventListener('load', displayLoad.bind(this));
47 window.$events.emitPublic(this.elem, 'editor-markdown::setup', {
48 markdownIt: this.markdown,
49 displayEl: this.display,
50 codeMirrorInstance: this.cm,
58 // Prevent markdown display link click redirect
59 this.displayDoc.addEventListener('click', event => {
60 let isDblClick = Date.now() - lastClick < 300;
62 let link = event.target.closest('a');
64 event.preventDefault();
65 window.open(link.getAttribute('href'));
69 let drawing = event.target.closest('[drawio-diagram]');
70 if (drawing !== null && isDblClick) {
71 this.actionEditDrawing(drawing);
75 lastClick = Date.now();
79 this.elem.addEventListener('click', event => {
80 let button = event.target.closest('button[data-action]');
81 if (button === null) return;
83 let action = button.getAttribute('data-action');
84 if (action === 'insertImage') this.actionInsertImage();
85 if (action === 'insertLink') this.actionShowLinkSelector();
86 if (action === 'insertDrawing' && (event.ctrlKey || event.metaKey)) {
87 this.actionShowImageManager();
90 if (action === 'insertDrawing') this.actionStartDrawing();
91 if (action === 'fullscreen') this.actionFullScreen();
94 // Mobile section toggling
95 this.elem.addEventListener('click', event => {
96 const toolbarLabel = event.target.closest('.editor-toolbar-label');
97 if (!toolbarLabel) return;
99 const currentActiveSections = this.elem.querySelectorAll('.markdown-editor-wrap');
100 for (let activeElem of currentActiveSections) {
101 activeElem.classList.remove('active');
104 toolbarLabel.closest('.markdown-editor-wrap').classList.add('active');
107 cmLoadPromise.then(cm => {
108 this.codeMirrorSetup(cm);
110 // Refresh CodeMirror on container resize
111 const resizeDebounced = debounce(() => this.Code.updateLayout(cm), 100, false);
112 const observer = new ResizeObserver(resizeDebounced);
113 observer.observe(this.elem);
116 this.listenForBookStackEditorEvents();
118 // Scroll to text if needed.
119 const queryParams = (new URL(window.location)).searchParams;
120 const scrollText = queryParams.get('content-text');
122 this.scrollToText(scrollText);
126 // Update the input content and render the display.
128 const content = this.cm.getValue();
129 this.input.value = content;
131 const html = this.markdown.render(content);
132 window.$events.emit('editor-html-change', html);
133 window.$events.emit('editor-markdown-change', content);
136 const target = this.getDisplayTarget();
137 this.displayDoc.body.className = 'page-content';
138 patchDomFromHtmlString(target, html);
140 // Copy styles from page head and set custom styles for editor
141 this.loadStylesIntoDisplay();
145 const body = this.displayDoc.body;
147 if (body.children.length === 0) {
148 const wrap = document.createElement('div');
149 this.displayDoc.body.append(wrap);
152 return body.children[0];
155 loadStylesIntoDisplay() {
156 if (this.displayStylesLoaded) return;
157 this.displayDoc.documentElement.classList.add('markdown-editor-display');
158 // Set display to be dark mode if parent is
160 if (document.documentElement.classList.contains('dark-mode')) {
161 this.displayDoc.documentElement.style.backgroundColor = '#222';
162 this.displayDoc.documentElement.classList.add('dark-mode');
165 this.displayDoc.head.innerHTML = '';
166 const styles = document.head.querySelectorAll('style,link[rel=stylesheet]');
167 for (let style of styles) {
168 const copy = style.cloneNode(true);
169 this.displayDoc.head.appendChild(copy);
172 this.displayStylesLoaded = true;
175 onMarkdownScroll(lineCount) {
176 const elems = this.displayDoc.body.children;
177 if (elems.length <= lineCount) return;
179 const topElem = (lineCount === -1) ? elems[elems.length-1] : elems[lineCount];
180 topElem.scrollIntoView({ block: 'start', inline: 'nearest', behavior: 'smooth'});
183 codeMirrorSetup(cm) {
184 const context = this;
187 // cm.setOption('direction', this.textDirection);
188 cm.setOption('direction', 'ltr'); // Will force to remain as ltr for now due to issues when HTML is in editor.
189 // Custom key commands
190 let metaKey = this.Code.getMetaKey();
191 const extraKeys = {};
192 // Insert Image shortcut
193 extraKeys[`${metaKey}-Alt-I`] = function(cm) {
194 let selectedText = cm.getSelection();
195 let newText = ``;
196 let cursorPos = cm.getCursor('from');
197 cm.replaceSelection(newText);
198 cm.setCursor(cursorPos.line, cursorPos.ch + newText.length -1);
201 extraKeys[`${metaKey}-S`] = cm => {window.$events.emit('editor-save-draft')};
203 extraKeys[`${metaKey}-Enter`] = cm => {window.$events.emit('editor-save-page')};
204 // Show link selector
205 extraKeys[`Shift-${metaKey}-K`] = cm => {this.actionShowLinkSelector()};
207 extraKeys[`${metaKey}-K`] = cm => {insertLink()};
209 extraKeys[`${metaKey}-1`] = cm => {replaceLineStart('##');};
210 extraKeys[`${metaKey}-2`] = cm => {replaceLineStart('###');};
211 extraKeys[`${metaKey}-3`] = cm => {replaceLineStart('####');};
212 extraKeys[`${metaKey}-4`] = cm => {replaceLineStart('#####');};
213 extraKeys[`${metaKey}-5`] = cm => {replaceLineStart('');};
214 extraKeys[`${metaKey}-D`] = cm => {replaceLineStart('');};
215 extraKeys[`${metaKey}-6`] = cm => {replaceLineStart('>');};
216 extraKeys[`${metaKey}-Q`] = cm => {replaceLineStart('>');};
217 extraKeys[`${metaKey}-7`] = cm => {wrapSelection('\n```\n', '\n```');};
218 extraKeys[`${metaKey}-8`] = cm => {wrapSelection('`', '`');};
219 extraKeys[`Shift-${metaKey}-E`] = cm => {wrapSelection('`', '`');};
220 extraKeys[`${metaKey}-9`] = cm => {wrapSelection('<p class="callout info">', '</p>');};
221 extraKeys[`${metaKey}-P`] = cm => {replaceLineStart('-')}
222 extraKeys[`${metaKey}-O`] = cm => {replaceLineStartForOrderedList()}
223 cm.setOption('extraKeys', extraKeys);
225 // Update data on content change
226 cm.on('change', (instance, changeObj) => {
227 this.updateAndRender();
230 const onScrollDebounced = debounce((instance) => {
231 // Thanks to http://liuhao.im/english/2015/11/10/the-sync-scroll-of-markdown-editor-in-javascript.html
232 let scroll = instance.getScrollInfo();
233 let atEnd = scroll.top + scroll.clientHeight === scroll.height;
235 this.onMarkdownScroll(-1);
239 let lineNum = instance.lineAtHeight(scroll.top, 'local');
240 let range = instance.getRange({line: 0, ch: null}, {line: lineNum, ch: null});
241 let parser = new DOMParser();
242 let doc = parser.parseFromString(this.markdown.render(range), 'text/html');
243 let totalLines = doc.documentElement.querySelectorAll('body > *');
244 this.onMarkdownScroll(totalLines.length);
247 // Handle scroll to sync display view
248 cm.on('scroll', instance => {
249 onScrollDebounced(instance);
252 // Handle image paste
253 cm.on('paste', (cm, event) => {
254 const clipboard = new Clipboard(event.clipboardData || event.dataTransfer);
256 // Don't handle the event ourselves if no items exist of contains table-looking data
257 if (!clipboard.hasItems() || clipboard.containsTabularData()) {
261 const images = clipboard.getImages();
262 for (const image of images) {
267 // Handle image & content drag n drop
268 cm.on('drop', (cm, event) => {
270 const templateId = event.dataTransfer.getData('bookstack/template');
272 const cursorPos = cm.coordsChar({left: event.pageX, top: event.pageY});
273 cm.setCursor(cursorPos);
274 event.preventDefault();
275 window.$http.get(`/templates/${templateId}`).then(resp => {
276 const content = resp.data.markdown || resp.data.html;
277 cm.replaceSelection(content);
281 const clipboard = new Clipboard(event.dataTransfer);
282 if (clipboard.hasItems() && clipboard.getImages().length > 0) {
283 const cursorPos = cm.coordsChar({left: event.pageX, top: event.pageY});
284 cm.setCursor(cursorPos);
285 event.stopPropagation();
286 event.preventDefault();
287 const images = clipboard.getImages();
288 for (const image of images) {
295 // Helper to replace editor content
296 function replaceContent(search, replace) {
297 let text = cm.getValue();
298 let cursor = cm.listSelections();
299 cm.setValue(text.replace(search, replace));
300 cm.setSelections(cursor);
303 // Helper to replace the start of the line
304 function replaceLineStart(newStart) {
305 let cursor = cm.getCursor();
306 let lineContent = cm.getLine(cursor.line);
307 let lineLen = lineContent.length;
308 let lineStart = lineContent.split(' ')[0];
310 // Remove symbol if already set
311 if (lineStart === newStart) {
312 lineContent = lineContent.replace(`${newStart} `, '');
313 cm.replaceRange(lineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
314 cm.setCursor({line: cursor.line, ch: cursor.ch - (newStart.length + 1)});
318 let alreadySymbol = /^[#>`]/.test(lineStart);
321 posDif = newStart.length - lineStart.length;
322 lineContent = lineContent.replace(lineStart, newStart).trim();
323 } else if (newStart !== '') {
324 posDif = newStart.length + 1;
325 lineContent = newStart + ' ' + lineContent;
327 cm.replaceRange(lineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
328 cm.setCursor({line: cursor.line, ch: cursor.ch + posDif});
331 function wrapLine(start, end) {
332 let cursor = cm.getCursor();
333 let lineContent = cm.getLine(cursor.line);
334 let lineLen = lineContent.length;
335 let newLineContent = lineContent;
337 if (lineContent.indexOf(start) === 0 && lineContent.slice(-end.length) === end) {
338 newLineContent = lineContent.slice(start.length, lineContent.length - end.length);
340 newLineContent = `${start}${lineContent}${end}`;
343 cm.replaceRange(newLineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
344 cm.setCursor({line: cursor.line, ch: cursor.ch + start.length});
347 function wrapSelection(start, end) {
348 let selection = cm.getSelection();
349 if (selection === '') return wrapLine(start, end);
351 let newSelection = selection;
355 if (selection.indexOf(start) === 0 && selection.slice(-end.length) === end) {
356 newSelection = selection.slice(start.length, selection.length - end.length);
357 endDiff = -(end.length + start.length);
359 newSelection = `${start}${selection}${end}`;
360 endDiff = start.length + end.length;
363 let selections = cm.listSelections()[0];
364 cm.replaceSelection(newSelection);
365 let headFirst = selections.head.ch <= selections.anchor.ch;
366 selections.head.ch += headFirst ? frontDiff : endDiff;
367 selections.anchor.ch += headFirst ? endDiff : frontDiff;
368 cm.setSelections([selections]);
371 function replaceLineStartForOrderedList() {
372 const cursor = cm.getCursor();
373 const prevLineContent = cm.getLine(cursor.line - 1) || '';
374 const listMatch = prevLineContent.match(/^(\s*)(\d)([).])\s/) || [];
376 const number = (Number(listMatch[2]) || 0) + 1;
377 const whiteSpace = listMatch[1] || '';
378 const listMark = listMatch[3] || '.'
380 const prefix = `${whiteSpace}${number}${listMark}`;
381 return replaceLineStart(prefix);
384 // Handle image upload and add image into markdown content
385 function uploadImage(file) {
386 if (file === null || file.type.indexOf('image') !== 0) return;
390 let fileNameMatches = file.name.match(/\.(.+)$/);
391 if (fileNameMatches.length > 1) ext = fileNameMatches[1];
394 // Insert image into markdown
395 const id = "image-" + Math.random().toString(16).slice(2);
396 const placeholderImage = window.baseUrl(`/loading.gif#upload${id}`);
397 const selectedText = cm.getSelection();
398 const placeHolderText = ``;
399 const cursor = cm.getCursor();
400 cm.replaceSelection(placeHolderText);
401 cm.setCursor({line: cursor.line, ch: cursor.ch + selectedText.length + 3});
403 const remoteFilename = "image-" + Date.now() + "." + ext;
404 const formData = new FormData();
405 formData.append('file', file, remoteFilename);
406 formData.append('uploaded_to', context.pageId);
408 window.$http.post('/images/gallery', formData).then(resp => {
409 const newContent = `[](${resp.data.url})`;
410 replaceContent(placeHolderText, newContent);
412 window.$events.emit('error', context.imageUploadErrorText);
413 replaceContent(placeHolderText, selectedText);
418 function insertLink() {
419 let cursorPos = cm.getCursor('from');
420 let selectedText = cm.getSelection() || '';
421 let newText = `[${selectedText}]()`;
423 cm.replaceSelection(newText);
424 let cursorPosDiff = (selectedText === '') ? -3 : -1;
425 cm.setCursor(cursorPos.line, cursorPos.ch + newText.length+cursorPosDiff);
428 this.updateAndRender();
431 actionInsertImage() {
432 const cursorPos = this.cm.getCursor('from');
433 window.ImageManager.show(image => {
434 const imageUrl = image.thumbs.display || image.url;
435 let selectedText = this.cm.getSelection();
436 let newText = "[](" + image.url + ")";
438 this.cm.replaceSelection(newText);
439 this.cm.setCursor(cursorPos.line, cursorPos.ch + newText.length);
443 actionShowImageManager() {
444 const cursorPos = this.cm.getCursor('from');
445 window.ImageManager.show(image => {
446 this.insertDrawing(image, cursorPos);
450 // Show the popup link selector and insert a link when finished
451 actionShowLinkSelector() {
452 const cursorPos = this.cm.getCursor('from');
453 window.EntitySelectorPopup.show(entity => {
454 let selectedText = this.cm.getSelection() || entity.name;
455 let newText = `[${selectedText}](${entity.link})`;
457 this.cm.replaceSelection(newText);
458 this.cm.setCursor(cursorPos.line, cursorPos.ch + newText.length);
463 const drawioUrlElem = document.querySelector('[drawio-url]');
464 return drawioUrlElem ? drawioUrlElem.getAttribute('drawio-url') : false;
467 // Show draw.io if enabled and handle save.
468 actionStartDrawing() {
469 const url = this.getDrawioUrl();
472 const cursorPos = this.cm.getCursor('from');
474 DrawIO.show(url,() => {
475 return Promise.resolve('');
480 uploaded_to: Number(this.pageId),
483 window.$http.post("/images/drawio", data).then(resp => {
484 this.insertDrawing(resp.data, cursorPos);
487 this.handleDrawingUploadError(err);
492 insertDrawing(image, originalCursor) {
493 const newText = `<div drawio-diagram="${image.id}"><img src="${image.url}"></div>`;
495 this.cm.replaceSelection(newText);
496 this.cm.setCursor(originalCursor.line, originalCursor.ch + newText.length);
499 // Show draw.io if enabled and handle save.
500 actionEditDrawing(imgContainer) {
501 const drawioUrl = this.getDrawioUrl();
506 const cursorPos = this.cm.getCursor('from');
507 const drawingId = imgContainer.getAttribute('drawio-diagram');
509 DrawIO.show(drawioUrl, () => {
510 return DrawIO.load(drawingId);
515 uploaded_to: Number(this.pageId),
518 window.$http.post("/images/drawio", data).then(resp => {
519 let newText = `<div drawio-diagram="${resp.data.id}"><img src="${resp.data.url}"></div>`;
520 let newContent = this.cm.getValue().split('\n').map(line => {
521 if (line.indexOf(`drawio-diagram="${drawingId}"`) !== -1) {
526 this.cm.setValue(newContent);
527 this.cm.setCursor(cursorPos);
531 this.handleDrawingUploadError(err);
536 handleDrawingUploadError(error) {
537 if (error.status === 413) {
538 window.$events.emit('error', this.serverUploadLimitText);
540 window.$events.emit('error', this.imageUploadErrorText);
545 // Make the editor full screen
547 const alreadyFullscreen = this.elem.classList.contains('fullscreen');
548 this.elem.classList.toggle('fullscreen', !alreadyFullscreen);
549 document.body.classList.toggle('markdown-fullscreen', !alreadyFullscreen);
552 // Scroll to a specified text
553 scrollToText(searchText) {
558 const content = this.cm.getValue();
559 const lines = content.split(/\r?\n/);
560 let lineNumber = lines.findIndex(line => {
561 return line && line.indexOf(searchText) !== -1;
564 if (lineNumber === -1) {
568 this.cm.scrollIntoView({
572 // set the cursor location.
575 char: lines[lineNumber].length
579 listenForBookStackEditorEvents() {
581 function getContentToInsert({html, markdown}) {
582 return markdown || html;
585 // Replace editor content
586 window.$events.listen('editor::replace', (eventContent) => {
587 const markdown = getContentToInsert(eventContent);
588 this.cm.setValue(markdown);
591 // Append editor content
592 window.$events.listen('editor::append', (eventContent) => {
593 const cursorPos = this.cm.getCursor('from');
594 const markdown = getContentToInsert(eventContent);
595 const content = this.cm.getValue() + '\n' + markdown;
596 this.cm.setValue(content);
597 this.cm.setCursor(cursorPos.line, cursorPos.ch);
600 // Prepend editor content
601 window.$events.listen('editor::prepend', (eventContent) => {
602 const cursorPos = this.cm.getCursor('from');
603 const markdown = getContentToInsert(eventContent);
604 const content = markdown + '\n' + this.cm.getValue();
605 this.cm.setValue(content);
606 const prependLineCount = markdown.split('\n').length;
607 this.cm.setCursor(cursorPos.line + prependLineCount, cursorPos.ch);
610 // Insert editor content at the current location
611 window.$events.listen('editor::insert', (eventContent) => {
612 const markdown = getContentToInsert(eventContent);
613 this.cm.replaceSelection(markdown);
617 window.$events.listen('editor::focus', () => {
623 export default MarkdownEditor ;