Added support for special characters in path for images. (#347)

This commit is contained in:
Jas Singh
2024-10-20 17:13:53 -07:00
committed by GitHub
parent 27d2652e8e
commit 3cae0e1fae

View File

@@ -1,11 +1,31 @@
import { Opt } from 'lib/option'; import { Opt } from 'lib/option';
import { Attribute, BoxWidget } from 'lib/types/widget'; import { Attribute, BoxWidget, Child } from 'lib/types/widget';
import FileChooserButton from 'types/widgets/filechooserbutton';
export const imageInputter = <T>(self: BoxWidget, opt: Opt<T>): Attribute => { export const imageInputter = <T>(self: BoxWidget, opt: Opt<T>): Attribute => {
return (self.child = Widget.FileChooserButton({ self.child = createFileChooserButton(opt);
class_name: 'image-chooser', return self.child;
on_file_set: ({ uri }) => {
opt.value = uri!.replace('file://', '') as T;
},
}));
}; };
const createFileChooserButton = <T>(opt: Opt<T>): FileChooserButton<Child, Attribute> => {
return Widget.FileChooserButton({
class_name: 'image-chooser',
on_file_set: handleFileSet(opt),
});
};
const handleFileSet =
<T>(opt: Opt<T>) =>
({ uri }: { uri: string | null }): void => {
if (!uri) {
console.warn('No URI selected');
return;
}
try {
const decodedPath = decodeURIComponent(uri.replace('file://', ''));
opt.value = decodedPath as T;
} catch (error) {
console.error('Failed to decode URI:', error);
}
};