2018-11-09 22:17:35 +01:00
|
|
|
import MarkdownIt from "markdown-it";
|
|
|
|
import mdTasksLists from 'markdown-it-task-lists';
|
2019-12-21 16:48:03 +01:00
|
|
|
import Clipboard from "../services/clipboard";
|
2019-06-07 18:46:19 +02:00
|
|
|
import {debounce} from "../services/util";
|
2022-06-07 17:07:28 +02:00
|
|
|
import {patchDomFromHtmlString} from "../services/vdom";
|
2018-11-09 22:17:35 +01:00
|
|
|
import DrawIO from "../services/drawio";
|
2018-01-20 21:40:21 +01:00
|
|
|
|
2017-09-23 13:24:06 +02:00
|
|
|
class MarkdownEditor {
|
|
|
|
|
2020-07-05 22:18:17 +02:00
|
|
|
setup() {
|
|
|
|
this.elem = this.$el;
|
2019-03-08 22:32:31 +01:00
|
|
|
|
2020-07-05 22:18:17 +02:00
|
|
|
this.pageId = this.$opts.pageId;
|
|
|
|
this.textDirection = this.$opts.textDirection;
|
2020-11-21 18:52:49 +01:00
|
|
|
this.imageUploadErrorText = this.$opts.imageUploadErrorText;
|
2021-05-26 19:23:27 +02:00
|
|
|
this.serverUploadLimitText = this.$opts.serverUploadLimitText;
|
2019-03-08 22:32:31 +01:00
|
|
|
|
2017-09-23 13:24:06 +02:00
|
|
|
this.markdown = new MarkdownIt({html: true});
|
|
|
|
this.markdown.use(mdTasksLists, {label: true});
|
|
|
|
|
|
|
|
this.display = this.elem.querySelector('.markdown-display');
|
2019-09-01 11:51:52 +02:00
|
|
|
|
2019-08-26 13:16:50 +02:00
|
|
|
this.displayStylesLoaded = false;
|
2017-09-23 13:24:06 +02:00
|
|
|
this.input = this.elem.querySelector('textarea');
|
2022-02-08 12:10:01 +01:00
|
|
|
|
|
|
|
this.cm = null;
|
|
|
|
this.Code = null;
|
|
|
|
const cmLoadPromise = window.importVersioned('code').then(Code => {
|
|
|
|
this.cm = Code.markdownEditor(this.input);
|
|
|
|
this.Code = Code;
|
|
|
|
return this.cm;
|
|
|
|
});
|
2017-09-23 13:24:06 +02:00
|
|
|
|
|
|
|
this.onMarkdownScroll = this.onMarkdownScroll.bind(this);
|
2018-06-10 09:41:10 +02:00
|
|
|
|
2020-07-05 22:18:17 +02:00
|
|
|
const displayLoad = () => {
|
2019-09-01 11:51:52 +02:00
|
|
|
this.displayDoc = this.display.contentDocument;
|
2022-02-08 12:10:01 +01:00
|
|
|
this.init(cmLoadPromise);
|
2020-07-05 22:18:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (this.display.contentDocument.readyState === 'complete') {
|
|
|
|
displayLoad();
|
|
|
|
} else {
|
|
|
|
this.display.addEventListener('load', displayLoad.bind(this));
|
|
|
|
}
|
2019-10-16 19:01:35 +02:00
|
|
|
|
2020-07-05 22:18:17 +02:00
|
|
|
window.$events.emitPublic(this.elem, 'editor-markdown::setup', {
|
2019-10-16 19:01:35 +02:00
|
|
|
markdownIt: this.markdown,
|
|
|
|
displayEl: this.display,
|
|
|
|
codeMirrorInstance: this.cm,
|
|
|
|
});
|
2017-09-23 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2022-02-08 12:10:01 +01:00
|
|
|
init(cmLoadPromise) {
|
2017-09-23 13:24:06 +02:00
|
|
|
|
2018-01-20 21:40:21 +01:00
|
|
|
let lastClick = 0;
|
|
|
|
|
2017-09-23 13:24:06 +02:00
|
|
|
// Prevent markdown display link click redirect
|
2019-08-26 13:16:50 +02:00
|
|
|
this.displayDoc.addEventListener('click', event => {
|
2018-01-20 21:40:21 +01:00
|
|
|
let isDblClick = Date.now() - lastClick < 300;
|
|
|
|
|
2017-09-23 13:24:06 +02:00
|
|
|
let link = event.target.closest('a');
|
2018-01-20 21:40:21 +01:00
|
|
|
if (link !== null) {
|
|
|
|
event.preventDefault();
|
|
|
|
window.open(link.getAttribute('href'));
|
|
|
|
return;
|
|
|
|
}
|
2017-09-23 13:24:06 +02:00
|
|
|
|
2018-01-20 21:40:21 +01:00
|
|
|
let drawing = event.target.closest('[drawio-diagram]');
|
|
|
|
if (drawing !== null && isDblClick) {
|
|
|
|
this.actionEditDrawing(drawing);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastClick = Date.now();
|
2017-09-23 13:24:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Button actions
|
|
|
|
this.elem.addEventListener('click', event => {
|
|
|
|
let button = event.target.closest('button[data-action]');
|
|
|
|
if (button === null) return;
|
|
|
|
|
|
|
|
let action = button.getAttribute('data-action');
|
|
|
|
if (action === 'insertImage') this.actionInsertImage();
|
|
|
|
if (action === 'insertLink') this.actionShowLinkSelector();
|
2019-04-20 14:12:35 +02:00
|
|
|
if (action === 'insertDrawing' && (event.ctrlKey || event.metaKey)) {
|
2018-05-27 15:33:50 +02:00
|
|
|
this.actionShowImageManager();
|
|
|
|
return;
|
|
|
|
}
|
2018-01-20 21:40:21 +01:00
|
|
|
if (action === 'insertDrawing') this.actionStartDrawing();
|
2019-12-22 15:21:18 +01:00
|
|
|
if (action === 'fullscreen') this.actionFullScreen();
|
2017-09-23 13:24:06 +02:00
|
|
|
});
|
|
|
|
|
2019-04-14 13:04:20 +02:00
|
|
|
// Mobile section toggling
|
|
|
|
this.elem.addEventListener('click', event => {
|
|
|
|
const toolbarLabel = event.target.closest('.editor-toolbar-label');
|
|
|
|
if (!toolbarLabel) return;
|
|
|
|
|
|
|
|
const currentActiveSections = this.elem.querySelectorAll('.markdown-editor-wrap');
|
|
|
|
for (let activeElem of currentActiveSections) {
|
|
|
|
activeElem.classList.remove('active');
|
|
|
|
}
|
|
|
|
|
|
|
|
toolbarLabel.closest('.markdown-editor-wrap').classList.add('active');
|
|
|
|
});
|
|
|
|
|
2022-02-08 12:10:01 +01:00
|
|
|
cmLoadPromise.then(cm => {
|
|
|
|
this.codeMirrorSetup(cm);
|
|
|
|
|
|
|
|
// Refresh CodeMirror on container resize
|
|
|
|
const resizeDebounced = debounce(() => this.Code.updateLayout(cm), 100, false);
|
|
|
|
const observer = new ResizeObserver(resizeDebounced);
|
|
|
|
observer.observe(this.elem);
|
|
|
|
});
|
|
|
|
|
2019-08-11 21:04:43 +02:00
|
|
|
this.listenForBookStackEditorEvents();
|
2019-09-01 11:51:52 +02:00
|
|
|
|
|
|
|
// Scroll to text if needed.
|
|
|
|
const queryParams = (new URL(window.location)).searchParams;
|
|
|
|
const scrollText = queryParams.get('content-text');
|
|
|
|
if (scrollText) {
|
|
|
|
this.scrollToText(scrollText);
|
|
|
|
}
|
2017-09-23 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the input content and render the display.
|
|
|
|
updateAndRender() {
|
2019-08-26 13:16:50 +02:00
|
|
|
const content = this.cm.getValue();
|
2017-09-23 13:24:06 +02:00
|
|
|
this.input.value = content;
|
2022-06-07 17:07:28 +02:00
|
|
|
|
2019-08-26 13:16:50 +02:00
|
|
|
const html = this.markdown.render(content);
|
2017-09-23 13:24:06 +02:00
|
|
|
window.$events.emit('editor-html-change', html);
|
|
|
|
window.$events.emit('editor-markdown-change', content);
|
2019-08-26 13:16:50 +02:00
|
|
|
|
|
|
|
// Set body content
|
2022-06-07 17:07:28 +02:00
|
|
|
const target = this.getDisplayTarget();
|
2019-08-26 13:16:50 +02:00
|
|
|
this.displayDoc.body.className = 'page-content';
|
2022-06-07 17:07:28 +02:00
|
|
|
patchDomFromHtmlString(target, html);
|
2019-08-26 13:16:50 +02:00
|
|
|
|
|
|
|
// Copy styles from page head and set custom styles for editor
|
|
|
|
this.loadStylesIntoDisplay();
|
|
|
|
}
|
|
|
|
|
2022-06-07 17:07:28 +02:00
|
|
|
getDisplayTarget() {
|
|
|
|
const body = this.displayDoc.body;
|
|
|
|
|
|
|
|
if (body.children.length === 0) {
|
|
|
|
const wrap = document.createElement('div');
|
|
|
|
this.displayDoc.body.append(wrap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return body.children[0];
|
|
|
|
}
|
|
|
|
|
2019-08-26 13:16:50 +02:00
|
|
|
loadStylesIntoDisplay() {
|
|
|
|
if (this.displayStylesLoaded) return;
|
2020-04-11 16:48:08 +02:00
|
|
|
this.displayDoc.documentElement.classList.add('markdown-editor-display');
|
|
|
|
// Set display to be dark mode if parent is
|
|
|
|
|
|
|
|
if (document.documentElement.classList.contains('dark-mode')) {
|
|
|
|
this.displayDoc.documentElement.style.backgroundColor = '#222';
|
|
|
|
this.displayDoc.documentElement.classList.add('dark-mode');
|
|
|
|
}
|
2019-08-26 13:16:50 +02:00
|
|
|
|
|
|
|
this.displayDoc.head.innerHTML = '';
|
|
|
|
const styles = document.head.querySelectorAll('style,link[rel=stylesheet]');
|
|
|
|
for (let style of styles) {
|
|
|
|
const copy = style.cloneNode(true);
|
|
|
|
this.displayDoc.head.appendChild(copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.displayStylesLoaded = true;
|
2017-09-23 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onMarkdownScroll(lineCount) {
|
2019-08-26 13:16:50 +02:00
|
|
|
const elems = this.displayDoc.body.children;
|
2017-09-23 13:24:06 +02:00
|
|
|
if (elems.length <= lineCount) return;
|
|
|
|
|
2019-06-07 18:46:19 +02:00
|
|
|
const topElem = (lineCount === -1) ? elems[elems.length-1] : elems[lineCount];
|
|
|
|
topElem.scrollIntoView({ block: 'start', inline: 'nearest', behavior: 'smooth'});
|
2017-09-23 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2022-02-08 12:10:01 +01:00
|
|
|
codeMirrorSetup(cm) {
|
2019-03-08 22:32:31 +01:00
|
|
|
const context = this;
|
|
|
|
|
2018-09-22 14:18:26 +02:00
|
|
|
// Text direction
|
|
|
|
// cm.setOption('direction', this.textDirection);
|
|
|
|
cm.setOption('direction', 'ltr'); // Will force to remain as ltr for now due to issues when HTML is in editor.
|
2017-09-23 13:24:06 +02:00
|
|
|
// Custom key commands
|
2022-02-08 12:10:01 +01:00
|
|
|
let metaKey = this.Code.getMetaKey();
|
2017-09-23 13:24:06 +02:00
|
|
|
const extraKeys = {};
|
|
|
|
// Insert Image shortcut
|
|
|
|
extraKeys[`${metaKey}-Alt-I`] = function(cm) {
|
|
|
|
let selectedText = cm.getSelection();
|
|
|
|
let newText = `![${selectedText}](http://)`;
|
|
|
|
let cursorPos = cm.getCursor('from');
|
|
|
|
cm.replaceSelection(newText);
|
|
|
|
cm.setCursor(cursorPos.line, cursorPos.ch + newText.length -1);
|
|
|
|
};
|
|
|
|
// Save draft
|
|
|
|
extraKeys[`${metaKey}-S`] = cm => {window.$events.emit('editor-save-draft')};
|
2017-12-07 19:44:20 +01:00
|
|
|
// Save page
|
|
|
|
extraKeys[`${metaKey}-Enter`] = cm => {window.$events.emit('editor-save-page')};
|
2017-09-23 13:24:06 +02:00
|
|
|
// Show link selector
|
|
|
|
extraKeys[`Shift-${metaKey}-K`] = cm => {this.actionShowLinkSelector()};
|
|
|
|
// Insert Link
|
|
|
|
extraKeys[`${metaKey}-K`] = cm => {insertLink()};
|
|
|
|
// FormatShortcuts
|
|
|
|
extraKeys[`${metaKey}-1`] = cm => {replaceLineStart('##');};
|
|
|
|
extraKeys[`${metaKey}-2`] = cm => {replaceLineStart('###');};
|
|
|
|
extraKeys[`${metaKey}-3`] = cm => {replaceLineStart('####');};
|
|
|
|
extraKeys[`${metaKey}-4`] = cm => {replaceLineStart('#####');};
|
|
|
|
extraKeys[`${metaKey}-5`] = cm => {replaceLineStart('');};
|
|
|
|
extraKeys[`${metaKey}-d`] = cm => {replaceLineStart('');};
|
|
|
|
extraKeys[`${metaKey}-6`] = cm => {replaceLineStart('>');};
|
|
|
|
extraKeys[`${metaKey}-q`] = cm => {replaceLineStart('>');};
|
|
|
|
extraKeys[`${metaKey}-7`] = cm => {wrapSelection('\n```\n', '\n```');};
|
|
|
|
extraKeys[`${metaKey}-8`] = cm => {wrapSelection('`', '`');};
|
|
|
|
extraKeys[`Shift-${metaKey}-E`] = cm => {wrapSelection('`', '`');};
|
|
|
|
extraKeys[`${metaKey}-9`] = cm => {wrapSelection('<p class="callout info">', '</p>');};
|
|
|
|
cm.setOption('extraKeys', extraKeys);
|
|
|
|
|
|
|
|
// Update data on content change
|
|
|
|
cm.on('change', (instance, changeObj) => {
|
|
|
|
this.updateAndRender();
|
|
|
|
});
|
|
|
|
|
2019-06-07 18:46:19 +02:00
|
|
|
const onScrollDebounced = debounce((instance) => {
|
2017-09-23 13:24:06 +02:00
|
|
|
// Thanks to http://liuhao.im/english/2015/11/10/the-sync-scroll-of-markdown-editor-in-javascript.html
|
|
|
|
let scroll = instance.getScrollInfo();
|
|
|
|
let atEnd = scroll.top + scroll.clientHeight === scroll.height;
|
|
|
|
if (atEnd) {
|
|
|
|
this.onMarkdownScroll(-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let lineNum = instance.lineAtHeight(scroll.top, 'local');
|
|
|
|
let range = instance.getRange({line: 0, ch: null}, {line: lineNum, ch: null});
|
|
|
|
let parser = new DOMParser();
|
|
|
|
let doc = parser.parseFromString(this.markdown.render(range), 'text/html');
|
|
|
|
let totalLines = doc.documentElement.querySelectorAll('body > *');
|
|
|
|
this.onMarkdownScroll(totalLines.length);
|
2019-06-07 18:46:19 +02:00
|
|
|
}, 100);
|
|
|
|
|
|
|
|
// Handle scroll to sync display view
|
|
|
|
cm.on('scroll', instance => {
|
|
|
|
onScrollDebounced(instance);
|
2017-09-23 13:24:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Handle image paste
|
|
|
|
cm.on('paste', (cm, event) => {
|
2019-12-21 16:48:03 +01:00
|
|
|
const clipboard = new Clipboard(event.clipboardData || event.dataTransfer);
|
2019-05-15 21:23:09 +02:00
|
|
|
|
2019-12-21 16:48:03 +01:00
|
|
|
// Don't handle the event ourselves if no items exist of contains table-looking data
|
|
|
|
if (!clipboard.hasItems() || clipboard.containsTabularData()) {
|
|
|
|
return;
|
2019-05-15 21:23:09 +02:00
|
|
|
}
|
|
|
|
|
2019-12-21 16:48:03 +01:00
|
|
|
const images = clipboard.getImages();
|
|
|
|
for (const image of images) {
|
|
|
|
uploadImage(image);
|
2017-09-23 13:24:06 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-26 16:34:03 +02:00
|
|
|
// Handle image & content drag n drop
|
2017-09-23 13:24:06 +02:00
|
|
|
cm.on('drop', (cm, event) => {
|
2019-08-26 16:34:03 +02:00
|
|
|
|
|
|
|
const templateId = event.dataTransfer.getData('bookstack/template');
|
|
|
|
if (templateId) {
|
|
|
|
const cursorPos = cm.coordsChar({left: event.pageX, top: event.pageY});
|
|
|
|
cm.setCursor(cursorPos);
|
|
|
|
event.preventDefault();
|
|
|
|
window.$http.get(`/templates/${templateId}`).then(resp => {
|
|
|
|
const content = resp.data.markdown || resp.data.html;
|
|
|
|
cm.replaceSelection(content);
|
|
|
|
});
|
2017-09-23 13:24:06 +02:00
|
|
|
}
|
2019-08-26 16:34:03 +02:00
|
|
|
|
2019-12-21 16:48:03 +01:00
|
|
|
const clipboard = new Clipboard(event.dataTransfer);
|
2020-09-13 19:29:48 +02:00
|
|
|
if (clipboard.hasItems() && clipboard.getImages().length > 0) {
|
2019-08-26 16:34:03 +02:00
|
|
|
const cursorPos = cm.coordsChar({left: event.pageX, top: event.pageY});
|
|
|
|
cm.setCursor(cursorPos);
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
2019-12-21 16:48:03 +01:00
|
|
|
const images = clipboard.getImages();
|
|
|
|
for (const image of images) {
|
|
|
|
uploadImage(image);
|
2019-08-26 16:34:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 13:24:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Helper to replace editor content
|
|
|
|
function replaceContent(search, replace) {
|
|
|
|
let text = cm.getValue();
|
|
|
|
let cursor = cm.listSelections();
|
|
|
|
cm.setValue(text.replace(search, replace));
|
|
|
|
cm.setSelections(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to replace the start of the line
|
|
|
|
function replaceLineStart(newStart) {
|
|
|
|
let cursor = cm.getCursor();
|
|
|
|
let lineContent = cm.getLine(cursor.line);
|
|
|
|
let lineLen = lineContent.length;
|
|
|
|
let lineStart = lineContent.split(' ')[0];
|
|
|
|
|
|
|
|
// Remove symbol if already set
|
|
|
|
if (lineStart === newStart) {
|
|
|
|
lineContent = lineContent.replace(`${newStart} `, '');
|
|
|
|
cm.replaceRange(lineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
|
|
|
|
cm.setCursor({line: cursor.line, ch: cursor.ch - (newStart.length + 1)});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let alreadySymbol = /^[#>`]/.test(lineStart);
|
|
|
|
let posDif = 0;
|
|
|
|
if (alreadySymbol) {
|
|
|
|
posDif = newStart.length - lineStart.length;
|
|
|
|
lineContent = lineContent.replace(lineStart, newStart).trim();
|
|
|
|
} else if (newStart !== '') {
|
|
|
|
posDif = newStart.length + 1;
|
|
|
|
lineContent = newStart + ' ' + lineContent;
|
|
|
|
}
|
|
|
|
cm.replaceRange(lineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
|
|
|
|
cm.setCursor({line: cursor.line, ch: cursor.ch + posDif});
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapLine(start, end) {
|
|
|
|
let cursor = cm.getCursor();
|
|
|
|
let lineContent = cm.getLine(cursor.line);
|
|
|
|
let lineLen = lineContent.length;
|
|
|
|
let newLineContent = lineContent;
|
|
|
|
|
|
|
|
if (lineContent.indexOf(start) === 0 && lineContent.slice(-end.length) === end) {
|
|
|
|
newLineContent = lineContent.slice(start.length, lineContent.length - end.length);
|
|
|
|
} else {
|
|
|
|
newLineContent = `${start}${lineContent}${end}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.replaceRange(newLineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
|
|
|
|
cm.setCursor({line: cursor.line, ch: cursor.ch + start.length});
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapSelection(start, end) {
|
|
|
|
let selection = cm.getSelection();
|
|
|
|
if (selection === '') return wrapLine(start, end);
|
|
|
|
|
|
|
|
let newSelection = selection;
|
|
|
|
let frontDiff = 0;
|
|
|
|
let endDiff = 0;
|
|
|
|
|
|
|
|
if (selection.indexOf(start) === 0 && selection.slice(-end.length) === end) {
|
|
|
|
newSelection = selection.slice(start.length, selection.length - end.length);
|
|
|
|
endDiff = -(end.length + start.length);
|
|
|
|
} else {
|
|
|
|
newSelection = `${start}${selection}${end}`;
|
|
|
|
endDiff = start.length + end.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
let selections = cm.listSelections()[0];
|
|
|
|
cm.replaceSelection(newSelection);
|
|
|
|
let headFirst = selections.head.ch <= selections.anchor.ch;
|
|
|
|
selections.head.ch += headFirst ? frontDiff : endDiff;
|
|
|
|
selections.anchor.ch += headFirst ? endDiff : frontDiff;
|
|
|
|
cm.setSelections([selections]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle image upload and add image into markdown content
|
|
|
|
function uploadImage(file) {
|
|
|
|
if (file === null || file.type.indexOf('image') !== 0) return;
|
|
|
|
let ext = 'png';
|
|
|
|
|
|
|
|
if (file.name) {
|
|
|
|
let fileNameMatches = file.name.match(/\.(.+)$/);
|
|
|
|
if (fileNameMatches.length > 1) ext = fileNameMatches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert image into markdown
|
2019-03-08 22:32:31 +01:00
|
|
|
const id = "image-" + Math.random().toString(16).slice(2);
|
|
|
|
const placeholderImage = window.baseUrl(`/loading.gif#upload${id}`);
|
|
|
|
const selectedText = cm.getSelection();
|
|
|
|
const placeHolderText = `![${selectedText}](${placeholderImage})`;
|
|
|
|
const cursor = cm.getCursor();
|
2017-09-23 13:24:06 +02:00
|
|
|
cm.replaceSelection(placeHolderText);
|
2018-11-10 16:35:13 +01:00
|
|
|
cm.setCursor({line: cursor.line, ch: cursor.ch + selectedText.length + 3});
|
2017-09-23 13:24:06 +02:00
|
|
|
|
2019-03-08 22:32:31 +01:00
|
|
|
const remoteFilename = "image-" + Date.now() + "." + ext;
|
|
|
|
const formData = new FormData();
|
2017-09-23 13:24:06 +02:00
|
|
|
formData.append('file', file, remoteFilename);
|
2019-03-08 22:32:31 +01:00
|
|
|
formData.append('uploaded_to', context.pageId);
|
2017-09-23 13:24:06 +02:00
|
|
|
|
2019-05-07 23:23:44 +02:00
|
|
|
window.$http.post('/images/gallery', formData).then(resp => {
|
2018-11-10 16:35:13 +01:00
|
|
|
const newContent = `[![${selectedText}](${resp.data.thumbs.display})](${resp.data.url})`;
|
|
|
|
replaceContent(placeHolderText, newContent);
|
2017-09-23 13:24:06 +02:00
|
|
|
}).catch(err => {
|
2020-11-21 18:52:49 +01:00
|
|
|
window.$events.emit('error', context.imageUploadErrorText);
|
2017-09-23 13:24:06 +02:00
|
|
|
replaceContent(placeHolderText, selectedText);
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertLink() {
|
|
|
|
let cursorPos = cm.getCursor('from');
|
|
|
|
let selectedText = cm.getSelection() || '';
|
|
|
|
let newText = `[${selectedText}]()`;
|
|
|
|
cm.focus();
|
|
|
|
cm.replaceSelection(newText);
|
|
|
|
let cursorPosDiff = (selectedText === '') ? -3 : -1;
|
|
|
|
cm.setCursor(cursorPos.line, cursorPos.ch + newText.length+cursorPosDiff);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateAndRender();
|
|
|
|
}
|
|
|
|
|
|
|
|
actionInsertImage() {
|
2019-03-08 22:32:31 +01:00
|
|
|
const cursorPos = this.cm.getCursor('from');
|
2017-09-23 13:24:06 +02:00
|
|
|
window.ImageManager.show(image => {
|
2022-01-10 19:12:11 +01:00
|
|
|
const imageUrl = image.thumbs.display || image.url;
|
2017-09-23 13:24:06 +02:00
|
|
|
let selectedText = this.cm.getSelection();
|
2022-01-10 19:12:11 +01:00
|
|
|
let newText = "[![" + (selectedText || image.name) + "](" + imageUrl + ")](" + image.url + ")";
|
2017-09-23 13:24:06 +02:00
|
|
|
this.cm.focus();
|
|
|
|
this.cm.replaceSelection(newText);
|
|
|
|
this.cm.setCursor(cursorPos.line, cursorPos.ch + newText.length);
|
2018-05-27 15:33:50 +02:00
|
|
|
}, 'gallery');
|
|
|
|
}
|
|
|
|
|
|
|
|
actionShowImageManager() {
|
2019-03-08 22:32:31 +01:00
|
|
|
const cursorPos = this.cm.getCursor('from');
|
2018-05-27 15:33:50 +02:00
|
|
|
window.ImageManager.show(image => {
|
|
|
|
this.insertDrawing(image, cursorPos);
|
|
|
|
}, 'drawio');
|
2017-09-23 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show the popup link selector and insert a link when finished
|
|
|
|
actionShowLinkSelector() {
|
2019-03-08 22:32:31 +01:00
|
|
|
const cursorPos = this.cm.getCursor('from');
|
2017-09-23 13:24:06 +02:00
|
|
|
window.EntitySelectorPopup.show(entity => {
|
|
|
|
let selectedText = this.cm.getSelection() || entity.name;
|
|
|
|
let newText = `[${selectedText}](${entity.link})`;
|
|
|
|
this.cm.focus();
|
|
|
|
this.cm.replaceSelection(newText);
|
|
|
|
this.cm.setCursor(cursorPos.line, cursorPos.ch + newText.length);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-05 18:27:16 +02:00
|
|
|
getDrawioUrl() {
|
|
|
|
const drawioUrlElem = document.querySelector('[drawio-url]');
|
|
|
|
return drawioUrlElem ? drawioUrlElem.getAttribute('drawio-url') : false;
|
|
|
|
}
|
|
|
|
|
2018-01-20 21:40:21 +01:00
|
|
|
// Show draw.io if enabled and handle save.
|
|
|
|
actionStartDrawing() {
|
2020-04-05 18:27:16 +02:00
|
|
|
const url = this.getDrawioUrl();
|
|
|
|
if (!url) return;
|
|
|
|
|
|
|
|
const cursorPos = this.cm.getCursor('from');
|
2018-01-20 21:40:21 +01:00
|
|
|
|
2020-04-05 18:27:16 +02:00
|
|
|
DrawIO.show(url,() => {
|
2018-01-20 21:40:21 +01:00
|
|
|
return Promise.resolve('');
|
|
|
|
}, (pngData) => {
|
2020-04-05 18:27:16 +02:00
|
|
|
|
|
|
|
const data = {
|
2018-01-20 21:40:21 +01:00
|
|
|
image: pngData,
|
2020-10-13 23:36:07 +02:00
|
|
|
uploaded_to: Number(this.pageId),
|
2018-01-20 21:40:21 +01:00
|
|
|
};
|
|
|
|
|
2020-10-13 23:36:07 +02:00
|
|
|
window.$http.post("/images/drawio", data).then(resp => {
|
2018-05-27 15:33:50 +02:00
|
|
|
this.insertDrawing(resp.data, cursorPos);
|
2018-01-20 21:40:21 +01:00
|
|
|
DrawIO.close();
|
|
|
|
}).catch(err => {
|
2021-05-26 19:23:27 +02:00
|
|
|
this.handleDrawingUploadError(err);
|
2018-01-20 21:40:21 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-27 15:33:50 +02:00
|
|
|
insertDrawing(image, originalCursor) {
|
2019-03-08 22:32:31 +01:00
|
|
|
const newText = `<div drawio-diagram="${image.id}"><img src="${image.url}"></div>`;
|
2018-05-27 15:33:50 +02:00
|
|
|
this.cm.focus();
|
|
|
|
this.cm.replaceSelection(newText);
|
|
|
|
this.cm.setCursor(originalCursor.line, originalCursor.ch + newText.length);
|
|
|
|
}
|
|
|
|
|
2018-01-20 21:40:21 +01:00
|
|
|
// Show draw.io if enabled and handle save.
|
|
|
|
actionEditDrawing(imgContainer) {
|
2020-04-05 18:27:16 +02:00
|
|
|
const drawioUrl = this.getDrawioUrl();
|
|
|
|
if (!drawioUrl) {
|
2019-03-08 22:32:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cursorPos = this.cm.getCursor('from');
|
|
|
|
const drawingId = imgContainer.getAttribute('drawio-diagram');
|
2018-01-20 21:40:21 +01:00
|
|
|
|
2020-04-05 18:27:16 +02:00
|
|
|
DrawIO.show(drawioUrl, () => {
|
2019-05-04 16:48:15 +02:00
|
|
|
return DrawIO.load(drawingId);
|
2018-01-20 21:40:21 +01:00
|
|
|
}, (pngData) => {
|
|
|
|
|
|
|
|
let data = {
|
|
|
|
image: pngData,
|
2020-10-13 23:36:07 +02:00
|
|
|
uploaded_to: Number(this.pageId),
|
2018-01-20 21:40:21 +01:00
|
|
|
};
|
|
|
|
|
2020-10-13 23:36:07 +02:00
|
|
|
window.$http.post("/images/drawio", data).then(resp => {
|
2018-05-27 15:33:50 +02:00
|
|
|
let newText = `<div drawio-diagram="${resp.data.id}"><img src="${resp.data.url}"></div>`;
|
2018-01-20 21:40:21 +01:00
|
|
|
let newContent = this.cm.getValue().split('\n').map(line => {
|
|
|
|
if (line.indexOf(`drawio-diagram="${drawingId}"`) !== -1) {
|
|
|
|
return newText;
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}).join('\n');
|
|
|
|
this.cm.setValue(newContent);
|
|
|
|
this.cm.setCursor(cursorPos);
|
|
|
|
this.cm.focus();
|
|
|
|
DrawIO.close();
|
|
|
|
}).catch(err => {
|
2021-05-26 19:23:27 +02:00
|
|
|
this.handleDrawingUploadError(err);
|
2018-01-20 21:40:21 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-26 19:23:27 +02:00
|
|
|
handleDrawingUploadError(error) {
|
|
|
|
if (error.status === 413) {
|
|
|
|
window.$events.emit('error', this.serverUploadLimitText);
|
|
|
|
} else {
|
|
|
|
window.$events.emit('error', this.imageUploadErrorText);
|
|
|
|
}
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
|
2019-12-22 15:21:18 +01:00
|
|
|
// Make the editor full screen
|
|
|
|
actionFullScreen() {
|
|
|
|
const alreadyFullscreen = this.elem.classList.contains('fullscreen');
|
|
|
|
this.elem.classList.toggle('fullscreen', !alreadyFullscreen);
|
|
|
|
document.body.classList.toggle('markdown-fullscreen', !alreadyFullscreen);
|
|
|
|
}
|
|
|
|
|
2018-06-10 09:41:10 +02:00
|
|
|
// Scroll to a specified text
|
2018-07-14 11:20:49 +02:00
|
|
|
scrollToText(searchText) {
|
2018-06-10 09:41:10 +02:00
|
|
|
if (!searchText) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-14 11:20:49 +02:00
|
|
|
|
2018-06-10 09:41:10 +02:00
|
|
|
const content = this.cm.getValue();
|
|
|
|
const lines = content.split(/\r?\n/);
|
2018-07-14 11:20:49 +02:00
|
|
|
let lineNumber = lines.findIndex(line => {
|
|
|
|
return line && line.indexOf(searchText) !== -1;
|
|
|
|
});
|
2018-06-10 09:41:10 +02:00
|
|
|
|
2018-07-14 11:20:49 +02:00
|
|
|
if (lineNumber === -1) {
|
|
|
|
return;
|
2018-06-10 09:41:10 +02:00
|
|
|
}
|
2018-07-14 11:20:49 +02:00
|
|
|
|
|
|
|
this.cm.scrollIntoView({
|
|
|
|
line: lineNumber,
|
|
|
|
}, 200);
|
|
|
|
this.cm.focus();
|
|
|
|
// set the cursor location.
|
|
|
|
this.cm.setCursor({
|
|
|
|
line: lineNumber,
|
|
|
|
char: lines[lineNumber].length
|
|
|
|
})
|
2018-06-10 09:41:10 +02:00
|
|
|
}
|
|
|
|
|
2019-08-11 21:04:43 +02:00
|
|
|
listenForBookStackEditorEvents() {
|
|
|
|
|
|
|
|
function getContentToInsert({html, markdown}) {
|
|
|
|
return markdown || html;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace editor content
|
|
|
|
window.$events.listen('editor::replace', (eventContent) => {
|
|
|
|
const markdown = getContentToInsert(eventContent);
|
|
|
|
this.cm.setValue(markdown);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Append editor content
|
|
|
|
window.$events.listen('editor::append', (eventContent) => {
|
|
|
|
const cursorPos = this.cm.getCursor('from');
|
|
|
|
const markdown = getContentToInsert(eventContent);
|
|
|
|
const content = this.cm.getValue() + '\n' + markdown;
|
|
|
|
this.cm.setValue(content);
|
|
|
|
this.cm.setCursor(cursorPos.line, cursorPos.ch);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Prepend editor content
|
|
|
|
window.$events.listen('editor::prepend', (eventContent) => {
|
|
|
|
const cursorPos = this.cm.getCursor('from');
|
|
|
|
const markdown = getContentToInsert(eventContent);
|
|
|
|
const content = markdown + '\n' + this.cm.getValue();
|
|
|
|
this.cm.setValue(content);
|
|
|
|
const prependLineCount = markdown.split('\n').length;
|
|
|
|
this.cm.setCursor(cursorPos.line + prependLineCount, cursorPos.ch);
|
|
|
|
});
|
2020-04-27 16:54:39 +02:00
|
|
|
|
2020-09-13 19:58:05 +02:00
|
|
|
// Insert editor content at the current location
|
|
|
|
window.$events.listen('editor::insert', (eventContent) => {
|
|
|
|
const markdown = getContentToInsert(eventContent);
|
|
|
|
this.cm.replaceSelection(markdown);
|
|
|
|
});
|
|
|
|
|
2020-04-27 16:54:39 +02:00
|
|
|
// Focus on editor
|
|
|
|
window.$events.listen('editor::focus', () => {
|
|
|
|
this.cm.focus();
|
|
|
|
});
|
2019-08-11 21:04:43 +02:00
|
|
|
}
|
2017-09-23 13:24:06 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 16:23:29 +01:00
|
|
|
export default MarkdownEditor ;
|