Prechádzať zdrojové kódy

Highlight sidebar item on navigation

shalvah 2 rokov pred
rodič
commit
ea8badc788

+ 4 - 3
resources/views/themes/elements/components/field-details.blade.php

@@ -1,13 +1,14 @@
 @php
     $hasChildren ??= false;
     $isArrayBody = $name == "[]";
+    $expandable = $hasChildren && !$isArrayBody;
 @endphp
 
 <div class="sl-flex sl-relative sl-max-w-full sl-py-2 sl-pl-3">
     <div class="sl-w-1 sl-mt-2 sl-mr-3 sl--ml-3 sl-border-t"></div>
-    <div class="sl-stack sl-stack--vertical sl-stack--1 sl-flex sl-flex-1 sl-flex-col sl-items-stretch sl-max-w-full sl-ml-2">
-        <div class="sl-flex sl-items-center sl-max-w-full @if($hasChildren) sl-cursor-pointer @endif">
-            @if($hasChildren && !$isArrayBody)
+    <div class="sl-stack sl-stack--vertical sl-stack--1 sl-flex sl-flex-1 sl-flex-col sl-items-stretch sl-max-w-full sl-ml-2 @if($expandable) sl-cursor-pointer @endif">
+        <div class="sl-flex sl-items-center sl-max-w-full">
+            @if($expandable)
                 <div class="sl-flex sl-justify-center sl-w-8 sl--ml-8 sl-pl-3 sl-text-muted" role="button">
                     <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="chevron-right"
                          class="expand-chevron svg-inline--fa fa-chevron-right fa-fw fa-sm sl-icon" role="img"

+ 1 - 1
resources/views/themes/elements/endpoint.blade.php

@@ -317,7 +317,7 @@
                                     <div class="sl-panel__content sl-p-0">@if(count($response->headers))
                                                 <details class="sl-pl-2">
                                                     <summary style="cursor: pointer; list-style: none;">
-                                                        <small onclick="wasOpen = parentElement.parentElement.open; event.target.querySelector('.expand-chevron').style.display = !wasOpen ? 'none' : 'initial';  event.target.querySelector('.expanded-chevron').style.display = wasOpen ? 'none' : 'initial'; ">
+                                                        <small>
                                                             <svg focusable="false"style="display: none;" class="expanded-chevron svg-inline--fa fa-chevron-down fa-fw fa-sm sl-icon" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path fill="currentColor" d="M224 416c-8.188 0-16.38-3.125-22.62-9.375l-192-192c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0L224 338.8l169.4-169.4c12.5-12.5 32.75-12.5 45.25 0s12.5 32.75 0 45.25l-192 192C240.4 412.9 232.2 416 224 416z"></path></svg>
                                                             <svg focusable="false" class="expand-chevron svg-inline--fa fa-chevron-right fa-fw fa-sm sl-icon" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><path fill="currentColor" d="M96 480c-8.188 0-16.38-3.125-22.62-9.375c-12.5-12.5-12.5-32.75 0-45.25L242.8 256L73.38 86.63c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0l192 192c12.5 12.5 12.5 32.75 0 45.25l-192 192C112.4 476.9 104.2 480 96 480z"></path></svg>
                                                             Headers

+ 54 - 20
resources/views/themes/elements/index.blade.php

@@ -2,7 +2,7 @@
     use Knuckles\Scribe\Tools\WritingUtils as u;
 @endphp
 
-<!doctype html>
+        <!doctype html>
 <html lang="en">
 <head>
     <meta charset="utf-8">
@@ -23,7 +23,8 @@
     <style>
         .hljs {
             /* Remove highlightjs background color */
-            background-color: transparent; !important;
+            background-color: transparent;
+        !important;
         }
     </style>
 
@@ -41,7 +42,7 @@
 
 </head>
 
-<body data-languages="{{ json_encode($metadata['example_languages'] ?? []) }}">
+<body>
 
 @if($metadata['example_languages'])
     <script>
@@ -60,6 +61,19 @@
         document.querySelectorAll(`.example-response-${endpointId}-toggle`).forEach(el => el.value = index);
     }
 
