*/
constructor(editor) {
this.editor = editor;
+ this.lastContent = {
+ html: '',
+ markdown: '',
+ };
}
updateAndRender() {
- const content = this.editor.cm.getValue();
+ const content = this.editor.cm.state.doc.toString();
this.editor.config.inputEl.value = content;
const html = this.editor.markdown.render(content);
- window.$events.emit('editor-html-change', html);
- window.$events.emit('editor-markdown-change', content);
+ window.$events.emit('editor-html-change', '');
+ window.$events.emit('editor-markdown-change', '');
+ this.lastContent.html = html;
+ this.lastContent.markdown = content;
this.editor.display.patchWithHtml(html);
}
- insertImage() {
+ getContent() {
+ return this.lastContent;
+ }
+
+ showImageInsert() {
+ // TODO
const cursorPos = this.editor.cm.getCursor('from');
/** @type {ImageManager} **/
const imageManager = window.$components.first('image-manager');
}, 'gallery');
}
+ insertImage() {
+ // TODO
+ const selectedText = this.editor.cm.getSelection();
+ const newText = ``;
+ const cursorPos = this.editor.cm.getCursor('from');
+ this.editor.cm.replaceSelection(newText);
+ this.editor.cm.setCursor(cursorPos.line, cursorPos.ch + newText.length -1);
+ }
+
insertLink() {
+ // TODO
const cursorPos = this.editor.cm.getCursor('from');
const selectedText = this.editor.cm.getSelection() || '';
const newText = `[${selectedText}]()`;
}
showImageManager() {
+ // TODO
const cursorPos = this.editor.cm.getCursor('from');
/** @type {ImageManager} **/
const imageManager = window.$components.first('image-manager');
// Show the popup link selector and insert a link when finished
showLinkSelector() {
+ // TODO
const cursorPos = this.editor.cm.getCursor('from');
/** @type {EntitySelectorPopup} **/
const selector = window.$components.first('entity-selector-popup');
// Show draw.io if enabled and handle save.
startDrawing() {
+ // TODO
const url = this.editor.config.drawioUrl;
if (!url) return;
const data = {
image: pngData,
- uploaded_to: Number(this.pageId),
+ uploaded_to: Number(this.editor.config.pageId),
};
window.$http.post("/images/drawio", data).then(resp => {
}
insertDrawing(image, originalCursor) {
+ // TODO
const newText = `<div drawio-diagram="${image.id}"><img src="${image.url}"></div>`;
this.editor.cm.focus();
this.editor.cm.replaceSelection(newText);
// Show draw.io if enabled and handle save.
editDrawing(imgContainer) {
+ // TODO
const drawioUrl = this.editor.config.drawioUrl;
if (!drawioUrl) {
return;
}
handleDrawingUploadError(error) {
+ // TODO
if (error.status === 413) {
window.$events.emit('error', this.editor.config.text.serverUploadLimit);
} else {
// Make the editor full screen
fullScreen() {
+ // TODO
const container = this.editor.config.container;
const alreadyFullscreen = container.classList.contains('fullscreen');
container.classList.toggle('fullscreen', !alreadyFullscreen);
// Scroll to a specified text
scrollToText(searchText) {
+ // TODO
if (!searchText) {
return;
}
}
focus() {
+ // TODO
this.editor.cm.focus();
}
* @param {String} content
*/
insertContent(content) {
+ // TODO
this.editor.cm.replaceSelection(content);
}
* @param {String} content
*/
prependContent(content) {
+ // TODO
const cursorPos = this.editor.cm.getCursor('from');
const newContent = content + '\n' + this.editor.cm.getValue();
this.editor.cm.setValue(newContent);
* @param {String} content
*/
appendContent(content) {
+ // TODO
const cursorPos = this.editor.cm.getCursor('from');
const newContent = this.editor.cm.getValue() + '\n' + content;
this.editor.cm.setValue(newContent);
* @param {String} content
*/
replaceContent(content) {
+ // TODO
this.editor.cm.setValue(content);
}
* @param {String} replace
*/
findAndReplaceContent(search, replace) {
+ // TODO
const text = this.editor.cm.getValue();
const cursor = this.editor.cm.listSelections();
this.editor.cm.setValue(text.replace(search, replace));
* @param {String} newStart
*/
replaceLineStart(newStart) {
+ // TODO
const cursor = this.editor.cm.getCursor();
let lineContent = this.editor.cm.getLine(cursor.line);
const lineLen = lineContent.length;
* @param {String} end
*/
wrapLine(start, end) {
+ // TODO
const cursor = this.editor.cm.getCursor();
const lineContent = this.editor.cm.getLine(cursor.line);
const lineLen = lineContent.length;
* @param {String} end
*/
wrapSelection(start, end) {
+ // TODO
const selection = this.editor.cm.getSelection();
if (selection === '') return this.wrapLine(start, end);
}
replaceLineStartForOrderedList() {
+ // TODO
const cursor = this.editor.cm.getCursor();
const prevLineContent = this.editor.cm.getLine(cursor.line - 1) || '';
const listMatch = prevLineContent.match(/^(\s*)(\d)([).])\s/) || [];
return this.replaceLineStart(prefix);
}
+ /**
+ * 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() {
+ // TODO
+ const selectionRange = this.editor.cm.listSelections()[0];
+ const lineContent = this.editor.cm.getLine(selectionRange.anchor.line);
+ const lineLength = lineContent.length;
+ const contentRange = {
+ anchor: {line: selectionRange.anchor.line, ch: 0},
+ head: {line: selectionRange.anchor.line, ch: lineLength},
+ };
+
+ const formats = ['info', 'success', 'warning', 'danger'];
+ const joint = formats.join('|');
+ const regex = new RegExp(`class="((${joint})\\s+callout|callout\\s+(${joint}))"`, 'i');
+ const matches = regex.exec(lineContent);
+ const format = (matches ? (matches[2] || matches[3]) : '').toLowerCase();
+
+ if (format === formats[formats.length - 1]) {
+ this.wrapLine(`<p class="callout ${formats[formats.length - 1]}">`, '</p>');
+ } else if (format === '') {
+ this.wrapLine('<p class="callout info">', '</p>');
+ } else {
+ const newFormatIndex = formats.indexOf(format) + 1;
+ const newFormat = formats[newFormatIndex];
+ const newContent = lineContent.replace(matches[0], matches[0].replace(format, newFormat));
+ this.editor.cm.replaceRange(newContent, contentRange.anchor, contentRange.head);
+
+ const chDiff = newContent.length - lineContent.length;
+ selectionRange.anchor.ch += chDiff;
+ if (selectionRange.anchor !== selectionRange.head) {
+ selectionRange.head.ch += chDiff;
+ }
+ this.editor.cm.setSelection(selectionRange.anchor, selectionRange.head);
+ }
+ }
+
/**
* Handle image upload and add image into markdown content
* @param {File} file
*/
uploadImage(file) {
+ // TODO
if (file === null || file.type.indexOf('image') !== 0) return;
let ext = 'png';
});
}
- syncDisplayPosition() {
+ syncDisplayPosition(event) {
// Thanks to http://liuhao.im/english/2015/11/10/the-sync-scroll-of-markdown-editor-in-javascript.html
- const scroll = this.editor.cm.getScrollInfo();
- const atEnd = scroll.top + scroll.clientHeight === scroll.height;
+ const scrollEl = event.target;
+ const atEnd = Math.abs(scrollEl.scrollHeight - scrollEl.clientHeight - scrollEl.scrollTop) < 1;
if (atEnd) {
- editor.display.scrollToIndex(-1);
+ this.editor.display.scrollToIndex(-1);
return;
}
- const lineNum = this.editor.cm.lineAtHeight(scroll.top, 'local');
- const range = this.editor.cm.getRange({line: 0, ch: null}, {line: lineNum, ch: null});
+ const blockInfo = this.editor.cm.lineBlockAtHeight(scrollEl.scrollTop);
+ const range = this.editor.cm.state.sliceDoc(0, blockInfo.from);
const parser = new DOMParser();
const doc = parser.parseFromString(this.editor.markdown.render(range), 'text/html');
const totalLines = doc.documentElement.querySelectorAll('body > *');
- editor.display.scrollToIndex(totalLines.length);
+ this.editor.display.scrollToIndex(totalLines.length);
}
/**
* @param {Number} posY
*/
insertTemplate(templateId, posX, posY) {
+ // TODO
const cursorPos = this.editor.cm.coordsChar({left: posX, top: posY});
this.editor.cm.setCursor(cursorPos);
window.$http.get(`/templates/${templateId}`).then(resp => {
* @param {File[]} images
*/
insertClipboardImages(images) {
+ // TODO
const cursorPos = this.editor.cm.coordsChar({left: event.pageX, top: event.pageY});
this.editor.cm.setCursor(cursorPos);
for (const image of images) {
- editor.actions.uploadImage(image);
+ this.uploadImage(image);
}
}
}
\ No newline at end of file