Skip to content

Commit

Permalink
Enable jack compiler in firefox in readonly mode
Browse files Browse the repository at this point in the history
  • Loading branch information
netalondon committed Sep 8, 2024
1 parent 1abfe6f commit ccbaf42
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
40 changes: 40 additions & 0 deletions web/src/pages/compiler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const Compiler = () => {

const [selected, setSelected] = useState(0);
const [suppressStatus, setSuppressStatus] = useState(false);
const [editable, setEditable] = useState(false);

const redirectRef = useRef<HTMLAnchorElement>(null);

Expand Down Expand Up @@ -58,6 +59,37 @@ export const Compiler = () => {
}, [state.selected]);

const uploadFiles = async () => {
if ("showDirectoryPicker" in window) {
await uploadFilesForEdit();
setEditable(true);
} else {
uploadFilesReadonly();
setEditable(false);
}
};

const fileUploadRef = useRef<HTMLInputElement>(null);
const uploadFilesReadonly = () => {
fileUploadRef.current?.click();
};

const onFileUpload = async () => {
const jackFiles: Record<string, string> = {};

for (const file of fileUploadRef.current?.files ?? []) {
if (file.name.endsWith(".jack")) {
jackFiles[file.name.replace(".jack", "")] = await file.text();
}
}

dispatch.current({
action: "setTitle",
payload: fileUploadRef.current?.dirName,
});
actions.loadFiles(jackFiles);
};

const uploadFilesForEdit = async () => {
const handle = await openNand2TetrisDirectory();
const fs = new FileSystem(new FileSystemAccessFileSystemAdapter(handle));

Expand Down Expand Up @@ -152,6 +184,13 @@ export const Compiler = () => {
to={URLs["vm"].href}
style={{ display: "none" }}
/>
<input
type="file"
webkitdirectory=""
ref={fileUploadRef}
onChange={onFileUpload}
style={{ display: "none" }}
/>
{newFileDialogComponent}
<Panel
className="code"
Expand Down Expand Up @@ -221,6 +260,7 @@ export const Compiler = () => {
}}
error={state.compiled[file].error}
language={"jack"}
disabled={!editable}
/>
</Tab>
))}
Expand Down
7 changes: 1 addition & 6 deletions web/src/shell/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ const headerButtons: HeaderButton[] = [
headerButtonFromURL(URLs["cpu"], "developer_board"),
headerButtonFromURL(URLs["asm"], "list_alt"),
headerButtonFromURL(URLs["vm"], "computer"),
// TODO(https://github.com/nand2tetris/web-ide/issues/349)
// reenable when this is resolved for Firefox and safari
// https://caniuse.com/?search=showDirectoryPicker
...("showDirectoryPicker" in window
? [headerButtonFromURL(URLs["compiler"], "code")]
: []),
headerButtonFromURL(URLs["compiler"], "code"),
headerButtonFromURL(URLs["bitmap"], "grid_on"),
headerButtonFromURL(URLs["util"], "function", "Converter Tool"),
{
Expand Down

0 comments on commit ccbaf42

Please sign in to comment.