InputState // Also segmentalize the buffer when typing reading.

This commit is contained in:
ShikiSuen 2022-08-05 09:33:02 +08:00
parent 9699580b57
commit 424b3791f8
1 changed files with 22 additions and 12 deletions

View File

@ -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() {