diff --git a/assets/css/app.css b/assets/css/app.css index ebecd5f..c322644 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -193,35 +193,22 @@ input.search-input:focus { position: -webkit-sticky; position: sticky; top: 0px; + display: flex; + align-items: center; + justify-content: space-between; z-index: 1; } -.ghd-file-header .reveal-diff { - display: none; - float: right; +.ghd-file-header .show-hide-diff { color: #999; } -.ghd-file-header .reveal-diff path { +.ghd-file-header .show-hide-diff path { fill: #999; } -.ghd-file-header.collapsed .reveal-diff { - display: block; -} - -.ghd-file-header .collapse-diff { - display: block; - float: right; - color: #999; -} - -.ghd-file-header .collapse-diff path { - fill: #999; -} - -.ghd-file-header.collapsed .collapse-diff { - display: none; +.ghd-file:has(.hidden) .show-hide-diff { + rotate: 180deg; } .ghd-chunk-header .ghd-line-number { diff --git a/assets/js/app.js b/assets/js/app.js index ff4282e..901c14b 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -87,19 +87,3 @@ lines.forEach(line => { } }) }) - -const fileHeaders = document.querySelectorAll('.ghd-file-header') -fileHeaders.forEach(header => { - header.addEventListener('click', e => { - const parent = header.parentNode - - parent.querySelectorAll('.ghd-diff').forEach(diff => { - diff.classList.toggle('hidden') - }) - header.classList.toggle('collapsed') && scrollIfNeeded(header) - }) -}) - -const scrollIfNeeded = elem => { - elem.getBoundingClientRect().top < 0 && elem.scrollIntoView(true) -} diff --git a/lib/diff_web/live/components/diff_component.ex b/lib/diff_web/live/components/diff_component.ex index 78757eb..d7f23f4 100644 --- a/lib/diff_web/live/components/diff_component.ex +++ b/lib/diff_web/live/components/diff_component.ex @@ -1,18 +1,22 @@ defmodule DiffWeb.DiffComponent do use DiffWeb, :live_component + alias Phoenix.LiveView.JS def render(assigns) do ~H"""
-
- - <%= diff_status(@diff) %> - - <%= file_header(@diff, diff_status(@diff)) %> - - +
+
+ + <%= diff_status(@diff) %> + + <%= file_header(@diff, diff_status(@diff)) %> +
+ + +
-
+
<%= for chunk <- @diff.chunks do %> @@ -46,31 +50,16 @@ defmodule DiffWeb.DiffComponent do """ end - defp file_header(diff, status) do - from = diff.from - to = diff.to + defp file_header(%{from: from}, "changed"), do: from + defp file_header(%{from: from, to: to}, "renamed"), do: "#{from} -> #{to}" + defp file_header(%{from: from}, "removed"), do: from + defp file_header(%{to: to}, "added"), do: to - case status do - "changed" -> from - "renamed" -> "#{from} -> #{to}" - "removed" -> from - "added" -> to - end - end - - defp diff_status(diff) do - from = diff.from - to = diff.to - - cond do - !from -> "added" - !to -> "removed" - from == to -> "changed" - true -> "renamed" - end - end + defp diff_status(%{from: nil, to: _to}), do: "added" + defp diff_status(%{from: _from, to: nil}), do: "removed" + defp diff_status(%{from: from, to: to}) when from == to, do: "changed" + defp diff_status(_), do: "renamed" - defp line_number(ln) when is_nil(ln), do: "" defp line_number(ln), do: to_string(ln) defp line_id(diff, line) do diff --git a/lib/diff_web/templates/live/diff.html.leex b/lib/diff_web/templates/live/diff.html.leex index 1a35425..d738b32 100644 --- a/lib/diff_web/templates/live/diff.html.leex +++ b/lib/diff_web/templates/live/diff.html.leex @@ -36,7 +36,7 @@ <% end %> <%= for diff_id <- @loaded_diffs do %> -
+
<%= raw(Map.fetch!(@loaded_diff_content, diff_id)) %>
<% end %> diff --git a/lib/diff_web/views/live_view.ex b/lib/diff_web/views/live_view.ex index bccae08..df86d8e 100644 --- a/lib/diff_web/views/live_view.ex +++ b/lib/diff_web/views/live_view.ex @@ -21,7 +21,7 @@ defmodule DiffWeb.LiveView do # Take the first diff (should only be one per file) diff = List.first(diffs) - DiffWeb.DiffComponent.render(%{diff: diff}) + DiffWeb.DiffComponent.render(%{diff: diff, diff_id: diff_id}) |> Phoenix.HTML.Safe.to_iodata() |> IO.iodata_to_binary() |> sanitize_utf8()