Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright 2016 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 Scheffler | 99f71f1 | 2021-02-10 21:00:02 | [diff] [blame] | 5 | /* eslint-disable rulesdir/no_underscored_properties */ |
| 6 | |
Tim van der Lippe | f18c6a2 | 2020-01-24 14:15:05 | [diff] [blame] | 7 | import * as Common from '../common/common.js'; |
| 8 | import * as Components from '../components/components.js'; |
Christy Chen | ae8efc5 | 2020-12-15 01:06:32 | [diff] [blame] | 9 | import * as i18n from '../i18n/i18n.js'; |
Tim van der Lippe | 213266c | 2021-01-18 15:48:31 | [diff] [blame] | 10 | import * as Platform from '../platform/platform.js'; |
Tim van der Lippe | f18c6a2 | 2020-01-24 14:15:05 | [diff] [blame] | 11 | import * as UI from '../ui/ui.js'; |
| 12 | import * as Workspace from '../workspace/workspace.js'; |
| 13 | |
Paul Lewis | 6196e75 | 2020-01-13 15:15:59 | [diff] [blame] | 14 | import {FileSystemWorkspaceBinding} from './FileSystemWorkspaceBinding.js'; |
Tim van der Lippe | a76954f | 2020-08-26 15:56:25 | [diff] [blame] | 15 | import {NetworkPersistenceManager} from './NetworkPersistenceManager.js'; |
Tim van der Lippe | bfbb58f | 2021-02-25 17:34:19 | [diff] [blame^] | 16 | import type {PersistenceBinding} from './PersistenceImpl.js'; |
| 17 | import {Events, PersistenceImpl} from './PersistenceImpl.js'; |
Paul Lewis | 6196e75 | 2020-01-13 15:15:59 | [diff] [blame] | 18 | |
Christy Chen | ae8efc5 | 2020-12-15 01:06:32 | [diff] [blame] | 19 | export const UIStrings = { |
| 20 | /** |
| 21 | *@description Text in Persistence Utils of the Workspace settings in Settings |
| 22 | *@example {example.url} PH1 |
| 23 | */ |
| 24 | linkedToSourceMapS: 'Linked to source map: {PH1}', |
| 25 | /** |
| 26 | *@description Text to show something is linked to another |
| 27 | *@example {example.url} PH1 |
| 28 | */ |
| 29 | linkedToS: 'Linked to {PH1}', |
| 30 | }; |
Jan Scheffler | 99f71f1 | 2021-02-10 21:00:02 | [diff] [blame] | 31 | const str_ = i18n.i18n.registerUIStrings('persistence/PersistenceUtils.ts', UIStrings); |
Christy Chen | ae8efc5 | 2020-12-15 01:06:32 | [diff] [blame] | 32 | const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
Paul Lewis | 6196e75 | 2020-01-13 15:15:59 | [diff] [blame] | 33 | export class PersistenceUtils { |
Jan Scheffler | 99f71f1 | 2021-02-10 21:00:02 | [diff] [blame] | 34 | static tooltipForUISourceCode(uiSourceCode: Workspace.UISourceCode.UISourceCode): string { |
Tim van der Lippe | cc37f9b | 2020-09-03 11:38:09 | [diff] [blame] | 35 | const binding = PersistenceImpl.instance().binding(uiSourceCode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 36 | if (!binding) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 37 | return ''; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 38 | } |
| 39 | if (uiSourceCode === binding.network) { |
Paul Lewis | 6196e75 | 2020-01-13 15:15:59 | [diff] [blame] | 40 | return FileSystemWorkspaceBinding.tooltipForUISourceCode(binding.fileSystem); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 41 | } |
| 42 | if (binding.network.contentType().isFromSourceMap()) { |
Tim van der Lippe | 213266c | 2021-01-18 15:48:31 | [diff] [blame] | 43 | return i18nString( |
| 44 | UIStrings.linkedToSourceMapS, {PH1: Platform.StringUtilities.trimMiddle(binding.network.url(), 150)}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 45 | } |
Tim van der Lippe | 213266c | 2021-01-18 15:48:31 | [diff] [blame] | 46 | return i18nString(UIStrings.linkedToS, {PH1: Platform.StringUtilities.trimMiddle(binding.network.url(), 150)}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 47 | } |
| 48 | |
Jan Scheffler | 99f71f1 | 2021-02-10 21:00:02 | [diff] [blame] | 49 | static iconForUISourceCode(uiSourceCode: Workspace.UISourceCode.UISourceCode): UI.Icon.Icon|null { |
Tim van der Lippe | cc37f9b | 2020-09-03 11:38:09 | [diff] [blame] | 50 | const binding = PersistenceImpl.instance().binding(uiSourceCode); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 51 | if (binding) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 52 | if (!binding.fileSystem.url().startsWith('file://')) { |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 53 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 54 | } |
Tim van der Lippe | f18c6a2 | 2020-01-24 14:15:05 | [diff] [blame] | 55 | const icon = UI.Icon.Icon.create('mediumicon-file-sync'); |
Tim van der Lippe | 70842f3 | 2020-11-23 16:56:57 | [diff] [blame] | 56 | UI.Tooltip.Tooltip.install(icon, PersistenceUtils.tooltipForUISourceCode(binding.network)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 57 | // TODO(allada) This will not work properly with dark theme. |
Tim van der Lippe | a76954f | 2020-08-26 15:56:25 | [diff] [blame] | 58 | if (NetworkPersistenceManager.instance().project() === binding.fileSystem.project()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 59 | icon.style.filter = 'hue-rotate(160deg)'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 60 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 61 | return icon; |
| 62 | } |
Tim van der Lippe | f18c6a2 | 2020-01-24 14:15:05 | [diff] [blame] | 63 | if (uiSourceCode.project().type() !== Workspace.Workspace.projectTypes.FileSystem || |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 64 | !uiSourceCode.url().startsWith('file://')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 65 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 66 | } |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 67 | |
Tim van der Lippe | f18c6a2 | 2020-01-24 14:15:05 | [diff] [blame] | 68 | const icon = UI.Icon.Icon.create('mediumicon-file'); |
Tim van der Lippe | 70842f3 | 2020-11-23 16:56:57 | [diff] [blame] | 69 | UI.Tooltip.Tooltip.install(icon, PersistenceUtils.tooltipForUISourceCode(uiSourceCode)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 70 | return icon; |
| 71 | } |
Tim van der Lippe | daf1627 | 2019-10-09 15:43:43 | [diff] [blame] | 72 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 73 | |
Jan Scheffler | 99f71f1 | 2021-02-10 21:00:02 | [diff] [blame] | 74 | export class LinkDecorator extends Common.ObjectWrapper.ObjectWrapper implements Components.Linkifier.LinkDecorator { |
| 75 | constructor(persistence: PersistenceImpl) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 76 | super(); |
Paul Lewis | 6196e75 | 2020-01-13 15:15:59 | [diff] [blame] | 77 | persistence.addEventListener(Events.BindingCreated, this._bindingChanged, this); |
| 78 | persistence.addEventListener(Events.BindingRemoved, this._bindingChanged, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 79 | } |
| 80 | |
Jan Scheffler | 99f71f1 | 2021-02-10 21:00:02 | [diff] [blame] | 81 | _bindingChanged(event: Common.EventTarget.EventTargetEvent): void { |
| 82 | const binding = event.data as PersistenceBinding; |
Tim van der Lippe | f18c6a2 | 2020-01-24 14:15:05 | [diff] [blame] | 83 | this.dispatchEventToListeners(Components.Linkifier.LinkDecorator.Events.LinkIconChanged, binding.network); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 84 | } |
| 85 | |
Jan Scheffler | 99f71f1 | 2021-02-10 21:00:02 | [diff] [blame] | 86 | linkIcon(uiSourceCode: Workspace.UISourceCode.UISourceCode): UI.Icon.Icon|null { |
Tim van der Lippe | daf1627 | 2019-10-09 15:43:43 | [diff] [blame] | 87 | return PersistenceUtils.iconForUISourceCode(uiSourceCode); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 88 | } |
Tim van der Lippe | daf1627 | 2019-10-09 15:43:43 | [diff] [blame] | 89 | } |