mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
- Move the full keyboard's engine, view and autocorrect families into HEXShared/FullKeyboard/Engine, View and Autocorrect. - Move dictation formatting, spelling and romanizer sources into HEXShared/Formatting, Spelling and Romanizers. - Split the 4,181-line FullKeyboardEngineTests file into per-capability extension files, and KeyboardClipboardTests into its own file. Pure moves: no behavior changes and the same 160 tests.
45 lines
2 KiB
Swift
45 lines
2 KiB
Swift
import UIKit
|
||
|
||
/// Add to vocabulary from the strip: hold the word you typed (the strip's left slot) and the
|
||
/// strip offers it; the chip adds it to Hex's vocabulary list (`CustomVocabularySettings`,
|
||
/// the one Settings › Keyboard › Vocabulary and Settings › Dictation edit), which the keys
|
||
/// then never autocorrect away, complete and swipe.
|
||
@MainActor
|
||
extension FullKeyboardView {
|
||
func offerVocabulary(_ suggestion: KeyboardSuggestion) {
|
||
var word = suggestion.replacement
|
||
// A capital only the sentence's start gave it ("Kubectl" opening a message) isn't its own.
|
||
if let first = word.first, first.isUppercase, !word.dropFirst().contains(where: \.isUppercase),
|
||
AutocorrectEngine.startsSentence(before: engine.target.documentContextBeforeInput ?? "", token: word) {
|
||
word = first.lowercased() + word.dropFirst()
|
||
}
|
||
guard suggestion.kind == .keepLiteral, engine.autocorrect.vocabularyTerm(for: word) == nil,
|
||
VocabularySpelling.normalizedKey(word).count >= 2 else { return }
|
||
typingLog?.ui("vocabulary_offer")
|
||
selectionFeedback()
|
||
vocabularyOffer = word
|
||
refreshStrip()
|
||
}
|
||
|
||
var vocabularyNotice: KeyboardStripNotice? {
|
||
guard let word = vocabularyOffer else { return nil }
|
||
return KeyboardStripNotice(
|
||
title: "“\(word)”",
|
||
detail: String(localized: "Never autocorrected"),
|
||
button: String(localized: "Add to vocabulary")
|
||
)
|
||
}
|
||
|
||
/// Saves `word` in the App Group (kept for this session too where the keyboard can't
|
||
/// write there, without Full Access) and uses it at once.
|
||
func addToVocabulary(_ word: String) {
|
||
typingLog?.ui("vocabulary_add")
|
||
let settings = CustomVocabularySettings()
|
||
let terms = CustomVocabularySettings.sanitize(settings.terms + engine.autocorrect.vocabulary + [word])
|
||
settings.terms = terms
|
||
engine.autocorrect.setVocabulary(terms)
|
||
vocabularyOffer = nil
|
||
engine.refreshSuggestions()
|
||
refreshStrip()
|
||
}
|
||
}
|