mirror of https://github.com/fantasticit/think.git
tiptap: fix paste without title extension
This commit is contained in:
parent
e50b81cb4f
commit
c6fc458ae3
|
@ -15,6 +15,8 @@ import {
|
||||||
normalizeMarkdown,
|
normalizeMarkdown,
|
||||||
} from 'tiptap/prose-utils';
|
} from 'tiptap/prose-utils';
|
||||||
|
|
||||||
|
import { TitleExtensionName } from './title';
|
||||||
|
|
||||||
interface IPasteOptions {
|
interface IPasteOptions {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -26,7 +28,7 @@ interface IPasteOptions {
|
||||||
* 将 markdown 转换为 prosemirror 节点
|
* 将 markdown 转换为 prosemirror 节点
|
||||||
* FIXME: prosemirror 节点的类型是什么?
|
* FIXME: prosemirror 节点的类型是什么?
|
||||||
*/
|
*/
|
||||||
markdownToProsemirror: (arg: { schema: Schema; content: string; hasTitle: boolean }) => unknown;
|
markdownToProsemirror: (arg: { schema: Schema; content: string; needTitle: boolean }) => unknown;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将 prosemirror 转换为 markdown
|
* 将 prosemirror 转换为 markdown
|
||||||
|
@ -100,6 +102,13 @@ export const Paste = Extension.create<IPasteOptions>({
|
||||||
|
|
||||||
const { markdownToProsemirror } = extensionThis.options;
|
const { markdownToProsemirror } = extensionThis.options;
|
||||||
|
|
||||||
|
console.log('p', {
|
||||||
|
text,
|
||||||
|
html,
|
||||||
|
node,
|
||||||
|
markdownText,
|
||||||
|
});
|
||||||
|
|
||||||
// 直接复制节点
|
// 直接复制节点
|
||||||
if (node) {
|
if (node) {
|
||||||
const json = safeJSONParse(node);
|
const json = safeJSONParse(node);
|
||||||
|
@ -159,13 +168,18 @@ export const Paste = Extension.create<IPasteOptions>({
|
||||||
if (markdownText || isMarkdown(text) || html.length === 0 || pasteCodeLanguage === 'markdown') {
|
if (markdownText || isMarkdown(text) || html.length === 0 || pasteCodeLanguage === 'markdown') {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const firstNode = view.props.state.doc.content.firstChild;
|
const firstNode = view.props.state.doc.content.firstChild;
|
||||||
|
const hasTitleExtension = !!editor.extensionManager.extensions.find(
|
||||||
|
(extension) => extension.name === TitleExtensionName
|
||||||
|
);
|
||||||
const hasTitle = isTitleNode(firstNode) && firstNode.content.size > 0;
|
const hasTitle = isTitleNode(firstNode) && firstNode.content.size > 0;
|
||||||
const schema = view.props.state.schema;
|
const schema = view.props.state.schema;
|
||||||
const doc = markdownToProsemirror({
|
const doc = markdownToProsemirror({
|
||||||
schema,
|
schema,
|
||||||
content: normalizeMarkdown(markdownText || text),
|
content: normalizeMarkdown(markdownText || text),
|
||||||
hasTitle,
|
needTitle: hasTitleExtension && !hasTitle,
|
||||||
});
|
});
|
||||||
|
console.log('p', markdownText, text);
|
||||||
|
|
||||||
let tr = view.state.tr;
|
let tr = view.state.tr;
|
||||||
const selection = tr.selection;
|
const selection = tr.selection;
|
||||||
view.state.doc.nodesBetween(selection.from, selection.to, (node, position) => {
|
view.state.doc.nodesBetween(selection.from, selection.to, (node, position) => {
|
||||||
|
|
|
@ -18,9 +18,13 @@ declare module '@tiptap/core' {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const TitleExtensionName = 'title';
|
||||||
|
|
||||||
export const Title = Node.create<TitleOptions>({
|
export const Title = Node.create<TitleOptions>({
|
||||||
name: 'title',
|
name: TitleExtensionName,
|
||||||
content: 'inline*',
|
content: 'inline*',
|
||||||
|
group: 'block',
|
||||||
|
selectable: true,
|
||||||
|
|
||||||
addOptions() {
|
addOptions() {
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -25,7 +25,7 @@ const extractImage = (html) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
// 将 markdown 字符串转换为 ProseMirror JSONDocument
|
// 将 markdown 字符串转换为 ProseMirror JSONDocument
|
||||||
export const markdownToProsemirror = ({ schema, content, hasTitle }) => {
|
export const markdownToProsemirror = ({ schema, content, needTitle }) => {
|
||||||
const html = markdownToHTML(content);
|
const html = markdownToHTML(content);
|
||||||
|
|
||||||
if (!html) return null;
|
if (!html) return null;
|
||||||
|
@ -34,7 +34,7 @@ export const markdownToProsemirror = ({ schema, content, hasTitle }) => {
|
||||||
const { body } = parser.parseFromString(extractImage(html), 'text/html');
|
const { body } = parser.parseFromString(extractImage(html), 'text/html');
|
||||||
|
|
||||||
body.append(document.createComment(content));
|
body.append(document.createComment(content));
|
||||||
const node = htmlToPromsemirror(body, !hasTitle);
|
const node = htmlToPromsemirror(body, needTitle);
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue