Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tim van der Lippe | 58e9112 | 2020-02-04 13:06:47 | [diff] [blame^] | 5 | import * as Common from '../common/common.js'; |
| 6 | import * as QuickOpen from '../quick_open/quick_open.js'; |
| 7 | import * as Workspace from '../workspace/workspace.js'; // eslint-disable-line no-unused-vars |
| 8 | |
Tim van der Lippe | 22c8203 | 2020-01-14 17:00:51 | [diff] [blame] | 9 | import {evaluateScriptSnippet} from './ScriptSnippetFileSystem.js'; |
| 10 | |
Paul Lewis | a0eb361 | 2019-11-27 15:44:52 | [diff] [blame] | 11 | export default class SnippetsQuickOpen extends QuickOpen.FilteredListWidget.Provider { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 12 | constructor() { |
| 13 | super(); |
Tim van der Lippe | 58e9112 | 2020-02-04 13:06:47 | [diff] [blame^] | 14 | /** @type {!Array<!Workspace.UISourceCode.UISourceCode>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 15 | this._snippets = []; |
| 16 | } |
| 17 | /** |
| 18 | * @override |
| 19 | * @param {?number} itemIndex |
| 20 | * @param {string} promptValue |
| 21 | */ |
| 22 | selectItem(itemIndex, promptValue) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 23 | if (itemIndex === null) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 24 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 25 | } |
Tim van der Lippe | 22c8203 | 2020-01-14 17:00:51 | [diff] [blame] | 26 | evaluateScriptSnippet(this._snippets[itemIndex]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @override |
| 31 | * @param {string} query |
| 32 | * @return {string} |
| 33 | */ |
| 34 | notFoundText(query) { |
Tim van der Lippe | 58e9112 | 2020-02-04 13:06:47 | [diff] [blame^] | 35 | return Common.UIString.UIString('No snippets found.'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @override |
| 40 | */ |
| 41 | attach() { |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 42 | this._snippets = Snippets.project.uiSourceCodes(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @override |
| 47 | */ |
| 48 | detach() { |
| 49 | this._snippets = []; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * @override |
| 55 | * @return {number} |
| 56 | */ |
| 57 | itemCount() { |
| 58 | return this._snippets.length; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @override |
| 63 | * @param {number} itemIndex |
| 64 | * @return {string} |
| 65 | */ |
| 66 | itemKeyAt(itemIndex) { |
| 67 | return this._snippets[itemIndex].name(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @override |
| 72 | * @param {number} itemIndex |
| 73 | * @param {string} query |
| 74 | * @param {!Element} titleElement |
| 75 | * @param {!Element} subtitleElement |
| 76 | */ |
| 77 | renderItem(itemIndex, query, titleElement, subtitleElement) { |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 78 | titleElement.textContent = unescape(this._snippets[itemIndex].name()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 79 | titleElement.classList.add('monospace'); |
Tim van der Lippe | 58e9112 | 2020-02-04 13:06:47 | [diff] [blame^] | 80 | QuickOpen.FilteredListWidget.FilteredListWidget.highlightRanges(titleElement, query, true); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 81 | } |
Paul Lewis | a0eb361 | 2019-11-27 15:44:52 | [diff] [blame] | 82 | } |