Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) IBM Corp. 2009 All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 15 | * its contributors may be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
Jan Scheffler | 4a89ed3 | 2020-07-29 16:19:13 | [diff] [blame] | 30 | |
Tim van der Lippe | 18f0489 | 2020-03-17 11:39:40 | [diff] [blame] | 31 | import * as TextUtils from '../text_utils/text_utils.js'; // eslint-disable-line no-unused-vars |
Tim van der Lippe | 2543c5c | 2020-02-04 11:58:07 | [diff] [blame] | 32 | import * as UI from '../ui/ui.js'; |
| 33 | |
Paul Lewis | aa437a5 | 2020-01-15 15:11:30 | [diff] [blame] | 34 | import {SourceFrameImpl} from './SourceFrame.js'; |
| 35 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 36 | /** |
| 37 | * @unrestricted |
| 38 | */ |
Paul Lewis | aa437a5 | 2020-01-15 15:11:30 | [diff] [blame] | 39 | export class ResourceSourceFrame extends SourceFrameImpl { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 40 | /** |
Tim van der Lippe | 18f0489 | 2020-03-17 11:39:40 | [diff] [blame] | 41 | * @param {!TextUtils.ContentProvider.ContentProvider} resource |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 42 | * @param {boolean=} autoPrettyPrint |
Joey Arhar | 19e85cc | 2019-01-29 22:07:02 | [diff] [blame] | 43 | * @param {!UI.TextEditor.Options=} codeMirrorOptions |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 44 | */ |
Joey Arhar | 19e85cc | 2019-01-29 22:07:02 | [diff] [blame] | 45 | constructor(resource, autoPrettyPrint, codeMirrorOptions) { |
Ingvar Stepanyan | 1c77184 | 2018-10-10 14:35:08 | [diff] [blame] | 46 | super(async () => { |
Rob Paveza | 2eb8c14 | 2019-10-13 18:02:38 | [diff] [blame] | 47 | let content = (await resource.requestContent()).content || ''; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 48 | if (await resource.contentEncoded()) { |
Ingvar Stepanyan | 1c77184 | 2018-10-10 14:35:08 | [diff] [blame] | 49 | content = window.atob(content); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 50 | } |
Rob Paveza | 2eb8c14 | 2019-10-13 18:02:38 | [diff] [blame] | 51 | return {content, isEncoded: false}; |
Joey Arhar | 19e85cc | 2019-01-29 22:07:02 | [diff] [blame] | 52 | }, codeMirrorOptions); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 53 | this._resource = resource; |
| 54 | this.setCanPrettyPrint(this._resource.contentType().isDocumentOrScriptOrStyleSheet(), autoPrettyPrint); |
| 55 | } |
| 56 | |
| 57 | /** |
Tim van der Lippe | 18f0489 | 2020-03-17 11:39:40 | [diff] [blame] | 58 | * @param {!TextUtils.ContentProvider.ContentProvider} resource |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 59 | * @param {string} highlighterType |
| 60 | * @param {boolean=} autoPrettyPrint |
Tim van der Lippe | 2543c5c | 2020-02-04 11:58:07 | [diff] [blame] | 61 | * @return {!UI.Widget.Widget} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 62 | */ |
| 63 | static createSearchableView(resource, highlighterType, autoPrettyPrint) { |
Tim van der Lippe | b88663f | 2019-11-07 15:53:21 | [diff] [blame] | 64 | return new SearchableContainer(resource, highlighterType, autoPrettyPrint); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | get resource() { |
| 68 | return this._resource; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @override |
Tim van der Lippe | 2543c5c | 2020-02-04 11:58:07 | [diff] [blame] | 73 | * @param {!UI.ContextMenu.ContextMenu} contextMenu |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 74 | * @param {number} lineNumber |
| 75 | * @param {number} columnNumber |
Andres Olivares | c2d30bd | 2020-10-25 23:35:18 | [diff] [blame] | 76 | * @return {!Promise<void>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 77 | */ |
| 78 | populateTextAreaContextMenu(contextMenu, lineNumber, columnNumber) { |
| 79 | contextMenu.appendApplicableItems(this._resource); |
| 80 | return Promise.resolve(); |
| 81 | } |
Tim van der Lippe | b88663f | 2019-11-07 15:53:21 | [diff] [blame] | 82 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 83 | |
Tim van der Lippe | 2543c5c | 2020-02-04 11:58:07 | [diff] [blame] | 84 | export class SearchableContainer extends UI.Widget.VBox { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 85 | /** |
Tim van der Lippe | 18f0489 | 2020-03-17 11:39:40 | [diff] [blame] | 86 | * @param {!TextUtils.ContentProvider.ContentProvider} resource |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 87 | * @param {string} highlighterType |
| 88 | * @param {boolean=} autoPrettyPrint |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 89 | */ |
| 90 | constructor(resource, highlighterType, autoPrettyPrint) { |
| 91 | super(true); |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 92 | this.registerRequiredCSS('source_frame/resourceSourceFrame.css', {enableLegacyPatching: true}); |
Tim van der Lippe | b88663f | 2019-11-07 15:53:21 | [diff] [blame] | 93 | const sourceFrame = new ResourceSourceFrame(resource, autoPrettyPrint); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 94 | this._sourceFrame = sourceFrame; |
| 95 | sourceFrame.setHighlighterType(highlighterType); |
Tim van der Lippe | 2543c5c | 2020-02-04 11:58:07 | [diff] [blame] | 96 | const searchableView = new UI.SearchableView.SearchableView(sourceFrame); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 97 | searchableView.element.classList.add('searchable-view'); |
| 98 | searchableView.setPlaceholder(ls`Find`); |
| 99 | sourceFrame.show(searchableView.element); |
| 100 | sourceFrame.setSearchableView(searchableView); |
| 101 | searchableView.show(this.contentElement); |
| 102 | |
Tim van der Lippe | 2543c5c | 2020-02-04 11:58:07 | [diff] [blame] | 103 | const toolbar = new UI.Toolbar.Toolbar('toolbar', this.contentElement); |
Simon Zünd | 308c74f | 2020-01-16 06:53:37 | [diff] [blame] | 104 | sourceFrame.toolbarItems().then(items => { |
| 105 | items.map(item => toolbar.appendToolbarItem(item)); |
| 106 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @param {number} lineNumber |
| 111 | * @param {number=} columnNumber |
| 112 | */ |
| 113 | async revealPosition(lineNumber, columnNumber) { |
| 114 | this._sourceFrame.revealPosition(lineNumber, columnNumber, true); |
| 115 | } |
Tim van der Lippe | b88663f | 2019-11-07 15:53:21 | [diff] [blame] | 116 | } |