InputState // Also perform kanji conversion in attributedString.
This commit is contained in:
parent
e65c567ab0
commit
ece3f72ee5
|
@ -137,6 +137,15 @@ class InputState {
|
|||
class NotEmpty: InputState {
|
||||
private(set) var composingBuffer: String
|
||||
private(set) var cursorIndex: Int = 0 { didSet { cursorIndex = max(cursorIndex, 0) } }
|
||||
var composingBufferConverted: String {
|
||||
let converted = IME.kanjiConversionIfRequired(composingBuffer)
|
||||
if converted.utf16.count != composingBuffer.utf16.count
|
||||
|| converted.count != composingBuffer.count
|
||||
{
|
||||
return composingBuffer
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
init(composingBuffer: String, cursorIndex: Int) {
|
||||
self.composingBuffer = composingBuffer
|
||||
|
@ -145,8 +154,10 @@ class InputState {
|
|||
}
|
||||
|
||||
var attributedString: NSAttributedString {
|
||||
/// 考慮到因為滑鼠點擊等其它行為導致的組字區內容遞交情況,
|
||||
/// 這裡對組字區內容也加上康熙字轉換或者 JIS 漢字轉換處理。
|
||||
let attributedString = NSAttributedString(
|
||||
string: IME.kanjiConversionIfRequired(composingBuffer),
|
||||
string: composingBufferConverted,
|
||||
attributes: [
|
||||
.underlineStyle: NSUnderlineStyle.single.rawValue,
|
||||
.markedClauseSegment: 0,
|
||||
|
@ -260,7 +271,9 @@ class InputState {
|
|||
}
|
||||
|
||||
override var attributedString: NSAttributedString {
|
||||
let attributedString = NSMutableAttributedString(string: IME.kanjiConversionIfRequired(composingBuffer))
|
||||
/// 考慮到因為滑鼠點擊等其它行為導致的組字區內容遞交情況,
|
||||
/// 這裡對組字區內容也加上康熙字轉換或者 JIS 漢字轉換處理。
|
||||
let attributedString = NSMutableAttributedString(string: composingBufferConverted)
|
||||
let end = markedRange.upperBound
|
||||
|
||||
attributedString.setAttributes(
|
||||
|
|
|
@ -446,19 +446,4 @@ class KeyHandler {
|
|||
func deleteCompositorReadingToTheFrontOfCursor() {
|
||||
compositor.deleteReadingToTheFrontOfCursor()
|
||||
}
|
||||
|
||||
// MARK: - 其他工具函式
|
||||
|
||||
func kanjiConversionIfRequired(_ text: String) -> String {
|
||||
if inputMode == InputMode.imeModeCHT {
|
||||
switch (mgrPrefs.chineseConversionEnabled, mgrPrefs.shiftJISShinjitaiOutputEnabled) {
|
||||
case (false, true): return vChewingKanjiConverter.cnvTradToJIS(text)
|
||||
case (true, false): return vChewingKanjiConverter.cnvTradToKangXi(text)
|
||||
// 本來這兩個開關不該同時開啟的,但萬一被開啟了的話就這樣處理:
|
||||
case (true, true): return vChewingKanjiConverter.cnvTradToJIS(text)
|
||||
case (false, false): return text
|
||||
}
|
||||
}
|
||||
return text
|
||||
}
|
||||
}
|
||||
|
|
|
@ -298,7 +298,7 @@ extension ctlInputMethod {
|
|||
|
||||
/// 遞交組字區內容。
|
||||
private func commit(text: String) {
|
||||
let buffer = keyHandler.kanjiConversionIfRequired(text)
|
||||
let buffer = IME.kanjiConversionIfRequired(text)
|
||||
if buffer.isEmpty {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -43,6 +43,19 @@ public enum IME {
|
|||
|
||||
static var currentInputMode: InputMode = .init(rawValue: mgrPrefs.mostRecentInputMode) ?? .imeModeNULL
|
||||
|
||||
static func kanjiConversionIfRequired(_ text: String) -> String {
|
||||
if currentInputMode == InputMode.imeModeCHT {
|
||||
switch (mgrPrefs.chineseConversionEnabled, mgrPrefs.shiftJISShinjitaiOutputEnabled) {
|
||||
case (false, true): return vChewingKanjiConverter.cnvTradToJIS(text)
|
||||
case (true, false): return vChewingKanjiConverter.cnvTradToKangXi(text)
|
||||
// 本來這兩個開關不該同時開啟的,但萬一被開啟了的話就這樣處理:
|
||||
case (true, true): return vChewingKanjiConverter.cnvTradToJIS(text)
|
||||
case (false, false): return text
|
||||
}
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
// MARK: - 開關判定當前應用究竟是?
|
||||
|
||||
static var areWeUsingOurOwnPhraseEditor: Bool = false
|
||||
|
|
Loading…
Reference in New Issue