Skip to content

Commit 5f34cd9

Browse files
committed
wip
1 parent 535928c commit 5f34cd9

File tree

7 files changed

+105
-56
lines changed

7 files changed

+105
-56
lines changed

ghcjs/delivery-calculator/src/App/Types.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,18 @@ newAsset = do
172172
$ DynamicFieldText mempty
173173
opfs <-
174174
either throw pure . decodeUtf8Strict . unTagged . htmlUid =<< newUid
175+
let opts =
176+
BlobOpts
177+
{ blobOptsOpfsDir = Just $ "delivery-calculator-" <> vsn,
178+
blobOptsOpfsFile = Just opfs,
179+
blobOptsMaxSizeKb = Just 400000
180+
}
175181
photo <-
176182
fmap
177183
( ( #fieldPairValue
178-
. #fieldSelectOpts
179-
. #selectOptsOpfsName
180-
.~ Just opfs
184+
. #fieldBlobOpts
185+
.~ opts
181186
)
182-
. ( #fieldPairValue
183-
. #fieldSelectOpts
184-
. #selectOptsMaxSizeKb
185-
.~ Just 400000
186-
)
187187
. ( #fieldPairValue
188188
. #fieldType
189189
.~ FieldTypeImage

ghcjs/delivery-calculator/src/Main.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,14 @@ opfsRead sink st =
455455
. #assetFieldPairs
456456
. ix fieldIdx
457457
. #fieldPairValue
458-
when (field ^. #fieldType == FieldTypeImage)
459-
$ whenJust (field ^. #fieldSelectOpts . #selectOptsOpfsName)
460-
$ \opfsName -> Jsm.opfsRead opfsName . flip whenJust $ \uri ->
458+
when
459+
( (field ^. #fieldType == FieldTypeImage)
460+
&& isJust (field ^. #fieldBlobOpts . #blobOptsOpfsDir)
461+
&& isJust (field ^. #fieldBlobOpts . #blobOptsOpfsFile)
462+
)
463+
$ Jsm.opfsRead (fieldBlobOpts field)
464+
. flip whenJust
465+
$ \uri ->
461466
liftIO
462467
. sink
463468
. PushUpdate

ghcjs/miso-functora/js/main.js

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,56 +78,92 @@ export async function resolveDataUrl(value, opts = {}) {
7878
}
7979
blob = await compressImage(blob, maxSizeKb);
8080
}
81-
if (opts.opfsName) {
82-
await opfsWrite(value, opts.opfsName);
81+
if (opts.opfsDir && opts.opfsFile) {
82+
await opfsWrite(value, opts);
8383
}
8484
return URL.createObjectURL(blob);
8585
} catch (e) {
8686
return value;
8787
}
8888
}
8989

90-
export async function opfsWrite(value, opfsName) {
90+
export async function opfsWrite(value, { opfsDir, opfsFile }) {
9191
try {
9292
const root = await navigator.storage.getDirectory();
93-
const handle = await root.getFileHandle(opfsName, { create: true });
94-
const stream = await handle.createWritable();
93+
const dir = await root.getDirectoryHandle(opfsDir, { create: true });
94+
const file = await dir.getFileHandle(opfsFile, { create: true });
95+
const stream = await file.createWritable();
9596
await stream.write(value);
9697
await stream.close();
9798
} catch (e) {
98-
alert("OPFS write failure: " + e.toString() + " file: " + opfsName);
99+
alert(
100+
"OPFS write failure: " +
101+
e.toString() +
102+
" dir: " +
103+
opfsDir +
104+
" file: " +
105+
opfsFile,
106+
);
99107
}
100108
return null;
101109
}
102110

103-
export async function opfsRead(opfsName) {
111+
export async function opfsRead({ opfsDir, opfsFile }) {
104112
try {
105113
const root = await navigator.storage.getDirectory();
106-
const handle = await root.getFileHandle(opfsName);
107-
const file = await handle.getFile();
108-
const uri = await file.text();
114+
const dir = await root.getDirectoryHandle(opfsDir);
115+
const file = await dir.getFileHandle(opfsFile);
116+
const blob = await file.getFile();
117+
const uri = await blob.text();
109118
const res = await resolveDataUrl(uri);
110119
return res;
111120
} catch (e) {
112-
alert("OPFS read failure: " + e.toString() + " file: " + opfsName);
121+
alert(
122+
"OPFS read failure: " +
123+
e.toString() +
124+
" dir: " +
125+
opfsDir +
126+
" file: " +
127+
opfsFile,
128+
);
113129
return null;
114130
}
115131
}
116132

117-
export async function opfsList() {
133+
export async function opfsList(opfsDir) {
118134
try {
119135
const res = [];
120136
const root = await navigator.storage.getDirectory();
121-
for await (let opfsName of root.keys()) {
122-
res.push(opfsName);
137+
const dir = await root.getDirectoryHandle(opfsDir);
138+
for await (let opfsFile of dir.keys()) {
139+
res.push(opfsFile);
123140
}
124141
return res;
125142
} catch (e) {
126-
alert("OPFS list failure: " + e.toString() + " file: " + opfsName);
143+
alert("OPFS list failure: " + e.toString() + " dir: " + opfsDir);
127144
return [];
128145
}
129146
}
130147

148+
export async function opfsRemove({ opfsDir, opfsFile }) {
149+
try {
150+
const root = await navigator.storage.getDirectory();
151+
const dir = await root.getDirectoryHandle(opfsDir);
152+
const file = await dir.getFileHandle(opfsFile);
153+
await file.remove();
154+
} catch (e) {
155+
alert(
156+
"OPFS remove failure: " +
157+
e.toString() +
158+
" dir: " +
159+
opfsDir +
160+
" file: " +
161+
opfsFile,
162+
);
163+
}
164+
return null;
165+
}
166+
131167
export async function openBrowserPage(url) {
132168
try {
133169
return await Browser.open({ url: url, windowName: "_blank" });

ghcjs/miso-functora/js/main.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ghcjs/miso-functora/src/Functora/Miso/Jsm/Generic.hs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ module Functora.Miso.Jsm.Generic
77
openBrowserPage,
88
enterOrEscapeBlur,
99
insertStorage,
10-
SelectOpts (..),
11-
defSelectOpts,
1210
selectStorage,
1311
selectBarcode,
1412
selectClipboard,
1513
selectFile,
1614
opfsRead,
1715
opfsList,
16+
opfsRemove,
1817
genericPromise,
1918
printCurrentPage,
2019
saveFileShow,
@@ -154,29 +153,36 @@ selectBarcode after =
154153
$ after
155154
. fmap strip
156155

157-
selectClipboard :: SelectOpts -> (Maybe Unicode -> JSM ()) -> JSM ()
156+
selectClipboard :: BlobOpts -> (Maybe Unicode -> JSM ()) -> JSM ()
158157
selectClipboard opts after = do
159158
jopts <- JS.toJSVal $ toJSON opts
160159
genericPromise @[JS.JSVal] @Unicode "selectClipboard" [jopts]
161160
$ after
162161
. fmap strip
163162

164-
selectFile :: SelectOpts -> JS.JSVal -> (Maybe Unicode -> JSM ()) -> JSM ()
163+
selectFile :: BlobOpts -> JS.JSVal -> (Maybe Unicode -> JSM ()) -> JSM ()
165164
selectFile opts file after = do
166165
jopts <- JS.toJSVal $ toJSON opts
167166
genericPromise @[JS.JSVal] @Unicode "selectFile" [file, jopts]
168167
$ after
169168
. fmap strip
170169

171-
opfsRead :: Unicode -> (Maybe Unicode -> JSM ()) -> JSM ()
172-
opfsRead opfsName after = do
173-
genericPromise @[Unicode] @Unicode "opfsRead" [opfsName]
170+
opfsRead :: BlobOpts -> (Maybe Unicode -> JSM ()) -> JSM ()
171+
opfsRead opts after = do
172+
jopts <- JS.toJSVal $ toJSON opts
173+
genericPromise @[JS.JSVal] @Unicode "opfsRead" [jopts]
174174
$ after
175175
. fmap strip
176176

177-
opfsList :: (Maybe [Unicode] -> JSM ()) -> JSM ()
178-
opfsList after =
179-
genericPromise @[Unicode] @[Unicode] "opfsList" mempty after
177+
opfsList :: Unicode -> (Maybe [Unicode] -> JSM ()) -> JSM ()
178+
opfsList dir after =
179+
genericPromise @[Unicode] @[Unicode] "opfsList" [dir] after
180+
181+
opfsRemove :: BlobOpts -> JSM ()
182+
opfsRemove opts = do
183+
pkg <- getPkg
184+
jopts <- JS.toJSVal $ toJSON opts
185+
void $ pkg ^. JS.js1 @Unicode "opfsRemove" jopts
180186

181187
genericPromise ::
182188
forall args res.

ghcjs/miso-functora/src/Functora/Miso/Types.hs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ module Functora.Miso.Types
5858
themeCssFile,
5959
noopAll,
6060
noop,
61-
SelectOpts (..),
62-
defSelectOpts,
61+
BlobOpts (..),
62+
defBlobOpts,
6363
module X,
6464
)
6565
where
@@ -130,7 +130,7 @@ data Field a f = Field
130130
{ fieldType :: FieldType,
131131
fieldInput :: f Unicode,
132132
fieldOutput :: a,
133-
fieldSelectOpts :: SelectOpts,
133+
fieldBlobOpts :: BlobOpts,
134134
fieldModalState :: OpenedOrClosed,
135135
fieldFocusState :: FocusedOrBlurred,
136136
fieldRequired :: Bool,
@@ -186,7 +186,7 @@ newField typ output newInput = do
186186
{ fieldType = typ,
187187
fieldInput = input,
188188
fieldOutput = output,
189-
fieldSelectOpts = defSelectOpts,
189+
fieldBlobOpts = defBlobOpts,
190190
fieldModalState = Closed,
191191
fieldFocusState = Blurred,
192192
fieldRequired = False,
@@ -199,7 +199,7 @@ newFieldId typ viewer output =
199199
{ fieldType = typ,
200200
fieldInput = Identity $ viewer output,
201201
fieldOutput = output,
202-
fieldSelectOpts = defSelectOpts,
202+
fieldBlobOpts = defBlobOpts,
203203
fieldModalState = Closed,
204204
fieldFocusState = Blurred,
205205
fieldRequired = False,
@@ -620,16 +620,18 @@ noop action event =
620620
. EffectUpdate
621621
$ pure ()
622622

623-
data SelectOpts = SelectOpts
624-
{ selectOptsOpfsName :: Maybe Unicode,
625-
selectOptsMaxSizeKb :: Maybe Int
623+
data BlobOpts = BlobOpts
624+
{ blobOptsOpfsDir :: Maybe Unicode,
625+
blobOptsOpfsFile :: Maybe Unicode,
626+
blobOptsMaxSizeKb :: Maybe Int
626627
}
627628
deriving stock (Eq, Ord, Show, Data, Generic)
628-
deriving (Binary, ToJSON, FromJSON) via GenericType SelectOpts
629-
630-
defSelectOpts :: SelectOpts
631-
defSelectOpts =
632-
SelectOpts
633-
{ selectOptsOpfsName = Nothing,
634-
selectOptsMaxSizeKb = Nothing
629+
deriving (Binary, ToJSON, FromJSON) via GenericType BlobOpts
630+
631+
defBlobOpts :: BlobOpts
632+
defBlobOpts =
633+
BlobOpts
634+
{ blobOptsOpfsDir = Nothing,
635+
blobOptsOpfsFile = Nothing,
636+
blobOptsMaxSizeKb = Nothing
635637
}

ghcjs/miso-functora/src/Functora/Miso/Widgets/Field.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,10 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
343343
else do
344344
file <- el JS.! ("files" :: Unicode) JS.!! 0
345345
Jsm.selectFile
346-
( fromMaybe defSelectOpts
346+
( fromMaybe defBlobOpts
347347
$ st
348348
^? cloneTraversal optic
349-
. #fieldSelectOpts
349+
. #fieldBlobOpts
350350
)
351351
file
352352
$ \case
@@ -461,8 +461,8 @@ fieldIcon full opts = \case
461461
. insertAction full
462462
. Jsm.selectClipboard
463463
$ fromMaybe
464-
defSelectOpts
465-
(st ^? cloneTraversal optic . #fieldSelectOpts)
464+
defBlobOpts
465+
(st ^? cloneTraversal optic . #fieldBlobOpts)
466466
ScanQrWidget ->
467467
fieldIconSimple opts Icon.IconQrCode mempty
468468
$ insertAction full Jsm.selectBarcode

0 commit comments

Comments
 (0)