InputState // Also segmentalize the buffer when typing reading.
This commit is contained in:
parent
9699580b57
commit
424b3791f8
|
@ -174,24 +174,34 @@ public enum InputState {
|
|||
init(composingBuffer: String, cursorIndex: Int, reading: String = "", nodeValuesArray: [String] = []) {
|
||||
self.composingBuffer = composingBuffer
|
||||
self.reading = reading
|
||||
self.nodeValuesArray = nodeValuesArray
|
||||
// 為了簡化運算,將 reading 本身也變成一個字詞節點。
|
||||
if !reading.isEmpty {
|
||||
var newNodeValuesArray = [String]()
|
||||
var temporaryNode = ""
|
||||
var charCounter = 0
|
||||
for node in nodeValuesArray {
|
||||
for char in node {
|
||||
if charCounter == cursorIndex - reading.utf16.count {
|
||||
newNodeValuesArray.append(temporaryNode)
|
||||
temporaryNode = ""
|
||||
newNodeValuesArray.append(reading)
|
||||
}
|
||||
temporaryNode += String(char)
|
||||
charCounter += 1
|
||||
}
|
||||
newNodeValuesArray.append(temporaryNode)
|
||||
temporaryNode = ""
|
||||
}
|
||||
self.nodeValuesArray = newNodeValuesArray
|
||||
} else {
|
||||
self.nodeValuesArray = nodeValuesArray
|
||||
}
|
||||
defer { self.cursorIndex = cursorIndex }
|
||||
}
|
||||
|
||||
var attributedString: NSMutableAttributedString {
|
||||
/// 考慮到因為滑鼠點擊等其它行為導致的組字區內容遞交情況,
|
||||
/// 這裡對組字區內容也加上康熙字轉換或者 JIS 漢字轉換處理。
|
||||
guard reading.isEmpty else {
|
||||
let attributedString = NSMutableAttributedString(
|
||||
string: composingBufferConverted,
|
||||
attributes: [
|
||||
/// 不能用 .thick,否則會看不到游標。
|
||||
.underlineStyle: NSUnderlineStyle.single.rawValue,
|
||||
.markedClauseSegment: 0,
|
||||
]
|
||||
)
|
||||
return attributedString
|
||||
}
|
||||
let attributedString = NSMutableAttributedString(string: composingBufferConverted)
|
||||
var newBegin = 0
|
||||
for (i, neta) in nodeValuesArray.enumerated() {
|
||||
|
|
Loading…
Reference in New Issue