浏览代码

fix: 关闭文档修复未恢复初始化状态

liutian 2 周之前
父节点
当前提交
435e6151b1

+ 0 - 2
packages/core/src/annotation/layer.js

@@ -659,7 +659,6 @@ class ComPDFAnnotationLayer {
     this.renderPaintScene()
     this.eventBus._on('toolChanged', this.onHandleTool)
     this.eventBus._on('toolModeChanged', this.onHandleToolMode)
-    this.eventBus._on('setDefaultSelect', this.onHandleDefaultSelect)
     this.eventBus._on('propertyPanelChanged', this.onHandlePropertyPanelChanged)
 
     if (viewport) {
@@ -966,7 +965,6 @@ class ComPDFAnnotationLayer {
     }
     this.eventBus._off('toolChanged', this.onHandleTool)
     this.eventBus._off('toolModeChanged', this.onHandleToolMode)
-    this.eventBus._off('setDefaultSelect', this.onHandleDefaultSelect)
     this.eventBus._off('propertyPanelChanged', this.onHandlePropertyPanelChanged)
 
     this.annotationStore.notFirstRender = true

+ 51 - 5
packages/core/src/index.js

@@ -49,6 +49,7 @@ class ComPDFKitViewer {
     this.scaleChangedCallback = null
     this.annotationsNumChangedCallback = null
     this.distanceChangedCallback = null
+    this._pagesCount = 0
     this._license = ''
     this._token = null
     this._pdfId = null
@@ -301,6 +302,7 @@ class ComPDFKitViewer {
     tool,
     color
   }) {
+    if (!this.pdfDocument) return
     this.activeTool = tool
     this.color = color
     this.eventBus.dispatch("toolChanged", { tool, color });
@@ -495,7 +497,7 @@ class ComPDFKitViewer {
     // }
 
     // 处理传入的PDF
-    return await loadingTask.promise.then(
+    return loadingTask.promise.then(
       async pdfDocument => {
         // 上传pdf
         await pdfDocument.getData().then(async (binaryData) => {
@@ -649,6 +651,7 @@ class ComPDFKitViewer {
   }
 
   getSelectedText(pageNumber) {
+    if (!this.pdfDocument) return ''
     if (pageNumber) {
       const page = this.pdfViewer._pages[pageNumber - 1]
       if (page) {
@@ -953,6 +956,7 @@ class ComPDFKitViewer {
   }
 
   getOutlines() {
+    if (!this.pdfDocument) return []
     if (this.outlines) {
       return this.outlines
     }
@@ -965,6 +969,7 @@ class ComPDFKitViewer {
   }
 
   async search(value) {
+    if (!this.pdfDocument) return
     const result = await this.messageHandler.sendWithPromise('Search', {
       pagesPtr: this.pagesPtr,
       value
@@ -990,6 +995,7 @@ class ComPDFKitViewer {
   }
 
   setActiveSearchResult(result = null, activeSearchIndex = 0) {
+    if (!this.pdfDocument) return
     if (!this.searchResults) return
     const activeResult = result ? result : this.searchResults[0]
     const quad = activeResult.quads[0]
@@ -1014,7 +1020,6 @@ class ComPDFKitViewer {
       pageView.setActiveSearchResult(activeResult)
       this.#activeSearchIndex = 0
     }
-
   }
 
   clearSearchResults() {
@@ -1153,12 +1158,14 @@ class ComPDFKitViewer {
   }
 
   removeAllAnnotations() {
+    if (!this.pdfDocument) return
     if (this.pdfViewer.annotationStorage) {
       this.pdfViewer.annotationStorage.cleanup()
     }
   }
 
   async importAnnotations(data) {
+    if (!this.pdfDocument) return
     const source = await new Promise((resolve) => {
       const reader = new FileReader();
       reader.onload = (event) => {
@@ -1264,6 +1271,7 @@ class ComPDFKitViewer {
           return false
         });
     } else {
+      if (!this.pdfDocument) return
       // 设置密码标志
       this.saveAction = 'set'
       this.#pwd = value
@@ -1304,6 +1312,7 @@ class ComPDFKitViewer {
           return false
         });
     } else {
+      if (!this.pdfDocument) return
       if (this.#pwd) {
         // 移除密码标志
         this.saveAction = 'remove'
@@ -1337,6 +1346,7 @@ class ComPDFKitViewer {
   }
 
   toggleSidebar() {
+    if (!this.pdfDocument) return
     this.pdfSidebar.toggle()
   }
 
@@ -1512,10 +1522,15 @@ class ComPDFKitViewer {
     this.pdfOutlineViewer?.reset();
     this.pdfAttachmentViewer?.reset();
 
+    this.pagesPtr.length = 0
     this.outlines = []
+    this.annotationHistory.length = 0
     this.#pwd = ''
     this._pdfId = null
     this.annotations = null
+    this._docName = ''
+    this._pagesCount = 0
+    annotationStore.annotationsAll = null
 
     await Promise.all(promises);
   }
@@ -1928,6 +1943,7 @@ class ComPDFKitViewer {
           return false
         });
     } else {
+      if (!this.pdfDocument) return
       let saveType = 2
       if (!this.saveAction || this.saveAction === 'set') {
         saveType = 2
@@ -1954,6 +1970,7 @@ class ComPDFKitViewer {
   }
 
   async flattenPdfDownload() {
+    if (!this.pdfDocument) return
     const blobData = await this.flattenPdf()
     if (blobData) {
       saveAs(blobData, this._docName)
@@ -2046,6 +2063,7 @@ class ComPDFKitViewer {
           return false
         });
     } else {
+      if (!this.pdfDocument) return
       const result = await this.messageHandler.sendWithPromise('XFDFExportAnnotations', {
         doc: this.doc
       })
@@ -2074,6 +2092,7 @@ class ComPDFKitViewer {
   }
 
   getSelectedPage(firstCoordinates, lastCoordinates) {
+    if (!this.pdfDocument) return null
     let firstPageIndex = null, lastPageIndex = null
     for (let i = 0; i < this.pagesCount; i++) {
       const el = this.pdfViewer._pages[i].div
@@ -2102,6 +2121,7 @@ class ComPDFKitViewer {
   }
 
   pageToWindow(coordinates, pageNumber) {
+    if (!this.pdfDocument) return null
     if (pageNumber > this.page && pageNumber < 1) return null
     const el = this.pdfViewer._pages[pageNumber - 1].div
     const left = el.offsetLeft
@@ -2115,6 +2135,7 @@ class ComPDFKitViewer {
   }
 
   windowToPage(coordinates, pageNumber) {
+    if (!this.pdfDocument) return null
     if (pageNumber > this.page && pageNumber < 1) return null
     const el = this.pdfViewer._pages[pageNumber - 1].div
     const left = el.offsetLeft
@@ -2252,6 +2273,7 @@ class ComPDFKitViewer {
   }
 
   requestFullScreenMode() {
+    if (!this.pdfDocument) return
     this.pdfPresentationMode?.request();
   }
 
@@ -2274,7 +2296,8 @@ class ComPDFKitViewer {
     this._cleanup()
   }
 
-  zoomIn(steps) {
+  zoomIn() {
+    if (!this.pdfDocument) return
     if (this.pdfViewer.isInPresentationMode) {
       return;
     }
@@ -2283,7 +2306,8 @@ class ComPDFKitViewer {
     });
   }
 
-  zoomOut(steps) {
+  zoomOut() {
+    if (!this.pdfDocument) return
     if (this.pdfViewer.isInPresentationMode) {
       return;
     }
@@ -2293,14 +2317,17 @@ class ComPDFKitViewer {
   }
 
   nextPage() {
+    if (!this.pdfDocument) return
     return this.pdfViewer.nextPage()
   }
 
   previousPage() {
+    if (!this.pdfDocument) return
     return this.pdfViewer.previousPage()
   }
 
   pageNumberChanged(value) {
+    if (!this.pdfDocument) return
     // Note that for `<input type="number">` HTML elements, an empty string will
     // be returned for non-number inputs; hence we simply do nothing in that case.
     if (value !== "") {
@@ -2394,10 +2421,12 @@ class ComPDFKitViewer {
   }
 
   webViewerSwitchAnnotationEditorMode(evt) {
+    if (!this.pdfDocument) return
     this.pdfViewer.annotationEditorMode = evt.mode;
   }
 
   switchTool(mode) {
+    if (!this.pdfDocument) return
     this.pdfCursorTools.switchTool(mode);
   }
 
@@ -2470,6 +2499,7 @@ class ComPDFKitViewer {
   }
 
   webViewerPageMode({ mode }) {
+    if (!this.pdfDocument) return
     // Handle the 'pagemode' hash parameter, see also `PDFLinkService_setHash`.
     let view;
     switch (mode) {
@@ -2497,6 +2527,7 @@ class ComPDFKitViewer {
   }
 
   async webViewerNamedAction(evt) {
+    if (!this.pdfDocument) return
     // Processing a couple of named actions that might be useful, see also
     // `PDFLinkService.executeNamedAction`.
     switch (evt.action) {
@@ -2516,6 +2547,7 @@ class ComPDFKitViewer {
   }
 
   triggerPrinting(data) {
+    if (!this.pdfDocument) return
     let url = data instanceof Blob ? URL.createObjectURL(data) : data
     url = decodeURIComponent(url)
     printJS({
@@ -2569,18 +2601,23 @@ class ComPDFKitViewer {
     }
   }
   webViewerScaleChanged(value) {
+    if (!this.pdfDocument) return
     this.pdfViewer.currentScaleValue = value;
   }
   webViewerRotateCw() {
+    if (!this.pdfDocument) return
     this.rotatePages(90);
   }
   webViewerRotateCcw() {
+    if (!this.pdfDocument) return
     this.rotatePages(-90);
   }
   webViewerSwitchScrollMode(mode) {
+    if (!this.pdfDocument) return
     this.pdfViewer.scrollMode = mode;
   }
   webViewerSwitchSpreadMode(mode) {
+    if (!this.pdfDocument) return
     this.pdfViewer.spreadMode = mode;
   }
 
@@ -2662,6 +2699,7 @@ class ComPDFKitViewer {
   }
 
   async setToolMode(mode) {
+    if (!this.pdfDocument) return
     const oldMode = this.toolMode;
     this.toolMode = mode;
     if (mode === 'editor' && !this.fontFileInited) {
@@ -2728,14 +2766,17 @@ class ComPDFKitViewer {
   }
 
   setPropertyPanel(props) {
+    if (!this.pdfDocument) return
     this.eventBus.dispatch("propertyPanelChanged", props);
   }
 
   setFreetextProperty(data) {
+    if (!this.pdfDocument) return
     this.eventBus.dispatch('setFreetextProperty', data)
   }
 
   setContentEditorProperty(type, props) {
+    if (!this.pdfDocument) return
     if (type === 'text') {
       this.eventBus.dispatch("textPropertyChanged", props);
     } else if (type === 'image') {
@@ -2744,6 +2785,7 @@ class ComPDFKitViewer {
   }
 
   setInitProperty() {
+    if (!this.pdfDocument) return
     if (this.activeTool === 'textfield') {
       this.eventBus.dispatch("initTextfieldName");
     } else if (this.activeTool === 'checkbox') {
@@ -2756,6 +2798,7 @@ class ComPDFKitViewer {
   }
 
   handleField(prop) {
+    if (!this.pdfDocument) return
     const id = prop.id
     const key = prop.key
     const value = prop.value
@@ -2777,6 +2820,7 @@ class ComPDFKitViewer {
   }
 
   handleSign(flag, param) {
+    if (!this.pdfDocument) return
     if (flag === 'create' && !this.InkSign) {
       this.InkSign = new InkSign({
         pdfViewer: this.pdfViewer,
@@ -2800,6 +2844,7 @@ class ComPDFKitViewer {
   }
 
   handleStamp(data) {
+    if (!this.pdfDocument) return
     if (!data) return
     let imgUi = data.stampContainer
     let pageNum = data.page
@@ -3022,6 +3067,7 @@ class ComPDFKitViewer {
   // 页面编辑 - 获取每个页面图片
   // 获取范围内的页面图片 index起始索引 num页面数量
   async getDocEditorPages(pageSize, index = 0, num = null) {
+    if (!this.pdfDocument) return
     if (!this.docEditorCopy) {
       const doc = await this.messageHandler.sendWithPromise('createEditorDoc', {
         doc: this.doc,
@@ -3392,6 +3438,7 @@ class ComPDFKitViewer {
   // 页面编辑 - 保存
   async saveDocumentEdit(op) {
     const doc = this.docEditorCopy.doc
+    if (!doc) return
     const clearDocument = async () => {
       await this.messageHandler.sendWithPromise('ClearDocument', doc)
       this.docEditorCopy = null
@@ -3417,7 +3464,6 @@ class ComPDFKitViewer {
 
       const newUrl = URL.createObjectURL(blobData)
       if (this.saveAction === 'set' && this.#oldPwd !== this.#pwd) this.#oldPwd = this.#pwd
-      await this.loadDocument(newUrl, { filename, notUpdatePwd: true })
       await clearDocument()
       return newUrl
     }

+ 1 - 1
packages/core/src/pdf_viewer.js

@@ -1162,7 +1162,7 @@ class PDFViewer {
     allowNegativeOffset = false,
     ignoreDestinationZoom = false,
   }) {
-    if (!this.pdfDocument) {
+    if (!this.pdfDocument || (this._pages && !this._pages.length)) {
       return;
     }
     const pageView =

+ 0 - 3
packages/webview/src/apis/index.js

@@ -20,7 +20,6 @@ export default () => {
   const CORE_NAMESPACE = 'Core';
   const UI_NAMESPACE = 'UI';
   const objForWebViewerCore = {
-    instance: window.instance
     // Tools: window.Core.Tools,
     // Annotations: window.Core.Annotations,
     // PartRetrievers: window.Core.PartRetrievers,
@@ -35,7 +34,6 @@ export default () => {
     setLanguage: setLanguage(useViewer),
     getPassword: getPassword(useDocument),
   }
-  const documentViewer = core.getDocumentViewer(1);
 
   window.instances = {
     // CORE_NAMESPACE_KEY: CORE_NAMESPACE,
@@ -43,7 +41,6 @@ export default () => {
     [CORE_NAMESPACE]: {
       ...objForWebViewerCore,
       ...core,
-      documentViewer,
       // annotationManager: documentViewer.getAnnotationManager(),
       getDocumentViewers: () => core.getDocumentViewers(),
     },

+ 1 - 0
packages/webview/src/components/DocumentEditorContainer/DocumentEditorContainer.vue

@@ -638,6 +638,7 @@ const handleAutoScroll = (clientY) => {
         justify-content: center;
         width: 100%;
         height: calc(100% - 24px);
+        user-select: none;
 
         img {
           pointer-events: none;

+ 5 - 1
packages/webview/src/components/DocumentEditorHeader/DocumentEditorHeader.vue

@@ -45,7 +45,11 @@ const save = async () => {
   useViewer.setUploadLoading(true)
   const oldScale = useViewer.getScale
 
-  const { newUrl } = await core.saveDocumentEdit()
+  const newUrl = await core.saveDocumentEdit()
+  const pwd = useDocument.getPassword
+  await core.loadDocument(newUrl, {
+    password: pwd
+  })
   
   useDocument.setCurrentPdfData(newUrl)
   const totalPages = core.getPagesCount()

+ 0 - 2
packages/webview/src/core/index.js

@@ -33,7 +33,6 @@ import webViewerPageMode from './webViewerPageMode'
 import setToolMode from './setToolMode'
 import setPropertyPanel from './setPropertyPanel'
 import setInitProperty from './setInitProperty'
-import setDefaultSelect from './setDefaultSelect'
 import handleField from './handleField'
 import handleSign from './handleSign'
 import handleStamp from './handleStamp'
@@ -101,7 +100,6 @@ export default {
   setToolMode,
   setPropertyPanel,
   setInitProperty,
-  setDefaultSelect,
   handleField,
   handleSign,
   handleStamp,

+ 0 - 3
packages/webview/src/core/setDefaultSelect.js

@@ -1,3 +0,0 @@
-import core from '@/core'
-
-export default (props) => core.getDocumentViewer().setDefaultSelect(props)