|
@@ -137,54 +137,100 @@ extension KMMainViewController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- func convertNotesUsingPDFDocument(_ pdfDocument: CPDFDocument) {
|
|
|
- beginProgressSheet(withMessage: NSLocalizedString("Converting notes", comment: "Message for progress sheet").appending("..."), maxValue: 0)
|
|
|
-
|
|
|
- let count = pdfDocument.pageCount
|
|
|
- let pdfView = listView
|
|
|
+ func showConvertNotesProgress() {
|
|
|
+ guard let pdfDocument = self.document else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ guard let _ = self.listView?.document else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ guard let _ = self.view.window else {
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- for i in 0..<count {
|
|
|
- let page = pdfDocument.page(at: i)
|
|
|
-
|
|
|
- var addAnnotations = [CPDFAnnotation]()
|
|
|
- var removeAnnotations = [CPDFAnnotation]()
|
|
|
+ self.model.needConvertNotes = false
|
|
|
+ Task { @MainActor in
|
|
|
+ self.convertNotesUsingPDFDocument(pdfDocument)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func convertNotesUsingPDFDocument(_ pdfDocument: CPDFDocument) {
|
|
|
+ guard let doc = self.listView?.document else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ self.beginProgressSheet(withMessage: NSLocalizedString("Converting notes", comment: "Message for progress sheet").appending("..."), maxValue: 0)
|
|
|
|
|
|
- for annotation in page?.annotations ?? [] {
|
|
|
- var newAnnotation: CPDFAnnotation?
|
|
|
- if let inkAnnotation = annotation as? CPDFInkAnnotation, inkAnnotation.contents.hasPrefix("<?xml version=\"1.0\" encoding=\"utf-8\"?>") {
|
|
|
- let table = KMTableAnnotation(KMNoteBounds: inkAnnotation.bounds, document: pdfView!.document)
|
|
|
- table.border = inkAnnotation.border
|
|
|
- table.color = inkAnnotation.color
|
|
|
- table.createForm(withList: inkAnnotation.contents, andPaths: inkAnnotation.bezierPaths())
|
|
|
- table.updateAppearanceInk(withIsAdd: false)
|
|
|
- table.contents = annotation.contents
|
|
|
- newAnnotation = table
|
|
|
- }
|
|
|
- if let newAnnotation = newAnnotation {
|
|
|
- addAnnotations.append(newAnnotation)
|
|
|
- removeAnnotations.append(annotation)
|
|
|
+ let count = pdfDocument.pageCount
|
|
|
+ DispatchQueue.global().async {
|
|
|
+// var addAnnotations = [CPDFAnnotation]()
|
|
|
+// var removeAnnotations = [CPDFAnnotation]()
|
|
|
+ self.model.addAnnotations.removeAll()
|
|
|
+ self.model.removeAnnotations.removeAll()
|
|
|
+ for i in 0..<count {
|
|
|
+ let page = pdfDocument.page(at: i)
|
|
|
+
|
|
|
+ // var addAnnotations = [CPDFAnnotation]()
|
|
|
+ // var removeAnnotations = [CPDFAnnotation]()
|
|
|
+ for annotation in page?.annotations ?? [] {
|
|
|
+ var newAnnotation: CPDFAnnotation?
|
|
|
+ if let inkAnnotation = annotation as? CPDFInkAnnotation, inkAnnotation.contents.hasPrefix("<?xml version=\"1.0\" encoding=\"utf-8\"?>") {
|
|
|
+ let table = KMTableAnnotation(KMNoteBounds: inkAnnotation.bounds, document: doc)
|
|
|
+ table.border = inkAnnotation.border
|
|
|
+ table.color = inkAnnotation.color
|
|
|
+ table.createForm(withList: inkAnnotation.contents, andPaths: inkAnnotation.bezierPaths())
|
|
|
+ table.updateAppearanceInk(withIsAdd: false)
|
|
|
+ table.contents = annotation.contents
|
|
|
+ newAnnotation = table
|
|
|
+ }
|
|
|
+ if let newAnnotation = newAnnotation {
|
|
|
+ self.model.addAnnotations.append(newAnnotation)
|
|
|
+ self.model.removeAnnotations.append(annotation)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // for i in 0..<addAnnotations.count {
|
|
|
+ // let newAnnotation = addAnnotations[i]
|
|
|
+ // let annotation = removeAnnotations[i]
|
|
|
+ //
|
|
|
+ // // this is only to make sure markup annotations generate the lineRects, for thread safety
|
|
|
+ // pdfView?.addAnnotation(with: newAnnotation, to: page)
|
|
|
+ // pdfView?.remove(annotation)
|
|
|
+ // if newAnnotation.contents != nil {
|
|
|
+ // if newAnnotation.contents.count == 0 {
|
|
|
+ // newAnnotation.autoUpdateString()
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- for i in 0..<addAnnotations.count {
|
|
|
- let newAnnotation = addAnnotations[i]
|
|
|
- let annotation = removeAnnotations[i]
|
|
|
|
|
|
- // this is only to make sure markup annotations generate the lineRects, for thread safety
|
|
|
- pdfView?.addAnnotation(with: newAnnotation, to: page)
|
|
|
- pdfView?.remove(annotation)
|
|
|
- if newAnnotation.contents != nil {
|
|
|
- if newAnnotation.contents.count == 0 {
|
|
|
- newAnnotation.autoUpdateString()
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ for i in 0..<self.model.addAnnotations.count {
|
|
|
+ let newAnnotation = self.model.addAnnotations[i]
|
|
|
+ let annotation = self.model.removeAnnotations[i]
|
|
|
+
|
|
|
+ let page = annotation.page
|
|
|
+ // this is only to make sure markup annotations generate the lineRects, for thread safety
|
|
|
+ self.listView?.addAnnotation(with: newAnnotation, to: page)
|
|
|
+ self.listView?.remove(annotation)
|
|
|
+ if newAnnotation.contents != nil {
|
|
|
+ if newAnnotation.contents.count == 0 {
|
|
|
+ newAnnotation.autoUpdateString()
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ self.dismissProgressSheet()
|
|
|
+
|
|
|
+ self.listView?.undoManager?.removeAllActions()
|
|
|
+ self.undoManager?.removeAllActions()
|
|
|
+
|
|
|
+ // 清空数据
|
|
|
+ self.model.addAnnotations.removeAll()
|
|
|
+ self.model.removeAnnotations.removeAll()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- dismissProgressSheet()
|
|
|
-
|
|
|
- pdfView?.undoManager?.removeAllActions()
|
|
|
- undoManager?.removeAllActions()
|
|
|
}
|
|
|
|
|
|
// MARK: - KMInterfaceThemeChangedProtocol
|