Skip to content

Commit 4ed01fa

Browse files
committed
Add support for expanding collapsed diff sections
We now collapse diff sections that don't have any changes, but now, if users want more context, they can expand and see everything.
1 parent 32f070d commit 4ed01fa

File tree

7 files changed

+333
-28
lines changed

7 files changed

+333
-28
lines changed

src/UnisonShare/BranchDiff.elm

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ type alias BranchDiff =
2828
-- HELPERS
2929

3030

31+
mapChangeLines : (ChangeLine -> ChangeLine) -> BranchDiff -> BranchDiff
32+
mapChangeLines f bd =
33+
{ bd | lines = List.map f bd.lines }
34+
35+
36+
updateChangeLineById : (ChangeLine -> ChangeLine) -> ChangeLineId -> BranchDiff -> BranchDiff
37+
updateChangeLineById f id bd =
38+
let
39+
updateIfMatched cl =
40+
if ChangeLine.matchesId id cl then
41+
f cl
42+
43+
else
44+
cl
45+
46+
go cl =
47+
case cl of
48+
Namespace items ->
49+
Namespace { items | lines = List.map go items.lines }
50+
51+
_ ->
52+
updateIfMatched cl
53+
in
54+
mapChangeLines go bd
55+
56+
3157
size : BranchDiff -> Int
3258
size branchDiff =
3359
branchDiff |> summary |> .numChanges

src/UnisonShare/BranchDiffState.elm

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import Http
55
import Json.Decode as Decode exposing (Decoder)
66
import Lib.Decode.Helpers exposing (whenFieldIs, whenPathIs)
77
import UnisonShare.BranchDiff as BranchDiff exposing (BranchDiff)
8+
import UnisonShare.BranchDiff.ChangeLine exposing (ChangeLine)
9+
import UnisonShare.BranchDiff.ChangeLineId exposing (ChangeLineId)
810

911

1012
type DiffErrorCulprit
@@ -41,7 +43,30 @@ type BranchDiffState
4143

4244

4345

44-
--
46+
-- HELPERS
47+
48+
49+
mapChangeLines : (ChangeLine -> ChangeLine) -> BranchDiffState -> BranchDiffState
50+
mapChangeLines f bds =
51+
case bds of
52+
Computed bd ->
53+
Computed (BranchDiff.mapChangeLines f bd)
54+
55+
_ ->
56+
bds
57+
58+
59+
updateChangeLineById : (ChangeLine -> ChangeLine) -> ChangeLineId -> BranchDiffState -> BranchDiffState
60+
updateChangeLineById f id bds =
61+
case bds of
62+
Computed bd ->
63+
Computed (BranchDiff.updateChangeLineById f id bd)
64+
65+
_ ->
66+
bds
67+
68+
69+
4570
-- DECODE
4671

4772

src/UnisonShare/DefinitionDiff.elm

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,37 @@ definitionTypeToString type_ =
7575
"Type"
7676

7777

78+
79+
-- Collapse
80+
81+
82+
expandCollapsedDiffSection : Int -> DefinitionDiff -> DefinitionDiff
83+
expandCollapsedDiffSection index diff =
84+
let
85+
expand i line =
86+
if i == index then
87+
case line of
88+
Collapsed diffLines ->
89+
NotCollapsed diffLines
90+
91+
_ ->
92+
line
93+
94+
else
95+
line
96+
in
97+
case diff of
98+
Diff details ->
99+
Diff
100+
{ details
101+
| left = List.indexedMap expand details.left
102+
, right = List.indexedMap expand details.right
103+
}
104+
105+
_ ->
106+
diff
107+
108+
78109
isCollapsable : DiffLine -> Bool
79110
isCollapsable l =
80111
case l of

src/UnisonShare/DefinitionDiffCard.elm

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import Html exposing (Html, code, div, header, pre, span, text)
77
import Html.Attributes exposing (class, style)
88
import List.Nonempty as NEL
99
import String.Extra exposing (pluralize)
10+
import UI.Click as Click
11+
import UI.Icon as Icon
1012
import UI.Tooltip as Tooltip
1113
import UnisonShare.DefinitionDiff as DefinitionDiff exposing (DefinitionDiff(..), DiffDetails, DiffLine(..), DiffSegment(..))
1214

@@ -113,21 +115,25 @@ viewDiffLine viewSeg changeIndicator gutterWidth line =
113115
[]
114116

115117

