123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- //
- // 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<IndexPath>, 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<IndexPath>) {
- 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<IndexPath>, 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<IndexPath>) {
- 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<IndexPath>) {
- 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<CPDFPage> = []
- 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<IndexPath>) {
- 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
- }
- }
|