Skip to content

files: Add option to show file selector immediately #35

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

Merged
merged 1 commit into from
Nov 13, 2022
Merged
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
42 changes: 41 additions & 1 deletion files.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
throw new Error('files extension must be run unsandboxed');
}

const MODE_MODAL = 'modal';
const MODE_IMMEDIATELY_SHOW_SELECTOR = 'selector';

let openFileSelectorMode = MODE_MODAL;

const showFilePrompt = (accept) => new Promise((_resolve) => {
// We can't reliably show an <input> picker without "user interaction" in all environments,
// so we have to show our own UI anyways. We may as well use this to implement some nice features
Expand Down Expand Up @@ -130,6 +135,10 @@
modal.appendChild(subtitle);

document.body.appendChild(outer);

if (openFileSelectorMode === MODE_IMMEDIATELY_SHOW_SELECTOR) {
input.click();
}
});

const download = (text, file) => {
Expand Down Expand Up @@ -184,8 +193,35 @@
defaultValue: 'save.txt'
}
}
},
{
opcode: 'setOpenMode',
blockType: Scratch.BlockType.COMMAND,
text: 'set open file selector mode to [mode]',
arguments: {
mode: {
type: Scratch.ArgumentType.STRING,
defaultValue: MODE_MODAL,
menu: 'automaticallyOpen'
}
}
}
],
menus: {
automaticallyOpen: {
acceptReporters: true,
items: [
{
text: 'show modal',
value: MODE_MODAL
},
{
text: 'open selector immediately',
value: MODE_IMMEDIATELY_SHOW_SELECTOR
}
]
}
]
}
};
}

Expand All @@ -200,6 +236,10 @@
download (args) {
download(args.text, args.file);
}

setOpenMode (args) {
openFileSelectorMode = args.mode;
}
}

Scratch.extensions.register(new Files());
Expand Down