oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1import SwiftUI
2
3/// The document's text view, embedded once per document (spec 2026-11-22
4/// §7 Design 9). Its inputs are identity-level only: the surface, Pages
5/// and zoom. The text itself never passes through SwiftUI: publications go
6/// model → `DocumentSurface` → storage by direct call, and the text view's
7/// callbacks go to the surface.
8#if os(macOS)
9import AppKit
10struct NativeDocumentTextView: NSViewRepresentable {
11 let surface: DocumentSurface
12 var paged: Bool
13 var zoom: Double = 1
14 /// The side the floating inspector covers, nil while it is hidden: the
15 /// desk centres the page clear of it (spec §1, page centring).
16 var inspector: DocsDeskLayout.Side? = nil
17 func makeNSView(context: Context) -> NSScrollView {
18 let scroll = NSScrollView()
19 scroll.hasVerticalScroller = true
20 scroll.hasHorizontalScroller = true
21 scroll.minMagnification = 0.5
22 scroll.maxMagnification = 3
23 scroll.allowsMagnification = false
24 scroll.magnification = 4.0 / 3.0
25 let text = DocsTextView(frame: .zero)
26 text.isVerticallyResizable = false
27 // The page is a fixed paper band on the desk, which centres it (S2a).
28 scroll.documentView = DocsDeskView(page: text)
29 text.isRichText = true
30 // Before the first render, and in an empty document, the caret and
31 // anything typed are in the default face, never the system's.
32 text.font = DocsFonts.defaultFont()
33 text.typingAttributes[.font] = DocsFonts.defaultFont()
34 text.isAutomaticLinkDetectionEnabled = false
35 text.isAutomaticTextReplacementEnabled = false
36 text.allowsUndo = false
37 text.setAccessibilityIdentifier("docs.editor")
38 text.setAccessibilityLabel(String(localized: "native.editor", bundle: DocsResources.bundle))
39 text.paged = paged
40 applyZoom(scroll, text)
41 surface.attach(text)
42 return scroll
43 }
44 func updateNSView(_ scroll: NSScrollView, context: Context) {
45 #if DOCS_PROBES
46 let probeStart = DocsProbes.now()
47 defer { DocsProbes.viewUpdates += 1; DocsProbes.viewUpdateMs += DocsProbes.ms(since: probeStart) }
48 #endif
49 guard let desk = scroll.documentView as? DocsDeskView, let text = desk.page as? DocsTextView else { return }
50 desk.inspector = inspector
51 if surface.textView !== text { surface.attach(text) }
52 applyZoom(scroll, text)
53 text.paged = paged
54 }
55 private func applyZoom(_ scroll: NSScrollView, _ text: NSTextView) {
56 let magnification = zoom * 4.0 / 3.0
57 if abs(scroll.magnification - magnification) > 0.001 {
58 scroll.setMagnification(magnification, centeredAt: scroll.contentView.bounds.origin)
59 text.needsLayout = true
60 }
61 }
62}
63#else
64import UIKit
65struct NativeDocumentTextView: UIViewRepresentable {
66 let surface: DocumentSurface
67 var paged: Bool
68 var zoom: Double = 1
69 /// The iPhone's large title and keyboard bar (S3); nil on iPad.
70 var phone: DocsPhoneEditorChrome? = nil
71 func makeUIView(context: Context) -> UITextView {
72 let text = DocsTextView()
73 if let phone {
74 text.header = DocsPhoneHeaderHost(model: phone.model, team: phone.team, state: phone.header)
75 text.keyboardBar = phone.makeKeyboardBar(hide: { [weak text] in _ = text?.resignFirstResponder() })
76 text.keyboardDismissMode = .interactive
77 }
78 text.adjustsFontForContentSizeCategory = true
79 // Before the first render, and in an empty document, the caret and
80 // anything typed are in the default face, never the system's.
81 text.font = DocsFonts.defaultFont()
82 text.typingAttributes[.font] = DocsFonts.defaultFont()
83 text.accessibilityIdentifier = "docs.editor"
84 text.accessibilityLabel = String(localized: "native.editor", bundle: DocsResources.bundle)
85 text.paged = paged
86 surface.attach(text)
87 return text
88 }
89 func updateUIView(_ view: UITextView, context: Context) {
90 #if DOCS_PROBES
91 let probeStart = DocsProbes.now()
92 defer { DocsProbes.viewUpdates += 1; DocsProbes.viewUpdateMs += DocsProbes.ms(since: probeStart) }
93 #endif
94 guard let text = view as? DocsTextView else { return }
95 if surface.textView !== text { surface.attach(text) }
96 text.paged = paged
97 }
98}
99#endif
100
101/// The view that holds the text view: it reads nothing the model
102/// publishes (sync status, selection, metadata, typing, the projection),
103/// only the surface and the identity-level settings, so it is evaluated
104/// when the document opens and when Pages or zoom change, and never per
105/// keystroke, per peer change or per sync-status change (spec §7 Design 9;
106/// DocsProbes.hostBodies counts it).
107struct DocumentEditorHost: View, Equatable {
108 let surface: DocumentSurface
109 let paged: Bool
110 var zoom: Double = 1
111 /// Mac: the side the floating Format inspector covers (S2a), nil while
112 /// it is hidden; identity-level, like Pages and zoom.
113 var inspector: DocsDeskLayout.Side? = nil
114 #if os(iOS)
115 /// iPhone: the large title and keyboard bar (S3), by identity.
116 var phone: DocsPhoneEditorChrome? = nil
117 #endif
118 nonisolated static func == (a: Self, b: Self) -> Bool {
119 #if os(iOS)
120 guard a.phone === b.phone else { return false }
121 #endif
122 return a.surface === b.surface && a.paged == b.paged && a.zoom == b.zoom && a.inspector == b.inspector
123 }
124 var body: some View {
125 #if DOCS_PROBES
126 let _ = DocsProbes.hostBodies += 1
127 #endif
128 #if os(macOS)
129 NativeDocumentTextView(surface: surface, paged: paged, zoom: zoom, inspector: inspector)
130 #else
131 NativeDocumentTextView(surface: surface, paged: paged, zoom: zoom, phone: phone)
132 #endif
133 }
134}
135