{{ define "title" }}{{ .Path }} at {{ .Ref }} &middot; {{ .RepoInfo.FullName }} &middot; Tangled{{ end }}

{{ define "extrameta" }}
    {{ template "repo/fragments/meta" . }}

    {{ $title := printf "%s at %s &middot; %s" .Path .Ref .RepoInfo.FullName }}
    {{ $url := printf "https://tangled.org/%s/blob/%s/%s" .RepoInfo.FullName .Ref .Path }}

    {{ template "repo/fragments/og" (dict "RepoInfo" .RepoInfo "Title" $title "Url" $url) }}

{{ end }}

{{ define "repoContent" }}
    {{ $linkstyle := "no-underline hover:underline" }}
    <div class="peer pb-2 mb-3 text-base border-b border-gray-200 dark:border-gray-700">
        <div class="flex flex-col md:flex-row md:justify-between gap-2">
            <div id="breadcrumbs" class="overflow-x-auto whitespace-nowrap text-gray-400 dark:text-gray-500">
                {{ range $idx, $value := .BreadCrumbs }}
                    {{ if ne $idx (sub (len $.BreadCrumbs) 1) }}
                        <a
                            href="{{ index . 1 }}"
                            class="text-bold text-gray-500 dark:text-gray-400 {{ $linkstyle }}"
                            >{{ pathUnescape (index . 0) }}</a
                        >
                        /
                    {{ else }}
                        <span class="text-bold text-black dark:text-white"
                            >{{ pathUnescape (index . 0) }}</span
                        >
                    {{ end }}
                {{ end }}
            </div>
            <div id="file-info" class="text-gray-500 dark:text-gray-400 text-xs md:text-sm flex flex-wrap items-center gap-1 md:gap-0">
                <span>at <a href="/{{ .RepoInfo.FullName }}/tree/{{ pathEscape .Ref }}">{{ .Ref }}</a></span>

                {{ if .BlobView.ShowingText }}
                  <span class="select-none px-1 md:px-2 [&:before]:content-['·']"></span>
                  <span>{{ .BlobView.Lines }} lines</span>
                {{ end }}

                {{ if .BlobView.SizeHint }}
                  <span class="select-none px-1 md:px-2 [&:before]:content-['·']"></span>
                  <span>{{ byteFmt .BlobView.SizeHint }}</span>
                {{ end }}

                {{ if .BlobView.HasRawView }}
                  <span class="select-none px-1 md:px-2 [&:before]:content-['·']"></span>
                  <a href="/{{ .RepoInfo.FullName }}/raw/{{ pathEscape .Ref }}/{{ .Path }}">View raw</a>
                {{ end }}

                {{ if .BlobView.ShowCopy }}
                  <span class="select-none px-1 md:px-2 [&:before]:content-['·']"></span>
                  {{ template "repo/fragments/copyFileButton" (dict "Url" (printf "/%s/raw/%s/%s" .RepoInfo.FullName (pathEscape .Ref) .Path)) }}
                {{ end }}

                {{ if .BlobView.ShowToggle }}
                  <span class="select-none px-1 md:px-2 [&:before]:content-['·']"></span>
                  <a href="/{{ .RepoInfo.FullName }}/blob/{{ pathEscape .Ref }}/{{ .Path }}?code={{ .BlobView.ShowingRendered }}" hx-boost="true">
                    View {{ if .BlobView.ShowingRendered }}code{{ else }}rendered{{ end }}
                  </a>
                {{ end }}

                {{ if .BlobView.ShowingText }}
                  <div id="toggle-wrap-content" class="flex items-center">
                    <span class="select-none px-1 md:px-2 [&:before]:content-['·']"></span>
                    <label class="flex items-center lowercase font-normal px-1 py-0 gap-1 text-xs md:text-sm">
                      <input id="toggle-wrap-content-checkbox" type="checkbox" name="wrap"/>
                      wrap content
                    </label>
                  </div>
                {{ end }}
            </div>
        </div>
    </div>

    {{ if .LastCommitInfo }}
      {{ template "repo/fragments/lastCommitPanel" $ }}
    {{ end }}

    {{ $wrapContentClasses := "peer-has-[:checked]:*:whitespace-pre-wrap peer-has-[:checked]:*:[overflow-wrap:anywhere]" }}

    {{ if .BlobView.IsUnsupported }}
      <p class="text-center text-gray-400 dark:text-gray-500">
          Previews are not supported for this file type.
      </p>
    {{ else if .BlobView.ContentType.IsSubmodule }}
      <p class="text-center text-gray-400 dark:text-gray-500">
        This directory is a git submodule of <a href="{{ .BlobView.ContentSrc }}">{{ .BlobView.ContentSrc }}</a>.
      </p>
    {{ else if .BlobView.ContentType.IsImage }}
      <div class="text-center">
        <img src="{{ .BlobView.ContentSrc }}"
             alt="{{ .Path }}"
             class="max-w-full h-auto mx-auto border border-gray-200 dark:border-gray-700 rounded" />
      </div>
    {{ else if .BlobView.ContentType.IsVideo }}
      <div class="text-center">
        <video controls class="max-w-full h-auto mx-auto border border-gray-200 dark:border-gray-700 rounded">
            <source src="{{ .BlobView.ContentSrc }}">
            Your browser does not support the video tag.
        </video>
      </div>
    {{ else if .BlobView.ContentType.IsSvg }}
      <div class="overflow-auto relative {{ $wrapContentClasses }}">
        {{ if .BlobView.ShowingRendered }}
          <div class="text-center">
            <img src="{{ .BlobView.ContentSrc }}"
                 alt="{{ .Path }}"
                 class="max-w-full h-auto mx-auto border border-gray-200 dark:border-gray-700 rounded" />
          </div>
        {{ else }}
          <div id="blob-contents" class="whitespace-pre peer-target:bg-yellow-200 dark:peer-target:bg-yellow-900">{{ code .BlobView.Contents .Path | escapeHtml }}</div>
        {{ end }}
      </div>
    {{ else if .BlobView.FileTooLarge }}
      <p class="text-center text-gray-400 dark:text-gray-500">
        This file is too large to render. <a href="/{{ .RepoInfo.FullName }}/raw/{{ pathEscape .Ref }}/{{ .Path }}">View raw.</a>.
      </p>
    {{ else if .BlobView.ContentType.IsMarkup }}
      <div class="overflow-auto relative {{ $wrapContentClasses }}">
        {{ if .BlobView.ShowingRendered }}
          <div id="blob-contents" class="prose dark:prose-invert">{{ .BlobView.Contents | readme }}</div>
        {{ else }}
          <div id="blob-contents" class="whitespace-pre peer-target:bg-yellow-200 dark:peer-target:bg-yellow-900">{{ code .BlobView.Contents .Path | escapeHtml }}</div>
        {{ end }}
      </div>
    {{ else if .BlobView.ContentType.IsCode }}
      <div class="overflow-auto relative {{ $wrapContentClasses }}">
        <div id="blob-contents" class="whitespace-pre peer-target:bg-yellow-200 dark:peer-target:bg-yellow-900">{{ code .BlobView.Contents .Path | escapeHtml }}</div>
      </div>
    {{ end }}
    {{ template "fragments/multiline-select" }}
    {{ template "repo/fragments/copyFileScript" }}
    <script>
      (() => {
        const abortController = new AbortController();
        const toggle = document.querySelector('#toggle-wrap-content');
        const toggleCheckbox = document.querySelector('#toggle-wrap-content-checkbox');
        const contents = document.querySelector('#blob-contents');

        function showWrapContentToggleOnOverflow() {
          if(!toggle || !toggleCheckbox || !contents) return;

          const isScrollable = contents.scrollWidth > contents.clientWidth;
          const showToggle = isScrollable || toggleCheckbox.checked;

          if(showToggle) {
            toggle.classList.remove('hidden');
          } else {
            toggle.classList.add('hidden');
          }
        }

        window.addEventListener('resize', () => showWrapContentToggleOnOverflow(), {signal: abortController.signal});
        document.body.addEventListener('htmx:afterSettle', () => showWrapContentToggleOnOverflow(), {signal: abortController.signal});
        document.body.addEventListener('htmx:beforeCleanupElement', (e) => {
          if(e.target === toggle) {
            abortController.abort();
          }
        }, {signal: abortController.signal});

        showWrapContentToggleOnOverflow();
      })();
    </script>
    {{ template "repo/fragments/permalinkShortcut" . }}
{{ end }}
