update(iamge & link): 优化 markdown 解析和触发机制

This commit is contained in:
yanmao 2022-02-13 21:34:43 +08:00
parent df3c9d62ab
commit f8cd441ed8
3 changed files with 11 additions and 13 deletions

View File

@ -148,7 +148,7 @@ export default class<
init() { init() {
if (isEngine(this.editor)) { if (isEngine(this.editor)) {
this.editor.on('keydown:enter', (event) => this.markdown(event)); this.editor.on('keydown:space', (event) => this.markdown(event));
this.editor.on('drop:files', (files) => this.dropFiles(files)); this.editor.on('drop:files', (files) => this.dropFiles(files));
this.editor.on('paste:event', ({ files }) => this.editor.on('paste:event', ({ files }) =>
this.pasteFiles(files), this.pasteFiles(files),
@ -684,20 +684,18 @@ export default class<
const range = change.range.get(); const range = change.range.get();
if (!range.collapsed || change.isComposing() || !this.markdown) return; if (!range.collapsed || change.isComposing() || !this.markdown) return;
const blockApi = this.editor.block; const cloneRange = range.cloneRange();
const block = blockApi.closest(range.startNode); cloneRange.shrinkToTextNode();
const { startNode } = cloneRange;
if (!node.isRootBlock(block)) { if (!startNode.isText()) return;
return; const text = startNode.text();
} const match = /!\[([^\]]{0,})\]\((https?:\/\/[^\)]{5,})\)/.exec(text);
const chars = blockApi.getLeftText(block);
const match = /!\[([^\]]{0,})\]\((https?:\/\/[^\)]{5,})\)/.exec(chars);
if (match) { if (match) {
cloneRange.setStart(startNode, match.index);
change.range.select(cloneRange);
event.preventDefault(); event.preventDefault();
const splits = match[1].split('|'); const splits = match[1].split('|');
const src = match[2]; const src = match[2];
blockApi.removeLeftText(block);
const alignment = splits[1]; const alignment = splits[1];
if (alignment === 'center' || alignment === 'right') { if (alignment === 'center' || alignment === 'right') {
this.editor.command.execute('alignment', alignment); this.editor.command.execute('alignment', alignment);

View File

@ -41,7 +41,7 @@ export default class<
markdown = markdown =
this.options.markdown === undefined this.options.markdown === undefined
? '\\[(.+?)\\]\\(s*([\\S]+?)\\s*\\)$' ? '[^!]\\[(.+?)\\]\\(s*([\\S]+?)\\s*\\)$'
: this.options.markdown; : this.options.markdown;
init() { init() {

View File

@ -45,7 +45,7 @@ export default class<
markdown = markdown =
this.options.markdown === undefined this.options.markdown === undefined
? '\\[(.+?)\\]\\(\\s*([\\S]+?)\\s*\\)$' ? '[^!]\\[(.+?)\\]\\(\\s*([\\S]+?)\\s*\\)$'
: this.options.markdown; : this.options.markdown;
init() { init() {