瀏覽代碼

:art: fix https://github.com/siyuan-note/siyuan/issues/7471

Vanessa 2 年之前
父節點
當前提交
7334b5f615

+ 1 - 0
app/appearance/langs/en_US.json

@@ -1,4 +1,5 @@
 {
+  "pdfIsLoading": "PDF is loading, please try again later",
   "addToDeck": "Add to Deck...",
   "quickMakeCard": "Quick make card",
   "allAttrs": "All attribute names and attribute values",

+ 1 - 0
app/appearance/langs/es_ES.json

@@ -1,4 +1,5 @@
 {
+  "pdfIsLoading": "El PDF se está cargando, inténtalo de nuevo más tarde",
   "addToDeck": "Agregar a la plataforma...",
   "quickMakeCard": "Tarjeta de creación rápida",
   "allAttrs": "Todos los nombres de atributos y valores de atributos",

+ 1 - 0
app/appearance/langs/fr_FR.json

@@ -1,4 +1,5 @@
 {
+  "pdfIsLoading": "Le PDF est en cours de chargement, veuillez réessayer plus tard",
   "addToDeck": "Ajouter au deck...",
   "quickMakeCard": "Carte de création rapide",
   "allAttrs": "Tous les noms d'attributs et les valeurs d'attributs",

+ 1 - 0
app/appearance/langs/zh_CHT.json

@@ -1,4 +1,5 @@
 {
+  "pdfIsLoading": "PDF 正在加載中,請稍後再試",
   "addToDeck": "添加到卡包...",
   "quickMakeCard": "快速制卡",
   "allAttrs": "所有屬性名和屬性值",

+ 1 - 0
app/appearance/langs/zh_CN.json

@@ -1,4 +1,5 @@
 {
+  "pdfIsLoading": "PDF 正在加载中,请稍后再试",
   "addToDeck": "添加到卡包...",
   "quickMakeCard": "快速制卡",
   "allAttrs": "所有属性名和属性值",

+ 3 - 1
app/src/asset/index.ts

@@ -467,14 +467,16 @@ export class Asset extends Model {
                     const observer = new MutationObserver(() => {
                         this.pdfObject = webViewerLoad(this.path.startsWith("file") ? this.path : document.getElementById("baseURL").getAttribute("href") + "/" + this.path,
                             this.element, this.pdfPage, this.pdfId);
+                        this.element.setAttribute("data-loading", "true")
                         observer.disconnect();
                     });
                     observer.observe(this.element, {attributeFilter: ["class"]});
                 } else {
                     this.pdfObject = webViewerLoad(this.path.startsWith("file") ? this.path : document.getElementById("baseURL").getAttribute("href") + "/" + this.path,
                         this.element, this.pdfPage, this.pdfId);
+                    this.element.setAttribute("data-loading", "true")
                 }
-            });
+            }, Constants.TIMEOUT_BLOCKLOAD);
             /// #endif
         }
     }

+ 10 - 0
app/src/asset/pdf/app.js

@@ -80,6 +80,7 @@ import { ViewHistory } from './view_history.js'
 import { Constants } from '../../constants'
 import { GenericExternalServices } from './genericcom'
 import { getPdfInstance, hlPDFRect } from '../anno'
+import {hasClosestByClassName} from "../../protyle/util/hasClosest";
 
 const DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000 // ms
 const FORCE_PAGES_LOADED_TIMEOUT = 10000 // ms
@@ -1294,6 +1295,11 @@ class PDFViewerApplication {
         // To prevent any future issues, e.g. the document being completely
         // blank on load, always trigger rendering here.
         pdfViewer.update()
+        // NOTE: 没有渲染完就切换页签导致 https://ld246.com/article/1677072688346
+        const tabElement = hasClosestByClassName(pdfViewer.container, "fn__flex-1")
+        if (tabElement) {
+            tabElement.removeAttribute("data-loading")
+        }
       })
     })
 
