client: fix markdown reference extensions

This commit is contained in:
fantasticit 2022-09-16 20:37:49 +08:00
parent c5812888f0
commit 57ac872948
1 changed files with 18 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import { Extension } from '@tiptap/core'; import { Editor as CoreEditor, Extension } from '@tiptap/core';
import { safeJSONParse } from 'helpers/json'; import { safeJSONParse } from 'helpers/json';
import { toggleMark } from 'prosemirror-commands'; import { toggleMark } from 'prosemirror-commands';
import { DOMParser, Fragment, Node, Schema } from 'prosemirror-model'; import { DOMParser, Fragment, Node, Schema } from 'prosemirror-model';
@ -53,7 +53,13 @@ interface IPasteOptions {
* *
* html prosemirror * html prosemirror
*/ */
htmlToProsemirror: (arg: { schema: Schema; html: string; needTitle: boolean; defaultTitle?: string }) => Node; htmlToProsemirror: (arg: {
editor: CoreEditor;
schema: Schema;
html: string;
needTitle: boolean;
defaultTitle?: string;
}) => Node;
/** /**
* markdown html * markdown html
@ -63,7 +69,7 @@ interface IPasteOptions {
/** /**
* markdown prosemirror * markdown prosemirror
*/ */
markdownToProsemirror: (arg: { schema: Schema; content: string; needTitle: boolean }) => Node; markdownToProsemirror: (arg: { editor: CoreEditor; schema: Schema; content: string; needTitle: boolean }) => Node;
/** /**
* prosemirror markdown * prosemirror markdown
@ -119,12 +125,6 @@ export const Paste = Extension.create<IPasteOptions>({
console.groupEnd(); console.groupEnd();
}); });
if (isInTitle(view.state)) {
if (text.length) {
return insertText(view, text);
}
}
// 直接复制节点 // 直接复制节点
if (node) { if (node) {
const json = safeJSONParse(node); const json = safeJSONParse(node);
@ -157,6 +157,7 @@ export const Paste = Extension.create<IPasteOptions>({
// TODO各家 office 套件标准不一样,是否需要做成用户自行选择粘贴 html 或者 图片? // TODO各家 office 套件标准不一样,是否需要做成用户自行选择粘贴 html 或者 图片?
if (html?.includes('urn:schemas-microsoft-com:office') || html?.includes('</table>')) { if (html?.includes('urn:schemas-microsoft-com:office') || html?.includes('</table>')) {
const doc = htmlToProsemirror({ const doc = htmlToProsemirror({
editor,
schema: editor.schema, schema: editor.schema,
html, html,
needTitle: hasTitleExtension && !hasTitle, needTitle: hasTitleExtension && !hasTitle,
@ -221,9 +222,11 @@ export const Paste = Extension.create<IPasteOptions>({
// 处理 markdown // 处理 markdown
if (markdownText || isMarkdown(text)) { if (markdownText || isMarkdown(text)) {
console.log(text);
event.preventDefault(); event.preventDefault();
const schema = view.props.state.schema; const schema = view.props.state.schema;
const doc = markdownToProsemirror({ const doc = markdownToProsemirror({
editor,
schema, schema,
content: normalizeMarkdown(markdownText || text), content: normalizeMarkdown(markdownText || text),
needTitle: hasTitleExtension && !hasTitle, needTitle: hasTitleExtension && !hasTitle,
@ -239,6 +242,12 @@ export const Paste = Extension.create<IPasteOptions>({
return true; return true;
} }
if (isInTitle(view.state)) {
if (text.length) {
return insertText(view, text);
}
}
return false; return false;
}, },
handleDrop: (view, event: any) => { handleDrop: (view, event: any) => {