-
Notifications
You must be signed in to change notification settings - Fork 50
Embed editor state in URL; remove session storage #299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
11d6215
ad9b25b
2d5c9bd
6eb73be
973b0ff
1f4b17a
409795b
0dfc21d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| }, | ||
| "dependencies": { | ||
| "ace-builds": "^1.5.0", | ||
| "jquery": "^1.12.4" | ||
| "jquery": "^1.12.4", | ||
| "lz-string": "^1.4.4" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ import Data.String (Pattern(..)) | |
| import Data.String.Regex as Regex | ||
| import Data.String.Regex.Flags as RegexFlags | ||
| import Effect (Effect) | ||
| import Effect.Aff (Aff, makeAff) | ||
| import Effect.Aff (Aff, Milliseconds(..), delay, makeAff) | ||
| import Effect.Aff as Aff | ||
| import Effect.Class.Console (error) | ||
| import Effect.Uncurried (EffectFn3, runEffectFn3) | ||
|
|
@@ -30,14 +30,14 @@ import Try.Editor (MarkerType(..), toStringMarkerType) | |
| import Try.Editor as Editor | ||
| import Try.Gist (getGistById, tryLoadFileFromGist) | ||
| import Try.GitHub (getRawGitHubFile) | ||
| import Try.QueryString (getQueryStringMaybe) | ||
| import Try.Session (createSessionIdIfNecessary, storeSession, tryRetrieveSession) | ||
| import Try.QueryString (compressToEncodedURIComponent, decompressFromEncodedURIComponent, getQueryStringMaybe, setQueryString) | ||
| import Try.SharedConfig as SharedConfig | ||
| import Type.Proxy (Proxy(..)) | ||
| import Web.HTML (window) | ||
| import Web.HTML.Window (alert) | ||
| import Web.HTML.Location (href) | ||
| import Web.HTML.Window (alert, location) | ||
|
|
||
| type Slots = ( editor :: Editor.Slot Unit ) | ||
| type Slots = ( editor :: Editor.Slot Unit, shareButton :: forall q o. H.Slot q o Unit ) | ||
|
|
||
| data SourceFile = GitHub String | Gist String | ||
|
|
||
|
|
@@ -84,10 +84,11 @@ data Action | |
| _editor :: Proxy "editor" | ||
| _editor = Proxy | ||
|
|
||
| type LoadCb = Effect Unit | ||
| type SucceedCb = Effect Unit | ||
| type FailCb = Effect Unit | ||
| foreign import setupIFrame :: EffectFn3 { code :: String } LoadCb FailCb Unit | ||
| foreign import setupIFrame :: EffectFn3 { code :: String } SucceedCb FailCb Unit | ||
| foreign import teardownIFrame :: Effect Unit | ||
| foreign import copyToClipboard :: EffectFn3 String SucceedCb FailCb Unit | ||
|
|
||
| component :: forall q i o. H.Component q i o Aff | ||
| component = H.mkComponent | ||
|
|
@@ -109,8 +110,7 @@ component = H.mkComponent | |
| handleAction :: Action -> H.HalogenM State Action Slots o Aff Unit | ||
| handleAction = case _ of | ||
| Initialize -> do | ||
| sessionId <- H.liftEffect $ createSessionIdIfNecessary | ||
| { code, sourceFile } <- H.liftAff $ withSession sessionId | ||
| { code, sourceFile } <- H.liftAff withSession | ||
|
|
||
| -- Load parameters | ||
| mbViewModeParam <- H.liftEffect $ getQueryStringMaybe "view" | ||
|
|
@@ -141,12 +141,7 @@ component = H.mkComponent | |
| handleAction $ Compile Nothing | ||
|
|
||
| Cache text -> H.liftEffect do | ||
| sessionId <- getQueryStringMaybe "session" | ||
| case sessionId of | ||
| Just sessionId_ -> do | ||
| storeSession sessionId_ { code: text } | ||
| Nothing -> | ||
| error "No session ID" | ||
| setQueryString "purs" $ compressToEncodedURIComponent text | ||
|
|
||
| Compile mbCode -> do | ||
| H.modify_ _ { compiled = Nothing } | ||
|
|
@@ -340,6 +335,7 @@ component = H.mkComponent | |
| ] | ||
| [ HH.text "Show JS" ] | ||
| ] | ||
| , HH.slot_ (Proxy :: _ "shareButton") unit shareButton unit | ||
| , HH.li | ||
| [ HP.class_ $ HH.ClassName "menu-item" ] | ||
| [ HH.a | ||
|
|
@@ -442,6 +438,36 @@ renderCompilerErrors errors = do | |
| , renderPlaintext message | ||
| ] | ||
|
|
||
| shareButton :: forall q i o. H.Component q i o Aff | ||
| shareButton = H.mkComponent | ||
| { initialState: \_ -> 0 | ||
| , render | ||
| , eval: H.mkEval $ H.defaultEval | ||
| { handleAction = handleAction | ||
| } | ||
| } | ||
| where | ||
| handleAction :: Unit -> H.HalogenM Int Unit () o Aff Unit | ||
| handleAction _ = do | ||
| url <- H.liftEffect $ window >>= location >>= href | ||
| H.liftAff $ makeAff \f -> do | ||
| runEffectFn3 copyToClipboard url (f (Right unit)) (f (Left $ Aff.error "Failed to copy to clipboard")) | ||
|
||
| mempty | ||
| H.modify_ (_ + 1) | ||
| H.liftAff $ delay (1_500.0 # Milliseconds) | ||
| H.modify_ (_ - 1) | ||
|
||
| render :: Int -> H.ComponentHTML Unit () Aff | ||
| render n = | ||
| HH.li | ||
| [ HP.class_ $ HH.ClassName "menu-item no-mobile" ] | ||
| [ HH.label | ||
| [ HP.id "share_label" | ||
| , HP.title "Share URL" | ||
| , HE.onClick \_ -> unit | ||
| ] | ||
| [ HH.text (if n > 0 then "✔️ Copied to clipboard" else "Share URL") ] | ||
| ] | ||
|
|
||
| menuRadio | ||
| :: forall w | ||
| . { name :: String | ||
|
|
@@ -505,13 +531,13 @@ toAnnotation markerType { position, message } = | |
| , text: message | ||
| } | ||
|
|
||
| withSession :: String -> Aff { sourceFile :: Maybe SourceFile, code :: String } | ||
| withSession sessionId = do | ||
| state <- H.liftEffect $ tryRetrieveSession sessionId | ||
| githubId <- H.liftEffect $ getQueryStringMaybe "github" | ||
| withSession :: Aff { sourceFile :: Maybe SourceFile, code :: String } | ||
| withSession = do | ||
|
Comment on lines
+546
to
+547
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also maybe the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are your thoughts on what this could be renamed to then?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't think of anything better 😅 so I'll just leave it |
||
| state <- H.liftEffect $ getQueryStringMaybe "purs" | ||
| githubId <- H.liftEffect $ getQueryStringMaybe "github" | ||
| gistId <- H.liftEffect $ getQueryStringMaybe "gist" | ||
| code <- case state of | ||
| Just { code } -> pure code | ||
| code <- case state >>= decompressFromEncodedURIComponent of | ||
| Just code -> pure code | ||
| Nothing -> do | ||
| let | ||
| action = oneOf | ||
|
|
||
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if
Cachestill makes sense as the action name, maybePersist?EncodeInURL?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EncodeInUrlsounds good to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed here 6eb73be