Skip to content

Commit be4fddf

Browse files
committed
OPFS wip
1 parent 6508285 commit be4fddf

File tree

6 files changed

+65
-23
lines changed

6 files changed

+65
-23
lines changed

ghcjs/delivery-calculator/src/App/Widgets/Asset.hs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import qualified Functora.Miso.Widgets.Icon as Icon
1313

1414
assetsViewer :: Model -> [View Action]
1515
assetsViewer st = do
16-
idx <- fmap fst . zip [0 ..] $ st ^. #modelState . #stAssets
17-
assetViewer st idx
16+
let assets = st ^. #modelState . #stAssets
17+
idx <- fmap fst $ zip [0 ..] assets
18+
assetViewer st idx $ "asset-" <> inspect (length assets - idx - 1)
1819

19-
assetViewer :: Model -> Int -> [View Action]
20-
assetViewer st idx =
20+
assetViewer :: Model -> Int -> Unicode -> [View Action]
21+
assetViewer st idx opfsPrefix =
2122
[ fieldset_ mempty
2223
$ ( legend_
2324
mempty
@@ -36,7 +37,7 @@ assetViewer st idx =
3637
]
3738
]
3839
)
39-
: FieldPairs.fieldPairsViewer fieldPairsOpts args
40+
: FieldPairs.fieldPairsViewer (fieldPairsOpts opfsPrefix) args
4041
]
4142
<> ( Dialog.dialog
4243
Dialog.defOpts
@@ -69,7 +70,7 @@ assetViewer st idx =
6970
failures False
7071
<> FieldPairs.fieldPairsEditor
7172
args
72-
fieldPairsOpts
73+
(fieldPairsOpts opfsPrefix)
7374
{ FieldPairs.optsAdvanced = False
7475
}
7576
}
@@ -124,13 +125,18 @@ assetViewer st idx =
124125
. #stAssets
125126
. ix idx
126127

127-
fieldPairsOpts :: FieldPairs.Opts model action
128-
fieldPairsOpts =
128+
fieldPairsOpts :: Unicode -> FieldPairs.Opts model action
129+
fieldPairsOpts opfsPrefix =
129130
FieldPairs.defOpts
130131
{ FieldPairs.optsField =
131132
Field.defOpts
132133
{ Field.optsExtraAttributesImage =
133134
[ style_ [("max-height", "10vh")]
134135
]
135-
}
136+
},
137+
FieldPairs.optsOpfsName =
138+
Just
139+
$ (opfsPrefix <>)
140+
. ("-field-" <>)
141+
. inspect
136142
}

ghcjs/miso-functora/js/main.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ export async function compressImage(quality, prevImage) {
4242
return nextImage;
4343
}
4444

