Skip to content
This repository was archived by the owner on Jul 19, 2022. It is now read-only.

Commit 0ed7e9e

Browse files
authored
Merge pull request #281 from unisonweb/route-driven-open-definitions
Ensure Definitions are opened via Route changes
2 parents b5ae8e3 + 7bd3f81 commit 0ed7e9e

File tree

3 files changed

+54
-48
lines changed

3 files changed

+54
-48
lines changed

src/App.elm

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,20 @@ update msg ({ env } as model) =
137137
( model, Cmd.none )
138138

139139
UrlChanged url ->
140-
-- URL changes happen when setting focus on a definitions.
141-
-- Currently, the URL change is a result of that as oppose to focus
142-
-- being a result of a URL change
143-
( { model | route = Route.fromUrl env.basePath url }, Cmd.none )
140+
let
141+
route =
142+
Route.fromUrl env.basePath url
143+
in
144+
case route of
145+
Route.Definition _ ref ->
146+
let
147+
( workspace, cmd ) =
148+
Workspace.open env model.workspace ref
149+
in
150+
( { model | route = route, workspace = workspace }, Cmd.map WorkspaceMsg cmd )
151+
152+
_ ->
153+
( { model | route = route }, Cmd.none )
144154

145155
ChangePerspective perspective ->
146156
replacePerspective model perspective
@@ -168,7 +178,7 @@ update msg ({ env } as model) =
168178
keydown model event
169179

170180
OpenDefinition ref ->
171-
openDefinition model ref
181+
navigateToDefinition model ref
172182

173183
ShowModal modal ->
174184
( { model | modal = modal }, Cmd.none )
@@ -203,7 +213,7 @@ update msg ({ env } as model) =
203213
in
204214
case outMsg of
205215
PerspectiveLanding.OpenDefinition ref ->
206-
openDefinition model2 ref
216+
navigateToDefinition model2 ref
207217

208218
PerspectiveLanding.ShowFinderRequest ->
209219
showFinder model2 Nothing
@@ -230,7 +240,7 @@ update msg ({ env } as model) =
230240
model4 =
231241
{ model2 | sidebarToggled = False }
232242
in
233-
openDefinition model4 ref
243+
navigateToDefinition model4 ref
234244

235245
CodebaseTree.ChangePerspectiveToNamespace fqn ->
236246
fqn
@@ -254,7 +264,7 @@ update msg ({ env } as model) =
254264
( { model | modal = NoModal }, Cmd.none )
255265

256266
Finder.OpenDefinition ref ->
257-
openDefinition { model | modal = NoModal } ref
267+
navigateToDefinition { model | modal = NoModal } ref
258268

259269
_ ->
260270
( model, Cmd.none )
@@ -271,19 +281,9 @@ update msg ({ env } as model) =
271281
-- UPDATE HELPERS
272282

273283

274-
openDefinition : Model -> Reference -> ( Model, Cmd Msg )
275-
openDefinition model ref =
276-
let
277-
( workspace, wCmd, outMsg ) =
278-
Workspace.open model.env model.workspace ref
279-
280-
model2 =
281-
{ model | workspace = workspace }
282-
283-
( model3, cmd ) =
284-
handleWorkspaceOutMsg model2 outMsg
285-
in
286-
( model3, Cmd.batch [ cmd, Cmd.map WorkspaceMsg wCmd ] )
284+
navigateToDefinition : Model -> Reference -> ( Model, Cmd Msg )
285+
navigateToDefinition model ref =
286+
( model, Route.navigateToDefinition model.navKey model.route ref )
287287

288288

289289
replacePerspective : Model -> Perspective -> ( Model, Cmd Msg )
@@ -336,7 +336,7 @@ handleWorkspaceOutMsg model out =
336336
showFinder model withinNamespace
337337

338338
Workspace.Focused ref ->
339-
( model, Route.navigateToByReference model.navKey model.route ref )
339+
( model, Route.navigateToDefinition model.navKey model.route ref )
340340

341341
Workspace.Emptied ->
342342
( model, Route.navigateToCurrentPerspective model.navKey model.route )

