Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/adhoc-workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

const DEFAULT_BASE_URL = 'https://idx.google.com/run.api';

const DEFAULT_BASE_URL = 'https://idx.google.com/run.api';

export interface AdhocWorkspaceContent {
/**
* A map of relative file paths (e.g. `src/foo.html`) to their contents (strings). Binary
* files aren't supported.
* A map of relative file paths (e.g. `src/foo.html`) to their contents.
* String for text files, Uint8Array for binary files.
*/
files: Record<string, string>;
files: Record<string, string | Uint8Array>;
/**
* Optional workspace configuration.
*/
Expand Down Expand Up @@ -44,8 +46,16 @@ export function newAdhocWorkspace(content: AdhocWorkspaceContent, options?: Adho

function createAdhocWorkspaceForm({ files, settings }: AdhocWorkspaceContent) {
const inputs: HTMLInputElement[] = [];
const addInput = (name, value, defaultValue = '') => {
inputs.push(createHiddenInput(name, typeof value === 'string' ? value : defaultValue));
const addInput = (name: string, value: string | Uint8Array, defaultValue = '') => {
let inputValue: string;
if (typeof value === 'string') {
inputValue = value;
} else if (value instanceof Uint8Array) {
inputValue = btoa(String.fromCharCode.apply(null, value));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably need to update everything up the stack as well to support base64 blobs, and indicate (e.g. via filename suffix like src/foo.png:base64) that a file is binary.

} else {
inputValue = defaultValue;
}
inputs.push(createHiddenInput(name, inputValue));
};

Object.entries(files).forEach(([path, contents]) => {
Expand All @@ -71,4 +81,4 @@ function createHiddenInput(name: string, value: string) {
input.name = name;
input.value = value;
return input;
}
}