From 3cae0e1fae24b47e644bf8e3e099220eb3ba5b8f Mon Sep 17 00:00:00 2001 From: Jas Singh Date: Sun, 20 Oct 2024 17:13:53 -0700 Subject: [PATCH] Added support for special characters in path for images. (#347) --- widget/settings/shared/components/image.ts | 34 +++++++++++++++++----- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/widget/settings/shared/components/image.ts b/widget/settings/shared/components/image.ts index 94f838a..1906004 100644 --- a/widget/settings/shared/components/image.ts +++ b/widget/settings/shared/components/image.ts @@ -1,11 +1,31 @@ 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 = (self: BoxWidget, opt: Opt): Attribute => { - return (self.child = Widget.FileChooserButton({ - class_name: 'image-chooser', - on_file_set: ({ uri }) => { - opt.value = uri!.replace('file://', '') as T; - }, - })); + self.child = createFileChooserButton(opt); + return self.child; }; + +const createFileChooserButton = (opt: Opt): FileChooserButton => { + return Widget.FileChooserButton({ + class_name: 'image-chooser', + on_file_set: handleFileSet(opt), + }); +}; + +const handleFileSet = + (opt: Opt) => + ({ 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); + } + };