Browse Source

AI - 修复免费、注册用户使用AI相关内容都需要弹出付费比较表

wanjun 1 year ago
parent
commit
1bf7ae6b1d

+ 56 - 43
PDF Office/PDF Master/Class/Home/ViewController/KMAIRewritingVC.swift

@@ -335,46 +335,53 @@ class KMAIRewritingVC: NSViewController {
     
     @IBAction func aiRewritingAction(_ sender: NSButton) {
         if self.uiState == .CanAIRewriting || self.uiState == .CanAIErrorCorrection || self.uiState == .CanCopy || self.uiState == .Copyed || self.uiState == .Exceed || self.uiState == .Error {
-            if !KMLightMemberManager.manager.isLogin() {
-                KMLoginWindowController.show(window: NSApp.mainWindow!)
-                return
-            }
-
-            self.errorView.isHidden = true
-            DispatchQueue.main.async {
-                self.showProgressWindow()
-            }
+//            if !KMLightMemberManager.manager.isLogin() {
+//                KMLoginWindowController.show(window: NSApp.mainWindow!)
+//
+//                return
+//            }
+            Task { @MainActor in
+                if await (KMLightMemberManager.manager.canPayFunction() == false) {
+                    let _ = KMComparativeTableViewController.aiFunctionShow(window: self.view.window!)
+                    return
+                }
+                
+                self.errorView.isHidden = true
+                DispatchQueue.main.async {
+                    self.showProgressWindow()
+                }
 
-            let infoDictionary = Bundle .main.infoDictionary!
-            let majorVersion = infoDictionary["CFBundleShortVersionString"]
-            KMRequestServerManager.manager.aiRewriting(content: self.rewritingTextField.stringValue, version: majorVersion as! String, state: self.state) { [unowned self] success, result in
-                self.hiddenProgressWindow()
+                let infoDictionary = Bundle .main.infoDictionary!
+                let majorVersion = infoDictionary["CFBundleShortVersionString"]
+                KMRequestServerManager.manager.aiRewriting(content: self.rewritingTextField.stringValue, version: majorVersion as! String, state: self.state) { [unowned self] success, result in
+                    self.hiddenProgressWindow()
 
-                if success {
-                    let result: NSDictionary = result!.result
-                    self.uiState = .CanCopy
-                    
-                    self.resultTextView.string = ""
-                    self.resultTextView.string =  result["content"] as? String ?? ""
-                    if self.state == .AIRewriting {
-                        self.aiRewritingResultStr = result["content"] as? String ?? ""
-                        self.aiRewritingErrorStr = ""
-                    } else if self.state == .AIErrorCorrection {
-                        self.aiErrorCorrectionResultStr = result["content"] as? String ?? ""
-                        self.aiErrorCorrectionErrorStr = ""
-                    }
-                } else {
-                    let result: String = result!.message
-                    
-                    DispatchQueue.main.async {
+                    if success {
+                        let result: NSDictionary = result!.result
+                        self.uiState = .CanCopy
+                        
                         self.resultTextView.string = ""
-                        self.errorLabel.stringValue = result
+                        self.resultTextView.string =  result["content"] as? String ?? ""
                         if self.state == .AIRewriting {
-                            self.aiRewritingErrorStr = result
+                            self.aiRewritingResultStr = result["content"] as? String ?? ""
+                            self.aiRewritingErrorStr = ""
                         } else if self.state == .AIErrorCorrection {
-                            self.aiErrorCorrectionErrorStr = result
+                            self.aiErrorCorrectionResultStr = result["content"] as? String ?? ""
+                            self.aiErrorCorrectionErrorStr = ""
+                        }
+                    } else {
+                        let result: String = result!.message
+                        
+                        DispatchQueue.main.async {
+                            self.resultTextView.string = ""
+                            self.errorLabel.stringValue = result
+                            if self.state == .AIRewriting {
+                                self.aiRewritingErrorStr = result
+                            } else if self.state == .AIErrorCorrection {
+                                self.aiErrorCorrectionErrorStr = result
+                            }
+                            self.uiState = .Error
                         }
-                        self.uiState = .Error
                     }
                 }
             }
@@ -383,16 +390,22 @@ class KMAIRewritingVC: NSViewController {
     
     @IBAction func copyAction(_ sender: NSButton) {
         if self.uiState == .CanCopy || self.uiState == .Copyed {
-            if !KMLightMemberManager.manager.isLogin() {
-                KMLoginWindowController.show(window: NSApp.mainWindow!)
-                return
+//            if !KMLightMemberManager.manager.isLogin() {
+//                KMLoginWindowController.show(window: NSApp.mainWindow!)
+//
+//                return
+//            }
+            Task { @MainActor in
+                if await (KMLightMemberManager.manager.canPayFunction() == false) {
+                    let _ = KMComparativeTableViewController.aiFunctionShow(window: self.view.window!)
+                    return
+                }
+                let pasteboard = NSPasteboard.general
+                pasteboard.clearContents()
+                pasteboard.setString(self.resultTextView.string, forType: .string)
+                
+                self.uiState = .Copyed
             }
-
-            let pasteboard = NSPasteboard.general
-            pasteboard.clearContents()
-            pasteboard.setString(self.resultTextView.string, forType: .string)
-            
-            self.uiState = .Copyed
         }
     }
 }

+ 64 - 57
PDF Office/PDF Master/Class/Home/ViewController/KMAITranslationVC.swift

@@ -108,67 +108,75 @@ class KMAITranslationVC: NSViewController {
         }
         self.selectYourFilesBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
             if downEntered {
-                if !KMLightMemberManager.manager.isLogin() {
-                    KMLoginWindowController.show(window: NSApp.mainWindow!)
-                    return
-                }
-                self.errorView.isHidden = true
+//                if !KMLightMemberManager.manager.isLogin() {
+//                    KMLoginWindowController.show(window: NSApp.mainWindow!)
+//
+//                    return
+//                }
+                Task { @MainActor in
+                    if await (KMLightMemberManager.manager.canPayFunction() == false) {
+                        let _ = KMComparativeTableViewController.aiFunctionShow(window: self.view.window!)
+                        return
+                    }
+                    
+                    self.errorView.isHidden = true
 
-                let openPanel = NSOpenPanel()
-                openPanel.allowedFileTypes = ["pdf", "PDF", "docx", "doc"]
-                openPanel.allowsMultipleSelection = false
-                openPanel.beginSheetModal(for: self.view.window!) { result in
-                    if result == .OK {
-                        for url in openPanel.urls {
-                            let isExceedsLimit = self.isPDFPageCountExceedsLimit(filePath: url.path)
-                            if (url.pathExtension == "pdf") || url.pathExtension == "PDF" {
-                                if !url.path.isPDFValid() {
-                                    let alert = NSAlert()
-                                    alert.alertStyle = .critical
-                                    alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
-                                    alert.runModal()
-                                    return
-                                }
-                            }
-                            if self.isFileGreaterThan10MB(atPath: url.path) {
-                                self.errorView.isHidden = false
-                                self.errorLabel.stringValue = NSLocalizedString("The uploaded file size cannot exceed 10MB", comment: "")
-                            } else if isExceedsLimit  {
-                                self.errorView.isHidden = false
-                                self.errorLabel.stringValue = NSLocalizedString("Documents cannot exceed 30 pages", comment: "")
-                            } else {
-                                DispatchQueue.main.async {
-                                    self.showProgressWindow()
-                                    self.progressController?.maxValue = Double(100)
+                    let openPanel = NSOpenPanel()
+                    openPanel.allowedFileTypes = ["pdf", "PDF", "docx", "doc"]
+                    openPanel.allowsMultipleSelection = false
+                    openPanel.beginSheetModal(for: self.view.window!) { result in
+                        if result == .OK {
+                            for url in openPanel.urls {
+                                let isExceedsLimit = self.isPDFPageCountExceedsLimit(filePath: url.path)
+                                if (url.pathExtension == "pdf") || url.pathExtension == "PDF" {
+                                    if !url.path.isPDFValid() {
+                                        let alert = NSAlert()
+                                        alert.alertStyle = .critical
+                                        alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
+                                        alert.runModal()
+                                        return
+                                    }
                                 }
-                                
-                                timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerTick), userInfo: nil, repeats: true)
-                                
-                                let infoDictionary = Bundle .main.infoDictionary!
-                                let majorVersion = infoDictionary["CFBundleShortVersionString"]
+                                if self.isFileGreaterThan10MB(atPath: url.path) {
+                                    self.errorView.isHidden = false
+                                    self.errorLabel.stringValue = NSLocalizedString("The uploaded file size cannot exceed 10MB", comment: "")
+                                } else if isExceedsLimit  {
+                                    self.errorView.isHidden = false
+                                    self.errorLabel.stringValue = NSLocalizedString("Documents cannot exceed 30 pages", comment: "")
+                                } else {
+                                    DispatchQueue.main.async {
+                                        self.showProgressWindow()
+                                        self.progressController?.maxValue = Double(100)
+                                    }
+                                    
+                                    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerTick), userInfo: nil, repeats: true)
+                                    
+                                    let infoDictionary = Bundle .main.infoDictionary!
+                                    let majorVersion = infoDictionary["CFBundleShortVersionString"]
 
-                                let languages = [self.automaticLabel.stringValue, self.languageLabel.stringValue]
-                                UserDefaults.standard.set(languages, forKey: "KMAITranslationLanguageArrayKey")
-                                UserDefaults.standard.synchronize()
-                                
-                                KMRequestServerManager.manager.aiTranslationFileUpload(file: url.path, version: majorVersion as! String) { [unowned self] success, result in
-//                                    self.hiddenProgressWindow()
-                                    if success {
-                                        let result: NSDictionary = result!.result
+                                    let languages = [self.automaticLabel.stringValue, self.languageLabel.stringValue]
+                                    UserDefaults.standard.set(languages, forKey: "KMAITranslationLanguageArrayKey")
+                                    UserDefaults.standard.synchronize()
+                                    
+                                    KMRequestServerManager.manager.aiTranslationFileUpload(file: url.path, version: majorVersion as! String) { [unowned self] success, result in
+    //                                    self.hiddenProgressWindow()
+                                        if success {
+                                            let result: NSDictionary = result!.result
 
-                                        let fileKey = result["fileKey"]
-                                        let fileName = result["fileName"]
-                                        let pageCount = result["pageCount"]
-                                        if fileKey != nil {
-                                            self.fileTranslateHandle(fileKey as! String)
-                                        }
-                                    } else {
-                                        let result: String = result!.message
+                                            let fileKey = result["fileKey"]
+                                            let fileName = result["fileName"]
+                                            let pageCount = result["pageCount"]
+                                            if fileKey != nil {
+                                                self.fileTranslateHandle(fileKey as! String)
+                                            }
+                                        } else {
+                                            let result: String = result!.message
 
-                                        DispatchQueue.main.async {
-                                            self.hiddenProgressWindow()
-                                            self.errorView.isHidden = false
-                                            self.errorLabel.stringValue = result
+                                            DispatchQueue.main.async {
+                                                self.hiddenProgressWindow()
+                                                self.errorView.isHidden = false
+                                                self.errorLabel.stringValue = result
+                                            }
                                         }
                                     }
                                 }
@@ -176,7 +184,6 @@ class KMAITranslationVC: NSViewController {
                         }
                     }
                 }
-
             }
         }
         

+ 58 - 52
PDF Office/PDF Master/Class/Home/WindowController/KMAITranslationConfirmWindowController.swift

@@ -131,63 +131,69 @@ class KMAITranslationConfirmWindowController: NSWindowController {
     // MARK: Action Methods
 
     @IBAction func translateAction(_ sender: NSButton) {
-        if !KMLightMemberManager.manager.isLogin() {
-            KMLoginWindowController.show(window: NSApp.mainWindow!)
-            return
-        }
-        
-        let filePathUrl = URL(fileURLWithPath: self.filePath)
-        let fileNameWithoutExtension = filePathUrl.deletingPathExtension().lastPathComponent
-        let newFileName = fileNameWithoutExtension + "_aiTranslation"
-        
-        let outputSavePanel = NSSavePanel()
-        outputSavePanel.title = NSLocalizedString("Translate", comment: "")
-        outputSavePanel.allowedFileTypes = ["pdf"]
-        outputSavePanel.nameFieldStringValue = newFileName
-        outputSavePanel.directoryURL = filePathUrl.deletingLastPathComponent()
-        
-        let result = outputSavePanel.runModal()
-        if result == .OK {
-            self.saveFileUrl = outputSavePanel.url!
-            
-            DispatchQueue.main.async {
-                self.showProgressWindow()
-                self.progressController?.maxValue = Double(100)
+//        if !KMLightMemberManager.manager.isLogin() {
+//            KMLoginWindowController.show(window: NSApp.mainWindow!)
+//
+//            return
+//        }
+        Task { @MainActor in
+            if await (KMLightMemberManager.manager.canPayFunction() == false) {
+                let _ = KMComparativeTableViewController.aiFunctionShow(window: self.window!)
+                return
             }
-            timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerTick), userInfo: nil, repeats: true)
-
-            let languages = [self.fromLabel.stringValue, self.toLabel.stringValue]
-            UserDefaults.standard.set(languages, forKey: "KMAITranslationLanguageArrayKey")
-            UserDefaults.standard.synchronize()
+            let filePathUrl = URL(fileURLWithPath: self.filePath)
+            let fileNameWithoutExtension = filePathUrl.deletingPathExtension().lastPathComponent
+            let newFileName = fileNameWithoutExtension + "_aiTranslation"
             
-            let infoDictionary = Bundle .main.infoDictionary!
-            let majorVersion = infoDictionary["CFBundleShortVersionString"]
-
-            KMRequestServerManager.manager.aiTranslationFileUpload(file: self.filePath, version: majorVersion as! String) { [unowned self] success, result in
-                if success {
-                    let result: NSDictionary = result!.result
-
-                    let fileKey = result["fileKey"]
-                    let fileName = result["fileName"]
-                    let pageCount = result["pageCount"]
-                    if fileKey != nil {
-                        self.fileTranslateHandle(fileKey as! String)
-                    }
-                } else {
-                    let result: String = result!.message
-
-                    DispatchQueue.main.async {
-                        self.hiddenProgressWindow()
-     
-                        let alert = NSAlert()
-                        alert.alertStyle = .critical
-                        alert.messageText = result
-                        alert.runModal()
+            let outputSavePanel = NSSavePanel()
+            outputSavePanel.title = NSLocalizedString("Translate", comment: "")
+            outputSavePanel.allowedFileTypes = ["pdf"]
+            outputSavePanel.nameFieldStringValue = newFileName
+            outputSavePanel.directoryURL = filePathUrl.deletingLastPathComponent()
+            
+            let result = outputSavePanel.runModal()
+            if result == .OK {
+                self.saveFileUrl = outputSavePanel.url!
+                
+                DispatchQueue.main.async {
+                    self.showProgressWindow()
+                    self.progressController?.maxValue = Double(100)
+                }
+                timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerTick), userInfo: nil, repeats: true)
+
+                let languages = [self.fromLabel.stringValue, self.toLabel.stringValue]
+                UserDefaults.standard.set(languages, forKey: "KMAITranslationLanguageArrayKey")
+                UserDefaults.standard.synchronize()
+                
+                let infoDictionary = Bundle .main.infoDictionary!
+                let majorVersion = infoDictionary["CFBundleShortVersionString"]
+
+                KMRequestServerManager.manager.aiTranslationFileUpload(file: self.filePath, version: majorVersion as! String) { [unowned self] success, result in
+                    if success {
+                        let result: NSDictionary = result!.result
+
+                        let fileKey = result["fileKey"]
+                        let fileName = result["fileName"]
+                        let pageCount = result["pageCount"]
+                        if fileKey != nil {
+                            self.fileTranslateHandle(fileKey as! String)
+                        }
+                    } else {
+                        let result: String = result!.message
+
+                        DispatchQueue.main.async {
+                            self.hiddenProgressWindow()
+         
+                            let alert = NSAlert()
+                            alert.alertStyle = .critical
+                            alert.messageText = result
+                            alert.runModal()
+                        }
                     }
                 }
+            } else {
+                outputSavePanel.close()
             }
-        } else {
-            outputSavePanel.close()
         }
     }
     

+ 58 - 46
PDF Office/PDF Master/Class/Home/WindowController/KMAITranslationWindowController.swift

@@ -391,49 +391,54 @@ class KMAITranslationWindowController: NSWindowController {
     
     @IBAction func aiTranslationAction(_ sender: NSButton) {
         if self.state == .CanTranslate || self.state == .CanCopy || self.state == .Copyed || self.state == .Exceed || self.state == .Error {
-            if !KMLightMemberManager.manager.isLogin() {
-                KMLoginWindowController.show(window: NSApp.mainWindow!)
-                return
-            }
-            
-            DispatchQueue.main.async {
-                self.showProgressWindow()
-            }
-
-            self.errorView.isHidden = true
-            self.toContentTextView.string = ""
-            
-            let languages = [self.fromLabel.stringValue, self.toLabel.stringValue]
-            UserDefaults.standard.set(languages, forKey: "KMAITranslationLanguageArrayKey")
-            UserDefaults.standard.synchronize()
-
-            let infoDictionary = Bundle .main.infoDictionary!
-            let majorVersion = infoDictionary["CFBundleShortVersionString"]
-            KMRequestServerManager.manager.aiTranslationTextTrans(q: self.fromContentTextField.stringValue, from: self.fromStr, to: self.toStr, version: majorVersion as! String) { [unowned self] success, result in
+//            if !KMLightMemberManager.manager.isLogin() {
+//                KMLoginWindowController.show(window: NSApp.mainWindow!)
+//                return
+//            }
+            Task { @MainActor in
+                if await (KMLightMemberManager.manager.canPayFunction() == false) {
+                    let _ = KMComparativeTableViewController.aiFunctionShow(window: self.window!)
+                    return
+                }
                 DispatchQueue.main.async {
-                    self.hiddenProgressWindow()
+                    self.showProgressWindow()
                 }
 
-                if success {
-                    let result: NSDictionary = result!.result
+                self.errorView.isHidden = true
+                self.toContentTextView.string = ""
+                
+                let languages = [self.fromLabel.stringValue, self.toLabel.stringValue]
+                UserDefaults.standard.set(languages, forKey: "KMAITranslationLanguageArrayKey")
+                UserDefaults.standard.synchronize()
 
-                    let form: String = result["form"] as! String
-                    let to: String = result["to"] as! String
-                    let src: String = result["src"] as! String
-                    let dst: String = result["dst"] as! String
-                    
+                let infoDictionary = Bundle .main.infoDictionary!
+                let majorVersion = infoDictionary["CFBundleShortVersionString"]
+                KMRequestServerManager.manager.aiTranslationTextTrans(q: self.fromContentTextField.stringValue, from: self.fromStr, to: self.toStr, version: majorVersion as! String) { [unowned self] success, result in
                     DispatchQueue.main.async {
-                        self.state = .CanCopy
-                        
-                        self.toContentTextView.string = dst
+                        self.hiddenProgressWindow()
                     }
-                } else {
-                    let result: String = result!.message
-                    
-                    DispatchQueue.main.async {
-                        self.errorLabel.stringValue = result
-                        self.state = .Error
-                        self.toContentTextView.string = ""
+
+                    if success {
+                        let result: NSDictionary = result!.result
+
+                        let form: String = result["form"] as! String
+                        let to: String = result["to"] as! String
+                        let src: String = result["src"] as! String
+                        let dst: String = result["dst"] as! String
+                        
+                        DispatchQueue.main.async {
+                            self.state = .CanCopy
+                            
+                            self.toContentTextView.string = dst
+                        }
+                    } else {
+                        let result: String = result!.message
+                        
+                        DispatchQueue.main.async {
+                            self.errorLabel.stringValue = result
+                            self.state = .Error
+                            self.toContentTextView.string = ""
+                        }
                     }
                 }
             }
@@ -442,16 +447,23 @@ class KMAITranslationWindowController: NSWindowController {
     
     @IBAction func copyAction(_ sender: NSButton) {
         if self.state == .CanCopy || self.state == .Copyed {
-            if !KMLightMemberManager.manager.isLogin() {
-                KMLoginWindowController.show(window: NSApp.mainWindow!)
-                return
-            }
+//            if !KMLightMemberManager.manager.isLogin() {
+//                KMLoginWindowController.show(window: NSApp.mainWindow!)
+//
+//                return
+//            }
+            Task { @MainActor in
+                if await (KMLightMemberManager.manager.canPayFunction() == false) {
+                    let _ = KMComparativeTableViewController.aiFunctionShow(window: self.window!)
+                    return
+                }
+                let pasteboard = NSPasteboard.general
+                pasteboard.clearContents()
+                pasteboard.setString(self.toContentTextView.string, forType: .string)
+                
+                self.state = .Copyed
 
-            let pasteboard = NSPasteboard.general
-            pasteboard.clearContents()
-            pasteboard.setString(self.toContentTextView.string, forType: .string)
-            
-            self.state = .Copyed
+            }
         }
     }
     

+ 45 - 0
PDF Office/PDF Master/Class/KMLightMember/Controller/ComparativeTable/KMComparativeTableViewController.swift

@@ -116,6 +116,51 @@ class KMComparativeTableViewController: NSWindowController {
         return nil
     }
     
+    /**
+     @abstract AI功能弹出比较表
+     @param window 弹出window
+     @param type 从什么页面跳转到比较表 默认batch
+     */
+    static func aiFunctionShow(window: NSWindow, _ type: KMComparativeInputType = .unknown) -> KMComparativeTableViewController? {
+        let controller: KMComparativeTableViewController = KMComparativeTableViewController.init(windowNibName: "KMComparativeTableViewController")
+        controller.inputType = type
+
+        controller.loginAction = { controller in
+            comparativeController = nil
+            comparativeMainWindow?.endSheet(controller.window!)
+            comparativeMainWindow = nil
+            controller.close()
+
+            if NSApp.mainWindow != nil {
+                KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable, .login)
+            }
+        }
+
+        controller.signUpAction = { controller in
+            comparativeController = nil
+            comparativeMainWindow?.endSheet(controller.window!)
+            comparativeMainWindow = nil
+            controller.close()
+
+            if NSApp.mainWindow != nil {
+                KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable, .register)
+            }
+        }
+        
+        controller.subscriptionAction = { controller in
+            controller.subscribeAction(controller: controller)
+        }
+        
+        controller.restoreAction = { controller in
+            controller.subscribeAction(controller: controller, isRestore:true)
+        }
+        
+        window.beginSheet(controller.window!)
+
+        comparativeController = controller
+        comparativeMainWindow = window
+        return controller
+    }
     
     override func windowDidLoad() {
         super.windowDidLoad()

+ 28 - 22
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+Action.swift

@@ -2141,31 +2141,37 @@ extension KMMainViewController : KMMainToolbarControllerDelegate {
     }
     
     func aiTranslationPDFFileAction() {
-        if !KMLightMemberManager.manager.isLogin() && NSApp.mainWindow != nil {
-            KMLoginWindowController.show(window: NSApp.mainWindow!)
-            return
-        }
-
-        let isExceedsLimit = self.isPDFPageCountExceedsLimit(filePath: (self.document?.documentURL.path)!)
-        if self.isFileGreaterThan10MB(atPath: (self.document?.documentURL.path)!) {
-            let alert = NSAlert()
-            alert.alertStyle = .critical
-            alert.messageText = NSLocalizedString("The uploaded file size cannot exceed 10MB", comment: "")
-            alert.runModal()
+//        if !KMLightMemberManager.manager.isLogin() && NSApp.mainWindow != nil {
+//            KMLoginWindowController.show(window: NSApp.mainWindow!)
+//
+//            return
+//        }
+        Task { @MainActor in
+            if await (KMLightMemberManager.manager.canPayFunction() == false) {
+                let _ = KMComparativeTableViewController.aiFunctionShow(window: self.view.window!)
+                return
+            }
+            let isExceedsLimit = self.isPDFPageCountExceedsLimit(filePath: (self.document?.documentURL.path)!)
+            if self.isFileGreaterThan10MB(atPath: (self.document?.documentURL.path)!) {
+                let alert = NSAlert()
+                alert.alertStyle = .critical
+                alert.messageText = NSLocalizedString("The uploaded file size cannot exceed 10MB", comment: "")
+                alert.runModal()
 
-            return
-        } else if isExceedsLimit  {
-            let alert = NSAlert()
-            alert.alertStyle = .critical
-            alert.messageText = NSLocalizedString("Documents cannot exceed 30 pages", comment: "")
-            alert.runModal()
+                return
+            } else if isExceedsLimit  {
+                let alert = NSAlert()
+                alert.alertStyle = .critical
+                alert.messageText = NSLocalizedString("Documents cannot exceed 30 pages", comment: "")
+                alert.runModal()
 
-            return
+                return
+            }
+            
+            self.aiTranslationConfirWC = KMAITranslationConfirmWindowController.init(windowNibName: NSNib.Name("KMAITranslationConfirmWindowController"))
+            self.aiTranslationConfirWC!.filePath = (self.document?.documentURL.path)!
+            await self.view.window?.beginSheet(self.aiTranslationConfirWC!.window!)
         }
-        
-        self.aiTranslationConfirWC = KMAITranslationConfirmWindowController.init(windowNibName: NSNib.Name("KMAITranslationConfirmWindowController"))
-        self.aiTranslationConfirWC!.filePath = (self.document?.documentURL.path)!
-        self.view.window?.beginSheet(self.aiTranslationConfirWC!.window!)
     }
     
     func toolbarViewController(_ viewController: KMToolbarViewController, shareDocument item: NSMenuItem) {