diff --git a/Source/Modules/ControllerModules/InputState.swift b/Source/Modules/ControllerModules/InputState.swift index 0b9b5d24..ee285409 100644 --- a/Source/Modules/ControllerModules/InputState.swift +++ b/Source/Modules/ControllerModules/InputState.swift @@ -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( diff --git a/Source/Modules/ControllerModules/KeyHandler_Core.swift b/Source/Modules/ControllerModules/KeyHandler_Core.swift index 1cf5db7b..604d7845 100644 --- a/Source/Modules/ControllerModules/KeyHandler_Core.swift +++ b/Source/Modules/ControllerModules/KeyHandler_Core.swift @@ -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 - } } diff --git a/Source/Modules/ControllerModules/ctlInputMethod_Core.swift b/Source/Modules/ControllerModules/ctlInputMethod_Core.swift index c9d35bd9..55d8161e 100644 --- a/Source/Modules/ControllerModules/ctlInputMethod_Core.swift +++ b/Source/Modules/ControllerModules/ctlInputMethod_Core.swift @@ -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 } diff --git a/Source/Modules/IMEModules/IME.swift b/Source/Modules/IMEModules/IME.swift index 47c35d22..05935c86 100644 --- a/Source/Modules/IMEModules/IME.swift +++ b/Source/Modules/IMEModules/IME.swift @@ -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