blob: a22043cd38b2411c7f1366d378ce8097c530717a [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
Jan Schefflercde20c72020-07-30 11:10:365
Tim van der Lippe58e91122020-02-04 13:06:476import * as Common from '../common/common.js';
7import * as QuickOpen from '../quick_open/quick_open.js';
8import * as Workspace from '../workspace/workspace.js'; // eslint-disable-line no-unused-vars
9
Andres Olivaresea2a4ad2020-10-25 23:08:0710import {evaluateScriptSnippet, project} from './ScriptSnippetFileSystem.js';
Tim van der Lippe22c82032020-01-14 17:00:5111
Tim van der Lippe046a8d32020-03-17 14:48:3512export class SnippetsQuickOpen extends QuickOpen.FilteredListWidget.Provider {
Blink Reformat4c46d092018-04-07 15:32:3713 constructor() {
14 super();
Tim van der Lippe58e91122020-02-04 13:06:4715 /** @type {!Array<!Workspace.UISourceCode.UISourceCode>} */
Blink Reformat4c46d092018-04-07 15:32:3716 this._snippets = [];
17 }
18 /**
19 * @override
20 * @param {?number} itemIndex
21 * @param {string} promptValue
22 */
23 selectItem(itemIndex, promptValue) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3424 if (itemIndex === null) {
Blink Reformat4c46d092018-04-07 15:32:3725 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3426 }
Tim van der Lippe22c82032020-01-14 17:00:5127 evaluateScriptSnippet(this._snippets[itemIndex]);
Blink Reformat4c46d092018-04-07 15:32:3728 }
29
30 /**
31 * @override
32 * @param {string} query
33 * @return {string}
34 */
35 notFoundText(query) {
Tim van der Lippe58e91122020-02-04 13:06:4736 return Common.UIString.UIString('No snippets found.');
Blink Reformat4c46d092018-04-07 15:32:3737 }
38
39 /**
40 * @override
41 */
42 attach() {
Andres Olivaresea2a4ad2020-10-25 23:08:0743 this._snippets = project.uiSourceCodes();
Blink Reformat4c46d092018-04-07 15:32:3744 }
45
46 /**
47 * @override
48 */
49 detach() {
50 this._snippets = [];
51 }
52
53
54 /**
55 * @override
56 * @return {number}
57 */
58 itemCount() {
59 return this._snippets.length;
60 }
61
62 /**
63 * @override
64 * @param {number} itemIndex
65 * @return {string}
66 */
67 itemKeyAt(itemIndex) {
68 return this._snippets[itemIndex].name();
69 }
70
71 /**
72 * @override
73 * @param {number} itemIndex
74 * @param {string} query
75 * @param {!Element} titleElement
76 * @param {!Element} subtitleElement
77 */
78 renderItem(itemIndex, query, titleElement, subtitleElement) {
Simon Zünda292e762020-07-23 19:22:1079 titleElement.textContent = unescape(this._snippets[itemIndex].name());
Blink Reformat4c46d092018-04-07 15:32:3780 titleElement.classList.add('monospace');
Tim van der Lippe58e91122020-02-04 13:06:4781 QuickOpen.FilteredListWidget.FilteredListWidget.highlightRanges(titleElement, query, true);
Blink Reformat4c46d092018-04-07 15:32:3782 }
Paul Lewisa0eb3612019-11-27 15:44:5283}