KMNThumbnailBaseViewController.swift 27 KB

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