change localStorage scheme and also rpolly more I forgor

This commit is contained in:
mtgmonkey 2025-05-09 06:00:00 -04:00
parent d6d84423e0
commit 9ec9c55912
8 changed files with 186 additions and 73 deletions

View file

@ -1 +1,19 @@
app=Elm.Main.init({flags:localStorage.getItem('cookies')?JSON.parse(localStorage.getItem('cookies')):''});app.ports.setStorage.subscribe(function(c){localStorage.setItem('cookies',JSON.stringify(c))}); const flags =
{ Theme: storedObject('Theme')
, Prompt: storedObject('Prompt')
, Font: storedObject('Font')
, CookiesStored: storedObject('CookiesStored')
}
console.log(flags);
app = Elm.Main.init({flags:flags});
console.log(storedObject('cookies'));
app.ports.setStorage.subscribe(function(kc){const [k,c]=kc;localStorage.setItem(k,JSON.stringify(c))});
app.ports.getStorage.subscribe(function(k){
const n = k;
const d = storedObject(k);
const ret =
{ name: n
, data: d
};
app.ports.receiveStorageFromJS.send(ret)});
function storedObject(k) {return localStorage.getItem(k)?JSON.parse(localStorage.getItem(k)):null;};

File diff suppressed because one or more lines are too long

View file

@ -32,6 +32,7 @@ index compiledElmApp = renderHtml $ do
port :: Port port :: Port
port = 8080 port = 8080
adminContact :: String adminContact :: String
adminContact = "[Matrix] @mtgmonkey:calitabby.net" adminContact = "[Matrix] @mtgmonkey:calitabby.net"

View file

@ -16,10 +16,10 @@ data Font = Font
deriving (Elm, ToJSON, FromJSON) via ElmStreet Font deriving (Elm, ToJSON, FromJSON) via ElmStreet Font
data Cookies = Cookies data Cookies = Cookies
{ cookiesFont :: Font { cookiesCookiesKept :: CookiesKept
, cookiesCookiesKept :: CookiesKept , cookiesFont :: Font
, cookiesTheme :: Theme
, cookiesPrompt :: Prompt , cookiesPrompt :: Prompt
, cookiesTheme :: Theme
} deriving (Generic) } deriving (Generic)
deriving (Elm, ToJSON, FromJSON) via ElmStreet Cookies deriving (Elm, ToJSON, FromJSON) via ElmStreet Cookies
@ -71,12 +71,13 @@ data ThemeColor
deriving (Elm, ToJSON, FromJSON) via ElmStreet ThemeColor deriving (Elm, ToJSON, FromJSON) via ElmStreet ThemeColor
data Command data Command
= HelpCommand = ClearCommand
| ClearCommand
| ColorsCommand | ColorsCommand
| CookiesCommand | CookiesCommand
| DebugCommand
| FontCommand | FontCommand
| HelloCommand | HelloCommand
| HelpCommand
| PromptCommand | PromptCommand
| ThemeCommand | ThemeCommand
| TodoCommand | TodoCommand

View file

@ -14,10 +14,10 @@ decodeFont = D.succeed T.Font
decodeCookies : Decoder T.Cookies decodeCookies : Decoder T.Cookies
decodeCookies = D.succeed T.Cookies decodeCookies = D.succeed T.Cookies
|> required "font" decodeFont
|> required "cookiesKept" decodeCookiesKept |> required "cookiesKept" decodeCookiesKept
|> required "theme" decodeTheme |> required "font" decodeFont
|> required "prompt" decodePrompt |> required "prompt" decodePrompt
|> required "theme" decodeTheme
decodeTheme : Decoder T.Theme decodeTheme : Decoder T.Theme
decodeTheme = elmStreetDecodeEnum T.readTheme decodeTheme = elmStreetDecodeEnum T.readTheme

View file

@ -16,10 +16,10 @@ encodeFont x = E.object
encodeCookies : T.Cookies -> Value encodeCookies : T.Cookies -> Value
encodeCookies x = E.object encodeCookies x = E.object
[ ("tag", E.string "Cookies") [ ("tag", E.string "Cookies")
, ("font", encodeFont x.font)
, ("cookiesKept", encodeCookiesKept x.cookiesKept) , ("cookiesKept", encodeCookiesKept x.cookiesKept)
, ("theme", encodeTheme x.theme) , ("font", encodeFont x.font)
, ("prompt", encodePrompt x.prompt) , ("prompt", encodePrompt x.prompt)
, ("theme", encodeTheme x.theme)
] ]
encodeTheme : T.Theme -> Value encodeTheme : T.Theme -> Value

View file

