123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- //
- // KMNPageEditViewController.swift
- // PDF Reader Pro
- //
- // Created by 丁林圭 on 2024/10/21.
- //
- import Cocoa
- import KMComponentLibrary
- @objc protocol KMNPageEditViewViewDelegate: AnyObject {
- @objc optional func changeBoundEditViewController(pageEditVC:KMNPageEditViewController)
- @objc optional func changeUndoManagerEditViewController(pageEditVC:KMNPageEditViewController)
- }
- class KMNPageEditViewController: KMNThumbnailBaseViewController {
-
- private var groupView: ComponentGroup? = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
- weak open var pageEditViewController: KMNPageEditViewViewDelegate?
- override func viewDidLoad() {
- super.viewDidLoad()
-
- collectionView.menuClickedAction = {[weak self] point in
- let menuStruct = self?.clickMenu(point: point)
- if self?.groupView != nil {
- self?.groupView?.clickedAutoHide = false
- self?.groupView?.groupDelegate = self
- self?.groupView?.frame = CGRectMake(0, 0, 180, menuStruct?.viewHeight ?? 0)
- self?.groupView?.updateGroupInfo(menuStruct?.menuitems ?? [])
-
- self?.groupView?.showWithPoint(CGPoint(x: point.x, y: point.y - (menuStruct?.viewHeight ?? 0)), relativeTo: self?.collectionView)
- }
- return NSMenu()
- }
- }
-
- override func updateUIThemeColor() {
- super.updateUIThemeColor()
-
- backViewBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-pdfView")
- }
-
- override func updateUILanguage() {
- super.updateUILanguage()
-
- self.groupView?.reloadData()
- }
-
- private func insertFilePath(filePath:String,pdfPassword:String?) {
- let selectedIndexPaths = collectionView.selectionIndexPaths
-
- let insertPDF = KMNPDFInsertPDFWindowController(showDocument, filePath: filePath, password: pdfPassword, selectionIndexPaths: selectedIndexPaths)
- insertPDF.pdfCallback = { [weak self] fileAttribute, insertIdx in
- let doc = fileAttribute.pdfDocument
-
- var insertPages: [CPDFPage] = []
-
- for number in fileAttribute.fetchSelectPages() {
- if let page = doc?.page(at: UInt(number-1)) {
- insertPages.append(page)
- }
- }
- self?.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: doc)
-
- self?.insertFormPages(insertPages: insertPages, pageDex: insertIdx)
- }
- insertPDF.fileCallback = { [weak self] filePath, insertIdx in
- self?.insertFromFilePath(fileNames: [filePath], formDex: 0, indexDex: UInt(insertIdx), selectIndexs: [], completionBlock: { newSelectIndexs in
- })
- }
-
- insertPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
- }
-
- // MARK: - public
- public func canZoomInPageSize()->Bool {
- if pageThumbnailSize.width > defaultItemSize.width {
- return false
- }
- return true
- }
-
- public func canZoomOutPageSize()->Bool {
- if pageThumbnailSize.width < defaultItemSize.width {
- return false
- }
- return true
- }
-
- public func zoomInPageAction() { //放大
- if (canZoomInPageSize() == false) {
- return
- }
- let indexpaths = collectionView.selectionIndexPaths
- if (pageThumbnailSize.width < defaultItemSize.width) {
- pageThumbnailSize = defaultItemSize
- } else if (pageThumbnailSize.width == defaultItemSize.width) {
- pageThumbnailSize = NSSize(width: defaultItemSize.width*1.5, height: defaultItemSize.height*1.5)
- }
-
- collectionView.reloadData()
- collectionView.selectionIndexPaths = indexpaths
- }
-
- public func zoomOutPageAction() { //缩小
- if (canZoomOutPageSize() == false) {
- return
- }
- let indexpaths = collectionView.selectionIndexPaths
- if (pageThumbnailSize.width > defaultItemSize.width) {
- pageThumbnailSize = defaultItemSize
- } else if (pageThumbnailSize.width == defaultItemSize.width) {
- pageThumbnailSize = NSSize(width: defaultItemSize.width*0.5, height: defaultItemSize.height*0.5)
- }
-
- collectionView.reloadData()
- collectionView.selectionIndexPaths = indexpaths
- }
- }
- //MARK: - ComponentSelectDelegate
- extension KMNPageEditViewController: ComponentGroupDelegate {
- func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
- if menuItemProperty?.identifier == ThumbnailMenuIdentifier_Copy {
- copyMenuItemAciton()
- } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Cut) {
- cutMenuItemAciton()
- } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Paste) {
- if(menuItemProperty != nil) {
- pastMenuItemAciton(menuitemProperty: menuItemProperty!)
- }
- } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Delete) {
- cutMenuItemAciton()
- } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_RotateRight) {
- rotatePageRightAction()
- } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_RotateLeft) {
- rotatePageLeftAction()
- } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_InsertFile) {
- insertFromPDFAction()
- } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_InsertBlank) {
- insertFromBlankAction()
- } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Export) {
- extractPDFAction()
- } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Share) {
- let indexpaths = collectionView.selectionIndexPaths
-
- let doucument = showDocument
- let filename : String = doucument?.documentURL.lastPathComponent ?? ""
- let folderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(filename)) ?? ""
-
- try? FileManager.default.removeItem(atPath: folderPath)
-
- let pdfdocument = CPDFDocument()
- let ret = pdfdocument?.importPages(KMNTools.indexpathsToIndexs(indexpaths: indexpaths), from: doucument, at: 0) ?? false
- let url = URL(fileURLWithPath: folderPath)
- if ret {
- let _ = pdfdocument?.write(toFile: folderPath)
- let selects: [URL] = [url]
-
- let picker = NSSharingServicePicker.init(items: selects)
- if(groupView != nil) {
- let rect = CGRectMake(CGRectGetMaxX(groupView!.bounds), CGRectGetMinY(groupView!.bounds) + 35.0, 1, 1)
- picker.show(relativeTo: rect, of: groupView!, preferredEdge: NSRectEdge.maxX)
- }
- }
- } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_FileShowSize) {
- displayPageSizeAction()
- }
- }
-
- func componentGroupDidDismiss(group: ComponentGroup?) {
-
- }
- }
|