diff --git a/package.json b/package.json index d89d2e3c..4b0232e8 100644 --- a/package.json +++ b/package.json @@ -126,7 +126,7 @@ }, { "command": "livePreview.setDefaultOpenFile", - "when": "resourceLangId == html", + "when": "resourceLangId == html && !inChat", "group": "1_livepreview@2" } ], diff --git a/src/editorPreview/previewManager.ts b/src/editorPreview/previewManager.ts index 8f2c714c..77a1590f 100644 --- a/src/editorPreview/previewManager.ts +++ b/src/editorPreview/previewManager.ts @@ -171,7 +171,7 @@ export class PreviewManager extends Disposable { let path = '/'; if (!connection?.workspace) { this._notifyLooseFileOpen(); - path = await this._endpointManager.encodeLooseFileEndpoint(file.fsPath); + path = await this._endpointManager.encodeLooseFileEndpoint(file); if (!path.startsWith('/')) { path = `/${path}`; diff --git a/src/infoManagers/endpointManager.ts b/src/infoManagers/endpointManager.ts index b7e08d0d..121307c4 100644 --- a/src/infoManagers/endpointManager.ts +++ b/src/infoManagers/endpointManager.ts @@ -37,7 +37,12 @@ export class EndpointManager extends Disposable { * @param location the file location to encode. * @returns the encoded endpoint. */ - public async encodeLooseFileEndpoint(location: string): Promise { + public async encodeLooseFileEndpoint(file: string | vscode.Uri): Promise { + if (typeof file !== 'string' && file.scheme === 'vscode-chat-code-block') { + return '/chatCodeBlock/' + encodeURIComponent(file.with({ fragment: '' }).toString()); + } + + const location = typeof file === 'string' ? file : file.fsPath; let fullParent = await PathUtil.GetParentDir(location); const child = await PathUtil.GetFileName(location, true); diff --git a/src/manager.ts b/src/manager.ts index 9938ca54..0a39b0e4 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -516,6 +516,7 @@ export class Manager extends Disposable { serverGrouping?: ServerGrouping ): Promise { if (file.scheme !== 'file') { + // Is this an error? console.error('Tried to open a non-file URI with file opener'); } if (!serverGrouping) { diff --git a/src/server/httpServer.ts b/src/server/httpServer.ts index b89f3f6f..cc2a8216 100644 --- a/src/server/httpServer.ts +++ b/src/server/httpServer.ts @@ -136,7 +136,7 @@ export class HttpServer extends Disposable { ...(contentLength ? { 'Content-Length': contentLength } : {}), // add CORP header for codespaces // https://github.com/microsoft/vscode-livepreview/issues/560 - ...{'Cross-Origin-Resource-Policy': 'cross-origin'}, + ...{ 'Cross-Origin-Resource-Policy': 'cross-origin' }, ...this._defaultHeaders }); } catch (e) { @@ -214,12 +214,15 @@ export class HttpServer extends Disposable { let contentType = 'application/octet-stream'; if (basePath === '') { - if (URLPathName.startsWith('/endpoint_unsaved')) { - const untitledFileName = URLPathName.substring( - URLPathName.lastIndexOf('/') + 1 - ); + if (URLPathName.startsWith('/endpoint_unsaved') || URLPathName.startsWith('/chatCodeBlock')) { + let fileName: string; + if (URLPathName.startsWith('/endpoint_unsaved')) { + fileName = URLPathName.substring(URLPathName.lastIndexOf('/') + 1); + } else { + fileName = decodeURIComponent(URLPathName.slice('/chatCodeBlock/'.length)); + } const content = await this._contentLoader.getFileStream( - untitledFileName, + fileName, false ); if (content.Stream) { diff --git a/src/server/serverUtils/contentLoader.ts b/src/server/serverUtils/contentLoader.ts index 7b3273c4..2c46ffb2 100644 --- a/src/server/serverUtils/contentLoader.ts +++ b/src/server/serverUtils/contentLoader.ts @@ -293,7 +293,7 @@ export class ContentLoader extends Disposable { let contentLength = 0; while (i < workspaceDocuments.length) { - if (PathUtil.PathEquals(readPath, workspaceDocuments[i].fileName)) { + if (PathUtil.PathEquals(readPath, workspaceDocuments[i].fileName) || (workspaceDocuments[i].uri.scheme === 'vscode-chat-code-block' && workspaceDocuments[i].uri.with({ fragment: '' }).toString() === readPath)) { if (inFilesystem && workspaceDocuments[i].isUntitled) { continue; }