2023-04-19 10:20:04 -04:00
|
|
|
import * as DrawIO from '../services/drawio';
|
2022-11-26 11:43:28 -05:00
|
|
|
|
|
|
|
export class Actions {
|
2023-04-18 17:20:02 -04:00
|
|
|
|
2022-11-26 11:43:28 -05:00
|
|
|
/**
|
|
|
|
* @param {MarkdownEditor} editor
|
|
|
|
*/
|
|
|
|
constructor(editor) {
|
|
|
|
this.editor = editor;
|
2023-02-23 07:30:27 -05:00
|
|
|
this.lastContent = {
|
|
|
|
html: '',
|
|
|
|
markdown: '',
|
|
|
|
};
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
updateAndRender() {
|
2023-04-13 07:51:52 -04:00
|
|
|
const content = this.#getText();
|
2022-11-26 11:43:28 -05:00
|
|
|
this.editor.config.inputEl.value = content;
|
|
|
|
|
|
|
|
const html = this.editor.markdown.render(content);
|
2023-02-23 07:30:27 -05:00
|
|
|
window.$events.emit('editor-html-change', '');
|
|
|
|
window.$events.emit('editor-markdown-change', '');
|
|
|
|
this.lastContent.html = html;
|
|
|
|
this.lastContent.markdown = content;
|
2022-11-26 11:43:28 -05:00
|
|
|
this.editor.display.patchWithHtml(html);
|
|
|
|
}
|
|
|
|
|
2023-02-23 07:30:27 -05:00
|
|
|
getContent() {
|
|
|
|
return this.lastContent;
|
|
|
|
}
|
|
|
|
|
2023-04-11 06:48:58 -04:00
|
|
|
showImageInsert() {
|
2023-04-18 17:20:02 -04:00
|
|
|
/** @type {ImageManager} * */
|
2022-11-26 11:43:28 -05:00
|
|
|
const imageManager = window.$components.first('image-manager');
|
2023-04-11 08:16:04 -04:00
|
|
|
|
2022-11-26 11:43:28 -05:00
|
|
|
imageManager.show(image => {
|
|
|
|
const imageUrl = image.thumbs.display || image.url;
|
2023-04-11 08:16:04 -04:00
|
|
|
const selectedText = this.#getSelectionText();
|
2023-04-18 17:20:02 -04:00
|
|
|
const newText = `[![${selectedText || image.name}](${imageUrl})](${image.url})`;
|
2023-04-11 08:16:04 -04:00
|
|
|
this.#replaceSelection(newText, newText.length);
|
2022-11-26 11:43:28 -05:00
|
|
|
}, 'gallery');
|
|
|
|
}
|
|
|
|
|
2023-04-11 06:48:58 -04:00
|
|
|
insertImage() {
|
2023-04-11 08:16:04 -04:00
|
|
|
const newText = `![${this.#getSelectionText()}](http://)`;
|
|
|
|
this.#replaceSelection(newText, newText.length - 1);
|
2023-04-11 06:48:58 -04:00
|
|
|
}
|
|
|
|
|
2022-11-26 11:43:28 -05:00
|
|
|
insertLink() {
|
2023-04-11 08:16:04 -04:00
|
|
|
const selectedText = this.#getSelectionText();
|
2022-11-26 11:43:28 -05:00
|
|
|
const newText = `[${selectedText}]()`;
|
|
|
|
const cursorPosDiff = (selectedText === '') ? -3 : -1;
|
2023-04-18 17:20:02 -04:00
|
|
|
this.#replaceSelection(newText, newText.length + cursorPosDiff);
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
showImageManager() {
|
2023-04-11 08:16:04 -04:00
|
|
|
const selectionRange = this.#getSelectionRange();
|
2023-04-18 17:20:02 -04:00
|
|
|
/** @type {ImageManager} * */
|
2022-11-26 11:43:28 -05:00
|
|
|
const imageManager = window.$components.first('image-manager');
|
|
|
|
imageManager.show(image => {
|
2023-04-11 08:16:04 -04:00
|
|
|
this.#insertDrawing(image, selectionRange);
|
2022-11-26 11:43:28 -05:00
|
|
|
}, 'drawio');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show the popup link selector and insert a link when finished
|
|
|
|
showLinkSelector() {
|
2023-04-11 08:16:04 -04:00
|
|
|
const selectionRange = this.#getSelectionRange();
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
/** @type {EntitySelectorPopup} * */
|
2022-11-26 11:43:28 -05:00
|
|
|
const selector = window.$components.first('entity-selector-popup');
|
|
|
|
selector.show(entity => {
|
2023-04-11 08:16:04 -04:00
|
|
|
const selectedText = this.#getSelectionText(selectionRange) || entity.name;
|
|
|
|
const newText = `[${selectedText}](${entity.link})`;
|
|
|
|
this.#replaceSelection(newText, newText.length, selectionRange);
|
2022-11-26 11:43:28 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show draw.io if enabled and handle save.
|
|
|
|
startDrawing() {
|
|
|
|
const url = this.editor.config.drawioUrl;
|
|
|
|
if (!url) return;
|
|
|
|
|
2023-04-11 08:16:04 -04:00
|
|
|
const selectionRange = this.#getSelectionRange();
|
2022-11-26 11:43:28 -05:00
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
DrawIO.show(url, () => Promise.resolve(''), pngData => {
|
2022-11-26 11:43:28 -05:00
|
|
|
const data = {
|
|
|
|
image: pngData,
|
2022-11-26 16:33:39 -05:00
|
|
|
uploaded_to: Number(this.editor.config.pageId),
|
2022-11-26 11:43:28 -05:00
|
|
|
};
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
window.$http.post('/images/drawio', data).then(resp => {
|
2023-04-11 08:16:04 -04:00
|
|
|
this.#insertDrawing(resp.data, selectionRange);
|
2022-11-26 11:43:28 -05:00
|
|
|
DrawIO.close();
|
|
|
|
}).catch(err => {
|
|
|
|
this.handleDrawingUploadError(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-11 08:16:04 -04:00
|
|
|
#insertDrawing(image, originalSelectionRange) {
|
2022-11-26 11:43:28 -05:00
|
|
|
const newText = `<div drawio-diagram="${image.id}"><img src="${image.url}"></div>`;
|
2023-04-11 08:16:04 -04:00
|
|
|
this.#replaceSelection(newText, newText.length, originalSelectionRange);
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show draw.io if enabled and handle save.
|
|
|
|
editDrawing(imgContainer) {
|
2023-04-18 17:20:02 -04:00
|
|
|
const {drawioUrl} = this.editor.config;
|
2022-11-26 11:43:28 -05:00
|
|
|
if (!drawioUrl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-13 07:51:52 -04:00
|
|
|
const selectionRange = this.#getSelectionRange();
|
2022-11-26 11:43:28 -05:00
|
|
|
const drawingId = imgContainer.getAttribute('drawio-diagram');
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
DrawIO.show(drawioUrl, () => DrawIO.load(drawingId), pngData => {
|
2022-11-26 11:43:28 -05:00
|
|
|
const data = {
|
|
|
|
image: pngData,
|
|
|
|
uploaded_to: Number(this.editor.config.pageId),
|
|
|
|
};
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
window.$http.post('/images/drawio', data).then(resp => {
|
2022-11-26 11:43:28 -05:00
|
|
|
const newText = `<div drawio-diagram="${resp.data.id}"><img src="${resp.data.url}"></div>`;
|
2023-04-13 07:51:52 -04:00
|
|
|
const newContent = this.#getText().split('\n').map(line => {
|
2022-11-26 11:43:28 -05:00
|
|
|
if (line.indexOf(`drawio-diagram="${drawingId}"`) !== -1) {
|
|
|
|
return newText;
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}).join('\n');
|
2023-04-13 07:51:52 -04:00
|
|
|
this.#setText(newContent, selectionRange);
|
2022-11-26 11:43:28 -05:00
|
|
|
DrawIO.close();
|
|
|
|
}).catch(err => {
|
|
|
|
this.handleDrawingUploadError(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDrawingUploadError(error) {
|
|
|
|
if (error.status === 413) {
|
|
|
|
window.$events.emit('error', this.editor.config.text.serverUploadLimit);
|
|
|
|
} else {
|
|
|
|
window.$events.emit('error', this.editor.config.text.imageUploadError);
|
|
|
|
}
|
2023-04-19 10:20:04 -04:00
|
|
|
console.error(error);
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the editor full screen
|
|
|
|
fullScreen() {
|
2023-04-18 17:20:02 -04:00
|
|
|
const {container} = this.editor.config;
|
2022-11-26 11:43:28 -05:00
|
|
|
const alreadyFullscreen = container.classList.contains('fullscreen');
|
|
|
|
container.classList.toggle('fullscreen', !alreadyFullscreen);
|
|
|
|
document.body.classList.toggle('markdown-fullscreen', !alreadyFullscreen);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scroll to a specified text
|
|
|
|
scrollToText(searchText) {
|
|
|
|
if (!searchText) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-11 08:16:04 -04:00
|
|
|
const text = this.editor.cm.state.doc;
|
|
|
|
let lineCount = 1;
|
|
|
|
let scrollToLine = -1;
|
|
|
|
for (const line of text.iterLines()) {
|
|
|
|
if (line.includes(searchText)) {
|
|
|
|
scrollToLine = lineCount;
|
|
|
|
break;
|
|
|
|
}
|
2023-04-19 10:20:04 -04:00
|
|
|
lineCount += 1;
|
2023-04-11 08:16:04 -04:00
|
|
|
}
|
2022-11-26 11:43:28 -05:00
|
|
|
|
2023-04-11 08:16:04 -04:00
|
|
|
if (scrollToLine === -1) {
|
2022-11-26 11:43:28 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-11 08:16:04 -04:00
|
|
|
const line = text.line(scrollToLine);
|
2023-04-13 12:38:11 -04:00
|
|
|
this.#setSelection(line.from, line.to, true);
|
2023-04-11 08:16:04 -04:00
|
|
|
this.focus();
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
2023-04-11 08:16:04 -04:00
|
|
|
if (!this.editor.cm.hasFocus) {
|
|
|
|
this.editor.cm.focus();
|
|
|
|
}
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert content into the editor.
|
|
|
|
* @param {String} content
|
|
|
|
*/
|
|
|
|
insertContent(content) {
|
2023-04-11 08:16:04 -04:00
|
|
|
this.#replaceSelection(content, content.length);
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepend content to the editor.
|
|
|
|
* @param {String} content
|
|
|
|
*/
|
|
|
|
prependContent(content) {
|
2023-04-13 07:51:52 -04:00
|
|
|
content = this.#cleanTextForEditor(content);
|
|
|
|
const selectionRange = this.#getSelectionRange();
|
2023-04-13 12:38:11 -04:00
|
|
|
const selectFrom = selectionRange.from + content.length + 1;
|
2023-04-18 17:20:02 -04:00
|
|
|
this.#dispatchChange(0, 0, `${content}\n`, selectFrom);
|
2023-04-13 07:51:52 -04:00
|
|
|
this.focus();
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append content to the editor.
|
|
|
|
* @param {String} content
|
|
|
|
*/
|
|
|
|
appendContent(content) {
|
2023-04-13 07:51:52 -04:00
|
|
|
content = this.#cleanTextForEditor(content);
|
2023-04-18 17:20:02 -04:00
|
|
|
this.#dispatchChange(this.editor.cm.state.doc.length, `\n${content}`);
|
2023-04-13 07:51:52 -04:00
|
|
|
this.focus();
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the editor's contents
|
|
|
|
* @param {String} content
|
|
|
|
*/
|
|
|
|
replaceContent(content) {
|
2023-04-18 17:20:02 -04:00
|
|
|
this.#setText(content);
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the start of the line
|
|
|
|
* @param {String} newStart
|
|
|
|
*/
|
|
|
|
replaceLineStart(newStart) {
|
2023-04-13 07:51:52 -04:00
|
|
|
const selectionRange = this.#getSelectionRange();
|
|
|
|
const line = this.editor.cm.state.doc.lineAt(selectionRange.from);
|
|
|
|
|
|
|
|
const lineContent = line.text;
|
2022-11-26 11:43:28 -05:00
|
|
|
const lineStart = lineContent.split(' ')[0];
|
|
|
|
|
|
|
|
// Remove symbol if already set
|
|
|
|
if (lineStart === newStart) {
|
2023-04-13 07:51:52 -04:00
|
|
|
const newLineContent = lineContent.replace(`${newStart} `, '');
|
2023-04-13 12:38:11 -04:00
|
|
|
const selectFrom = selectionRange.from + (newLineContent.length - lineContent.length);
|
|
|
|
this.#dispatchChange(line.from, line.to, newLineContent, selectFrom);
|
2022-11-26 11:43:28 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-13 07:51:52 -04:00
|
|
|
let newLineContent = lineContent;
|
2022-11-26 11:43:28 -05:00
|
|
|
const alreadySymbol = /^[#>`]/.test(lineStart);
|
|
|
|
if (alreadySymbol) {
|
2023-04-13 07:51:52 -04:00
|
|
|
newLineContent = lineContent.replace(lineStart, newStart).trim();
|
2022-11-26 11:43:28 -05:00
|
|
|
} else if (newStart !== '') {
|
2023-04-18 17:20:02 -04:00
|
|
|
newLineContent = `${newStart} ${lineContent}`;
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
2023-04-13 12:38:11 -04:00
|
|
|
const selectFrom = selectionRange.from + (newLineContent.length - lineContent.length);
|
|
|
|
this.#dispatchChange(line.from, line.to, newLineContent, selectFrom);
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrap the selection in the given contents start and end contents.
|
|
|
|
* @param {String} start
|
|
|
|
* @param {String} end
|
|
|
|
*/
|
|
|
|
wrapSelection(start, end) {
|
2023-04-19 10:20:04 -04:00
|
|
|
const selectRange = this.#getSelectionRange();
|
|
|
|
const selectionText = this.#getSelectionText(selectRange);
|
|
|
|
if (!selectionText) {
|
|
|
|
this.#wrapLine(start, end);
|
|
|
|
return;
|
|
|
|
}
|
2022-11-26 11:43:28 -05:00
|
|
|
|
2023-04-13 07:51:52 -04:00
|
|
|
let newSelectionText = selectionText;
|
|
|
|
let newRange;
|
2022-11-26 11:43:28 -05:00
|
|
|
|
2023-04-13 07:51:52 -04:00
|
|
|
if (selectionText.startsWith(start) && selectionText.endsWith(end)) {
|
|
|
|
newSelectionText = selectionText.slice(start.length, selectionText.length - end.length);
|
2023-04-19 10:20:04 -04:00
|
|
|
newRange = selectRange.extend(selectRange.from, selectRange.to - (start.length + end.length));
|
2022-11-26 11:43:28 -05:00
|
|
|
} else {
|
2023-04-13 07:51:52 -04:00
|
|
|
newSelectionText = `${start}${selectionText}${end}`;
|
2023-04-19 10:20:04 -04:00
|
|
|
newRange = selectRange.extend(selectRange.from, selectRange.to + (start.length + end.length));
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
2023-04-19 10:20:04 -04:00
|
|
|
this.#dispatchChange(
|
|
|
|
selectRange.from,
|
|
|
|
selectRange.to,
|
|
|
|
newSelectionText,
|
|
|
|
newRange.anchor,
|
|
|
|
newRange.head,
|
|
|
|
);
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
replaceLineStartForOrderedList() {
|
2023-04-13 12:18:32 -04:00
|
|
|
const selectionRange = this.#getSelectionRange();
|
|
|
|
const line = this.editor.cm.state.doc.lineAt(selectionRange.from);
|
|
|
|
const prevLine = this.editor.cm.state.doc.line(line.number - 1);
|
|
|
|
|
|
|
|
const listMatch = prevLine.text.match(/^(\s*)(\d)([).])\s/) || [];
|
2022-11-26 11:43:28 -05:00
|
|
|
|
|
|
|
const number = (Number(listMatch[2]) || 0) + 1;
|
|
|
|
const whiteSpace = listMatch[1] || '';
|
2023-04-18 17:20:02 -04:00
|
|
|
const listMark = listMatch[3] || '.';
|
2022-11-26 11:43:28 -05:00
|
|
|
|
|
|
|
const prefix = `${whiteSpace}${number}${listMark}`;
|
|
|
|
return this.replaceLineStart(prefix);
|
|
|
|
}
|
|
|
|
|
2022-11-26 18:18:51 -05:00
|
|
|
/**
|
|
|
|
* Cycles through the type of callout block within the selection.
|
|
|
|
* Creates a callout block if none existing, and removes it if cycling past the danger type.
|
|
|
|
*/
|
|
|
|
cycleCalloutTypeAtSelection() {
|
2023-04-13 12:18:32 -04:00
|
|
|
const selectionRange = this.#getSelectionRange();
|
|
|
|
const line = this.editor.cm.state.doc.lineAt(selectionRange.from);
|
2022-11-26 18:18:51 -05:00
|
|
|
|
|
|
|
const formats = ['info', 'success', 'warning', 'danger'];
|
|
|
|
const joint = formats.join('|');
|
|
|
|
const regex = new RegExp(`class="((${joint})\\s+callout|callout\\s+(${joint}))"`, 'i');
|
2023-04-13 12:18:32 -04:00
|
|
|
const matches = regex.exec(line.text);
|
2022-11-26 18:18:51 -05:00
|
|
|
const format = (matches ? (matches[2] || matches[3]) : '').toLowerCase();
|
|
|
|
|
|
|
|
if (format === formats[formats.length - 1]) {
|
2023-04-13 12:18:32 -04:00
|
|
|
this.#wrapLine(`<p class="callout ${formats[formats.length - 1]}">`, '</p>');
|
2022-11-26 18:18:51 -05:00
|
|
|
} else if (format === '') {
|
2023-04-13 12:18:32 -04:00
|
|
|
this.#wrapLine('<p class="callout info">', '</p>');
|
2022-11-26 18:18:51 -05:00
|
|
|
} else {
|
|
|
|
const newFormatIndex = formats.indexOf(format) + 1;
|
|
|
|
const newFormat = formats[newFormatIndex];
|
2023-04-13 12:18:32 -04:00
|
|
|
const newContent = line.text.replace(matches[0], matches[0].replace(format, newFormat));
|
|
|
|
const lineDiff = newContent.length - line.text.length;
|
2023-04-19 10:20:04 -04:00
|
|
|
this.#dispatchChange(
|
|
|
|
line.from,
|
|
|
|
line.to,
|
|
|
|
newContent,
|
|
|
|
selectionRange.anchor + lineDiff,
|
|
|
|
selectionRange.head + lineDiff,
|
|
|
|
);
|
2022-11-26 18:18:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 10:01:44 -04:00
|
|
|
syncDisplayPosition(event) {
|
2022-11-26 11:43:28 -05:00
|
|
|
// Thanks to http://liuhao.im/english/2015/11/10/the-sync-scroll-of-markdown-editor-in-javascript.html
|
2023-04-10 10:01:44 -04:00
|
|
|
const scrollEl = event.target;
|
|
|
|
const atEnd = Math.abs(scrollEl.scrollHeight - scrollEl.clientHeight - scrollEl.scrollTop) < 1;
|
2022-11-26 11:43:28 -05:00
|
|
|
if (atEnd) {
|
2022-11-26 16:33:39 -05:00
|
|
|
this.editor.display.scrollToIndex(-1);
|
2022-11-26 11:43:28 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-10 10:01:44 -04:00
|
|
|
const blockInfo = this.editor.cm.lineBlockAtHeight(scrollEl.scrollTop);
|
|
|
|
const range = this.editor.cm.state.sliceDoc(0, blockInfo.from);
|
2022-11-26 11:43:28 -05:00
|
|
|
const parser = new DOMParser();
|
|
|
|
const doc = parser.parseFromString(this.editor.markdown.render(range), 'text/html');
|
|
|
|
const totalLines = doc.documentElement.querySelectorAll('body > *');
|
2022-11-26 16:33:39 -05:00
|
|
|
this.editor.display.scrollToIndex(totalLines.length);
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch and insert the template of the given ID.
|
|
|
|
* The page-relative position provided can be used to determine insert location if possible.
|
|
|
|
* @param {String} templateId
|
|
|
|
* @param {Number} posX
|
|
|
|
* @param {Number} posY
|
|
|
|
*/
|
2023-04-13 12:18:32 -04:00
|
|
|
async insertTemplate(templateId, posX, posY) {
|
|
|
|
const cursorPos = this.editor.cm.posAtCoords({x: posX, y: posY}, false);
|
|
|
|
const {data} = await window.$http.get(`/templates/${templateId}`);
|
|
|
|
const content = data.markdown || data.html;
|
2023-04-13 12:38:11 -04:00
|
|
|
this.#dispatchChange(cursorPos, cursorPos, content, cursorPos);
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-13 12:18:32 -04:00
|
|
|
* Insert multiple images from the clipboard from an event at the provided
|
|
|
|
* screen coordinates (Typically form a paste event).
|
2022-11-26 11:43:28 -05:00
|
|
|
* @param {File[]} images
|
2023-04-13 12:18:32 -04:00
|
|
|
* @param {Number} posX
|
|
|
|
* @param {Number} posY
|
2022-11-26 11:43:28 -05:00
|
|
|
*/
|
2023-04-13 12:18:32 -04:00
|
|
|
insertClipboardImages(images, posX, posY) {
|
|
|
|
const cursorPos = this.editor.cm.posAtCoords({x: posX, y: posY}, false);
|
2022-11-26 11:43:28 -05:00
|
|
|
for (const image of images) {
|
2023-04-13 12:18:32 -04:00
|
|
|
this.uploadImage(image, cursorPos);
|
2022-11-26 11:43:28 -05:00
|
|
|
}
|
|
|
|
}
|
2023-04-11 08:16:04 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle image upload and add image into markdown content
|
|
|
|
* @param {File} file
|
2023-04-13 12:18:32 -04:00
|
|
|
* @param {?Number} position
|
2023-04-11 08:16:04 -04:00
|
|
|
*/
|
2023-04-18 17:20:02 -04:00
|
|
|
async uploadImage(file, position = null) {
|
2023-04-11 08:16:04 -04:00
|
|
|
if (file === null || file.type.indexOf('image') !== 0) return;
|
|
|
|
let ext = 'png';
|
|
|
|
|
2023-04-13 12:18:32 -04:00
|
|
|
if (position === null) {
|
|
|
|
position = this.#getSelectionRange().from;
|
|
|
|
}
|
|
|
|
|
2023-04-11 08:16:04 -04:00
|
|
|
if (file.name) {
|
2023-04-18 17:20:02 -04:00
|
|
|
const fileNameMatches = file.name.match(/\.(.+)$/);
|
2023-04-11 08:16:04 -04:00
|
|
|
if (fileNameMatches.length > 1) ext = fileNameMatches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert image into markdown
|
2023-04-18 17:20:02 -04:00
|
|
|
const id = `image-${Math.random().toString(16).slice(2)}`;
|
2023-04-11 08:16:04 -04:00
|
|
|
const placeholderImage = window.baseUrl(`/loading.gif#upload${id}`);
|
2023-04-13 12:18:32 -04:00
|
|
|
const placeHolderText = `![](${placeholderImage})`;
|
2023-04-13 12:38:11 -04:00
|
|
|
this.#dispatchChange(position, position, placeHolderText, position);
|
2023-04-11 08:16:04 -04:00
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
const remoteFilename = `image-${Date.now()}.${ext}`;
|
2023-04-11 08:16:04 -04:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('file', file, remoteFilename);
|
|
|
|
formData.append('uploaded_to', this.editor.config.pageId);
|
|
|
|
|
2023-04-13 12:18:32 -04:00
|
|
|
try {
|
|
|
|
const {data} = await window.$http.post('/images/gallery', formData);
|
|
|
|
const newContent = `[![](${data.thumbs.display})](${data.url})`;
|
2023-04-13 07:51:52 -04:00
|
|
|
this.#findAndReplaceContent(placeHolderText, newContent);
|
2023-04-13 12:18:32 -04:00
|
|
|
} catch (err) {
|
2023-04-11 08:16:04 -04:00
|
|
|
window.$events.emit('error', this.editor.config.text.imageUploadError);
|
2023-04-13 12:18:32 -04:00
|
|
|
this.#findAndReplaceContent(placeHolderText, '');
|
2023-04-19 10:20:04 -04:00
|
|
|
console.error(err);
|
2023-04-13 12:18:32 -04:00
|
|
|
}
|
2023-04-11 08:16:04 -04:00
|
|
|
}
|
|
|
|
|
2023-04-13 07:51:52 -04:00
|
|
|
/**
|
|
|
|
* Get the current text of the editor instance.
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
#getText() {
|
|
|
|
return this.editor.cm.state.doc.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the text of the current editor instance.
|
|
|
|
* @param {String} text
|
|
|
|
* @param {?SelectionRange} selectionRange
|
|
|
|
*/
|
|
|
|
#setText(text, selectionRange = null) {
|
|
|
|
selectionRange = selectionRange || this.#getSelectionRange();
|
2023-06-13 10:13:07 -04:00
|
|
|
const newDoc = this.editor.cm.state.toText(text);
|
|
|
|
const newSelectFrom = Math.min(selectionRange.from, newDoc.length);
|
|
|
|
this.#dispatchChange(0, this.editor.cm.state.doc.length, text, newSelectFrom);
|
2023-04-13 07:51:52 -04:00
|
|
|
this.focus();
|
|
|
|
}
|
|
|
|
|
2023-04-11 08:16:04 -04:00
|
|
|
/**
|
|
|
|
* Replace the current selection and focus the editor.
|
|
|
|
* Takes an offset for the cursor, after the change, relative to the start of the provided string.
|
|
|
|
* Can be provided a selection range to use instead of the current selection range.
|
|
|
|
* @param {String} newContent
|
|
|
|
* @param {Number} cursorOffset
|
|
|
|
* @param {?SelectionRange} selectionRange
|
|
|
|
*/
|
|
|
|
#replaceSelection(newContent, cursorOffset = 0, selectionRange = null) {
|
|
|
|
selectionRange = selectionRange || this.editor.cm.state.selection.main;
|
2023-04-19 10:20:04 -04:00
|
|
|
const selectFrom = selectionRange.from + cursorOffset;
|
|
|
|
this.#dispatchChange(selectionRange.from, selectionRange.to, newContent, selectFrom);
|
2023-04-11 08:16:04 -04:00
|
|
|
this.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the text content of the main current selection.
|
|
|
|
* @param {SelectionRange} selectionRange
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
#getSelectionText(selectionRange = null) {
|
|
|
|
selectionRange = selectionRange || this.#getSelectionRange();
|
|
|
|
return this.editor.cm.state.sliceDoc(selectionRange.from, selectionRange.to);
|
|
|
|
}
|
|
|
|
|
2023-04-13 07:51:52 -04:00
|
|
|
/**
|
|
|
|
* Get the range of the current main selection.
|
|
|
|
* @return {SelectionRange}
|
|
|
|
*/
|
2023-04-11 08:16:04 -04:00
|
|
|
#getSelectionRange() {
|
|
|
|
return this.editor.cm.state.selection.main;
|
|
|
|
}
|
2023-04-13 07:51:52 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans the given text to work with the editor.
|
|
|
|
* Standardises line endings to what's expected.
|
|
|
|
* @param {String} text
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
#cleanTextForEditor(text) {
|
2023-04-18 17:20:02 -04:00
|
|
|
return text.replace(/\r\n|\r/g, '\n');
|
2023-04-13 07:51:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find and replace the first occurrence of [search] with [replace]
|
|
|
|
* @param {String} search
|
|
|
|
* @param {String} replace
|
|
|
|
*/
|
|
|
|
#findAndReplaceContent(search, replace) {
|
|
|
|
const newText = this.#getText().replace(search, replace);
|
|
|
|
this.#setText(newText);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrap the line in the given start and end contents.
|
|
|
|
* @param {String} start
|
|
|
|
* @param {String} end
|
|
|
|
*/
|
|
|
|
#wrapLine(start, end) {
|
|
|
|
const selectionRange = this.#getSelectionRange();
|
|
|
|
const line = this.editor.cm.state.doc.lineAt(selectionRange.from);
|
|
|
|
const lineContent = line.text;
|
|
|
|
let newLineContent;
|
|
|
|
let lineOffset = 0;
|
|
|
|
|
|
|
|
if (lineContent.startsWith(start) && lineContent.endsWith(end)) {
|
|
|
|
newLineContent = lineContent.slice(start.length, lineContent.length - end.length);
|
|
|
|
lineOffset = -(start.length);
|
|
|
|
} else {
|
|
|
|
newLineContent = `${start}${lineContent}${end}`;
|
|
|
|
lineOffset = start.length;
|
|
|
|
}
|
|
|
|
|
2023-04-13 12:38:11 -04:00
|
|
|
this.#dispatchChange(line.from, line.to, newLineContent, selectionRange.from + lineOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch changes to the editor.
|
|
|
|
* @param {Number} from
|
|
|
|
* @param {?Number} to
|
|
|
|
* @param {?String} text
|
|
|
|
* @param {?Number} selectFrom
|
|
|
|
* @param {?Number} selectTo
|
|
|
|
*/
|
|
|
|
#dispatchChange(from, to = null, text = null, selectFrom = null, selectTo = null) {
|
2023-04-18 17:20:02 -04:00
|
|
|
const tr = {changes: {from, to, insert: text}};
|
2023-04-13 12:38:11 -04:00
|
|
|
|
|
|
|
if (selectFrom) {
|
|
|
|
tr.selection = {anchor: selectFrom};
|
2023-04-19 10:20:04 -04:00
|
|
|
if (selectTo) {
|
|
|
|
tr.selection.head = selectTo;
|
|
|
|
}
|
2023-04-13 12:38:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.editor.cm.dispatch(tr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current selection range.
|
|
|
|
* Optionally will scroll the new range into view.
|
|
|
|
* @param {Number} from
|
|
|
|
* @param {Number} to
|
|
|
|
* @param {Boolean} scrollIntoView
|
|
|
|
*/
|
|
|
|
#setSelection(from, to, scrollIntoView = false) {
|
2023-04-13 07:51:52 -04:00
|
|
|
this.editor.cm.dispatch({
|
2023-04-13 12:38:11 -04:00
|
|
|
selection: {anchor: from, head: to},
|
|
|
|
scrollIntoView,
|
2023-04-13 07:51:52 -04:00
|
|
|
});
|
|
|
|
}
|
2023-04-18 17:20:02 -04:00
|
|
|
|
|
|
|
}
|