| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<!-- |
| 4 |
Copyright 2010 The Closure Library Authors. All Rights Reserved. |
| 5 |
|
| 6 |
Use of this source code is governed by the Apache License, Version 2.0. |
| 7 |
See the COPYING file for details. |
| 8 |
--> |
| 9 |
<head> |
| 10 |
<title></title> |
| 11 |
<style> |
| 12 |
|
| 13 |
label { |
| 14 |
display: block; |
| 15 |
} |
| 16 |
|
| 17 |
</style> |
| 18 |
<script src="../base.js"></script> |
| 19 |
<script> |
| 20 |
goog.require('goog.dom'); |
| 21 |
goog.require('goog.dom.selection'); |
| 22 |
</script> |
| 23 |
</head> |
| 24 |
<body> |
| 25 |
|
| 26 |
<script> |
| 27 |
|
| 28 |
var $ = goog.dom.getElement; |
| 29 |
|
| 30 |
var isSyncing = false; |
| 31 |
|
| 32 |
function update(i) { |
| 33 |
isSyncing = true; |
| 34 |
var selectionStart = $('selectionStart' + i).value; |
| 35 |
var selectionEnd = $('selectionEnd' + i).value; |
| 36 |
var selectionText = $('selectionText' + i).value; |
| 37 |
var textField = $('textField' + i); |
| 38 |
textField.focus(); |
| 39 |
|
| 40 |
if (!isNaN(selectionStart)) { |
| 41 |
goog.dom.selection.setStart(textField, selectionStart); |
| 42 |
} |
| 43 |
|
| 44 |
if (!isNaN(selectionEnd)) { |
| 45 |
goog.dom.selection.setEnd(textField, selectionEnd); |
| 46 |
} |
| 47 |
|
| 48 |
selection.setText(textField, selectionText); |
| 49 |
isSyncing = false; |
| 50 |
sync(i); |
| 51 |
} |
| 52 |
|
| 53 |
function sync(i) { |
| 54 |
isSyncing = true; |
| 55 |
var textField = $('textField' + i); |
| 56 |
$('selectionStart' + i).value = goog.dom.selection.getStart(textField); |
| 57 |
$('selectionEnd' + i).value = goog.dom.selection.getEnd(textField); |
| 58 |
$('selectionText' + i).value = goog.dom.selection.getText(textField); |
| 59 |
isSyncing = false; |
| 60 |
} |
| 61 |
|
| 62 |
</script> |
| 63 |
|
| 64 |
<label>selectionStart <input type=text id=selectionStart1></label> |
| 65 |
<label>selectionEnd <input type=text id=selectionEnd1></label> |
| 66 |
<label>selectionText <input type=text id=selectionText1></label> |
| 67 |
<button onclick="update(1)">Update Textarea</button><br> |
| 68 |
|
| 69 |
<textarea id=textField1 onkeydown="sync(1)" onkeyup="sync(1)"></textarea> |
| 70 |
|
| 71 |
|
| 72 |
<label>selectionStart <input type=text id=selectionStart2></label> |
| 73 |
<label>selectionEnd <input type=text id=selectionEnd2></label> |
| 74 |
<label>selectionText <input type=text id=selectionText2></label> |
| 75 |
<button onclick="update(2)">Update Input</button><br> |
| 76 |
<input type=text id=textField2 onkeydown="sync(2)" onkeyup="sync(2)"> |
| 77 |
|
| 78 |
<script> |
| 79 |
|
| 80 |
window.onload = function() { |
| 81 |
sync(1); |
| 82 |
sync(2); |
| 83 |
}; |
| 84 |
|
| 85 |
</script> |
| 86 |
|
| 87 |
</body> |
| 88 |
</html> |