Merge pull request #178 from fantasticit/fix/0824

This commit is contained in:
fantasticit 2022-08-24 11:53:44 +08:00 committed by GitHub
commit 7fc999ea4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 9 deletions

View File

@ -8,6 +8,7 @@ import {
debug, debug,
handleFileEvent, handleFileEvent,
isInCode, isInCode,
isInTitle,
isMarkdown, isMarkdown,
isTitleNode, isTitleNode,
isValidURL, isValidURL,
@ -16,6 +17,37 @@ import {
import { TitleExtensionName } from './title'; import { TitleExtensionName } from './title';
function insertText(view, text) {
const texts = text.split('\n').filter(Boolean);
event.preventDefault();
view.dispatch(view.state.tr.insertText(texts[0]));
const json = {
type: 'doc',
content: [{ type: 'title', attrs: { cover: '' }, content: [{ type: 'text', text: texts[0] }] }].concat(
// @ts-ignore
texts.slice(1).map((t) => {
return {
type: 'paragraph',
attrs: { indent: 0, textAlign: 'left' },
content: [{ type: 'text', text: t }],
};
})
),
};
let tr = view.state.tr;
const selection = tr.selection;
view.state.doc.nodesBetween(selection.from, selection.to, (node, position) => {
const startPosition = Math.min(position, selection.from) || 0;
const endPosition = Math.min(position + node.nodeSize, selection.to);
tr = tr.replaceWith(startPosition, endPosition, view.state.schema.nodeFromJSON(json));
});
view.dispatch(tr.scrollIntoView());
return true;
}
interface IPasteOptions { interface IPasteOptions {
/** /**
* *
@ -87,6 +119,12 @@ 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);
@ -202,9 +240,7 @@ export const Paste = Extension.create<IPasteOptions>({
} }
if (text.length !== 0) { if (text.length !== 0) {
event.preventDefault(); return insertText(view, text);
view.dispatch(view.state.tr.insertText(text));
return true;
} }
return false; return false;

View File

@ -160,19 +160,25 @@ export const Title = Node.create<TitleOptions>({
} }
} }
const newTitleNodes = (newState.tr.doc.content.content || []).filter((item) => item.type.name === this.name); const filterTitleNode = (nodes, equal = true) => {
return (nodes || [])
.filter(Boolean)
.filter((item) => (equal ? item.type.name === this.name : item.type.name !== this.name));
};
const newTitleNodes = filterTitleNode(newState.tr.doc.content.content || []);
if (newTitleNodes.length > 1) { if (newTitleNodes.length > 1) {
const oldTitleNode = (oldState.tr.doc.content.content || []).filter((item) => item.type.name === this.name); const oldTitleNodes = filterTitleNode(oldState.tr.doc.content.content || []);
const allTitleNodes = [...oldTitleNodes, ...newTitleNodes].filter(Boolean);
const nextNewTitleNode = allTitleNodes.find((node) => node.nodeSize > 2) || allTitleNodes[0];
const otherNewNodes = (newState.tr.doc.content.content || []).filter( const otherNewNodes = filterTitleNode(newState.tr.doc.content.content || [], false);
(item) => item.type.name !== this.name
);
const fixedDoc = { const fixedDoc = {
...newState.tr.doc.toJSON(), ...newState.tr.doc.toJSON(),
content: [].concat( content: [].concat(
((oldTitleNode && oldTitleNode[0]) || newTitleNodes[0]).toJSON(), nextNewTitleNode.toJSON(),
otherNewNodes.map((node) => node.toJSON()) otherNewNodes.map((node) => node.toJSON())
), ),
}; };