45-
export async function selectClipboard() {
45+
export async function selectClipboard(opfsName = null) {
4646
const { value } = await Clipboard.read();
47-
return await selectDataUrl(value);
47+
return await selectDataUrl(value, opfsName);
4848
}
4949

50-
export async function selectFile(file) {
50+
export async function selectFile(file, opfsName = null) {
5151
const value = await new Promise((resolve, reject) => {
5252
var fr = new FileReader();
5353
fr.onload = () => {
@@ -56,22 +56,39 @@ export async function selectFile(file) {
5656
fr.onerror = reject;
5757
fr.readAsDataURL(file);
5858
});
59-
return await selectDataUrl(value);
59+
return await selectDataUrl(value, opfsName);
6060
}
6161

62-
export async function selectDataUrl(value) {
62+
export async function selectDataUrl(value, opfsName = null) {
6363
try {
6464
const { buffer: u8a, typeFull: mime } = dataUriToBuffer(value);
6565
let blob = new Blob([u8a], { type: mime });
6666
if (mime.startsWith("image")) {
6767
blob = await compressImage(1, blob);
6868
}
69+
if (opfsName) {
70+
await opfsWrite(value, opfsName);
71+
}
6972
return URL.createObjectURL(blob);
7073
} catch (e) {
7174
return value;
7275
}
7376
}
7477

78+
export async function opfsWrite(value, opfsName) {
79+
try {
80+
const root = await navigator.storage.getDirectory();
81+
const handle = await root.getFileHandle(opfsName, { create: true });
82+
const stream = await handle.createWritable();
83+
await stream.write(value);
84+
await stream.close();
85+
console.log("OPFS success", opfsName, handle);
86+
} catch (e) {
87+
console.log("OPFS failure", opfsName, e);
88+
}
89+
return null;
90+
}
91+
7592
export async function openBrowserPage(url) {
7693
try {
7794
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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,17 @@ selectBarcode after =
150150
$ after
151151
. fmap strip
152152

153-
selectClipboard :: (Maybe Unicode -> JSM ()) -> JSM ()
154-
selectClipboard after =
155-
genericPromise @[Unicode] @Unicode "selectClipboard" mempty
153+
selectClipboard :: Maybe Unicode -> (Maybe Unicode -> JSM ()) -> JSM ()
154+
selectClipboard opfsName after = do
155+
jopfsName <- JS.toJSVal opfsName
156+
genericPromise @[JS.JSVal] @Unicode "selectClipboard" [jopfsName]
156157
$ after
157158
. fmap strip
158159

159-
selectFile :: JS.JSVal -> (Maybe Unicode -> JSM ()) -> JSM ()
160-
selectFile file after =
161-
genericPromise @[JS.JSVal] @Unicode "selectFile" [file]
160+
selectFile :: Maybe Unicode -> JS.JSVal -> (Maybe Unicode -> JSM ()) -> JSM ()
161+
selectFile opfsName file after = do
162+
jopfsName <- JS.toJSVal opfsName
163+
genericPromise @[JS.JSVal] @Unicode "selectFile" [file, jopfsName]
162164
$ after
163165
. fmap strip
164166

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ data Full model action t f = Full
4646
data Opts model action = Opts
4747
{ optsIcon :: Icon.Icon -> View action,
4848
optsLabel :: Maybe Unicode,
49+
optsOpfsName :: Maybe Unicode,
4950
optsDisabled :: Bool,
5051
optsFullWidth :: Bool,
5152
optsPlaceholder :: Unicode,
5253
optsOnInputAction :: Maybe (Update model -> action),
54+
--
55+
-- TODO : optsTrailingWidgets :: [Unicode -> FocusedOrBlurred -> OptsWidget]
56+
--
5357
optsLeadingWidget :: Maybe (OptsWidgetPair model action),
5458
optsTrailingWidget :: Maybe (OptsWidgetPair model action),
5559
optsOnKeyDownAction :: Unicode -> KeyCode -> Update model,
@@ -70,6 +74,7 @@ defOpts =
7074
Opts
7175
{ optsIcon = Icon.icon @Icon.Fa,
7276
optsLabel = Nothing,
77+
optsOpfsName = Nothing,
7378
optsDisabled = False,
7479
optsFullWidth = False,
7580
optsPlaceholder = mempty,
@@ -350,7 +355,7 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
350355
then Jsm.popupText @Unicode "File does not exist!"
351356
else do
352357
file <- el JS.! ("files" :: Unicode) JS.!! 0
353-
Jsm.selectFile file $ \case
358+
Jsm.selectFile (optsOpfsName opts) file $ \case
354359
Nothing -> Jsm.popupText @Unicode "File is not selected!"
355360
Just url -> argsEmitter args . PureUpdate $ do
356361
let next =
@@ -456,7 +461,9 @@ fieldIcon full opts = \case
456461
)
457462
PasteWidget ->
458463
fieldIconSimple opts Icon.IconPaste mempty
459-
$ insertAction full Jsm.selectClipboard
464+
. insertAction full
465+
. Jsm.selectClipboard
466+
$ optsOpfsName opts
460467
ScanQrWidget ->
461468
fieldIconSimple opts Icon.IconQrCode mempty
462469
$ insertAction full Jsm.selectBarcode

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ data Args model action f = Args
2424
data Opts model action = Opts
2525
{ optsIcon :: Icon.Icon -> View action,
2626
optsField :: Field.Opts model action,
27+
optsOpfsName :: Maybe (Int -> Unicode),
2728
optsAdvanced :: Bool
2829
}
2930
deriving stock (Generic)
@@ -33,6 +34,7 @@ defOpts =
3334
Opts
3435
{ optsIcon = Icon.icon @Icon.Fa,
3536
optsField = Field.defOpts,
37+
optsOpfsName = Nothing,
3638
optsAdvanced = True
3739
}
3840

@@ -79,6 +81,8 @@ fieldPairViewer opts args@Args {argsOptic = optic} idx pair =
7981
else
8082
Field.fieldViewer
8183
( optsField opts
84+
& #optsOpfsName
85+
.~ fmap ($ idx) (optsOpfsName opts)
8286
& #optsIcon
8387
.~ optsIcon opts
8488
& #optsLeftRightViewer
@@ -132,6 +136,8 @@ fieldPairEditor
132136
Field.argsEmitter = emitter
133137
}
134138
( optsField opts
139+
& #optsOpfsName
140+
.~ fmap ($ idx) (optsOpfsName opts)
135141
& #optsLabel
136142
.~ Just
137143
( fromMaybe ("#" <> inspect (idx + 1))
@@ -161,6 +167,8 @@ fieldPairEditor
161167
Field.argsEmitter = emitter
162168
}
163169
( optsField opts
170+
& #optsOpfsName
171+
.~ fmap ($ idx) (optsOpfsName opts)
164172
& #optsPlaceholder
165173
.~ ("Label " <> idxTxt)
166174
& ( #optsLeadingWidget ::
@@ -186,6 +194,8 @@ fieldPairEditor
186194
Field.argsEmitter = emitter
187195
}
188196
( optsField opts
197+
& #optsOpfsName
198+
.~ fmap ($ idx) (optsOpfsName opts)
189199
& #optsPlaceholder
190200
.~ ( "Value "
191201
<> idxTxt

0 commit comments

Comments
 (0)