feat(keyboard): add clipboard history and undo pastes

Ship optional keyboard clipboard history with settings, suggestion strip, and paste-permission guidance; let undo roll back clipboard inserts; bump build to 64.
This commit is contained in:
Rocky
2026-08-11 02:49:54 +08:00
parent 3de665d254
commit fd6e0d3e7e
30 changed files with 1478 additions and 118 deletions
@@ -10,6 +10,10 @@ import OSGKeyboardShared
@MainActor
final class KeyboardTextInserter {
/// Hosts truncate `documentContextBeforeInput` (often to the current
/// paragraph), so a long insertion can only ever be matched by its tail.
private static let caretVerificationLimit = 80
private let state: KeyboardState
private let insertText: (String) -> Void
private let deleteBackward: () -> Void
@@ -18,9 +22,10 @@ final class KeyboardTextInserter {
private let selectedText: () -> String?
private let scheduleAutoClearError: () -> Void
/// Exact string last inserted by voice (including any word-boundary
/// separator). Cleared after a successful undo or when the caret no
/// longer sits after that text.
/// Exact string last inserted through this inserter dictation, AI answer,
/// edit result or clipboard paste (including any word-boundary separator).
/// Cleared after a successful undo or when the caret no longer sits after
/// that text.
private var lastInsertedText: String?
/// Text captured when the last insertion was undone, so redo can
/// re-apply it. Cleared by any new insertion or external edit.
@@ -80,6 +85,7 @@ final class KeyboardTextInserter {
)
let inserted = separator + trimmed
insertText(inserted)
state.noteUserDidInputText()
recordLastInsertion(
inserted,
displayText: trimmed,
@@ -111,6 +117,7 @@ final class KeyboardTextInserter {
)
let inserted = separator + trimmed
insertText(inserted)
state.noteUserDidInputText()
let mutation = HistoryMutation(
action: .append,
@@ -134,13 +141,28 @@ final class KeyboardTextInserter {
return true
}
/// Roll back the last voice insertion when it is still at the caret.
/// Insert clipboard text verbatim and make it undoable. Pasted text is not
/// a dictation result: it never becomes an editable "last input" reference
/// and never reaches the history outbox, so it only takes the undo record.
func insertPasteboardText(_ text: String) {
guard !text.isEmpty else { return }
// Verbatim on purpose paste must reproduce exactly what was copied,
// unlike dictation which needs word-boundary hygiene.
insertText(text)
recordUndoableInsertion(text)
// The paste pushed any previous input away from the caret, so the
// "editable last input" hint no longer applies.
clearEditHintIfPositive()
OSGLog.keyboardExt.info("clipboard insert length=\(text.count, privacy: .public)")
}
/// Roll back the last insertion when it is still at the caret.
func undoLastInsertion() {
if undoLastEditIfPossible() {
return
}
guard let text = lastInsertedText, !text.isEmpty else { return }
guard let preceding = contextBeforeInput(), preceding.hasSuffix(text) else {
guard caretSitsAfter(text) else {
clearLastInsertion()
return
}
@@ -148,6 +170,9 @@ final class KeyboardTextInserter {
isUndoing = true
defer { isUndoing = false }
// `UITextDocumentProxy` has no ranged delete, so the whole insertion is
// walked back one grapheme at a time. Staying synchronous keeps it in a
// single run-loop turn, which the host coalesces into one visual update.
for _ in 0..<text.count {
deleteBackward()
}
@@ -157,11 +182,11 @@ final class KeyboardTextInserter {
redoContextBefore = contextBeforeInput()
lastInsertedText = nil
state.undoAvailable = false
OSGLog.keyboardExt.info("voice undo length=\(text.count, privacy: .public)")
OSGLog.keyboardExt.info("undo length=\(text.count, privacy: .public)")
}
/// Re-apply the last undone voice insertion when it is still absent from
/// the caret (i.e. the undo was not overwritten by an external edit).
/// Re-apply the last undone insertion when it is still absent from the
/// caret (i.e. the undo was not overwritten by an external edit).
func redoLastInsertion() {
guard let text = redoText, !text.isEmpty,
contextBeforeInput() == redoContextBefore else {
@@ -170,17 +195,14 @@ final class KeyboardTextInserter {
state.redoAvailable = false
return
}
guard let preceding = contextBeforeInput(), !preceding.hasSuffix(text) else {
guard !caretSitsAfter(text) else {
redoText = nil
state.redoAvailable = false
return
}
insertText(text)
lastInsertedText = text
redoText = nil
redoContextBefore = nil
state.undoAvailable = true
OSGLog.keyboardExt.info("voice redo length=\(text.count, privacy: .public)")
recordUndoableInsertion(text)
OSGLog.keyboardExt.info("redo length=\(text.count, privacy: .public)")
}
/// Copy the host field's current selection to the pasteboard. Needs Full
@@ -207,7 +229,7 @@ final class KeyboardTextInserter {
// `deleteBackward`); the undo method manages availability itself.
if !isUndoing {
if let text = lastInsertedText, !text.isEmpty {
let available = contextBeforeInput()?.hasSuffix(text) == true
let available = caretSitsAfter(text)
if !available {
// Caret moved or the user edited the insertion drop it.
lastInsertedText = nil
@@ -345,7 +367,6 @@ final class KeyboardTextInserter {
HistoryMutationOutbox.enqueue(mutation)
transaction.phase = .committed
PendingTextEditTransactionStore.save(transaction)
lastEditUndo = transaction
recordLastInsertion(
inserted,
displayText: result,
@@ -355,6 +376,9 @@ final class KeyboardTextInserter {
: 0,
pendingHistoryMutationID: mutation.id
)
// Set after recording: the shared bookkeeping drops any stale
// transaction, and this one must survive as the undo target.
lastEditUndo = transaction
PendingTextEditTransactionStore.clear()
return true
}
@@ -389,12 +413,11 @@ final class KeyboardTextInserter {
}
private func undoLastEditIfPossible() -> Bool {
guard let transaction = lastEditUndo,
let preceding = contextBeforeInput() else {
guard let transaction = lastEditUndo else {
return false
}
let insertedAfter = lastInsertedText ?? transaction.afterText
guard preceding.hasSuffix(insertedAfter) else {
guard caretSitsAfter(insertedAfter) else {
lastEditUndo = nil
return false
}
@@ -436,6 +459,38 @@ final class KeyboardTextInserter {
return true
}
/// Is the caret still sitting right after `text`?
///
/// The comparison is limited to the tail of the insertion's last line: a
/// truncated host context can never contain the earlier part, and text
/// before a line break is usually stripped from `documentContextBeforeInput`.
private func caretSitsAfter(_ text: String) -> Bool {
guard let preceding = contextBeforeInput() else { return false }
let lastLine: String
if let breakRange = text.rangeOfCharacter(from: .newlines, options: .backwards) {
lastLine = String(text[breakRange.upperBound...])
} else {
lastLine = text
}
let expected = String(lastLine.suffix(Self.caretVerificationLimit))
// The insertion ended on a line break, so nothing measurable is left
// before the caret an empty context is the expected state.
guard !expected.isEmpty else { return preceding.isEmpty }
return preceding.hasSuffix(expected)
}
/// Undo bookkeeping shared by every insertion path. Callers that also own
/// history / editable-reference state layer `recordLastInsertion` on top.
private func recordUndoableInsertion(_ text: String) {
lastInsertedText = text
redoText = nil
redoContextBefore = nil
// A newer insertion supersedes any edit transaction: undo must roll back
// this text, not re-apply the original text of an older edit.
lastEditUndo = nil
state.undoAvailable = true
}
private func recordLastInsertion(
_ text: String,
displayText: String,
@@ -443,10 +498,7 @@ final class KeyboardTextInserter {
historyEntryRevision: Int64?,
pendingHistoryMutationID: UUID?
) {
lastInsertedText = text
redoText = nil
redoContextBefore = nil
state.undoAvailable = true
recordUndoableInsertion(text)
EditableInputReferenceStore.save(
EditableInputReference(
historyEntryID: historyEntryID,
@@ -474,6 +526,14 @@ final class KeyboardTextInserter {
}
}
private func clearEditHintIfPositive() {
guard state.editHintIsPositive else { return }
editHintTask?.cancel()
editHintTask = nil
state.editHint = nil
state.editHintIsPositive = false
}
private func clearLastInsertion() {
lastInsertedText = nil
state.undoAvailable = false