SPB Git

Repository Public

QuickNoteActions.swift 3.8 KB 63 lines swift

1// SP Notes iOS2// File: App/Intents/QuickNoteActions.swift3// Description: Note operations used by the system entry points (App Intents, deep links): quick creation, appending text,4//              searching. They go through the same code path as the screens (`AppModel.createNote` / `AppModel.save`), so the5//              change is written locally, scheduled for synchronisation and reflected in the widgets.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import SPNotesCore1112extension AppModel {13    /// Title and body of a quick note. Without a title the first line becomes the title (as in the system Notes app); a first14    /// line too long to be a title is shortened and the whole text stays in the body.15    static func quickNoteParts(title: String?, text: String) -> (title: String, body: String) {16        let body = text.trimmingCharacters(in: .whitespacesAndNewlines)17        let given = (title ?? "").trimmingCharacters(in: .whitespacesAndNewlines)18        if !given.isEmpty { return (given, body) }19        guard let firstLine = body.split(separator: "\n", maxSplits: 1, omittingEmptySubsequences: true).first else { return ("", "") }20        let first = firstLine.trimmingCharacters(in: .whitespaces)21        if first.count > 80 { return (String(first.prefix(79)).trimmingCharacters(in: .whitespaces) + "…", body) }22        let rest = body.dropFirst(firstLine.count).trimmingCharacters(in: .whitespacesAndNewlines)23        return (first, rest)24    }2526    /// Creates a note from a title and a text (Markdown is understood). `kind == .tasks` starts it with a task list.27    @discardableResult28    func quickCreate(title: String?, text: String, kind: DeepLink.NewKind = .note) async throws -> String {29        let parts = Self.quickNoteParts(title: title, text: text)30        var markdown = parts.body31        if kind == .tasks {32            let lines = markdown.split(separator: "\n").map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }33            markdown = lines.isEmpty ? "- [ ] " : lines.map { $0.hasPrefix("- [") ? $0 : "- [ ] " + $0 }.joined(separator: "\n")34        }35        return try await createNote(title: parts.title, document: NoteDocument(markdown: markdown))36    }3738    /// Appends `text` at the end of a note, one paragraph per line. Returns false when there was nothing to add.39    @discardableResult40    func appendText(_ text: String, toNote noteId: String) async throws -> Bool {41        let paragraphs = text.split(whereSeparator: \.isNewline).map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }42        guard !paragraphs.isEmpty else { return false }43        guard let page = try await store.page(id: noteId), page.deletedAt == nil, !page.isFolder else { throw AppError.noteNotFound }44        var document = try NoteDocument(json: page.content)45        // A note that only holds its initial empty paragraph is filled rather than extended.46        if document.lines.count == 1, document.lines[0].path.isEmpty, document.lines[0].type == NodeType.paragraph, document.plainText().isEmpty {47            document.lines.removeAll()48        }49        document.insert(lines: paragraphs.map { Line.paragraph(text: $0) }, at: document.lines.count)50        return try await save(noteId: noteId, title: page.title, document: document)51    }5253    /// Notes matching a query (same rules as the search screen), as system entities.54    func searchNotes(_ query: String, limit: Int = 20) async throws -> [NoteEntity] {55        try await store.search(query: query, limit: limit * 2).map(\.page).filter(WidgetDataProvider.isNote).prefix(limit).map { NoteEntity(WidgetNote($0)) }56    }5758    func noteEntity(id: String) async throws -> NoteEntity {59        guard let summary = try await store.summary(id: id) else { throw AppError.noteNotFound }60        return NoteEntity(WidgetNote(summary))61    }62}63
Files main
Menu

Theme