KMPDFEditViewController.swift 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  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 = { [weak self] type in
  183. self?.km_comboBoxSelectionDidChange(type)
  184. }
  185. toolBar.pageRangeValueDidChange = { [weak 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 (self.itemSize.width < self.defaultItemSize.width) {
  906. self.itemSize = self.defaultItemSize
  907. } else if (self.itemSize.width == self.defaultItemSize.width) {
  908. self.itemSize = NSSize(width: self.defaultItemSize.width*2, height: self.defaultItemSize.height*2)
  909. }
  910. // self.toolBar.zoomInButton?.isEnabled = true
  911. // if (self.itemSize.width > self.defaultItemSize.width) {
  912. // sender?.isEnabled = false
  913. // }
  914. self.pageEditThumbnailView.zoomOut()
  915. }
  916. @objc func zoomInButtonAction(sender: NSButton?) {
  917. if (self.itemSize.width < self.defaultItemSize.width) {
  918. return
  919. }
  920. if (self.itemSize.width > self.defaultItemSize.width) {
  921. self.itemSize = self.defaultItemSize
  922. } else if (self.itemSize.width == self.defaultItemSize.width) {
  923. self.itemSize = NSSize(width: self.defaultItemSize.width*0.5, height: self.defaultItemSize.height*0.5)
  924. }
  925. // self.toolBar.zoomOutButton?.isEnabled = true
  926. // if (self.itemSize.width < self.defaultItemSize.width) {
  927. // sender?.isEnabled = false
  928. // }
  929. self.pageEditThumbnailView.zoomIn()
  930. }
  931. // MARK: menu 菜单事件
  932. override func menuNeedsUpdate(_ menu: NSMenu) {
  933. // let row = self.thumbnailView.collectionView.selectionIndexes
  934. // let point = self.thumbnailView.collectionView.convert(NSEvent.mouseLocation, to: nil)
  935. let point = self.view.window?.currentEvent?.locationInView(self.thumbnailView.collectionView) ?? .zero
  936. let indexpaths = self.thumbnailView.collectionView.indexPathForItem(at: point)
  937. menu.removeAllItems()
  938. if indexpaths == nil || indexpaths!.isEmpty { // 空白处点击
  939. if KMThumbnailManager.manager.copyPages.isEmpty { // 不能粘贴
  940. // if self.canPaste() {
  941. return
  942. }
  943. let item_ = menu.addItem(withTitle: NSLocalizedString("Paste", comment: ""), action: #selector(pasteActonForEmpty), target: self)
  944. // item_?.keyEquivalent = KMKeyEquivalent.paste
  945. let repObj = self.thumbnailView.collectionView.km_indexPathForItem(at: point, nearest: true)
  946. item_?.representedObject = repObj
  947. return
  948. }
  949. super.menuNeedsUpdate(menu)
  950. }
  951. @objc func item_insertCustomPage(sender: NSMenuItem?) {
  952. if IAPProductsManager.default().isAvailableAllFunction() == false {
  953. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  954. return
  955. }
  956. guard let doc = self.pdfDocument else {
  957. NSSound.beep()
  958. return
  959. }
  960. let windowC = KMPDFEditInsertBlankPageWindow(document: doc)
  961. windowC.currentPage = self.getSelecteIndex() + 1
  962. windowC.insertLocation = 3
  963. windowC.callback = { [weak self] pdfDoc, _, pages, insertI in
  964. if pages != nil {
  965. // mm 单位的大小
  966. guard let _winC = self?.kmCurrentWindowC as? KMPDFEditInsertBlankPageWindow else {
  967. self?.km_endSheet()
  968. return
  969. }
  970. var pageSize = _winC.pageSize
  971. let direction = windowC.pageRotation == 0 ? 0 : 1
  972. if (direction == 0) { // 纵向
  973. if (pageSize.width > pageSize.height) { // 需要交换
  974. let tmp = pageSize.width
  975. pageSize.width = pageSize.height
  976. pageSize.height = tmp
  977. } else {
  978. // no things.
  979. }
  980. } else { // 横向
  981. if (pageSize.width > pageSize.height) {
  982. // no things.
  983. } else { // 需要交换
  984. let tmp = pageSize.width
  985. pageSize.width = pageSize.height
  986. pageSize.height = tmp
  987. }
  988. }
  989. /// 插入位置
  990. let document = CPDFDocument()
  991. document?.insertPage(pageSize, at: 0)
  992. if let page: CPDFPage = (document?.page(at: 0)) {
  993. self?.insertPages(pages: [page], at: IndexSet(integer: insertI))
  994. }
  995. let ips: Set<IndexPath> = [IndexPath(item: insertI, section: 0)]
  996. self?.thumbnailView.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
  997. }
  998. self?.km_endSheet()
  999. }
  1000. self.km_beginSheet(windowC: windowC)
  1001. }
  1002. func extractSelectPageItemAction() {
  1003. if IAPProductsManager.default().isAvailableAllFunction() == false {
  1004. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  1005. return
  1006. }
  1007. let indexs = self.thumbnailView.selectionIndexPaths
  1008. if indexs.isEmpty {
  1009. self._progressHUD(message: KMLocalizedString(Self.kNoPagesSelectedTipMessage))
  1010. return
  1011. }
  1012. self.extractPages(indexpaths: indexs, oneDocumentPerPage: false) { result, params in }
  1013. }
  1014. // MARK: toolBar 菜单事件
  1015. @objc func reverseMenuAction(sender: AnyObject?) {
  1016. let selectedIndexs = self.thumbnailView.selectionIndexPaths
  1017. if (selectedIndexs.count < 2 ) {
  1018. let alertView = CustomAlertView.alertView(message: NSLocalizedString("Please select at least two pages for editing first", comment: ""), fromView: self.thumbnailView, withStyle: .blue)
  1019. alertView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  1020. alertView?.layer?.cornerRadius = 4
  1021. return
  1022. }
  1023. self.reversePages(indexs: self.indexpathsToIndexs(indexpaths: selectedIndexs))
  1024. self.dealPdfDocumentDidEditCallback(type: .reverse)
  1025. }
  1026. override func addMenuItem(menu: NSMenu, item: KMPDFThumbViewMenu.Item) -> NSMenuItem? {
  1027. var item_: NSMenuItem?
  1028. if (item.sel == KMPDFThumbViewMenu.insertSelector) {
  1029. item_ = menu.addItem(withTitle: NSLocalizedString("Blank Page", comment: ""), action: #selector(insertPageItemAction), target: self)
  1030. } else if (item.sel == KMPDFThumbViewMenu.splitSelector) {
  1031. if (self.thumbnailView.selectionIndexPaths.count > 1) {
  1032. item_ = menu.addItem(withTitle: NSLocalizedString("Split", comment: ""), action: KMPDFThumbViewMenu.splitSelector, target: self)
  1033. }
  1034. } else if (item.sel == KMPDFThumbViewMenu.reverseSelector) {
  1035. if (self.thumbnailView.selectionIndexPaths.count > 1) {
  1036. item_ = menu.addItem(withTitle: NSLocalizedString("Reverse", comment: ""), action: KMPDFThumbViewMenu.reverseSelector, target: self)
  1037. }
  1038. } else {
  1039. return super.addMenuItem(menu: menu, item: item)
  1040. }
  1041. return item_
  1042. }
  1043. // MARK: - Zoom Methods
  1044. @IBAction func buttonClicked_Bigger(_ sender: AnyObject?) {
  1045. // if (self.thumbnailView.thumbnailSize.width == 100.) {
  1046. // self.thumbnailView.thumbnailSize = CGSizeMake(200, 200);
  1047. // } else if (self.thumbnailView.thumbnailSize.width == 200.) {
  1048. // self.thumbnailView.thumbnailSize = CGSizeMake(400, 400);
  1049. // }
  1050. self.zoomOutButtonAction(sender: sender as? NSButton)
  1051. }
  1052. @IBAction func buttonClicked_Smaller(_ sender: AnyObject?) {
  1053. // if (self.thumbnailView.thumbnailSize.width == 400.) {
  1054. // self.thumbnailView.thumbnailSize = CGSizeMake(200, 200);
  1055. // } else if (self.thumbnailView.thumbnailSize.width == 200.) {
  1056. // self.thumbnailView.thumbnailSize = CGSizeMake(100, 100);
  1057. // }
  1058. self.zoomInButtonAction(sender: sender as? NSButton)
  1059. }
  1060. override func mouseMoved(with event: NSEvent) {
  1061. super.mouseMoved(with: event)
  1062. self.view.window?.mouseMoved(with: event)
  1063. }
  1064. }
  1065. extension KMPDFEditViewController {
  1066. //MARK: menu Action
  1067. @objc func insertPageItemAction(menu:NSMenuItem) {
  1068. if (menu.tag == 0) {
  1069. self.insertBlankPageAction()
  1070. } else if (menu.tag == 1) {
  1071. self.item_insertCustomPage(sender: nil)
  1072. } else if (menu.tag == 2) {
  1073. self.insertFileAction()
  1074. }
  1075. }
  1076. @objc func sharePageItemAction(menu:NSMenuItem) {
  1077. guard let item = menu.parent else {
  1078. NSSound.beep()
  1079. return
  1080. }
  1081. let index = (item.representedObject as? IndexSet)?.first ?? -1
  1082. if Int(index) >= 0 {
  1083. let doucument = self.pdfDocument
  1084. // let page = doucument.page(at: UInt(index))
  1085. let filename : String = doucument?.documentURL.lastPathComponent ?? ""
  1086. let folderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(filename)) ?? ""
  1087. try? FileManager.default.removeItem(atPath: folderPath)
  1088. let pdfdocument = CPDFDocument()
  1089. let ret = pdfdocument?.importPages((item.representedObject as? IndexSet) ?? [], from: self.thumbnailView.document, at: 0) ?? false
  1090. let url = URL(fileURLWithPath: folderPath)
  1091. if ret {
  1092. let success = pdfdocument?.write(to:url) ?? false
  1093. // if success {
  1094. // let workspace = NSWorkspace.shared
  1095. // workspace.activateFileViewerSelecting([url])
  1096. // }
  1097. }
  1098. let represent = menu.representedObject as? NSSharingService
  1099. represent?.perform(withItems: [url])
  1100. }
  1101. }
  1102. @objc func showPageSizeItemAction(menu: NSMenuItem) {
  1103. self.thumbnailView.isShowPageSize = !self.thumbnailView.isShowPageSize
  1104. self.refreshUIForKeepSelecteds()
  1105. }
  1106. @objc func printItemAction(menu:NSMenuItem) {
  1107. var datas: [Int] = []
  1108. for indexpath in self.thumbnailView.selectionIndexPaths {
  1109. datas.append(indexpath.item)
  1110. }
  1111. guard let callback = self.itemClick else {
  1112. return
  1113. }
  1114. callback(2, datas.sorted())
  1115. }
  1116. override func menuItemAction_extract(_ sender: Any?) {
  1117. self.extractSelectPageItemAction()
  1118. }
  1119. }
  1120. extension KMPDFEditViewController {
  1121. override func menuItemAction_print(_ sender: Any?) {
  1122. if let item = sender as? NSMenuItem {
  1123. self.printItemAction(menu: item)
  1124. }
  1125. }
  1126. override func menuItemAction_showPageSize(_ sender: Any?) {
  1127. if let item = sender as? NSMenuItem {
  1128. self.showPageSizeItemAction(menu: item)
  1129. }
  1130. }
  1131. override func menuItemAction_share(_ sender: Any?) {
  1132. if let item = sender as? NSMenuItem {
  1133. self.sharePageItemAction(menu: item)
  1134. }
  1135. }
  1136. }
  1137. // MARK: - KMSelectPopButtonDelegate
  1138. extension KMPDFEditViewController {
  1139. func km_comboBoxSelectionDidChange(_ obj: KMPageRange) {
  1140. self.pageIsUpdate = false
  1141. var indexpaths: Set<IndexPath> = []
  1142. let type: KMPageRange? = obj
  1143. let pageCnt = self.pdfDocument?.pageCount ?? 0
  1144. for i in 0 ..< pageCnt {
  1145. if (type == .all) {
  1146. indexpaths.insert(IndexPath(item: Int(i), section: 0))
  1147. } else if (type == .odd) {
  1148. if (i % 2 == 1) {
  1149. continue
  1150. }
  1151. indexpaths.insert(IndexPath(item: Int(i), section: 0))
  1152. } else if (type == .even) {
  1153. if (i % 2 == 0) {
  1154. continue
  1155. }
  1156. indexpaths.insert(IndexPath(item: Int(i), section: 0))
  1157. } else if (type == .horizontal) {
  1158. let page = self.pdfDocument?.page(at: i)
  1159. guard let isVertical = page?.isVertical(), !isVertical else {
  1160. continue
  1161. }
  1162. indexpaths.insert(IndexPath(item: Int(i), section: 0))
  1163. } else if (type == .vertical) {
  1164. let page = self.pdfDocument?.page(at: i)
  1165. guard let isHorizontal = page?.isHorizontal(), !isHorizontal else {
  1166. continue
  1167. }
  1168. indexpaths.insert(IndexPath(item: Int(i), section: 0))
  1169. }
  1170. }
  1171. if (type == .custom) {
  1172. let windowC = KMPDFEditPageRangeWindowController()
  1173. windowC.pdfDocument = self.pdfDocument
  1174. windowC.pageSelectCallBack = { [weak self] pagesArray in
  1175. var indexpaths = Set<IndexPath>()
  1176. for page in pagesArray {
  1177. indexpaths.insert(IndexPath(item: page-1, section: 0))
  1178. }
  1179. self?.km_endSheet()
  1180. self?.selectPages(indexpaths: indexpaths)
  1181. self?.thumbnailView.collectionView.scrollToItems(at: indexpaths, scrollPosition: .bottom)
  1182. }
  1183. self.km_beginSheet(windowC: windowC)
  1184. } else {
  1185. self.pageEditThumbnailView.selectPages(at: indexpaths)
  1186. }
  1187. }
  1188. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  1189. self.view.window?.makeFirstResponder(self)
  1190. for index in 0 ..< obj.numberOfItems {
  1191. if (obj.items[index] == obj.stringValue) {
  1192. return
  1193. }
  1194. }
  1195. let pages = KMPageRangeTools.findSelectPage(pageRangeString: self.toolBar.pageRangeView?.stringValue ?? "", pageCount: Int((self.listView?.document.pageCount) ?? 0))
  1196. if (pages.isEmpty) {
  1197. let alert = NSAlert()
  1198. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  1199. alert.runModal()
  1200. self.toolBar.setPageRangeString("")
  1201. DispatchQueue.main.async {
  1202. self.view.window?.makeFirstResponder(self.toolBar.pageRangeView?.textField)
  1203. }
  1204. return
  1205. }
  1206. var indexpaths: Set<IndexPath> = []
  1207. for page in pages {
  1208. if (page >= 1) {
  1209. indexpaths.insert(IndexPath(item: page-1, section: 0))
  1210. }
  1211. }
  1212. self.pageIsUpdate = false
  1213. self.pageEditThumbnailView.selectPages(at: indexpaths)
  1214. }
  1215. }
  1216. // MARK: -
  1217. // MARK: KMThumbnailViewDelegate
  1218. extension KMPDFEditViewController: KMThumbnailViewDelegate {
  1219. func thumbnailView(thumbanView: KMThumbnailView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
  1220. return IAPProductsManager.default().isAvailableAllFunction()
  1221. }
  1222. func thumbnailView(thumbanView: KMThumbnailView, shouldAcceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  1223. return true
  1224. }
  1225. func thumbnailView(thumbanView: KMThumbnailView, didDragAddFiles files: [URL], indexpath: IndexPath) {
  1226. self.km_add_file_multi(fileUrls: files) { index, params in
  1227. if (self.fetchProgressBlockParamsIsPasswordFile(params: params)) { // 加密文档进度回调
  1228. return
  1229. }
  1230. let _fileUrl = self.fetchProgressBlockParamsForFileUrl(params: params)
  1231. if let exn = _fileUrl?.pathExtension, KMTools.isPDFType(exn) {
  1232. if (_fileUrl!.path.isPDFValid() == false) {
  1233. let alert = NSAlert()
  1234. alert.alertStyle = .critical
  1235. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  1236. alert.runModal()
  1237. }
  1238. }
  1239. } completionBlock: { [weak self] documents in
  1240. var insertIndex: Int = indexpath.item
  1241. var pages: Array<CPDFPage> = []
  1242. var indexs = IndexSet()
  1243. self?.model.insertedDocumentSet.formUnion(documents)
  1244. for document in documents {
  1245. for i in 0 ..< document.pageCount {
  1246. if let page = document.page(at: i) {
  1247. pages.append(page)
  1248. indexs.insert(insertIndex)
  1249. insertIndex += 1
  1250. }
  1251. }
  1252. }
  1253. self?.insertPages(pages: pages, at: indexs)
  1254. }
  1255. }
  1256. func thumbnailView(thumbanView: KMThumbnailView, didDrag dragedIndexPaths: [IndexPath], indexpath: IndexPath, dragInfo: [KMThumbnailViewDragInfoKey.RawValue : Any]) {
  1257. let toIndex = max(0, indexpath.item)
  1258. var indexs = IndexSet()
  1259. for indexpath in dragedIndexPaths {
  1260. indexs.insert(indexpath.item)
  1261. }
  1262. var dp = NSCollectionView.DropOperation.on
  1263. if let _dp = dragInfo[KMThumbnailViewDragInfoKey.dropOperation.rawValue] as? NSCollectionView.DropOperation {
  1264. dp = _dp
  1265. }
  1266. self.dragPages(indexs, toIndex, dp: dp)
  1267. if (dragedIndexPaths.count > 0) {
  1268. self.dealPdfDocumentDidEditCallback()
  1269. }
  1270. }
  1271. func thumbnailView(thumbanView: KMThumbnailView, shouldPasteboardWriterForItemAt indexPath: IndexPath) -> Bool {
  1272. // if (!self.canPayFunction) {
  1273. // return false
  1274. // }
  1275. return true
  1276. }
  1277. func thumbnailView(thumbanView: KMThumbnailView, insetForSectionAt section: Int) -> NSEdgeInsets {
  1278. return NSEdgeInsets(top: 6, left: 40, bottom: 0, right: 40)
  1279. }
  1280. func thumbnailView(thumbanView: KMThumbnailView, sizeForItemAt indexpath: IndexPath) -> NSSize {
  1281. self.thumbnailView.thumbnailSzie = CGSize(width: 120, height: 155)
  1282. if (!self.thumbnailView.isShowPageSize) {
  1283. return NSMakeSize(self.itemSize.width, self.itemSize.height+26)
  1284. }
  1285. return self.itemSize
  1286. }
  1287. func thumbnailView(thumbanView: KMThumbnailView, numberOfItemsInSection section: Int) -> Int {
  1288. if let count = self.pdfDocument?.pageCount {
  1289. return Int(count)
  1290. }
  1291. return 0
  1292. }
  1293. func thumbnailView(thumbanView: KMThumbnailView, itemForRepresentedObjectAt indexpath: IndexPath) -> NSCollectionViewItem {
  1294. let cellView: KMPageEditThumbnailItem = thumbanView.collectionView.km.dequeueReusableCell(for: indexpath)
  1295. cellView.isShowPageSize = self.thumbnailView.isShowPageSize
  1296. if let data = self.pdfDocument?.page(at: UInt(indexpath.item)) {
  1297. cellView.setPage(page: data)
  1298. }
  1299. cellView.doubleClickAction = { [weak self] _ in
  1300. guard let callback = self?.itemClick else {
  1301. return
  1302. }
  1303. callback(1, indexpath.item)
  1304. }
  1305. cellView.mouseDownAction = { [unowned self] _ in
  1306. let selectionIndexPaths = self.thumbnailView.selectionIndexPaths
  1307. if (selectionIndexPaths.count == 1 && selectionIndexPaths.first?.item == indexpath.item) {
  1308. return
  1309. }
  1310. self.selectPages(indexs: IndexSet.init(integer: indexpath.item))
  1311. }
  1312. cellView.rightMouseDownAction = { [weak self] _ in
  1313. self?.selectPages(indexs: IndexSet.init(integer: indexpath.item))
  1314. }
  1315. return cellView
  1316. }
  1317. }
  1318. // MARK: - 系统菜单栏
  1319. extension KMPDFEditViewController {
  1320. override func undo(_ sender: Any?) {
  1321. super.undo(sender)
  1322. self.km_becomeFirstResponder()
  1323. }
  1324. override func redo(_ sender: Any?) {
  1325. super.redo(sender)
  1326. self.km_becomeFirstResponder()
  1327. }
  1328. }
  1329. // MARK: - KMExtensions
  1330. extension KMPDFEditViewController {
  1331. override func deleteAfter(indexpaths: Set<IndexPath>) {
  1332. self.clearSelectPages()
  1333. self.km_becomeFirstResponder()
  1334. }
  1335. override func copyAfter(indexpaths: Set<IndexPath>) {
  1336. self.km_becomeFirstResponder()
  1337. }
  1338. override func pasteAfter(at index: Int) {
  1339. self.km_becomeFirstResponder()
  1340. }
  1341. override func cutAfter(indexpaths: Set<IndexPath>) {
  1342. self.clearSelectPages()
  1343. self.km_becomeFirstResponder()
  1344. }
  1345. override func leftRotateAfter(indexpaths: Set<IndexPath>) {
  1346. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMPDFViewRotatePage"), object: self.thumbnailView.document)
  1347. self.km_becomeFirstResponder()
  1348. }
  1349. override func rightRotateAfter(indexpaths: Set<IndexPath>) {
  1350. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMPDFViewRotatePage"), object: self.thumbnailView.document)
  1351. self.km_becomeFirstResponder()
  1352. }
  1353. override func replaceAfter(of targetIndexs: IndexSet, with pages: [CPDFPage], at index: Int) {
  1354. var indexpaths = Set<IndexPath>()
  1355. var insertIndex = index
  1356. for _ in pages {
  1357. indexpaths.insert(IndexPath(item: insertIndex, section: 0))
  1358. insertIndex += 1
  1359. }
  1360. self.thumbnailView.selectionIndexPaths = indexpaths
  1361. self.dealPdfDocumentDidEditCallback(type: .replace)
  1362. }
  1363. override func insertBlankPageAfter(size: NSSize, at index: Int) {
  1364. self.selectPages(indexs: IndexSet(integer: index))
  1365. let ips: Set<IndexPath> = [IndexPath(item: index, section: 0)]
  1366. self.thumbnailView.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
  1367. self.dealPdfDocumentDidEditCallback(type: .insert)
  1368. }
  1369. func listViewCurrentPageUpdateForDrag(indexs: IndexSet, toIndex: Int) {
  1370. if (indexs.isEmpty) {
  1371. return
  1372. }
  1373. // listView 当前页面 被拖拽
  1374. if (indexs.contains(self._listViewCurrentIndex)) {
  1375. // let idx = indexs.firstIndex(of: self._listViewCurrentIndex)
  1376. var idx: Int = 0
  1377. for index in indexs {
  1378. if (index == self._listViewCurrentIndex) {
  1379. break
  1380. }
  1381. idx += 1
  1382. }
  1383. // 插入位置偏移量
  1384. var toIndexOffset: Int = 0
  1385. // 从后面往前删除
  1386. for index in indexs.reversed() {
  1387. let newPage = (self.listView?.document.page(at: UInt(index)))?.copy() as? CPDFPage
  1388. if (newPage == nil) {
  1389. continue
  1390. }
  1391. if (index < toIndex) { /// 删除了插入位置前面的页面,需要改变 toIndex
  1392. toIndexOffset += 1
  1393. }
  1394. }
  1395. if (indexs.last! < toIndex) { // 往后拖拽
  1396. toIndexOffset -= 1
  1397. }
  1398. let insertIndex = max(0, toIndex-toIndexOffset)
  1399. self._listViewCurrentIndex = insertIndex+idx
  1400. } else {
  1401. var forwardCnt = 0
  1402. var nextCnt = 0
  1403. for index in indexs {
  1404. if (index > self._listViewCurrentIndex) {
  1405. nextCnt += 1
  1406. } else {
  1407. forwardCnt += 1
  1408. }
  1409. }
  1410. if (self._listViewCurrentIndex > toIndex) { // 插入到目标前面, 前面不管,只管后面的格式
  1411. self._listViewCurrentIndex += nextCnt
  1412. } else if (self._listViewCurrentIndex < toIndex) { // 插入到目标后面, 后面不管,只管前面的格式
  1413. self._listViewCurrentIndex = max(0, self._listViewCurrentIndex - forwardCnt)
  1414. } else { // 插入到目标位置 [跟插入到目标后面一样]
  1415. self._listViewCurrentIndex = max(0, self._listViewCurrentIndex - forwardCnt)
  1416. }
  1417. }
  1418. }
  1419. }
  1420. // MARK: - Analytics (埋点)
  1421. extension KMPDFEditViewController {
  1422. func trackEvent(type: KMPageEditType) -> Void {
  1423. if (type == .insert) {
  1424. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Insert", parameters: [
  1425. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1426. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1427. } else if (type == .extract) {
  1428. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Extract", parameters: [
  1429. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1430. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1431. } else if (type == .leftRotate || type == .rightRotate) {
  1432. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Rotate", parameters: [
  1433. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1434. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1435. } else if (type == .replace) {
  1436. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Replace", 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 == .split) {
  1440. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Split", 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 == .reverse) {
  1444. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Reverse", 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 == .delete) {
  1448. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Delete", parameters: [
  1449. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1450. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1451. }
  1452. }
  1453. func trackEvent_copy() -> Void {
  1454. KMAnalytics.trackEvent(eventName: "Btn_SubTbr_Copy", parameters: [
  1455. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.subTbr_PageEdit,
  1456. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.subTbr_Btn], platform: .AppCenter, appTarget: .all)
  1457. }
  1458. }