Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions App/Sources/MarkdownEditor/EditorTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1771,10 +1771,35 @@ final class EditorTextView: NSTextView {
return found
}

/// The value of `key` under `point`, and the range it covers.
private func hit<Value>(_ key: NSAttributedString.Key, at point: NSPoint) -> (Value, NSRange)? {
/// The value of `key` on the character under `point`, and the range it covers.
func hit<Value>(_ key: NSAttributedString.Key, at point: NSPoint) -> (Value, NSRange)? {
let index = characterIndexForInsertion(at: point)
return attributeRun(key, at: index) ?? attributeRun(key, at: index - 1)
if let run: (Value, NSRange) = attributeRun(key, at: index) { return run }
// A click on a character's right half puts the caret after it: the character before the caret is the one under
// the pointer, when the pointer is over its room. Beside the text (past a line's last character, under the
// note's last line) it is over none, and must not open what merely ends there: an address ending its line
// opened from a click in the empty room after it, once the space typed after the link was not saved.
let before = index - 1
guard covers(before, point), let run: (Value, NSRange) = attributeRun(key, at: before) else { return nil }
return run
}

/// Whether `point` is over the room the character at `index` takes on its line, not merely nearest to it: as wide
/// as the character draws (a hidden one's room being the kern the styling gives it, which is where its chip is
/// drawn) and as tall as the line, so that a task's box, drawn larger than its characters, still takes a click.
private func covers(_ index: Int, _ point: NSPoint) -> Bool {
guard index >= 0, let storage = textStorage, index < storage.length else { return false }
let range = NSRange(location: index, length: 1)
var box = rect(for: range) ?? .zero
if box.width < 1, let room = storage.attribute(.kern, at: index, effectiveRange: nil) as? CGFloat, room > 0 {
box.size.width = room
}
guard box.width > 0 else { return false }
if let line = lineBox(for: range) {
box.origin.y = line.minY
box.size.height = line.height
}
return box.insetBy(dx: -1, dy: 0).contains(point)
}

/// The value of `key` on the character at `index`, and the whole run it covers.
Expand Down
81 changes: 81 additions & 0 deletions App/Tests/MarkdownEditorTests/LinkClickTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import AppKit
import XCTest
@testable import MarkdownEditor

/// A click opens a link only when it lands on the link's own characters; a click in the room past the end of a line
/// opens nothing there, and puts the caret at the line's end as it does beside any text. That room is where a note
/// read back differs from what was typed: the space typed after a link is not saved, so a note ending on an address
/// comes back with the address at its line's end, and a click after it used to open the address.
@MainActor
final class LinkClickTests: XCTestCase {
private var window: NSWindow!
private var textView: EditorTextView!
private var controller: EditorController!

override func setUp() async throws {
window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 800, height: 600), styleMask: [.titled], backing: .buffered, defer: false)
window.isReleasedWhenClosed = false
textView = EditorTextView(usingTextLayoutManager: true)
textView.frame = window.contentView!.bounds
textView.isRichText = false
textView.allowsUndo = true
window.contentView!.addSubview(textView)
controller = EditorController()
controller.attach(textView)
window.makeFirstResponder(textView)
}

override func tearDown() async throws {
window.close()
}

private func load(_ text: String) {
controller.show(EditorContent(id: 1, text: text))
textView.setSelectedRange(NSRange(location: 0, length: 0))
if let layout = textView.textLayoutManager { layout.ensureLayout(for: layout.documentRange) }
}

/// The address a click at `point` would open, as `EditorTextView.mouseDown` looks it up.
private func openedLink(at point: NSPoint) -> String? {
let found: (String, NSRange)? = textView.hit(.markdownLink, at: point)
return found?.0
}

/// Where the characters of `range` are drawn.
private func letters(of range: NSRange) -> NSRect {
textView.selectionBoxes(for: range).reduce(NSRect.null) { $0.union($1) }
}

func testAClickPastTheEndOfALinkThatEndsItsLineOpensNothing() {
let text = "Read https://example.com\nNext line"
load(text)
let link = (text as NSString).range(of: "https://example.com")
let box = letters(of: link)
XCTAssertNil(openedLink(at: NSPoint(x: box.maxX + 40, y: box.midY)), "the room after the line belongs to no character")
}

func testAClickPastTheEndOfALinkEndingTheNoteOpensNothing() {
let text = "Read https://example.com"
load(text)
let link = (text as NSString).range(of: "https://example.com")
let box = letters(of: link)
XCTAssertNil(openedLink(at: NSPoint(x: box.maxX + 40, y: box.midY)))
}

func testAClickOnTheLinkOpensIt() {
let text = "Read https://example.com\nNext line"
load(text)
let link = (text as NSString).range(of: "https://example.com")
let box = letters(of: link)
XCTAssertEqual(openedLink(at: NSPoint(x: box.midX, y: box.midY)), "https://example.com")
// The right half of its last letter, whose caret the click sets after it: still the link's own room.
XCTAssertEqual(openedLink(at: NSPoint(x: box.maxX - 1, y: box.midY)), "https://example.com")
}

func testAClickOnTheWordBeforeTheLinkOpensNothing() {
let text = "Read https://example.com\nNext line"
load(text)
let word = letters(of: (text as NSString).range(of: "Read"))
XCTAssertNil(openedLink(at: NSPoint(x: word.midX, y: word.midY)))
}
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ How to write it: AGENTS.md, "Releasing an update".

## Unreleased

- Fixed: a click in the empty space after a link opens nothing; only a click on the link itself opens it.

## 0.1.2 - 2026-11-07

- Fixed: a board or collection inside a note follows a theme change (light, dark or another theme) at once, card titles included.
Expand Down