Repository Public
1// ORIX Docs2// File: App/Sources/Inspector/TypographyInspector.swift3// Description: The Typography Studio tab: character and paragraph formatting of the selection, with markers on the properties that4// are direct formatting rather than inherited from the style.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SwiftUI9import DocModel10import DocLayout1112extension DocumentSession {13 /// Direct character properties at the caret or at the start of the selection.14 var directCharacterProps: (props: CharProps, charStyle: String?) {15 guard let p = editor.currentParagraph() else { return (.none, nil) }16 let pos = selection.start17 if selection.isCaret {18 let ta = p.typingAttributes(at: pos.offset)19 return (editor.typingOverride ?? ta.props, ta.charStyle)20 }21 let r = p.run(containing: pos.offset) ?? p.runs.last22 return (r?.props ?? .none, r?.charStyle)23 }2425 /// Character controls bound to the selection.26 var selectionCharacterTarget: CharacterTarget {27 let direct = directCharacterProps28 var base = doc.styles.styleProps(editor.currentParagraph()?.style ?? StyleID.normal).char29 if let cs = direct.charStyle { for s in doc.styles.chain(cs) { base.apply(s.char) } }30 return CharacterTarget(resolved: editor.currentCharacterFormat(), direct: direct.props, base: base) { [weak self] name, key, change in31 guard let self else { return }32 // At a caret the change becomes the typing format, which only survives a coalescing step.33 let k = key ?? (self.selection.isCaret ? "caret-\(name)" : nil)34 self.perform(name, coalesce: k) { $0.formatCharacters(change) }35 self.refocusCanvas()36 }37 }3839 /// Paragraph controls bound to the paragraphs of the selection.40 var selectionParagraphTarget: ParagraphTarget {41 let p = editor.currentParagraph()42 return ParagraphTarget(resolved: editor.currentParagraphFormat(), direct: p?.props ?? .none,43 base: doc.styles.styleProps(p?.style ?? StyleID.normal).para) { [weak self] name, key, change in44 guard let self else { return }45 self.perform(name, coalesce: key) { $0.formatParagraphs { change(&$0.props) } }46 self.refocusCanvas()47 }48 }49}5051struct TypographyInspector: View {52 @Bindable var session: DocumentSession5354 var body: some View {55 let _ = session.selectionRevision56 let _ = session.revision57 let character = session.selectionCharacterTarget58 let paragraph = session.selectionParagraphTarget59 let para = session.editor.currentParagraph()60 let styleName = para.flatMap { session.doc.styles.style($0.style)?.name } ?? "—"61 let charStyle = session.directCharacterProps.charStyle62 let hasDirect = TypographyInspector.hasVisibleFormatting(character.direct) || !(para?.props.isEmpty ?? true) || charStyle != nil6364 VStack(alignment: .leading, spacing: ORIX.Space.s) {65 HStack(spacing: ORIX.Space.s) {66 Image(systemName: "paragraphsign").font(.system(size: 10, weight: .semibold)).foregroundStyle(ORIX.Palette.accent).accessibilityHidden(true)67 Text(styleName).font(.system(size: 12, weight: .semibold)).foregroundStyle(ORIX.Palette.text).lineLimit(1)68 if let cs = charStyle, let name = session.doc.styles.style(cs)?.name { Pill(text: name) }69 Spacer(minLength: 0)70 if hasDirect { Pill(text: String(localized: "Modified"), color: ORIX.Palette.secondary).help("This text has direct formatting on top of its style") }71 }72 .accessibilityElement(children: .combine)73 .accessibilityLabel(Text("Paragraph style: \(styleName)"))74 Text(verbatim: "\(character.resolved.fontFamily) · \(FontCatalog.weightName(character.resolved.weight)) · \(TypographyInspector.number(character.resolved.fontSize)) pt")75 .font(ORIX.Font.label).foregroundStyle(ORIX.Palette.textSecondary).lineLimit(1)76 InspectorButton("Clear Direct Formatting", symbol: "eraser", isEnabled: hasDirect, identifier: "format-clear") {77 session.run(.clearFormatting)78 session.refocusCanvas()79 }80 .help("Remove direct character and paragraph formatting so the text shows its style as designed")81 }82 .padding(.horizontal, ORIX.Space.l).padding(.vertical, ORIX.Space.m)83 .frame(maxWidth: .infinity, alignment: .leading)84 .overlay(alignment: .bottom) { Rectangle().fill(ORIX.Palette.hairline.opacity(0.7)).frame(height: 1) }8586 CharacterGroups(target: character, scopeID: "format")87 ParagraphGroups(target: paragraph, scopeID: "format", fontSize: character.resolved.fontSize)88 }8990 /// Direct character formatting other than the link and the language (which "Clear" keeps).91 static func hasVisibleFormatting(_ props: CharProps) -> Bool {92 var p = props93 p.link = nil; p.language = nil94 return !p.isEmpty95 }9697 static func number(_ v: Double) -> String {98 let f = NumberFormatter(); f.minimumFractionDigits = 0; f.maximumFractionDigits = 1; f.usesGroupingSeparator = false99 return f.string(from: NSNumber(value: v)) ?? String(v)100 }101}102