OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style license that can be |
| 4 found in the LICENSE file. |
| 5 --> |
| 6 |
| 7 <link rel="import" href="../bower_components/polymer/polymer.html"> |
| 8 |
| 9 <script src="third_party/ace/ace.js"></script> |
| 10 <script src="third_party/ace/ext-language_tools.js"></script> |
| 11 <script src="third_party/ace/mode-json.js"></script> |
| 12 |
| 13 <link rel="import" href="rpc-completer.html"> |
| 14 |
| 15 <!-- The `rpc-editor` is an Ace editor for JSON with optional autocomplete --> |
| 16 <dom-module id="rpc-editor"> |
| 17 <template> |
| 18 <style> |
| 19 :host, pre { |
| 20 display: block; |
| 21 height: 100%; |
| 22 } |
| 23 </style> |
| 24 <pre id="editor" class="editor"></pre> |
| 25 <rpc-completer id="completer" |
| 26 description="[[description]]" root-type-name="[[rootTypeName]]"> |
| 27 </rpc-completer> |
| 28 </template> |
| 29 |
| 30 <script> |
| 31 'use strict'; |
| 32 |
| 33 Polymer({ |
| 34 is: 'rpc-editor', |
| 35 |
| 36 properties: { |
| 37 value: { |
| 38 type: String, |
| 39 notify: true, |
| 40 observer: '_onValueChanged' |
| 41 }, |
| 42 |
| 43 editor: { |
| 44 type: Object, |
| 45 readOnly: true |
| 46 }, |
| 47 |
| 48 /** @type {FileDescriptorSet} */ |
| 49 description: Object, |
| 50 |
| 51 rootTypeName: String |
| 52 }, |
| 53 |
| 54 ready: function() { |
| 55 var self = this; |
| 56 this._setEditor(ace.edit(this.$.editor)); |
| 57 this.editor.session.setMode('ace/mode/json'); |
| 58 |
| 59 // Set and sync text; |
| 60 this.editor.session.setValue(this.value || ''); |
| 61 this.editor.session.on('change', function() { |
| 62 var text = self.editor.session.getValue(); |
| 63 if (text !== self.value) { |
| 64 self._maybeSync(function() { |
| 65 self.value = text; |
| 66 }); |
| 67 } |
| 68 }); |
| 69 |
| 70 this.editor.commands.removeCommands(['gotoline', 'find']); |
| 71 |
| 72 this.editor.setOptions({ |
| 73 enableBasicAutocompletion: [this.$.completer], |
| 74 }); |
| 75 }, |
| 76 |
| 77 _onValueChanged: function(newVal) { |
| 78 var newText = newVal || ''; |
| 79 if (this.editor && this.editor.session.getValue() != newText) { |
| 80 this._maybeSync(function() { |
| 81 var selection = this.editor.selection; |
| 82 var origRange = selection.getRange(); |
| 83 this.editor.session.setValue(newText); |
| 84 // Restore cursor position, in a lame way, best effort. |
| 85 selection.setSelectionRange(origRange); |
| 86 }) |
| 87 } |
| 88 }, |
| 89 |
| 90 _maybeSync: function(action) { |
| 91 if (this._syncing) { |
| 92 return; |
| 93 } |
| 94 this._syncing = true; |
| 95 try { |
| 96 action.call(this); |
| 97 } finally { |
| 98 this._syncing = false; |
| 99 } |
| 100 } |
| 101 }); |
| 102 </script> |
| 103 </dom-module> |
OLD | NEW |