// // KMPDFEditViewController_dmg.swift // PDF Master DMG // // Created by tangchao on 2023/8/8. // import Cocoa // 页面编辑控制器 [dmg] /* 登录策略 与 free 不同 */ class KMPDFEditViewController_dmg: KMPDFEditViewController { // MARK: menu 菜单事件 @objc override func item_insertCustomPage(sender: NSMenuItem?) { Task { @MainActor in let windowController = KMPageEditInsertCustomPageWindowController() if (self.thumbnailView.selectionIndexPaths.count > 0) { if (self.getPasteIndex() != NSNotFound) { if let page = self.thumbnailView.document?.page(at: UInt(self.getPasteIndex())) { let width = page.bounds.size.width * 210 / 595 let height = page.bounds.size.height * 297 / 842 windowController.selectedPageSize = NSSize(width: width, height: height) } } } self.km_beginSheet(windowC: windowController) windowController.itemClick = { [weak self] index in if (index == 1) { /// 取消 self?.km_endSheet() return } if let data = self?.canUseAdvanced, data == false { let _ = KMComparativeTableViewController.show(window: self!.kmCurrentWindowC!.window!, .pageEdit) return } /// 插入 guard let windowC = self?.kmCurrentWindowC as? KMPageEditInsertCustomPageWindowController else { self?.km_endSheet() return } guard let _insertIndex = self?.getInsertIndex() else { self?.km_endSheet() return } /// 样式 // let type = windowC.typeIndex // mm 单位的大小 let pageSize = windowC.pageSize let direction = windowC.direction var _pageSize = KMPageEditTools.sizeUnitToBoundSize(unit: .mm, pageSize: pageSize) if (direction == 0) { // 纵向 if (_pageSize.width > _pageSize.height) { // 需要交换 let tmp = _pageSize.width _pageSize.width = _pageSize.height _pageSize.height = tmp } else { // no things. } } else { // 横向 if (_pageSize.width > _pageSize.height) { // no things. } else { // 需要交换 let tmp = _pageSize.width _pageSize.width = _pageSize.height _pageSize.height = tmp } } /// 插入位置 // if (type == 1) { /// 空白页 let document = CPDFDocument() document?.insertPage(_pageSize, at: 0) if let page: CPDFPage = (document?.page(at: 0)) { self?.insertPages(pages: [page], at: IndexSet(integer: _insertIndex+1)) } // } else { // let document = CPDFDocument() // var imageName = "plaid" // if (type == 2) { // imageName = "horizontal_line" // } else if (type == 3) { // imageName = "five_line_score" // } // let imagePath = Bundle.main.pathForImageResource(imageName) // document?.insertPage(pageSize, withImage: imagePath, at: 0) // let page: CPDFPage = (document?.page(at: 0))! // if (direction == 0) { /// 纵向 // page.rotation = 90 // } // // self?.insertPagesForDescSort(pages: [page], indexs: IndexSet(integer: _insertIndex+1)) // } self?.km_endSheet() } } } override func splitAction() { self.trackEvent(type: .split) Task { @MainActor in self.toolBar.unSelectItem(for: KMToolbarPageEditSplitItemIdentifier) self.showSplitWindow { [unowned self] splitWindowC, model in if (!self.canUseAdvanced){ _ = KMComparativeTableViewController.show(window: splitWindowC.window!) return } if (!self.canPayFunction) { _ = KMSubscribeWaterMarkWindowController.show(window: splitWindowC.window!, type: .split) { isSubscribeSuccess, isWaterMarkExport, isClose in if (isClose) { return } if (isSubscribeSuccess) { self.km_endSheet() self.splitMethod(windowC: splitWindowC, model: model) return } if (isWaterMarkExport) { self.km_endSheet() self.splitMethod(windowC: splitWindowC, model: model, limit: true) return } } return } /// 拆分 self.splitMethod(windowC: splitWindowC, model: model) } } } override func replaceAction() { self.km_open_file_multi { [unowned self] index, params in if (self.fetchProgressBlockParamsIsPasswordFile(params: params)) { // 加密文档进度回调 return } var _fileUrl = self.fetchProgressBlockParamsForFileUrl(params: params) if let exn = _fileUrl?.pathExtension, KMTools.isPDFType(exn) { if (_fileUrl!.path.isPDFValid() == false) { 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() } } } completionBlock: { [unowned self] documents in if !self.canUseAdvanced { _ = KMComparativeTableViewController.show(window: self.view.window!) return } self.replacePages(of: self.thumbnailView.selectionIndexPaths, with: documents) } } override func replacePages(of targetIndexpaths: Set, with documents: [CPDFDocument]) { Task { @MainActor in if await (KMLightMemberManager.manager.canUseAdvanced() == false) { let _ = KMComparativeTableViewController.show(window: self.view.window!) return } super.replacePages(of: targetIndexpaths, with: documents) } } override func extractActionIndexpaths(indexpaths: Set) { self.showExtractWindow_success(indexpaths: indexpaths) { [unowned self] oneDocumentPerPage, extractAfterDelete in self.extractPages(indexpaths: indexpaths, oneDocumentPerPage: oneDocumentPerPage, limit: !self.canPayFunction) { [unowned self] result, params in if (result == .failure || result == .cancel) { return } if (extractAfterDelete) { self.deleteIndexPaths(indexpaths: indexpaths) } } } } override func showExtractWindow(indexpaths: Set, callback: @escaping KMResultBlock) { let windowC = KMPageEditExtractWindowController(nil) if let data = self.thumbnailView.document?.pageCount, data == indexpaths.count { windowC.canDeleteAll = false } else { windowC.canDeleteAll = true } self.km_beginSheet(windowC: windowC) windowC.itemClick = { [weak self] index, _ in if (index == 1) { /// 取消 self?.km_endSheet() callback(.cancel) return } guard let _windowC = self?.kmCurrentWindowC as? KMPageEditExtractWindowController else { self?.km_endSheet() callback(nil) return } /// 提取设置项 let settingView = _windowC.settingView as! KMPageEditExtractSettingView let oneDocumentPerPage = settingView.independentPerPageValue let extractAfterDelete = settingView.extractAfterDeletePageValue Task { @MainActor in if await (KMLightMemberManager.manager.canUseAdvanced() == false) { _ = KMComparativeTableViewController.show(window: _windowC.window!) return } if await (KMLightMemberManager.manager.canPayFunction() == false) { let _ = KMSubscribeWaterMarkWindowController.show(window: _windowC.window!, type: .extract) { isSubscribeSuccess, isWaterMarkExport, isClose in if (isClose) { return } if (isSubscribeSuccess) { self?.km_endSheet() callback(.ok, oneDocumentPerPage, extractAfterDelete) return } if (isWaterMarkExport) { self?.km_endSheet() callback(.ok, oneDocumentPerPage, extractAfterDelete) return } } return } self?.km_endSheet() callback(.ok, oneDocumentPerPage, extractAfterDelete) } } } override func reverseAction() { self.trackEvent(type: .reverse) Task { @MainActor in if await (KMLightMemberManager.manager.canUseAdvanced() == false) { let _ = KMComparativeTableViewController.show(window: self.view.window!, .pageEdit) return } self.toolBar.unSelectItem(for: KMToolbarPageEditReverseItemIdentifier) self.reverseMenuAction(sender: nil) } } override func leftRotateIndexpaths(indexpaths: Set) { Task { @MainActor in if await (KMLightMemberManager.manager.canUseAdvanced() == false) { let _ = KMComparativeTableViewController.show(window: self.view.window!) return } super.leftRotateIndexpaths(indexpaths: indexpaths) } } override func rightRotateIndexpaths(indexpaths: Set) { Task { @MainActor in if await (KMLightMemberManager.manager.canUseAdvanced() == false) { let _ = KMComparativeTableViewController.show(window: self.view.window!) return } super.rightRotateIndexpaths(indexpaths: indexpaths) } } override func insertBlankPage(size: NSSize, at index: Int) { Task { @MainActor in if await (KMLightMemberManager.manager.canUseAdvanced() == false) { let _ = KMComparativeTableViewController.show(window: self.view.window!) return } super.insertBlankPage(size: size, at: index) } } override func insertFileAction() { self.km_open_file_multi { [unowned self] index, params in if (self.fetchProgressBlockParamsIsPasswordFile(params: params)) { // 加密文档进度回调 return } var _fileUrl = self.fetchProgressBlockParamsForFileUrl(params: params) if let exn = _fileUrl?.pathExtension, KMTools.isPDFType(exn) { if (_fileUrl!.path.isPDFValid() == false) { 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() } } } completionBlock: { [unowned self] documents in var insertIndex = self.getInsertIndex() var pages: Array = [] var indexs = IndexSet() for document in documents { for i in 0 ..< document.pageCount { guard let page = document.page(at: i) else { continue } pages.append(page) insertIndex += 1 indexs.insert(insertIndex) } } if (!self.canUseAdvanced) { let _ = KMComparativeTableViewController.show(window: self.view.window!) return } self.insertPages(pages: pages, at: indexs) } } override func deleteIndexPaths(indexpaths: Set) { if (!self.indexpathsIsValid(indexpaths: indexpaths)) { KMPrint("indexpaths invalid. indexpaths: \(indexpaths)") return } Task { @MainActor in if await (KMLightMemberManager.manager.canUseAdvanced() == false) { let _ = KMComparativeTableViewController.show(window: self.view.window!) return } super.deleteIndexPaths(indexpaths: indexpaths) } } override func cutAction() { Task { @MainActor in if await (KMLightMemberManager.manager.canUseAdvanced() == false) { let _ = KMComparativeTableViewController.show(window: self.view.window!) return } super.cutAction() } } override func copyAction() { Task { @MainActor in if await (KMLightMemberManager.manager.canUseAdvanced() == false) { let _ = KMComparativeTableViewController.show(window: self.view.window!) return } super.copyAction() } } override func pasteAction() { Task { @MainActor in if await (KMLightMemberManager.manager.canUseAdvanced() == false) { let _ = KMComparativeTableViewController.show(window: self.view.window!) return } super.pasteAction() } } } extension KMPDFEditViewController_dmg { override func thumbnailView(thumbanView: KMThumbnailView, shouldAcceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool { if (!self.canUseAdvanced) { let _ = KMComparativeTableViewController.show(window: self.view.window!, .pageEdit) return false } return true } override func thumbnailView(thumbanView: KMThumbnailView, shouldPasteboardWriterForItemAt indexPath: IndexPath) -> Bool { if (!self.canUseAdvanced) { DispatchQueue.main.async { let _ = KMComparativeTableViewController.show(window: self.view.window!, .pageEdit) } return false } if (!self.canPayFunction) { return false } return true } }