From ae1a08eb82e7fd68dbb912a1c6bc9ee26452e921 Mon Sep 17 00:00:00 2001 From: Spedon <70063177+Sped0n@users.noreply.github.com> Date: Sat, 20 Jan 2024 18:03:26 +0800 Subject: [PATCH] feat: embed threshold and index val of navbar into html/inline js (#241) * feat: refactor image retrieval logic and improve variable initialization - Add a new file `layouts/partials/function/getImageSlice.html` - Initialize variables `$context`, `$Path`, and `$params` - Set `$Path` based on the result of the `partial/function/currentMenuItem.html` partial - Retrieve the page `$gallery` based on `$Path` - Return all resources of type `image` from `$gallery` * refactor: adapt getImageSlice function to `single.json` * feat: refactor navigation UI components to use static go template * feat: update navigation functionality and threshold rendering --- assets/ts/nav.ts | 5 --- layouts/_default/single.json | 16 +++---- layouts/partials/function/getImageSlice.html | 10 +++++ layouts/partials/nav.html | 45 ++++++++++++++++++-- 4 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 layouts/partials/function/getImageSlice.html diff --git a/assets/ts/nav.ts b/assets/ts/nav.ts index 911551c..6435ebe 100644 --- a/assets/ts/nav.ts +++ b/assets/ts/nav.ts @@ -36,11 +36,6 @@ const indexDispNums = Array.from( */ export function initNav(): void { - const s = state.get() - // init threshold text - updateThresholdText(expand(s.threshold)) - // init index text - updateIndexText(expand(s.index + 1), expand(s.length)) // add watcher for updating nav text state.addWatcher((o) => { updateIndexText(expand(o.index + 1), expand(o.length)) diff --git a/layouts/_default/single.json b/layouts/_default/single.json index f1d391e..5150800 100644 --- a/layouts/_default/single.json +++ b/layouts/_default/single.json @@ -1,15 +1,9 @@ -{{- $Page := . -}} -{{- $Path := "" -}} +{{- $context := . -}} {{- $params := .Page.Params | merge .Site.Params.Page -}} -{{- with partial "function/currentMenuItem.html" . -}} - {{- $Path = .Identifier -}} -{{- end -}} - -{{- $gallery := site.GetPage $Path -}} -{{- with $gallery.Resources.ByType "image" -}} +{{- with partial "function/getImageSlice.html" . -}} {{- $index := len . -}} - {{- $Page.Scratch.Add "img" slice -}} + {{- $context.Scratch.Add "img" slice -}} {{- range sort . "Name" "desc" -}} {{- $image := . -}} {{- $index = sub $index 1 -}} @@ -19,7 +13,7 @@ {{- end -}} {{- $lores := .Resize (site.Params.loResOpt | default "700x webp Lanczos q60") -}} {{- $hires := .Resize (site.Params.hiResOpt | default "2000x webp Lanczos q75") -}} - {{- $Page.Scratch.Add "img" (dict + {{- $context.Scratch.Add "img" (dict "index" (int $index) "alt" (string $alt) "loUrl" (string $lores.RelPermalink) @@ -31,7 +25,7 @@ ) -}} {{- end -}} - {{ $Page.Scratch.Get "img" | jsonify }} + {{ $context.Scratch.Get "img" | jsonify }} {{- else -}} [] {{- end -}} diff --git a/layouts/partials/function/getImageSlice.html b/layouts/partials/function/getImageSlice.html new file mode 100644 index 0000000..8bbfca2 --- /dev/null +++ b/layouts/partials/function/getImageSlice.html @@ -0,0 +1,10 @@ +{{- $context := . -}} +{{- $Path := "" -}} +{{- $params := .Page.Params | merge .Site.Params.Page -}} + +{{- with partial "function/currentMenuItem.html" . -}} + {{- $Path = .Identifier -}} +{{- end -}} + +{{- $gallery := site.GetPage $Path -}} +{{- return $gallery.Resources.ByType "image" -}} diff --git a/layouts/partials/nav.html b/layouts/partials/nav.html index f2592aa..a18ff47 100644 --- a/layouts/partials/nav.html +++ b/layouts/partials/nav.html @@ -33,6 +33,10 @@ {{- end -}}