Repository Public
1// SP Notes iOS2// File: Editor/EditorAttributes.swift3// Description: Attributes that carry the document model inside the text storage: paragraph identity (LineStyle), raw marks, atoms.4// The text storage is the working representation while editing; these attributes make it convertible back to the model.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import UIKit9import SPNotesCore1011/// Everything of a model `Line` except its content: where the paragraph sits (containers), what it is, and its raw attributes.12struct LineStyle: Hashable, Sendable {13 var path: [Container]14 var type: String15 var attrs: [String: JSONValue]1617 init(path: [Container] = [], type: String = "paragraph", attrs: [String: JSONValue] = [:]) {18 self.path = path; self.type = type; self.attrs = attrs19 }20 init(_ line: Line) { self.init(path: line.path, type: line.type, attrs: line.attrs) }2122 static let paragraphAttrs: [String: JSONValue] = ["textAlign": .null]23 /// A fresh paragraph at the same place as `self` (same containers), without block id (ids are assigned at serialization).24 func paragraphHere() -> LineStyle { LineStyle(path: path, type: "paragraph", attrs: Self.paragraphAttrs) }2526 var isCode: Bool { type == "codeBlock" }27 var headingLevel: Int? {28 guard type == "heading" else { return nil }29 if case .int(let l)? = attrs["level"] { return Int(l) }30 if case .double(let l)? = attrs["level"] { return Int(l) }31 return 132 }33 /// Innermost list item container (listItem or taskItem), if any.34 var innermostItem: Container? { path.last(where: { $0.type == "listItem" || $0.type == "taskItem" }) }35 var innermostItemIndex: Int? { path.lastIndex(where: { $0.type == "listItem" || $0.type == "taskItem" }) }36 /// The list container that owns the innermost item.37 var innermostList: Container? {38 guard let i = innermostItemIndex, i > 0 else { return nil }39 return path[i - 1]40 }41 var listDepth: Int { path.filter { $0.type == "listItem" || $0.type == "taskItem" }.count }42 var quoteDepth: Int { path.filter { $0.type == "blockquote" }.count }43 var callout: Container? { path.last(where: { $0.type == "callout" || $0.type == "aiBlock" }) }44 var isInDetails: Bool { path.contains { $0.type == "details" } }45 var isTaskChecked: Bool {46 guard let item = innermostItem, item.type == "taskItem" else { return false }47 if case .bool(let b)? = item.attrs["checked"] { return b }48 return false49 }50}5152/// Reference box so that a LineStyle can live in an attributed string (attribute values are compared with isEqual when runs are coalesced).53final class LineBox: NSObject, @unchecked Sendable {54 let style: LineStyle55 init(_ style: LineStyle) { self.style = style }56 override func isEqual(_ object: Any?) -> Bool { (object as? LineBox)?.style == style }57 override var hash: Int { style.hashValue }58}5960/// Raw ProseMirror marks of a text run (the source of truth; fonts and colours are derived from them).61final class MarksBox: NSObject, @unchecked Sendable {62 let marks: [PMMark]63 init(_ marks: [PMMark]) { self.marks = marks }64 override func isEqual(_ object: Any?) -> Bool { (object as? MarksBox)?.marks == marks }65 override var hash: Int { marks.hashValue }66 func has(_ type: String) -> Bool { marks.contains { $0.type == type } }67 var link: String? {68 guard let m = marks.first(where: { $0.type == "link" }), case .string(let href)? = m.attrs?["href"] else { return nil }69 return href70 }71}7273extension NSAttributedString.Key {74 /// `LineBox` — uniform over a whole paragraph, including its terminating newline.75 static let spnLine = NSAttributedString.Key("spn.line")76 /// `MarksBox` — on text runs that carry marks.77 static let spnMarks = NSAttributedString.Key("spn.marks")78 /// `NSNumber` — presentation only: the ordinal of an ordered-list item, on the first paragraph of the item.79 static let spnOrdinal = NSAttributedString.Key("spn.ordinal")80 /// `NSNumber(1)` — presentation only: this paragraph is the first one of its innermost list item (it shows the marker).81 static let spnItemHead = NSAttributedString.Key("spn.itemHead")82}8384/// An atom of the document held by one U+FFFC character: an inline atom (mention, inline math, footnote reference, unknown inline node)85/// or a block atom (image, file, table, block math, rule, embed, unknown block) that fills its own paragraph.86final class AtomAttachment: NSTextAttachment {87 private(set) var node: PMNode = PMNode(type: "unknown", attrs: nil, content: nil, text: nil, marks: nil, extra: [:])88 private(set) var isBlock = false89 /// Width the image was rendered for (block atoms are re-rendered when the container width changes).90 var renderedWidth: CGFloat = 091 /// True while an image atom shows a placeholder because its picture is not on the device yet.92 var isPlaceholder = false9394 convenience init(node: PMNode, isBlock: Bool) {95 self.init(data: nil, ofType: nil)96 self.node = node97 self.isBlock = isBlock98 }99 override init(data contentData: Data?, ofType uti: String?) { super.init(data: contentData, ofType: uti) }100 required init?(coder: NSCoder) { super.init(coder: coder) }101102 func replaceNode(_ node: PMNode) { self.node = node }103}104105enum EditorCharacter {106 /// Stands for one atom in the storage (and counts as one UTF-16 unit in the model's inline offsets).107 static let attachment = "\u{FFFC}"108 /// A line break that does not end the paragraph: a `hardBreak` node, or a newline inside a code block.109 static let softBreak = "\u{2028}"110 static let softBreakScalar: unichar = 0x2028111 static let attachmentScalar: unichar = 0xFFFC112 static let newlineScalar: unichar = 0x000A113}114115enum EditorNodeTypes {116 static let hardBreak = PMNode(type: "hardBreak", attrs: nil, content: nil, text: nil, marks: nil, extra: [:])117 static func isList(_ type: String) -> Bool { type == "bulletList" || type == "orderedList" || type == "taskList" }118 static func isItem(_ type: String) -> Bool { type == "listItem" || type == "taskItem" }119}120