KMPDFEditViewController_dmg.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //
  2. // KMPDFEditViewController_dmg.swift
  3. // PDF Master 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 let data = self?.canUseAdvanced, data == false {
  33. let _ = KMComparativeTableViewController.show(window: self!.kmCurrentWindowC!.window!, .pageEdit)
  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 (!self.canUseAdvanced){
  102. _ = KMComparativeTableViewController.show(window: splitWindowC.window!)
  103. return
  104. }
  105. if (!self.canPayFunction) {
  106. _ = KMSubscribeWaterMarkWindowController.show(window: splitWindowC.window!, type: .split) { isSubscribeSuccess, isWaterMarkExport, isClose in
  107. if (isClose) {
  108. return
  109. }
  110. if (isSubscribeSuccess) {
  111. self.km_endSheet()
  112. self.splitMethod(windowC: splitWindowC, model: model)
  113. return
  114. }
  115. if (isWaterMarkExport) {
  116. self.km_endSheet()
  117. self.splitMethod(windowC: splitWindowC, model: model, limit: true)
  118. return
  119. }
  120. }
  121. return
  122. }
  123. /// 拆分
  124. self.splitMethod(windowC: splitWindowC, model: model)
  125. }
  126. }
  127. }
  128. override func replaceAction() {
  129. self.km_open_file_multi { [unowned self] index, params in
  130. if (self.fetchProgressBlockParamsIsPasswordFile(params: params)) { // 加密文档进度回调
  131. return
  132. }
  133. var _fileUrl = self.fetchProgressBlockParamsForFileUrl(params: params)
  134. if let exn = _fileUrl?.pathExtension, KMTools.isPDFType(exn) {
  135. if (_fileUrl!.path.isPDFValid() == false) {
  136. let alert = NSAlert()
  137. alert.alertStyle = .critical
  138. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  139. alert.runModal()
  140. }
  141. }
  142. } completionBlock: { [unowned self] documents in
  143. if !self.canUseAdvanced {
  144. _ = KMComparativeTableViewController.show(window: self.view.window!)
  145. return
  146. }
  147. self.replacePages(of: self.thumbnailView.selectionIndexPaths, with: documents)
  148. }
  149. }
  150. override func replacePages(of targetIndexpaths: Set<IndexPath>, with documents: [CPDFDocument]) {
  151. Task { @MainActor in
  152. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  153. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  154. return
  155. }
  156. super.replacePages(of: targetIndexpaths, with: documents)
  157. }
  158. }
  159. override func extractActionIndexpaths(indexpaths: Set<IndexPath>) {
  160. self.showExtractWindow_success(indexpaths: indexpaths) { [unowned self] oneDocumentPerPage, extractAfterDelete in
  161. self.extractPages(indexpaths: indexpaths, oneDocumentPerPage: oneDocumentPerPage, limit: !self.canPayFunction) { [unowned self] result, params in
  162. if (result == .failure || result == .cancel) {
  163. return
  164. }
  165. if (extractAfterDelete) {
  166. self.deleteIndexPaths(indexpaths: indexpaths)
  167. }
  168. }
  169. }
  170. }
  171. override func showExtractWindow(indexpaths: Set<IndexPath>, callback: @escaping KMResultBlock) {
  172. let windowC = KMPageEditExtractWindowController(nil)
  173. if let data = self.thumbnailView.document?.pageCount, data == indexpaths.count {
  174. windowC.canDeleteAll = false
  175. } else {
  176. windowC.canDeleteAll = true
  177. }
  178. self.km_beginSheet(windowC: windowC)
  179. windowC.itemClick = { [weak self] index, _ in
  180. if (index == 1) { /// 取消
  181. self?.km_endSheet()
  182. callback(.cancel)
  183. return
  184. }
  185. guard let _windowC = self?.kmCurrentWindowC as? KMPageEditExtractWindowController else {
  186. self?.km_endSheet()
  187. callback(nil)
  188. return
  189. }
  190. /// 提取设置项
  191. let settingView = _windowC.settingView as! KMPageEditExtractSettingView
  192. let oneDocumentPerPage = settingView.independentPerPageValue
  193. let extractAfterDelete = settingView.extractAfterDeletePageValue
  194. Task { @MainActor in
  195. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  196. _ = KMComparativeTableViewController.show(window: _windowC.window!)
  197. return
  198. }
  199. if await (KMLightMemberManager.manager.canPayFunction() == false) {
  200. let _ = KMSubscribeWaterMarkWindowController.show(window: _windowC.window!, type: .extract) { isSubscribeSuccess, isWaterMarkExport, isClose in
  201. if (isClose) {
  202. return
  203. }
  204. if (isSubscribeSuccess) {
  205. self?.km_endSheet()
  206. callback(.ok, oneDocumentPerPage, extractAfterDelete)
  207. return
  208. }
  209. if (isWaterMarkExport) {
  210. self?.km_endSheet()
  211. callback(.ok, oneDocumentPerPage, extractAfterDelete)
  212. return
  213. }
  214. }
  215. return
  216. }
  217. self?.km_endSheet()
  218. callback(.ok, oneDocumentPerPage, extractAfterDelete)
  219. }
  220. }
  221. }
  222. override func reverseAction() {
  223. self.trackEvent(type: .reverse)
  224. Task { @MainActor in
  225. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  226. let _ = KMComparativeTableViewController.show(window: self.view.window!, .pageEdit)
  227. return
  228. }
  229. self.toolBar.unSelectItem(for: KMToolbarPageEditReverseItemIdentifier)
  230. self.reverseMenuAction(sender: nil)
  231. }
  232. }
  233. override func leftRotateIndexpaths(indexpaths: Set<IndexPath>) {
  234. Task { @MainActor in
  235. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  236. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  237. return
  238. }
  239. super.leftRotateIndexpaths(indexpaths: indexpaths)
  240. }
  241. }
  242. override func rightRotateIndexpaths(indexpaths: Set<IndexPath>) {
  243. Task { @MainActor in
  244. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  245. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  246. return
  247. }
  248. super.rightRotateIndexpaths(indexpaths: indexpaths)
  249. }
  250. }
  251. override func insertBlankPage(size: NSSize, at index: Int) {
  252. Task { @MainActor in
  253. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  254. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  255. return
  256. }
  257. super.insertBlankPage(size: size, at: index)
  258. }
  259. }
  260. override func insertFileAction() {
  261. self.km_open_file_multi { [unowned self] index, params in
  262. if (self.fetchProgressBlockParamsIsPasswordFile(params: params)) { // 加密文档进度回调
  263. return
  264. }
  265. var _fileUrl = self.fetchProgressBlockParamsForFileUrl(params: params)
  266. if let exn = _fileUrl?.pathExtension, KMTools.isPDFType(exn) {
  267. if (_fileUrl!.path.isPDFValid() == false) {
  268. let alert = NSAlert()
  269. alert.alertStyle = .critical
  270. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  271. alert.runModal()
  272. }
  273. }
  274. } completionBlock: { [unowned self] documents in
  275. var insertIndex = self.getInsertIndex()
  276. var pages: Array<CPDFPage> = []
  277. var indexs = IndexSet()
  278. for document in documents {
  279. for i in 0 ..< document.pageCount {
  280. guard let page = document.page(at: i) else {
  281. continue
  282. }
  283. pages.append(page)
  284. insertIndex += 1
  285. indexs.insert(insertIndex)
  286. }
  287. }
  288. if (!self.canUseAdvanced) {
  289. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  290. return
  291. }
  292. self.insertPages(pages: pages, at: indexs)
  293. }
  294. }
  295. override func deleteIndexPaths(indexpaths: Set<IndexPath>) {
  296. if (!self.indexpathsIsValid(indexpaths: indexpaths)) {
  297. KMPrint("indexpaths invalid. indexpaths: \(indexpaths)")
  298. return
  299. }
  300. Task { @MainActor in
  301. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  302. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  303. return
  304. }
  305. super.deleteIndexPaths(indexpaths: indexpaths)
  306. }
  307. }
  308. override func cutAction() {
  309. Task { @MainActor in
  310. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  311. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  312. return
  313. }
  314. super.cutAction()
  315. }
  316. }
  317. override func copyAction() {
  318. Task { @MainActor in
  319. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  320. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  321. return
  322. }
  323. super.copyAction()
  324. }
  325. }
  326. override func pasteAction() {
  327. Task { @MainActor in
  328. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  329. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  330. return
  331. }
  332. super.pasteAction()
  333. }
  334. }
  335. }
  336. extension KMPDFEditViewController_dmg {
  337. override func thumbnailView(thumbanView: KMThumbnailView, shouldAcceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  338. if (!self.canUseAdvanced) {
  339. let _ = KMComparativeTableViewController.show(window: self.view.window!, .pageEdit)
  340. return false
  341. }
  342. return true
  343. }
  344. override func thumbnailView(thumbanView: KMThumbnailView, shouldPasteboardWriterForItemAt indexPath: IndexPath) -> Bool {
  345. if (!self.canUseAdvanced) {
  346. DispatchQueue.main.async {
  347. let _ = KMComparativeTableViewController.show(window: self.view.window!, .pageEdit)
  348. }
  349. return false
  350. }
  351. if (!self.canPayFunction) {
  352. return false
  353. }
  354. return true
  355. }
  356. }