Jelajahi Sumber

【BOTA】注释补充搜索功能

tangchao 1 tahun lalu
induk
melakukan
cfd5e14f99

+ 4 - 4
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFKitExtensions/CPDFAnnotationExtensions/CPDFMarkupAnnotation+PDFListView.swift

@@ -220,25 +220,25 @@ class CPDFAnnotationMarkupExtraIvars: NSObject {
                     break
                 }
                 var ptlt: NSPoint = .zero
-                if let data = (points[step] as? NSNumber)?.pointValue {
+                if let data = points[step] as? NSPoint {
                     ptlt = data
                 }
                 step += 1
                 
                 var ptrt: NSPoint = .zero
-                if let data = (points[step] as? NSNumber)?.pointValue {
+                if let data = points[step] as? NSPoint {
                     ptrt = data
                 }
                 step += 1
                 
                 var ptlb: NSPoint = .zero
-                if let data = (points[step] as? NSNumber)?.pointValue {
+                if let data = points[step] as?NSPoint {
                     ptlb = data
                 }
                 step += 1
                 
                 var ptrb: NSPoint = .zero
-                if let data = (points[step] as? NSNumber)?.pointValue {
+                if let data = points[step] as? NSPoint {
                     ptrb = data
                 }
                 step += 1

+ 182 - 11
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/KMLeftSideViewController.swift

@@ -157,7 +157,8 @@ class KMLeftSideViewController: KMSideViewController {
                 if self._findState == .content {
                     self.search(self.searchField)
                 } else if self.findState == .note {
-                    self.searchNotes(self.searchField)
+//                    self.searchNotes(self.searchField)
+                    self.searchNotes(nil)
                 } else if self.findState == .snapshot {
                     self.searchNotes(self.searchField)
                 }
@@ -245,6 +246,9 @@ class KMLeftSideViewController: KMSideViewController {
     var canFoldNotes: [CPDFAnnotation] = []
     
     var isRenameNoteOutline = false
+    var caseInsensitiveNoteSearch = false
+    var noteSearchArray: [CPDFAnnotation] = []
+    var noteSearchMode = false
     
     override func loadView() {
         super.loadView()
@@ -1551,10 +1555,28 @@ class KMLeftSideViewController: KMSideViewController {
     func updateNoteFilterPredicate() {
         //注释筛选
     //    [rightSideController.noteArrayController setFilterPredicate:[noteTypeSheetController filterPredicateForSearchString:[rightSideController.searchField stringValue] caseInsensitive:mwcFlags.caseInsensitiveNoteSearch]];
+        let stringValue = self.noteSearchField.stringValue
         
 //        NSPredicate *predicate = [noteTypeSheetController filterPredicateForSearchString:[rightSideController.searchField stringValue] caseInsensitive:mwcFlags.caseInsensitiveNoteSearch];
 //        [self loadAnnotationSortData:[NSArray arrayWithObjects:predicate, nil]];
-//        [rightSideController.noteOutlineView reloadData];
+        self.noteSearchArray.removeAll()
+        if stringValue.isEmpty {
+            self.noteSearchArray = self.allAnnotations
+        } else {
+            for note in self.allAnnotations {
+                var noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
+                if let anno = note as? CPDFMarkupAnnotation {
+                    noteString = anno.markupContent()
+                }
+                if noteString.contains(stringValue) {
+                    self.noteSearchArray.append(note)
+                }
+            }
+        }
+        
+        Task { @MainActor in
+            self.noteOutlineView.reloadData()
+        }
     }
     
     @objc func selectSelectedNote(_ sender: AnyObject?) {
@@ -1611,6 +1633,140 @@ class KMLeftSideViewController: KMSideViewController {
     }
 }
 
+// MARK: - Note
+
+extension KMLeftSideViewController {
+    func loadAnnotationSortData(_ morePredicates: NSArray) {
+        var isLink = false
+        var typeMutableArr = NSMutableArray()
+        if self.allAnnotations.count > 0 {
+            for annotation in self.allAnnotations {
+                if annotation is CPDFLinkAnnotation || annotation is CPDFTextWidgetAnnotation || annotation is CPDFButtonWidgetAnnotation || annotation is CPDFChoiceWidgetAnnotation {
+//                    [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature] ||
+//                    [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeText] ||
+//                    [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeButton] ||
+//                    [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeChoice]) {
+                    isLink = true
+                } else {
+                    if typeMutableArr.contains(annotation.type) == false {
+                        typeMutableArr.add(annotation.type)
+                    }
+                }
+            }
+        }
+        
+        var colorMutableArray = NSMutableArray()
+        var typeMutableArray = NSMutableArray()
+        var authorMutableArray = NSMutableArray()
+        for annotation in self.allAnnotations {
+            if annotation is CPDFLinkAnnotation || annotation is CPDFTextWidgetAnnotation || annotation is CPDFButtonWidgetAnnotation || annotation is CPDFChoiceWidgetAnnotation {
+//                    [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature] ||
+//                    [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeText] ||
+//                    [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeButton] ||
+//                    [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeChoice]) {
+                let noteColor = annotation.color ?? .clear
+                let noteType = annotation.type ?? ""
+                let authorString = annotation.userName() ?? ""
+                if (noteColor != nil) {
+                    if (colorMutableArray.count > 0) {
+                        if colorMutableArray.contains(noteColor) == false {
+                            colorMutableArray.add(noteColor)
+                        }
+                    } else {
+                        colorMutableArray.add(noteColor)
+                    }
+                }
+                if noteType.isEmpty == false {
+                    if typeMutableArray.count > 0 {
+                        if typeMutableArray.contains(noteType) == false {
+                            typeMutableArray.add(noteType)
+                        }
+                    } else {
+                        typeMutableArray.add(noteType)
+                    }
+                }
+                if authorString.isEmpty == false {
+                    if authorString.count > 0 {
+                        if authorMutableArray.contains(authorString) == false {
+                            authorMutableArray.add(authorString)
+                        }
+                    } else {
+                        authorMutableArray.add(authorString)
+                    }
+                }
+            } else {
+                if typeMutableArr.contains(annotation.type) == false {
+                    typeMutableArr.add(annotation.type)
+                }
+            }
+        }
+        
+        let sud = UserDefaults.standard
+        var typeArr = NSMutableArray()
+        if let typeData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Type") as? Data {
+            if let data = NSKeyedUnarchiver.unarchiveObject(with: typeData) as? NSArray {
+                typeArr = NSMutableArray(array: data)
+            }
+        }
+        var colorArr = NSMutableArray()
+        if let colorData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Color") as? Data {
+            if let data = NSKeyedUnarchiver.unarchiveObject(with: colorData) as? NSArray {
+                colorArr = NSMutableArray(array: data)
+            }
+        }
+        var authorArr = NSMutableArray()
+        if let authorData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Author") as? Data {
+            if let data = NSKeyedUnarchiver.unarchiveObject(with: authorData) as? NSArray {
+                authorArr = NSMutableArray(array: data)
+            }
+        }
+        
+        var temporaryArr1 = NSMutableArray()
+        var temporaryArr2 = NSMutableArray()
+        var temporaryArr3 = NSMutableArray()
+        if (typeArr.count > 0) {
+            for type in typeArr {
+                if typeMutableArray.contains(type) == false {
+                    temporaryArr1.add(type)
+                }
+            }
+        }
+        if (colorArr.count > 0) {
+            for color in colorArr {
+                if colorMutableArray.contains(color) == false {
+                    temporaryArr2.add(color)
+                }
+            }
+        }
+        if (authorArr.count > 0) {
+            for author in authorArr {
+                if authorMutableArray.contains(author) == false {
+                    temporaryArr3.add(author)
+                }
+            }
+        }
+        
+        if (temporaryArr1.count > 0) {
+            for type in temporaryArr1 {
+                typeArr.remove(type)
+            }
+        }
+        if (temporaryArr2.count > 0) {
+            for color in temporaryArr2 {
+                colorArr.remove(color)
+            }
+        }
+        if (temporaryArr3.count > 0) {
+            for author in temporaryArr3 {
+                authorArr.remove(author)
+            }
+        }
+        
+        var predicateMutableArr = NSMutableArray()
+//        predicateMutableArr.add
+    }
+}
+
 // MARK: - KMBotaTableViewDelegate
 
 extension KMLeftSideViewController: KMBotaTableViewDelegate {
@@ -2992,6 +3148,9 @@ extension KMLeftSideViewController: NSOutlineViewDelegate, NSOutlineViewDataSour
                 }
             }
         } else if outlineView.isEqual(to: self.noteOutlineView) {
+            if self.noteSearchMode {
+                return self.noteSearchArray.count
+            }
             var count = 0
             for section in self._annotations {
                 if section.annotations?.count != 0 {
@@ -3103,6 +3262,9 @@ extension KMLeftSideViewController: NSOutlineViewDelegate, NSOutlineViewDataSour
 //            }
 //            else
 //                return [item noteText];
+            if self.noteSearchMode {
+                return self.noteSearchArray[index]
+            }
             var tempArray: [KMBOTAAnnotationItem] = []
             for secion in self._annotations {
                 if secion.annotations?.count != 0 {
@@ -3240,8 +3402,14 @@ extension KMLeftSideViewController: NSOutlineViewDelegate, NSOutlineViewDataSour
 //            }
             return cell
         } else if outlineView.isEqual(to: self.noteOutlineView) {
-            let model = item as! KMBOTAAnnotationItem
-            let note = (item as! KMBOTAAnnotationItem).annotation!
+            let model = item as? KMBOTAAnnotationItem
+            var note: CPDFAnnotation!
+            if self.noteSearchMode {
+                note = item as! CPDFAnnotation
+            } else {
+                note = (item as! KMBOTAAnnotationItem).annotation!
+            }
+            
             let cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMNoteTableViewCell"), owner: self) as! KMNoteTableViewCell
             cell.cellNote = note
             
@@ -3390,7 +3558,7 @@ extension KMLeftSideViewController: NSOutlineViewDelegate, NSOutlineViewDataSour
                 var contentString = KMBOTAAnnotationTool.fetchText(text: markup.markupContent() ?? "")
                 contentString = contentString.replacingOccurrences(of: "\r", with: "")
                 contentString = contentString.replacingOccurrences(of: "\n", with: "")
-//                cell.noteContentLabel.stringValue = contentString
+                cell.noteContentLabel.stringValue = contentString
                 if(contentString.isEmpty == false) {
                     cell.foldButton.isHidden = false
                 }
@@ -3406,9 +3574,9 @@ extension KMLeftSideViewController: NSOutlineViewDelegate, NSOutlineViewDataSour
                 }
                 cell.annotationContentLabel.attributedStringValue = attributeStr
 
-                if (model.foldType == .unfold) {
+                if let data = model?.foldType, data == .unfold {
                     cell.isFold = false
-                } else if (model.foldType == .fold) {
+                } else if let data = model?.foldType, data == .fold {
                     cell.isFold = true
                 } else {
                     if self.allFoldNotes.isEmpty == false && self.allFoldNotes.contains(note) {
@@ -3516,7 +3684,7 @@ extension KMLeftSideViewController: NSOutlineViewDelegate, NSOutlineViewDataSour
                 }
             }
             cell.isUnFoldNote = { [unowned self] cellNote, isUnfold in
-                model.foldType = isUnfold ? .unfold : .fold
+                model?.foldType = isUnfold ? .unfold : .fold
 //                let COLUMN_INDENTATION: CGFloat = 16
 //                if let _cell = tableColumn?.dataCell as? NSCell {
 //                    _cell.objectValue = cell.annotationContentLabel.attributedStringValue
@@ -3531,7 +3699,7 @@ extension KMLeftSideViewController: NSOutlineViewDelegate, NSOutlineViewDataSour
 //                    KMPrint(bound)
                     // 154 34 273
                     // 273 - 188 = 85 noteContentHeightConstraint maltlineLabelLayoutConstraint
-                    model.foldH = isUnfold ? 30 : (cell.noteContentHeightConstraint.constant + cell.maltlineLabelLayoutConstraint.constant + 85)
+                    model?.foldH = isUnfold ? 30 : (cell.noteContentHeightConstraint.constant + cell.maltlineLabelLayoutConstraint.constant + 85)
                 }
                 if (isUnfold) {
                     if (self.allFoldNotes.contains(note)) {
@@ -4500,9 +4668,12 @@ extension KMLeftSideViewController {
     }
     
     @objc func searchNotes(_ sender: AnyObject?) {
+        self.noteSearchMode = false
         if self.findState == .note {
-            //        if (mwcFlags.findState == SKFindStateNote)
-//            [self updateNoteFilterPredicate];
+            if self.noteSearchField.isEqual(to: sender) {
+                self.noteSearchMode = true
+            }
+            self.updateNoteFilterPredicate()
         } else {
             self.updateSnapshotFilterPredicate()
         }