116-
viewCollapsed : (DiffLine -> Html msg) -> DefinitionDiff.Collapsed -> Html msg
117-
viewCollapsed viewLine collapsed =
118+
viewCollapsed : ViewConfig msg -> (DiffLine -> Html msg) -> Int -> DefinitionDiff.Collapsed -> Html msg
119+
viewCollapsed cfg viewLine index collapsed =
118120
case collapsed of
119121
DefinitionDiff.Collapsed lines ->
120122
let
121123
numCollapsedLines =
122124
List.length lines
123125
in
124-
div [ class "collapsed-section" ]
125-
[ text
126-
(String.fromInt numCollapsedLines
127-
++ pluralize " line " " lines " numCollapsedLines
128-
++ "hidden..."
129-
)
130-
]
126+
Click.onClick (cfg.toExpandCollapsedDiffSectionMsg { index = index })
127+
|> Click.view
128+
[ class "collapsed-section" ]
129+
[ Icon.view Icon.dots
130+
, text
131+
(String.fromInt numCollapsedLines
132+
++ pluralize "line " "lines " numCollapsedLines
133+
++ "hidden"
134+
)
135+
, Icon.view Icon.dots
136+
]
131137

132138
DefinitionDiff.NotCollapsed lines ->
133139
div [] (List.map viewLine lines)
@@ -149,14 +155,14 @@ diffLength collapsedLines =
149155
|> List.sum
150156

151157

152-
viewDiff : (Bool -> SyntaxConfig msg) -> DiffDetails -> Html msg
153-
viewDiff toSyntaxConfig { left, right } =
158+
viewDiff : ViewConfig msg -> DiffDetails -> Html msg
159+
viewDiff cfg { left, right } =
154160
let
155161
toGutterWidth len =
156162
String.length (String.fromInt len)
157163

158164
toViewDiffSegment isNew =
159-
viewDiffSegment (toSyntaxConfig isNew)
165+
viewDiffSegment (cfg.toSyntaxConfig isNew)
160166

161167
viewLeftDiffLine =
162168
viewDiffLine (toViewDiffSegment False)
@@ -169,10 +175,10 @@ viewDiff toSyntaxConfig { left, right } =
169175
(toGutterWidth (diffLength right))
170176

171177
before =
172-
List.map (viewCollapsed viewLeftDiffLine) left
178+
List.indexedMap (viewCollapsed cfg viewLeftDiffLine) left
173179

174180
after =
175-
List.map (viewCollapsed viewRightDiffLine) right
181+
List.indexedMap (viewCollapsed cfg viewRightDiffLine) right
176182
in
177183
div [ class "diff-side-by-side" ]
178184
[ pre [ class "monochrome diff-side left" ]
@@ -186,14 +192,20 @@ viewDiff toSyntaxConfig { left, right } =
186192
]
187193

188194

189-
view : (Bool -> SyntaxConfig msg) -> DefinitionDiff -> Html msg
190-
view toSyntaxConfig defDiff =
195+
type alias ViewConfig msg =
196+
{ toSyntaxConfig : Bool -> SyntaxConfig msg
197+
, toExpandCollapsedDiffSectionMsg : { index : Int } -> msg
198+
}
199+
200+
201+
view : ViewConfig msg -> DefinitionDiff -> Html msg
202+
view cfg defDiff =
191203
case defDiff of
192204
Diff details ->
193-
div [] [ viewDiff toSyntaxConfig details ]
205+
div [] [ viewDiff cfg details ]
194206

195207
Mismatched { left, right } ->
196208
div [ class "diff-side-by-side" ]
197-
[ pre [ class "monochrome diff-side" ] [ code [] (viewSegments (toSyntaxConfig False) "mismatched old" left) ]
198-
, pre [ class "monochrome diff-side" ] [ code [] (viewSegments (toSyntaxConfig True) "mismatched new" right) ]
209+
[ pre [ class "monochrome diff-side" ] [ code [] (viewSegments (cfg.toSyntaxConfig False) "mismatched old" left) ]
210+
, pre [ class "monochrome diff-side" ] [ code [] (viewSegments (cfg.toSyntaxConfig True) "mismatched new" right) ]
199211
]

src/UnisonShare/Page/ProjectContributionChangesPage.elm

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import UnisonShare.BranchDiff.ToggledChangeLines as ToggledChangeLines exposing
3838
import UnisonShare.BranchDiffState as BranchDiffState exposing (BranchDiffState)
3939
import UnisonShare.Contribution exposing (ContributionDetails)
4040
import UnisonShare.Contribution.ContributionRef exposing (ContributionRef)
41+
import UnisonShare.DefinitionDiff as DefinitionDiff
4142
import UnisonShare.DefinitionDiffCard as DefinitionDiffCard
4243
import UnisonShare.Link as Link
4344
import UnisonShare.Project.ProjectRef exposing (ProjectRef)
@@ -84,6 +85,7 @@ type Msg
8485
| CopyChangeLinePermalink ChangeLineId
8586
| SetChangeLinePermalink ChangeLineId
8687
| ScrollTo ChangeLineId
88+
| ExpandCollapsedDiffSection ChangeLineId { index : Int }
8789
| NoOp
8890

8991

@@ -163,8 +165,10 @@ update appContext projectRef contribRef msg model =
163165
SetChangeLinePermalink changeLineId ->
164166
let
165167
route =
166-
changeLineId
167-
|> Route.projectContributionChange projectRef contribRef
168+
Route.projectContributionChange
169+
projectRef
170+
contribRef
171+
changeLineId
168172
in
169173
( model
170174
, Cmd.batch
@@ -176,8 +180,10 @@ update appContext projectRef contribRef msg model =
176180
CopyChangeLinePermalink changeLineId ->
177181
let
178182
route =
179-
changeLineId
180-
|> Route.projectContributionChange projectRef contribRef
183+
Route.projectContributionChange
184+
projectRef
185+
contribRef
186+
changeLineId
181187

182188
url =
183189
route
@@ -192,6 +198,26 @@ update appContext projectRef contribRef msg model =
192198
]
193199
)
194200

