|
| 1 | +// ***************************************************************************** |
| 2 | +// Copyright (C) 2024 EclipseSource GmbH. |
| 3 | +// |
| 4 | +// This program and the accompanying materials are made available under the |
| 5 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | +// http://www.eclipse.org/legal/epl-2.0. |
| 7 | +// |
| 8 | +// This Source Code may also be made available under the following Secondary |
| 9 | +// Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | +// Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | +// with the GNU Classpath Exception which is available at |
| 12 | +// https://www.gnu.org/software/classpath/license.html. |
| 13 | +// |
| 14 | +// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 |
| 15 | +// ***************************************************************************** |
| 16 | + |
| 17 | +import { InMemoryResources, URI, nls } from '@theia/core'; |
| 18 | +import { AbstractDialog } from '@theia/core/lib/browser/dialogs'; |
| 19 | + |
| 20 | +import { Message } from '@theia/core/lib/browser'; |
| 21 | +import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor'; |
| 22 | +import { MonacoEditorProvider } from '@theia/monaco/lib/browser/monaco-editor-provider'; |
| 23 | + |
| 24 | +export interface SessionSettingsDialogProps { |
| 25 | + initialSettings: { [key: string]: unknown } | undefined; |
| 26 | +} |
| 27 | + |
| 28 | +const DEFAULT_DIALOG_HEIGHT = 400; |
| 29 | +const DEFAULT_DIALOG_WIDTH = 600; |
| 30 | + |
| 31 | +export class SessionSettingsDialog extends AbstractDialog<{ [key: string]: unknown }> { |
| 32 | + |
| 33 | + protected jsonEditor: MonacoEditor | undefined; |
| 34 | + protected dialogContent: HTMLDivElement; |
| 35 | + protected errorMessageDiv: HTMLDivElement; |
| 36 | + protected settings: { [key: string]: unknown } = {}; |
| 37 | + protected initialSettingsString: string; |
| 38 | + |
| 39 | + constructor( |
| 40 | + protected readonly editorProvider: MonacoEditorProvider, |
| 41 | + protected readonly resources: InMemoryResources, |
| 42 | + protected readonly uri: URI, |
| 43 | + protected readonly options: SessionSettingsDialogProps |
| 44 | + ) { |
| 45 | + super({ |
| 46 | + title: nls.localize('theia/ai/session-settings-dialog/title', 'Set Session Settings') |
| 47 | + }); |
| 48 | + |
| 49 | + const initialSettings = options.initialSettings; |
| 50 | + this.initialSettingsString = JSON.stringify(initialSettings, undefined, 2) || '{}'; |
| 51 | + |
| 52 | + this.contentNode.classList.add('monaco-session-settings-dialog'); |
| 53 | + this.contentNode.style.width = `${DEFAULT_DIALOG_WIDTH}px`; |
| 54 | + this.contentNode.style.height = `${DEFAULT_DIALOG_HEIGHT}px`; |
| 55 | + |
| 56 | + this.dialogContent = document.createElement('div'); |
| 57 | + this.dialogContent.className = 'session-settings-container'; |
| 58 | + this.dialogContent.style.height = `${DEFAULT_DIALOG_HEIGHT - 70}px`; |
| 59 | + this.contentNode.appendChild(this.dialogContent); |
| 60 | + |
| 61 | + this.errorMessageDiv = document.createElement('div'); |
| 62 | + this.errorMessageDiv.className = 'session-settings-error'; |
| 63 | + this.contentNode.appendChild(this.errorMessageDiv); |
| 64 | + |
| 65 | + this.appendCloseButton(nls.localizeByDefault('Cancel')); |
| 66 | + this.appendAcceptButton(nls.localizeByDefault('Apply')); |
| 67 | + |
| 68 | + this.createJsonEditor(); |
| 69 | + |
| 70 | + this.validateJson(); |
| 71 | + } |
| 72 | + |
| 73 | + protected override onAfterAttach(msg: Message): void { |
| 74 | + super.onAfterAttach(msg); |
| 75 | + this.update(); |
| 76 | + } |
| 77 | + |
| 78 | + protected override onActivateRequest(msg: Message): void { |
| 79 | + super.onActivateRequest(msg); |
| 80 | + if (this.jsonEditor) { |
| 81 | + this.jsonEditor.focus(); |
| 82 | + } |
| 83 | + } |
| 84 | + protected async createJsonEditor(): Promise<void> { |
| 85 | + |
| 86 | + this.resources.update(this.uri, this.initialSettingsString); |
| 87 | + try { |
| 88 | + const editor = await this.editorProvider.createInline(this.uri, this.dialogContent, { |
| 89 | + language: 'json', |
| 90 | + automaticLayout: true, |
| 91 | + minimap: { |
| 92 | + enabled: false |
| 93 | + }, |
| 94 | + scrollBeyondLastLine: false, |
| 95 | + folding: true, |
| 96 | + lineNumbers: 'on', |
| 97 | + fontSize: 13, |
| 98 | + wordWrap: 'on', |
| 99 | + renderValidationDecorations: 'on', |
| 100 | + scrollbar: { |
| 101 | + vertical: 'auto', |
| 102 | + horizontal: 'auto' |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + editor.getControl().onDidChangeModelContent(() => { |
| 107 | + this.validateJson(); |
| 108 | + }); |
| 109 | + editor.document.textEditorModel.setValue(this.initialSettingsString); |
| 110 | + |
| 111 | + this.jsonEditor = editor; |
| 112 | + this.validateJson(); |
| 113 | + } catch (error) { |
| 114 | + console.error('Failed to create JSON editor:', error); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + protected validateJson(): void { |
| 119 | + if (!this.jsonEditor) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + const jsonContent = this.jsonEditor.getControl().getValue(); |
| 124 | + |
| 125 | + try { |
| 126 | + this.settings = JSON.parse(jsonContent); |
| 127 | + this.errorMessageDiv.textContent = ''; |
| 128 | + this.errorMessageDiv.style.display = 'none'; |
| 129 | + this.setErrorButtonState(false); |
| 130 | + } catch (error) { |
| 131 | + this.errorMessageDiv.textContent = `${error}`; |
| 132 | + this.errorMessageDiv.style.display = 'block'; |
| 133 | + this.setErrorButtonState(true); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + protected setErrorButtonState(isError: boolean): void { |
| 138 | + const acceptButton = this.acceptButton; |
| 139 | + if (acceptButton) { |
| 140 | + acceptButton.disabled = isError; |
| 141 | + if (isError) { |
| 142 | + acceptButton.classList.add('disabled'); |
| 143 | + } else { |
| 144 | + acceptButton.classList.remove('disabled'); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + get value(): { [key: string]: unknown } { |
| 150 | + return this.settings; |
| 151 | + } |
| 152 | +} |
0 commit comments