blob: 655ff8bc0eeb47f5f5f473a3fb7b853b74400d0d [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// 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 Lippe58e91122020-02-04 13:06:475import * as Common from '../common/common.js';
6import * as QuickOpen from '../quick_open/quick_open.js';
7import * as Workspace from '../workspace/workspace.js'; // eslint-disable-line no-unused-vars
8
Tim van der Lippe22c82032020-01-14 17:00:519import {evaluateScriptSnippet} from './ScriptSnippetFileSystem.js';
10
Tim van der Lippe046a8d32020-03-17 14:48:3511export class SnippetsQuickOpen extends QuickOpen.FilteredListWidget.Provider {
Blink Reformat4c46d092018-04-07 15:32:3712 constructor() {
13 super();
Tim van der Lippe58e91122020-02-04 13:06:4714 /** @type {!Array<!Workspace.UISourceCode.UISourceCode>} */
Blink Reformat4c46d092018-04-07 15:32:3715 this._snippets = [];
16 }
17 /**
18 * @override
19 * @param {?number} itemIndex
20 * @param {string} promptValue
21 */
22 selectItem(itemIndex, promptValue) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3423 if (itemIndex === null) {
Blink Reformat4c46d092018-04-07 15:32:3724 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3425 }
Tim van der Lippe22c82032020-01-14 17:00:5126 evaluateScriptSnippet(this._snippets[itemIndex]);
Blink Reformat4c46d092018-04-07 15:32:3727 }
28
29 /**
30 * @override
31 * @param {string} query
32 * @return {string}
33 */
34 notFoundText(query) {
Tim van der Lippe58e91122020-02-04 13:06:4735 return Common.UIString.UIString('No snippets found.');
Blink Reformat4c46d092018-04-07 15:32:3736 }
37
38 /**
39 * @override
40 */
41 attach() {
Alexey Kozyatinskiy3a2eb282018-08-21 16:22:0242 this._snippets = Snippets.project.uiSourceCodes();
Blink Reformat4c46d092018-04-07 15:32:3743 }
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) {
Simon Zünda292e762020-07-23 19:22:1078 titleElement.textContent = unescape(this._snippets[itemIndex].name());
Blink Reformat4c46d092018-04-07 15:32:3779 titleElement.classList.add('monospace');
Tim van der Lippe58e91122020-02-04 13:06:4780 QuickOpen.FilteredListWidget.FilteredListWidget.highlightRanges(titleElement, query, true);
Blink Reformat4c46d092018-04-07 15:32:3781 }
Paul Lewisa0eb3612019-11-27 15:44:5282}