improves plak

This commit is contained in:
2026-10-17 17:33:13 +02:00
parent 7b891819b0
commit a92d6f8871
633 changed files with 17623 additions and 7556 deletions
+81 -25
View File
@@ -14,6 +14,8 @@ final class PageText {
let imageBase: URL
let images: ImageCache
private(set) var font: NSFont
/// The font of code blocks and tables.
private(set) var codeFont: NSFont
private(set) var lineHeight: CGFloat = 0
/// The look of every character; an image leaves out the baseline offset.
private(set) var attributes: [NSAttributedString.Key: Any] = [:]
@@ -22,12 +24,14 @@ final class PageText {
self.imageBase = imageBase
self.images = images
font = PlakFonts.body(bodySize)
codeFont = PlakFonts.mono(round(bodySize * PageMetrics.codeScale))
setBodySize(bodySize)
}
/// The minimum line height lets a line with an image grow to the image.
func setBodySize(_ size: CGFloat) {
font = PlakFonts.body(size)
codeFont = PlakFonts.mono(round(size * PageMetrics.codeScale))
lineHeight = PageMetrics.lineHeight(font)
let style = NSMutableParagraphStyle()
style.minimumLineHeight = lineHeight
@@ -49,10 +53,8 @@ final class PageText {
/// The text of the page; every character of a block carries its date, or the undated mark.
func attributedString(_ file: PageFile) -> NSAttributedString {
let image = "\u{FFFC}"
let string = file.lines.map { line in
if case .image = line { return image }
return line.text
Self.isObject(line) ? "\u{FFFC}" : line.text
}.joined(separator: "\n")
let text = NSMutableAttributedString(string: string, attributes: attributes)
var starts: [Int] = []
@@ -60,7 +62,7 @@ final class PageText {
var location = 0
for line in file.lines {
starts.append(location)
if case .image = line {
if Self.isObject(line) {
location += 2
} else {
location += (line.text as NSString).length + 1
@@ -69,8 +71,13 @@ final class PageText {
starts.append(location)
text.beginEditing()
for (index, line) in file.lines.enumerated() {
if case let .image(path) = line {
text.setAttributes(attachmentAttributes(path), range: NSRange(location: starts[index], length: 1))
switch line {
case let .image(path):
text.setAttributes(attachmentAttributes(imageAttachment(path)), range: NSRange(location: starts[index], length: 1))
case let .fenced(kind, body):
text.setAttributes(attachmentAttributes(object(kind, body)), range: NSRange(location: starts[index], length: 1))
case .text:
break
}
}
for block in file.blocks {
@@ -85,43 +92,69 @@ final class PageText {
return text
}
/// Pasted plain text; a line that is exactly an image line of an existing image file becomes that image.
/// Pasted plain text; an image line of an existing image file becomes that image, a fenced block its object.
func attributedString(plainText: String) -> NSAttributedString {
let text = NSMutableAttributedString()
let lines = plainText.replacingOccurrences(of: "\r\n", with: "\n").components(separatedBy: "\n")
for (index, line) in lines.enumerated() {
for (index, line) in PageFile.lines(pasted: plainText).enumerated() {
if index > 0 { text.append(NSAttributedString(string: "\n", attributes: attributes)) }
if let path = PageFile.imagePath(fromLine: line), FileManager.default.fileExists(atPath: imageURL(path).path) {
switch line {
case let .image(path) where FileManager.default.fileExists(atPath: imageURL(path).path):
text.append(imageString(path))
} else {
text.append(NSAttributedString(string: line, attributes: attributes))
case let .fenced(kind, body):
text.append(objectString(kind, body))
default:
text.append(NSAttributedString(string: line.text, attributes: attributes))
}
}
return text
}
/// Pasted text exactly as it is: fences and image lines stay text.
func plainString(_ plainText: String) -> NSAttributedString {
NSAttributedString(string: plainText.replacingOccurrences(of: "\r\n", with: "\n"), attributes: attributes)
}
func imageString(_ path: String) -> NSAttributedString {
NSAttributedString(string: "\u{FFFC}", attributes: attachmentAttributes(path))
NSAttributedString(string: "\u{FFFC}", attributes: attachmentAttributes(imageAttachment(path)))
}
/// A code block, or a table for csv.
func objectString(_ kind: FenceKind, _ body: String) -> NSAttributedString {
NSAttributedString(string: "\u{FFFC}", attributes: attachmentAttributes(object(kind, body)))
}
func object(_ kind: FenceKind, _ body: String) -> NSTextAttachment {
kind == .csv ? TableAttachment(body: body, font: codeFont) : CodeAttachment(kind: kind, body: body, font: codeFont)
}
func newline() -> NSAttributedString {
NSAttributedString(string: "\n", attributes: attributes)
}
private func attachmentAttributes(_ path: String) -> [NSAttributedString.Key: Any] {
private func imageAttachment(_ path: String) -> ImageAttachment {
ImageAttachment(path: path, image: images.image(at: imageURL(path)), font: font)
}
private func attachmentAttributes(_ attachment: NSTextAttachment) -> [NSAttributedString.Key: Any] {
var attributes = imageAttributes
attributes[.attachment] = ImageAttachment(path: path, image: images.image(at: imageURL(path)), font: font)
attributes[.attachment] = attachment
return attributes
}
private static func isObject(_ line: PageFile.Line) -> Bool {
if case .text = line { return false }
return true
}
private func imageURL(_ path: String) -> URL {
imageBase.appendingPathComponent(path)
}
// MARK: Text to page file
/// The page file of the text: one line per text line, an image always on a line of its own.
static func pageFile(_ text: NSAttributedString) -> PageFile {
/// The page file of the text: one line per text line, an image or other object always on a line of its own.
/// A draft puts the edit of an open editor in place of its object, without building a new object.
static func pageFile(_ text: NSAttributedString, draft: (location: Int, edit: ObjectEdit)? = nil) -> PageFile {
let string = text.string as NSString
guard string.length > 0 else { return PageFile() }
var lines: [PageFile.Line] = []
@@ -130,7 +163,11 @@ final class PageText {
var location = 0
while location <= string.length {
let end = string.lineEnd(from: location)
let pieces = lineContents(text, NSRange(location: location, length: end - location))
let pieces = lineContents(text, NSRange(location: location, length: end - location), draft: draft)
guard !pieces.isEmpty else {
location = end + 1
continue
}
let blank = pieces.count == 1 && pieces[0].isBlank
if !blank, previousBlank, location < string.length, let date = text.attribute(.plakCreated, at: location, effectiveRange: nil) as? Date {
dates[lines.count] = date
@@ -142,12 +179,12 @@ final class PageText {
return PageFile(lines: lines, dates: dates)
}
/// Plain text of a range, as for the pasteboard: each image becomes its image line.
/// Plain text of a range, as for the pasteboard: each image becomes its image line, each object its fenced block.
static func plainText(_ text: NSAttributedString, range: NSRange) -> String {
var out = ""
text.enumerateAttribute(.attachment, in: range) { value, run, _ in
if let image = value as? ImageAttachment {
out += String(repeating: PageFile.imageLine(image.path), count: run.length)
if let line = value.flatMap(line(for:)) {
out += Array(repeating: line.text, count: run.length).joined(separator: "\n")
} else {
out += (text.string as NSString).substring(with: run)
}
@@ -156,21 +193,40 @@ final class PageText {
}
/// One text line as page file lines; text typed beside an image goes on its own line.
private static func lineContents(_ text: NSAttributedString, _ range: NSRange) -> [PageFile.Line] {
/// An object the draft removes takes its line with it: then there are no lines at all.
private static func lineContents(_ text: NSAttributedString, _ range: NSRange,
draft: (location: Int, edit: ObjectEdit)?) -> [PageFile.Line] {
let string = text.string as NSString
guard range.length > 0, string.range(of: "\u{FFFC}", range: range).location != NSNotFound else {
return [.text(string.substring(with: range))]
}
var lines: [PageFile.Line] = []
var removed = false
text.enumerateAttribute(.attachment, in: range) { value, run, _ in
if let image = value as? ImageAttachment {
lines += Array(repeating: .image(image.path), count: run.length)
if let line = value.flatMap(line(for:)) {
for location in run.location..<NSMaxRange(run) {
switch location == draft?.location ? draft?.edit : nil {
case let .replaced(kind, body)?: lines.append(.fenced(kind, body))
case .removed?: removed = true
case .unchanged?, nil: lines.append(line)
}
}
} else {
let piece = string.substring(with: run)
if !PageFile.isBlank(piece) { lines.append(.text(piece)) }
}
}
return lines.isEmpty ? [.text("")] : lines
return lines.isEmpty && !removed ? [.text("")] : lines
}
/// The page file line of an image, code block or table attachment.
static func line(for attachment: Any) -> PageFile.Line? {
switch attachment {
case let image as ImageAttachment: .image(image.path)
case let code as CodeAttachment: .fenced(code.kind, code.body)
case let table as TableAttachment: .fenced(.csv, table.body)
default: nil
}
}
// MARK: Created dates