Path: blob/main/src/vs/workbench/contrib/comments/browser/timestamp.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as dom from '../../../../base/browser/dom.js';6import type { IManagedHover } from '../../../../base/browser/ui/hover/hover.js';7import { getDefaultHoverDelegate } from '../../../../base/browser/ui/hover/hoverDelegateFactory.js';8import { fromNow } from '../../../../base/common/date.js';9import { Disposable } from '../../../../base/common/lifecycle.js';10import { language } from '../../../../base/common/platform.js';11import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';12import type { IHoverService } from '../../../../platform/hover/browser/hover.js';13import { COMMENTS_SECTION, ICommentsConfiguration } from '../common/commentsConfiguration.js';1415export class TimestampWidget extends Disposable {16private _date: HTMLElement;17private _timestamp: Date | undefined;18private _useRelativeTime: boolean;1920private hover: IManagedHover;2122constructor(23private configurationService: IConfigurationService,24hoverService: IHoverService,25container: HTMLElement,26timeStamp?: Date27) {28super();29this._date = dom.append(container, dom.$('span.timestamp'));30this._date.style.display = 'none';31this._useRelativeTime = this.useRelativeTimeSetting;32this.hover = this._register(hoverService.setupManagedHover(getDefaultHoverDelegate('mouse'), this._date, ''));33this.setTimestamp(timeStamp);34}3536private get useRelativeTimeSetting(): boolean {37return this.configurationService.getValue<ICommentsConfiguration>(COMMENTS_SECTION).useRelativeTime;38}3940public async setTimestamp(timestamp: Date | undefined) {41if ((timestamp !== this._timestamp) || (this.useRelativeTimeSetting !== this._useRelativeTime)) {42this.updateDate(timestamp);43}44this._timestamp = timestamp;45this._useRelativeTime = this.useRelativeTimeSetting;46}4748private updateDate(timestamp?: Date) {49if (!timestamp) {50this._date.textContent = '';51this._date.style.display = 'none';52} else if ((timestamp !== this._timestamp)53|| (this.useRelativeTimeSetting !== this._useRelativeTime)) {54this._date.style.display = '';55let textContent: string;56let tooltip: string | undefined;57if (this.useRelativeTimeSetting) {58textContent = this.getRelative(timestamp);59tooltip = this.getDateString(timestamp);60} else {61textContent = this.getDateString(timestamp);62}6364this._date.textContent = textContent;65this.hover.update(tooltip ?? '');66}67}6869private getRelative(date: Date): string {70return fromNow(date, true, true);71}7273private getDateString(date: Date): string {74return date.toLocaleString(language);75}76}777879