oficina / docs Public

Docs native S1b: ⌘B typed behind a letter applies (Fable's delta review 3)

The text view's key equivalents (DocsTextView.performKeyEquivalent on the
Mac, its UIKeyCommands on iOS) go to DocsModel.perform, which refused
bold, italic, code, indent, outdent and clear formatting while the letter
before them was pending. Those now go through whenCommandsReady like a
click (queued, in order, bounded as the previous commit says); undo, redo
and the rest as before.

Probe counters for the two paths a formatting command can take (the text
view's key equivalent, the Format menu). The window smoke test
(test-document-window.py) types a letter, sends ⌘B through the app's own
event dispatch and types another: ⌘B reaches the text view's key
equivalent, not the menu, and the next letter is bold. Unit test through
performKeyEquivalent. Both fail with the routing removed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev dd051678e6daa969ba0a6e0d69dd3ff789b6e0f0 parent 666ab07
6 files changed, +58 −2
  • native/apple/Sources/DocsKit/DocsEditorContext.swift +3 −0
  • native/apple/Sources/DocsKit/DocsModel.swift +9 −2
  • native/apple/Sources/DocsKit/DocsProbes.swift +5 −0
  • native/apple/Sources/DocsKit/DocumentComposition.swift +3 −0
  • native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift +12 −0
  • native/smoke/document-window/Sources/Smoke.swift +26 −0
diff --git a/native/apple/Sources/DocsKit/DocsEditorContext.swift b/native/apple/Sources/DocsKit/DocsEditorContext.swift
index 76a39e9..277ed06 100644
--- a/native/apple/Sources/DocsKit/DocsEditorContext.swift
+++ b/native/apple/Sources/DocsKit/DocsEditorContext.swift
@@ -24,6 +24,9 @@ struct DocsEditorContext {
/// chosen has been drawn (`DocsModel.whenCommandsReady`).
@MainActor func ready(_ command: @escaping @MainActor (DocumentCommands) -> Void) {
guard actions != nil else { return }
+ #if DOCS_PROBES
+ DocsProbes.menuCommands += 1
+ #endif
model.whenCommandsReady(command)
}
}
diff --git a/native/apple/Sources/DocsKit/DocsModel.swift b/native/apple/Sources/DocsKit/DocsModel.swift
index 98b644f..3431ca3 100644
--- a/native/apple/Sources/DocsKit/DocsModel.swift
+++ b/native/apple/Sources/DocsKit/DocsModel.swift
@@ -929,10 +929,17 @@ import RastrilloNative
readyQueue.removeFirst()(commands)
}
}
- /// A command from the text view's keys.
+ /// A command from the text view's keys. Formatting typed straight after
+ /// a letter (⌘B while the letter is pending) is queued like a click.
func perform(_ action: String) -> Bool {
if action == "jump" { jumpRequests &+= 1; return true }
- return DocumentCommands(model: self, content: surface.content).perform(action)
+ let commands = DocumentCommands(model: self, content: surface.content)
+ guard ["bold", "italic", "code", "indent", "outdent", "clear-format"].contains(action), !commands.isEnabled else {
+ return commands.perform(action)
+ }
+ guard commands.isAvailable, !commandsHeld else { return false }
+ whenCommandsReady { $0.perform(action) }
+ return true
}
func canPerform(_ action: String) -> Bool {
detail?.can_write == true && !hasPendingInput && !composing && !inputBlocked
diff --git a/native/apple/Sources/DocsKit/DocsProbes.swift b/native/apple/Sources/DocsKit/DocsProbes.swift
index f72da15..7b89a87 100644
--- a/native/apple/Sources/DocsKit/DocsProbes.swift
+++ b/native/apple/Sources/DocsKit/DocsProbes.swift
@@ -28,6 +28,11 @@ public enum DocsProbes {
/// not reach (S1b step 0: each key re-evaluated them, 2–4 ms a key):
/// the workspace, and the Mac's Format inspector.
nonisolated(unsafe) public static var workspaceBodies = 0
+ /// Formatting commands by path: the text view's own key equivalent
+ /// (⌘B in the editor) and the Format menu (the smoke test checks which
+ /// one a typed ⌘B takes).
+ nonisolated(unsafe) public static var keyEquivalentCommands = 0
+ nonisolated(unsafe) public static var menuCommands = 0
nonisolated(unsafe) public static var inspectorBodies = 0
/// iOS `UITextView.attributedText` assignments by the editor.
nonisolated(unsafe) public static var attributedTextAssignments = 0
diff --git a/native/apple/Sources/DocsKit/DocumentComposition.swift b/native/apple/Sources/DocsKit/DocumentComposition.swift
index 708d50f..0b44394 100644
--- a/native/apple/Sources/DocsKit/DocumentComposition.swift
+++ b/native/apple/Sources/DocsKit/DocumentComposition.swift
@@ -38,6 +38,9 @@ final class DocsTextView: DocsPagedTextView {
let flags = event.modifierFlags
if let key = event.charactersIgnoringModifiers,
let name = DocumentKeyboardBindings.action(key: key, command: flags.contains(.command), shift: flags.contains(.shift), option: flags.contains(.option), control: flags.contains(.control)) {
+ #if DOCS_PROBES
+ DocsProbes.keyEquivalentCommands += 1
+ #endif
_ = action?(name); return true
}
return super.performKeyEquivalent(with: event)
diff --git a/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift b/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift
index 3033707..0e48fc4 100644
--- a/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift
+++ b/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift
@@ -140,6 +140,18 @@ import XCTest
try await until("the bold chosen while composing") { model.typingMarks?["strong"] != nil }
}
+ /// ⌘B typed straight after a letter, through the text view's own key
+ /// equivalent (DocsTextView.performKeyEquivalent → DocsModel.perform),
+ /// applies once the letter is drawn (delta review 3); it was dropped.
+ func testBoldTypedRightAfterALetterApplies() async throws {
+ typeInTheMiddle("x")
+ XCTAssertTrue(model.hasPendingInput)
+ let event = try XCTUnwrap(NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: .command, timestamp: 0, windowNumber: 0,
+ context: nil, characters: "b", charactersIgnoringModifiers: "b", isARepeat: false, keyCode: 11))
+ XCTAssertTrue(text.performKeyEquivalent(with: event))
+ try await until("the bold typed behind the letter") { model.typingMarks?["strong"] != nil }
+ }
+
/// Undo and redo read the observed history, set with each publication:
/// it is current while input is still pending (the toolbar selection
/// keeps its last state then), and for a reader, whose toolbar
diff --git a/native/smoke/document-window/Sources/Smoke.swift b/native/smoke/document-window/Sources/Smoke.swift
index 0ca442d..11a10d8 100644
--- a/native/smoke/document-window/Sources/Smoke.swift
+++ b/native/smoke/document-window/Sources/Smoke.swift
@@ -191,6 +191,32 @@ private struct Launcher: View {
let settled = editor.firstRect(forCharacterRange: editor.selectedRange(), actualRange: nil)
precondition(abs(immediate.minY - settled.minY) < 1, "Caret jumped after publication")
}
+ #if DEBUG
+ // ⌘B typed straight behind a letter, through the app's own event
+ // dispatch (key window, then the menu): the text view's key
+ // equivalent takes it, and the bold applies to what follows (S1b,
+ // Fable's delta review 3).
+ do {
+ window.makeKeyAndOrderFront(nil)
+ window.makeFirstResponder(editor)
+ func key(_ character: String, _ flags: NSEvent.ModifierFlags = [], code: UInt16 = 0) -> NSEvent {
+ NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: flags, timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: window.windowNumber, context: nil, characters: character, charactersIgnoringModifiers: character, isARepeat: false, keyCode: code)!
+ }
+ let keys = DocsProbes.keyEquivalentCommands, menus = DocsProbes.menuCommands
+ editor.keyDown(with: key("z"))
+ NSApp.sendEvent(key("b", .command, code: 11))
+ try? await Task.sleep(for: .milliseconds(300))
+ editor.keyDown(with: key("w"))
+ try? await Task.sleep(for: .milliseconds(300))
+ precondition(DocsProbes.keyEquivalentCommands == keys + 1, "⌘B did not reach the text view's key equivalent")
+ precondition(DocsProbes.menuCommands == menus, "⌘B went to the Format menu")
+ let at = editor.selectedRange().location - 1
+ let marks = (editor.textStorage?.attribute(NSAttributedString.Key("OficinaDocsMarks"), at: at, effectiveRange: nil) as? Data)
+ .flatMap { try? JSONSerialization.jsonObject(with: $0) as? [String: Any] } ?? [:]
+ precondition(marks["strong"] != nil, "⌘B typed behind a letter did not apply: \(marks)")
+ print("PASS: ⌘B typed straight behind a letter goes through the text view's key equivalent and applies")
+ }
+ #endif
if let path = ProcessInfo.processInfo.environment["DOCS_SMOKE_SCREENSHOT"], let view = window.contentView,
let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) {
view.cacheDisplay(in: view.bounds, to: bitmap)