* Respond to editor and filter input
()
| 88 | */ |
| 89 | |
| 90 | handleEvents() { |
| 91 | this.formatOnNextChange = false |
| 92 | |
| 93 | // Change event triggers input validation |
| 94 | this.editor.on('change', () => { |
| 95 | this.validate() |
| 96 | if (this.formatOnNextChange) { |
| 97 | this.format() |
| 98 | this.formatOnNextChange = false |
| 99 | } |
| 100 | }) |
| 101 | |
| 102 | // Paste (Cmd / Cntrl + v) triggers input validation and auto-format |
| 103 | this.editor.on('inputRead', (cm, e) => { |
| 104 | if (e.origin === 'paste') { |
| 105 | const pasted = e.text[0] |
| 106 | |
| 107 | // If pasted text looks like url, try to download it |
| 108 | if (isUrl(pasted)) { |
| 109 | superagent.get(pasted).end((err, res) => { |
| 110 | if (!err && res.body) { |
| 111 | this.formatOnNextChange = true |
| 112 | this.editor.setValue(JSON.stringify(res.body)) |
| 113 | } |
| 114 | }) |
| 115 | } else { |
| 116 | |
| 117 | // Validate and format the input |
| 118 | this.validate() |
| 119 | this.format() |
| 120 | } |
| 121 | } |
| 122 | }) |
| 123 | |
| 124 | // File drop event (editor's value has not been changed yet) |
| 125 | this.editor.on('drop', () => { |
| 126 | this.formatOnNextChange = true |
| 127 | }) |
| 128 | |
| 129 | // Run the filter as the user types |
| 130 | this.filter.on('keyup', () => { |
| 131 | this.runFilter() |
| 132 | }) |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Validate editor input. Emits 'input-valid' and sets this.data. |