Path: blob/main/src/vs/workbench/contrib/chat/browser/attachments/chatAttachmentWidgetRegistry.ts
13406 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 event from '../../../../../base/common/event.js';6import { IDisposable } from '../../../../../base/common/lifecycle.js';7import { createDecorator } from '../../../../../platform/instantiation/common/instantiation.js';8import { IChatRequestVariableEntry } from '../../common/attachments/chatVariableEntries.js';910/**11* Interface for a contributed attachment widget instance.12*/13export interface IChatAttachmentWidgetInstance extends IDisposable {14readonly element: HTMLElement;15readonly onDidDelete: event.Event<Event>;16readonly onDidOpen: event.Event<void>;17/** Optional label element, used for applying warning styles on omitted attachments. */18readonly label?: { readonly element: HTMLElement };19}2021/**22* Factory function type for creating attachment widgets.23*/24export type ChatAttachmentWidgetFactory = (25attachment: IChatRequestVariableEntry,26options: { shouldFocusClearButton: boolean; supportsDeletion: boolean },27container: HTMLElement,28) => IChatAttachmentWidgetInstance;2930export const IChatAttachmentWidgetRegistry = createDecorator<IChatAttachmentWidgetRegistry>('chatAttachmentWidgetRegistry');3132export interface IChatAttachmentWidgetRegistry {33readonly _serviceBrand: undefined;3435/**36* Register a widget factory for a specific attachment kind.37*/38registerFactory(kind: string, factory: ChatAttachmentWidgetFactory): IDisposable;3940/**41* Try to create a widget for the given attachment using a registered factory.42* Returns undefined if no factory is registered for the attachment's kind.43*/44createWidget(45attachment: IChatRequestVariableEntry,46options: { shouldFocusClearButton: boolean; supportsDeletion: boolean },47container: HTMLElement,48): IChatAttachmentWidgetInstance | undefined;49}5051export class ChatAttachmentWidgetRegistry implements IChatAttachmentWidgetRegistry {5253declare readonly _serviceBrand: undefined;5455private readonly _factories = new Map<string, ChatAttachmentWidgetFactory>();5657registerFactory(kind: string, factory: ChatAttachmentWidgetFactory): IDisposable {58this._factories.set(kind, factory);59return {60dispose: () => {61if (this._factories.get(kind) === factory) {62this._factories.delete(kind);63}64}65};66}6768createWidget(69attachment: IChatRequestVariableEntry,70options: { shouldFocusClearButton: boolean; supportsDeletion: boolean },71container: HTMLElement,72): IChatAttachmentWidgetInstance | undefined {73const factory = this._factories.get(attachment.kind);74if (!factory) {75return undefined;76}77return factory(attachment, options, container);78}79}808182