Skip to content

Commit

Permalink
Merge pull request #69 from LuchoTurtle/warning-message
Browse files Browse the repository at this point in the history
[PR] Adding warning message option.
  • Loading branch information
LuchoTurtle authored Jun 1, 2024
2 parents 50df977 + ca12306 commit 4ade87c
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ A Tweakpane plugin for importing files.
> You can install it.
>
> ```sh
> npm i tweakpane-plugin-file-import@0.1.4
> npm i tweakpane-plugin-file-import@0.2.0
> ```
>
> And use it like so.
Expand Down Expand Up @@ -79,6 +79,7 @@ pane
view: 'file-input',
lineCount: 3,
filetypes: ['.png', '.jpg'],
invalidFiletypeMessage: "We can't accept those filetypes!"
})
.on('change', (ev) => {
console.log(ev.value);
Expand All @@ -90,6 +91,7 @@ pane
view: 'file-input',
lineCount: 3,
filetypes: ['.png', '.jpg'],
invalidFiletypeMessage: "We can't accept those filetypes!"
})
.on('change', (ev) => {
console.log(ev.value);
Expand All @@ -103,6 +105,8 @@ pane
|-----------|--------|--------------------------------|
| lineCount | int | Number of lines for the height of the container. Similar to [FPS graph ](https://github.com/tweakpane/plugin-essentials#fps-graph) |
| filetypes | array | Array of valid file extensions. |
| invalidFiletypeMessage | string | String shown when the user tries to upload an invalid filetype. |




Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tweakpane-plugin-file-import",
"version": "1.0.2",
"version": "1.1.0",
"description": "A general-purpose and simple file input Tweakpane plugin",
"homepage": "https://github.com/LuchoTurtle/tweakpane-plugin-file-import",
"main": "dist/tweakpane-plugin-file-import.js",
Expand Down
7 changes: 2 additions & 5 deletions src/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface Config {
value: Value<File | null>;
viewProps: ViewProps;

invalidFiletypeMessage: string;
lineCount: number;
filetypes?: string[];
}
Expand All @@ -24,6 +25,7 @@ export class FilePluginController implements Controller<FilePluginView> {
this.view = new FilePluginView(doc, {
viewProps: this.viewProps,
value: config.value,
invalidFiletypeMessage: config.invalidFiletypeMessage,
lineCount: config.lineCount,
filetypes: config.filetypes,
});
Expand Down Expand Up @@ -78,10 +80,8 @@ export class FilePluginController implements Controller<FilePluginView> {
*/
private showWarning() {
this.view.warning.style.display = 'block';
this.view.warning.innerHTML = 'Unaccepted file type.';
setTimeout(() => {
// Resetting warning text
this.view.warning.innerHTML = '';
this.view.warning.style.display = 'none';
}, 5000);
}
Expand Down Expand Up @@ -117,7 +117,6 @@ export class FilePluginController implements Controller<FilePluginView> {
this.view.input.value = '';

// Resetting the warning text
this.view.warning.innerHTML = '';
this.view.warning.style.display = 'none';
}
}
Expand Down Expand Up @@ -192,7 +191,6 @@ export class FilePluginController implements Controller<FilePluginView> {
}

// Resetting warning text
this.view.warning.innerHTML = '';
this.view.warning.style.display = 'none';

// Adding button to delete
Expand All @@ -207,7 +205,6 @@ export class FilePluginController implements Controller<FilePluginView> {
containerEl.removeChild(textEl);

// Resetting warning text
this.view.warning.innerHTML = '';
this.view.warning.style.display = 'none';

// Hiding button and resetting border
Expand Down
5 changes: 5 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {FilePluginController} from './controller/controller.js';

export interface PluginInputParams extends BaseInputParams {
view: 'file-input';
invalidFiletypeMessage?: string;
lineCount?: number;
filetypes?: string[];
}
Expand All @@ -36,6 +37,7 @@ export const TweakpaneFileInputPlugin: InputBindingPlugin<
// `view` option may be useful to provide a custom control for primitive values
view: p.required.constant('file-input'),

invalidFiletypeMessage: p.optional.string,
lineCount: p.optional.number,
filetypes: p.optional.array(p.required.string),
}));
Expand Down Expand Up @@ -73,12 +75,15 @@ export const TweakpaneFileInputPlugin: InputBindingPlugin<

controller(args) {
const defaultNumberOfLines = 3;
const defaultFiletypeWarningText = 'Unaccepted file type.';

// Create a controller for the plugin
return new FilePluginController(args.document, {
value: args.value,
viewProps: args.viewProps,

invalidFiletypeMessage:
args.params.invalidFiletypeMessage ?? defaultFiletypeWarningText,
lineCount: args.params.lineCount ?? defaultNumberOfLines,
filetypes: args.params.filetypes,
});
Expand Down
3 changes: 1 addition & 2 deletions src/sass/plugin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
display: inline-block;
font-size: 0.9em;
height: max-content;
line-height: 0.9;
margin: 0.2rem;
line-height: 1.5;
opacity: 0.5;
white-space: normal;
width: max-content;
Expand Down
2 changes: 2 additions & 0 deletions src/view/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface Config {
value: Value<File | null>;
viewProps: ViewProps;

invalidFiletypeMessage: string;
lineCount: number;
filetypes?: string[];
}
Expand Down Expand Up @@ -55,6 +56,7 @@ export class FilePluginView implements View {
// Warning text
this.warning = doc.createElement('span');
this.warning.classList.add(containerClassName('warning'));
this.warning.innerHTML = config.invalidFiletypeMessage;
this.warning.style.display = 'none';

// Delete button
Expand Down
3 changes: 2 additions & 1 deletion test/browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
pane
.addBinding(params, 'file', {
view: 'file-input',
invalidFiletypeMessage: "We can't accept those filetypes!",
lineCount: 3,
filetypes: ['.png', '.jpg'],
filetypes: ['.png', '.jpeg'],
})
.on('change', (ev) => {
console.log(ev.value);
Expand Down

0 comments on commit 4ade87c

Please sign in to comment.