+    function toggleExpansionChevrons(evt) {
+        let elem = evt.currentTarget;
+
+        const newState = elem.querySelector('.expand-chevron').style.display === 'none' ? 'expand' : 'expanded';
+        if (newState === 'expanded') {
+            elem.querySelector('.expand-chevron').style.display = 'none';
+            elem.querySelector('.expanded-chevron').style.removeProperty('display');
+        } else {
+            elem.querySelector('.expand-chevron').style.removeProperty('display')
+            elem.querySelector('.expanded-chevron').style.display = 'none'
+        }
+    }
+
     function toggleElementChildren(evt) {
         let elem = evt.currentTarget;
         let children = elem.querySelector(`.children`);
@@ -70,22 +84,46 @@
         let oldState = children.style.display
         if (oldState === 'none') {
             children.style.removeProperty('display');
-            elem.querySelector('.expand-chevron').style.display = 'none'
-            elem.querySelector('.expanded-chevron').style.removeProperty('display')
+            toggleExpansionChevrons(evt);
         } else {
             children.style.display = 'none';
-            elem.querySelector('.expand-chevron').style.removeProperty('display')
-            elem.querySelector('.expanded-chevron').style.display = 'none'
+            toggleExpansionChevrons(evt);
         }
 
         evt.stopPropagation();
     }
 
+    function highlightSidebarItem(evt = null) {
+        if (evt && evt.oldURL) {
+            let oldHash = new URL(evt.oldURL).hash.slice(1);
+            if (oldHash) {
+                let previousItem = window['sidebar'].querySelector(`#toc-item-${oldHash}`);
+                previousItem.classList.remove('sl-bg-primary-tint');
+                previousItem.classList.add('sl-bg-canvas-100');
+            }
+        }
+
+        let newHash = location.hash.slice(1);
+        if (newHash) {
+            let item = window['sidebar'].querySelector(`#toc-item-${newHash}`);
+            item.classList.remove('sl-bg-canvas-100');
+            item.classList.add('sl-bg-primary-tint');
+        }
+    }
+
     window.addEventListener('DOMContentLoaded', () => {
+        highlightSidebarItem();
+
         document.querySelectorAll('.expandable').forEach(el => {
             el.addEventListener('click', toggleElementChildren);
         });
+
+        document.querySelectorAll('details').forEach(el => {
+            el.addEventListener('toggle', toggleExpansionChevrons);
+        });
     });
+
+    addEventListener('hashchange', highlightSidebarItem);
 </script>
 
 <div style="height: 100%;">
@@ -133,23 +171,19 @@
                                                     @if($metadata['postman_collection_url'])
                                                         <div class="sl-menu-item sl-flex sl-items-center sl-text-base sl-whitespace-nowrap sl-pt-1 sl-pr-3 sl-pb-1 sl-pl-3"
                                                              role="menuitem">
-                                                            <div class="sl-menu-item__title-wrapper sl-flex-1 sl-w-full sl-pr-0">
-                                                                <div class="sl-truncate">
-                                                                    <a href="{!! $metadata['postman_collection_url'] !!}" target="_blank">Postman
-                                                                        collection</a>
-                                                                </div>
-                                                            </div>
+                                                            <a href="{!! $metadata['postman_collection_url'] !!}"
+                                                               target="_blank"
+                                                               class="sl-menu-item__title-wrapper sl-flex-1 sl-w-full sl-pr-0">Postman
+                                                                collection</a>
                                                         </div>
                                                     @endif
                                                     @if($metadata['openapi_spec_url'])
                                                         <div class="sl-menu-item sl-flex sl-items-center sl-text-base sl-whitespace-nowrap sl-pt-1 sl-pr-3 sl-pb-1 sl-pl-3"
                                                              role="menuitem">
