From dde4c66033f60d713b65b5fadc2b38f91cc74ee9 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Mon, 13 Mar 2023 11:42:18 +0800 Subject: [PATCH] :art: fix https://github.com/siyuan-note/siyuan/issues/7624 --- app/src/protyle/upload/index.ts | 36 ++++++++++++++++++++++-------- app/src/protyle/util/insertHTML.ts | 2 +- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/app/src/protyle/upload/index.ts b/app/src/protyle/upload/index.ts index 3f7ab60c2..0bf4575fc 100644 --- a/app/src/protyle/upload/index.ts +++ b/app/src/protyle/upload/index.ts @@ -6,6 +6,8 @@ import {fetchPost} from "../../util/fetch"; import {getEditorRange} from "../util/selection"; import {pathPosix} from "../../util/pathName"; import {genAssetHTML} from "../../asset/renderAssets"; +import {hasClosestBlock} from "../util/hasClosest"; +import {getContenteditableElement} from "../wysiwyg/getBlock"; export class Upload { public element: HTMLElement; @@ -96,7 +98,25 @@ const genUploadedLabel = (responseText: string, protyle: IProtyle) => { if (errorTip) { showMessage(errorTip); } - + let insertBlock = true; + const range = getEditorRange(protyle.wysiwyg.element); + if (range.toString() === "" && range.startContainer.nodeType === 3 && protyle.toolbar.getCurrentType(range).length > 0) { + // 防止链接插入其他元素中 https://ld246.com/article/1676003478664 + range.setEndAfter(range.startContainer.parentElement); + range.collapse(false); + } + // https://github.com/siyuan-note/siyuan/issues/7624 + const nodeElement = hasClosestBlock(range.startContainer) + if (nodeElement) { + if (nodeElement.classList.contains("table")) { + insertBlock = false; + } else { + const editableElement = getContenteditableElement(nodeElement); + if (editableElement && editableElement.textContent !== "" && nodeElement.classList.contains("p")) { + insertBlock = false; + } + } + } let succFileText = ""; const keys = Object.keys(response.data.succMap); keys.forEach((key, index) => { @@ -106,17 +126,15 @@ const genUploadedLabel = (responseText: string, protyle: IProtyle) => { succFileText += genAssetHTML(type, path, filename.substring(0, filename.length - type.length), filename); if (!Constants.SIYUAN_ASSETS_AUDIO.includes(type) && !Constants.SIYUAN_ASSETS_VIDEO.includes(type) && keys.length - 1 !== index) { - succFileText += "\n\n"; + if (insertBlock) { + succFileText += "\n\n"; + } else { + succFileText += "\n"; + } } }); - const range = getEditorRange(protyle.wysiwyg.element); - if (range.toString() === "" && range.startContainer.nodeType === 3 && protyle.toolbar.getCurrentType(range).length > 0) { - // 防止链接插入其他元素中 https://ld246.com/article/1676003478664 - range.setEndAfter(range.startContainer.parentElement); - range.collapse(false); - } // 避免插入代码块中,其次因为都要独立成块 https://github.com/siyuan-note/siyuan/issues/7607 - insertHTML(succFileText, protyle, true); + insertHTML(succFileText, protyle, insertBlock); }; export const uploadLocalFiles = (files: string[], protyle: IProtyle, isUpload: boolean) => { diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.ts index 4561fc37f..755066a63 100644 --- a/app/src/protyle/util/insertHTML.ts +++ b/app/src/protyle/util/insertHTML.ts @@ -187,7 +187,7 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false, lastElement = item; } }); - if (editableElement && editableElement.textContent === "") { + if (editableElement && editableElement.textContent === "" && blockElement.classList.contains("p")) { // 选中当前块所有内容粘贴再撤销会导致异常 https://ld246.com/article/1662542137636 doOperation.find((item, index) => { if (item.id === id) {