Repository navigation
Expand file tree
/
Copy pathNotesTableView.swift
More file actions
595 lines (474 loc) · 17.9 KB
/
Copy pathNotesTableView.swift
File metadata and controls
595 lines (474 loc) · 17.9 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
//
// NotesTableView.swift
// FSNotes
//
// Created by Oleksandr Glushchenko on 7/31/17.
// Copyright © 2017 Oleksandr Glushchenko. All rights reserved.
//
import Carbon
import Cocoa
import FSNotesCore_macOS
class NotesTableView: NSTableView, NSTableViewDataSource,
NSTableViewDelegate {
var noteList = [Note]()
var defaultCell = NoteCellView()
var pinnedCell = NoteCellView()
var storage = Storage.sharedInstance()
public var history = [URL]()
public var historyPosition = 0
public var limitedActionsList = [
"note.print",
"note.copyTitle",
"note.copyURL",
"note.rename",
"note.saveRevision",
"note.history"
]
override func draw(_ dirtyRect: NSRect) {
allowsTypeSelect = false
self.gridColor = NSColor.clear
self.dataSource = self
self.delegate = self
super.draw(dirtyRect)
}
override func keyUp(with event: NSEvent) {
guard let vc = self.window?.contentViewController as? ViewController else {
super.keyUp(with: event)
return
}
if event.keyCode == kVK_Tab && !event.modifierFlags.contains(.control) {
if vc.editor?.isPreviewEnabled() == true {
NSApp.mainWindow?.makeFirstResponder(vc.editor.markdownView)
} else {
vc.focusEditArea()
}
return
}
super.keyUp(with: event)
}
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
return true
}
override func mouseDown(with event: NSEvent) {
guard let vc = self.window?.contentViewController as? ViewController else { return }
if let selectedProject = vc.getSidebarProject(),
selectedProject.isLocked()
{
vc.sidebarOutlineView.toggleFolderLock(NSMenuItem())
return
}
UserDataService.instance.searchTrigger = false
super.mouseDown(with: event)
}
func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
if (noteList.indices.contains(row)) {
saveNavigationHistory(note: noteList[row])
}
return true
}
override func rightMouseDown(with event: NSEvent) {
UserDataService.instance.searchTrigger = false
let point = convert(event.locationInWindow, from: nil)
let rowIndex = row(at: point)
if (rowIndex < 0 || self.numberOfRows < rowIndex) {
return
}
saveNavigationHistory(note: noteList[rowIndex])
if !selectedRowIndexes.contains(rowIndex) {
selectRowIndexes(IndexSet(integer: rowIndex), byExtendingSelection: false)
scrollRowToVisible(rowIndex)
}
if rowView(atRow: rowIndex, makeIfNecessary: false) as? NoteRowView != nil {
if let menu = menu {
NSMenu.popUpContextMenu(menu, with: event, for: self)
}
}
}
// Custom note highlight style
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
return NoteRowView()
}
// Populate table data
func numberOfRows(in tableView: NSTableView) -> Int {
return noteList.count
}
func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
let height = CGFloat(21 + UserDefaultsManagement.cellSpacing)
if !UserDefaultsManagement.horizontalOrientation && !UserDefaultsManagement.hidePreviewImages {
if row < noteList.count {
let note = noteList[row]
note.loadPreviewInfo()
if let urls = note.imageUrl, urls.count > 0 {
let previewCharsQty = note.preview.count
if (previewCharsQty == 0) {
if note.getTitle() != nil {
// Title + image
return 79 + 17
}
// Images only
return 79
}
// Title + Prevew + Images
return (height + 58)
}
}
}
// Title + preview
return height
}
// On selected row show notes in right panel
func tableViewSelectionDidChange(_ notification: Notification) {
let vc = self.window?.contentViewController as! ViewController
if vc.editAreaScroll.isFindBarVisible {
let menu = NSMenuItem(title: "", action: nil, keyEquivalent: "")
menu.tag = NSTextFinder.Action.hideFindInterface.rawValue
vc.editor.performTextFinderAction(menu)
}
if UserDataService.instance.isNotesTableEscape {
if vc.sidebarOutlineView.selectedRow == -1 {
UserDataService.instance.isNotesTableEscape = false
}
vc.sidebarOutlineView.deselectAll(nil)
vc.sidebarOutlineView.reloadTags()
vc.editor.clear()
return
}
// Select row
if (noteList.indices.contains(selectedRow)) {
let note = noteList[selectedRow]
guard selectedRowIndexes.count == 0x01 else {
vc.editor.clear()
return
}
vc.editor.changePreviewState(note.previewState)
vc.editor.fill(note: note, highlight: true)
if UserDefaultsManagement.focusInEditorOnNoteSelect && !UserDataService.instance.searchTrigger {
vc.focusEditArea()
}
return
}
// Clean
vc.editor.clear()
if !UserDefaultsManagement.inlineTags {
vc.sidebarOutlineView.deselectAllTags()
}
}
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
if (noteList.indices.contains(row)) {
return noteList[row]
}
return nil
}
func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
var title = String()
var urls = [URL]()
for row in rowIndexes {
let note = noteList[row]
urls.append(note.url)
if let unwarpped = note.title.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) {
title = "fsnotes://find/" + unwarpped
}
}
pboard.setString(title, forType: NSPasteboard.PasteboardType.string)
let data = NSKeyedArchiver.archivedData(withRootObject: urls)
pboard.setData(data, forType: NSPasteboard.noteType)
return true
}
@IBAction func copy(_ sender: Any) {
guard let vc = ViewController.shared() else { return }
vc.saveTextAtClipboard()
}
func getNoteFromSelectedRow() -> Note? {
var note: Note? = nil
let selected = self.selectedRow
if (selected < 0) {
return nil
}
if (noteList.indices.contains(selected)) {
note = noteList[selected]
}
return note
}
func getSelectedNote() -> Note? {
var note: Note? = nil
let row = selectedRow
if (noteList.indices.contains(row)) {
note = noteList[row]
}
return note
}
func getSelectedNotes() -> [Note]? {
var notes = [Note]()
for row in selectedRowIndexes {
if (noteList.indices.contains(row)) {
notes.append(noteList[row])
}
}
if notes.isEmpty {
return nil
}
return notes
}
public func deselectNotes() {
self.deselectAll(nil)
}
override func performKeyEquivalent(with event: NSEvent) -> Bool {
if event.modifierFlags.contains(.control) && event.keyCode == kVK_Tab {
return true
}
return super.performKeyEquivalent(with: event)
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
guard noteList.indices.contains(row) else {
return nil
}
let note = noteList[row]
if (note.isPinned) {
pinnedCell = makeCell(note: note)
pinnedCell.pin.frame.size.width = 23
return pinnedCell
}
defaultCell = makeCell(note: note)
defaultCell.pin.frame.size.width = 0
return defaultCell
}
func makeCell(note: Note) -> NoteCellView {
let cell = makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "NoteCellView"), owner: self) as! NoteCellView
cell.configure(note: note)
cell.loadImagesPreview()
cell.attachHeaders(note: note)
return cell
}
override func willOpenMenu(_ menu: NSMenu, with event: NSEvent) {
if (clickedRow > -1 && selectedRow < 0) {
selectRowIndexes([clickedRow], byExtendingSelection: false)
}
if selectedRow < 0 {
return
}
guard let vc = self.window?.contentViewController as? ViewController else { return }
menu.autoenablesItems = false
var note = vc.editor.note
if note == nil {
note = vc.getSelectedNotes()?.first
}
for menuItem in menu.items {
if let identifier = menuItem.identifier?.rawValue,
limitedActionsList.contains(identifier)
{
menuItem.isEnabled = (vc.notesTableView.selectedRowIndexes.count == 1)
}
if menuItem.identifier?.rawValue == "fileMenu.pinUnpin" {
if let note = note {
menuItem.title = note.isPinned
? NSLocalizedString("Unpin", comment: "")
: NSLocalizedString("Pin", comment: "")
}
}
if menuItem.identifier?.rawValue == "note.toggleContainer" {
if let note = note, note.container != .encryptedTextPack {
menuItem.title = note.container == .none
? NSLocalizedString("Convert to TextBundle", comment: "")
: NSLocalizedString("Convert to Plain", comment: "")
menuItem.isEnabled = true
} else {
menuItem.isEnabled = false
}
}
if menuItem.identifier?.rawValue == "fileMenu.lockUnlock" {
if let note = note {
menuItem.title = note.isEncryptedAndLocked()
? NSLocalizedString("Unlock", comment: "")
: NSLocalizedString("Lock", comment: "")
}
}
if menuItem.identifier?.rawValue == "fileMenu.removeEncryption" {
if let note = note, note.isEncrypted() {
menuItem.isEnabled = true
menuItem.isHidden = false
} else {
menuItem.isEnabled = false
menuItem.isHidden = true
}
}
if menuItem.identifier?.rawValue == "noteMenu.removeOverSSH" {
if let note = vc.editor.note, !note.isEncrypted(), note.uploadPath != nil || note.apiId != nil {
menuItem.isHidden = false
} else {
menuItem.isHidden = true
}
}
if menuItem.identifier?.rawValue == "noteMenu.uploadOverSSH" {
if let note = vc.editor.note, !note.isEncrypted() {
if note.uploadPath != nil || note.apiId != nil {
menuItem.title = NSLocalizedString("Update Web Page", comment: "")
} else {
menuItem.title = NSLocalizedString("Create Web Page", comment: "")
}
menuItem.isHidden = false
} else {
menuItem.isHidden = true
}
}
}
vc.loadMoveMenu()
}
func getIndex(_ note: Note) -> Int? {
if let index = noteList.firstIndex(where: {$0 === note}) {
return index
}
return nil
}
public func selectCurrent() {
guard noteList.count > 0 else { return }
UserDataService.instance.searchTrigger = false
let i = selectedRowIndexes.count > 0 ? selectedRowIndexes : [0]
if let first = i.first {
saveNavigationHistory(note: noteList[first])
selectRowIndexes(i, byExtendingSelection: false)
scrollRowToVisible(first)
}
}
public func selectNext() {
UserDataService.instance.searchTrigger = false
let i = selectedRow + 1
if (noteList.indices.contains(i)) {
saveNavigationHistory(note: noteList[i])
}
if (noteList.indices.contains(i)) {
self.selectRowIndexes([i], byExtendingSelection: false)
self.scrollRowToVisible(i)
}
}
public func selectPrev() {
UserDataService.instance.searchTrigger = false
let i = selectedRow - 1
if (noteList.indices.contains(i)) {
saveNavigationHistory(note: noteList[i])
}
if (noteList.indices.contains(i)) {
self.selectRowIndexes([i], byExtendingSelection: false)
self.scrollRowToVisible(i)
}
}
public func selectRow(_ i: Int) {
if (noteList.indices.contains(i)) {
DispatchQueue.main.async {
self.selectRowIndexes([i], byExtendingSelection: false)
self.scrollRowToVisible(i)
}
}
}
public func selectRowAndSidebarItem(note: Note) {
guard let vc = ViewController.shared() else { return }
if let index = getIndex(note) {
selectRow(index)
} else {
vc.sidebarOutlineView.select(note: note)
}
}
func setSelected(note: Note) {
if let i = getIndex(note) {
selectRow(i)
scrollRowToVisible(i)
}
}
func removeByNotes(notes: [Note]) {
guard let vc = ViewController.shared() else { return }
beginUpdates()
for note in notes {
if let i = noteList.firstIndex(where: {$0 === note}) {
let indexSet = IndexSet(integer: i)
noteList.remove(at: i)
removeRows(at: indexSet, withAnimation: .slideDown)
}
}
endUpdates()
if UserDefaultsManagement.inlineTags {
vc.sidebarOutlineView.removeTags(notes: notes)
}
}
@objc public func unDelete(_ urls: [URL: URL]) {
for (src, dst) in urls {
do {
if let note = storage.getBy(url: src) {
storage.removeBy(note: note)
if let destination = Storage.sharedInstance().getProjectByNote(url: dst) {
note.moveImages(to: destination)
}
}
try FileManager.default.moveItem(at: src, to: dst)
} catch {
print(error)
}
}
}
public func countVisiblePinned() -> Int {
var i = 0
for note in noteList {
if (note.isPinned) {
i += 1
}
}
return i
}
public func insertNew(note: Note) {
guard let vc = self.window?.contentViewController as? ViewController else { return }
guard noteList.first(where: { $0.isEqualURL(url: note.url) }) == nil else { return }
guard vc.isFit(note: note, shouldLoadMain: true) else { return }
let at = self.countVisiblePinned()
self.noteList.insert(note, at: at)
self.beginUpdates()
self.insertRows(at: IndexSet(integer: at), withAnimation: .effectFade)
self.reloadData(forRowIndexes: IndexSet(integer: at), columnIndexes: [0])
self.endUpdates()
vc.sidebarOutlineView.insertTags(note: note)
}
public func reloadRow(note: Note) {
note.invalidateCache()
note.loadPreviewInfo()
let urls = note.imageUrl
DispatchQueue.main.async {
if let i = self.noteList.firstIndex(of: note) {
if let row = self.rowView(atRow: i, makeIfNecessary: false) as? NoteRowView, let cell = row.subviews.first as? NoteCellView {
cell.date.stringValue = note.getDateForLabel()
cell.loadImagesPreview(position: i, urls: urls)
cell.attachHeaders(note: note)
cell.renderPin()
cell.applyPreviewStyle()
self.noteHeightOfRows(withIndexesChanged: [i])
}
}
}
}
public func reloadDate(note: Note) {
DispatchQueue.main.async {
if let i = self.noteList.firstIndex(of: note) {
if let row = self.rowView(atRow: i, makeIfNecessary: false) as? NoteRowView, let cell = row.subviews.first as? NoteCellView {
cell.date.stringValue = note.getDateForLabel()
}
}
}
}
public func saveNavigationHistory(note: Note) {
guard history.last != note.url else {
historyPosition = history.count - 1
return
}
history.append(note.url)
historyPosition = history.count - 1
}
public func enableLockedProject() {
ViewController.shared()?.lockedFolder.isHidden = false
usesAlternatingRowBackgroundColors = false
clean()
}
public func disableLockedProject() {
ViewController.shared()?.lockedFolder.isHidden = true
usesAlternatingRowBackgroundColors = true
}
public func clean() {
noteList.removeAll()
reloadData()
}
}