src/Route.elm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module Route exposing
22
( Route(..)
33
, fromUrl
44
, navigate
5-
, navigateToByReference
65
, navigateToCurrentPerspective
6+
, navigateToDefinition
77
, navigateToLatestCodebase
88
, navigateToPerspective
99
, perspectiveParams
@@ -263,8 +263,8 @@ navigateToLatestCodebase navKey =
263263
navigateToPerspective navKey (ByCodebase Relative)
264264

265265

266-
navigateToByReference : Nav.Key -> Route -> Reference -> Cmd msg
267-
navigateToByReference navKey currentRoute reference =
266+
navigateToDefinition : Nav.Key -> Route -> Reference -> Cmd msg
267+
navigateToDefinition navKey currentRoute reference =
268268
navigate navKey (toDefinition currentRoute reference)
269269

270270

src/Workspace.elm

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Perspective exposing (Perspective)
2727
import Task
2828
import UI.Button as Button
2929
import UI.Icon as Icon
30-
import Workspace.WorkspaceItem as WorkspaceItem exposing (Item, WorkspaceItem(..))
30+
import Workspace.WorkspaceItem as WorkspaceItem exposing (Item, WorkspaceItem)
3131
import Workspace.WorkspaceItems as WorkspaceItems exposing (WorkspaceItems)
3232

3333

@@ -55,7 +55,7 @@ init env mRef =
5555

5656
Just ref ->
5757
let
58-
( m, c, _ ) =
58+
( m, c ) =
5959
open env model ref
6060
in
6161
( m, c )
@@ -116,7 +116,7 @@ update env msg ({ workspaceItems } as model) =
116116
in
117117
( { model | workspaceItems = nextWorkspaceItems }
118118
, cmd
119-
, openDefinitionsFocusToOutMsg nextWorkspaceItems
119+
, None
120120
)
121121

122122
IsDocCropped ref res ->
@@ -157,7 +157,7 @@ update env msg ({ workspaceItems } as model) =
157157
WorkspaceItemMsg wiMsg ->
158158
case wiMsg of
159159
WorkspaceItem.OpenReference relativeToRef ref ->
160-
openItem env model (Just relativeToRef) ref
160+
openReference env model relativeToRef ref
161161

162162
WorkspaceItem.Close ref ->
163163
let
@@ -218,11 +218,6 @@ type alias WithWorkspaceItems m =
218218
{ m | workspaceItems : WorkspaceItems }
219219

220220

221-
open : Env -> WithWorkspaceItems m -> Reference -> ( WithWorkspaceItems m, Cmd Msg, OutMsg )
222-
open env model ref =
223-
openItem env model Nothing ref
224-
225-
226221
replaceWorkspaceItemReferencesWithHashOnly : Model -> Model
227222
replaceWorkspaceItemReferencesWithHashOnly model =
228223
let
@@ -232,7 +227,29 @@ replaceWorkspaceItemReferencesWithHashOnly model =
232227
{ model | workspaceItems = workspaceItems }
233228

234229

235-
openItem : Env -> WithWorkspaceItems m -> Maybe Reference -> Reference -> ( WithWorkspaceItems m, Cmd Msg, OutMsg )
230+
open : Env -> WithWorkspaceItems m -> Reference -> ( WithWorkspaceItems m, Cmd Msg )
231+
open env model ref =
232+
openItem env model Nothing ref
233+
234+
235+
{-| openReference opens a definition relative to another definition. This is
236+
done within Workspace, as opposed to from the outside via a URL change. This
237+
function returns a Focused command for the newly opened reference and as such
238+
changes the URL.
239+
-}
240+
openReference : Env -> WithWorkspaceItems m -> Reference -> Reference -> ( WithWorkspaceItems m, Cmd Msg, OutMsg )
241+
openReference env model relativeToRef ref =
242+
let
243+
( newModel, cmd ) =
244+
openItem env model (Just relativeToRef) ref
245+
246+
out =
247+
openDefinitionsFocusToOutMsg newModel.workspaceItems
248+
in
249+
( newModel, cmd, out )
250+
251+
252+
openItem : Env -> WithWorkspaceItems m -> Maybe Reference -> Reference -> ( WithWorkspaceItems m, Cmd Msg )
236253
openItem env ({ workspaceItems } as model) relativeToRef ref =
237254
-- We don't want to refetch or replace any already open definitions, but we
238255
-- do want to focus and scroll to it
@@ -243,7 +260,6 @@ openItem env ({ workspaceItems } as model) relativeToRef ref =
243260
in
244261
( { model | workspaceItems = nextWorkspaceItems }
245262
, scrollToDefinition ref
246-
, openDefinitionsFocusToOutMsg nextWorkspaceItems
247263
)
248264

249265
else
@@ -261,24 +277,14 @@ openItem env ({ workspaceItems } as model) relativeToRef ref =
261277
in
262278
( { model | workspaceItems = nextWorkspaceItems }
263279
, Cmd.batch [ Api.perform env.apiBasePath (fetchDefinition env.perspective ref), scrollToDefinition ref ]
264-
, openDefinitionsFocusToOutMsg nextWorkspaceItems
265280
)
266281

267282

268283
openDefinitionsFocusToOutMsg : WorkspaceItems -> OutMsg
269284
openDefinitionsFocusToOutMsg openDefs =
270-
let
271-
toFocusedOut workspaceItem =
272-
case workspaceItem of
273-
Success ref _ ->
274-
Focused ref
275-
276-
_ ->
277-
None
278-
in
279285
openDefs
280-
|> WorkspaceItems.focus
281-
|> Maybe.map toFocusedOut
286+
|> WorkspaceItems.focusedReference
287+
|> Maybe.map Focused
282288
|> Maybe.withDefault Emptied
283289

284290

0 commit comments

Comments
 (0)