@ -9,10 +9,10 @@ type alias Font =
} }
type alias Cookies = type alias Cookies =
{ font : Font { cookiesKept : CookiesKept
, cookiesKept : CookiesKept , font : Font
, theme : Theme
, prompt : Prompt , prompt : Prompt
, theme : Theme
} }
type Theme type Theme
@ -148,48 +148,52 @@ universeThemeColor = [ Background
, BrightWhite ] , BrightWhite ]
type Command type Command
= HelpCommand = ClearCommand
| ClearCommand
| ColorsCommand | ColorsCommand
| CookiesCommand | CookiesCommand
| DebugCommand
| FontCommand | FontCommand
| HelloCommand | HelloCommand
| HelpCommand
| PromptCommand | PromptCommand
| ThemeCommand | ThemeCommand
| TodoCommand | TodoCommand
showCommand : Command -> String showCommand : Command -> String
showCommand x = case x of showCommand x = case x of
HelpCommand -> "HelpCommand"
ClearCommand -> "ClearCommand" ClearCommand -> "ClearCommand"
ColorsCommand -> "ColorsCommand" ColorsCommand -> "ColorsCommand"
CookiesCommand -> "CookiesCommand" CookiesCommand -> "CookiesCommand"
DebugCommand -> "DebugCommand"
FontCommand -> "FontCommand" FontCommand -> "FontCommand"
HelloCommand -> "HelloCommand" HelloCommand -> "HelloCommand"
HelpCommand -> "HelpCommand"
PromptCommand -> "PromptCommand" PromptCommand -> "PromptCommand"
ThemeCommand -> "ThemeCommand" ThemeCommand -> "ThemeCommand"
TodoCommand -> "TodoCommand" TodoCommand -> "TodoCommand"
readCommand : String -> Maybe Command readCommand : String -> Maybe Command
readCommand x = case x of readCommand x = case x of
"HelpCommand" -> Just HelpCommand
"ClearCommand" -> Just ClearCommand "ClearCommand" -> Just ClearCommand
"ColorsCommand" -> Just ColorsCommand "ColorsCommand" -> Just ColorsCommand
"CookiesCommand" -> Just CookiesCommand "CookiesCommand" -> Just CookiesCommand
"DebugCommand" -> Just DebugCommand
"FontCommand" -> Just FontCommand "FontCommand" -> Just FontCommand
"HelloCommand" -> Just HelloCommand "HelloCommand" -> Just HelloCommand
"HelpCommand" -> Just HelpCommand
"PromptCommand" -> Just PromptCommand "PromptCommand" -> Just PromptCommand
"ThemeCommand" -> Just ThemeCommand "ThemeCommand" -> Just ThemeCommand
"TodoCommand" -> Just TodoCommand "TodoCommand" -> Just TodoCommand
_ -> Nothing _ -> Nothing
universeCommand : List Command universeCommand : List Command
universeCommand = [ HelpCommand universeCommand = [ ClearCommand
, ClearCommand
, ColorsCommand , ColorsCommand
, CookiesCommand , CookiesCommand
, DebugCommand
, FontCommand , FontCommand
, HelloCommand , HelloCommand
, HelpCommand
, PromptCommand , PromptCommand
, ThemeCommand , ThemeCommand
, TodoCommand ] , TodoCommand ]

View file

