blob: c2ecb204b6183a7e258581454121fce997bc8709 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// 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 Scheffler99f71f12021-02-10 21:00:025/* eslint-disable rulesdir/no_underscored_properties */
6
Tim van der Lippef18c6a22020-01-24 14:15:057import * as Common from '../common/common.js';
8import * as Components from '../components/components.js';
Christy Chenae8efc52020-12-15 01:06:329import * as i18n from '../i18n/i18n.js';
Tim van der Lippe213266c2021-01-18 15:48:3110import * as Platform from '../platform/platform.js';
Tim van der Lippef18c6a22020-01-24 14:15:0511import * as UI from '../ui/ui.js';
12import * as Workspace from '../workspace/workspace.js';
13
Paul Lewis6196e752020-01-13 15:15:5914import {FileSystemWorkspaceBinding} from './FileSystemWorkspaceBinding.js';
Tim van der Lippea76954f2020-08-26 15:56:2515import {NetworkPersistenceManager} from './NetworkPersistenceManager.js';
Tim van der Lippebfbb58f2021-02-25 17:34:1916import type {PersistenceBinding} from './PersistenceImpl.js';
17import {Events, PersistenceImpl} from './PersistenceImpl.js';
Paul Lewis6196e752020-01-13 15:15:5918
Christy Chenae8efc52020-12-15 01:06:3219export 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 Scheffler99f71f12021-02-10 21:00:0231const str_ = i18n.i18n.registerUIStrings('persistence/PersistenceUtils.ts', UIStrings);
Christy Chenae8efc52020-12-15 01:06:3232const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
Paul Lewis6196e752020-01-13 15:15:5933export class PersistenceUtils {
Jan Scheffler99f71f12021-02-10 21:00:0234 static tooltipForUISourceCode(uiSourceCode: Workspace.UISourceCode.UISourceCode): string {
Tim van der Lippecc37f9b2020-09-03 11:38:0935 const binding = PersistenceImpl.instance().binding(uiSourceCode);
Tim van der Lippe1d6e57a2019-09-30 11:55:3436 if (!binding) {
Blink Reformat4c46d092018-04-07 15:32:3737 return '';
Tim van der Lippe1d6e57a2019-09-30 11:55:3438 }
39 if (uiSourceCode === binding.network) {
Paul Lewis6196e752020-01-13 15:15:5940 return FileSystemWorkspaceBinding.tooltipForUISourceCode(binding.fileSystem);
Tim van der Lippe1d6e57a2019-09-30 11:55:3441 }
42 if (binding.network.contentType().isFromSourceMap()) {
Tim van der Lippe213266c2021-01-18 15:48:3143 return i18nString(
44 UIStrings.linkedToSourceMapS, {PH1: Platform.StringUtilities.trimMiddle(binding.network.url(), 150)});
Tim van der Lippe1d6e57a2019-09-30 11:55:3445 }
Tim van der Lippe213266c2021-01-18 15:48:3146 return i18nString(UIStrings.linkedToS, {PH1: Platform.StringUtilities.trimMiddle(binding.network.url(), 150)});
Blink Reformat4c46d092018-04-07 15:32:3747 }
48
Jan Scheffler99f71f12021-02-10 21:00:0249 static iconForUISourceCode(uiSourceCode: Workspace.UISourceCode.UISourceCode): UI.Icon.Icon|null {
Tim van der Lippecc37f9b2020-09-03 11:38:0950 const binding = PersistenceImpl.instance().binding(uiSourceCode);
Blink Reformat4c46d092018-04-07 15:32:3751 if (binding) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3452 if (!binding.fileSystem.url().startsWith('file://')) {
Alexey Kozyatinskiy3a2eb282018-08-21 16:22:0253 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:3454 }
Tim van der Lippef18c6a22020-01-24 14:15:0555 const icon = UI.Icon.Icon.create('mediumicon-file-sync');
Tim van der Lippe70842f32020-11-23 16:56:5756 UI.Tooltip.Tooltip.install(icon, PersistenceUtils.tooltipForUISourceCode(binding.network));
Blink Reformat4c46d092018-04-07 15:32:3757 // TODO(allada) This will not work properly with dark theme.
Tim van der Lippea76954f2020-08-26 15:56:2558 if (NetworkPersistenceManager.instance().project() === binding.fileSystem.project()) {
Blink Reformat4c46d092018-04-07 15:32:3759 icon.style.filter = 'hue-rotate(160deg)';
Tim van der Lippe1d6e57a2019-09-30 11:55:3460 }
Blink Reformat4c46d092018-04-07 15:32:3761 return icon;
62 }
Tim van der Lippef18c6a22020-01-24 14:15:0563 if (uiSourceCode.project().type() !== Workspace.Workspace.projectTypes.FileSystem ||
Tim van der Lippe1d6e57a2019-09-30 11:55:3464 !uiSourceCode.url().startsWith('file://')) {
Blink Reformat4c46d092018-04-07 15:32:3765 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:3466 }
Alexey Kozyatinskiy3a2eb282018-08-21 16:22:0267
Tim van der Lippef18c6a22020-01-24 14:15:0568 const icon = UI.Icon.Icon.create('mediumicon-file');
Tim van der Lippe70842f32020-11-23 16:56:5769 UI.Tooltip.Tooltip.install(icon, PersistenceUtils.tooltipForUISourceCode(uiSourceCode));
Blink Reformat4c46d092018-04-07 15:32:3770 return icon;
71 }
Tim van der Lippedaf16272019-10-09 15:43:4372}
Blink Reformat4c46d092018-04-07 15:32:3773
Jan Scheffler99f71f12021-02-10 21:00:0274export class LinkDecorator extends Common.ObjectWrapper.ObjectWrapper implements Components.Linkifier.LinkDecorator {
75 constructor(persistence: PersistenceImpl) {
Blink Reformat4c46d092018-04-07 15:32:3776 super();
Paul Lewis6196e752020-01-13 15:15:5977 persistence.addEventListener(Events.BindingCreated, this._bindingChanged, this);
78 persistence.addEventListener(Events.BindingRemoved, this._bindingChanged, this);
Blink Reformat4c46d092018-04-07 15:32:3779 }
80
Jan Scheffler99f71f12021-02-10 21:00:0281 _bindingChanged(event: Common.EventTarget.EventTargetEvent): void {
82 const binding = event.data as PersistenceBinding;
Tim van der Lippef18c6a22020-01-24 14:15:0583 this.dispatchEventToListeners(Components.Linkifier.LinkDecorator.Events.LinkIconChanged, binding.network);
Blink Reformat4c46d092018-04-07 15:32:3784 }
85
Jan Scheffler99f71f12021-02-10 21:00:0286 linkIcon(uiSourceCode: Workspace.UISourceCode.UISourceCode): UI.Icon.Icon|null {
Tim van der Lippedaf16272019-10-09 15:43:4387 return PersistenceUtils.iconForUISourceCode(uiSourceCode);
Blink Reformat4c46d092018-04-07 15:32:3788 }
Tim van der Lippedaf16272019-10-09 15:43:4389}