Skip to content

Commit

Permalink
- Fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
glushchenko committed Dec 23, 2023
1 parent c224ce9 commit 0065276
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 69 deletions.
4 changes: 2 additions & 2 deletions FSNotes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -4559,7 +4559,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 606;
CURRENT_PROJECT_VERSION = 607;
DEVELOPMENT_TEAM = 866P6MTE92;
EMBED_ASSET_PACKS_IN_PRODUCT_BUNDLE = YES;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down Expand Up @@ -4599,7 +4599,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 606;
CURRENT_PROJECT_VERSION = 607;
DEPLOYMENT_LOCATION = NO;
DEVELOPMENT_TEAM = 866P6MTE92;
EMBED_ASSET_PACKS_IN_PRODUCT_BUNDLE = YES;
Expand Down
76 changes: 38 additions & 38 deletions FSNotes/Business/Note.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public class Note: NSObject {
init(meta: NoteMeta, project: Project) {
isLoadedFromCache = true

if meta.title.count > 0 {
if meta.title.count > 0 || meta.imageUrl != nil {
isParsed = true
}

Expand All @@ -125,6 +125,10 @@ public class Note: NSObject {

parseURL(loadProject: false)
}

public func isValidForCaching() -> Bool {
return isLoaded || title.count > 0 || isEncrypted() || imageUrl != nil
}

func getMeta() -> NoteMeta {
let date = creationDate ?? Date()
Expand Down Expand Up @@ -1202,46 +1206,42 @@ public class Note: NSObject {

var tags = [String]()

if var tempContent = content.copy() as? NSMutableAttributedString {
do {
let range = NSRange(location: 0, length: tempContent.length)
let re = try NSRegularExpression(pattern: FSParser.tagsPattern, options: options)

re.enumerateMatches(
in: tempContent.string,
options: matchingOptions,
range: range,
using: { (result, flags, stop) -> Void in

guard var range = result?.range(at: 1) else { return }
let cleanTag = tempContent.mutableString.substring(with: range)

range = NSRange(location: range.location - 1, length: range.length + 1)
do {
let range = NSRange(location: 0, length: content.length)
let re = try NSRegularExpression(pattern: FSParser.tagsPattern, options: options)

re.enumerateMatches(
in: content.string,
options: matchingOptions,
range: range,
using: { (result, flags, stop) -> Void in

guard var range = result?.range(at: 1) else { return }
let cleanTag = content.mutableString.substring(with: range)

range = NSRange(location: range.location - 1, length: range.length + 1)

let codeBlock = FSParser.getFencedCodeBlockRange(paragraphRange: range, string: content)
let spanBlock = FSParser.getSpanCodeBlockRange(content: content, range: range)

if codeBlock == nil && spanBlock == nil && isValid(tag: cleanTag) {

let codeBlock = FSParser.getFencedCodeBlockRange(paragraphRange: range, string: content)
let spanBlock = FSParser.getSpanCodeBlockRange(content: content, range: range)
let parRange = content.mutableString.paragraphRange(for: range)
let par = content.mutableString.substring(with: parRange)
if par.starts(with: " ") || par.starts(with: "\t") {
return
}

if codeBlock == nil && spanBlock == nil && isValid(tag: cleanTag) {

let parRange = tempContent.mutableString.paragraphRange(for: range)
let par = tempContent.mutableString.substring(with: parRange)
if par.starts(with: " ") || par.starts(with: "\t") {
return
}

if cleanTag.last == "/" {
tags.append(String(cleanTag.dropLast()))
} else {
tags.append(cleanTag)
}
if cleanTag.last == "/" {
tags.append(String(cleanTag.dropLast()))
} else {
tags.append(cleanTag)
}
}
)
} catch {
print("Tags parsing: \(error)")
}

tempContent = NSMutableAttributedString()
}
)
} catch {
print("Tags parsing: \(error)")
}

if tags.contains("notags") {
Expand Down Expand Up @@ -1377,7 +1377,7 @@ public class Note: NSObject {
public func loadPreviewInfo(text: String? = nil) {
let content = text ?? self.content.string

if title.count > 0 && self.isParsed {
if (title.count > 0 || imageUrl != nil) && self.isParsed {
return
}

Expand Down
6 changes: 5 additions & 1 deletion FSNotes/Business/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public class Project: Equatable {

notes = Array(deduplicatedNotes)

let meta = notes.filter({ $0.isLoaded && $0.title.count > 0 }).map({ $0.getMeta() })
let meta = notes.filter({ $0.isValidForCaching() }).map({ $0.getMeta() })
let jsonEncoder = JSONEncoder()

do {
Expand Down Expand Up @@ -877,6 +877,10 @@ public class Project: Equatable {


let results = checkFSAndMemoryDiff()

if results.1.count > 0 {
print(results)
}

print("Cache diff found: removed - \(results.0.count), added - \(results.1.count), modified - \(results.2.count).")

Expand Down
14 changes: 13 additions & 1 deletion FSNotes/Business/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,19 @@ class Storage {
}

func sortNotes(noteList: [Note], filter: String? = nil, project: Project? = nil, operation: BlockOperation? = nil) -> [Note] {

var noteList = noteList

// Pre sort by creation and modified date, title
if let filter = filter, filter.count > 0 {
noteList = noteList.sorted(by: {
if let operation = operation, operation.isCancelled {
return false
}

return sortQuery(note: $0, next: $1, project: project)
})
}

return noteList.sorted(by: {
if let operation = operation, operation.isCancelled {
return false
Expand Down
2 changes: 1 addition & 1 deletion FSNotes/EditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ class EditorViewController: NSViewController, NSTextViewDelegate, WebFrameLoadDe
noteDupe.save()

Storage.shared().add(noteDupe)
ViewController.shared()?.notesTableView.insertNew(note: noteDupe)
ViewController.shared()?.notesTableView.insertRows(notes: [noteDupe])
}
}

Expand Down
5 changes: 4 additions & 1 deletion FSNotes/Helpers/FileSystemEventManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class FileSystemEventManager {
} else {
if !note.isTrash() {
OperationQueue.main.addOperation {
self.delegate.notesTableView.insertNew(note: note)
self.delegate.notesTableView.insertRows(notes: [note])
}
}
}
Expand Down Expand Up @@ -278,6 +278,7 @@ class FileSystemEventManager {
// reload view

self.delegate.notesTableView.reloadRow(note: note)
self.delegate.reSort(note: note)

let editors = AppDelegate.getEditTextViews()
for editor in editors {
Expand All @@ -291,7 +292,9 @@ class FileSystemEventManager {
if modificationDate != note.modifiedLocalAt || creationDate != note.creationDate {
note.modifiedLocalAt = modificationDate
note.creationDate = creationDate

delegate.notesTableView.reloadDate(note: note)
delegate.reSort(note: note)

// Reload images if note moved (cache invalidated)
note.loadPreviewInfo()
Expand Down
2 changes: 1 addition & 1 deletion FSNotes/View/MPreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ class MPreviewView: WKWebView, WKUIDelegate, WKNavigationDelegate {
font-weight: bold;
}
body {font: \(fontSize)px '\(familyName)', '-apple-system'; margin: 0 \(width + 5)px;}
body {font: \(fontSize)px '\(familyName)', '-apple-system'; margin: 0 \(width + 5)px; -webkit-text-size-adjust: none;}
code, pre {font: \(codeFontSize)px '\(codeFamilyName)', Courier, monospace, 'Liberation Mono', Menlo; line-height: \(codeLineHeight + 3)px; -webkit-text-size-adjust: none; }
img {display: block; margin: 0 auto; max-width: \(maxImageWidth)px; }
img:not(footer img) { max-width: \(maxImageWidth)px; }
Expand Down
43 changes: 24 additions & 19 deletions FSNotes/View/NotesTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,31 @@ class NotesTableView: NSTableView, NSTableViewDataSource,
}

public func insertRows(notes: [Note]) {
guard let vc = self.window?.contentViewController as? ViewController else { return }
var insert = [Note]()

for note in notes {
insertNew(note: note)
if noteList.first(where: { $0.isEqualURL(url: note.url) }) == nil,
vc.isFit(note: note, shouldLoadMain: true) {
insert.append(note)
noteList.append(contentsOf: insert)
}
}

let projects = vc.sidebarOutlineView.getSidebarProjects()
self.noteList = vc.storage.sortNotes(noteList: self.noteList, filter: vc.search.stringValue, project: projects?.first)

var indexSet = IndexSet()
for note in insert {
if let noteIndex = self.noteList.firstIndex(of: note) {
indexSet.insert(noteIndex)
}
}

self.insertRows(at: indexSet, withAnimation: .effectFade)

for note in insert {
vc.sidebarOutlineView.insertTags(note: note)
}
}

Expand Down Expand Up @@ -590,24 +613,6 @@ class NotesTableView: NSTableView, NSTableViewDataSource,
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()
Expand Down
5 changes: 0 additions & 5 deletions FSNotes/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1347,11 +1347,6 @@ class ViewController: EditorViewController,
}
}

// Pre sort by creation and modified date, title
if filter.count > 0 {
notes = self.storage.sortNotes(noteList: notes, project: projects?.first, operation: operation)
}

let orderedNotesList = self.storage.sortNotes(noteList: notes, filter: filter, project: projects?.first, operation: operation)

// Check diff
Expand Down

0 comments on commit 0065276

Please sign in to comment.