@@ -1698,6 +1704,10 @@ class PDFViewerApplication {
     this.pdfViewer.cleanup()
     this.pdfThumbnailViewer.cleanup()
 
+    // NOTE: 防止定时任务导致 PDF this.messageHandler.sendWithPromise 报错
+    if (this.pdfLoadingTask.destroyed) {
+      return;
+    }
     if (
       typeof PDFJSDev === 'undefined' ||
       PDFJSDev.test('!PRODUCTION || GENERIC')

+ 2 - 0
app/src/asset/pdf/ui_utils.js

@@ -13,6 +13,8 @@
  * limitations under the License.
  */
 
+import {hasClosestByAttribute} from "../../protyle/util/hasClosest";
+
 const DEFAULT_SCALE_VALUE = "auto";
 const DEFAULT_SCALE = 1.0;
 const DEFAULT_SCALE_DELTA = 1.1;

+ 1 - 0
app/src/assets/scss/pdf/_pdf.scss

@@ -251,6 +251,7 @@
   border-bottom: 1px solid var(--b3-theme-surface-lighter);
   transition-duration: 200ms;
   transition-timing-function: ease;
+  z-index: 1;
 }
 
 #loadingBar {

+ 19 - 6
app/src/editor/util.ts

@@ -1,7 +1,7 @@
 import {Tab} from "../layout/Tab";
 import {Editor} from "./index";
 import {Wnd} from "../layout/Wnd";
-import {getDockByType, getInstanceById, getWndByLayout} from "../layout/util";
+import {getDockByType, getInstanceById, getWndByLayout, pdfIsLoading} from "../layout/util";
 import {getAllModels, getAllTabs} from "../layout/getAll";
 import {highlightById, scrollCenter} from "../util/highlightById";
 import {getDisplayName, pathPosix} from "../util/pathName";
@@ -77,9 +77,11 @@ const openFile = (options: IOpenFileOptions) => {
     if (options.assetPath) {
         const asset = allModels.asset.find((item) => {
             if (item.path == options.assetPath) {
-                item.parent.parent.switchTab(item.parent.headElement);
-                item.parent.parent.showHeading();
-                item.goToPage(options.page);
+                if (!pdfIsLoading(item.parent.parent.element)) {
+                    item.parent.parent.switchTab(item.parent.headElement);
+                    item.parent.parent.showHeading();
+                    item.goToPage(options.page);
+                }
                 return true;
             }
         });
@@ -104,7 +106,9 @@ const openFile = (options: IOpenFileOptions) => {
             editor = activeEditor;
         }
         if (editor) {
-            switchEditor(editor, options, allModels);
+            if (!pdfIsLoading(editor.parent.parent.element)) {
+                switchEditor(editor, options, allModels);
+            }
             return true;
         }
         // 没有初始化的页签无法检测到
@@ -141,6 +145,9 @@ const openFile = (options: IOpenFileOptions) => {
                 });
             }
             if (targetWnd) {
+                if (pdfIsLoading(targetWnd.element)) {
+                    return;
+                }
                 // 在右侧/下侧打开已有页签将进行页签切换 https://github.com/siyuan-note/siyuan/issues/5366
                 let hasEditor = targetWnd.children.find(item => {
                     if (item.model && item.model instanceof Editor && item.model.editor.protyle.block.rootID === options.rootID) {
@@ -157,7 +164,13 @@ const openFile = (options: IOpenFileOptions) => {
             } else {
                 wnd.split(direction).addTab(newTab(options));
             }
-        } else if (options.keepCursor && wnd.children[0].headElement) {
+            wnd.showHeading();
+            return;
+        }
+        if (pdfIsLoading(wnd.element)) {
+            return;
+        }
+        if (options.keepCursor && wnd.children[0].headElement) {
             const tab = newTab(options);
             tab.headElement.setAttribute("keep-cursor", options.id);
             wnd.addTab(tab, options.keepCursor);

+ 3 - 3
app/src/layout/Wnd.ts

@@ -3,7 +3,7 @@ import {genUUID} from "../util/genID";
 import {
     getInstanceById,
     getWndByLayout, JSONToCenter,
-    newCenterEmptyTab,
+    newCenterEmptyTab, pdfIsLoading,
     resizeTabs,
     setPanelFocus,
     switchWnd
@@ -100,7 +100,7 @@ export class Wnd {
                 } else if (target.classList.contains("item__close") && target.getAttribute("data-type") === "more") {
                     this.renderTabList(event);
                     break;
-                } else if (target.tagName === "LI" && target.getAttribute("data-id")) {
+                } else if (target.tagName === "LI" && target.getAttribute("data-id") && !pdfIsLoading(this.element)) {
                     this.switchTab(target, true);
                     break;
                 }
@@ -441,7 +441,7 @@ export class Wnd {
         if (currentTab && target === currentTab.headElement) {
             if (currentTab.model instanceof Graph) {
                 currentTab.model.onGraph(false);
-            } else if (currentTab.model instanceof Asset && currentTab.model.pdfObject) {
+            } else if (currentTab.model instanceof Asset && currentTab.model.pdfObject && currentTab.model.pdfObject.pdfViewer) {
                 // https://github.com/siyuan-note/siyuan/issues/5655
                 currentTab.model.pdfObject.pdfViewer.container.focus();
             }

+ 9 - 0
app/src/layout/util.ts

@@ -28,6 +28,7 @@ import {pdfResize} from "../asset/renderAssets";
 import {Backlink} from "./dock/Backlink";
 import {openFileById} from "../editor/util";
 import {getSearch, isWindow} from "../util/functions";
+import {showMessage} from "../dialog/message";
 
 export const setPanelFocus = (element: Element) => {
     if (element.classList.contains("layout__tab--active") || element.classList.contains("layout__wnd--active")) {
@@ -579,6 +580,14 @@ export const copyTab = (tab: Tab) => {
     });
 };
 
+export const pdfIsLoading = (element: HTMLElement) => {
+    const isLoading = element.querySelector('.layout-tab-container > [data-loading="true"]') ? true : false
+    if (isLoading) {
+        showMessage(window.siyuan.languages.pdfIsLoading);
+    }
+    return isLoading;
+}
+
 export const getInstanceById = (id: string, layout = window.siyuan.layout.centerLayout) => {
     const _getInstanceById = (item: Layout | Wnd, id: string) => {
         if (item.id === id) {