fix: fix js error

This commit is contained in:
fantasticit
2025-03-23 14:25:08 +08:00
parent 34a5437e6b
commit 959a6c22ee
2 changed files with 65 additions and 75 deletions
@@ -106,7 +106,7 @@ export const DocumentFullscreen: React.FC<IProps> = ({ data }) => {
const docJSON = safeJSONParse(data.content, { default: {} }).default;
const titleNode = docJSON.content.find((item) => item.type === 'title');
docJSON.content = docJSON.content.filter((item) => item.type !== 'title');
setCover(titleNode.attrs.cover ?? '');
setCover(titleNode?.attrs?.cover ?? '');
editor.commands.setContent(docJSON);
}, [editor, data, visible]);
@@ -13,10 +13,9 @@ import {
safePos,
} from 'tiptap/prose-utils';
import { safeJSONParse } from 'helpers/json';
import { toggleMark } from 'prosemirror-commands';
import { DOMParser as PMDOMParser, Fragment, Node, Schema } from 'prosemirror-model';
import { EditorState, Plugin, PluginKey, TextSelection } from 'prosemirror-state';
import { Plugin, PluginKey } from 'prosemirror-state';
import { uploadFile } from 'services/file';
const reuploadImageAndUpdateSrc = async (img: HTMLImageElement) => {
@@ -38,7 +37,7 @@ const reuploadImageAndUpdateSrc = async (img: HTMLImageElement) => {
const htmlToProsemirror = async (editor: CoreEditor, html, isPasteMarkdown = false) => {
const firstNode = editor.view.state.doc.content.firstChild;
const shouldInsertTitleText = !!(firstNode?.textContent?.length <= 0 ?? true);
const shouldInsertTitleText = !!(firstNode?.textContent?.length <= 0);
if (!shouldInsertTitleText && !isPasteMarkdown) return false;
@@ -129,9 +128,10 @@ export const Paste = Extension.create<IPasteOptions>({
name: 'paste',
priority: EXTENSION_PRIORITY_HIGHEST,
// @ts-ignore
addOptions() {
return {
htmlToProsemirror: (arg) => '',
htmlToProsemirror: (arg) => arg,
markdownToHTML: (arg) => arg,
markdownToProsemirror: (arg) => arg.content,
prosemirrorToMarkdown: (arg) => String(arg.content),
@@ -150,87 +150,77 @@ export const Paste = Extension.create<IPasteOptions>({
new Plugin({
key: new PluginKey('paste'),
props: {
// handlePaste: (view, event: ClipboardEvent) => {
// if (view.props.editable && !view.props.editable(view.state)) {
// return false;
// }
handlePaste: (view, event: ClipboardEvent) => {
if (view.props.editable && !view.props.editable(view.state)) {
return false;
}
// if (!event.clipboardData) return false;
if (!event.clipboardData) return false;
// const files = Array.from(event.clipboardData.files);
// const text = event.clipboardData.getData('text/plain');
// const html = event.clipboardData.getData('text/html');
// const vscode = event.clipboardData.getData('vscode-editor-data');
// const node = event.clipboardData.getData('text/node');
// const markdownText = event.clipboardData.getData('text/markdown');
// const { state, dispatch } = view;
const files = Array.from(event.clipboardData.files);
const text = event.clipboardData.getData('text/plain');
const html = event.clipboardData.getData('text/html');
const vscode = event.clipboardData.getData('vscode-editor-data');
const node = event.clipboardData.getData('text/node');
const markdownText = event.clipboardData.getData('text/markdown');
const { state, dispatch } = view;
// debug(() => {
// console.group('paste');
// console.log({ text, vscode, node, markdownText, files });
// console.log(html);
// console.groupEnd();
// });
debug(() => {
console.group('paste');
console.log({ text, vscode, node, markdownText, files });
console.log(html);
console.groupEnd();
});
// // 直接复制节点
// if (node) {
// const json = safeJSONParse(node);
// const tr = view.state.tr;
// const selection = tr.selection;
// view.dispatch(tr.insert(selection.from - 1, view.state.schema.nodeFromJSON(json)).scrollIntoView());
// return true;
// }
// 链接
if (isValidURL(text)) {
if (!state.selection.empty) {
toggleMark(this.editor.schema.marks.link, { href: text })(state, dispatch);
return true;
}
// // 链接
// if (isValidURL(text)) {
// if (!state.selection.empty) {
// toggleMark(this.editor.schema.marks.link, { href: text })(state, dispatch);
// return true;
// }
const transaction = view.state.tr
.insertText(text, state.selection.from, state.selection.to)
.addMark(
state.selection.from,
state.selection.to + text.length,
state.schema.marks.link.create({ href: text })
);
view.dispatch(transaction);
return true;
}
// const transaction = view.state.tr
// .insertText(text, state.selection.from, state.selection.to)
// .addMark(
// state.selection.from,
// state.selection.to + text.length,
// state.schema.marks.link.create({ href: text })
// );
// view.dispatch(transaction);
// return true;
// }
// 粘贴代码
if (isInCode(view.state)) {
event.preventDefault();
view.dispatch(view.state.tr.insertText(text).scrollIntoView());
return true;
}
// // 粘贴代码
// if (isInCode(view.state)) {
// event.preventDefault();
// view.dispatch(view.state.tr.insertText(text).scrollIntoView());
// return true;
// }
const vscodeMeta = vscode ? JSON.parse(vscode) : undefined;
const pasteCodeLanguage = vscodeMeta?.mode;
// const vscodeMeta = vscode ? JSON.parse(vscode) : undefined;
// const pasteCodeLanguage = vscodeMeta?.mode;
const { markdownToHTML } = extensionThis.options;
// if (html.length > 0) {
// return htmlToProsemirror(editor, html);
// }
if ((markdownText || isMarkdown(text)) && markdownToHTML) {
event.preventDefault();
const html = markdownToHTML(normalizeMarkdown(markdownText || text));
if (html && html.length) {
htmlToProsemirror(editor, html, true);
return true;
}
}
// const { markdownToHTML } = extensionThis.options;
if (files.length) {
event.preventDefault();
files.forEach((file) => {
handleFileEvent({ editor, file });
});
return true;
}
// if ((markdownText || isMarkdown(text)) && markdownToHTML) {
// event.preventDefault();
// const html = markdownToHTML(normalizeMarkdown(markdownText || text));
// if (html && html.length) return htmlToProsemirror(editor, html, true);
// }
// if (files.length) {
// event.preventDefault();
// files.forEach((file) => {
// handleFileEvent({ editor, file });
// });
// return true;
// }
// return false;
// },
return false;
},
handleDrop: (view, event: any) => {
if (view.props.editable && !view.props.editable(view.state)) {
return false;