2019-06-08 00:46:19 +08:00
|
|
|
import {debounce} from "../services/util";
|
2022-11-16 21:04:22 +08:00
|
|
|
import {Component} from "./component";
|
2022-11-27 00:43:28 +08:00
|
|
|
import {init as initEditor} from "../markdown/editor";
|
2018-01-21 04:40:21 +08:00
|
|
|
|
2022-11-16 21:04:22 +08:00
|
|
|
export class MarkdownEditor extends Component {
|
2017-09-23 19:24:06 +08:00
|
|
|
|
2020-07-06 04:18:17 +08:00
|
|
|
setup() {
|
|
|
|
this.elem = this.$el;
|
2019-03-09 05:32:31 +08:00
|
|
|
|
2020-07-06 04:18:17 +08:00
|
|
|
this.pageId = this.$opts.pageId;
|
|
|
|
this.textDirection = this.$opts.textDirection;
|
2020-11-22 01:52:49 +08:00
|
|
|
this.imageUploadErrorText = this.$opts.imageUploadErrorText;
|
2021-05-27 01:23:27 +08:00
|
|
|
this.serverUploadLimitText = this.$opts.serverUploadLimitText;
|
2019-03-09 05:32:31 +08:00
|
|
|
|
2022-11-27 00:43:28 +08:00
|
|
|
this.display = this.$refs.display;
|
|
|
|
this.input = this.$refs.input;
|
2017-09-23 19:24:06 +08:00
|
|
|
|
2022-11-27 00:43:28 +08:00
|
|
|
this.editor = null;
|
|
|
|
initEditor({
|
|
|
|
pageId: this.pageId,
|
|
|
|
container: this.elem,
|
|
|
|
displayEl: this.display,
|
|
|
|
inputEl: this.input,
|
|
|
|
drawioUrl: this.getDrawioUrl(),
|
|
|
|
text: {
|
|
|
|
serverUploadLimit: this.serverUploadLimitText,
|
|
|
|
imageUploadError: this.imageUploadErrorText,
|
|
|
|
}
|
|
|
|
}).then(editor => {
|
|
|
|
this.editor = editor;
|
|
|
|
this.setupListeners();
|
|
|
|
this.emitEditorEvents();
|
|
|
|
this.scrollToTextIfNeeded();
|
|
|
|
this.editor.actions.updateAndRender();
|
2022-02-08 19:10:01 +08:00
|
|
|
});
|
2022-11-27 00:43:28 +08:00
|
|
|
}
|
2017-09-23 19:24:06 +08:00
|
|
|
|
2022-11-27 00:43:28 +08:00
|
|
|
emitEditorEvents() {
|
2020-07-06 04:18:17 +08:00
|
|
|
window.$events.emitPublic(this.elem, 'editor-markdown::setup', {
|
2022-11-27 00:43:28 +08:00
|
|
|
markdownIt: this.editor.markdown.getRenderer(),
|
2019-10-17 01:01:35 +08:00
|
|
|
displayEl: this.display,
|
2022-11-27 00:43:28 +08:00
|
|
|
codeMirrorInstance: this.editor.cm,
|
2019-10-17 01:01:35 +08:00
|
|
|
});
|
2017-09-23 19:24:06 +08:00
|
|
|
}
|
|
|
|
|
2022-11-27 00:43:28 +08:00
|
|
|
setupListeners() {
|
2017-09-23 19:24:06 +08:00
|
|
|
|
|
|
|
// Button actions
|
|
|
|
this.elem.addEventListener('click', event => {
|
|
|
|
let button = event.target.closest('button[data-action]');
|
|
|
|
if (button === null) return;
|
|
|
|
|
2022-11-27 00:43:28 +08:00
|
|
|
const action = button.getAttribute('data-action');
|
|
|
|
if (action === 'insertImage') this.editor.actions.insertImage();
|
|
|
|
if (action === 'insertLink') this.editor.actions.showLinkSelector();
|
2019-04-20 20:12:35 +08:00
|
|
|
if (action === 'insertDrawing' && (event.ctrlKey || event.metaKey)) {
|
2022-11-27 00:43:28 +08:00
|
|
|
this.editor.actions.showImageManager();
|
2018-05-27 21:33:50 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-11-27 00:43:28 +08:00
|
|
|
if (action === 'insertDrawing') this.editor.actions.startDrawing();
|
|
|
|
if (action === 'fullscreen') this.editor.actions.fullScreen();
|
2017-09-23 19:24:06 +08:00
|
|
|
});
|
|
|
|
|
2019-04-14 19:04:20 +08: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');
|
2022-11-27 00:43:28 +08:00
|
|
|
for (const activeElem of currentActiveSections) {
|
2019-04-14 19:04:20 +08:00
|
|
|
activeElem.classList.remove('active');
|
|
|
|
}
|
|
|
|
|
|
|
|
toolbarLabel.closest('.markdown-editor-wrap').classList.add('active');
|
|
|
|
});
|
|
|
|
|
2022-11-27 00:43:28 +08:00
|
|
|
// Refresh CodeMirror on container resize
|
|
|
|
const resizeDebounced = debounce(() => this.editor.cm.refresh(), 100, false);
|
|
|
|
const observer = new ResizeObserver(resizeDebounced);
|
|
|
|
observer.observe(this.elem);
|
|
|
|
}
|
2019-09-01 17:51:52 +08:00
|
|
|
|
2022-11-27 00:43:28 +08:00
|
|
|
scrollToTextIfNeeded() {
|
2019-09-01 17:51:52 +08:00
|
|
|
const queryParams = (new URL(window.location)).searchParams;
|
|
|
|
const scrollText = queryParams.get('content-text');
|
|
|
|
if (scrollText) {
|
2022-11-27 00:43:28 +08:00
|
|
|
this.editor.actions.scrollToText(scrollText);
|
2017-09-23 19:24:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 00:43:28 +08:00
|
|
|
/**
|
|
|
|
* Get the URL for the configured drawio instance.
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2020-04-06 00:27:16 +08:00
|
|
|
getDrawioUrl() {
|
2022-11-27 00:43:28 +08:00
|
|
|
const drawioAttrEl = document.querySelector('[drawio-url]');
|
|
|
|
if (!drawioAttrEl) {
|
|
|
|
return '';
|
2019-03-09 05:32:31 +08:00
|
|
|
}
|
|
|
|
|
2022-11-27 00:43:28 +08:00
|
|
|
return drawioAttrEl.getAttribute('drawio-url') || '';
|
2018-01-21 04:40:21 +08:00
|
|
|
}
|
|
|
|
|
2017-09-23 19:24:06 +08:00
|
|
|
}
|