Skip to content

Commit ccf58d9

Browse files
committed
fix: signavio#498 support IME with input hint alongの取り込み
signavio#499
1 parent 9db0524 commit ccf58d9

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

src/utils/applyChangeToValue.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,22 @@ const applyChangeToValue = (
6060

6161
// find start of diff
6262
spliceStart = 0
63-
while (plainTextValue[spliceStart] === controlPlainTextValue[spliceStart])
63+
while (plainTextValue[spliceStart] === oldPlainTextValue[spliceStart])
6464
spliceStart++
6565

66+
// find end of diff
67+
let spliceEndOfNew = plainTextValue.length
68+
let spliceEndOfOld = oldPlainTextValue.length
69+
while (plainTextValue[spliceEndOfNew -1] === oldPlainTextValue[spliceEndOfOld - 1]) {
70+
spliceEndOfNew--
71+
spliceEndOfOld--
72+
}
73+
6674
// extract auto-corrected insertion
67-
insert = plainTextValue.slice(spliceStart, selectionEndAfter)
75+
insert = plainTextValue.slice(spliceStart, spliceEndOfNew)
6876

6977
// find index of the unchanged remainder
70-
spliceEnd = oldPlainTextValue.lastIndexOf(
71-
plainTextValue.substring(selectionEndAfter)
72-
)
78+
spliceEnd = spliceEndOfOld >= spliceStart ? spliceEndOfOld : selectionEndAfter
7379

7480
// re-map the corrected indices
7581
mappedSpliceStart = mapPlainTextIndex(value, config, spliceStart, 'START')

src/utils/applyChangeToValue.spec.js

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,49 @@ describe('#applyChangeToValue', () => {
7070
)
7171
})
7272

73+
it('should correctly add characters with cursor in the middle at the end, beginning, and in the middle of text', () => {
74+
let changed = 'S[start]' + plainText
75+
let result = applyChangeToValue(
76+
value,
77+
changed,
78+
{
79+
selectionStartBefore: 0,
80+
selectionEndBefore: 0,
81+
selectionEndAfter: 1,
82+
},
83+
config
84+
)
85+
expect(result).toEqual('S[start]' + value)
86+
87+
changed = plainText + 'E[end]'
88+
result = applyChangeToValue(
89+
value,
90+
changed,
91+
{
92+
selectionStartBefore: plainText.length,
93+
selectionEndBefore: plainText.length,
94+
selectionEndAfter: plainText.length + 1,
95+
},
96+
config
97+
)
98+
expect(result).toEqual(value + 'E[end]')
99+
100+
changed = "Hi John Doe, \n\nlet's M[mid]add [email protected] to this conversation..."
101+
result = applyChangeToValue(
102+
value,
103+
changed,
104+
{
105+
selectionStartBefore: 21,
106+
selectionEndBefore: 21,
107+
selectionEndAfter: 22,
108+
},
109+
config
110+
)
111+
expect(result).toEqual(
112+
"Hi @[John Doe](user:johndoe), \n\nlet's M[mid]add @[[email protected]](email:[email protected]) to this conversation..."
113+
)
114+
})
115+
73116
it('should correctly delete single characters and ranges of selected text', () => {
74117
// delete "i"
75118
let changed =
@@ -133,6 +176,20 @@ describe('#applyChangeToValue', () => {
133176
config
134177
)
135178
expect(result).toEqual(value.replace('add', 'remove'))
179+
180+
// replace range with cursor in the middle, eg. remove|[remove]
181+
changed = plainText.replace('add', 'remove[remove]')
182+
result = applyChangeToValue(
183+
value,
184+
changed,
185+
{
186+
selectionStartBefore: plainText.indexOf('add'),
187+
selectionEndBefore: plainText.indexOf('add') + 'add'.length,
188+
selectionEndAfter: plainText.indexOf('add') + 'remove'.length,
189+
},
190+
config
191+
)
192+
expect(result).toEqual(value.replace('add', 'remove[remove]'))
136193
})
137194

138195
it('should remove mentions markup contained in deleted text ranges', () => {
@@ -257,7 +314,7 @@ describe('#applyChangeToValue', () => {
257314
})
258315

259316
it('should correctly handle text auto-correction', () => {
260-
const result = applyChangeToValue(
317+
let result = applyChangeToValue(
261318
'ill',
262319
"I'll",
263320
{
@@ -268,5 +325,20 @@ describe('#applyChangeToValue', () => {
268325
config
269326
)
270327
expect(result).toEqual("I'll")
328+
329+
// case like
330+
// input queue: s -> a -> d
331+
// IME queue(| is the cursor position): s|[sa] -> s'a|[sa'a] -> sad|[sad]
332+
result = applyChangeToValue(
333+
"s'a[sa'a]",
334+
"sad[sad]",
335+
{
336+
selectionStartBefore: 2,
337+
selectionEndBefore: 2,
338+
selectionEndAfter: 3,
339+
},
340+
config
341+
)
342+
expect(result).toEqual("sad[sad]")
271343
})
272344
})

0 commit comments

Comments
 (0)