change localStorage scheme and also rpolly more I forgor
This commit is contained in:
parent
d6d84423e0
commit
9ec9c55912
8 changed files with 186 additions and 73 deletions
|
@ -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
|
@ -32,6 +32,7 @@ index compiledElmApp = renderHtml $ do
|
|||
port :: Port
|
||||
port = 8080
|
||||
|
||||
|
||||
adminContact :: String
|
||||
adminContact = "[Matrix] @mtgmonkey:calitabby.net"
|
||||
|
||||
|
|
|
@ -16,10 +16,10 @@ data Font = Font
|
|||
deriving (Elm, ToJSON, FromJSON) via ElmStreet Font
|
||||
|
||||
data Cookies = Cookies
|
||||
{ cookiesFont :: Font
|
||||
, cookiesCookiesKept :: CookiesKept
|
||||
, cookiesTheme :: Theme
|
||||
{ cookiesCookiesKept :: CookiesKept
|
||||
, cookiesFont :: Font
|
||||
, cookiesPrompt :: Prompt
|
||||
, cookiesTheme :: Theme
|
||||
} deriving (Generic)
|
||||
deriving (Elm, ToJSON, FromJSON) via ElmStreet Cookies
|
||||
|
||||
|
@ -71,12 +71,13 @@ data ThemeColor
|
|||
deriving (Elm, ToJSON, FromJSON) via ElmStreet ThemeColor
|
||||
|
||||
data Command
|
||||
= HelpCommand
|
||||
| ClearCommand
|
||||
= ClearCommand
|
||||
| ColorsCommand
|
||||
| CookiesCommand
|
||||
| DebugCommand
|
||||
| FontCommand
|
||||
| HelloCommand
|
||||
| HelpCommand
|
||||
| PromptCommand
|
||||
| ThemeCommand
|
||||
| TodoCommand
|
||||
|
|
|
@ -14,10 +14,10 @@ decodeFont = D.succeed T.Font
|
|||
|
||||
decodeCookies : Decoder T.Cookies
|
||||
decodeCookies = D.succeed T.Cookies
|
||||
|> required "font" decodeFont
|
||||
|> required "cookiesKept" decodeCookiesKept
|
||||
|> required "theme" decodeTheme
|
||||
|> required "font" decodeFont
|
||||
|> required "prompt" decodePrompt
|
||||
|> required "theme" decodeTheme
|
||||
|
||||
decodeTheme : Decoder T.Theme
|
||||
decodeTheme = elmStreetDecodeEnum T.readTheme
|
||||
|
|
|
@ -16,10 +16,10 @@ encodeFont x = E.object
|
|||
encodeCookies : T.Cookies -> Value
|
||||
encodeCookies x = E.object
|
||||
[ ("tag", E.string "Cookies")
|
||||
, ("font", encodeFont x.font)
|
||||
, ("cookiesKept", encodeCookiesKept x.cookiesKept)
|
||||
, ("theme", encodeTheme x.theme)
|
||||
, ("font", encodeFont x.font)
|
||||
, ("prompt", encodePrompt x.prompt)
|
||||
, ("theme", encodeTheme x.theme)
|
||||
]
|
||||
|
||||
encodeTheme : T.Theme -> Value
|
||||
|
|
|
@ -9,10 +9,10 @@ type alias Font =
|
|||
}
|
||||
|
||||
type alias Cookies =
|
||||
{ font : Font
|
||||
, cookiesKept : CookiesKept
|
||||
, theme : Theme
|
||||
{ cookiesKept : CookiesKept
|
||||
, font : Font
|
||||
, prompt : Prompt
|
||||
, theme : Theme
|
||||
}
|
||||
|
||||
type Theme
|
||||
|
@ -148,48 +148,52 @@ universeThemeColor = [ Background
|
|||
, BrightWhite ]
|
||||
|
||||
type Command
|
||||
= HelpCommand
|
||||
| ClearCommand
|
||||
= ClearCommand
|
||||
| ColorsCommand
|
||||
| CookiesCommand
|
||||
| DebugCommand
|
||||
| FontCommand
|
||||
| HelloCommand
|
||||
| HelpCommand
|
||||
| PromptCommand
|
||||
| ThemeCommand
|
||||
| TodoCommand
|
||||
|
||||
showCommand : Command -> String
|
||||
showCommand x = case x of
|
||||
HelpCommand -> "HelpCommand"
|
||||
ClearCommand -> "ClearCommand"
|
||||
ColorsCommand -> "ColorsCommand"
|
||||
CookiesCommand -> "CookiesCommand"
|
||||
DebugCommand -> "DebugCommand"
|
||||
FontCommand -> "FontCommand"
|
||||
HelloCommand -> "HelloCommand"
|
||||
HelpCommand -> "HelpCommand"
|
||||
PromptCommand -> "PromptCommand"
|
||||
ThemeCommand -> "ThemeCommand"
|
||||
TodoCommand -> "TodoCommand"
|
||||
|
||||
readCommand : String -> Maybe Command
|
||||
readCommand x = case x of
|
||||
"HelpCommand" -> Just HelpCommand
|
||||
"ClearCommand" -> Just ClearCommand
|
||||
"ColorsCommand" -> Just ColorsCommand
|
||||
"CookiesCommand" -> Just CookiesCommand
|
||||
"DebugCommand" -> Just DebugCommand
|
||||
"FontCommand" -> Just FontCommand
|
||||
"HelloCommand" -> Just HelloCommand
|
||||
"HelpCommand" -> Just HelpCommand
|
||||
"PromptCommand" -> Just PromptCommand
|
||||
"ThemeCommand" -> Just ThemeCommand
|
||||
"TodoCommand" -> Just TodoCommand
|
||||
_ -> Nothing
|
||||
|
||||
universeCommand : List Command
|
||||
universeCommand = [ HelpCommand
|
||||
, ClearCommand
|
||||
universeCommand = [ ClearCommand
|
||||
, ColorsCommand
|
||||
, CookiesCommand
|
||||
, DebugCommand
|
||||
, FontCommand
|
||||
, HelloCommand
|
||||
, HelpCommand
|
||||
, PromptCommand
|
||||
, ThemeCommand
|
||||
, TodoCommand ]
|
||||
|
|
|
@ -70,35 +70,37 @@ init flags url key =
|
|||
, text "\nRun `help` to get started"
|
||||
]
|
||||
|
||||
localStorage =
|
||||
case D.decodeValue decodeCookies flags of
|
||||
Ok cookies ->
|
||||
cookies
|
||||
|
||||
th =
|
||||
case D.decodeValue (D.field "Theme" decodeTheme) flags of
|
||||
Ok val ->
|
||||
val
|
||||
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
|
||||
( { key = key
|
||||
, url = url
|
||||
, theme =
|
||||
if localStorage.cookiesKept.keepTheme then
|
||||
localStorage.theme
|
||||
|
||||
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
|
||||
, theme = th
|
||||
, font = fo
|
||||
, cookiesKept = cK
|
||||
, prompt = pr
|
||||
, content = initContent
|
||||
, cliContent = ""
|
||||
}
|
||||
|
@ -115,6 +117,7 @@ type Msg
|
|||
| UrlChanged Url.Url
|
||||
| TakeInput String
|
||||
| NoInitFocus
|
||||
| ReceivedStorage E.Value
|
||||
|
||||
|
||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||
|
@ -159,6 +162,58 @@ update msg model =
|
|||
NoInitFocus ->
|
||||
( 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
|
||||
|
@ -184,6 +239,9 @@ parseInput input =
|
|||
Just "cookies" ->
|
||||
Ok CookiesCommand
|
||||
|
||||
Just "debug" ->
|
||||
Ok DebugCommand
|
||||
|
||||
Just "font" ->
|
||||
Ok FontCommand
|
||||
|
||||
|
@ -243,6 +301,9 @@ runCommand model input =
|
|||
CookiesCommand ->
|
||||
runCookies
|
||||
|
||||
DebugCommand ->
|
||||
runDebug
|
||||
|
||||
FontCommand ->
|
||||
runFont
|
||||
|
||||
|
@ -400,13 +461,7 @@ runCookies : CommandRunner
|
|||
runCookies model args =
|
||||
case List.head args of
|
||||
Nothing ->
|
||||
let
|
||||
newModel =
|
||||
{ model | content = model.content ++ [ text <| "\n" ++ cookiesKeptToString model.cookiesKept ] }
|
||||
in
|
||||
( newModel
|
||||
, Cmd.batch [ setStorage <| encodeModel newModel ]
|
||||
)
|
||||
( { model | content = model.content ++ [ text <| "\n" ++ cookiesKeptToString model.cookiesKept ] }, Cmd.none )
|
||||
|
||||
Just "set" ->
|
||||
let
|
||||
|
@ -445,10 +500,10 @@ runCookies model args =
|
|||
"keepFont" ->
|
||||
case third of
|
||||
"true" ->
|
||||
saveModel { model | cookiesKept = { cookiesKept | keepFont = True } }
|
||||
saveCookiesKept { model | cookiesKept = { cookiesKept | keepFont = True } }
|
||||
|
||||
"false" ->
|
||||
saveModel { model | cookiesKept = { cookiesKept | keepFont = False } }
|
||||
saveCookiesKept { model | cookiesKept = { cookiesKept | keepFont = False } }
|
||||
|
||||
_ ->
|
||||
( { model | content = model.content ++ wrongArgs CookiesCommand 3 args }, Cmd.none )
|
||||
|
@ -456,10 +511,10 @@ runCookies model args =
|
|||
"keepTheme" ->
|
||||
case third of
|
||||
"true" ->
|
||||
saveModel { model | cookiesKept = { cookiesKept | keepTheme = True } }
|
||||
saveCookiesKept { model | cookiesKept = { cookiesKept | keepTheme = True } }
|
||||
|
||||
"false" ->
|
||||
saveModel { model | cookiesKept = { cookiesKept | keepTheme = False } }
|
||||
saveCookiesKept { model | cookiesKept = { cookiesKept | keepTheme = False } }
|
||||
|
||||
_ ->
|
||||
( { model | content = model.content ++ wrongArgs CookiesCommand 3 args }, Cmd.none )
|
||||
|
@ -467,10 +522,10 @@ runCookies model args =
|
|||
"keepPrompt" ->
|
||||
case third of
|
||||
"true" ->
|
||||
saveModel { model | cookiesKept = { cookiesKept | keepPrompt = True } }
|
||||
saveCookiesKept { model | cookiesKept = { cookiesKept | keepPrompt = True } }
|
||||
|
||||
"false" ->
|
||||
saveModel { model | cookiesKept = { cookiesKept | keepPrompt = False } }
|
||||
saveCookiesKept { model | cookiesKept = { cookiesKept | keepPrompt = False } }
|
||||
|
||||
_ ->
|
||||
( { 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 )
|
||||
|
||||
|
||||
runDebug : CommandRunner
|
||||
runDebug model args =
|
||||
( model, getStorage "Theme" )
|
||||
|
||||
|
||||
runHello : CommandRunner
|
||||
runHello model args =
|
||||
case List.head args of
|
||||
|
@ -527,7 +587,7 @@ runFont model args =
|
|||
, font = { fontSize = float }
|
||||
}
|
||||
in
|
||||
saveModel newModel
|
||||
saveFont newModel
|
||||
|
||||
else
|
||||
( { model
|
||||
|
@ -555,7 +615,7 @@ runFont model args =
|
|||
, font = { fontSize = 20 }
|
||||
}
|
||||
in
|
||||
saveModel newModel
|
||||
saveFont newModel
|
||||
|
||||
_ ->
|
||||
( { model
|
||||
|
@ -578,11 +638,7 @@ runFont model args =
|
|||
, font = { fontSize = 20 }
|
||||
}
|
||||
in
|
||||
( newModel
|
||||
, Cmd.batch
|
||||
[ setStorage <| encodeModel newModel
|
||||
]
|
||||
)
|
||||
saveFont newModel
|
||||
|
||||
Just string ->
|
||||
( { model | content = model.content ++ wrongArgs FontCommand 1 args }, Cmd.none )
|
||||
|
@ -599,7 +655,7 @@ runPrompt model args =
|
|||
oldPrompt =
|
||||
model.prompt
|
||||
in
|
||||
saveModel { model | prompt = { oldPrompt | prompt = string } }
|
||||
savePrompt { model | prompt = { oldPrompt | prompt = string } }
|
||||
|
||||
|
||||
runTheme : CommandRunner
|
||||
|
@ -686,7 +742,7 @@ setTheme model theme =
|
|||
newModel =
|
||||
{ model | theme = theme }
|
||||
in
|
||||
saveModel newModel
|
||||
saveTheme newModel
|
||||
|
||||
|
||||
wrongArgs : Command -> Int -> List String -> List (Html Msg)
|
||||
|
@ -706,6 +762,9 @@ wrongArgs command expected args =
|
|||
CookiesCommand ->
|
||||
"cookies"
|
||||
|
||||
DebugCommand ->
|
||||
"debug"
|
||||
|
||||
FontCommand ->
|
||||
"font"
|
||||
|
||||
|
@ -764,9 +823,29 @@ cookiesKeptToString cookiesKept =
|
|||
++ "\n}"
|
||||
|
||||
|
||||
saveModel : Model -> ( Model, Cmd Msg )
|
||||
saveModel model =
|
||||
( model, Cmd.batch [ setStorage <| encodeModel model ] )
|
||||
saveCookiesKept : Model -> ( Model, Cmd Msg )
|
||||
saveCookiesKept 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
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
|
||||
-- SUBSCRIPTIONS
|
||||
|
||||
|
||||
subscriptions : Model -> Sub Msg
|
||||
subscriptions _ =
|
||||
Sub.none
|
||||
subscriptions model =
|
||||
receiveStorageFromJS ReceivedStorage
|
||||
|
||||
|
||||
|
||||
|
@ -825,6 +912,8 @@ viewBody model =
|
|||
|
||||
|
||||
-- STYLES
|
||||
|
||||
|
||||
allColors : Model -> List Color
|
||||
allColors model =
|
||||
List.map
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue