Spaces:
Paused
Paused
File size: 2,055 Bytes
4e7644b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | /** Open raw HTML in a new browser context across browser and desktop shells. */
import { invoke } from "@tauri-apps/api/core";
import { buildAuthHeaders } from "../api/authHeaders";
import { workspaceApi } from "../api/modules/workspace";
import type { WorkspaceRoot } from "../features/files-workspace/types";
import { isDesktopTauriRuntime } from "./openExternalLink";
import { getPyWebViewApi } from "./pywebview";
interface OpenHtmlFileOptions {
content: string;
filePath: string;
chatId?: string;
projectDirOverride?: string;
root?: WorkspaceRoot;
workspaceBacked?: boolean;
}
function workspaceHeaders(
chatId?: string,
projectDirOverride?: string,
): Record<string, string> {
return {
...buildAuthHeaders(),
...(chatId ? { "X-Chat-Id": chatId } : {}),
...(!chatId && projectDirOverride
? { "X-Session-Project-Dir": projectDirOverride }
: {}),
};
}
function openBlobHtml(content: string): void {
const url = URL.createObjectURL(
new Blob([content], { type: "text/html;charset=utf-8" }),
);
window.open(url, "_blank", "noopener,noreferrer");
window.setTimeout(() => URL.revokeObjectURL(url), 60_000);
}
export function openHtmlFile(options: OpenHtmlFileOptions): void {
const {
content,
filePath,
chatId,
projectDirOverride,
root = "project",
workspaceBacked = false,
} = options;
const resolverUrl = workspaceApi.getHtmlFileUriUrl(filePath, root);
const headers = workspaceHeaders(chatId, projectDirOverride);
const pywebviewApi = getPyWebViewApi();
if (workspaceBacked && pywebviewApi?.open_workspace_html) {
void pywebviewApi
.open_workspace_html(resolverUrl, headers)
.catch((error) =>
console.warn("[html-preview] Native open failed", error),
);
return;
}
if (workspaceBacked && isDesktopTauriRuntime()) {
void invoke("open_workspace_html", { url: resolverUrl, headers }).catch(
(error) => console.warn("[html-preview] Tauri open failed", error),
);
return;
}
openBlobHtml(content);
}
|