|
@@ -145,35 +145,6 @@ class KMThumbnailViewController: NSViewController {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-// // 插入位置偏移量
|
|
|
-// var toIndexOffset: Int = 0
|
|
|
-// // 需要移动的页面index数组
|
|
|
-// var newPages: [CPDFPage] = []
|
|
|
-// for index in indexs {
|
|
|
-// let newPage: CPDFPage = self.listView?.document.page(at: UInt(index)).copy() as! CPDFPage
|
|
|
-// newPages.append(newPage)
|
|
|
-// self.listView?.document.removePage(at: UInt(index))
|
|
|
-// if (index < toIndex) { /// 删除了插入位置前面的页面,需要改变 toIndex
|
|
|
-// toIndexOffset += 1
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// let insertIndex = max(0, toIndex-toIndexOffset)
|
|
|
-// for (index, page) in newPages.enumerated() {
|
|
|
-// self.listView?.document.insertPageObject(page, at: UInt(insertIndex+index))
|
|
|
-// }
|
|
|
-
|
|
|
- // 新增 undo
|
|
|
-// self.undoRedoManager.registerUndo(withTarget: self) { target in
|
|
|
-// target.dragPages_reversed(indexs, toIndex, newPages)
|
|
|
-// }
|
|
|
-
|
|
|
- // 刷新UI
|
|
|
-// self.listView?.layoutDocumentView()
|
|
|
-// self.thumbnailView.refreshUI()
|
|
|
-
|
|
|
-// self.thumbnailView.reloadData()
|
|
|
-
|
|
|
//获取原有page的下标 和 开始添加的下标
|
|
|
var indexTemp: Int = toIndex
|
|
|
var pageItems: [KMThumbnailPageItem] = []
|
|
@@ -517,10 +488,7 @@ class KMThumbnailViewController: NSViewController {
|
|
|
@IBAction func deletePageItemAction(menu:NSMenuItem) {
|
|
|
let indexs = self.thumbnailView.collectionView.selectionIndexes
|
|
|
if indexs.count > 0 {
|
|
|
- self.listView.document.removePage(at: indexs)
|
|
|
- self.listView.layoutDocumentView()
|
|
|
- self.thumbnailView.reloadData()
|
|
|
- self.pdfCurrentPageChange()
|
|
|
+ self.deletePageItemWithIndex(indexs: indexs)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -568,23 +536,21 @@ class KMThumbnailViewController: NSViewController {
|
|
|
let page = self.thumbnailView.document.page(at: UInt(i))
|
|
|
self.copysPages.append(page ?? CPDFPage())
|
|
|
}
|
|
|
- self.listView.document.removePage(at: indexs)
|
|
|
- self.listView.layoutDocumentView()
|
|
|
- self.thumbnailView.reloadData()
|
|
|
+
|
|
|
+ self.deletePageItemWithIndex(indexs: indexs)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@IBAction func pastePageItemAction(menu:NSMenuItem) {
|
|
|
var index = (self.thumbnailView.collectionView.selectionIndexes).last ?? -1
|
|
|
if Int(index) >= 0 && self.copysPages.count > 0 {
|
|
|
+ var pageItems: [KMThumbnailPageItem] = []
|
|
|
for page in self.copysPages {
|
|
|
- let result = self.thumbnailView.document.insertPageObject(page, at: UInt(index))
|
|
|
- if result {
|
|
|
- index = index + 1
|
|
|
- }
|
|
|
+ let pageItem = KMThumbnailPageItem(page: page, index: index, oldIndex: index)
|
|
|
+ index = index + 1
|
|
|
+ pageItems.append(pageItem)
|
|
|
}
|
|
|
- self.listView.layoutDocumentView()
|
|
|
- self.thumbnailView.reloadData()
|
|
|
+ self.addPageItems(pageItems: pageItems)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -668,6 +634,13 @@ extension KMThumbnailViewController : NSMenuDelegate,NSMenuItemValidation {
|
|
|
}
|
|
|
return false
|
|
|
}
|
|
|
+
|
|
|
+ if (action == #selector(undo)) {
|
|
|
+ return self.undoRedoManager.canUndo
|
|
|
+ }
|
|
|
+ if (action == #selector(redo)) {
|
|
|
+ return self.undoRedoManager.canRedo
|
|
|
+ }
|
|
|
return true
|
|
|
}
|
|
|
|
|
@@ -759,8 +732,56 @@ extension KMThumbnailViewController : NSMenuDelegate,NSMenuItemValidation {
|
|
|
|
|
|
//MARK: undoRedo
|
|
|
extension KMThumbnailViewController {
|
|
|
+ func rotatePageItemWithIndex(indexs: IndexSet) {
|
|
|
+ var pageItems: [KMThumbnailPageItem] = []
|
|
|
+ for index in indexs {
|
|
|
+ let page: CPDFPage = self.listView.document.page(at: UInt(index))
|
|
|
+ let pageItem = KMThumbnailPageItem(page: page, index: index, oldIndex: index)
|
|
|
+ pageItems.append(pageItem)
|
|
|
+ }
|
|
|
+ self.deletePageItems(pageItems: pageItems)
|
|
|
+ }
|
|
|
|
|
|
+ func deletePageItemWithIndex(indexs: IndexSet) {
|
|
|
+ var pageItems: [KMThumbnailPageItem] = []
|
|
|
+ for index in indexs {
|
|
|
+ let page: CPDFPage = self.listView.document.page(at: UInt(index))
|
|
|
+ let pageItem = KMThumbnailPageItem(page: page, index: index, oldIndex: index)
|
|
|
+ pageItems.append(pageItem)
|
|
|
+ }
|
|
|
+ self.deletePageItems(pageItems: pageItems)
|
|
|
+ }
|
|
|
|
|
|
+ func deletePageItems(pageItems: [KMThumbnailPageItem]) {
|
|
|
+ //移除现有
|
|
|
+ var tempPageItems = pageItems
|
|
|
+ tempPageItems.sort(){$0.oldIndex > $1.oldIndex}
|
|
|
+ for pageItem in tempPageItems {
|
|
|
+ self.listView?.document.removePage(at: UInt(pageItem.oldIndex))
|
|
|
+ }
|
|
|
+ self.listView.layoutDocumentView()
|
|
|
+ self.thumbnailView.reloadData()
|
|
|
+ self.pdfCurrentPageChange()
|
|
|
+
|
|
|
+ self.undoRedoManager.registerUndo(withTarget: self) { targetType in
|
|
|
+ self.addPageItems(pageItems: tempPageItems)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func addPageItems(pageItems: [KMThumbnailPageItem]) {
|
|
|
+ var tempPageItems = pageItems
|
|
|
+ tempPageItems.sort(){$0.oldIndex < $1.oldIndex}
|
|
|
+ for pageItem in tempPageItems {
|
|
|
+ self.listView?.document.insertPageObject(pageItem.page, at: UInt(pageItem.oldIndex))
|
|
|
+ }
|
|
|
+ self.listView.layoutDocumentView()
|
|
|
+ self.thumbnailView.reloadData()
|
|
|
+ self.pdfCurrentPageChange()
|
|
|
+
|
|
|
+ self.undoRedoManager.registerUndo(withTarget: self) { targetType in
|
|
|
+ self.deletePageItems(pageItems: tempPageItems)
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
func dragPageItems(pageItems: [KMThumbnailPageItem]) {
|
|
@@ -806,7 +827,9 @@ extension KMThumbnailViewController {
|
|
|
|
|
|
struct KMThumbnailPageItem {
|
|
|
var page: CPDFPage
|
|
|
- var index: Int
|
|
|
- var oldIndex: Int
|
|
|
+ var index: Int = 0
|
|
|
+ var oldIndex: Int = 0
|
|
|
+ var rotate: Int = 0
|
|
|
+ var oldRotate: Int = 0
|
|
|
}
|
|
|
}
|