Vanessa 2025-02-16 18:04:03 +08:00
parent d579e7511f
commit da11e337aa
6 changed files with 67 additions and 70 deletions

View file

@ -39,6 +39,27 @@ export const readText = () => {
return navigator.clipboard.readText();
};
export const readClipboard = async () => {
const text: { textPlain?: string, textHTML?: string } = {}
if (isInAndroid()) {
text.textPlain = window.JSAndroid.readClipboard();
} else if (isInHarmony()) {
text.textPlain = window.JSHarmony.readClipboard();
}
const clipboardContents = await navigator.clipboard.read();
for (const item of clipboardContents) {
if (item.types.includes("text/html")) {
const blob = await item.getType("text/html");
text.textHTML = await blob.text();
}
if (item.types.includes("text/plain")) {
const blob = await item.getType("text/plain");
text.textPlain = await blob.text();
}
}
return text;
}
export const writeText = (text: string) => {
let range: Range;
if (getSelection().rangeCount > 0) {
@ -145,7 +166,7 @@ export const isWin11 = async () => {
const ua = await (navigator as any).userAgentData.getHighEntropyValues(["platformVersion"]);
if ((navigator as any).userAgentData.platform === "Windows") {
if (parseInt(ua.platformVersion.split(".")[0]) >= 13) {
return true;
return true;
}
}
return false;

View file

@ -128,7 +128,7 @@ export const pasteEscaped = async (protyle: IProtyle, nodeElement: Element) => {
.replace(/\|/g, "\\|")
.replace(/\./g, "\\.");
// 转义文本不能使用 DOM 结构 https://github.com/siyuan-note/siyuan/issues/11778
pasteText(protyle, clipText, nodeElement, false);
paste(protyle, {textPlain: clipText, target: nodeElement as HTMLElement});
} catch (e) {
console.log(e);
}
@ -228,57 +228,6 @@ export const restoreLuteMarkdownSyntax = (protyle: IProtyle) => {
protyle.lute.SetMark(window.siyuan.config.editor.markdown.inlineMark);
};
export const pasteText = async (protyle: IProtyle, textPlain: string, nodeElement: Element, toBlockDOM = true) => {
if (protyle && protyle.app && protyle.app.plugins) {
for (let i = 0; i < protyle.app.plugins.length; i++) {
const response: IObject = await new Promise((resolve) => {
const emitResult = protyle.app.plugins[i].eventBus.emit("paste", {
protyle,
resolve,
textHTML: textPlain,
textPlain,
siyuanHTML: textPlain,
files: []
});
if (emitResult) {
resolve(undefined);
}
});
if (response?.textPlain) {
textPlain = response.textPlain;
}
}
}
const range = getEditorRange(protyle.wysiwyg.element);
if (nodeElement.getAttribute("data-type") === "NodeCodeBlock") {
// 粘贴在代码位置
insertHTML(textPlain, protyle);
return;
}
if (range.toString() !== "") {
if (isDynamicRef(textPlain)) {
textPlain = textPlain.replace(/'.+'\)\)$/, ` "${range.toString()}"))`);
} else if (isFileAnnotation(textPlain)) {
textPlain = textPlain.replace(/".+">>$/, `"${range.toString()}">>`);
} else {
const linkDest = protyle.lute.GetLinkDest(textPlain);
if (linkDest) {
textPlain = `[${range.toString()}](${linkDest})`;
}
}
}
insertHTML(toBlockDOM ? protyle.lute.Md2BlockDOM(textPlain) : textPlain, protyle, false, false, true);
blockRender(protyle, protyle.wysiwyg.element);
processRender(protyle.wysiwyg.element);
highlightRender(protyle.wysiwyg.element);
avRender(protyle.wysiwyg.element, protyle);
filterClipboardHint(protyle, textPlain);
scrollCenter(protyle, undefined, false, "smooth");
};
const readLocalFile = async (protyle: IProtyle, localFiles: string[]) => {
if (protyle && protyle.app && protyle.app.plugins) {
for (let i = 0; i < protyle.app.plugins.length; i++) {
@ -303,9 +252,16 @@ const readLocalFile = async (protyle: IProtyle, localFiles: string[]) => {
uploadLocalFiles(localFiles, protyle, true);
};
export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEvent) & { target: HTMLElement }) => {
event.stopPropagation();
event.preventDefault();
export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEvent | {
textHTML?: string,
textPlain?: string,
}) & {
target: HTMLElement
}) => {
if ("clipboardData" in event || "dataTransfer" in event) {
event.stopPropagation();
event.preventDefault();
}
let textHTML: string;
let textPlain: string;
let siyuanHTML: string;
@ -315,13 +271,16 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
textPlain = event.clipboardData.getData("text/plain");
siyuanHTML = event.clipboardData.getData("text/siyuan");
files = event.clipboardData.files;
} else {
} else if ("dataTransfer" in event) {
textHTML = event.dataTransfer.getData("text/html");
textPlain = event.dataTransfer.getData("text/plain");
siyuanHTML = event.dataTransfer.getData("text/siyuan");
if (event.dataTransfer.types[0] === "Files") {
files = event.dataTransfer.items;
}
} else {
textHTML = event.textHTML
textPlain = event.textPlain
}
// Improve the pasting of selected text in PDF rectangular annotation https://github.com/siyuan-note/siyuan/issues/11629
@ -585,3 +544,4 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
scrollCenter(protyle, undefined, false, "smooth");
}
};