Repository Public
1// SP Notes iOS2// File: Features/Drawing/DrawingEntryPoints.swift3// Description: The 1.0 entry points of drawings, kept working in 2.0 (`Request.drawing`, `EditorCover.drawing`): a new drawing is4// now a `drawing` block; an existing 1.0 drawing (picture + PencilKit source) is edited as before and can be5// converted into a block when the user asks. Also the block detail context of a block of the open note.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import PencilKit10import SPNotesCore11import SwiftUI1213extension NoteEditorModel {14 /// The context a block detail screen works with, for the block at `range` (nil: a block being created, inserted at the15 /// caret by the first commit).16 func blockDetailContext(range: NSRange?, isEditable: Bool = true, dismiss: @escaping () -> Void) -> BlockDetailContext {17 let anchor = BlockAnchor(range: range)18 return BlockDetailContext(app: app, noteId: noteId, isEditable: isEditable && !isReadOnly, commit: { [weak self] node in19 guard let self else { return }20 if let range = anchor.range {21 if let node { self.controller.updateAtom(at: range, node: node) } else { self.controller.deleteAtom(at: range); anchor.range = nil; anchor.isDeleted = true }22 } else if let node, !anchor.isDeleted {23 self.controller.insertBlockAtom(node)24 // the atom sits two characters before the caret `insertBlockAtom` leaves in the next paragraph25 let location = self.controller.textView.selectedRange.location - 226 if location >= 0 { anchor.range = NSRange(location: location, length: 1) }27 }28 }, insertMarkdownBelow: { [weak self] markdown in29 guard let self, !markdown.isEmpty else { return }30 if let range = anchor.range { self.controller.textView.selectedRange = self.controller.clampSelection(NSRange(location: range.location, length: 0)) }31 self.insertMarkdown(markdown, replacingSelection: false)32 }, open: { [weak self] route in self?.app.pendingRoute = route }, dismiss: dismiss)33 }3435 /// Replaces the 1.0 picture at `range` by the `drawing` block made from its drawing. The picture and its source stay attached.36 func convertLegacyDrawing(_ drawing: PKDrawing, at range: NSRange, insertions: [String] = []) async {37 guard let atom = controller.storage.attribute(.attachment, at: range.location, effectiveRange: nil) as? AtomAttachment else { return }38 do {39 let saved = try await DrawingBlockIO.convertLegacy(drawing, imageNode: atom.node, app: app, noteId: noteId)40 let context = blockDetailContext(range: range, dismiss: {})41 context.commit(saved.block.node())42 insertions.forEach(context.insertMarkdownBelow)43 } catch { app.banner = error.localizedDescription }44 }4546 /// Inserts a drawing as a new `drawing` block at the caret.47 func insertDrawingBlock(_ drawing: PKDrawing, insertions: [String] = []) async {48 do {49 let saved = try await DrawingBlockIO.save(drawing, background: DrawingPaper.white, into: DrawingBlock(background: DrawingPaper.white), app: app, noteId: noteId)50 let context = blockDetailContext(range: nil, dismiss: {})51 context.commit(saved.block.node())52 insertions.forEach(context.insertMarkdownBelow)53 } catch { app.banner = error.localizedDescription }54 }55}5657/// Where a block is in the text while its detail screen is open.58@MainActor59private final class BlockAnchor {60 var range: NSRange?61 var isDeleted = false62 init(range: NSRange?) { self.range = range }63}6465extension NoteEditorContent {66 /// What `EditorCover.drawing` shows. `data` nil: a new drawing (a `drawing` block). `data` + `range`: the 1.0 drawing at `range`.67 @ViewBuilder func drawingCover(data: Data?, range: NSRange?) -> some View {68 if let data {69 LegacyDrawingScreen(drawingData: data, onKeep: { result in70 self.cover = nil71 guard !result.isEmpty else { return }72 busy {73 await model.insertDrawing(png: result.png, source: result.drawingData, replacing: range)74 if let text = result.recognizedText, let range { insertBelow(range, markdown: text) } else if let text = result.recognizedText { model.insertMarkdown(text, replacingSelection: false) }75 }76 }, onConvert: { output in77 self.cover = nil78 busy {79 if let range { await model.convertLegacyDrawing(output.drawing, at: range, insertions: output.insertions) }80 else { await model.insertDrawingBlock(output.drawing, insertions: output.insertions) }81 }82 }, onCancel: { self.cover = nil })83 } else {84 DrawingBlockScreen(node: DrawingBlock(background: DrawingPaper.white).node(), context: model.blockDetailContext(range: range, dismiss: { self.cover = nil }))85 }86 }8788 private func insertBelow(_ range: NSRange, markdown: String) {89 controller.textView.selectedRange = controller.clampSelection(NSRange(location: range.location, length: 0))90 model.insertMarkdown(markdown, replacingSelection: false)91 }92}93