@@ -203,6 +203,61 @@ it('Keyboard syncInstanceInputs will work', () => {
203203 expect ( keyboard2 . getInput ( ) ) . toBe ( "123456" ) ;
204204} ) ;
205205
206+ it ( 'Keyboard syncInstanceInputs keeps the caret when another instance is pressed' , ( ) => {
207+ clearDOM ( ) ;
208+
209+ document . body . innerHTML = `
210+ <input class="input" />
211+ <div class="keyboard1"></div>
212+ <div class="keyboard2"></div>
213+ ` ;
214+
215+ const sharedOptions = { syncInstanceInputs : true } ;
216+ const keyboard1 = new Keyboard ( ".keyboard1" , sharedOptions ) ;
217+ const keyboard2 = new Keyboard ( ".keyboard2" , sharedOptions ) ;
218+
219+ const input = document . querySelector ( ".input" ) ;
220+ input . value = "abcdef" ;
221+ input . selectionStart = 3 ;
222+ input . selectionEnd = 3 ;
223+
224+ keyboard1 . caretEventHandler ( { target : input , type : "select" } ) ;
225+
226+ expect ( keyboard2 . getCaretPosition ( ) ) . toBe ( 3 ) ;
227+
228+ // A press on keyboard1 reaches the document as a click outside keyboard2. Only the
229+ // event path tells keyboard2 this came from a sibling instance rather than elsewhere.
230+ const pressed = keyboard1 . keyboardDOM . querySelector ( ".hg-button" ) ;
231+ keyboard2 . caretEventHandler ( {
232+ target : pressed ,
233+ type : "mouseup" ,
234+ composedPath : ( ) => [ pressed , keyboard1 . keyboardDOM , document . body ]
235+ } ) ;
236+
237+ expect ( keyboard2 . getCaretPosition ( ) ) . toBe ( 3 ) ;
238+ } ) ;
239+
240+ it ( 'Keyboard caretEventHandler drops the caret for a click outside any instance' , ( ) => {
241+ clearDOM ( ) ;
242+
243+ document . body . innerHTML = `
244+ <div class="outside"></div>
245+ <div class="keyboard1"></div>
246+ ` ;
247+
248+ const keyboard = new Keyboard ( ".keyboard1" , { syncInstanceInputs : true } ) ;
249+ keyboard . setCaretPosition ( 2 ) ;
250+
251+ const outside = document . querySelector ( ".outside" ) ;
252+ keyboard . caretEventHandler ( {
253+ target : outside ,
254+ type : "mouseup" ,
255+ composedPath : ( ) => [ outside , document . body ]
256+ } ) ;
257+
258+ expect ( keyboard . getCaretPosition ( ) ) . toBe ( null ) ;
259+ } ) ;
260+
206261it ( 'Keyboard onChange will work' , ( ) => {
207262 let output = false ;
208263
0 commit comments