forked from glushchenko/fsnotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorViewController.swift
More file actions
962 lines (741 loc) · 32.4 KB
/
Copy pathEditorViewController.swift
File metadata and controls
962 lines (741 loc) · 32.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
//
// EditorViewController.swift
// FSNotes
//
// Created by Oleksandr Hlushchenko on 26.06.2022.
// Copyright © 2022 Oleksandr Hlushchenko. All rights reserved.
//
import Foundation
import AppKit
import LocalAuthentication
import WebKit
class EditorViewController: NSViewController, NSTextViewDelegate, WebFrameLoadDelegate {
public var alert: NSAlert?
public var noteLoading: ProgressState = .none
public var vcEditor: EditTextView?
public var vcTitleLabel: TitleTextField?
public var vcEmptyEditAreaImage: NSImageView?
public var vcPreviewButton: NSButton?
public var vcShareButton: NSButton?
public var vcLockUnlockButton: NSButton?
public var vcEditorScrollView: EditorScrollView?
public var previewResizeTimer = Timer()
public var rowUpdaterTimer = Timer()
public var editorUndoManager = UndoManager()
public var breakUndoTimer = Timer()
public var printWebView = WebView()
// git
public var snapshotsTimer = Timer()
public var lastSnapshot: Int = 0
public var pullTimer = Timer()
public func initView() {
vcEditor?.delegate = self
}
public func getSelectedNotes() -> [Note]? {
// Active main window
if let cvc = NSApplication.shared.keyWindow?.contentViewController,
cvc.isKind(of: ViewController.self),
let vc = ViewController.shared(),
let selected = vc.notesTableView.getSelectedNotes() {
return selected
}
// Active note window
if let note = vcEditor?.note {
return [note]
}
return nil
}
// MARK: Window bar actions
@IBAction func toggleNotesLock(_ sender: Any) {
guard var notes = getSelectedNotes() else { return }
notes = lockUnlocked(notes: notes)
guard notes.count > 0 else { return }
getMasterPassword() { password, isTypedByUser in
guard password.count > 0 else { return }
for note in notes {
var success = false
if note.container == .encryptedTextPack {
success = note.unLock(password: password)
if success {
if notes.count == 0x01 {
note.password = password
DispatchQueue.main.async {
self.reloadAllOpenedWindows(note: note)
}
}
let insertTags = note.scanContentTags().0
DispatchQueue.main.async {
ViewController.shared()?.sidebarOutlineView?.addTags(insertTags)
}
}
} else {
success = note.encrypt(password: password)
if success {
note.password = nil
DispatchQueue.main.async {
self.reloadAllOpenedWindows(note: note)
ViewController.shared()?.focusTable()
}
}
}
if success && isTypedByUser {
self.save(password: password)
}
DispatchQueue.main.async {
ViewController.shared()?.notesTableView.reloadRow(note: note)
}
}
}
}
@IBAction func togglePreview(_ sender: Any) {
guard let editor = vcEditor else { return }
let firstResp = view.window?.firstResponder
editor.togglePreviewState()
if (editor.isPreviewEnabled()) {
//Preview mode doesn't support text search
cancelTextSearch()
refillEditArea()
if let mdView = vcEditor?.editorViewController?.vcEditor?.markdownView {
view.window?.makeFirstResponder(mdView)
}
} else {
disablePreview()
}
if let responder = firstResp, (
ViewController.shared()?.search.currentEditor() == firstResp
|| responder.isKind(of: NotesTableView.self)
|| responder.isKind(of: SidebarOutlineView.self)
) {
view.window?.makeFirstResponder(firstResp)
} else {
var responder: NSResponder? = vcEditor
if vcEditor?.isPreviewEnabled() == true, let mView = vcEditor?.markdownView {
responder = mView
}
if let responder = responder {
view.window?.makeFirstResponder(responder)
}
}
vcEditor?.userActivity?.needsSave = true
Storage.sharedInstance().saveNotesSettings()
}
@IBAction func toggleMathJax(_ sender: NSMenuItem) {
sender.state = sender.state == .on ? .off : .on
UserDefaultsManagement.mathJaxPreview = sender.state == .on
refillEditArea(force: true)
}
@IBAction func toggleSoulverCore(_ sender: NSMenuItem) {
sender.state = sender.state == .on ? .off : .on
UserDefaultsManagement.soulverPreview = sender.state == .on
refillEditArea(force: true)
}
@IBAction func shareSheet(_ sender: NSButton) {
if let note = vcEditor?.note {
let sharingPicker = NSSharingServicePicker(items: [
note.content,
note.url
])
sharingPicker.delegate = self
sharingPicker.show(relativeTo: NSZeroRect, of: sender, preferredEdge: .minY)
}
}
// MARK: File menu
@IBAction func printNotes(_ sender: NSMenuItem) {
guard let notes = getSelectedNotes(), let note = notes.first else { return }
if note.isMarkdown() {
printMarkdownPreview()
return
}
let pv = NSTextView(frame: NSMakeRect(0, 0, 528, 688))
pv.textStorage?.append(note.content)
let printInfo = NSPrintInfo.shared
printInfo.isHorizontallyCentered = false
printInfo.isVerticallyCentered = false
printInfo.scalingFactor = 1
printInfo.topMargin = 40
printInfo.leftMargin = 40
printInfo.rightMargin = 40
printInfo.bottomMargin = 40
let operation: NSPrintOperation = NSPrintOperation(view: pv, printInfo: printInfo)
operation.printPanel.options.insert(NSPrintPanel.Options.showsPaperSize)
operation.printPanel.options.insert(NSPrintPanel.Options.showsOrientation)
operation.run()
}
@IBAction func finderMenu(_ sender: NSMenuItem) {
guard let notes = getSelectedNotes() else { return }
var urls = [URL]()
for note in notes {
urls.append(note.url)
}
NSWorkspace.shared.activateFileViewerSelecting(urls)
}
@IBAction func pinMenu(_ sender: Any) {
guard let notes = getSelectedNotes() else { return }
ViewController.shared()?.pin(selectedNotes: notes)
}
@IBAction func editorMenu(_ sender: Any) {
guard let notes = getSelectedNotes() else { return }
ViewController.shared()?.external(selectedNotes: notes)
}
@IBAction func copyURL(_ sender: Any) {
guard let note = getSelectedNotes()?.first else { return }
if let title = note.title.addingPercentEncoding(withAllowedCharacters: .alphanumerics) {
let name = "fsnotes://find?id=\(title)"
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
pasteboard.setString(name, forType: NSPasteboard.PasteboardType.string)
let notification = NSUserNotification()
notification.title = "FSNotes"
notification.informativeText = NSLocalizedString("URL has been copied to clipboard", comment: "")
notification.soundName = NSUserNotificationDefaultSoundName
NSUserNotificationCenter.default.deliver(notification)
}
}
@IBAction func copyTitle(_ sender: Any) {
guard let note = getSelectedNotes()?.first else { return }
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
pasteboard.setString(note.title, forType: NSPasteboard.PasteboardType.string)
}
@IBAction func removeNoteEncryption(_ sender: Any) {
guard var notes = getSelectedNotes() else { return }
notes = decryptUnlocked(notes: notes)
guard notes.count > 0 else { return }
UserDataService.instance.fsUpdatesDisabled = true
getMasterPassword() { password, isTypedByUser in
for note in notes {
if note.container == .encryptedTextPack {
let success = note.unEncrypt(password: password)
if success && notes.count == 0x01 {
note.password = nil
DispatchQueue.main.async {
self.reloadAllOpenedWindows(note: note)
}
}
}
ViewController.shared()?.notesTableView.reloadRow(note: note)
}
UserDataService.instance.fsUpdatesDisabled = false
}
}
@IBAction func changeCreationDate(_ sender: Any) {
guard let notes = getSelectedNotes() else { return }
guard let note = notes.first else { return }
guard let creationDate = note.getFileCreationDate() else { return }
guard let window = view.window else { return }
alert = NSAlert()
let field = NSTextField(frame: NSRect(x: 0, y: 0, width: 290, height: 20))
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = formatter.string(from: creationDate)
field.stringValue = date
field.placeholderString = "2020-08-28 21:59:07"
alert?.messageText = NSLocalizedString("Change Creation Date", comment: "Menu") + ":"
alert?.accessoryView = field
alert?.alertStyle = .informational
alert?.addButton(withTitle: "OK")
alert?.beginSheetModal(for: window) { (returnCode: NSApplication.ModalResponse) -> Void in
if returnCode == NSApplication.ModalResponse.alertFirstButtonReturn {
for note in notes {
if note.setCreationDate(string: field.stringValue) {
ViewController.shared()?.notesTableView.reloadRow(note: note)
}
}
}
self.alert = nil
}
field.becomeFirstResponder()
}
// MARK: Move menu
@IBAction func deleteNote(_ sender: Any) {
var forceRemove = false
if let menuItem = sender as? NSMenuItem,
menuItem.identifier?.rawValue == "fileMenu.forceRemove" ||
menuItem.identifier?.rawValue == "context.fileMenu.forceRemove" {
forceRemove = true
}
guard let vc = ViewController.shared() else { return }
guard let notes = getSelectedNotes() else { return }
let si = vc.getSidebarItem()
if si?.isTrash() == true || forceRemove {
vc.removeForever()
// Call from window, close it!
if let cvc = NSApplication.shared.keyWindow?.contentViewController,
cvc.isKind(of: NoteViewController.self) {
DispatchQueue.main.async {
self.view.window?.close()
}
}
return
}
let selectedRow = vc.notesTableView.selectedRowIndexes.min()
UserDataService.instance.searchTrigger = true
vc.notesTableView.removeByNotes(notes: notes)
// Delete tags
for note in notes {
let tags = note.tags
note.tags.removeAll()
vc.sidebarOutlineView.removeTags(tags)
}
vc.storage.removeNotes(notes: notes) { urls in
if let md = AppDelegate.mainWindowController {
let undoManager = md.notesListUndoManager
if let ntv = vc.notesTableView {
undoManager.registerUndo(withTarget: ntv, selector: #selector(ntv.unDelete), object: urls)
undoManager.setActionName(NSLocalizedString("Delete", comment: ""))
}
if let i = selectedRow, i > -1 {
if vc.notesTableView.noteList.count > i {
vc.notesTableView.selectRow(i)
} else {
vc.notesTableView.selectRow(vc.notesTableView.noteList.count - 1)
}
}
UserDataService.instance.searchTrigger = false
}
vc.editor.clear()
}
// Call from window, close it!
if let cvc = NSApplication.shared.keyWindow?.contentViewController,
cvc.isKind(of: NoteViewController.self) {
DispatchQueue.main.async {
self.view.window?.close()
}
return
}
// If is main window – focus to notes list
if let cvc = NSApplication.shared.keyWindow?.contentViewController,
cvc.isKind(of: ViewController.self) {
NSApp.mainWindow?.makeFirstResponder(vc.notesTableView)
}
}
@IBAction func archiveNote(_ sender: Any) {
guard let vc = ViewController.shared() else { return }
guard let notes = getSelectedNotes() else { return }
if let project = Storage.sharedInstance().getArchive() {
vc.moveReq(notes: notes, project: project) { success in
guard success else { return }
if let cvc = NSApplication.shared.keyWindow?.contentViewController,
cvc.isKind(of: NoteViewController.self) {
self.updateTitle(note: notes.first!)
}
}
}
}
@IBAction func createInNewWindow(_ sender: Any) {
var content = String()
if let inlineTags = ViewController.shared()?.sidebarOutlineView.getSelectedInlineTags() {
content = inlineTags
}
if let note = createNote(content: content, openInNewWindow: true) {
openInNewWindow(note: note)
}
}
@IBAction func quickNote(_ sender: Any) {
if let note = createNote(content: "", openInNewWindow: true) {
NSApp.activate(ignoringOtherApps: true)
if !NSApp.isActive {
AppDelegate.mainWindowController?.window?.miniaturize(self)
}
openInNewWindow(note: note)
}
}
@IBAction func historyMenu(_ sender: Any) {
guard let cvc = NSApplication.shared.keyWindow?.contentViewController,
let vc = ViewController.shared(),
let note = getSelectedNotes()?.first else { return }
let moveMenu = NSMenu()
do {
if let repository = try note.project.getRepository() {
let commits = getCommits(from: repository, by: note)
// Port
if commits.count == 0 {
return
}
for commit in commits {
let menuItem = NSMenuItem()
menuItem.title = commit.getDate()
menuItem.representedObject = commit
menuItem.action = #selector(vc.checkoutRevision(_:))
moveMenu.addItem(menuItem)
}
let general = moveMenu.item(at: 0)
// Main window
if cvc.isKind(of: ViewController.self),
vc.notesTableView.selectedRow >= 0 {
let view = vc.notesTableView.rect(ofRow: vc.notesTableView.selectedRow)
let x = vc.splitView.subviews[0].frame.width + 5
moveMenu.popUp(positioning: general, at: NSPoint(x: x, y: view.origin.y + 8), in: vc.notesTableView)
return
}
// Opened in new window
if cvc.isKind(of: NoteViewController.self) {
moveMenu.popUp(positioning: general, at: NSPoint(x: view.frame.width + 10, y: view.frame.height - 5), in: view)
}
}
} catch {
print(error)
}
}
public func getCommits(from repository: Repository, by note: Note) -> [Commit] {
let path = note.getGitPath().recode4byteString()
var commits = [Commit]()
do {
let fileRevLog = try FileHistoryIterator(repository: repository, path: path)
while let rev = fileRevLog.next() {
if let commit = try? repository.commitLookup(oid: rev) {
commits.append(commit)
}
}
if fileRevLog.checkFirstCommit() {
if let oid = fileRevLog.getLast(), let commit = try? repository.commitLookup(oid: oid) {
commits.append(commit)
}
}
} catch {/*_*/}
return commits
}
@IBAction func duplicate(_ sender: Any) {
guard let notes = getSelectedNotes() else { return }
for note in notes {
let src = note.url
let dst = NameHelper.generateCopy(file: note.url)
if note.isTextBundle() || note.isEncrypted() {
try? FileManager.default.copyItem(at: src, to: dst)
continue
}
let name = dst.deletingPathExtension().lastPathComponent
let noteDupe = Note(name: name, project: note.project, type: note.type, cont: note.container)
noteDupe.content = NSMutableAttributedString(string: note.content.string)
// Clone images
if note.type == .Markdown && note.container == .none {
let images = note.getAllImages()
for image in images {
noteDupe.move(from: image.url, imagePath: image.path, to: note.project, copy: true)
}
}
noteDupe.save()
Storage.sharedInstance().add(noteDupe)
ViewController.shared()?.notesTableView.insertNew(note: noteDupe)
}
}
@IBAction func importNote(_ sender: NSMenuItem) {
guard let vc = ViewController.shared() else { return }
let panel = NSOpenPanel()
panel.allowsMultipleSelection = true
panel.canChooseDirectories = false
panel.canChooseFiles = true
panel.canCreateDirectories = false
panel.begin { (result) -> Void in
if result == NSApplication.ModalResponse.OK {
let urls = panel.urls
let project = vc.getSidebarProject() ?? Storage.sharedInstance().getMainProject()
for url in urls {
_ = vc.copy(project: project, url: url)
}
}
}
}
@objc func moveNote(_ sender: NSMenuItem) {
let project = sender.representedObject as! Project
guard let notes = getSelectedNotes() else { return }
ViewController.shared()?.moveReq(notes: notes, project: project) { success in
guard success else { return }
if let cvc = NSApplication.shared.keyWindow?.contentViewController,
cvc.isKind(of: NoteViewController.self) {
self.updateTitle(note: notes.first!)
}
}
}
// MARK: Dep methods
public func openInNewWindow(note: Note, frame: NSRect? = nil, preview: Bool = false) {
guard let windowController = NSStoryboard(name: "Main", bundle: nil)
.instantiateController(withIdentifier: "noteWindowController") as? NSWindowController else { return }
windowController.showWindow(nil)
windowController.window?.makeKeyAndOrderFront(windowController)
let viewController = windowController.contentViewController as! NoteViewController
viewController.initWindow()
viewController.editor.changePreviewState(preview)
viewController.editor.fill(note: note)
if note.isEncryptedAndLocked() {
viewController.toggleNotesLock(self)
}
AppDelegate.noteWindows.insert(windowController, at: 0)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
if let frame = frame {
windowController.window?.setFrame(frame, display: true)
}
viewController.view.window?.makeFirstResponder(viewController.editor)
}
}
func cancelTextSearch() {
let menu = NSMenuItem(title: "", action: nil, keyEquivalent: "")
menu.tag = NSTextFinder.Action.hideFindInterface.rawValue
vcEditor?.performTextFinderAction(menu)
}
func disablePreview() {
vcEditor?.disablePreviewEditorAndNote()
vcEditor?.markdownView?.removeFromSuperview()
vcEditor?.markdownView = nil
guard let editor = self.vcEditor else { return }
editor.subviews.removeAll(where: { $0.isKind(of: MPreviewView.self) })
refillEditArea()
}
public func viewDidResize() {
guard vcEditor?.isPreviewEnabled() == true else { return }
if noteLoading != .incomplete {
previewResizeTimer.invalidate()
previewResizeTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(reloadPreview), userInfo: nil, repeats: false)
}
}
@objc private func reloadPreview() {
DispatchQueue.main.async {
MPreviewView.template = nil
self.refillEditArea(force: true)
}
}
public func updateTitle(note: Note) {
guard let vcTitleLabel = vcTitleLabel else { return }
var titleString = note.getFileName()
if titleString.isValidUUID {
titleString = String()
}
if titleString.count > 0 {
vcTitleLabel.stringValue = note.project.getNestedLabel() + " › " + titleString
} else {
vcTitleLabel.stringValue = note.project.getNestedLabel()
}
vcTitleLabel.currentEditor()?.selectedRange = NSRange(location: 0, length: 0)
view.window?.title = vcTitleLabel.stringValue
}
func refillEditArea(saveTyping: Bool = false, force: Bool = false) {
noteLoading = .incomplete
vcPreviewButton?.state = vcEditor?.isPreviewEnabled() == true ? .on : .off
if let note = vcEditor?.note {
vcEditor?.fill(note: note, saveTyping: saveTyping, force: force)
}
noteLoading = .done
}
public func unLock(notes: [Note]) {
getMasterPassword() { password, isTypedByUser in
guard password.count > 0 else { return }
var i = 0
for note in notes {
let success = note.unLock(password: password)
if success {
let insertTags = note.scanContentTags().0
DispatchQueue.main.async {
ViewController.shared()?.sidebarOutlineView?.addTags(insertTags)
ViewController.shared()?.notesTableView.reloadRow(note: note)
}
if i == 0 {
note.password = password
DispatchQueue.main.async {
self.reloadAllOpenedWindows(note: note)
}
if isTypedByUser {
self.save(password: password)
}
}
}
i = i + 1
}
}
}
public func reloadAllOpenedWindows(note: Note) {
let editors = AppDelegate.getEditTextViews()
for editor in editors {
if editor.note == note {
editor.editorViewController?.refillEditArea(force: true)
editor.window?.makeFirstResponder(editor)
}
}
}
public func getMasterPassword(completion: @escaping (String, Bool) -> ()) {
if #available(OSX 10.12.2, *), UserDefaultsManagement.allowTouchID {
let context = LAContext()
context.localizedFallbackTitle = NSLocalizedString("Enter Master Password", comment: "")
guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) else {
masterPasswordPrompt(completion: completion)
return
}
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "To access secure data") { (success, evaluateError) in
if !success {
self.masterPasswordPrompt(completion: completion)
return
}
do {
let item = KeychainPasswordItem(service: KeychainConfiguration.serviceName, account: "Master Password")
let password = try item.readPassword()
completion(password, false)
return
} catch {
print(error)
}
self.masterPasswordPrompt(completion: completion)
}
} else {
masterPasswordPrompt(completion: completion)
}
}
private func masterPasswordPrompt(completion: @escaping (String, Bool) -> ()) {
DispatchQueue.main.async {
guard let window = self.view.window else { return }
self.alert = NSAlert()
guard let alert = self.alert else { return }
let field = NSSecureTextField(frame: NSRect(x: 0, y: 0, width: 290, height: 20))
alert.messageText = NSLocalizedString("Master password:", comment: "")
alert.informativeText = NSLocalizedString("Please enter password for current note", comment: "")
alert.accessoryView = field
alert.alertStyle = .informational
alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
alert.beginSheetModal(for: window) { (returnCode: NSApplication.ModalResponse) -> Void in
if returnCode == NSApplication.ModalResponse.alertFirstButtonReturn {
completion(field.stringValue, true)
}
self.alert = nil
}
field.becomeFirstResponder()
}
}
private func save(password: String) {
guard password.count > 0, UserDefaultsManagement.savePasswordInKeychain else { return }
let item = KeychainPasswordItem(service: KeychainConfiguration.serviceName, account: "Master Password")
var oldPassword = String()
do {
oldPassword = try item.readPassword()
} catch {/*_*/}
do {
guard oldPassword.count == 0 else { return }
try item.savePassword(password)
} catch {
print("Master password saving error: \(error)")
}
}
public func lockUnlocked(notes: [Note]) -> [Note] {
var notes = notes
var isFirst = true
for note in notes {
if note.isUnlocked() && note.isEncrypted() {
if note.lock() && isFirst {
reloadAllOpenedWindows(note: note)
}
removeTags(note: note)
notes.removeAll { $0 === note }
}
isFirst = false
ViewController.shared()?.notesTableView.reloadRow(note: note)
}
// Focus notes list if active main window
if let vc = view.window?.contentViewController as? ViewController, let mainWindow = view.window {
mainWindow.makeFirstResponder(vc.notesTableView)
}
return notes
}
public func decryptUnlocked(notes: [Note]) -> [Note] {
var notes = notes
for note in notes {
if note.isUnlocked() {
if note.unEncryptUnlocked() {
notes.removeAll { $0 === note }
ViewController.shared()?.notesTableView.reloadRow(note: note)
}
}
}
return notes
}
public func removeTags(note: Note) {
let tags = note.tags
note.tags = []
ViewController.shared()?.sidebarOutlineView?.removeTags(tags)
}
public func dropTitle() {
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? "FSNotes"
vcTitleLabel?.stringValue = appName
view.window?.title = appName
}
func focusEditArea() {
guard let editor = vcEditor, let note = editor.note,
!editor.isPreviewEnabled() || note.isRTF(),
note.container != .encryptedTextPack
else { return }
editor.window?.makeFirstResponder(editor)
if let ntv = ViewController.shared()?.notesTableView, ntv.selectedRow > -1 {
vcEditor?.isEditable = true
vcEmptyEditAreaImage?.isHidden = true
}
}
// Changed main edit view
func textDidChange(_ notification: Notification) {
guard let editor = vcEditor,
let note = editor.note,
let vc = ViewController.shared() else { return }
vc.prevCommit = nil
vc.blockFSUpdates()
if editor.isEditable {
editor.removeHighlight()
editor.saveImages()
note.save(attributed: editor.attributedString())
vc.reSort(note: note)
}
breakUndoTimer.invalidate()
breakUndoTimer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(breakUndo), userInfo: nil, repeats: true)
}
@objc func breakUndo() {
guard let editor = vcEditor else { return }
if (
editor.isPreviewEnabled() == false
&& editor.isEditable
) {
editor.breakUndoCoalescing()
}
}
public func createNote(name: String = "", content: String = "", type: NoteType? = nil, project: Project? = nil, load: Bool = false, openInNewWindow: Bool = false) -> Note? {
guard let vc = ViewController.shared() else { return nil }
let selectedProjects = vc.sidebarOutlineView.getSidebarProjects()
var sidebarProject = project ?? selectedProjects?.first
var text = content
if let type = vc.getSidebarType(), type == .Todo, content.count == 0 {
text = "- [ ] "
}
if sidebarProject == nil {
sidebarProject = Storage.sharedInstance().getRootProject()
}
guard let project = sidebarProject else { return nil }
let note = Note(name: name, project: project, type: type)
note.content = NSMutableAttributedString(string: text)
note.save()
_ = note.scanContentTags()
if let selectedProjects = selectedProjects, !selectedProjects.contains(project) {
return note
}
if !openInNewWindow {
disablePreview()
vc.notesTableView.deselectNotes()
vc.editor.string = text
vc.editor.note = note
vc.search.stringValue.removeAll()
}
vc.updateTable() {
if openInNewWindow {
return
}
DispatchQueue.main.async {
vc.notesTableView.saveNavigationHistory(note: note)
if let index = vc.notesTableView.getIndex(note) {
vc.notesTableView.selectRowIndexes([index], byExtendingSelection: false)
vc.notesTableView.scrollRowToVisible(index)
}
vc.focusEditArea()
}
}
// Project encrypted and unlocked – encrypt by default
if let password = project.password {
if note.encrypt(password: password) {
_ = note.unLock(password: password)
}
}
return note
}
}