Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use file-system-access ponyfill for Firefox support #354

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
6 changes: 1 addition & 5 deletions components/src/stores/base.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {

export interface BaseContext {
fs: FileSystem;
canUpgradeFs: boolean;
upgradeFs: (force?: boolean) => void;
closeFs: () => void;
upgraded?: string;
Expand Down Expand Up @@ -59,10 +58,9 @@ export function useBaseContext(): BaseContext {
});
}, [upgraded, replaceFs]);

const canUpgradeFs = `showDirectoryPicker` in window;
const upgradeFs = useCallback(
async (force = false) => {
if (!canUpgradeFs || (upgraded && !force)) return;
if (upgraded && !force) return;
const handler = await openNand2TetrisDirectory();
const adapter = await createAndStoreLocalAdapterInIndexedDB(handler);
replaceFs(adapter);
Expand All @@ -84,7 +82,6 @@ export function useBaseContext(): BaseContext {
status,
setStatus,
storage: localStorage,
canUpgradeFs,
upgradeFs,
closeFs,
upgraded,
Expand All @@ -93,7 +90,6 @@ export function useBaseContext(): BaseContext {

export const BaseContext = createContext<BaseContext>({
fs: new FileSystem(new LocalStorageFileSystemAdapter()),
canUpgradeFs: false,
// eslint-disable-next-line @typescript-eslint/no-empty-function
upgradeFs() {},
// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand Down
3 changes: 2 additions & 1 deletion components/src/stores/base/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
SEP,
Stats,
} from "@davidsouther/jiffies/lib/esm/fs.js";
import { showDirectoryPicker } from "file-system-access";

function dirname(path: string): string {
return path.split(SEP).slice(0, -1).join(SEP);
}

export function openNand2TetrisDirectory(): Promise<FileSystemDirectoryHandle> {
return window.showDirectoryPicker({
return showDirectoryPicker({
id: "nand2tetris",

Check failure on line 15 in components/src/stores/base/fs.ts

View workflow job for this annotation

GitHub Actions / all

Argument of type '{ id: string; mode: string; startIn: string; }' is not assignable to parameter of type 'CustomDirectoryPickerOptions'.
mode: "readwrite",
startIn: "documents",
});
Expand Down
1 change: 0 additions & 1 deletion extension/views/hdl/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const baseContext: BaseContext = {
new ObjectFileSystemAdapter({ "projects/01/Not.hdl": Not.hdl })
),
upgraded: "true",
canUpgradeFs: false,
upgradeFs() {},
closeFs() {},
storage: {},
Expand Down
72 changes: 72 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@
"engines": {
"node": ">=16",
"npm": ">=7"
},
"dependencies": {
"file-system-access": "^1.0.4"
}
}
7 changes: 1 addition & 6 deletions web/src/shell/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,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
6 changes: 2 additions & 4 deletions web/src/shell/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const showUpgradeFs = false;

export const Settings = () => {
const { toolStates } = useContext(AppContext);
const { fs, setStatus, canUpgradeFs, upgradeFs, closeFs, upgraded } =
const { fs, setStatus, upgradeFs, closeFs, upgraded } =
useContext(BaseContext);
const { settings, monaco, theme, setTheme, tracking } =
useContext(AppContext);
Expand Down Expand Up @@ -155,7 +155,7 @@ export const Settings = () => {
<Trans>Files</Trans>
</dt>
<dd>
{showUpgradeFs && canUpgradeFs ? (
{showUpgradeFs && (
<>
<button
disabled={upgrading}
Expand Down Expand Up @@ -194,8 +194,6 @@ export const Settings = () => {
<></>
)}
</>
) : (
<></>
)}
<button
onClick={async () => {
Expand Down
1 change: 0 additions & 1 deletion web/src/testing/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const useTestingAppContext = () => ({
"projects/01/Not/Not.cmp": Not.cmp,
}),
),
canUpgradeFs: false,
// eslint-disable-next-line @typescript-eslint/no-empty-function
upgradeFs() {},
// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand Down
Loading