KMPDFEditViewController_dmg.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. //
  2. // KMPDFEditViewController_dmg.swift
  3. // PDF Reader Pro DMG
  4. //
  5. // Created by tangchao on 2023/8/8.
  6. //
  7. import Cocoa
  8. // 页面编辑控制器 [dmg]
  9. /*
  10. 登录策略 与 free 不同
  11. */
  12. class KMPDFEditViewController_dmg: KMPDFEditViewController {
  13. // MARK: menu 菜单事件
  14. @objc override func item_insertCustomPage(sender: NSMenuItem?) {
  15. Task { @MainActor in
  16. let windowController = KMPageEditInsertCustomPageWindowController()
  17. if (self.thumbnailView.selectionIndexPaths.count > 0) {
  18. if (self.getPasteIndex() != NSNotFound) {
  19. if let page = self.thumbnailView.document?.page(at: UInt(self.getPasteIndex())) {
  20. let width = page.bounds.size.width * 210 / 595
  21. let height = page.bounds.size.height * 297 / 842
  22. windowController.selectedPageSize = NSSize(width: width, height: height)
  23. }
  24. }
  25. }
  26. self.km_beginSheet(windowC: windowController)
  27. windowController.itemClick = { [weak self] index in
  28. if (index == 1) { /// 取消
  29. self?.km_endSheet()
  30. return
  31. }
  32. if IAPProductsManager.default().isAvailableAllFunction() == false {
  33. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  34. return
  35. }
  36. /// 插入
  37. guard let windowC = self?.kmCurrentWindowC as? KMPageEditInsertCustomPageWindowController else {
  38. self?.km_endSheet()
  39. return
  40. }
  41. guard let _insertIndex = self?.getInsertIndex() else {
  42. self?.km_endSheet()
  43. return
  44. }
  45. /// 样式
  46. // let type = windowC.typeIndex
  47. // mm 单位的大小
  48. let pageSize = windowC.pageSize
  49. let direction = windowC.direction
  50. var _pageSize = KMPageEditTools.sizeUnitToBoundSize(unit: .mm, pageSize: pageSize)
  51. if (direction == 0) { // 纵向
  52. if (_pageSize.width > _pageSize.height) { // 需要交换
  53. let tmp = _pageSize.width
  54. _pageSize.width = _pageSize.height
  55. _pageSize.height = tmp
  56. } else {
  57. // no things.
  58. }
  59. } else { // 横向
  60. if (_pageSize.width > _pageSize.height) {
  61. // no things.
  62. } else { // 需要交换
  63. let tmp = _pageSize.width
  64. _pageSize.width = _pageSize.height
  65. _pageSize.height = tmp
  66. }
  67. }
  68. /// 插入位置
  69. // if (type == 1) { /// 空白页
  70. let document = CPDFDocument()
  71. document?.insertPage(_pageSize, at: 0)
  72. if let page: CPDFPage = (document?.page(at: 0)) {
  73. self?.insertPages(pages: [page], at: IndexSet(integer: _insertIndex+1))
  74. }
  75. // } else {
  76. // let document = CPDFDocument()
  77. // var imageName = "plaid"
  78. // if (type == 2) {
  79. // imageName = "horizontal_line"
  80. // } else if (type == 3) {
  81. // imageName = "five_line_score"
  82. // }
  83. // let imagePath = Bundle.main.pathForImageResource(imageName)
  84. // document?.insertPage(pageSize, withImage: imagePath, at: 0)
  85. // let page: CPDFPage = (document?.page(at: 0))!
  86. // if (direction == 0) { /// 纵向
  87. // page.rotation = 90
  88. // }
  89. //
  90. // self?.insertPagesForDescSort(pages: [page], indexs: IndexSet(integer: _insertIndex+1))
  91. // }
  92. self?.km_endSheet()
  93. }
  94. }
  95. }
  96. override func splitAction() {
  97. self.trackEvent(type: .split)
  98. Task { @MainActor in
  99. self.toolBar.unSelectItem(for: KMToolbarPageEditSplitItemIdentifier)
  100. self.showSplitWindow { [unowned self] splitWindowC, model in
  101. if IAPProductsManager.default().isAvailableAllFunction() == false {
  102. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  103. return
  104. }
  105. /// 拆分
  106. self.splitMethod(windowC: splitWindowC, model: model)
  107. }
  108. }
  109. }
  110. override func replaceAction() {
  111. self.km_open_file_multi { [unowned self] index, params in
  112. if (self.fetchProgressBlockParamsIsPasswordFile(params: params)) { // 加密文档进度回调
  113. return
  114. }
  115. var _fileUrl = self.fetchProgressBlockParamsForFileUrl(params: params)
  116. if let exn = _fileUrl?.pathExtension, KMTools.isPDFType(exn) {
  117. if (_fileUrl!.path.isPDFValid() == false) {
  118. let alert = NSAlert()
  119. alert.alertStyle = .critical
  120. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  121. alert.runModal()
  122. }
  123. }
  124. } completionBlock: { [unowned self] documents in
  125. if IAPProductsManager.default().isAvailableAllFunction() == false {
  126. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  127. return
  128. }
  129. self.replacePages(of: self.thumbnailView.selectionIndexPaths, with: documents)
  130. }
  131. }
  132. override func replacePages(of targetIndexpaths: Set<IndexPath>, with documents: [CPDFDocument]) {
  133. Task { @MainActor in
  134. if IAPProductsManager.default().isAvailableAllFunction() == false {
  135. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  136. return
  137. }
  138. super.replacePages(of: targetIndexpaths, with: documents)
  139. }
  140. }
  141. override func extractActionIndexpaths(indexpaths: Set<IndexPath>) {
  142. self.showExtractWindow_success(indexpaths: indexpaths) { [unowned self] oneDocumentPerPage, extractAfterDelete in
  143. self.extractPages(indexpaths: indexpaths, oneDocumentPerPage: oneDocumentPerPage, limit: !self.canPayFunction) { [unowned self] result, params in
  144. if (result == .failure || result == .cancel) {
  145. return
  146. }
  147. if (extractAfterDelete) {
  148. self.deleteIndexPaths(indexpaths: indexpaths)
  149. }
  150. }
  151. }
  152. }
  153. override func showExtractWindow(indexpaths: Set<IndexPath>, callback: @escaping KMResultBlock) {
  154. let windowC = KMPageEditExtractWindowController(nil)
  155. if let data = self.thumbnailView.document?.pageCount, data == indexpaths.count {
  156. windowC.canDeleteAll = false
  157. } else {
  158. windowC.canDeleteAll = true
  159. }
  160. self.km_beginSheet(windowC: windowC)
  161. windowC.itemClick = { [weak self] index, _ in
  162. if (index == 1) { /// 取消
  163. self?.km_endSheet()
  164. callback(.cancel)
  165. return
  166. }
  167. guard let _windowC = self?.kmCurrentWindowC as? KMPageEditExtractWindowController else {
  168. self?.km_endSheet()
  169. callback(nil)
  170. return
  171. }
  172. /// 提取设置项
  173. let settingView = _windowC.settingView as! KMPageEditExtractSettingView
  174. let oneDocumentPerPage = settingView.independentPerPageValue
  175. let extractAfterDelete = settingView.extractAfterDeletePageValue
  176. Task { @MainActor in
  177. if IAPProductsManager.default().isAvailableAllFunction() == false {
  178. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  179. return
  180. }
  181. self?.km_endSheet()
  182. callback(.ok, oneDocumentPerPage, extractAfterDelete)
  183. }
  184. }
  185. }
  186. override func reverseAction() {
  187. self.trackEvent(type: .reverse)
  188. Task { @MainActor in
  189. if IAPProductsManager.default().isAvailableAllFunction() == false {
  190. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  191. return
  192. }
  193. self.toolBar.unSelectItem(for: KMToolbarPageEditReverseItemIdentifier)
  194. self.reverseMenuAction(sender: nil)
  195. }
  196. }
  197. override func leftRotateIndexpaths(indexpaths: Set<IndexPath>) {
  198. Task { @MainActor in
  199. if IAPProductsManager.default().isAvailableAllFunction() == false {
  200. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  201. return
  202. }
  203. super.leftRotateIndexpaths(indexpaths: indexpaths)
  204. }
  205. }
  206. override func rightRotateIndexpaths(indexpaths: Set<IndexPath>) {
  207. Task { @MainActor in
  208. if IAPProductsManager.default().isAvailableAllFunction() == false {
  209. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  210. return
  211. }
  212. super.rightRotateIndexpaths(indexpaths: indexpaths)
  213. }
  214. }
  215. override func insertBlankPage(size: NSSize, at index: Int, position: KMInsertPosition = .after) {
  216. Task { @MainActor in
  217. if IAPProductsManager.default().isAvailableAllFunction() == false {
  218. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  219. return
  220. }
  221. super.insertBlankPage(size: size, at: index, position: position)
  222. }
  223. }
  224. override func insertFileAction() {
  225. self.km_open_file_multi { [unowned self] index, params in
  226. if (self.fetchProgressBlockParamsIsPasswordFile(params: params)) { // 加密文档进度回调
  227. return
  228. }
  229. var _fileUrl = self.fetchProgressBlockParamsForFileUrl(params: params)
  230. if let exn = _fileUrl?.pathExtension, KMTools.isPDFType(exn) {
  231. if (_fileUrl!.path.isPDFValid() == false) {
  232. let alert = NSAlert()
  233. alert.alertStyle = .critical
  234. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  235. alert.runModal()
  236. }
  237. }
  238. } completionBlock: { [unowned self] documents in
  239. var insertIndex = self.getInsertIndex()
  240. var pages: Array<CPDFPage> = []
  241. var indexs = IndexSet()
  242. for document in documents {
  243. for i in 0 ..< document.pageCount {
  244. guard let page = document.page(at: i) else {
  245. continue
  246. }
  247. pages.append(page)
  248. insertIndex += 1
  249. indexs.insert(insertIndex)
  250. }
  251. }
  252. if IAPProductsManager.default().isAvailableAllFunction() == false {
  253. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  254. return
  255. }
  256. self.insertPages(pages: pages, at: indexs)
  257. }
  258. }
  259. override func deleteIndexPaths(indexpaths: Set<IndexPath>) {
  260. if (!self.indexpathsIsValid(indexpaths: indexpaths)) {
  261. KMPrint("indexpaths invalid. indexpaths: \(indexpaths)")
  262. return
  263. }
  264. Task { @MainActor in
  265. if IAPProductsManager.default().isAvailableAllFunction() == false {
  266. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  267. return
  268. }
  269. super.deleteIndexPaths(indexpaths: indexpaths)
  270. }
  271. }
  272. override func cutAction() {
  273. Task { @MainActor in
  274. if IAPProductsManager.default().isAvailableAllFunction() == false {
  275. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  276. return
  277. }
  278. super.cutAction()
  279. }
  280. }
  281. override func copyAction() {
  282. Task { @MainActor in
  283. if IAPProductsManager.default().isAvailableAllFunction() == false {
  284. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  285. return
  286. }
  287. super.copyAction()
  288. }
  289. }
  290. override func pasteAction() {
  291. Task { @MainActor in
  292. if IAPProductsManager.default().isAvailableAllFunction() == false {
  293. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  294. return
  295. }
  296. super.pasteAction()
  297. }
  298. }
  299. }
  300. extension KMPDFEditViewController_dmg {
  301. override func thumbnailView(thumbanView: KMThumbnailView, shouldAcceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  302. if IAPProductsManager.default().isAvailableAllFunction() == false {
  303. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  304. return false
  305. }
  306. return true
  307. }
  308. override func thumbnailView(thumbanView: KMThumbnailView, shouldPasteboardWriterForItemAt indexPath: IndexPath) -> Bool {
  309. if IAPProductsManager.default().isAvailableAllFunction() == false {
  310. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  311. return false
  312. }
  313. // if (!self.canPayFunction) {
  314. // return false
  315. // }
  316. return true
  317. }
  318. }