1 import DrawIO from "../services/drawio";
5 * @param {MarkdownEditor} editor
16 const content = this.editor.cm.getValue();
17 this.editor.config.inputEl.value = content;
19 const html = this.editor.markdown.render(content);
20 window.$events.emit('editor-html-change', '');
21 window.$events.emit('editor-markdown-change', '');
22 this.lastContent.html = html;
23 this.lastContent.markdown = content;
24 this.editor.display.patchWithHtml(html);
28 return this.lastContent;
32 const cursorPos = this.editor.cm.getCursor('from');
33 /** @type {ImageManager} **/
34 const imageManager = window.$components.first('image-manager');
35 imageManager.show(image => {
36 const imageUrl = image.thumbs.display || image.url;
37 let selectedText = this.editor.cm.getSelection();
38 let newText = "[](" + image.url + ")";
39 this.editor.cm.focus();
40 this.editor.cm.replaceSelection(newText);
41 this.editor.cm.setCursor(cursorPos.line, cursorPos.ch + newText.length);
46 const cursorPos = this.editor.cm.getCursor('from');
47 const selectedText = this.editor.cm.getSelection() || '';
48 const newText = `[${selectedText}]()`;
49 this.editor.cm.focus();
50 this.editor.cm.replaceSelection(newText);
51 const cursorPosDiff = (selectedText === '') ? -3 : -1;
52 this.editor.cm.setCursor(cursorPos.line, cursorPos.ch + newText.length+cursorPosDiff);
56 const cursorPos = this.editor.cm.getCursor('from');
57 /** @type {ImageManager} **/
58 const imageManager = window.$components.first('image-manager');
59 imageManager.show(image => {
60 this.insertDrawing(image, cursorPos);
64 // Show the popup link selector and insert a link when finished
66 const cursorPos = this.editor.cm.getCursor('from');
67 /** @type {EntitySelectorPopup} **/
68 const selector = window.$components.first('entity-selector-popup');
69 selector.show(entity => {
70 let selectedText = this.editor.cm.getSelection() || entity.name;
71 let newText = `[${selectedText}](${entity.link})`;
72 this.editor.cm.focus();
73 this.editor.cm.replaceSelection(newText);
74 this.editor.cm.setCursor(cursorPos.line, cursorPos.ch + newText.length);
78 // Show draw.io if enabled and handle save.
80 const url = this.editor.config.drawioUrl;
83 const cursorPos = this.editor.cm.getCursor('from');
85 DrawIO.show(url,() => {
86 return Promise.resolve('');
91 uploaded_to: Number(this.editor.config.pageId),
94 window.$http.post("/images/drawio", data).then(resp => {
95 this.insertDrawing(resp.data, cursorPos);
98 this.handleDrawingUploadError(err);
103 insertDrawing(image, originalCursor) {
104 const newText = `<div drawio-diagram="${image.id}"><img src="${image.url}"></div>`;
105 this.editor.cm.focus();
106 this.editor.cm.replaceSelection(newText);
107 this.editor.cm.setCursor(originalCursor.line, originalCursor.ch + newText.length);
110 // Show draw.io if enabled and handle save.
111 editDrawing(imgContainer) {
112 const drawioUrl = this.editor.config.drawioUrl;
117 const cursorPos = this.editor.cm.getCursor('from');
118 const drawingId = imgContainer.getAttribute('drawio-diagram');
120 DrawIO.show(drawioUrl, () => {
121 return DrawIO.load(drawingId);
126 uploaded_to: Number(this.editor.config.pageId),
129 window.$http.post("/images/drawio", data).then(resp => {
130 const newText = `<div drawio-diagram="${resp.data.id}"><img src="${resp.data.url}"></div>`;
131 const newContent = this.editor.cm.getValue().split('\n').map(line => {
132 if (line.indexOf(`drawio-diagram="${drawingId}"`) !== -1) {
137 this.editor.cm.setValue(newContent);
138 this.editor.cm.setCursor(cursorPos);
139 this.editor.cm.focus();
142 this.handleDrawingUploadError(err);
147 handleDrawingUploadError(error) {
148 if (error.status === 413) {
149 window.$events.emit('error', this.editor.config.text.serverUploadLimit);
151 window.$events.emit('error', this.editor.config.text.imageUploadError);
156 // Make the editor full screen
158 const container = this.editor.config.container;
159 const alreadyFullscreen = container.classList.contains('fullscreen');
160 container.classList.toggle('fullscreen', !alreadyFullscreen);
161 document.body.classList.toggle('markdown-fullscreen', !alreadyFullscreen);
164 // Scroll to a specified text
165 scrollToText(searchText) {
170 const content = this.editor.cm.getValue();
171 const lines = content.split(/\r?\n/);
172 let lineNumber = lines.findIndex(line => {
173 return line && line.indexOf(searchText) !== -1;
176 if (lineNumber === -1) {
180 this.editor.cm.scrollIntoView({
183 this.editor.cm.focus();
184 // set the cursor location.
185 this.editor.cm.setCursor({
187 char: lines[lineNumber].length
192 this.editor.cm.focus();
196 * Insert content into the editor.
197 * @param {String} content
199 insertContent(content) {
200 this.editor.cm.replaceSelection(content);
204 * Prepend content to the editor.
205 * @param {String} content
207 prependContent(content) {
208 const cursorPos = this.editor.cm.getCursor('from');
209 const newContent = content + '\n' + this.editor.cm.getValue();
210 this.editor.cm.setValue(newContent);
211 const prependLineCount = content.split('\n').length;
212 this.editor.cm.setCursor(cursorPos.line + prependLineCount, cursorPos.ch);
216 * Append content to the editor.
217 * @param {String} content
219 appendContent(content) {
220 const cursorPos = this.editor.cm.getCursor('from');
221 const newContent = this.editor.cm.getValue() + '\n' + content;
222 this.editor.cm.setValue(newContent);
223 this.editor.cm.setCursor(cursorPos.line, cursorPos.ch);
227 * Replace the editor's contents
228 * @param {String} content
230 replaceContent(content) {
231 this.editor.cm.setValue(content);
235 * @param {String|RegExp} search
236 * @param {String} replace
238 findAndReplaceContent(search, replace) {
239 const text = this.editor.cm.getValue();
240 const cursor = this.editor.cm.listSelections();
241 this.editor.cm.setValue(text.replace(search, replace));
242 this.editor.cm.setSelections(cursor);
246 * Replace the start of the line
247 * @param {String} newStart
249 replaceLineStart(newStart) {
250 const cursor = this.editor.cm.getCursor();
251 let lineContent = this.editor.cm.getLine(cursor.line);
252 const lineLen = lineContent.length;
253 const lineStart = lineContent.split(' ')[0];
255 // Remove symbol if already set
256 if (lineStart === newStart) {
257 lineContent = lineContent.replace(`${newStart} `, '');
258 this.editor.cm.replaceRange(lineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
259 this.editor.cm.setCursor({line: cursor.line, ch: cursor.ch - (newStart.length + 1)});
263 const alreadySymbol = /^[#>`]/.test(lineStart);
266 posDif = newStart.length - lineStart.length;
267 lineContent = lineContent.replace(lineStart, newStart).trim();
268 } else if (newStart !== '') {
269 posDif = newStart.length + 1;
270 lineContent = newStart + ' ' + lineContent;
272 this.editor.cm.replaceRange(lineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
273 this.editor.cm.setCursor({line: cursor.line, ch: cursor.ch + posDif});
277 * Wrap the line in the given start and end contents.
278 * @param {String} start
279 * @param {String} end
281 wrapLine(start, end) {
282 const cursor = this.editor.cm.getCursor();
283 const lineContent = this.editor.cm.getLine(cursor.line);
284 const lineLen = lineContent.length;
285 let newLineContent = lineContent;
287 if (lineContent.indexOf(start) === 0 && lineContent.slice(-end.length) === end) {
288 newLineContent = lineContent.slice(start.length, lineContent.length - end.length);
290 newLineContent = `${start}${lineContent}${end}`;
293 this.editor.cm.replaceRange(newLineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
294 this.editor.cm.setCursor({line: cursor.line, ch: cursor.ch + start.length});
298 * Wrap the selection in the given contents start and end contents.
299 * @param {String} start
300 * @param {String} end
302 wrapSelection(start, end) {
303 const selection = this.editor.cm.getSelection();
304 if (selection === '') return this.wrapLine(start, end);
306 let newSelection = selection;
310 if (selection.indexOf(start) === 0 && selection.slice(-end.length) === end) {
311 newSelection = selection.slice(start.length, selection.length - end.length);
312 endDiff = -(end.length + start.length);
314 newSelection = `${start}${selection}${end}`;
315 endDiff = start.length + end.length;
318 const selections = this.editor.cm.listSelections()[0];
319 this.editor.cm.replaceSelection(newSelection);
320 const headFirst = selections.head.ch <= selections.anchor.ch;
321 selections.head.ch += headFirst ? frontDiff : endDiff;
322 selections.anchor.ch += headFirst ? endDiff : frontDiff;
323 this.editor.cm.setSelections([selections]);
326 replaceLineStartForOrderedList() {
327 const cursor = this.editor.cm.getCursor();
328 const prevLineContent = this.editor.cm.getLine(cursor.line - 1) || '';
329 const listMatch = prevLineContent.match(/^(\s*)(\d)([).])\s/) || [];
331 const number = (Number(listMatch[2]) || 0) + 1;
332 const whiteSpace = listMatch[1] || '';
333 const listMark = listMatch[3] || '.'
335 const prefix = `${whiteSpace}${number}${listMark}`;
336 return this.replaceLineStart(prefix);
340 * Cycles through the type of callout block within the selection.
341 * Creates a callout block if none existing, and removes it if cycling past the danger type.
343 cycleCalloutTypeAtSelection() {
344 const selectionRange = this.editor.cm.listSelections()[0];
345 const lineContent = this.editor.cm.getLine(selectionRange.anchor.line);
346 const lineLength = lineContent.length;
347 const contentRange = {
348 anchor: {line: selectionRange.anchor.line, ch: 0},
349 head: {line: selectionRange.anchor.line, ch: lineLength},
352 const formats = ['info', 'success', 'warning', 'danger'];
353 const joint = formats.join('|');
354 const regex = new RegExp(`class="((${joint})\\s+callout|callout\\s+(${joint}))"`, 'i');
355 const matches = regex.exec(lineContent);
356 const format = (matches ? (matches[2] || matches[3]) : '').toLowerCase();
358 if (format === formats[formats.length - 1]) {
359 this.wrapLine(`<p class="callout ${formats[formats.length - 1]}">`, '</p>');
360 } else if (format === '') {
361 this.wrapLine('<p class="callout info">', '</p>');
363 const newFormatIndex = formats.indexOf(format) + 1;
364 const newFormat = formats[newFormatIndex];
365 const newContent = lineContent.replace(matches[0], matches[0].replace(format, newFormat));
366 this.editor.cm.replaceRange(newContent, contentRange.anchor, contentRange.head);
368 const chDiff = newContent.length - lineContent.length;
369 selectionRange.anchor.ch += chDiff;
370 if (selectionRange.anchor !== selectionRange.head) {
371 selectionRange.head.ch += chDiff;
373 this.editor.cm.setSelection(selectionRange.anchor, selectionRange.head);
378 * Handle image upload and add image into markdown content
382 if (file === null || file.type.indexOf('image') !== 0) return;
386 let fileNameMatches = file.name.match(/\.(.+)$/);
387 if (fileNameMatches.length > 1) ext = fileNameMatches[1];
390 // Insert image into markdown
391 const id = "image-" + Math.random().toString(16).slice(2);
392 const placeholderImage = window.baseUrl(`/loading.gif#upload${id}`);
393 const selectedText = this.editor.cm.getSelection();
394 const placeHolderText = ``;
395 const cursor = this.editor.cm.getCursor();
396 this.editor.cm.replaceSelection(placeHolderText);
397 this.editor.cm.setCursor({line: cursor.line, ch: cursor.ch + selectedText.length + 3});
399 const remoteFilename = "image-" + Date.now() + "." + ext;
400 const formData = new FormData();
401 formData.append('file', file, remoteFilename);
402 formData.append('uploaded_to', this.editor.config.pageId);
404 window.$http.post('/images/gallery', formData).then(resp => {
405 const newContent = `[](${resp.data.url})`;
406 this.findAndReplaceContent(placeHolderText, newContent);
408 window.$events.emit('error', this.editor.config.text.imageUploadError);
409 this.findAndReplaceContent(placeHolderText, selectedText);
414 syncDisplayPosition() {
415 // Thanks to http://liuhao.im/english/2015/11/10/the-sync-scroll-of-markdown-editor-in-javascript.html
416 const scroll = this.editor.cm.getScrollInfo();
417 const atEnd = scroll.top + scroll.clientHeight === scroll.height;
419 this.editor.display.scrollToIndex(-1);
423 const lineNum = this.editor.cm.lineAtHeight(scroll.top, 'local');
424 const range = this.editor.cm.getRange({line: 0, ch: null}, {line: lineNum, ch: null});
425 const parser = new DOMParser();
426 const doc = parser.parseFromString(this.editor.markdown.render(range), 'text/html');
427 const totalLines = doc.documentElement.querySelectorAll('body > *');
428 this.editor.display.scrollToIndex(totalLines.length);
432 * Fetch and insert the template of the given ID.
433 * The page-relative position provided can be used to determine insert location if possible.
434 * @param {String} templateId
435 * @param {Number} posX
436 * @param {Number} posY
438 insertTemplate(templateId, posX, posY) {
439 const cursorPos = this.editor.cm.coordsChar({left: posX, top: posY});
440 this.editor.cm.setCursor(cursorPos);
441 window.$http.get(`/templates/${templateId}`).then(resp => {
442 const content = resp.data.markdown || resp.data.html;
443 this.editor.cm.replaceSelection(content);
448 * Insert multiple images from the clipboard.
449 * @param {File[]} images
451 insertClipboardImages(images) {
452 const cursorPos = this.editor.cm.coordsChar({left: event.pageX, top: event.pageY});
453 this.editor.cm.setCursor(cursorPos);
454 for (const image of images) {
455 this.uploadImage(image);