Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/browser/attachments/chatAttachmentWidgetRegistry.ts
13406 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 event from '../../../../../base/common/event.js';
7
import { IDisposable } from '../../../../../base/common/lifecycle.js';
8
import { createDecorator } from '../../../../../platform/instantiation/common/instantiation.js';
9
import { IChatRequestVariableEntry } from '../../common/attachments/chatVariableEntries.js';
10
11
/**
12
* Interface for a contributed attachment widget instance.
13
*/
14
export interface IChatAttachmentWidgetInstance extends IDisposable {
15
readonly element: HTMLElement;
16
readonly onDidDelete: event.Event<Event>;
17
readonly onDidOpen: event.Event<void>;
18
/** Optional label element, used for applying warning styles on omitted attachments. */
19
readonly label?: { readonly element: HTMLElement };
20
}
21
22
/**
23
* Factory function type for creating attachment widgets.
24
*/
25
export type ChatAttachmentWidgetFactory = (
26
attachment: IChatRequestVariableEntry,
27
options: { shouldFocusClearButton: boolean; supportsDeletion: boolean },
28
container: HTMLElement,
29
) => IChatAttachmentWidgetInstance;
30
31
export const IChatAttachmentWidgetRegistry = createDecorator<IChatAttachmentWidgetRegistry>('chatAttachmentWidgetRegistry');
32
33
export interface IChatAttachmentWidgetRegistry {
34
readonly _serviceBrand: undefined;
35
36
/**
37
* Register a widget factory for a specific attachment kind.
38
*/
39
registerFactory(kind: string, factory: ChatAttachmentWidgetFactory): IDisposable;
40
41
/**
42
* Try to create a widget for the given attachment using a registered factory.
43
* Returns undefined if no factory is registered for the attachment's kind.
44
*/
45
createWidget(
46
attachment: IChatRequestVariableEntry,
47
options: { shouldFocusClearButton: boolean; supportsDeletion: boolean },
48
container: HTMLElement,
49
): IChatAttachmentWidgetInstance | undefined;
50
}
51
52
export class ChatAttachmentWidgetRegistry implements IChatAttachmentWidgetRegistry {
53
54
declare readonly _serviceBrand: undefined;
55
56
private readonly _factories = new Map<string, ChatAttachmentWidgetFactory>();
57
58
registerFactory(kind: string, factory: ChatAttachmentWidgetFactory): IDisposable {
59
this._factories.set(kind, factory);
60
return {
61
dispose: () => {
62
if (this._factories.get(kind) === factory) {
63
this._factories.delete(kind);
64
}
65
}
66
};
67
}
68
69
createWidget(
70
attachment: IChatRequestVariableEntry,
71
options: { shouldFocusClearButton: boolean; supportsDeletion: boolean },
72
container: HTMLElement,
73
): IChatAttachmentWidgetInstance | undefined {
74
const factory = this._factories.get(attachment.kind);
75
if (!factory) {
76
return undefined;
77
}
78
return factory(attachment, options, container);
79
}
80
}
81
82