@ -70,35 +70,37 @@ init flags url key =
, text "\nRun `help` to get started" , text "\nRun `help` to get started"
] ]
localStorage = th =
case D.decodeValue decodeCookies flags of case D.decodeValue (D.field "Theme" decodeTheme) flags of
Ok cookies -> Ok val ->
cookies val
Err _ -> Err _ ->
defaultCookies defaultCookies.theme
pr =
case D.decodeValue (D.field "Prompt" decodePrompt) flags of
Ok val ->
val
Err _ ->
defaultCookies.prompt
cK =
case D.decodeValue (D.field "CookiesKept" decodeCookiesKept) flags of
Ok val ->
val
Err _ ->
defaultCookies.cookiesKept
fo =
case D.decodeValue (D.field "Font" decodeFont) flags of
Ok val ->
val
Err _ ->
defaultCookies.font
in in
( { key = key ( { key = key
, url = url , url = url
, theme = , theme = th
if localStorage.cookiesKept.keepTheme then , font = fo
localStorage.theme , cookiesKept = cK
, prompt = pr
else
defaultCookies.theme
, font =
if localStorage.cookiesKept.keepFont then
localStorage.font
else
defaultCookies.font
, cookiesKept = localStorage.cookiesKept
, prompt =
if localStorage.cookiesKept.keepPrompt then
localStorage.prompt
else
defaultCookies.prompt
, content = initContent , content = initContent
, cliContent = "" , cliContent = ""
} }
@ -115,6 +117,7 @@ type Msg
| UrlChanged Url.Url | UrlChanged Url.Url
| TakeInput String | TakeInput String
| NoInitFocus | NoInitFocus
| ReceivedStorage E.Value
update : Msg -> Model -> ( Model, Cmd Msg ) update : Msg -> Model -> ( Model, Cmd Msg )
@ -159,6 +162,58 @@ update msg model =
NoInitFocus -> NoInitFocus ->
( model, Cmd.none ) ( model, Cmd.none )
ReceivedStorage value ->
applyJSONData model value
applyJSONData : Model -> E.Value -> ( Model, Cmd Msg )
applyJSONData model data =
case D.decodeValue (D.field "name" D.string) data of
Ok "Theme" ->
case D.decodeValue (D.field "data" decodeTheme) data of
Ok th ->
( { model | theme = th }, Cmd.none )
Err e ->
errApplyingJSON model e
Ok "Prompt" ->
case D.decodeValue (D.field "data" decodePrompt) data of
Ok pr ->
( { model | prompt = pr }, Cmd.none )
Err e ->
errApplyingJSON model e
Ok "Font" ->
case D.decodeValue (D.field "data" decodeFont) data of
Ok fo ->
( { model | font = fo }, Cmd.none )
Err e ->
errApplyingJSON model e
Ok _ ->
( { model | content = model.content ++ [ text "expecting field `name` to contain type while applyJSONData" ] }, Cmd.none )
Err e ->
errApplyingJSON model e
errApplyingJSON : Model -> D.Error -> ( Model, Cmd Msg )
errApplyingJSON model e =
( { model | content = model.content ++ [ coloredText (coreColor Red) <| "\n" ++ D.errorToString e ] }, Cmd.none )
isOK : Result x a -> Bool
isOK res =
case res of
Ok _ ->
True
Err _ ->
False
-- COMMANDS -- COMMANDS
@ -184,6 +239,9 @@ parseInput input =
Just "cookies" -> Just "cookies" ->
Ok CookiesCommand Ok CookiesCommand
Just "debug" ->
Ok DebugCommand
Just "font" -> Just "font" ->
Ok FontCommand Ok FontCommand
@ -243,6 +301,9 @@ runCommand model input =
CookiesCommand -> CookiesCommand ->
runCookies runCookies
DebugCommand ->
runDebug
FontCommand -> FontCommand ->
runFont runFont
@ -400,13 +461,7 @@ runCookies : CommandRunner
runCookies model args = runCookies model args =
case List.head args of case List.head args of
Nothing -> Nothing ->
let ( { model | content = model.content ++ [ text <| "\n" ++ cookiesKeptToString model.cookiesKept ] }, Cmd.none )
newModel =
{ model | content = model.content ++ [ text <| "\n" ++ cookiesKeptToString model.cookiesKept ] }
in
( newModel
, Cmd.batch [ setStorage <| encodeModel newModel ]
)
Just "set" -> Just "set" ->
let let
@ -445,10 +500,10 @@ runCookies model args =
"keepFont" -> "keepFont" ->
case third of case third of
"true" -> "true" ->
saveModel { model | cookiesKept = { cookiesKept | keepFont = True } } saveCookiesKept { model | cookiesKept = { cookiesKept | keepFont = True } }
"false" -> "false" ->
saveModel { model | cookiesKept = { cookiesKept | keepFont = False } } saveCookiesKept { model | cookiesKept = { cookiesKept | keepFont = False } }
_ -> _ ->
( { model | content = model.content ++ wrongArgs CookiesCommand 3 args }, Cmd.none ) ( { model | content = model.content ++ wrongArgs CookiesCommand 3 args }, Cmd.none )
@ -456,10 +511,10 @@ runCookies model args =
"keepTheme" -> "keepTheme" ->
case third of case third of
"true" -> "true" ->
saveModel { model | cookiesKept = { cookiesKept | keepTheme = True } } saveCookiesKept { model | cookiesKept = { cookiesKept | keepTheme = True } }
"false" -> "false" ->
saveModel { model | cookiesKept = { cookiesKept | keepTheme = False } } saveCookiesKept { model | cookiesKept = { cookiesKept | keepTheme = False } }
_ -> _ ->
( { model | content = model.content ++ wrongArgs CookiesCommand 3 args }, Cmd.none ) ( { model | content = model.content ++ wrongArgs CookiesCommand 3 args }, Cmd.none )
@ -467,10 +522,10 @@ runCookies model args =
"keepPrompt" -> "keepPrompt" ->
case third of case third of
"true" -> "true" ->
saveModel { model | cookiesKept = { cookiesKept | keepPrompt = True } } saveCookiesKept { model | cookiesKept = { cookiesKept | keepPrompt = True } }
"false" -> "false" ->
saveModel { model | cookiesKept = { cookiesKept | keepPrompt = False } } saveCookiesKept { model | cookiesKept = { cookiesKept | keepPrompt = False } }
_ -> _ ->
( { model | content = model.content ++ wrongArgs CookiesCommand 3 args }, Cmd.none ) ( { model | content = model.content ++ wrongArgs CookiesCommand 3 args }, Cmd.none )
@ -482,6 +537,11 @@ runCookies model args =
( { model | content = model.content ++ wrongArgs CookiesCommand 1 args }, Cmd.none ) ( { model | content = model.content ++ wrongArgs CookiesCommand 1 args }, Cmd.none )
runDebug : CommandRunner
runDebug model args =
( model, getStorage "Theme" )
runHello : CommandRunner runHello : CommandRunner
runHello model args = runHello model args =
case List.head args of case List.head args of
@ -527,7 +587,7 @@ runFont model args =
, font = { fontSize = float } , font = { fontSize = float }
} }
in in
saveModel newModel saveFont newModel
else else
( { model ( { model
@ -555,7 +615,7 @@ runFont model args =
, font = { fontSize = 20 } , font = { fontSize = 20 }
} }
in in
saveModel newModel saveFont newModel
_ -> _ ->
( { model ( { model
@ -578,11 +638,7 @@ runFont model args =
, font = { fontSize = 20 } , font = { fontSize = 20 }
} }
in in
( newModel saveFont newModel
, Cmd.batch
[ setStorage <| encodeModel newModel
]
)
Just string -> Just string ->
( { model | content = model.content ++ wrongArgs FontCommand 1 args }, Cmd.none ) ( { model | content = model.content ++ wrongArgs FontCommand 1 args }, Cmd.none )
@ -599,7 +655,7 @@ runPrompt model args =
oldPrompt = oldPrompt =
model.prompt model.prompt
in in
saveModel { model | prompt = { oldPrompt | prompt = string } } savePrompt { model | prompt = { oldPrompt | prompt = string } }
runTheme : CommandRunner runTheme : CommandRunner
@ -686,7 +742,7 @@ setTheme model theme =
newModel = newModel =
{ model | theme = theme } { model | theme = theme }
in in
saveModel newModel saveTheme newModel
wrongArgs : Command -> Int -> List String -> List (Html Msg) wrongArgs : Command -> Int -> List String -> List (Html Msg)
@ -706,6 +762,9 @@ wrongArgs command expected args =
CookiesCommand -> CookiesCommand ->
"cookies" "cookies"
DebugCommand ->
"debug"
FontCommand -> FontCommand ->
"font" "font"
@ -764,9 +823,29 @@ cookiesKeptToString cookiesKept =
++ "\n}" ++ "\n}"
saveModel : Model -> ( Model, Cmd Msg ) saveCookiesKept : Model -> ( Model, Cmd Msg )
saveModel model = saveCookiesKept model =
( model, Cmd.batch [ setStorage <| encodeModel model ] ) ( model, setStorage ( "CookiesKept", encodeCookiesKept model.cookiesKept ) )
saveFont : Model -> ( Model, Cmd Msg )
saveFont model =
( model, setStorage ( "Font", encodeFont model.font ) )
savePrompt : Model -> ( Model, Cmd Msg )
savePrompt model =
( model, setStorage ( "Prompt", encodePrompt model.prompt ) )
saveTheme : Model -> ( Model, Cmd Msg )
saveTheme model =
( model, setStorage ( "Theme", encodeTheme model.theme ) )
loadStorage : Model -> String -> ( Model, Cmd Msg )
loadStorage model key =
( model, getStorage key )
@ -774,7 +853,13 @@ saveModel model =
-- sets localStorage 'cookies' to E.Value -- sets localStorage 'cookies' to E.Value
port setStorage : E.Value -> Cmd a port setStorage : ( String, E.Value ) -> Cmd a
port getStorage : String -> Cmd a
port receiveStorageFromJS : (E.Value -> msg) -> Sub msg
@ -791,12 +876,14 @@ encodeModel model =
, prompt = model.prompt , prompt = model.prompt
} }
-- SUBSCRIPTIONS -- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg subscriptions : Model -> Sub Msg
subscriptions _ = subscriptions model =
Sub.none receiveStorageFromJS ReceivedStorage
@ -825,6 +912,8 @@ viewBody model =
-- STYLES -- STYLES
allColors : Model -> List Color allColors : Model -> List Color
allColors model = allColors model =
List.map List.map