Parcourir la source

:bug: fix https://github.com/siyuan-note/siyuan/issues/10996

Vanessa il y a 1 an
Parent
commit
f822e4d69e

+ 12 - 1
app/src/protyle/render/av/cell.ts

@@ -12,6 +12,7 @@ import {getColIconByType} from "./col";
 import {genAVValueHTML} from "./blockAttr";
 import {Constants} from "../../../constants";
 import {hintRef} from "../../hint/extend";
+import {pathPosix} from "../../../util/pathName";
 
 const renderCellURL = (urlContent: string) => {
     let host = urlContent;
@@ -133,7 +134,7 @@ export const genCellValue = (colType: TAVCol, value: string | any) => {
         type: colType,
         [colType === "select" ? "mSelect" : colType]: value as IAVCellDateValue
     };
-    if (typeof value === "string" && value && colType !== "mAsset") {
+    if (typeof value === "string" && value) {
         if (colType === "number") {
             cellValue = {
                 type: colType,
@@ -181,6 +182,16 @@ export const genCellValue = (colType: TAVCol, value: string | any) => {
                 type: colType,
                 relation: {blockIDs: [value], contents: []}
             };
+        } else if (colType === "mAsset") {
+            const type = pathPosix().extname(value).toLowerCase();
+            cellValue = {
+                type: colType,
+                mAsset: [{
+                    type: Constants.SIYUAN_ASSETS_IMAGE.includes(type) ? "image" : "file",
+                    content: value,
+                    name: "",
+                }]
+            };
         }
     } else if (typeof value === "undefined" || !value) {
         if (colType === "number") {

+ 9 - 6
app/src/protyle/render/av/keydown.ts

@@ -23,19 +23,22 @@ export const avKeydown = (event: KeyboardEvent, nodeElement: HTMLElement, protyl
         if (!rowElement) {
             return false;
         }
-        if (event.key === "Backspace" || event.key === "Delete") {
-            updateCellsValue(protyle, nodeElement, undefined, Array.from(nodeElement.querySelectorAll(".av__cell--active, .av__cell--select")));
-            event.preventDefault();
-            return true;
-        }
         const avPanelElement = document.querySelector(".av__panel");
         if (avPanelElement &&
-            (event.key === "Escape" || event.key.startsWith("ArrowLeft") || event.key === "Enter" || matchHotKey("⇥", event) || matchHotKey("⇧⇥", event))) {
+            (event.key === "Backspace" || event.key === "Delete" || event.key === "Escape" ||
+                event.key.startsWith("ArrowLeft") || event.key === "Enter" || matchHotKey("⇥", event) ||
+                matchHotKey("⇧⇥", event))) {
             avPanelElement.remove();
             event.preventDefault();
             event.stopPropagation();
             return true;
         }
+        // 需在 avPanelElement 之后,否则点击资源单元格后删除,资源面板不会更新
+        if (event.key === "Backspace" || event.key === "Delete") {
+            updateCellsValue(protyle, nodeElement, undefined, Array.from(nodeElement.querySelectorAll(".av__cell--active, .av__cell--select")));
+            event.preventDefault();
+            return true;
+        }
         if (event.key === "Escape") {
             selectCellElement.classList.remove("av__cell--select", "av__cell--active");
             selectCellElement.querySelector(".av__drag-fill")?.remove();

+ 39 - 8
app/src/protyle/upload/index.ts

@@ -8,7 +8,7 @@ import {pathPosix} from "../../util/pathName";
 import {genAssetHTML} from "../../asset/renderAssets";
 import {hasClosestBlock} from "../util/hasClosest";
 import {getContenteditableElement} from "../wysiwyg/getBlock";
-import {updateCellsValue} from "../render/av/cell";
+import {getTypeByCellElement, updateCellsValue} from "../render/av/cell";
 
 export class Upload {
     public element: HTMLElement;
@@ -143,16 +143,47 @@ const genUploadedLabel = (responseText: string, protyle: IProtyle) => {
             }
         }
     });
+
     if ((nodeElement && nodeElement.classList.contains("av"))) {
-        updateCellsValue(protyle, nodeElement, avAssets);
-        document.querySelector(".av__panel")?.remove();
-        return;
+        const cellElements: HTMLElement[] = []
+        nodeElement.querySelectorAll(".av__row--select:not(.av__row--header)").forEach(item => {
+            item.querySelectorAll(".av__cell").forEach((cellItem: HTMLElement) => {
+                if (getTypeByCellElement(cellItem) === "mAsset") {
+                    cellElements.push(cellItem);
+                }
+            })
+        })
+        if (cellElements.length === 0) {
+            protyle.wysiwyg.element.querySelectorAll(".av__cell--active").forEach((item: HTMLElement) => {
+                if (getTypeByCellElement(item) === "mAsset") {
+                    cellElements.push(item);
+                }
+            })
+        }
+        if (cellElements.length > 0) {
+            updateCellsValue(protyle, nodeElement, avAssets, cellElements);
+            document.querySelector(".av__panel")?.remove();
+            return;
+        } else {
+            return;
+        }
     }
+
     if (document.querySelector(".av__panel")) {
-        const blockElement = hasClosestBlock(protyle.wysiwyg.element.querySelector(".av__cell--select"));
-        if (blockElement) {
-            updateCellsValue(protyle, blockElement, avAssets);
-            document.querySelector(".av__panel")?.remove();
+        const cellElements: HTMLElement[] = []
+        protyle.wysiwyg.element.querySelectorAll(".av__cell--active").forEach((item: HTMLElement) => {
+            if (getTypeByCellElement(item) === "mAsset") {
+                cellElements.push(item);
+            }
+        })
+        if (cellElements.length > 0) {
+            const blockElement = hasClosestBlock(cellElements[0]);
+            if (blockElement) {
+                updateCellsValue(protyle, blockElement, avAssets, cellElements);
+                document.querySelector(".av__panel")?.remove();
+                return;
+            }
+        } else {
             return;
         }
     }