Skip to content

Commit 1b081cb

Browse files
authored
Refactor diff component and improve LiveView integration (#111)
* Pass diff_id to DiffComponent render * Update diff container ID format * Use phx-click with class toggle for dynamic LiveView content integration * Remove broken click event listeners for file headers * Refactor ghd-file-header styles for improved diff visibility and layout * Refactor diff component header for improved structure and clarity * Remove useless nil check
1 parent 13cc4ef commit 1b081cb

5 files changed

Lines changed: 29 additions & 69 deletions

File tree

assets/css/app.css

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -193,35 +193,22 @@ input.search-input:focus {
193193
position: -webkit-sticky;
194194
position: sticky;
195195
top: 0px;
196+
display: flex;
197+
align-items: center;
198+
justify-content: space-between;
196199
z-index: 1;
197200
}
198201

199-
.ghd-file-header .reveal-diff {
200-
display: none;
201-
float: right;
202+
.ghd-file-header .show-hide-diff {
202203
color: #999;
203204
}
204205

205-
.ghd-file-header .reveal-diff path {
206+
.ghd-file-header .show-hide-diff path {
206207
fill: #999;
207208
}
208209

209-
.ghd-file-header.collapsed .reveal-diff {
210-
display: block;
211-
}
212-
213-
.ghd-file-header .collapse-diff {
214-
display: block;
215-
float: right;
216-
color: #999;
217-
}
218-
219-
.ghd-file-header .collapse-diff path {
220-
fill: #999;
221-
}
222-
223-
.ghd-file-header.collapsed .collapse-diff {
224-
display: none;
210+
.ghd-file:has(.hidden) .show-hide-diff {
211+
rotate: 180deg;
225212
}
226213

227214
.ghd-chunk-header .ghd-line-number {

assets/js/app.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,3 @@ lines.forEach(line => {
8787
}
8888
})
8989
})
90-
91-
const fileHeaders = document.querySelectorAll('.ghd-file-header')
92-
fileHeaders.forEach(header => {
93-
header.addEventListener('click', e => {
94-
const parent = header.parentNode
95-
96-
parent.querySelectorAll('.ghd-diff').forEach(diff => {
97-
diff.classList.toggle('hidden')
98-
})
99-
header.classList.toggle('collapsed') && scrollIfNeeded(header)
100-
})
101-
})
102-
103-
const scrollIfNeeded = elem => {
104-
elem.getBoundingClientRect().top < 0 && elem.scrollIntoView(true)
105-
}

lib/diff_web/live/components/diff_component.ex

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
defmodule DiffWeb.DiffComponent do
22
use DiffWeb, :live_component
3+
alias Phoenix.LiveView.JS
34

45
def render(assigns) do
56
~H"""
67
<div class="ghd-file">
7-
<div class="ghd-file-header">
8-
<span class={"ghd-file-status ghd-file-status-#{diff_status(@diff)}"}>
9-
<%= diff_status(@diff) %>
10-
</span>
11-
<%= file_header(@diff, diff_status(@diff)) %>
12-
<span class="collapse-diff"><svg xmlns="http://www.w3.org/2000/svg" width="10" height="16" viewBox="0 0 10 16"><path fill-rule="evenodd" d="M10 10l-1.5 1.5L5 7.75 1.5 11.5 0 10l5-5 5 5z"/></svg></span>
13-
<span class="reveal-diff"><svg xmlns="http://www.w3.org/2000/svg" width="10" height="16" viewBox="0 0 10 16"><path fill-rule="evenodd" d="M5 11L0 6l1.5-1.5L5 8.25 8.5 4.5 10 6l-5 5z"/></svg></span>
8+
<div class="ghd-file-header" phx-click={JS.toggle_class("hidden", to: "##{@diff_id}-body")}>
9+
<div>
10+
<span class={"ghd-file-status ghd-file-status-#{diff_status(@diff)}"}>
11+
<%= diff_status(@diff) %>
12+
</span>
13+
<%= file_header(@diff, diff_status(@diff)) %>
14+
</div>
15+
<svg class="show-hide-diff" xmlns="http://www.w3.org/2000/svg" width="10" height="16" viewBox="0 0 10 16">
16+
<path fill-rule="evenodd" d="M10 10l-1.5 1.5L5 7.75 1.5 11.5 0 10l5-5 5 5z"/>
17+
</svg>
1418
</div>
15-
<div class="ghd-diff">
19+
<div class="ghd-diff" id={"#{@diff_id}-body"}>
1620
<table class="ghd-diff">
1721
<%= for chunk <- @diff.chunks do %>
1822
<tr class="ghd-chunk-header">
@@ -46,31 +50,16 @@ defmodule DiffWeb.DiffComponent do
4650
"""
4751
end
4852

49-
defp file_header(diff, status) do
50-
from = diff.from
51-
to = diff.to
53+
defp file_header(%{from: from}, "changed"), do: from
54+
defp file_header(%{from: from, to: to}, "renamed"), do: "#{from} -> #{to}"
55+
defp file_header(%{from: from}, "removed"), do: from
56+
defp file_header(%{to: to}, "added"), do: to
5257

53-
case status do
54-
"changed" -> from
55-
"renamed" -> "#{from} -> #{to}"
56-
"removed" -> from
57-
"added" -> to
58-
end
59-
end
60-
61-
defp diff_status(diff) do
62-
from = diff.from
63-
to = diff.to
64-
65-
cond do
66-
!from -> "added"
67-
!to -> "removed"
68-
from == to -> "changed"
69-
true -> "renamed"
70-
end
71-
end
58+
defp diff_status(%{from: nil, to: _to}), do: "added"
59+
defp diff_status(%{from: _from, to: nil}), do: "removed"
60+
defp diff_status(%{from: from, to: to}) when from == to, do: "changed"
61+
defp diff_status(_), do: "renamed"
7262

73-
defp line_number(ln) when is_nil(ln), do: ""
7463
defp line_number(ln), do: to_string(ln)
7564

7665
defp line_id(diff, line) do

lib/diff_web/templates/live/diff.html.leex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<% end %>
3737

3838
<%= for diff_id <- @loaded_diffs do %>
39-
<div id="diff-<%= diff_id %>">
39+
<div id="<%= diff_id %>-container">
4040
<%= raw(Map.fetch!(@loaded_diff_content, diff_id)) %>
4141
</div>
4242
<% end %>

lib/diff_web/views/live_view.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule DiffWeb.LiveView do
2121
# Take the first diff (should only be one per file)
2222
diff = List.first(diffs)
2323

24-
DiffWeb.DiffComponent.render(%{diff: diff})
24+
DiffWeb.DiffComponent.render(%{diff: diff, diff_id: diff_id})
2525
|> Phoenix.HTML.Safe.to_iodata()
2626
|> IO.iodata_to_binary()
2727
|> sanitize_utf8()

0 commit comments

Comments
 (0)