From 030b28ccdb3dfde8a51a12e07e72fb525582978f Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 23 Aug 2022 11:05:32 +0800 Subject: [PATCH] KeyHandler // +previousParsableReading(). --- .../ControllerModules/KeyHandler_Core.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Source/Modules/ControllerModules/KeyHandler_Core.swift b/Source/Modules/ControllerModules/KeyHandler_Core.swift index a6963c66..5f0112c6 100644 --- a/Source/Modules/ControllerModules/KeyHandler_Core.swift +++ b/Source/Modules/ControllerModules/KeyHandler_Core.swift @@ -364,6 +364,23 @@ public class KeyHandler { composer.phonabetCombinationCorrectionEnabled = mgrPrefs.autoCorrectReadingCombination } + /// 返回前一個游標位置的可解析的漢字讀音。 + /// 返回的內容分別是:「完整讀音」「去掉聲調的讀音」「是否有聲調」。 + var previousParsableReading: (String, String, Bool)? { + if compositor.cursor == 0 { return nil } + let cursorPrevious = max(compositor.cursor - 1, 0) + let rawData = compositor.keys[cursorPrevious] + let components = rawData.charComponents + var hasIntonation = false + for neta in components { + if !Tekkon.allowedPhonabets.contains(neta) || neta == " " { return nil } + if Tekkon.allowedIntonations.contains(neta) { hasIntonation = true } + } + if hasIntonation, components.count == 1 { return nil } // 剔除純聲調之情形 + let rawDataSansIntonation = hasIntonation ? components.dropLast(1).joined() : rawData + return (rawData, rawDataSansIntonation, hasIntonation) + } + // MARK: - Extracted methods and functions (Megrez). /// 將輸入法偏好設定同步至語言模組內。