Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Editor/AtomAttachment.swift3// Description: Text attachments that hold ProseMirror nodes edited as a unit (tables, images, rules, equations, files, mentions, unknown nodes) and their native drawing.4import AppKit5import SPNotesDocument6import SPNotesDesign78public extension NSAttributedString.Key {9 /// On a U+2028 character: the inline node it stands for (`hardBreak`). Without it, U+2028 is a literal line feed of a text node.10 static let spInlineNode = NSAttributedString.Key("sp.inlineNode")11}1213public final class NodeBox: NSObject, @unchecked Sendable {14 public let node: PMNode15 public init(_ node: PMNode) { self.node = node }16 public override func isEqual(_ object: Any?) -> Bool { (object as? NodeBox)?.node == node }17 public override var hash: Int { node.type.hashValue }18}1920/// Resources the drawing code needs from the application (the editor module knows nothing about files or the network).21@MainActor public protocol AtomResources: AnyObject {22 /// Image for a document source such as `/api/files/<id>`; nil when it is not available locally (yet).23 func image(for src: String) -> NSImage?24 /// Local file of an attachment (PDF, ink strokes, audio); nil when it is not on this Mac (yet).25 func attachmentURL(id: String) -> URL?26 /// The block a `blockRef` points to, read from the local store; nil while it is being looked up.27 func blockReference(pageId: String, blockId: String?) -> BlockReference?28 /// Title of a page of the local store (canvas links); nil when the page is not known.29 func pageTitle(id: String) -> String?30 /// Number of distinct references cited by the document being edited (bibliography card).31 func citedReferenceCount() -> Int?32 /// « Auteur Année » of a research reference when the node does not carry its cached label.33 func citationLabel(refId: String) -> String?34}3536public extension AtomResources {37 func attachmentURL(id: String) -> URL? { nil }38 func blockReference(pageId: String, blockId: String?) -> BlockReference? { nil }39 func pageTitle(id: String) -> String? { nil }40 func citedReferenceCount() -> Int? { nil }41 func citationLabel(refId: String) -> String? { nil }42}4344/// Set by a rendering that used a placeholder because a resource was not there yet: such an image is not cached.45final class PendingFlag { var value = false }4647/// One attachment character = one node. The node is immutable; editing an atom replaces the attachment (undoable).48public final class AtomAttachment: NSTextAttachment, @unchecked Sendable {49 public let node: PMNode50 public let isBlock: Bool51 public nonisolated(unsafe) var theme: EditorTheme52 public nonisolated(unsafe) weak var resources: AtomResources?53 private nonisolated(unsafe) var cache: (width: CGFloat, dark: Bool, image: NSImage)?5455 public init(node: PMNode, isBlock: Bool, theme: EditorTheme, resources: AtomResources?) {56 self.node = node; self.isBlock = isBlock; self.theme = theme; self.resources = resources57 super.init(data: nil, ofType: nil)58 }59 required init?(coder: NSCoder) { fatalError("AtomAttachment is never archived") }6061 public nonisolated func invalidate() { cache = nil }6263 /// Rendering for a width and an appearance (the text view's own: a window can be dark while the system is light).64 @MainActor private func rendered(lineWidth: CGFloat, in container: NSTextContainer?) -> NSImage {65 let appearance = container?.textView?.effectiveAppearance ?? NSAppearance.currentDrawing()66 let dark = appearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua67 let w = max(40, lineWidth.rounded(.down))68 if let c = cache, c.width == w, c.dark == dark { return c.image }69 var img = NSImage()70 let renderer = AtomRenderer(theme: theme, resources: resources)71 appearance.performAsCurrentDrawingAppearance { img = renderer.image(for: node, isBlock: isBlock, width: w) }72 // a resource that is not on disk yet (picture, PDF, referenced block) must be looked up again once it arrives:73 // a rendering made with a placeholder is not cached74 if !renderer.pending.value { cache = (w, dark, img) }75 return img76 }7778 public override func attachmentBounds(for attributes: [NSAttributedString.Key: Any], location: NSTextLocation, textContainer: NSTextContainer?, proposedLineFragment: CGRect, position: CGPoint) -> CGRect {79 let available = isBlock ? proposedLineFragment.width - position.x : proposedLineFragment.width80 let size = (attributes[.font] as? NSFont)?.pointSize ?? theme.fontSize81 nonisolated(unsafe) let me = self82 nonisolated(unsafe) let tc = textContainer83 return MainActor.assumeIsolated {84 let img = me.rendered(lineWidth: available, in: tc)85 // TextKit 2 draws the attachment's `image` property: it is kept equal to the rendering for the current width86 if me.image !== img { me.image = img }87 if me.isBlock { return CGRect(origin: .zero, size: img.size) }88 let descent = AtomRenderer.inlineDescent(node: me.node, theme: me.theme, fontSize: size)89 return CGRect(x: 0, y: -descent, width: img.size.width, height: img.size.height)90 }91 }92 public override func image(for bounds: CGRect, attributes: [NSAttributedString.Key: Any], location: NSTextLocation, textContainer: NSTextContainer?) -> NSImage? {93 let w = isBlock ? bounds.width : (textContainer?.size.width ?? 600)94 nonisolated(unsafe) let me = self95 nonisolated(unsafe) let tc = textContainer96 return MainActor.assumeIsolated { me.rendered(lineWidth: w, in: tc) }97 }98 // TextKit 1 fallbacks (printing and RTF export lay the text out with the classic engine)99 public override func attachmentBounds(for textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect {100 let w = isBlock ? lineFrag.width - position.x : lineFrag.width101 nonisolated(unsafe) let me = self102 nonisolated(unsafe) let tc = textContainer103 return MainActor.assumeIsolated { let img = me.rendered(lineWidth: w, in: tc); return CGRect(x: 0, y: me.isBlock ? 0 : -3, width: img.size.width, height: img.size.height) }104 }105 public override func image(forBounds imageBounds: CGRect, textContainer: NSTextContainer?, characterIndex charIndex: Int) -> NSImage? {106 let w = isBlock ? imageBounds.width : (textContainer?.size.width ?? 600)107 nonisolated(unsafe) let me = self108 nonisolated(unsafe) let tc = textContainer109 return MainActor.assumeIsolated { me.rendered(lineWidth: w, in: tc) }110 }111}112113/// Draws nodes with AppKit. Every image is produced by a drawing handler, so dynamic colours follow the appearance.114@MainActor public struct AtomRenderer {115 public var theme: EditorTheme116 public weak var resources: AtomResources?117 let pending = PendingFlag()118 public init(theme: EditorTheme, resources: AtomResources?) { self.theme = theme; self.resources = resources }119120 static func inlineDescent(node: PMNode, theme: EditorTheme, fontSize: CGFloat) -> CGFloat {121 if node.type == "inlineMath" || node.type == "math", let m = MathRenderer.render(node.attrs?["latex"]?.string ?? "", fontSize: fontSize, display: false) { return m.descent }122 return 3123 }124125 public func image(for node: PMNode, isBlock: Bool, width: CGFloat) -> NSImage {126 switch node.type {127 case "horizontalRule": return rule(width: width)128 case "image": return picture(node, width: width)129 case "blockMath": return blockMath(node, width: width)130 case "table": return table(node, width: width)131 case "fileBlock": return card(symbol: "paperclip", title: node.attrs?["name"]?.string ?? "Fichier", subtitle: Self.byteCount(node.attrs?["size"]?.int ?? 0), width: min(width, 420))132 case "youtube": return card(symbol: "play.rectangle", title: "Vidéo", subtitle: node.attrs?["src"]?.string ?? "", width: min(width, 420))133 case "mention": return pill("@" + (node.attrs?["label"]?.string ?? node.attrs?["id"]?.string ?? "note"), color: EditorTheme.accent)134 case "inlineMath", "math": return inlineMath(node)135 case "footnoteRef": return pill("[" + (node.attrs?["id"]?.string ?? "?") + "]", color: EditorTheme.mutedText, small: true)136 case "citation": return citationChip(node)137 default:138 // the 3.0 block atoms, and any node this build does not know (kept verbatim, shown as a neutral card)139 if isBlock { return nextCard(node, width: width) }140 return pill("⟨\(node.type)⟩", color: EditorTheme.mutedText)141 }142 }143144 /// Renders into a 2× bitmap with the current appearance (the attachment is drawn again when the appearance changes).145 func make(_ size: CGSize, _ draw: @escaping (CGRect) -> Void) -> NSImage {146 let size = CGSize(width: max(1, ceil(size.width)), height: max(1, ceil(size.height)))147 let scale: CGFloat = max(2, NSScreen.main?.backingScaleFactor ?? 2)148 guard let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(size.width * scale), pixelsHigh: Int(size.height * scale), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0) else { return NSImage(size: size) }149 rep.size = size150 let render = {151 guard let ctx = NSGraphicsContext(bitmapImageRep: rep) else { return }152 NSGraphicsContext.saveGraphicsState(); NSGraphicsContext.current = ctx153 // flipped coordinates: y grows downwards, like the text view154 let t = NSAffineTransform(); t.translateX(by: 0, yBy: size.height); t.scaleX(by: 1, yBy: -1); t.concat()155 let flipped = NSGraphicsContext(cgContext: ctx.cgContext, flipped: true); NSGraphicsContext.current = flipped156 draw(CGRect(origin: .zero, size: size))157 NSGraphicsContext.restoreGraphicsState()158 }159 render() // the caller has set the drawing appearance160 let img = NSImage(size: size); img.addRepresentation(rep)161 return img162 }163164 private func rule(width: CGFloat) -> NSImage {165 make(CGSize(width: width, height: 17)) { r in166 EditorTheme.strongLine.setFill(); CGRect(x: 0, y: 8, width: r.width, height: 1).fill()167 }168 }169170 private func picture(_ node: PMNode, width: CGFloat) -> NSImage {171 let src = node.attrs?["src"]?.string ?? ""172 guard let image = resources?.image(for: src), image.size.width > 0 else {173 pending.value = true174 let alt = node.attrs?["alt"]?.string ?? ""175 return card(symbol: "photo", title: alt.isEmpty ? "Image" : alt, subtitle: src.hasPrefix("/api/files/") ? "Pas encore sur ce Mac — se télécharge à la prochaine connexion" : src, width: min(width, 420))176 }177 let maxW = min(width, EditorMetrics.imageMaxWidth); let scale = min(1, maxW / image.size.width)178 let size = CGSize(width: image.size.width * scale, height: image.size.height * scale)179 return make(size) { r in180 let path = NSBezierPath(roundedRect: r, xRadius: EditorMetrics.imageRadius, yRadius: EditorMetrics.imageRadius); NSGraphicsContext.saveGraphicsState(); path.addClip()181 image.draw(in: r, from: .zero, operation: .sourceOver, fraction: 1, respectFlipped: true, hints: [.interpolation: NSImageInterpolation.high.rawValue])182 NSGraphicsContext.restoreGraphicsState()183 }184 }185186 private func blockMath(_ node: PMNode, width: CGFloat) -> NSImage {187 let tex = node.attrs?["latex"]?.string ?? ""188 if let m = MathRenderer.render(tex, fontSize: theme.fontSize * 1.15, display: true) {189 let h = m.size.height + 16190 return make(CGSize(width: width, height: h)) { r in191 let w = min(m.size.width, r.width); let s = w / m.size.width192 let dest = CGRect(x: (r.width - w) / 2, y: 8 + (m.size.height - m.size.height * s) / 2, width: w, height: m.size.height * s)193 Self.drawTemplate(m.image, in: dest, color: EditorTheme.text)194 }195 }196 let text = NSAttributedString(string: tex.isEmpty ? "Équation vide — double-clic pour la saisir" : tex, attributes: [.font: theme.codeFont(), .foregroundColor: EditorTheme.mutedText])197 let b = text.boundingRect(with: CGSize(width: width - 16, height: 4000), options: [.usesLineFragmentOrigin])198 return make(CGSize(width: width, height: ceil(b.height) + 16)) { _ in text.draw(with: CGRect(x: 8, y: 8, width: width - 16, height: ceil(b.height)), options: [.usesLineFragmentOrigin]) }199 }200201 private func inlineMath(_ node: PMNode) -> NSImage {202 let tex = node.attrs?["latex"]?.string ?? ""203 if let m = MathRenderer.render(tex, fontSize: theme.fontSize, display: false) {204 return make(m.size) { r in Self.drawTemplate(m.image, in: r, color: EditorTheme.text) }205 }206 return pill("$" + tex + "$", color: EditorTheme.mutedText)207 }208209 nonisolated static func drawTemplate(_ image: NSImage, in rect: CGRect, color: NSColor) {210 guard let ctx = NSGraphicsContext.current?.cgContext else { return }211 ctx.saveGState(); ctx.beginTransparencyLayer(in: rect, auxiliaryInfo: nil)212 image.draw(in: rect, from: .zero, operation: .sourceOver, fraction: 1, respectFlipped: true, hints: nil)213 ctx.setBlendMode(.sourceIn); ctx.setFillColor(color.cgColor); ctx.fill(rect)214 ctx.endTransparencyLayer(); ctx.restoreGState()215 }216217 private func pill(_ text: String, color: NSColor, small: Bool = false) -> NSImage {218 let font = theme.baseFont(size: small ? theme.fontSize * 0.78 : theme.fontSize)219 let s = NSAttributedString(string: text, attributes: [.font: font, .foregroundColor: color])220 let size = s.size()221 let pad: CGFloat = small ? 1 : 4222 return make(CGSize(width: size.width + pad * 2, height: size.height)) { r in223 if !small { (color == EditorTheme.accent ? EditorTone.accent.fill : EditorTone.neutral.fill).setFill(); NSBezierPath(roundedRect: r.insetBy(dx: 0, dy: 1), xRadius: EditorMetrics.pillRadius, yRadius: EditorMetrics.pillRadius).fill() }224 s.draw(at: CGPoint(x: pad, y: 0))225 }226 }227228 private func card(symbol: String, title: String, subtitle: String, width: CGFloat) -> NSImage {229 let t = NSAttributedString(string: title, attributes: [.font: EditorTheme.uiFont(14, weight: .medium), .foregroundColor: EditorTheme.text])230 let para = NSMutableParagraphStyle(); para.lineBreakMode = .byTruncatingMiddle231 let s = NSAttributedString(string: subtitle, attributes: [.font: EditorTheme.uiFont(EditorMetrics.microFontSize), .foregroundColor: EditorTheme.mutedText, .paragraphStyle: para])232 let pad = EditorMetrics.cardPadding233 let h: CGFloat = EditorMetrics.cardIcon + pad * 2234 return make(CGSize(width: max(180, width), height: h + NVSpace.sm)) { r in235 let box = CGRect(x: 0.5, y: NVSpace.xs, width: r.width - 1, height: h)236 let shape = NSBezierPath(roundedRect: box, xRadius: EditorMetrics.cardRadius, yRadius: EditorMetrics.cardRadius)237 EditorTheme.surface.setFill(); shape.fill(); EditorTheme.hairline.setStroke(); shape.lineWidth = 1; shape.stroke()238 let tile = CGRect(x: box.minX + pad, y: box.minY + pad, width: EditorMetrics.cardIcon, height: EditorMetrics.cardIcon)239 EditorTone.neutral.fill.setFill(); NSBezierPath(roundedRect: tile, xRadius: NVRadius.sm, yRadius: NVRadius.sm).fill()240 if let icon = NSImage(systemSymbolName: symbol, accessibilityDescription: nil)?.withSymbolConfiguration(.init(pointSize: 14, weight: .regular)) {241 Self.drawTemplate(icon, in: CGRect(x: tile.midX - icon.size.width / 2, y: tile.midY - icon.size.height / 2, width: icon.size.width, height: icon.size.height), color: EditorTheme.mutedText)242 }243 let tx = tile.maxX + NVSpace.md, tw = r.width - tx - pad244 if subtitle.isEmpty { t.draw(with: CGRect(x: tx, y: box.midY - 9, width: tw, height: 20), options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine]) }245 else {246 t.draw(with: CGRect(x: tx, y: tile.minY - 2, width: tw, height: 20), options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine])247 s.draw(with: CGRect(x: tx, y: tile.minY + 16, width: tw, height: 16), options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine])248 }249 }250 }251252 // MARK: Tables253254 struct TableLayout { var rows: [[(text: NSAttributedString, header: Bool, span: Int)]]; var columns: Int; var colWidth: CGFloat; var rowHeights: [CGFloat]; var size: CGSize }255256 func tableLayout(_ node: PMNode, width: CGFloat) -> TableLayout {257 var rows: [[(NSAttributedString, Bool, Int)]] = []258 for row in node.content ?? [] where row.type == "tableRow" {259 rows.append((row.content ?? []).map { cell in260 let header = cell.type == "tableHeader"261 let doc = NoteDocument(node: PMNode(type: "doc", attrs: nil, content: cell.content ?? [], text: nil, marks: nil, extra: [:]))262 var t = theme; t.fontSize = theme.fontSize * 0.93263 let s = DocumentCodec(theme: t, resources: resources).attributedString(from: doc)264 if s.length > 0, s.string.hasSuffix("\n") { s.deleteCharacters(in: NSRange(location: s.length - 1, length: 1)) }265 if header { s.enumerateAttribute(.font, in: NSRange(location: 0, length: s.length)) { v, r, _ in if let f = v as? NSFont { s.addAttribute(.font, value: NSFontManager.shared.convert(f, toHaveTrait: .boldFontMask), range: r) } } }266 // cells use the default paragraph geometry: list indents of the document do not apply inside a grid267 let p = NSMutableParagraphStyle(); p.lineSpacing = 1; s.addAttribute(.paragraphStyle, value: p, range: NSRange(location: 0, length: s.length)); s.removeAttribute(.spDecoration, range: NSRange(location: 0, length: s.length))268 return (s, header, max(1, cell.attrs?["colspan"]?.int ?? 1))269 })270 }271 let columns = max(1, rows.map { $0.reduce(0) { $0 + $1.2 } }.max() ?? 1)272 let colWidth = max(64, min(260, (width - 1) / CGFloat(columns)))273 let heights = rows.map { row in row.map { ceil($0.0.boundingRect(with: CGSize(width: colWidth * CGFloat($0.2) - 16, height: 6000), options: [.usesLineFragmentOrigin]).height) }.max().map { max(22, $0) + 12 } ?? 34 }274 return TableLayout(rows: rows.map { $0.map { (text: $0.0, header: $0.1, span: $0.2) } }, columns: columns, colWidth: colWidth, rowHeights: heights, size: CGSize(width: colWidth * CGFloat(columns) + 1, height: heights.reduce(0, +) + 1 + 8))275 }276277 private func table(_ node: PMNode, width: CGFloat) -> NSImage {278 let l = tableLayout(node, width: width)279 return make(l.size) { _ in280 var y: CGFloat = 4.5281 for (ri, row) in l.rows.enumerated() {282 var x: CGFloat = 0.5283 for cell in row {284 let rect = CGRect(x: x, y: y, width: l.colWidth * CGFloat(cell.span), height: l.rowHeights[ri])285 if cell.header { EditorTheme.codeBackground.setFill(); rect.fill() }286 EditorTheme.strongLine.setStroke(); let b = NSBezierPath(rect: rect); b.lineWidth = 1; b.stroke()287 cell.text.draw(with: rect.insetBy(dx: 8, dy: 6), options: [.usesLineFragmentOrigin])288 x += rect.width289 }290 y += l.rowHeights[ri]291 }292 }293 }294295 static func byteCount(_ n: Int) -> String { n > 0 ? ByteCountFormatter.string(fromByteCount: Int64(n), countStyle: .file) : "" }296}297298struct RenderedMath { let image: NSImage; let descent: CGFloat; let size: CGSize }299