Repository Public
1// SP Notes iOS2// File: Features/Editor/NoteEditorView.swift3// Description: SwiftUI host of the native editor. The title is a native text field placed inside the text view's scroll area, so4// title and body scroll as one page. Keyboard avoidance (bottom content inset only, the frame never moves) is done by5// the screen that hosts this view.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SwiftUI10import UIKit1112struct NoteEditorView: UIViewRepresentable {13 let model: NoteEditorModel14 var focusTitleOnAppear = false1516 func makeCoordinator() -> Coordinator { Coordinator(model: model) }1718 func makeUIView(context: Context) -> NoteTextView {19 let textView = model.controller.textView20 context.coordinator.install(on: textView)21 if focusTitleOnAppear { DispatchQueue.main.async { context.coordinator.titleView.becomeFirstResponder() } }22 return textView23 }2425 func updateUIView(_ textView: NoteTextView, context: Context) {26 context.coordinator.syncTitle(model.title)27 }2829 @MainActor30 final class Coordinator: NSObject, UITextViewDelegate {31 let model: NoteEditorModel32 let titleView = UITextView()33 private let placeholder = UILabel()34 private weak var host: NoteTextView?35 private var lastTitleHeight: CGFloat = 03637 init(model: NoteEditorModel) { self.model = model }3839 func install(on textView: NoteTextView) {40 host = textView41 titleView.removeFromSuperview()42 titleView.delegate = self43 titleView.isScrollEnabled = false44 titleView.backgroundColor = .clear45 titleView.textContainerInset = .zero46 titleView.textContainer.lineFragmentPadding = 047 titleView.font = Self.titleFont48 titleView.adjustsFontForContentSizeCategory = true49 titleView.textColor = UIColor(named: "TextPrimary")50 titleView.tintColor = UIColor(named: "AccentColor")51 titleView.returnKeyType = .next52 titleView.accessibilityIdentifier = "note-title"53 titleView.accessibilityLabel = String(localized: "Titre de la note")54 titleView.text = model.title55 placeholder.text = String(localized: "Titre")56 placeholder.font = Self.titleFont57 placeholder.adjustsFontForContentSizeCategory = true58 placeholder.textColor = UIColor(named: "Muted")59 placeholder.isAccessibilityElement = false60 titleView.addSubview(placeholder)61 textView.addSubview(titleView)62 textView.onLayout = { [weak self] in self?.layout() }63 layout()64 }6566 static var titleFont: UIFont {67 let base = UIFont.preferredFont(forTextStyle: .title1)68 return UIFont(descriptor: base.fontDescriptor.withSymbolicTraits(.traitBold) ?? base.fontDescriptor, size: 0)69 }7071 func syncTitle(_ title: String) {72 titleView.isEditable = !model.isReadOnly73 if titleView.text != title, !titleView.isFirstResponder { titleView.text = title; layout() }74 placeholder.isHidden = !titleView.text.isEmpty75 }7677 /// Places the title above the first line of the body and reserves its height in the text container inset.78 func layout() {79 guard let host else { return }80 let side: CGFloat = 1881 let width = max(100, host.bounds.width - 2 * side - host.adjustedContentInset.left - host.adjustedContentInset.right)82 let height = ceil(titleView.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude)).height)83 titleView.frame = CGRect(x: side, y: 14, width: width, height: height)84 placeholder.frame = CGRect(x: 0, y: 0, width: width, height: ceil(Self.titleFont.lineHeight))85 placeholder.isHidden = !titleView.text.isEmpty86 if abs(height - lastTitleHeight) > 0.5 || host.textContainerInset.top < height {87 lastTitleHeight = height88 var inset = host.textContainerInset89 inset.top = 14 + height + 1090 host.textContainerInset = inset91 }92 }9394 // MARK: UITextViewDelegate (title)9596 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {97 if text == "\n" { model.focusBody(); return false }98 if text.contains("\n") {99 // a pasted multi-line text keeps its first line as the title100 textView.insertText(text.replacingOccurrences(of: "\r", with: "").components(separatedBy: "\n").first ?? "")101 return false102 }103 return true104 }105106 func textViewDidChange(_ textView: UITextView) {107 if textView.text.count > 500 { textView.text = String(textView.text.prefix(500)) }108 model.title = textView.text109 model.titleChanged()110 layout()111 }112 }113}114