1 import * as DrawIO from '../services/drawio.ts';
6 * @param {MarkdownEditor} editor
17 const content = this.#getText();
18 this.editor.config.inputEl.value = content;
20 const html = this.editor.markdown.render(content);
21 window.$events.emit('editor-html-change', '');
22 window.$events.emit('editor-markdown-change', '');
23 this.lastContent.html = html;
24 this.lastContent.markdown = content;
25 this.editor.display.patchWithHtml(html);
29 return this.lastContent;
33 /** @type {ImageManager} * */
34 const imageManager = window.$components.first('image-manager');
36 imageManager.show(image => {
37 const imageUrl = image.thumbs?.display || image.url;
38 const selectedText = this.#getSelectionText();
39 const newText = `[](${image.url})`;
40 this.#replaceSelection(newText, newText.length);
45 const newText = ``;
46 this.#replaceSelection(newText, newText.length - 1);
50 const selectedText = this.#getSelectionText();
51 const newText = `[${selectedText}]()`;
52 const cursorPosDiff = (selectedText === '') ? -3 : -1;
53 this.#replaceSelection(newText, newText.length + cursorPosDiff);
57 const selectionRange = this.#getSelectionRange();
58 /** @type {ImageManager} * */
59 const imageManager = window.$components.first('image-manager');
60 imageManager.show(image => {
61 this.#insertDrawing(image, selectionRange);
65 // Show the popup link selector and insert a link when finished
67 const selectionRange = this.#getSelectionRange();
69 /** @type {EntitySelectorPopup} * */
70 const selector = window.$components.first('entity-selector-popup');
71 const selectionText = this.#getSelectionText(selectionRange);
72 selector.show(entity => {
73 const selectedText = selectionText || entity.name;
74 const newText = `[${selectedText}](${entity.link})`;
75 this.#replaceSelection(newText, newText.length, selectionRange);
77 initialValue: selectionText,
78 searchEndpoint: '/search/entity-selector',
79 entityTypes: 'page,book,chapter,bookshelf',
80 entityPermission: 'view',
84 // Show draw.io if enabled and handle save.
86 const url = this.editor.config.drawioUrl;
89 const selectionRange = this.#getSelectionRange();
91 DrawIO.show(url, () => Promise.resolve(''), async pngData => {
94 uploaded_to: Number(this.editor.config.pageId),
98 const resp = await window.$http.post('/images/drawio', data);
99 this.#insertDrawing(resp.data, selectionRange);
102 this.handleDrawingUploadError(err);
103 throw new Error(`Failed to save image with error: ${err}`);
108 #insertDrawing(image, originalSelectionRange) {
109 const newText = `<div drawio-diagram="${image.id}"><img src="${image.url}"></div>`;
110 this.#replaceSelection(newText, newText.length, originalSelectionRange);
113 // Show draw.io if enabled and handle save.
114 editDrawing(imgContainer) {
115 const {drawioUrl} = this.editor.config;
120 const selectionRange = this.#getSelectionRange();
121 const drawingId = imgContainer.getAttribute('drawio-diagram');
123 DrawIO.show(drawioUrl, () => DrawIO.load(drawingId), async pngData => {
126 uploaded_to: Number(this.editor.config.pageId),
130 const resp = await window.$http.post('/images/drawio', data);
131 const newText = `<div drawio-diagram="${resp.data.id}"><img src="${resp.data.url}"></div>`;
132 const newContent = this.#getText().split('\n').map(line => {
133 if (line.indexOf(`drawio-diagram="${drawingId}"`) !== -1) {
138 this.#setText(newContent, selectionRange);
141 this.handleDrawingUploadError(err);
142 throw new Error(`Failed to save image with error: ${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);
153 console.error(error);
156 // Make the editor full screen
158 const {container} = this.editor.config;
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 text = this.editor.cm.state.doc;
172 let scrollToLine = -1;
173 for (const line of text.iterLines()) {
174 if (line.includes(searchText)) {
175 scrollToLine = lineCount;
181 if (scrollToLine === -1) {
185 const line = text.line(scrollToLine);
186 this.#setSelection(line.from, line.to, true);
191 if (!this.editor.cm.hasFocus) {
192 this.editor.cm.focus();
197 * Insert content into the editor.
198 * @param {String} content
200 insertContent(content) {
201 this.#replaceSelection(content, content.length);
205 * Prepend content to the editor.
206 * @param {String} content
208 prependContent(content) {
209 content = this.#cleanTextForEditor(content);
210 const selectionRange = this.#getSelectionRange();
211 const selectFrom = selectionRange.from + content.length + 1;
212 this.#dispatchChange(0, 0, `${content}\n`, selectFrom);
217 * Append content to the editor.
218 * @param {String} content
220 appendContent(content) {
221 content = this.#cleanTextForEditor(content);
222 this.#dispatchChange(this.editor.cm.state.doc.length, `\n${content}`);
227 * Replace the editor's contents
228 * @param {String} content
230 replaceContent(content) {
231 this.#setText(content);
235 * Replace the start of the line
236 * @param {String} newStart
238 replaceLineStart(newStart) {
239 const selectionRange = this.#getSelectionRange();
240 const line = this.editor.cm.state.doc.lineAt(selectionRange.from);
242 const lineContent = line.text;
243 const lineStart = lineContent.split(' ')[0];
245 // Remove symbol if already set
246 if (lineStart === newStart) {
247 const newLineContent = lineContent.replace(`${newStart} `, '');
248 const selectFrom = selectionRange.from + (newLineContent.length - lineContent.length);
249 this.#dispatchChange(line.from, line.to, newLineContent, selectFrom);
253 let newLineContent = lineContent;
254 const alreadySymbol = /^[#>`]/.test(lineStart);
256 newLineContent = lineContent.replace(lineStart, newStart).trim();
257 } else if (newStart !== '') {
258 newLineContent = `${newStart} ${lineContent}`;
261 const selectFrom = selectionRange.from + (newLineContent.length - lineContent.length);
262 this.#dispatchChange(line.from, line.to, newLineContent, selectFrom);
266 * Wrap the selection in the given contents start and end contents.
267 * @param {String} start
268 * @param {String} end
270 wrapSelection(start, end) {
271 const selectRange = this.#getSelectionRange();
272 const selectionText = this.#getSelectionText(selectRange);
273 if (!selectionText) {
274 this.#wrapLine(start, end);
278 let newSelectionText = selectionText;
281 if (selectionText.startsWith(start) && selectionText.endsWith(end)) {
282 newSelectionText = selectionText.slice(start.length, selectionText.length - end.length);
283 newRange = selectRange.extend(selectRange.from, selectRange.to - (start.length + end.length));
285 newSelectionText = `${start}${selectionText}${end}`;
286 newRange = selectRange.extend(selectRange.from, selectRange.to + (start.length + end.length));
289 this.#dispatchChange(
298 replaceLineStartForOrderedList() {
299 const selectionRange = this.#getSelectionRange();
300 const line = this.editor.cm.state.doc.lineAt(selectionRange.from);
301 const prevLine = this.editor.cm.state.doc.line(line.number - 1);
303 const listMatch = prevLine.text.match(/^(\s*)(\d)([).])\s/) || [];
305 const number = (Number(listMatch[2]) || 0) + 1;
306 const whiteSpace = listMatch[1] || '';
307 const listMark = listMatch[3] || '.';
309 const prefix = `${whiteSpace}${number}${listMark}`;
310 return this.replaceLineStart(prefix);
314 * Cycles through the type of callout block within the selection.
315 * Creates a callout block if none existing, and removes it if cycling past the danger type.
317 cycleCalloutTypeAtSelection() {
318 const selectionRange = this.#getSelectionRange();
319 const line = this.editor.cm.state.doc.lineAt(selectionRange.from);
321 const formats = ['info', 'success', 'warning', 'danger'];
322 const joint = formats.join('|');
323 const regex = new RegExp(`class="((${joint})\\s+callout|callout\\s+(${joint}))"`, 'i');
324 const matches = regex.exec(line.text);
325 const format = (matches ? (matches[2] || matches[3]) : '').toLowerCase();
327 if (format === formats[formats.length - 1]) {
328 this.#wrapLine(`<p class="callout ${formats[formats.length - 1]}">`, '</p>');
329 } else if (format === '') {
330 this.#wrapLine('<p class="callout info">', '</p>');
332 const newFormatIndex = formats.indexOf(format) + 1;
333 const newFormat = formats[newFormatIndex];
334 const newContent = line.text.replace(matches[0], matches[0].replace(format, newFormat));
335 const lineDiff = newContent.length - line.text.length;
336 this.#dispatchChange(
340 selectionRange.anchor + lineDiff,
341 selectionRange.head + lineDiff,
346 syncDisplayPosition(event) {
347 // Thanks to http://liuhao.im/english/2015/11/10/the-sync-scroll-of-markdown-editor-in-javascript.html
348 const scrollEl = event.target;
349 const atEnd = Math.abs(scrollEl.scrollHeight - scrollEl.clientHeight - scrollEl.scrollTop) < 1;
351 this.editor.display.scrollToIndex(-1);
355 const blockInfo = this.editor.cm.lineBlockAtHeight(scrollEl.scrollTop);
356 const range = this.editor.cm.state.sliceDoc(0, blockInfo.from);
357 const parser = new DOMParser();
358 const doc = parser.parseFromString(this.editor.markdown.render(range), 'text/html');
359 const totalLines = doc.documentElement.querySelectorAll('body > *');
360 this.editor.display.scrollToIndex(totalLines.length);
364 * Fetch and insert the template of the given ID.
365 * The page-relative position provided can be used to determine insert location if possible.
366 * @param {String} templateId
367 * @param {Number} posX
368 * @param {Number} posY
370 async insertTemplate(templateId, posX, posY) {
371 const cursorPos = this.editor.cm.posAtCoords({x: posX, y: posY}, false);
372 const {data} = await window.$http.get(`/templates/${templateId}`);
373 const content = data.markdown || data.html;
374 this.#dispatchChange(cursorPos, cursorPos, content, cursorPos);
378 * Insert multiple images from the clipboard from an event at the provided
379 * screen coordinates (Typically form a paste event).
380 * @param {File[]} images
381 * @param {Number} posX
382 * @param {Number} posY
384 insertClipboardImages(images, posX, posY) {
385 const cursorPos = this.editor.cm.posAtCoords({x: posX, y: posY}, false);
386 for (const image of images) {
387 this.uploadImage(image, cursorPos);
392 * Handle image upload and add image into markdown content
394 * @param {?Number} position
396 async uploadImage(file, position = null) {
397 if (file === null || file.type.indexOf('image') !== 0) return;
400 if (position === null) {
401 position = this.#getSelectionRange().from;
405 const fileNameMatches = file.name.match(/\.(.+)$/);
406 if (fileNameMatches.length > 1) ext = fileNameMatches[1];
409 // Insert image into markdown
410 const id = `image-${Math.random().toString(16).slice(2)}`;
411 const placeholderImage = window.baseUrl(`/loading.gif#upload${id}`);
412 const placeHolderText = ``;
413 this.#dispatchChange(position, position, placeHolderText, position);
415 const remoteFilename = `image-${Date.now()}.${ext}`;
416 const formData = new FormData();
417 formData.append('file', file, remoteFilename);
418 formData.append('uploaded_to', this.editor.config.pageId);
421 const {data} = await window.$http.post('/images/gallery', formData);
422 const newContent = `[](${data.url})`;
423 this.#findAndReplaceContent(placeHolderText, newContent);
425 window.$events.error(err?.data?.message || this.editor.config.text.imageUploadError);
426 this.#findAndReplaceContent(placeHolderText, '');
432 * Get the current text of the editor instance.
436 return this.editor.cm.state.doc.toString();
440 * Set the text of the current editor instance.
441 * @param {String} text
442 * @param {?SelectionRange} selectionRange
444 #setText(text, selectionRange = null) {
445 selectionRange = selectionRange || this.#getSelectionRange();
446 const newDoc = this.editor.cm.state.toText(text);
447 const newSelectFrom = Math.min(selectionRange.from, newDoc.length);
448 const scrollTop = this.editor.cm.scrollDOM.scrollTop;
449 this.#dispatchChange(0, this.editor.cm.state.doc.length, text, newSelectFrom);
451 window.requestAnimationFrame(() => {
452 this.editor.cm.scrollDOM.scrollTop = scrollTop;
457 * Replace the current selection and focus the editor.
458 * Takes an offset for the cursor, after the change, relative to the start of the provided string.
459 * Can be provided a selection range to use instead of the current selection range.
460 * @param {String} newContent
461 * @param {Number} cursorOffset
462 * @param {?SelectionRange} selectionRange
464 #replaceSelection(newContent, cursorOffset = 0, selectionRange = null) {
465 selectionRange = selectionRange || this.editor.cm.state.selection.main;
466 const selectFrom = selectionRange.from + cursorOffset;
467 this.#dispatchChange(selectionRange.from, selectionRange.to, newContent, selectFrom);
472 * Get the text content of the main current selection.
473 * @param {SelectionRange} selectionRange
476 #getSelectionText(selectionRange = null) {
477 selectionRange = selectionRange || this.#getSelectionRange();
478 return this.editor.cm.state.sliceDoc(selectionRange.from, selectionRange.to);
482 * Get the range of the current main selection.
483 * @return {SelectionRange}
485 #getSelectionRange() {
486 return this.editor.cm.state.selection.main;
490 * Cleans the given text to work with the editor.
491 * Standardises line endings to what's expected.
492 * @param {String} text
495 #cleanTextForEditor(text) {
496 return text.replace(/\r\n|\r/g, '\n');
500 * Find and replace the first occurrence of [search] with [replace]
501 * @param {String} search
502 * @param {String} replace
504 #findAndReplaceContent(search, replace) {
505 const newText = this.#getText().replace(search, replace);
506 this.#setText(newText);
510 * Wrap the line in the given start and end contents.
511 * @param {String} start
512 * @param {String} end
514 #wrapLine(start, end) {
515 const selectionRange = this.#getSelectionRange();
516 const line = this.editor.cm.state.doc.lineAt(selectionRange.from);
517 const lineContent = line.text;
521 if (lineContent.startsWith(start) && lineContent.endsWith(end)) {
522 newLineContent = lineContent.slice(start.length, lineContent.length - end.length);
523 lineOffset = -(start.length);
525 newLineContent = `${start}${lineContent}${end}`;
526 lineOffset = start.length;
529 this.#dispatchChange(line.from, line.to, newLineContent, selectionRange.from + lineOffset);
533 * Dispatch changes to the editor.
534 * @param {Number} from
535 * @param {?Number} to
536 * @param {?String} text
537 * @param {?Number} selectFrom
538 * @param {?Number} selectTo
540 #dispatchChange(from, to = null, text = null, selectFrom = null, selectTo = null) {
541 const tr = {changes: {from, to, insert: text}};
544 tr.selection = {anchor: selectFrom};
546 tr.selection.head = selectTo;
550 this.editor.cm.dispatch(tr);
554 * Set the current selection range.
555 * Optionally will scroll the new range into view.
556 * @param {Number} from
558 * @param {Boolean} scrollIntoView
560 #setSelection(from, to, scrollIntoView = false) {
561 this.editor.cm.dispatch({
562 selection: {anchor: from, head: to},