201+
ExpandCollapsedDiffSection changeLineId { index } ->
202+
let
203+
expandCollapsedDiffSection changeLine =
204+
case changeLine of
205+
ChangeLine.Updated type_ details ->
206+
ChangeLine.Updated
207+
type_
208+
{ details | diff = DefinitionDiff.expandCollapsedDiffSection index details.diff }
209+
210+
_ ->
211+
changeLine
212+
213+
branchDiff =
214+
BranchDiffState.updateChangeLineById
215+
expandCollapsedDiffSection
216+
changeLineId
217+
model.branchDiff
218+
in
219+
( { model | branchDiff = branchDiff }, Cmd.none )
220+
195221
NoOp ->
196222
( model, Cmd.none )
197223

@@ -421,12 +447,23 @@ viewChangedDefinitionCard projectRef toggledChangeLines branchDiff maxBadgeLengt
421447

422448
( expanded, toggleIcon ) =
423449
if ToggledChangeLines.isCollapsed toggledChangeLines changeLine then
424-
( Nothing, Icon.arrowsFromLine )
450+
( Nothing, Icon.expandDown )
425451

426452
else
427453
case changeLine of
428454
ChangeLine.Updated _ { diff } ->
429-
( Just (DefinitionDiffCard.view toSyntaxConfig diff), Icon.arrowsToLine )
455+
case ChangeLine.toChangeLineId changeLine of
456+
Just id ->
457+
let
458+
viewConfig =
459+
{ toSyntaxConfig = toSyntaxConfig
460+
, toExpandCollapsedDiffSectionMsg = ExpandCollapsedDiffSection id
461+
}
462+
in
463+
( Just (DefinitionDiffCard.view viewConfig diff), Icon.collapseUp )
464+
465+
Nothing ->
466+
( Nothing, Icon.dash )
430467

431468
_ ->
432469
case ChangeLine.source changeLine of
@@ -451,11 +488,11 @@ viewChangedDefinitionCard projectRef toggledChangeLines branchDiff maxBadgeLengt
451488
[ code [] [ Syntax.view linked source ] ]
452489
in
453490
( Just expandedContent
454-
, Icon.arrowsToLine
491+
, Icon.collapseUp
455492
)
456493

457494
Nothing ->
458-
( Nothing, Icon.arrowsFromLine )
495+
( Nothing, Icon.expandDown )
459496

460497
domId =
461498
changeLine

src/css/unison-share/page/project-contribution-changes-page.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@
230230
}
231231

232232
& .definition-change.card .collapsed-section {
233+
border-radius: 0;
233234
color: var(--u-color_text_subdued);
234235
font-family: var(--font-monospace);
235236
background: var(--u-color_element_subdued);
@@ -244,6 +245,15 @@
244245
border-top: 1px solid var(--u-color_border_subdued);
245246
border-bottom: 1px solid var(--u-color_border_subdued);
246247
font-size: var(--font-size-extra-small);
248+
overflow: hidden;
249+
}
250+
251+
& .definition-change.card .collapsed-section .icon {
252+
color: var(--u-color_icon_subdued);
253+
}
254+
255+
& .definition-change.card .collapsed-section:hover {
256+
color: var(--u-color_interactive);
247257
}
248258

249259
& .definition-change.card .collapsed-section:first-child {

0 commit comments

Comments
 (0)