KMPDFEditViewController.swift 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  1. //
  2. // KMPDFEditViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/1/6.
  6. //
  7. typealias KMPDFEditViewControllerSelectionDidChange = (_ selectedIndexs: Set<IndexPath>) -> ()
  8. class KMPDFEditViewController: KMPDFThumbViewBaseController {
  9. static let kNoPagesSelectedTipMessage = "No pages selected. Please select one or more pages first to organize."
  10. @IBOutlet weak var topBarBox: NSBox!
  11. @IBOutlet weak var contentBox: NSBox!
  12. @IBOutlet weak var thumbnailSizeView: NSView!
  13. @IBOutlet weak var lineBoxView: NSView!
  14. var toolBar = KMPDFEditToolbar(frame: NSZeroRect)
  15. override var thumbnailView: KMPDFThumbnailView {
  16. get {
  17. if (super.thumbnailView is KMPageEditThumbnailView) {
  18. return super.thumbnailView
  19. }
  20. super.thumbnailView = KMPageEditThumbnailView()
  21. return super.thumbnailView
  22. }
  23. set {
  24. super.thumbnailView = newValue
  25. }
  26. }
  27. var pageEditThumbnailView: KMPageEditThumbnailView {
  28. get {
  29. return self.thumbnailView as! KMPageEditThumbnailView
  30. }
  31. }
  32. override var menuItems: [[KMPDFThumbViewMenu.Item]] {
  33. get {
  34. return [[.insert ,.extract], [.leftRotate, .rightRotate, .copy, .paste, .reverse],
  35. [.delete], [.share]]
  36. }
  37. }
  38. private var pdfDocument: CPDFDocument?
  39. var listView: CPDFView?
  40. var pageIsUpdate: Bool = true
  41. var selectedPages: [Int] = []
  42. private let defaultItemSize = NSMakeSize(215, 250)
  43. var itemSize: NSSize = NSMakeSize(215, 250)
  44. // index=1 双击退出;index=2 打印
  45. var itemClick: KMCommonClickBlock?
  46. private var _isEdited = false
  47. var isEdited: Bool {
  48. return self._isEdited
  49. }
  50. var documentEditedCallback: KMCommonBlock?
  51. var selectionDidChange: KMPDFEditViewControllerSelectionDidChange?
  52. internal var viewFirstAppear = true
  53. private var _listViewCurrentIndex: Int = 0
  54. var listViewCurrentIndex: Int {
  55. get {
  56. return self._listViewCurrentIndex
  57. }
  58. }
  59. private var fileAttri_: KMFileAttribute?
  60. private var popOver_: NSPopover?
  61. required init?(coder: NSCoder) {
  62. super.init(coder: coder)
  63. }
  64. init(_ document: CPDFDocument) {
  65. super.init(nibName: "KMPDFEditViewController", bundle: nil)
  66. self.pdfDocument = document
  67. self.kmUndoManager = UndoManager()
  68. self.fileAttri_ = KMFileAttribute()
  69. self.fileAttri_?.password = document.password ?? ""
  70. self.fileAttri_?.filePath = document.documentURL.path
  71. }
  72. override func viewDidAppear() {
  73. super.viewDidAppear()
  74. let isLocked = self.pdfDocument?.isLocked ?? false
  75. let first = self.viewFirstAppear
  76. if (isLocked == false && self.viewFirstAppear) {
  77. self.viewFirstAppear = false
  78. if let doc = self.pdfDocument {
  79. self.thumbnailView.document = doc
  80. }
  81. self.thumbnailView.reloadData()
  82. }
  83. if let view = self.listView { // 存储阅读视图的当前页面索引
  84. self._listViewCurrentIndex = view.currentPageIndex
  85. }
  86. var item: Int = 0
  87. if (self.selectedPages.isEmpty) {
  88. let index = self.listView?.currentPageIndex ?? 0
  89. self.selectPages(indexs: IndexSet(integer: index))
  90. self.toolBar.setPageRangeString("\(index+1)")
  91. item = index
  92. } else {
  93. var indexs: IndexSet = []
  94. for pageIndex in self.selectedPages {
  95. let pageCnt = self.listView?.document?.pageCount ?? 0
  96. if (pageIndex < 0 || pageIndex >= pageCnt) {
  97. continue
  98. }
  99. indexs.insert(pageIndex)
  100. }
  101. self.selectPages(indexs: indexs)
  102. self.toolBar.setPageRangeString(KMPageRangeTools.newParseSelectedIndexs(selectedIndex: indexs.sorted()))
  103. item = indexs.sorted().last ?? 0
  104. }
  105. if first {
  106. var ips = Set<IndexPath>()
  107. ips.insert(IndexPath(item: item, section: 0))
  108. self.thumbnailView.collectionView.scrollToItems(at: ips, scrollPosition: .bottom)
  109. }
  110. self.view.window?.makeFirstResponder(self)
  111. self._showPDFEditDetails()
  112. }
  113. override func viewDidLoad() {
  114. super.viewDidLoad()
  115. self._isEdited = false
  116. // 显示拖放移动动效
  117. // self.thumbnailView.dragMoveEffectAnimated = true
  118. // self.thumbnailView.collectionView.backgroundColor(NSColor.km_init(hex: "#CED0D4"))
  119. self.thumbnailView.collectionView.wantsLayer = true
  120. // self.thumbnailView.collectionView.layer?.backgroundColor = KMAppearance.PDFViewBackgroundColor().cgColor
  121. if KMAppearance.isDarkMode() {
  122. self.thumbnailView.collectionView.layer?.backgroundColor = NSColor.km_init(hex: "#5F5F60").cgColor
  123. } else {
  124. self.thumbnailView.collectionView.layer?.backgroundColor = NSColor.km_init(hex: "#DADBDE").cgColor
  125. }
  126. self.thumbnailSizeView.wantsLayer = true
  127. self.thumbnailSizeView.layer?.backgroundColor = KMAppearance.Layout.b80Color().cgColor
  128. self.lineBoxView.wantsLayer = true
  129. self.lineBoxView.layer?.backgroundColor = KMAppearance.Layout.h2Color().cgColor
  130. var allowedFileTypes: [String] = []
  131. // if let _types = KMConvertPDFManager.supportFileType() {
  132. allowedFileTypes = KMTools.pdfExtensions + KMConvertPDFManager.supportFileType()
  133. // } else {
  134. // allowedFileTypes = KMTools.pdfExtensions + KMTools.imageExtensions
  135. // }
  136. self.thumbnailView.kmAllowedFileTypes = allowedFileTypes
  137. let toolBar = self.toolBar
  138. self.topBarBox.contentView = toolBar
  139. self.topBarBox.fillColor = NSColor.km_init(hex: "#F7F8FA")
  140. toolBar.itemClick = { [unowned self] item, index in
  141. guard let type = KMPageEditType(rawValue: index) else {
  142. if (index == KMPageEditType.insert_files) {
  143. self.insertFileAction()
  144. } else if (index == KMPageEditType.insert_blank_page) {
  145. self.insertBlankPageAction()
  146. } else if (index == KMPageEditType.insert_custom_page) {
  147. self.item_insertCustomPage(sender: nil)
  148. } else if (index == KMPageEditType.extract_selected_pages) {
  149. self.extractSelectPageItemAction()
  150. } else if (index == KMPageEditType.split_selected_pages) {
  151. self.splitMenuAction()
  152. }
  153. return
  154. }
  155. if (type == .leftRotate) {
  156. self.leftRotateAction()
  157. } else if (type == .rightRotate) {
  158. self.rightRotateAction()
  159. } else if (type == .zoomOut) {
  160. self.zoomOutButtonAction(sender: item?.zoomOutButton)
  161. } else if (type == .zoomIn) {
  162. self.zoomInButtonAction(sender: item?.zoomInButton)
  163. } else if (type == .extract) {
  164. self.extractAction()
  165. } else if (type == .replace) {
  166. self.replaceAction()
  167. } else if (type == .split) {
  168. self.splitAction()
  169. } else if (type == .reverse) {
  170. self.reverseAction()
  171. } else if (type == .delete) {
  172. self.deleteAction()
  173. } else if (type == .pageRange) {
  174. } else if (type == .insert) {
  175. self.trackEvent(type: .insert)
  176. } else if (type == .copy) {
  177. self.copyAction()
  178. } else if (type == .paste) {
  179. self.pasteAction()
  180. }
  181. }
  182. toolBar.pageRangeitemClick = { [unowned self] type in
  183. self.km_comboBoxSelectionDidChange(type)
  184. }
  185. toolBar.pageRangeValueDidChange = { [unowned self] value, _ in
  186. if let data = self.toolBar.pageRangeView {
  187. self.km_controlTextDidEndEditing(data)
  188. }
  189. }
  190. let view = self.thumbnailView
  191. self.contentBox.contentView?.addSubview(view)
  192. view.frame = self.contentBox.contentView?.bounds ?? .zero
  193. view.autoresizingMask = [.width, .height]
  194. self.pageEditThumbnailView.delegate = self
  195. self.pageEditThumbnailView.selectionDidChange = { [weak self] selectedIndexs in
  196. guard let self = self else {
  197. return
  198. }
  199. self.selectionDidChange?(selectedIndexs)
  200. self.view.window?.makeFirstResponder(self)
  201. let enabled = (selectedIndexs.count > 0)
  202. for i in 0 ..< self.toolBar.toolBar.items.count {
  203. let item = self.toolBar.toolBar.items[i]
  204. if item.itemIdentifier == KMToolbarPageEditLeftRotateItemIdentifier
  205. || item.itemIdentifier == KMToolbarPageEditRightRotateItemIdentifier {
  206. item.unEnabled = !enabled
  207. }
  208. }
  209. if (self.pageIsUpdate == false) {
  210. self.pageIsUpdate = true
  211. return
  212. }
  213. var indexs: Array<Int> = []
  214. for indexPath in selectedIndexs {
  215. indexs.append(indexPath.item)
  216. }
  217. indexs.sort(){$0 < $1}
  218. self.toolBar.setPageRangeString(KMPageRangeTools.newParseSelectedIndexs(selectedIndex: indexs))
  219. if self.listView != nil { // 存储阅读视图的当前页面索引
  220. if indexs.count != 0 {
  221. self._listViewCurrentIndex = indexs.last!
  222. }
  223. }
  224. }
  225. }
  226. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  227. super.interfaceThemeDidChanged(appearance)
  228. self.thumbnailView.collectionView.wantsLayer = true
  229. if KMAppearance.isDarkMode() {
  230. self.thumbnailView.collectionView.layer?.backgroundColor = NSColor.km_init(hex: "#5F5F60").cgColor
  231. } else {
  232. self.thumbnailView.collectionView.layer?.backgroundColor = NSColor.km_init(hex: "#DADBDE").cgColor
  233. }
  234. Task { @MainActor in
  235. let ips = self.thumbnailView.selectionIndexPaths
  236. self.thumbnailView.collectionView.reloadData()
  237. self.thumbnailView.selectionIndexPaths = ips
  238. }
  239. }
  240. override func keyDown(with event: NSEvent) {
  241. // super.keyDown(with: event)
  242. let cmd = event.modifierFlags.contains(.command)
  243. let shift = event.modifierFlags.contains(.shift)
  244. let code = event.keyCode
  245. let isLeft = code == 123
  246. let isUp = code == 126
  247. let isRight = code == 124
  248. let isDown = code == 125
  249. if cmd {
  250. if code == 0 { // cmd + a
  251. self.km_comboBoxSelectionDidChange(.all)
  252. return
  253. }
  254. // let ips = self.thumbnailView.selectionIndexPaths
  255. // let indexs = self.indexpathsToIndexs(indexpaths: ips)
  256. //// let idx = indexs.last ?? 0
  257. // var selectedIndexs = indexs
  258. // let numberItems = self.thumbnailView.collectionView.numberOfItems(inSection: 0)
  259. // let firstIdx = 0
  260. // let lastIdx = max(numberItems-1, 0)
  261. // if isLeft { // +
  262. // if self.isEmptySelection() {
  263. // selectedIndexs.insert(lastIdx)
  264. // } else {
  265. // if let idx = selectedIndexs.first, idx > 0 {
  266. // selectedIndexs.insert(idx-1)
  267. // }
  268. // }
  269. // } else if isRight { // -
  270. // if self.isEmptySelection() {
  271. // selectedIndexs.insert(firstIdx)
  272. // } else {
  273. // if let idx = selectedIndexs.last, idx < lastIdx {
  274. // selectedIndexs.insert(idx+1)
  275. // }
  276. // }
  277. // } else {
  278. // var numberPerLine = 0
  279. // let itemW = self.thumbnailView.itemSize.width
  280. // if itemW > 0 {
  281. // numberPerLine = Int(NSWidth(self.thumbnailView.collectionView.frame) / itemW)
  282. // }
  283. // if isUp {
  284. // if self.isEmptySelection() {
  285. // selectedIndexs.insert(lastIdx)
  286. // } else {
  287. // if let idx = selectedIndexs.first, idx > 0 {
  288. // selectedIndexs.insert(max(idx-numberPerLine, 0))
  289. // }
  290. // }
  291. // } else if isDown {
  292. // if self.isEmptySelection() {
  293. // selectedIndexs.insert(firstIdx)
  294. // } else {
  295. // if let idx = selectedIndexs.last, idx < lastIdx {
  296. // selectedIndexs.insert(min(idx+numberPerLine, lastIdx))
  297. // }
  298. // }
  299. // }
  300. // }
  301. //
  302. // if selectedIndexs.isEmpty == false {
  303. // let ips = self.indexsToIndexpaths(indexs: selectedIndexs)
  304. // self.thumbnailView.selectionIndexPaths = ips
  305. // self.thumbnailView.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
  306. // }
  307. } else if shift {
  308. } else {
  309. let ips = self.thumbnailView.selectionIndexPaths
  310. let indexs = self.indexpathsToIndexs(indexpaths: ips)
  311. let idx = indexs.last ?? 0
  312. var selectedIndexs = IndexSet()
  313. let numberItems = self.thumbnailView.collectionView.numberOfItems(inSection: 0)
  314. let firstIdx = 0
  315. let lastIdx = max(numberItems-1, 0)
  316. if isLeft {
  317. if self.isEmptySelection() {
  318. selectedIndexs.insert(lastIdx)
  319. } else {
  320. if idx > 0 {
  321. selectedIndexs.insert(idx-1)
  322. }
  323. }
  324. } else if isRight {
  325. if self.isEmptySelection() {
  326. selectedIndexs.insert(firstIdx)
  327. } else {
  328. if idx < lastIdx {
  329. selectedIndexs.insert(idx+1)
  330. }
  331. }
  332. } else {
  333. var numberPerLine = 0
  334. let itemW = self.thumbnailView.itemSize.width
  335. if itemW > 0 {
  336. numberPerLine = Int(NSWidth(self.thumbnailView.collectionView.frame) / itemW)
  337. }
  338. if isUp {
  339. if self.isEmptySelection() {
  340. selectedIndexs.insert(lastIdx)
  341. } else {
  342. selectedIndexs.insert(max(idx-numberPerLine, 0))
  343. }
  344. } else if isDown {
  345. if self.isEmptySelection() {
  346. selectedIndexs.insert(firstIdx)
  347. } else {
  348. selectedIndexs.insert(min(idx+numberPerLine, lastIdx))
  349. }
  350. }
  351. }
  352. if selectedIndexs.isEmpty == false {
  353. let ips = self.indexsToIndexpaths(indexs: selectedIndexs)
  354. self.thumbnailView.selectionIndexPaths = ips
  355. self.thumbnailView.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
  356. }
  357. }
  358. }
  359. // MARK: Private Methods
  360. private func getSelecteIndex() -> Int {
  361. let index = self.getPasteIndex()
  362. if (index != NSNotFound) {
  363. return index
  364. }
  365. return 0
  366. }
  367. private func getSelectedPage() -> CPDFPage? {
  368. return self.pdfDocument?.page(at: UInt(getSelecteIndex()))
  369. }
  370. private func selectPages(indexs: IndexSet) {
  371. var indexpaths: Set<IndexPath> = []
  372. for index in indexs {
  373. indexpaths.insert(IndexPath(item: index, section: 0))
  374. }
  375. self.selectPages(indexpaths: indexpaths)
  376. }
  377. private func km_becomeFirstResponder() {
  378. DispatchQueue.main.async {
  379. self.view.window?.makeFirstResponder(self)
  380. }
  381. }
  382. fileprivate func dealPdfDocumentDidEditCallback(type: KMPageEditType = .none) {
  383. self._isEdited = true
  384. // 提醒外面文档已编辑
  385. guard let callback = self.documentEditedCallback else {
  386. return
  387. }
  388. callback(type)
  389. }
  390. private func _progressHUD(message: String, style: KMCustomAlertStyle = .blue) {
  391. _ = CustomAlertView.alertView(message: message, fromView: self.thumbnailView, withStyle: style)
  392. }
  393. private func _showPDFEditDetails() {
  394. var insertItem: KMToolbarItemView?
  395. for item in self.toolBar.toolBar.items {
  396. if item.itemIdentifier == KMToolbarPageEditInsetItemIdentifier {
  397. insertItem = item
  398. break
  399. }
  400. }
  401. if insertItem?.window == nil {
  402. return
  403. }
  404. let showConvertDetails = KMPageEditPopViewController.showPageEditDetails()
  405. if (showConvertDetails) {
  406. let item = insertItem
  407. let popViewController = KMPageEditPopViewController()
  408. self.popOver_ = NSPopover()
  409. self.popOver_?.contentViewController = popViewController
  410. self.popOver_?.animates = false
  411. self.popOver_?.setBackgroundColor(KMAppearance.Interactive.a0Color())
  412. self.popOver_?.behavior = .transient
  413. // self.popOver.appearance = NSPopoverAppearanceMinimal;
  414. // self.popOver_?.appearance = .minimal
  415. self.popOver_?.contentSize = popViewController.view.frame.size
  416. self.popOver_?.show(relativeTo: item?.bounds ?? .zero, of: item!, preferredEdge: .maxY)
  417. }
  418. }
  419. // MARK: - 清空选择
  420. private func clearSelectPages() {
  421. self.selectPages(indexs: IndexSet())
  422. }
  423. override func refreshUI(indexpaths: Set<IndexPath> = [], keepSelected: Bool = false) {
  424. super.refreshUI(indexpaths: indexpaths, keepSelected: keepSelected)
  425. self.listView?.layoutDocumentView()
  426. }
  427. override func cutAction() {
  428. if IAPProductsManager.default().isAvailableAllFunction() == false {
  429. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  430. return
  431. }
  432. let pageCnt = self.pdfDocument?.pageCount ?? 0
  433. if (self.thumbnailView.selectionIndexPaths.count == pageCnt) {
  434. self._progressHUD(message: NSLocalizedString("Can not delete all pages.", comment: ""))
  435. return
  436. }
  437. super.cutAction()
  438. }
  439. override func copyAction() {
  440. self.trackEvent_copy()
  441. super.copyAction()
  442. }
  443. override func deleteAction() {
  444. if IAPProductsManager.default().isAvailableAllFunction() == false {
  445. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  446. return
  447. }
  448. if self.isEmptySelection() {
  449. self._progressHUD(message: KMLocalizedString(Self.kNoPagesSelectedTipMessage))
  450. return
  451. }
  452. let msg = String(format: NSLocalizedString("Are you sure to delete page %@ ?", comment: ""), KMTools.parseIndexSet(indexSet: self.indexpathsToIndexs(indexpaths: self.thumbnailView.selectionIndexPaths)))
  453. Task {
  454. let res = await KMAlertTool.runModel(message: msg, buttons: [NSLocalizedString("Delete", comment: ""), NSLocalizedString("Cancel", comment: "")])
  455. if res != .alertFirstButtonReturn {
  456. return
  457. }
  458. self.trackEvent(type: .delete)
  459. super.deleteAction()
  460. self.toolBar.unSelectItem(for: KMToolbarPageEditDeleteItemIdentifier)
  461. }
  462. }
  463. override func deleteIndexPaths(indexpaths: Set<IndexPath>) {
  464. guard let pageCount = self.thumbnailView.document?.pageCount else {
  465. KMPrint("pageCount invalid.")
  466. return
  467. }
  468. if (indexpaths.count == pageCount) {
  469. self._progressHUD(message: NSLocalizedString("Can not delete all pages.", comment: ""))
  470. return
  471. }
  472. self.dealPdfDocumentDidEditCallback(type: .delete)
  473. super.deleteIndexPaths(indexpaths: indexpaths)
  474. }
  475. override func leftRotateAction() {
  476. if IAPProductsManager.default().isAvailableAllFunction() == false {
  477. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  478. return
  479. }
  480. self.trackEvent(type: .leftRotate)
  481. super.leftRotateAction()
  482. // self.toolBar.unSelectItem(for: KMToolbarPageEditLeftRotateItemIdentifier)
  483. }
  484. override func leftRotateIndexpaths(indexpaths: Set<IndexPath>) {
  485. if (!self.indexpathsIsValid(indexpaths: indexpaths) || !self.canRotate()) {
  486. return
  487. }
  488. super.leftRotateIndexpaths(indexpaths: indexpaths)
  489. self.dealPdfDocumentDidEditCallback(type: .leftRotate)
  490. }
  491. override func rightRotateAction() {
  492. if IAPProductsManager.default().isAvailableAllFunction() == false {
  493. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  494. return
  495. }
  496. if self.isEmptySelection() {
  497. self._progressHUD(message: KMLocalizedString(Self.kNoPagesSelectedTipMessage))
  498. return
  499. }
  500. self.trackEvent(type: .rightRotate)
  501. super.rightRotateAction()
  502. // self.toolBar.unSelectItem(for: KMToolbarPageEditRightRotateItemIdentifier)
  503. }
  504. override func rightRotateIndexpaths(indexpaths: Set<IndexPath>) {
  505. if (!self.indexpathsIsValid(indexpaths: indexpaths) || !self.canRotate()) {
  506. return
  507. }
  508. super.rightRotateIndexpaths(indexpaths: indexpaths)
  509. self.dealPdfDocumentDidEditCallback(type: .rightRotate)
  510. }
  511. override func insertPages(pages: Array<CPDFPage>, at indexs: IndexSet) {
  512. super.insertPages(pages: pages, at: indexs)
  513. if (!indexs.isEmpty) {
  514. self.selectPages(indexs: indexs)
  515. self.dealPdfDocumentDidEditCallback(type: .insert)
  516. }
  517. }
  518. override func pasteAction() {
  519. if IAPProductsManager.default().isAvailableAllFunction() == false {
  520. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  521. return
  522. }
  523. if (self.canPaste()) {
  524. super.pasteAction()
  525. self.dealPdfDocumentDidEditCallback(type: .paste)
  526. } else {
  527. if KMThumbnailManager.manager.copyPages.count <= 0 {
  528. self._progressHUD(message: KMLocalizedString("No copied page selected. Please select and copy a page first."))
  529. }
  530. }
  531. }
  532. @objc func pasteActonForEmpty(_ sender: NSMenuItem) {
  533. guard let indexpaths = sender.representedObject as? IndexPath else {
  534. NSSound.beep()
  535. return
  536. }
  537. let idx = max(0, indexpaths.item-1)
  538. // self.pastePage(at: indexpaths.item)
  539. self.pastePage(at: idx)
  540. }
  541. override func pastePage(at index: Int) {
  542. if IAPProductsManager.default().isAvailableAllFunction() == false {
  543. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  544. return
  545. }
  546. super.pastePage(at: index)
  547. }
  548. override func extractAction() {
  549. if IAPProductsManager.default().isAvailableAllFunction() == false {
  550. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  551. return
  552. }
  553. if self.isEmptySelection() {
  554. self._progressHUD(message: KMLocalizedString(Self.kNoPagesSelectedTipMessage))
  555. return
  556. }
  557. self.trackEvent(type: .extract)
  558. super.extractAction()
  559. DispatchQueue.main.async {
  560. self.toolBar.unSelectItem(for: KMToolbarPageEditExtractItemIdentifier)
  561. }
  562. }
  563. override func replaceAction() {
  564. if IAPProductsManager.default().isAvailableAllFunction() == false {
  565. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  566. return
  567. }
  568. if self.isEmptySelection() {
  569. self._progressHUD(message: KMLocalizedString(Self.kNoPagesSelectedTipMessage))
  570. return
  571. }
  572. self.trackEvent(type: .replace)
  573. super.replaceAction()
  574. DispatchQueue.main.async {
  575. self.toolBar.unSelectItem(for: KMToolbarPageEditReplaceItemIdentifier)
  576. }
  577. }
  578. override func replacePages(of targetIndexpaths: Set<IndexPath>, with documents: [CPDFDocument]) {
  579. self.model.insertedDocumentSet.formUnion(documents)
  580. super.replacePages(of: targetIndexpaths, with: documents)
  581. }
  582. // 插入规则:选中后面插入[多选插入第后个后面];没有选中,插入阅读页当前页;
  583. override func getInsertIndex() -> Int {
  584. let ips = self.thumbnailView.selectionIndexPaths.sorted()
  585. if ips.isEmpty {
  586. return self.listView?.currentPageIndex ?? 0
  587. }
  588. return ips.last?.item ?? 0
  589. }
  590. override func splitAction() {
  591. if IAPProductsManager.default().isAvailableAllFunction() == false {
  592. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  593. return
  594. }
  595. if self.isEmptySelection() {
  596. self._progressHUD(message: KMLocalizedString(Self.kNoPagesSelectedTipMessage))
  597. return
  598. }
  599. guard let doc = self.pdfDocument else {
  600. NSSound.beep()
  601. return
  602. }
  603. let windowC = SplitWindowController(document: doc)
  604. windowC.fileAttribute?.pagesType = .custom
  605. let ips = self.indexpathsToIndexs(indexpaths: self.thumbnailView.selectionIndexPaths).sorted()
  606. let string = KMPageRangeTools.newParseSelectedIndexs(selectedIndex: ips)
  607. windowC.fileAttribute?.pagesString = string
  608. self.km_beginSheet(windowC: windowC)
  609. }
  610. func splitMethod(windowC: KMPageEditSplitWindowController, model: KMPageEditSplitSettingModel, limit: Bool = false) {
  611. if IAPProductsManager.default().isAvailableAllFunction() == false {
  612. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  613. return
  614. }
  615. if self.isEmptySelection() {
  616. self._progressHUD(message: KMLocalizedString(Self.kNoPagesSelectedTipMessage))
  617. return
  618. }
  619. self._splitMethod(windowC: windowC, model: model, limit: limit)
  620. }
  621. func splitMenuAction() {
  622. // if (![IAPProductsManager defaultManager].isAvailableAllFunction) {
  623. // #if VERSION_DMG
  624. // [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil];
  625. // #else
  626. // KMToolCompareWindowController * vc = [KMToolCompareWindowController toolCompareWithType:KMCompareWithToolType_PageEdit setSelectIndex:1];
  627. // [vc startModal:nil];
  628. // #endif
  629. // return;
  630. // }
  631. if IAPProductsManager.default().isAvailableAllFunction() == false {
  632. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  633. return
  634. }
  635. let pdf = self.thumbnailView.document
  636. let pages = self.thumbnailView.selectionIndexPaths
  637. if (pages.isEmpty == false) {
  638. if self.fileAttri_ == nil {
  639. self.fileAttri_ = KMFileAttribute()
  640. self.fileAttri_?.password = self.thumbnailView.document?.password ?? ""
  641. self.fileAttri_?.filePath = self.thumbnailView.document?.documentURL.path ?? ""
  642. }
  643. let fileAttribute = self.fileAttri_
  644. fileAttribute?.bAllPage = false
  645. fileAttribute?.pagesType = .custom
  646. fileAttribute?.pdfDocument = pdf
  647. fileAttribute?.pagesString = KMPageRangeTools.newParseSelectedIndexs(selectedIndex: self.indexpathsToIndexs(indexpaths: pages).sorted())
  648. fileAttribute?.pageCnt = Int(self.thumbnailView.document?.pageCount ?? 0)
  649. let pages = fileAttribute?.fetchSelectPages() ?? []
  650. if pages.isEmpty {
  651. KMAlertTool.runModelForMainThread(message: KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
  652. } else {
  653. let panel = NSOpenPanel()
  654. panel.canChooseFiles = false
  655. panel.canChooseDirectories = true
  656. panel.canCreateDirectories = true
  657. panel.beginSheetModal(for: self.view.window!) { result in
  658. if (result == .OK) {
  659. let outputURL = panel.urls.first!
  660. var tFolderPath = outputURL.path
  661. var index = 0
  662. var folderName = self.pdfDocument?.documentURL.deletingPathExtension().lastPathComponent
  663. if folderName == nil {
  664. folderName = KMLocalizedString("Untitled", nil)
  665. }
  666. var folderPath = "\(tFolderPath)/\(folderName ?? "")"
  667. //创建目录
  668. while (FileManager.default.fileExists(atPath: folderPath)) {
  669. index += 1
  670. folderPath = "\(tFolderPath)/\(folderName ?? "")_\(index)"
  671. // folderPath = [tFolderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%ld",folderName,index]];
  672. }
  673. tFolderPath = folderPath
  674. let result = try?FileManager.default.createDirectory(atPath: tFolderPath, withIntermediateDirectories: true)
  675. if (result == nil) {
  676. // if (![[NSFileManager defaultManager] createDirectoryAtPath:tFolderPath withIntermediateDirectories:YES attributes:nil error:nil]) {
  677. KMAlertTool.runModelForMainThread(message: KMLocalizedString("Failed to split!", nil))
  678. return
  679. }
  680. let vc = KMProgressWindowController()
  681. NSApp.beginSheet(vc.window!, modalFor: self.view.window!, modalDelegate: nil, didEnd: nil, contextInfo: nil)
  682. DispatchQueue.global().async {
  683. // NSArray * successArray = [self.pdfDocument splitByFileWithPages:fileAttribute folerPath:tFolderPath fileName:folderName];
  684. let successArray = self.pdfDocument?.splitByFileWithPages(fileAttribute!, folerPath: tFolderPath, fileName: folderName ?? "") ?? []
  685. DispatchQueue.main.async {
  686. if successArray.count > 0 {
  687. let filePath = successArray.first ?? ""
  688. let workSpace = NSWorkspace.shared
  689. workSpace.activateFileViewerSelecting([URL(fileURLWithPath: filePath)])
  690. } else {
  691. KMAlertTool.runModelForMainThread(message: KMLocalizedString("Failed to split!", nil))
  692. }
  693. NSApp.endSheet(vc.window!)
  694. vc.close()
  695. }
  696. }
  697. }
  698. }
  699. }
  700. } else{
  701. self._progressHUD(message: KMLocalizedString(Self.kNoPagesSelectedTipMessage))
  702. }
  703. }
  704. override func reverseAction() {
  705. if IAPProductsManager.default().isAvailableAllFunction() == false {
  706. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  707. return
  708. }
  709. let cnt = self.thumbnailView.selectionIndexPaths.count
  710. if cnt < 2 {
  711. self._progressHUD(message: KMLocalizedString("No page selected. Please select at least two pages to organize."))
  712. return
  713. }
  714. self.trackEvent(type: .reverse)
  715. self.toolBar.unSelectItem(for: KMToolbarPageEditReverseItemIdentifier)
  716. self.reverseMenuAction(sender: nil)
  717. }
  718. func showSplitWindow(callback: @escaping (KMPageEditSplitWindowController, KMPageEditSplitSettingModel)->Void) {
  719. let model = KMPageEditSplitSettingModel()
  720. model.documentURL = self.listView?.document.documentURL
  721. if let pagecnt = self.thumbnailView.document?.pageCount {
  722. model.pageCount = Int(pagecnt)
  723. }
  724. model.fileName = model.documentURL?.lastPathComponent ?? ""
  725. model.pathExtension = model.fileName?.components(separatedBy: ".").last
  726. if (self.isEmptySelection() == false) {
  727. model.type = 2
  728. model.pageRangeType = 2
  729. var selectedIndexs: Array<Int> = []
  730. for indexpath in self.thumbnailView.selectionIndexPaths {
  731. selectedIndexs.append(indexpath.item)
  732. }
  733. model.pageRangeString = KMPageRangeTools.newParseSelectedIndexs(selectedIndex: selectedIndexs)
  734. } else {
  735. model.type = 0
  736. }
  737. let windowController = KMPageEditSplitWindowController(model)
  738. self.km_beginSheet(windowC: windowController)
  739. windowController.itemClick = { [weak self] index, value in
  740. if (index == 1) { /// 取消
  741. self?.km_endSheet()
  742. return
  743. }
  744. guard let _windowC = self?.kmCurrentWindowC as? KMPageEditSplitWindowController else {
  745. self?.km_endSheet()
  746. return
  747. }
  748. callback(_windowC, model)
  749. }
  750. }
  751. private func _splitMethod(windowC: KMPageEditSplitWindowController, model: KMPageEditSplitSettingModel, limit: Bool = false) {
  752. let outputModel = windowC.model as! KMPageEditSplitSettingModel
  753. DispatchQueue.main.async {
  754. let panel = NSOpenPanel()
  755. panel.canChooseFiles = false
  756. panel.canChooseDirectories = true
  757. panel.canCreateDirectories = true
  758. panel.beginSheetModal(for: NSWindow.currentWindow()) { response in
  759. if (response == .cancel) {
  760. return
  761. }
  762. let file_indexs = outputModel.getSplitIndexSets ?? []
  763. if (file_indexs.count <= 0) {
  764. return
  765. }
  766. DispatchQueue.main.async {
  767. self.showProgressWindow(message: NSLocalizedString("Spliting...", comment: ""))
  768. self.progressC?.maxValue = Double(file_indexs.count)
  769. let _document = self.listView?.document
  770. let filePath = "\(panel.url!.path)/\(_document?.documentURL.deletingPathExtension().lastPathComponent ?? "")"
  771. let uniqueFilePath = KMTools.getUniqueFilePath(filePath: filePath)
  772. // Swift.debugPrint(uniqueFilePath)
  773. if (!FileManager.default.fileExists(atPath: uniqueFilePath)) {
  774. try?FileManager.default.createDirectory(atPath: uniqueFilePath, withIntermediateDirectories: true)
  775. }
  776. var filepaths: [String] = []
  777. for i in 0 ..< file_indexs.count {
  778. let indexs = file_indexs[i]
  779. if (indexs.isEmpty) {
  780. continue
  781. }
  782. let filepath = String(format: "%@/%@.pdf", uniqueFilePath, model.getNewOutputFileName(index: i+1))
  783. filepaths.append(filepath)
  784. let newDocument = CPDFDocument()
  785. newDocument?.importPages(indexs, from: _document, at: 0)
  786. self.progressC?.increment(by: 1.0)
  787. newDocument?.write(to: URL(fileURLWithPath: filepath))
  788. }
  789. self.hiddenProgressWindow()
  790. self.km_endSheet()
  791. if let _urlString = filepaths.first?.deletingLastPathComponent {
  792. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: _urlString)])
  793. }
  794. }
  795. }
  796. }
  797. }
  798. // MARK: Undo / Redo
  799. @objc private func reversePages(indexs: IndexSet) {
  800. if let doc = self.thumbnailView.document {
  801. KMPageEditTools.reverse(doc, indexs
  802. ) { result, error in
  803. if (result) {
  804. self.refreshUI(indexpaths: self.indexsToIndexpaths(indexs: indexs), keepSelected: true)
  805. self.kmUndoManager?.registerUndo(withTarget: self, selector: #selector(self.reversePages), object: indexs)
  806. }
  807. }
  808. }
  809. }
  810. @objc private func dragPages(_ indexs: IndexSet, _ toIndex: Int, dp: NSCollectionView.DropOperation, reversed: Bool = false) {
  811. if (indexs.count <= 0) {
  812. return
  813. }
  814. self.listViewCurrentPageUpdateForDrag(indexs: indexs, toIndex: toIndex)
  815. // 插入位置偏移量
  816. var toIndexOffset: Int = 0
  817. // 需要移动的页面index数组
  818. var newPages: [CPDFPage] = []
  819. var currentPage: CPDFPage?
  820. var originalIndex = self.listViewCurrentIndex
  821. // 从后面往前删除
  822. for index in indexs.reversed() {
  823. let newPage = (self.pdfDocument?.page(at: UInt(index)))?.copy() as? CPDFPage
  824. if (newPage == nil) {
  825. continue
  826. }
  827. newPages.insert(newPage!, at: 0)
  828. self.pdfDocument?.removePage(at: UInt(index))
  829. if (index < toIndex) { /// 删除了插入位置前面的页面,需要改变 toIndex
  830. toIndexOffset += 1
  831. }
  832. if (currentPage == nil) && (originalIndex == index) {
  833. currentPage = newPage
  834. }
  835. }
  836. if (indexs.last! < toIndex && dp == .on) { // 往后拖拽
  837. toIndexOffset -= 1
  838. }
  839. let insertIndex = max(0, toIndex-toIndexOffset)
  840. var selectIndexs: IndexSet = []
  841. for (index, page) in newPages.enumerated() {
  842. self.pdfDocument?.insertPageObject(page, at: UInt(insertIndex+index))
  843. selectIndexs.insert(insertIndex+index)
  844. if (currentPage?.pageIndex() == page.pageIndex()) {
  845. // self._listViewCurrentIndex = insertIndex+index
  846. }
  847. }
  848. // 新增 undo
  849. self.kmUndoManager?.registerUndo(withTarget: self) { target in
  850. target.clearSelectPages()
  851. target.dragPages_reversed(indexs, toIndex, newPages, dp: dp)
  852. }
  853. // 刷新UI
  854. self.refreshUI()
  855. if (reversed == false) {
  856. self.selectPages(indexs: selectIndexs)
  857. }
  858. }
  859. @objc private func dragPages_reversed(_ indexs: IndexSet, _ toIndex: Int, _ pages: [CPDFPage], dp: NSCollectionView.DropOperation) {
  860. if (indexs.count <= 0) {
  861. return
  862. }
  863. // 插入位置偏移量
  864. var toIndexOffset: Int = 0
  865. for index in indexs {
  866. if (index < toIndex) { /// 删除了插入位置前面的页面,需要改变 toIndex
  867. toIndexOffset += 1
  868. }
  869. }
  870. if (indexs.last! < toIndex && dp == .on) { // 往后拖拽
  871. toIndexOffset -= 1
  872. }
  873. let insertIndex = max(0, toIndex-toIndexOffset)
  874. // 需要移动的页面index数组
  875. var datas: [Int] = []
  876. for index in insertIndex ..< (insertIndex + indexs.count) {
  877. datas.append(index)
  878. }
  879. // 从后面开始删除
  880. for index in datas.reversed() {
  881. self.pdfDocument?.removePage(at: UInt(index))
  882. }
  883. // 再插入到对应的位置
  884. for (index,value) in indexs.enumerated() {
  885. var page: CPDFPage?
  886. if (index < pages.count) {
  887. page = pages[index]
  888. }
  889. if (page != nil) {
  890. self.pdfDocument?.insertPageObject(page!, at: UInt(value))
  891. }
  892. }
  893. // 新增 undo
  894. self.kmUndoManager?.registerUndo(withTarget: self) { target in
  895. target.dragPages(indexs, toIndex, dp: dp, reversed: true)
  896. }
  897. // 刷新UI
  898. self.refreshUI()
  899. }
  900. // MARK: topBar 事件
  901. @objc func zoomOutButtonAction(sender: NSButton?) {
  902. if (self.itemSize.width > self.defaultItemSize.width) {
  903. return
  904. }
  905. if let _document = self.thumbnailView.document {
  906. for i in 0 ..< _document.pageCount {
  907. let page = _document.page(at: i)
  908. page?.pageEditImage = nil
  909. }
  910. }
  911. if (self.itemSize.width < self.defaultItemSize.width) {
  912. self.itemSize = self.defaultItemSize
  913. } else if (self.itemSize.width == self.defaultItemSize.width) {
  914. self.itemSize = NSSize(width: self.defaultItemSize.width*2, height: self.defaultItemSize.height*2)
  915. }
  916. // self.toolBar.zoomInButton?.isEnabled = true
  917. // if (self.itemSize.width > self.defaultItemSize.width) {
  918. // sender?.isEnabled = false
  919. // }
  920. self.pageEditThumbnailView.zoomOut()
  921. }
  922. @objc func zoomInButtonAction(sender: NSButton?) {
  923. if (self.itemSize.width < self.defaultItemSize.width) {
  924. return
  925. }
  926. if let _document = self.thumbnailView.document {
  927. for i in 0 ..< _document.pageCount {
  928. let page = _document.page(at: i)
  929. page?.pageEditImage = nil
  930. }
  931. }
  932. if (self.itemSize.width > self.defaultItemSize.width) {
  933. self.itemSize = self.defaultItemSize
  934. } else if (self.itemSize.width == self.defaultItemSize.width) {
  935. self.itemSize = NSSize(width: self.defaultItemSize.width*0.5, height: self.defaultItemSize.height*0.5)
  936. }
  937. // self.toolBar.zoomOutButton?.isEnabled = true
  938. // if (self.itemSize.width < self.defaultItemSize.width) {
  939. // sender?.isEnabled = false
  940. // }
  941. self.pageEditThumbnailView.zoomIn()
  942. }
  943. // MARK: menu 菜单事件
  944. override func menuNeedsUpdate(_ menu: NSMenu) {
  945. // let row = self.thumbnailView.collectionView.selectionIndexes
  946. // let point = self.thumbnailView.collectionView.convert(NSEvent.mouseLocation, to: nil)
  947. let point = self.view.window?.currentEvent?.locationInView(self.thumbnailView.collectionView) ?? .zero
  948. let indexpaths = self.thumbnailView.collectionView.indexPathForItem(at: point)
  949. menu.removeAllItems()
  950. if indexpaths == nil || indexpaths!.isEmpty { // 空白处点击
  951. if KMThumbnailManager.manager.copyPages.isEmpty { // 不能粘贴
  952. // if self.canPaste() {
  953. return
  954. }
  955. let item_ = menu.addItem(withTitle: NSLocalizedString("Paste", comment: ""), action: #selector(pasteActonForEmpty), target: self)
  956. // item_?.keyEquivalent = KMKeyEquivalent.paste
  957. let repObj = self.thumbnailView.collectionView.km_indexPathForItem(at: point, nearest: true)
  958. item_?.representedObject = repObj
  959. return
  960. }
  961. super.menuNeedsUpdate(menu)
  962. }
  963. @objc func item_insertCustomPage(sender: NSMenuItem?) {
  964. if IAPProductsManager.default().isAvailableAllFunction() == false {
  965. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  966. return
  967. }
  968. guard let doc = self.pdfDocument else {
  969. NSSound.beep()
  970. return
  971. }
  972. let windowC = KMPDFEditInsertBlankPageWindow(document: doc)
  973. windowC.currentPage = self.getSelecteIndex() + 1
  974. windowC.insertLocation = 3
  975. windowC.callback = { [weak self] pdfDoc, _, pages, insertI in
  976. if pages != nil {
  977. // mm 单位的大小
  978. guard let _winC = self?.kmCurrentWindowC as? KMPDFEditInsertBlankPageWindow else {
  979. self?.km_endSheet()
  980. return
  981. }
  982. var pageSize = _winC.pageSize
  983. let direction = windowC.pageRotation == 0 ? 0 : 1
  984. if (direction == 0) { // 纵向
  985. if (pageSize.width > pageSize.height) { // 需要交换
  986. let tmp = pageSize.width
  987. pageSize.width = pageSize.height
  988. pageSize.height = tmp
  989. } else {
  990. // no things.
  991. }
  992. } else { // 横向
  993. if (pageSize.width > pageSize.height) {
  994. // no things.
  995. } else { // 需要交换
  996. let tmp = pageSize.width
  997. pageSize.width = pageSize.height
  998. pageSize.height = tmp
  999. }
  1000. }
  1001. /// 插入位置
  1002. let document = CPDFDocument()
  1003. document?.insertPage(pageSize, at: 0)
  1004. if let page: CPDFPage = (document?.page(at: 0)) {
  1005. self?.insertPages(pages: [page], at: IndexSet(integer: insertI))
  1006. }
  1007. let ips: Set<IndexPath> = [IndexPath(item: insertI, section: 0)]
  1008. self?.thumbnailView.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
  1009. }
  1010. self?.km_endSheet()
  1011. }
  1012. self.km_beginSheet(windowC: windowC)
  1013. }
  1014. func extractSelectPageItemAction() {
  1015. if IAPProductsManager.default().isAvailableAllFunction() == false {
  1016. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  1017. return
  1018. }
  1019. let indexs = self.thumbnailView.selectionIndexPaths
  1020. if indexs.isEmpty {
  1021. self._progressHUD(message: KMLocalizedString(Self.kNoPagesSelectedTipMessage))
  1022. return
  1023. }
  1024. self.extractPages(indexpaths: indexs, oneDocumentPerPage: false) { result, params in }
  1025. }
  1026. // MARK: toolBar 菜单事件
  1027. @objc func reverseMenuAction(sender: AnyObject?) {
  1028. let selectedIndexs = self.thumbnailView.selectionIndexPaths
  1029. if (selectedIndexs.count < 2 ) {
  1030. let alertView = CustomAlertView.alertView(message: NSLocalizedString("Please select at least two pages for editing first", comment: ""), fromView: self.thumbnailView, withStyle: .blue)
  1031. alertView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  1032. alertView?.layer?.cornerRadius = 4
  1033. return
  1034. }
  1035. self.reversePages(indexs: self.indexpathsToIndexs(indexpaths: selectedIndexs))
  1036. self.dealPdfDocumentDidEditCallback(type: .reverse)
  1037. }
  1038. override func addMenuItem(menu: NSMenu, item: KMPDFThumbViewMenu.Item) -> NSMenuItem? {
  1039. var item_: NSMenuItem?
  1040. if (item.sel == KMPDFThumbViewMenu.insertSelector) {
  1041. item_ = menu.addItem(withTitle: NSLocalizedString("Blank Page", comment: ""), action: #selector(insertPageItemAction), target: self)
  1042. } else if (item.sel == KMPDFThumbViewMenu.splitSelector) {
  1043. if (self.thumbnailView.selectionIndexPaths.count > 1) {
  1044. item_ = menu.addItem(withTitle: NSLocalizedString("Split", comment: ""), action: KMPDFThumbViewMenu.splitSelector, target: self)
  1045. }
  1046. } else if (item.sel == KMPDFThumbViewMenu.reverseSelector) {
  1047. if (self.thumbnailView.selectionIndexPaths.count > 1) {
  1048. item_ = menu.addItem(withTitle: NSLocalizedString("Reverse", comment: ""), action: KMPDFThumbViewMenu.reverseSelector, target: self)
  1049. }
  1050. } else {
  1051. return super.addMenuItem(menu: menu, item: item)
  1052. }
  1053. return item_
  1054. }
  1055. // MARK: - Zoom Methods
  1056. @IBAction func buttonClicked_Bigger(_ sender: AnyObject?) {
  1057. // if (self.thumbnailView.thumbnailSize.width == 100.) {
  1058. // self.thumbnailView.thumbnailSize = CGSizeMake(200, 200);
  1059. // } else if (self.thumbnailView.thumbnailSize.width == 200.) {
  1060. // self.thumbnailView.thumbnailSize = CGSizeMake(400, 400);
  1061. // }
  1062. self.zoomOutButtonAction(sender: sender as? NSButton)
  1063. }
  1064. @IBAction func buttonClicked_Smaller(_ sender: AnyObject?) {
  1065. // if (self.thumbnailView.thumbnailSize.width == 400.) {
  1066. // self.thumbnailView.thumbnailSize = CGSizeMake(200, 200);
  1067. // } else if (self.thumbnailView.thumbnailSize.width == 200.) {
  1068. // self.thumbnailView.thumbnailSize = CGSizeMake(100, 100);
  1069. // }
  1070. self.zoomInButtonAction(sender: sender as? NSButton)
  1071. }
  1072. override func mouseMoved(with event: NSEvent) {
  1073. super.mouseMoved(with: event)
  1074. self.view.window?.mouseMoved(with: event)
  1075. }
  1076. }
  1077. extension KMPDFEditViewController {
  1078. //MARK: menu Action
  1079. @objc func insertPageItemAction(menu:NSMenuItem) {
  1080. if (menu.tag == 0) {
  1081. self.insertBlankPageAction()
  1082. } else if (menu.tag == 1) {
  1083. self.item_insertCustomPage(sender: nil)
  1084. } else if (menu.tag == 2) {
  1085. self.insertFileAction()
  1086. }
  1087. }
  1088. @objc func sharePageItemAction(menu:NSMenuItem) {
  1089. guard let item = menu.parent else {
  1090. NSSound.beep()
  1091. return
  1092. }
  1093. let index = (item.representedObject as? IndexSet)?.first ?? -1
  1094. if Int(index) >= 0 {
  1095. let doucument = self.pdfDocument
  1096. // let page = doucument.page(at: UInt(index))
  1097. let filename : String = doucument?.documentURL.lastPathComponent ?? ""
  1098. let folderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(filename)) ?? ""
  1099. try? FileManager.default.removeItem(atPath: folderPath)
  1100. let pdfdocument = CPDFDocument()
  1101. let ret = pdfdocument?.importPages((item.representedObject as? IndexSet) ?? [], from: self.thumbnailView.document, at: 0) ?? false
  1102. let url = URL(fileURLWithPath: folderPath)
  1103. if ret {
  1104. let success = pdfdocument?.write(to:url) ?? false
  1105. // if success {
  1106. // let workspace = NSWorkspace.shared
  1107. // workspace.activateFileViewerSelecting([url])
  1108. // }
  1109. }
  1110. let represent = menu.representedObject as? NSSharingService
  1111. represent?.perform(withItems: [url])
  1112. }
  1113. }
  1114. @objc func showPageSizeItemAction(menu: NSMenuItem) {
  1115. self.thumbnailView.isShowPageSize = !self.thumbnailView.isShowPageSize
  1116. self.refreshUIForKeepSelecteds()
  1117. }
  1118. @objc func printItemAction(menu:NSMenuItem) {
  1119. var datas: [Int] = []
  1120. for indexpath in self.thumbnailView.selectionIndexPaths {
  1121. datas.append(indexpath.item)
  1122. }
  1123. guard let callback = self.itemClick else {
  1124. return
  1125. }
  1126. callback(2, datas.sorted())
  1127. }
  1128. override func menuItemAction_extract(_ sender: Any?) {
  1129. self.extractSelectPageItemAction()
  1130. }
  1131. }
  1132. extension KMPDFEditViewController {
  1133. override func menuItemAction_print(_ sender: Any?) {
  1134. if let item = sender as? NSMenuItem {
  1135. self.printItemAction(menu: item)
  1136. }
  1137. }
  1138. override func menuItemAction_showPageSize(_ sender: Any?) {
  1139. if let item = sender as? NSMenuItem {
  1140. self.showPageSizeItemAction(menu: item)
  1141. }
  1142. }
  1143. override func menuItemAction_share(_ sender: Any?) {
  1144. if let item = sender as? NSMenuItem {
  1145. self.sharePageItemAction(menu: item)
  1146. }
  1147. }
  1148. }
  1149. // MARK: - KMSelectPopButtonDelegate
  1150. extension KMPDFEditViewController {
  1151. func km_comboBoxSelectionDidChange(_ obj: KMPageRange) {
  1152. self.pageIsUpdate = false
  1153. var indexpaths: Set<IndexPath> = []
  1154. let type: KMPageRange? = obj
  1155. let pageCnt = self.pdfDocument?.pageCount ?? 0
  1156. for i in 0 ..< pageCnt {
  1157. if (type == .all) {
  1158. indexpaths.insert(IndexPath(item: Int(i), section: 0))
  1159. } else if (type == .odd) {
  1160. if (i % 2 == 1) {
  1161. continue
  1162. }
  1163. indexpaths.insert(IndexPath(item: Int(i), section: 0))
  1164. } else if (type == .even) {
  1165. if (i % 2 == 0) {
  1166. continue
  1167. }
  1168. indexpaths.insert(IndexPath(item: Int(i), section: 0))
  1169. } else if (type == .horizontal) {
  1170. let page = self.pdfDocument?.page(at: i)
  1171. guard let isVertical = page?.isVertical(), !isVertical else {
  1172. continue
  1173. }
  1174. indexpaths.insert(IndexPath(item: Int(i), section: 0))
  1175. } else if (type == .vertical) {
  1176. let page = self.pdfDocument?.page(at: i)
  1177. guard let isHorizontal = page?.isHorizontal(), !isHorizontal else {
  1178. continue
  1179. }
  1180. indexpaths.insert(IndexPath(item: Int(i), section: 0))
  1181. }
  1182. }
  1183. if (type == .custom) {
  1184. let windowC = KMPDFEditPageRangeWindowController()
  1185. windowC.pdfDocument = self.pdfDocument
  1186. windowC.pageSelectCallBack = { [weak self] pagesArray in
  1187. var indexpaths = Set<IndexPath>()
  1188. for page in pagesArray {
  1189. indexpaths.insert(IndexPath(item: page-1, section: 0))
  1190. }
  1191. self?.km_endSheet()
  1192. self?.selectPages(indexpaths: indexpaths)
  1193. self?.thumbnailView.collectionView.scrollToItems(at: indexpaths, scrollPosition: .bottom)
  1194. }
  1195. self.km_beginSheet(windowC: windowC)
  1196. } else {
  1197. self.pageEditThumbnailView.selectPages(at: indexpaths)
  1198. }
  1199. }
  1200. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  1201. self.view.window?.makeFirstResponder(self)
  1202. for index in 0 ..< obj.numberOfItems {
  1203. if (obj.items[index] == obj.stringValue) {
  1204. return
  1205. }
  1206. }
  1207. let pages = KMPageRangeTools.findSelectPage(pageRangeString: self.toolBar.pageRangeView?.stringValue ?? "", pageCount: Int((self.listView?.document.pageCount) ?? 0))
  1208. if (pages.isEmpty) {
  1209. let alert = NSAlert()
  1210. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  1211. alert.runModal()
  1212. self.toolBar.setPageRangeString("")
  1213. DispatchQueue.main.async {
  1214. self.view.window?.makeFirstResponder(self.toolBar.pageRangeView?.textField)
  1215. }
  1216. return
  1217. }
  1218. var indexpaths: Set<IndexPath> = []
  1219. for page in pages {
  1220. if (page >= 1) {
  1221. indexpaths.insert(IndexPath(item: page-1, section: 0))
  1222. }
  1223. }
  1224. self.pageIsUpdate = false
  1225. self.pageEditThumbnailView.selectPages(at: indexpaths)
  1226. }
  1227. }
  1228. // MARK: -
  1229. // MARK: KMThumbnailViewDelegate
  1230. extension KMPDFEditViewController: KMThumbnailViewDelegate {
  1231. func thumbnailView(thumbanView: KMThumbnailView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
  1232. return IAPProductsManager.default().isAvailableAllFunction()
  1233. }
  1234. func thumbnailView(thumbanView: KMThumbnailView, shouldAcceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  1235. return true
  1236. }
  1237. func thumbnailView(thumbanView: KMThumbnailView, didDragAddFiles files: [URL], indexpath: IndexPath) {
  1238. self.km_add_file_multi(fileUrls: files) { index, params in
  1239. if (self.fetchProgressBlockParamsIsPasswordFile(params: params)) { // 加密文档进度回调
  1240. return
  1241. }
  1242. let _fileUrl = self.fetchProgressBlockParamsForFileUrl(params: params)
  1243. if let exn = _fileUrl?.pathExtension, KMTools.isPDFType(exn) {
  1244. if (_fileUrl!.path.isPDFValid() == false) {
  1245. let alert = NSAlert()
  1246. alert.alertStyle = .critical
  1247. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  1248. alert.runModal()
  1249. }
  1250. }
  1251. } completionBlock: { [unowned self] documents in
  1252. var insertIndex: Int = indexpath.item
  1253. var pages: Array<CPDFPage> = []
  1254. var indexs = IndexSet()
  1255. self.model.insertedDocumentSet.formUnion(documents)
  1256. for document in documents {
  1257. for i in 0 ..< document.pageCount {
  1258. if let page = document.page(at: i) {
  1259. pages.append(page)
  1260. indexs.insert(insertIndex)
  1261. insertIndex += 1
  1262. }
  1263. }
  1264. }
  1265. self.insertPages(pages: pages, at: indexs)
  1266. }
  1267. }
  1268. func thumbnailView(thumbanView: KMThumbnailView, didDrag dragedIndexPaths: [IndexPath], indexpath: IndexPath, dragInfo: [KMThumbnailViewDragInfoKey.RawValue : Any]) {
  1269. let toIndex = max(0, indexpath.item)
  1270. var indexs = IndexSet()
  1271. for indexpath in dragedIndexPaths {
  1272. indexs.insert(indexpath.item)
  1273. }
  1274. var dp = NSCollectionView.DropOperation.on
  1275. if let _dp = dragInfo[KMThumbnailViewDragInfoKey.dropOperation.rawValue] as? NSCollectionView.DropOperation {
  1276. dp = _dp
  1277. }
  1278. self.dragPages(indexs, toIndex, dp: dp)
  1279. if (dragedIndexPaths.count > 0) {
  1280. self.dealPdfDocumentDidEditCallback()
  1281. }
  1282. }
  1283. func thumbnailView(thumbanView: KMThumbnailView, shouldPasteboardWriterForItemAt indexPath: IndexPath) -> Bool {
  1284. // if (!self.canPayFunction) {
  1285. // return false
  1286. // }
  1287. return true
  1288. }
  1289. func thumbnailView(thumbanView: KMThumbnailView, insetForSectionAt section: Int) -> NSEdgeInsets {
  1290. return NSEdgeInsets(top: 6, left: 40, bottom: 0, right: 40)
  1291. }
  1292. func thumbnailView(thumbanView: KMThumbnailView, sizeForItemAt indexpath: IndexPath) -> NSSize {
  1293. self.thumbnailView.thumbnailSzie = CGSize(width: 120, height: 155)
  1294. if (!self.thumbnailView.isShowPageSize) {
  1295. return NSMakeSize(self.itemSize.width, self.itemSize.height+26)
  1296. }
  1297. return self.itemSize
  1298. }
  1299. func thumbnailView(thumbanView: KMThumbnailView, numberOfItemsInSection section: Int) -> Int {
  1300. if let count = self.pdfDocument?.pageCount {
  1301. return Int(count)
  1302. }
  1303. return 0
  1304. }
  1305. func thumbnailView(thumbanView: KMThumbnailView, itemForRepresentedObjectAt indexpath: IndexPath) -> NSCollectionViewItem {
  1306. let cellView: KMPageEditThumbnailItem = thumbanView.collectionView.km.dequeueReusableCell(for: indexpath)
  1307. cellView.isShowPageSize = self.thumbnailView.isShowPageSize
  1308. if let data = self.pdfDocument?.page(at: UInt(indexpath.item)) {
  1309. cellView.setPage(page: data)
  1310. }
  1311. cellView.doubleClickAction = { [unowned self] _ in
  1312. guard let callback = self.itemClick else {
  1313. return
  1314. }
  1315. callback(1, indexpath.item)
  1316. }
  1317. cellView.mouseDownAction = { [unowned self] _ in
  1318. let selectionIndexPaths = self.thumbnailView.selectionIndexPaths
  1319. if (selectionIndexPaths.count == 1 && selectionIndexPaths.first?.item == indexpath.item) {
  1320. return
  1321. }
  1322. self.selectPages(indexs: IndexSet.init(integer: indexpath.item))
  1323. }
  1324. cellView.rightMouseDownAction = { [unowned self] _ in
  1325. self.selectPages(indexs: IndexSet.init(integer: indexpath.item))
  1326. }
  1327. return cellView
  1328. }
  1329. }
  1330. // MARK: - 系统菜单栏
  1331. extension KMPDFEditViewController {
  1332. override func undo(_ sender: Any?) {
  1333. super.undo(sender)
  1334. self.km_becomeFirstResponder()
  1335. }
  1336. override func redo(_ sender: Any?) {
  1337. super.redo(sender)
  1338. self.km_becomeFirstResponder()
  1339. }
  1340. }
  1341. // MARK: - KMExtensions
  1342. extension KMPDFEditViewController {
  1343. override func deleteAfter(indexpaths: Set<IndexPath>) {
  1344. self.clearSelectPages()
  1345. self.km_becomeFirstResponder()
  1346. }
  1347. override func copyAfter(indexpaths: Set<IndexPath>) {
  1348. self.km_becomeFirstResponder()
  1349. }
  1350. override func pasteAfter(at index: Int) {
  1351. self.km_becomeFirstResponder()
  1352. }
  1353. override func cutAfter(indexpaths: Set<IndexPath>) {
  1354. self.clearSelectPages()
  1355. self.km_becomeFirstResponder()
  1356. }
  1357. override func leftRotateAfter(indexpaths: Set<IndexPath>) {
  1358. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMPDFViewRotatePage"), object: self.thumbnailView.document)
  1359. self.km_becomeFirstResponder()
  1360. }
  1361. override func rightRotateAfter(indexpaths: Set<IndexPath>) {
  1362. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMPDFViewRotatePage"), object: self.thumbnailView.document)
  1363. self.km_becomeFirstResponder()
  1364. }
  1365. override func replaceAfter(of targetIndexs: IndexSet, with pages: [CPDFPage], at index: Int) {
  1366. var indexpaths = Set<IndexPath>()
  1367. var insertIndex = index
  1368. for _ in pages {
  1369. indexpaths.insert(IndexPath(item: insertIndex, section: 0))
  1370. insertIndex += 1
  1371. }
  1372. self.thumbnailView.selectionIndexPaths = indexpaths
  1373. self.dealPdfDocumentDidEditCallback(type: .replace)
  1374. }
  1375. override func insertBlankPageAfter(size: NSSize, at index: Int) {
  1376. self.selectPages(indexs: IndexSet(integer: index))
  1377. let ips: Set<IndexPath> = [IndexPath(item: index, section: 0)]
  1378. self.thumbnailView.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
  1379. self.dealPdfDocumentDidEditCallback(type: .insert)
  1380. }
  1381. func listViewCurrentPageUpdateForDrag(indexs: IndexSet, toIndex: Int) {
  1382. if (indexs.isEmpty) {
  1383. return
  1384. }
  1385. // listView 当前页面 被拖拽
  1386. if (indexs.contains(self._listViewCurrentIndex)) {
  1387. // let idx = indexs.firstIndex(of: self._listViewCurrentIndex)
  1388. var idx: Int = 0
  1389. for index in indexs {
  1390. if (index == self._listViewCurrentIndex) {
  1391. break
  1392. }
  1393. idx += 1
  1394. }
  1395. // 插入位置偏移量
  1396. var toIndexOffset: Int = 0
  1397. // 从后面往前删除
  1398. for index in indexs.reversed() {
  1399. let newPage = (self.listView?.document.page(at: UInt(index)))?.copy() as? CPDFPage
  1400. if (newPage == nil) {
  1401. continue
  1402. }
  1403. if (index < toIndex) { /// 删除了插入位置前面的页面,需要改变 toIndex
  1404. toIndexOffset += 1
  1405. }
  1406. }
  1407. if (indexs.last! < toIndex) { // 往后拖拽
  1408. toIndexOffset -= 1
  1409. }
  1410. let insertIndex = max(0, toIndex-toIndexOffset)
  1411. self._listViewCurrentIndex = insertIndex+idx
  1412. } else {
  1413. var forwardCnt = 0
  1414. var nextCnt = 0
  1415. for index in indexs {
  1416. if (index > self._listViewCurrentIndex) {
  1417. nextCnt += 1
  1418. } else {
  1419. forwardCnt += 1
  1420. }
  1421. }
  1422. if (self._listViewCurrentIndex > toIndex) { // 插入到目标前面, 前面不管,只管后面的格式
  1423. self._listViewCurrentIndex += nextCnt
  1424. } else if (self._listViewCurrentIndex < toIndex) { // 插入到目标后面, 后面不管,只管前面的格式
  1425. self._listViewCurrentIndex = max(0, self._listViewCurrentIndex - forwardCnt)
  1426. } else { // 插入到目标位置 [跟插入到目标后面一样]
  1427. self._listViewCurrentIndex = max(0, self._listViewCurrentIndex - forwardCnt)
  1428. }
  1429. }
  1430. }
  1431. }
  1432. // MARK: - Analytics (埋点)
  1433. extension KMPDFEditViewController {
  1434. func trackEvent(type: KMPageEditType) -> Void {
  1435. if (type == .insert) {
  1436. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Insert", parameters: [
  1437. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1438. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1439. } else if (type == .extract) {
  1440. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Extract", parameters: [
  1441. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1442. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1443. } else if (type == .leftRotate || type == .rightRotate) {
  1444. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Rotate", parameters: [
  1445. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1446. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1447. } else if (type == .replace) {
  1448. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Replace", parameters: [
  1449. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1450. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1451. } else if (type == .split) {
  1452. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Split", parameters: [
  1453. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1454. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1455. } else if (type == .reverse) {
  1456. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Reverse", parameters: [
  1457. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1458. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1459. } else if (type == .delete) {
  1460. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Delete", parameters: [
  1461. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1462. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1463. }
  1464. }
  1465. func trackEvent_copy() -> Void {
  1466. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Copy", parameters: [
  1467. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1468. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1469. }
  1470. }