UNPKG

3.8 kBPlain TextView Raw
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
5import * as Common from '../../core/common/common.js';
6import * as i18n from '../../core/i18n/i18n.js';
7import * as Platform from '../../core/platform/platform.js';
8import * as Components from '../../ui/legacy/components/utils/utils.js';
9import * as UI from '../../ui/legacy/legacy.js';
10import * as Workspace from '../workspace/workspace.js';
11
12import {FileSystemWorkspaceBinding} from './FileSystemWorkspaceBinding.js';
13import {NetworkPersistenceManager} from './NetworkPersistenceManager.js';
14
15import type {PersistenceBinding} from './PersistenceImpl.js';
16import {Events, PersistenceImpl} from './PersistenceImpl.js';
17
18const UIStrings = {
19 /**
20 *@description Text in Persistence Utils of the Workspace settings in Settings
21 *@example {example.url} PH1
22 */
23 linkedToSourceMapS: 'Linked to source map: {PH1}',
24 /**
25 *@description Text to show something is linked to another
26 *@example {example.url} PH1
27 */
28 linkedToS: 'Linked to {PH1}',
29};
30const str_ = i18n.i18n.registerUIStrings('models/persistence/PersistenceUtils.ts', UIStrings);
31const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
32export class PersistenceUtils {
33 static tooltipForUISourceCode(uiSourceCode: Workspace.UISourceCode.UISourceCode): string {
34 const binding = PersistenceImpl.instance().binding(uiSourceCode);
35 if (!binding) {
36 return '';
37 }
38 if (uiSourceCode === binding.network) {
39 return FileSystemWorkspaceBinding.tooltipForUISourceCode(binding.fileSystem);
40 }
41 if (binding.network.contentType().isFromSourceMap()) {
42 return i18nString(
43 UIStrings.linkedToSourceMapS, {PH1: Platform.StringUtilities.trimMiddle(binding.network.url(), 150)});
44 }
45 return i18nString(UIStrings.linkedToS, {PH1: Platform.StringUtilities.trimMiddle(binding.network.url(), 150)});
46 }
47
48 static iconForUISourceCode(uiSourceCode: Workspace.UISourceCode.UISourceCode): UI.Icon.Icon|null {
49 const binding = PersistenceImpl.instance().binding(uiSourceCode);
50 if (binding) {
51 if (!binding.fileSystem.url().startsWith('file://')) {
52 return null;
53 }
54 const icon = UI.Icon.Icon.create('mediumicon-file-sync');
55 UI.Tooltip.Tooltip.install(icon, PersistenceUtils.tooltipForUISourceCode(binding.network));
56 // TODO(allada) This will not work properly with dark theme.
57 if (NetworkPersistenceManager.instance().project() === binding.fileSystem.project()) {
58 icon.style.filter = 'hue-rotate(160deg)';
59 }
60 return icon;
61 }
62 if (uiSourceCode.project().type() !== Workspace.Workspace.projectTypes.FileSystem ||
63 !uiSourceCode.url().startsWith('file://')) {
64 return null;
65 }
66
67 const icon = UI.Icon.Icon.create('mediumicon-file');
68 UI.Tooltip.Tooltip.install(icon, PersistenceUtils.tooltipForUISourceCode(uiSourceCode));
69 return icon;
70 }
71}
72
73export class LinkDecorator extends Common.ObjectWrapper.ObjectWrapper<Components.Linkifier.LinkDecorator.EventTypes>
74 implements Components.Linkifier.LinkDecorator {
75 constructor(persistence: PersistenceImpl) {
76 super();
77 persistence.addEventListener(Events.BindingCreated, this.bindingChanged, this);
78 persistence.addEventListener(Events.BindingRemoved, this.bindingChanged, this);
79 }
80
81 private bindingChanged(event: Common.EventTarget.EventTargetEvent<PersistenceBinding>): void {
82 const binding = event.data;
83 this.dispatchEventToListeners(Components.Linkifier.LinkDecorator.Events.LinkIconChanged, binding.network);
84 }
85
86 linkIcon(uiSourceCode: Workspace.UISourceCode.UISourceCode): UI.Icon.Icon|null {
87 return PersistenceUtils.iconForUISourceCode(uiSourceCode);
88 }
89}