Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / apple / Notes / NoteBodyView.swift

NoteBodyView.swift59 lines · 3.1 KiBRaw
1// EverySync Notes — native NoteBody rendering.
2//
3// Renders the XCFramework's `NoteBody` blocks natively (text/markdown,
4// blob-channel images by `fileName`, PencilKit ink). This file does NO
5// parsing/serialization/hashing/merging: it takes an already-parsed `NoteBody`
6// from the XCFramework (`parseNoteBody`) and binds edits back through
7// `serializeNoteBody`. Swift never defines its own block schema.
8//
9// Wire shape (mirrored verbatim — do not extend in Swift):
10// {"blocks":[
11// {"type":"text","id":"…","markdown":"…"},
12// {"type":"image","id":"…","fileName":"<sha256>.jpg","widthFraction":1.0},
13// {"type":"ink","id":"…","strokes":[],"heightDp":300}]}
14// `InkStroke`: `brushFamily` (default `"pressurePen"`), `brushSize`,
15// `brushColor` (argb int), `brushEpsilon` (default `0.1`), `points`.
16// `InkPoint`: `x, y, timeMillis, pressure, tiltRadians, orientationRadians`.
17//
18// MAC-CHECK (needs xcodebuild on a Mac):
19// - `EverysyncCore` Kotlin class names below are the K/N export shape
20// (`NoteBody` / `NoteBlock.Text` / `.Image` / `.Ink`). The Mac must confirm
21// the exact names in the generated `EverysyncCore.h` and adjust call sites.
22
23import SwiftUI
24import EverysyncCore
25
26/// Renders one note's blocks. The view model holds the XCFramework `NoteBody`
27/// object; every edit (text change, image resize, new strokes) writes through
28/// `serializeNoteBody` into the envelope IPC. Tombstoned notes (`deleted`)
29/// render a "deleted" placeholder — Swift never raw-deletes.
30struct NoteBodyView: View {
31 // TODO(Mac): bind to the XCFramework NoteBody object.
32 // Cases (from `NoteBlock` — exhaustive over Text/Image/Ink):
33 // - Text: native TextEditor bound to `markdown`. Markdown rendering is
34 // display-only; the stored string stays verbatim (merge hashes it).
35 // - Image: `fileName` resolved via BlobStore (see
36 // ../Shared/BlobStore.swift); prefetch the vault's set via
37 // `NoteKind.imageNames(blocksJson)`. `widthFraction` scales the image
38 // to that fraction of the editor width. Missing blob = placeholder
39 // (the hub is still fetching from `notes-blobs:<vaultId>`).
40 // - Ink: PencilKit canvas (`PKCanvasView` wrapped in UIViewRepresentable)
41 // with `heightDp` height (default 300); strokes mapped via
42 // `PencilKitMapping.swift`. Canvas edits append/replace strokes in the
43 // XCFramework object — serialization stays in `serializeNoteBody`.
44 var body: some View {
45 Text("Note body renders here (Mac wires the XCFramework NoteBody)")
46 }
47}
48
49/// Converts a `UIColor` to the shared-model argb int (`InkStroke.brushColor`).
50/// Display helper only — the mapping itself lives in `PencilKitMapping.swift`.
51enum InkColorHelp {
52 static func argbInt(red: Double, green: Double, blue: Double, alpha: Double = 1.0) -> Int32 {
53 let a = Int32((alpha * 255).rounded()) & 0xFF
54 let r = Int32((red * 255).rounded()) & 0xFF
55 let g = Int32((green * 255).rounded()) & 0xFF
56 let b = Int32((blue * 255).rounded()) & 0xFF
57 return (a << 24) | (r << 16) | (g << 8) | b
58 }
59}