| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 // Copyright 2015 The Chromium Authors. All rights reserved. | 2 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be |
| 4 // found in the LICENSE file. | 4 // found in the LICENSE file. |
| 5 --> | 5 --> |
| 6 <import src="/sky/framework/sky-element/sky-element.sky" as="SkyElement" /> | 6 <import src="/sky/framework/sky-element/sky-element.sky" as="SkyElement" /> |
| 7 | 7 |
| 8 <sky-element name="sky-input" attributes="value:string"> | 8 <sky-element name="sky-input" attributes="value:string"> |
| 9 <template> | 9 <template> |
| 10 <style> | 10 <style> |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 white-space: nowrap; | 21 white-space: nowrap; |
| 22 overflow: hidden; | 22 overflow: hidden; |
| 23 } | 23 } |
| 24 </style> | 24 </style> |
| 25 <div id="control" contenteditable on-keydown="handleKeyDown">{{ value }}</div> | 25 <div id="control" contenteditable on-keydown="handleKeyDown">{{ value }}</div> |
| 26 </template> | 26 </template> |
| 27 <script> | 27 <script> |
| 28 module.exports = class extends SkyElement { | 28 module.exports = class extends SkyElement { |
| 29 shadowRootReady() { | 29 shadowRootReady() { |
| 30 var control = this.shadowRoot.getElementById('control'); | 30 var control = this.shadowRoot.getElementById('control'); |
| 31 var text = control.firstChild; |
| 31 | 32 |
| 32 var observer = new MutationObserver(function() { | 33 var observer = new MutationObserver(function() { |
| 34 // contenteditable might remove the text node, but we need to keep it |
| 35 // since that's where the data binding is connected to. |
| 36 if (!text.parentNode) |
| 37 control.appendChild(text); |
| 38 if (this.value == control.textContent) |
| 39 return; |
| 33 this.value = control.textContent; | 40 this.value = control.textContent; |
| 34 this.dispatchEvent(new CustomEvent('change', { | 41 this.dispatchEvent(new CustomEvent('change', { |
| 35 bubbles: true, | 42 bubbles: true, |
| 36 })); | 43 })); |
| 37 }.bind(this)); | 44 }.bind(this)); |
| 38 | 45 |
| 39 observer.observe(control, { | 46 observer.observe(control, { |
| 40 subtree: true, | 47 subtree: true, |
| 41 characterData: true, | 48 characterData: true, |
| 42 childList: true, | 49 childList: true, |
| 43 }); | 50 }); |
| 44 } | 51 } |
| 45 handleKeyDown(event) { | 52 handleKeyDown(event) { |
| 46 // TODO(abarth): You can still get newlines if the user pastes them. | 53 // TODO(abarth): You can still get newlines if the user pastes them. |
| 47 if (event.keyCode == 0xD) | 54 if (event.keyCode == 0xD) |
| 48 event.preventDefault(); | 55 event.preventDefault(); |
| 49 } | 56 } |
| 50 }.register(); | 57 }.register(); |
| 51 </script> | 58 </script> |
| 52 </sky-element> | 59 </sky-element> |
| OLD | NEW |