KMNThumbnailBaseViewController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. //
  2. // KMNThumbnailBaseViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/21.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMNThumbnailBaseViewDelegate: AnyObject {
  10. @objc optional func clickThumbnailViewControlle(pageEditVC:KMNThumbnailBaseViewController?,currentIndex:Int)
  11. @objc optional func insertPDFThumbnailViewControlle(pageEditVC:KMNThumbnailBaseViewController?,pdfDocment:CPDFDocument?)
  12. @objc optional func changeIndexPathsThumbnailViewControlle(pageEditVC:KMNThumbnailBaseViewController?,selectionIndexPaths: Set<IndexPath>,selectionStrings:String )
  13. }
  14. enum KMNThumbnailChoosePageStyle: Int {
  15. case odd
  16. case even
  17. case horizontal
  18. case vertical
  19. case allPage
  20. case custom
  21. }
  22. internal let kmnThumLocalForDraggedTypes = NSPasteboard.PasteboardType(rawValue: "kmnThumLocalForDraggedTypes")
  23. let topThumOffset: CGFloat = 12.0 // 缩图顶部高度
  24. let infoThumTitleHeight: CGFloat = 16.0 //文字高度
  25. let infoThumTitleBottom: CGFloat = 16.0 // 底部高度
  26. let ThumbnailMenuIdentifier_Copy = "ThumbnailMenuIdentifier_Copy"
  27. let ThumbnailMenuIdentifier_Cut = "ThumbnailMenuIdentifier_Cut"
  28. let ThumbnailMenuIdentifier_Paste = "ThumbnailMenuIdentifier_Paste"
  29. let ThumbnailMenuIdentifier_Delete = "ThumbnailMenuIdentifier_Delete"
  30. let ThumbnailMenuIdentifier_RotateRight = "ThumbnailMenuIdentifier_RotateRight"
  31. let ThumbnailMenuIdentifier_RotateLeft = "ThumbnailMenuIdentifier_RotateLeft"
  32. let ThumbnailMenuIdentifier_InsertFile = "ThumbnailMenuIdentifier_InsertFile"
  33. let ThumbnailMenuIdentifier_InsertBlank = "ThumbnailMenuIdentifier_InsertBlank"
  34. let ThumbnailMenuIdentifier_Replace = "ThumbnailMenuIdentifier_Replace"
  35. let ThumbnailMenuIdentifier_Share = "ThumbnailMenuIdentifier_Share"
  36. let ThumbnailMenuIdentifier_Export = "ThumbnailMenuIdentifier_Export"
  37. let ThumbnailMenuIdentifier_PastNull = "ThumbnailMenuIdentifier_PastNull"
  38. let ThumbnailMenuIdentifier_FileShowSize = "ThumbnailMenuIdentifier_FileShowSize"
  39. struct ThumbnailMenuStruct {
  40. var menuitems: [ComponentMenuitemProperty]
  41. var viewHeight: CGFloat // 不可变属性
  42. }
  43. class KMNThumbnailBaseViewController: KMNBaseViewController,NSCollectionViewDelegate, NSCollectionViewDataSource,NSCollectionViewDelegateFlowLayout {
  44. let maxCellHeight: CGFloat = 320.0 - infoThumTitleBottom - infoThumTitleHeight - topThumOffset * 2
  45. let minCellHeight: CGFloat = 80.0 - infoThumTitleBottom - infoThumTitleHeight - topThumOffset - topThumOffset * 2
  46. weak open var thumbnailBaseViewDelegate: KMNThumbnailBaseViewDelegate?
  47. @IBOutlet var backViewBox: NSBox!
  48. @IBOutlet var scrollView: NSScrollView!
  49. @IBOutlet var collectionView: KMNThumbnailCollectionView!
  50. @IBOutlet var flowLayout: KMNThumCustomCollectionViewFlowLayout!
  51. private var currentDocument:CPDFDocument?
  52. private var isChangeIndexPaths = false
  53. var lockedFiles: [URL] = []
  54. var filePromiseQueue: OperationQueue = {
  55. let queue = OperationQueue()
  56. return queue
  57. }()
  58. fileprivate var dragFilePath: String?
  59. fileprivate var dragFlag = false
  60. public var thumbnails:[KMNThumbnail] = []
  61. public var dragLocalityPages: [CPDFPage] = []
  62. public var currentUndoManager:UndoManager?
  63. public var showDocument: CPDFDocument? {
  64. return currentDocument
  65. }
  66. public var changeDocument:CPDFDocument? {
  67. didSet {
  68. if(changeDocument != nil && changeDocument != currentDocument) {
  69. currentDocument = changeDocument
  70. refreshDatas()
  71. collectionView.reloadData()
  72. }
  73. }
  74. }
  75. public var thumbnailChoosePageStyle:KMNThumbnailChoosePageStyle = .custom {
  76. didSet {
  77. var tSelectionIndexPaths: Set<IndexPath> = []
  78. let pageCount = currentDocument?.pageCount ?? 0
  79. switch thumbnailChoosePageStyle {
  80. case .even:
  81. for i in 0 ..< pageCount {
  82. if(i % 2 == 0) {
  83. tSelectionIndexPaths.insert(IndexPath(item: Int(i), section: 0))
  84. }
  85. }
  86. case .odd:
  87. for i in 0 ..< pageCount {
  88. if(i % 2 != 0) {
  89. tSelectionIndexPaths.insert(IndexPath(item: Int(i), section: 0))
  90. }
  91. }
  92. case .allPage:
  93. for i in 0 ..< pageCount {
  94. tSelectionIndexPaths.insert(IndexPath(item: Int(i), section: 0))
  95. }
  96. case .vertical:
  97. for i in 0 ..< pageCount {
  98. let page = showDocument?.page(at: i)
  99. if(page != nil) {
  100. if(page!.rotation % 180 != 0) {
  101. tSelectionIndexPaths.insert(IndexPath(item: Int(i), section: 0))
  102. }
  103. }
  104. }
  105. case .horizontal:
  106. for i in 0 ..< pageCount {
  107. let page = showDocument?.page(at: i)
  108. if(page != nil) {
  109. if(page!.rotation % 180 == 0) {
  110. tSelectionIndexPaths.insert(IndexPath(item: Int(i), section: 0))
  111. }
  112. }
  113. }
  114. default: break
  115. }
  116. isChangeIndexPaths = true
  117. collectionView.selectionIndexPaths = tSelectionIndexPaths
  118. isChangeIndexPaths = false
  119. }
  120. }
  121. var selectionIndexPaths: Set<IndexPath> = [] {
  122. didSet {
  123. var indexpaths: Set<IndexPath> = []
  124. for indexpath in selectionIndexPaths {
  125. if (indexpath.section >= collectionView.numberOfSections) {
  126. continue
  127. }
  128. if indexpath.section < 0 {
  129. continue
  130. }
  131. if (indexpath.item >= collectionView.numberOfItems(inSection: indexpath.section)) {
  132. continue
  133. }
  134. if indexpath.item < 0 {
  135. continue
  136. }
  137. indexpaths.insert(indexpath)
  138. }
  139. if indexpaths.count > 0 {
  140. self.collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
  141. }
  142. self.collectionView.selectionIndexPaths = indexpaths
  143. }
  144. }
  145. public var isShowPageSize:Bool = false {
  146. didSet {
  147. if oldValue != isShowPageSize {
  148. var pageSize = pageThumbnailSize
  149. if(isShowPageSize) {
  150. pageSize.height += infoThumTitleHeight
  151. } else {
  152. pageSize.height -= infoThumTitleHeight
  153. }
  154. pageThumbnailSize = pageSize
  155. collectionView.reloadData()
  156. }
  157. }
  158. }
  159. public var pageThumbnailSize:CGSize = CGSizeMake(185.0, 260) {
  160. didSet {
  161. collectionView.reloadData()
  162. }
  163. }
  164. public let defaultItemSize = NSMakeSize(185.0, 260)
  165. deinit {
  166. thumbnailBaseViewDelegate = nil
  167. KMPrint("KMNThumbnailBaseViewController deinit.")
  168. }
  169. init(_ document: CPDFDocument?) {
  170. super.init(nibName: "KMNThumbnailBaseViewController", bundle: nil)
  171. currentDocument = document
  172. }
  173. init(_ filePath: String,password:String?) {
  174. super.init(nibName: "KMNThumbnailBaseViewController", bundle: nil)
  175. let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
  176. if password != nil {
  177. document?.unlock(withPassword: password as String?)
  178. }
  179. if document?.allowsCopying == false || document?.allowsPrinting == false {
  180. exitCurrentView()
  181. } else {
  182. currentDocument = document
  183. }
  184. }
  185. required init?(coder: NSCoder) {
  186. fatalError("init(coder:) has not been implemented")
  187. }
  188. override func viewDidLoad() {
  189. super.viewDidLoad()
  190. collectionView.delegate = self
  191. collectionView.dataSource = self
  192. collectionView.isSelectable = true //支持拖拽需设置未True
  193. collectionView.allowsMultipleSelection = true
  194. collectionView.enclosingScrollView?.hasVerticalScroller = false
  195. collectionView.enclosingScrollView?.hasHorizontalScroller = false
  196. scrollView.scrollerStyle = .overlay
  197. collectionView.register(KMNThumbnailCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "thumbnailCollectionViewItem"))
  198. collectionView.registerForDraggedTypes([.fileURL,.string,.pdf])
  199. collectionView.setDraggingSourceOperationMask(.every, forLocal: false)
  200. collectionView.setDraggingSourceOperationMask(.every, forLocal: true)
  201. collectionView.collectionSelectChanges = {[weak self] in
  202. if self?.isChangeIndexPaths == false {
  203. let indexpathsz = self?.collectionView.selectionIndexPaths
  204. let dex:IndexSet = KMNTools.indexpathsToIndexs(indexpaths: indexpathsz ?? [])
  205. let selectedIndexPathsString = KMNTools.parseIndexSet(indexSet: dex)
  206. self?.thumbnailBaseViewDelegate?.changeIndexPathsThumbnailViewControlle?(pageEditVC: self, selectionIndexPaths: indexpathsz ?? [], selectionStrings: selectedIndexPathsString)
  207. }
  208. }
  209. refreshDatas()
  210. }
  211. public func exitCurrentView() {
  212. let minIndexPath = selectionIndexPaths.max(by: { $0.item < $1.item })
  213. thumbnailBaseViewDelegate?.clickThumbnailViewControlle?(pageEditVC: self, currentIndex: minIndexPath?.item ?? 0)
  214. }
  215. public func supportDragFileTypes()->[String] {
  216. let supportFiles = KMNConvertTool.pdfExtensions + KMConvertPDFManager.supportFileType()
  217. return supportFiles
  218. }
  219. public func refreshDatas() {
  220. thumbnails = []
  221. if currentDocument != nil {
  222. for i in 0 ..< currentDocument!.pageCount {
  223. let thumbnail = KMNThumbnail.init(document: currentDocument!, currentPageIndex: Int(i))
  224. thumbnails.append(thumbnail)
  225. }
  226. }
  227. }
  228. public func reloadDatas() {
  229. refreshDatas()
  230. collectionView.reloadData()
  231. }
  232. public func reloadDataWithIndexs(pageIndexs: IndexSet) {
  233. if currentDocument != nil {
  234. for i in pageIndexs {
  235. if i < currentDocument?.pageCount ?? 0 {
  236. let thumbnail = KMNThumbnail.init(document: currentDocument!, currentPageIndex: i)
  237. thumbnail.removeCacheImage()
  238. }
  239. }
  240. reloadDatas()
  241. }
  242. }
  243. // MARK: - private
  244. public func clickMenu(point:NSPoint)->ThumbnailMenuStruct {
  245. let copyPages: [CPDFPage] = KMNThumbnailManager.manager.copyPages
  246. let selectedIndexPaths = collectionView.selectionIndexPaths
  247. var viewHeight: CGFloat = 8
  248. var menuItemArr: [ComponentMenuitemProperty] = []
  249. var items: [(String, String)] = []
  250. if selectedIndexPaths.count > 0 {
  251. items.append(("Copy", ThumbnailMenuIdentifier_Copy))
  252. items.append(("Cut", ThumbnailMenuIdentifier_Cut))
  253. if(copyPages.count > 0) {
  254. items.append(("Paste", ThumbnailMenuIdentifier_Paste))
  255. }
  256. items.append(("Delete", ThumbnailMenuIdentifier_Delete))
  257. items.append(("", ""))
  258. items.append(("90° CW", ThumbnailMenuIdentifier_RotateRight))
  259. items.append(("90° CCW", ThumbnailMenuIdentifier_RotateLeft))
  260. items.append(("", ""))
  261. if(selectedIndexPaths.count == 1) {
  262. items.append(("Insert File", ThumbnailMenuIdentifier_InsertFile))
  263. items.append(("Insert a Blank Page", ThumbnailMenuIdentifier_InsertBlank))
  264. items.append(("Replace", ThumbnailMenuIdentifier_Replace))
  265. }
  266. items.append(("Extract", ThumbnailMenuIdentifier_Export))
  267. items.append(("Share", ThumbnailMenuIdentifier_Share))
  268. items.append(("", ""))
  269. items.append(("Display Page Size", ThumbnailMenuIdentifier_FileShowSize))
  270. } else {
  271. if(copyPages.count > 0) {
  272. items.append(("Paste", ThumbnailMenuIdentifier_Paste))
  273. items.append(("", ""))
  274. } else {
  275. items.append(("Paste", ThumbnailMenuIdentifier_PastNull))
  276. items.append(("", ""))
  277. }
  278. items.append(("Display Page Size", ThumbnailMenuIdentifier_FileShowSize))
  279. }
  280. for (i, value) in items {
  281. if value.count == 0 {
  282. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  283. menuItemArr.append(property)
  284. viewHeight += 8
  285. } else {
  286. var isDisabled = false
  287. if(value == ThumbnailMenuIdentifier_PastNull) {
  288. isDisabled = true
  289. }
  290. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  291. itemSelected: false,
  292. isDisabled: isDisabled,
  293. keyEquivalent: nil,
  294. text: KMLocalizedString(i),
  295. identifier: value,representedObject: point)
  296. if value == ThumbnailMenuIdentifier_Copy {
  297. properties_Menuitem.keyEquivalent = "⌘ C"
  298. } else if value == ThumbnailMenuIdentifier_Cut {
  299. properties_Menuitem.keyEquivalent = "⌘ x"
  300. } else if value == ThumbnailMenuIdentifier_Paste {
  301. properties_Menuitem.keyEquivalent = "⌘ v"
  302. } else if value == ThumbnailMenuIdentifier_PastNull {
  303. properties_Menuitem.keyEquivalent = "⌘ v"
  304. } else if value == ThumbnailMenuIdentifier_Delete {
  305. properties_Menuitem.keyEquivalent = "⌘ " + String(Unicode.Scalar(NSBackspaceCharacter)!)
  306. } else if value == ThumbnailMenuIdentifier_RotateRight {
  307. properties_Menuitem.keyEquivalent = "⌥ ⌘ R"
  308. } else if value == ThumbnailMenuIdentifier_RotateLeft {
  309. properties_Menuitem.keyEquivalent = "⌥ ⌘ L"
  310. }
  311. if(value == ThumbnailMenuIdentifier_FileShowSize && isShowPageSize) {
  312. properties_Menuitem.righticon = NSImage(named: "KMNImageNameMenuSelect")
  313. }
  314. menuItemArr.append(properties_Menuitem)
  315. viewHeight += 36
  316. }
  317. }
  318. let menuStruct = ThumbnailMenuStruct(menuitems: menuItemArr, viewHeight: viewHeight)
  319. return menuStruct
  320. }
  321. // MARK: - NSCollectionViewDataSource
  322. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  323. return thumbnails.count
  324. }
  325. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  326. let item: KMNThumbnailCollectionViewItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "thumbnailCollectionViewItem"), for: indexPath) as! KMNThumbnailCollectionViewItem
  327. item.isShowFileSize = isShowPageSize
  328. item.doubleClickBack = { [weak self] in
  329. self?.thumbnailBaseViewDelegate?.clickThumbnailViewControlle?(pageEditVC: self, currentIndex: indexPath.item)
  330. }
  331. item.thumbnailMode = thumbnails[indexPath.item]
  332. return item
  333. }
  334. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  335. if isChangeIndexPaths == false {
  336. let indexpathsz = collectionView.selectionIndexPaths
  337. let dex:IndexSet = KMNTools.indexpathsToIndexs(indexpaths: indexpathsz)
  338. let selectedIndexPathsString = KMNTools.parseIndexSet(indexSet: dex)
  339. thumbnailBaseViewDelegate?.changeIndexPathsThumbnailViewControlle?(pageEditVC: self, selectionIndexPaths: indexpathsz, selectionStrings: selectedIndexPathsString)
  340. }
  341. }
  342. func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  343. if isChangeIndexPaths == false {
  344. let indexpathsz = self.collectionView.selectionIndexPaths
  345. let dex:IndexSet = KMNTools.indexpathsToIndexs(indexpaths: indexpathsz)
  346. let selectedIndexPathsString = KMNTools.parseIndexSet(indexSet: dex)
  347. thumbnailBaseViewDelegate?.changeIndexPathsThumbnailViewControlle?(pageEditVC: self, selectionIndexPaths: indexpathsz, selectionStrings: selectedIndexPathsString)
  348. }
  349. }
  350. // MARK: - NSCollectionViewDelegateFlowLayout
  351. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  352. return pageThumbnailSize
  353. }
  354. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  355. return 16.0
  356. }
  357. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  358. return 24.0
  359. }
  360. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  361. return NSEdgeInsetsMake(24.0, 24.0, 24.0, 24.0)
  362. }
  363. //MARK: - NSCollectionViewDelegate
  364. func collectionView(_ collectionView: NSCollectionView,
  365. pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
  366. var provider: NSFilePromiseProvider?
  367. // 创建数据提供者
  368. let fileExtension = "pdf"
  369. if #available(macOS 11.0, *) {
  370. if let typeIdentifier = UTType(filenameExtension: fileExtension) {
  371. provider = KMFilePromiseProvider(fileType: typeIdentifier.identifier, delegate: self)
  372. }
  373. } else {
  374. if let typeIdentifier =
  375. UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension as CFString, nil) {
  376. provider = KMFilePromiseProvider(fileType: typeIdentifier.takeRetainedValue() as String, delegate: self)
  377. }
  378. }
  379. do {
  380. if let _url = showDocument?.documentURL {
  381. let data = try NSKeyedArchiver.archivedData(withRootObject: indexPath, requiringSecureCoding: false)
  382. provider?.userInfo = [KMFilePromiseProvider.UserInfoKeys.urlKey: _url,
  383. KMFilePromiseProvider.UserInfoKeys.indexPathKey: data]
  384. } else {
  385. let data = try NSKeyedArchiver.archivedData(withRootObject: indexPath, requiringSecureCoding: false)
  386. provider?.userInfo = [KMFilePromiseProvider.UserInfoKeys.indexPathKey: data]
  387. }
  388. } catch {
  389. fatalError("failed to archive indexPath to pasteboard")
  390. }
  391. return provider
  392. }
  393. func collectionView(_ collectionView: NSCollectionView,
  394. draggingSession session: NSDraggingSession,
  395. willBeginAt screenPoint: NSPoint,
  396. forItemsAt indexPaths: Set<IndexPath>) {
  397. let sortedIndexPaths = indexPaths.sorted { (ip1, ip2) -> Bool in
  398. if ip1.section == ip2.section {
  399. return ip1.item < ip2.item
  400. }
  401. return ip1.section < ip2.section
  402. }
  403. dragLocalityPages = []
  404. for fromIndexPath in sortedIndexPaths {
  405. let page = thumbnails[fromIndexPath.item].thumbnaiPage
  406. if(page != nil) {
  407. dragLocalityPages.append(page!)
  408. }
  409. }
  410. var docmentName = currentDocument?.documentURL.lastPathComponent.deletingPathExtension ?? ""
  411. let pagesName = indexPaths.count > 1 ? " pages" : " page"
  412. var tFileName = pagesName + KMNTools.parseIndexPathsSet(indexSets: collectionView.selectionIndexPaths)
  413. if tFileName.count > 50 {
  414. tFileName = String(tFileName.prefix(50))
  415. }
  416. var cachesDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
  417. cachesDir = cachesDir.appendingPathComponent("PageEdit_Pasteboard")
  418. let fileManager = FileManager.default
  419. if !fileManager.fileExists(atPath: cachesDir.path) {
  420. try? FileManager.default.createDirectory(atPath: cachesDir.path, withIntermediateDirectories: true, attributes: nil)
  421. }
  422. docmentName = "\(docmentName)\(tFileName)"
  423. if docmentName.count > 50 {
  424. docmentName = String(docmentName.prefix(50))
  425. }
  426. // 将拖拽的page插入临时路径(文档)
  427. var indexs = IndexSet()
  428. for indexpath in indexPaths {
  429. indexs.insert(indexpath.item)
  430. }
  431. // 重置拖拽标识
  432. self.dragFlag = false
  433. if (indexs.count > 0) {
  434. let writePDFDocument = CPDFDocument()
  435. writePDFDocument?.importPages(indexs, from: self.showDocument, at: 0)
  436. let filePathURL = cachesDir.appendingPathComponent(docmentName).appendingPathExtension("pdf")
  437. let success = writePDFDocument?.write(to: filePathURL, isSaveFontSubset:false)
  438. if(success == true) {
  439. self.dragFilePath = filePathURL.path
  440. }
  441. }
  442. }
  443. func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  444. let pboard = draggingInfo.draggingPasteboard
  445. if dragLocalityPages.count != 0 {
  446. if proposedDropOperation.pointee == .on {
  447. proposedDropOperation.pointee = .before
  448. }
  449. return .move
  450. } else if ((pboard.availableType(from: [.fileURL])) != nil) {
  451. guard let pbItems = pboard.pasteboardItems else {
  452. return NSDragOperation(rawValue: 0)
  453. }
  454. var hasValidFile = false
  455. for item in pbItems {
  456. guard let data = item.string(forType: .fileURL), let url = URL(string: data) else {
  457. continue
  458. }
  459. let type = url.pathExtension.lowercased()
  460. if (supportDragFileTypes().contains(type)) {
  461. hasValidFile = true
  462. break
  463. }
  464. }
  465. if (!hasValidFile) {
  466. return NSDragOperation(rawValue: 0)
  467. }
  468. }
  469. return .copy
  470. }
  471. func collectionView(_ collectionView: NSCollectionView,
  472. acceptDrop draggingInfo: NSDraggingInfo,
  473. indexPath: IndexPath,
  474. dropOperation: NSCollectionView.DropOperation) -> Bool {
  475. let pboard = draggingInfo.draggingPasteboard
  476. if dragLocalityPages.count != 0 {
  477. movePages(dragPages: dragLocalityPages, destinationDex: indexPath.item)
  478. return true
  479. } else if ((pboard.availableType(from: [.fileURL])) != nil) {
  480. let index = indexPath.item
  481. guard let pbItems = pboard.pasteboardItems else {
  482. return false
  483. }
  484. //获取url
  485. var fileNames: [String] = []
  486. for item in pbItems {
  487. guard let data = item.string(forType: .fileURL), let url = URL(string: data) else {
  488. continue
  489. }
  490. let type = url.pathExtension.lowercased()
  491. if (supportDragFileTypes().contains(type)) {
  492. if(FileManager.default.fileExists(atPath: url.path)) {
  493. fileNames.append(url.path)
  494. }
  495. }
  496. }
  497. if(fileNames.count > 0) {
  498. insertFromFilePath(fileNames: fileNames, formDex: 0, indexDex: UInt(index), selectIndexs: []) { newSelectIndexs in
  499. }
  500. return true
  501. }
  502. }
  503. return false
  504. }
  505. func collectionView(_ collectionView: NSCollectionView,
  506. draggingSession session: NSDraggingSession,
  507. endedAt screenPoint: NSPoint,
  508. dragOperation operation: NSDragOperation) {
  509. dragLocalityPages = []
  510. }
  511. }
  512. // MARK: - NSFilePromiseProviderDelegate
  513. extension KMNThumbnailBaseViewController: NSFilePromiseProviderDelegate {
  514. func operationQueue(for filePromiseProvider: NSFilePromiseProvider) -> OperationQueue {
  515. return self.filePromiseQueue
  516. }
  517. func filePromiseProvider(_ filePromiseProvider: NSFilePromiseProvider,
  518. writePromiseTo url: URL,
  519. completionHandler: @escaping (Error?) -> Void) {
  520. do {
  521. /** Copy the file to the location provided to you. You always do a copy, not a move.
  522. It's important you call the completion handler.
  523. */
  524. if let _urlString = dragFilePath, !dragFlag {
  525. dragFlag = true
  526. try FileManager.default.copyItem(at: URL(fileURLWithPath: _urlString), to: url)
  527. }
  528. completionHandler(nil)
  529. } catch let error {
  530. OperationQueue.main.addOperation {
  531. if let win = self.view.window {
  532. self.presentError(error, modalFor: win,
  533. delegate: nil, didPresent: nil, contextInfo: nil)
  534. }
  535. }
  536. completionHandler(error)
  537. }
  538. }
  539. func filePromiseProvider(_ filePromiseProvider: NSFilePromiseProvider, fileNameForType fileType: String) -> String {
  540. var fileName: String = "Untitle"
  541. if let _string = showDocument?.documentURL.deletingPathExtension().lastPathComponent {
  542. fileName = _string
  543. }
  544. fileName.append(" pages")
  545. var indexs = IndexSet()
  546. for page in dragLocalityPages {
  547. indexs.insert(Int(page.pageIndex()))
  548. }
  549. fileName.append(" ")
  550. fileName.append(KMPageRangeTools.newParseSelectedIndexs(selectedIndex: indexs.sorted()))
  551. fileName.append(".pdf")
  552. return fileName
  553. }
  554. }