Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/comments/browser/timestamp.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as dom from '../../../../base/browser/dom.js';
7
import type { IManagedHover } from '../../../../base/browser/ui/hover/hover.js';
8
import { getDefaultHoverDelegate } from '../../../../base/browser/ui/hover/hoverDelegateFactory.js';
9
import { fromNow } from '../../../../base/common/date.js';
10
import { Disposable } from '../../../../base/common/lifecycle.js';
11
import { language } from '../../../../base/common/platform.js';
12
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
13
import type { IHoverService } from '../../../../platform/hover/browser/hover.js';
14
import { COMMENTS_SECTION, ICommentsConfiguration } from '../common/commentsConfiguration.js';
15
16
export class TimestampWidget extends Disposable {
17
private _date: HTMLElement;
18
private _timestamp: Date | undefined;
19
private _useRelativeTime: boolean;
20
21
private hover: IManagedHover;
22
23
constructor(
24
private configurationService: IConfigurationService,
25
hoverService: IHoverService,
26
container: HTMLElement,
27
timeStamp?: Date
28
) {
29
super();
30
this._date = dom.append(container, dom.$('span.timestamp'));
31
this._date.style.display = 'none';
32
this._useRelativeTime = this.useRelativeTimeSetting;
33
this.hover = this._register(hoverService.setupManagedHover(getDefaultHoverDelegate('mouse'), this._date, ''));
34
this.setTimestamp(timeStamp);
35
}
36
37
private get useRelativeTimeSetting(): boolean {
38
return this.configurationService.getValue<ICommentsConfiguration>(COMMENTS_SECTION).useRelativeTime;
39
}
40
41
public async setTimestamp(timestamp: Date | undefined) {
42
if ((timestamp !== this._timestamp) || (this.useRelativeTimeSetting !== this._useRelativeTime)) {
43
this.updateDate(timestamp);
44
}
45
this._timestamp = timestamp;
46
this._useRelativeTime = this.useRelativeTimeSetting;
47
}
48
49
private updateDate(timestamp?: Date) {
50
if (!timestamp) {
51
this._date.textContent = '';
52
this._date.style.display = 'none';
53
} else if ((timestamp !== this._timestamp)
54
|| (this.useRelativeTimeSetting !== this._useRelativeTime)) {
55
this._date.style.display = '';
56
let textContent: string;
57
let tooltip: string | undefined;
58
if (this.useRelativeTimeSetting) {
59
textContent = this.getRelative(timestamp);
60
tooltip = this.getDateString(timestamp);
61
} else {
62
textContent = this.getDateString(timestamp);
63
}
64
65
this._date.textContent = textContent;
66
this.hover.update(tooltip ?? '');
67
}
68
}
69
70
private getRelative(date: Date): string {
71
return fromNow(date, true, true);
72
}
73
74
private getDateString(date: Date): string {
75
return date.toLocaleString(language);
76
}
77
}
78
79