-                                                            <div class="sl-menu-item__title-wrapper sl-flex-1 sl-w-full sl-pr-0">
-                                                                <div class="sl-truncate">
-                                                                    <a href="{!! $metadata['openapi_spec_url'] !!}" target="_blank">OpenAPI
-                                                                        spec</a>
-                                                                </div>
-                                                            </div>
+                                                            <a href="{!! $metadata['openapi_spec_url'] !!}"
+                                                               target="_blank"
+                                                               class="sl-menu-item__title-wrapper sl-flex-1 sl-w-full sl-pr-0">OpenAPI
+                                                                spec</a>
                                                         </div>
                                                     @endif
                                                 </div>
@@ -173,7 +207,7 @@
                         <div class="sl-mb-10">
                             <div class="sl-relative">
                                 <div class="sl-prose sl-markdown-viewer sl-my-5">
-                        {!! $append !!}
+                                    {!! $append !!}
                                 </div>
                             </div>
                         </div>

+ 6 - 6
resources/views/themes/elements/sidebar.blade.php

@@ -1,4 +1,4 @@
-<div class="sl-flex sl-overflow-y-auto sl-flex-col sl-sticky sl-inset-y-0 sl-pt-8 sl-bg-canvas-100 sl-border-r"
+<div id="sidebar" class="sl-flex sl-overflow-y-auto sl-flex-col sl-sticky sl-inset-y-0 sl-pt-8 sl-bg-canvas-100 sl-border-r"
      style="width: calc((100% - 1800px) / 2 + 300px); padding-left: calc((100% - 1800px) / 2); min-width: 300px; max-height: 100vh">
     <div class="sl-flex sl-items-center sl-mb-5 sl-ml-4">
         @if($metadata['logo'] != false)
@@ -17,7 +17,7 @@
             <div class="sl-my-3">
                 @foreach($headings as $h1)
                     <div class="expandable">
-                        <div title="{!! $h1['name'] !!}"
+                        <div title="{!! $h1['name'] !!}" id="toc-item-{!! $h1['slug'] !!}"
                              class="sl-flex sl-items-center sl-h-md sl-pr-4 sl-pl-4 sl-bg-canvas-100 hover:sl-bg-canvas-200 sl-cursor-pointer sl-select-none">
                             <a href="#{!! $h1['slug'] !!}"
                                class="sl-flex-1 sl-items-center sl-truncate sl-mr-1.5 sl-p-0">{!! $h1['name'] !!}</a>
@@ -50,7 +50,7 @@
                                     <div class="expandable">
                                         <div class="sl-flex sl-items-center sl-h-md sl-pr-4 sl-pl-8 sl-bg-canvas-100 hover:sl-bg-canvas-200 sl-cursor-pointer sl-select-none">
                                             <div class="sl-flex-1 sl-items-center sl-truncate sl-mr-1.5 sl-p-0"
-                                                 title="{!! $h2['slug'] !!}">
+                                                 title="{!! $h2['name'] !!}" id="toc-item-{!! $h2['slug'] !!}">
                                                 <a class="ElementsTableOfContentsItem sl-block sl-no-underline"
                                                    href="#{!! $h2['slug'] !!}">
                                                     {!! $h2['name'] !!}
@@ -82,11 +82,11 @@
                                         @if(count($h2['subheadings']) > 0)
                                             <div class="children" style="display: none;">
                                                 @foreach($h2['subheadings'] as $h3)
-                                                    <a class="children ElementsTableOfContentsItem sl-block sl-no-underline"
+                                                    <a class="ElementsTableOfContentsItem sl-block sl-no-underline"
                                                        href="#{!! $h3['slug'] !!}">
-                                                        <div title="{!! $h3['slug'] !!}"
+                                                        <div title="{!! $h3['name'] !!}" id="toc-item-{!! $h3['slug'] !!}"
                                                              class="sl-flex sl-items-center sl-h-md sl-pr-4 sl-pl-12 sl-bg-canvas-100 hover:sl-bg-canvas-200 sl-cursor-pointer sl-select-none">
-                                                            <div class="sl-flex-1 sl-items-center sl-truncate sl-mr-1.5">{!! $h3['name'] !!}</div>
+                                                            {!! $h3['name'] !!}
                                                         </div>
                                                     </a>
                                                 @endforeach