From ac0a3791371a3f6834199f1beaee220fc7ef64c1 Mon Sep 17 00:00:00 2001 From: razonyang Date: Mon, 22 Aug 2022 10:56:07 +0800 Subject: [PATCH 1/4] feat(search): add sorting support --- assets/search/engine.ts | 30 ++++++++++++++++++++++++++++++ assets/search/form.ts | 4 ++++ i18n/en.toml | 6 ++++++ i18n/zh-cn.toml | 6 ++++++ i18n/zh-hans.toml | 6 ++++++ i18n/zh-hant.toml | 6 ++++++ i18n/zh-hk.toml | 6 ++++++ i18n/zh-tw.toml | 6 ++++++ layouts/partials/search/form.html | 5 +++++ layouts/partials/search/index.json | 1 + 10 files changed, 76 insertions(+) diff --git a/assets/search/engine.ts b/assets/search/engine.ts index 3e9ebb180..aecddec55 100644 --- a/assets/search/engine.ts +++ b/assets/search/engine.ts @@ -3,6 +3,7 @@ import Form from './form'; class Engine { private fuse: Fuse; + private pages; constructor(form: Form, callback: (data: FormData) => void) { const options = Object.assign(window.fuseOptions, { @@ -21,6 +22,7 @@ class Engine { return response.json(); }).then((response) => { const pages = response.pages; + this.pages = pages; const taxonomies = ['categories', 'authors', 'series', 'tags']; for (const i in taxonomies) { const datalist = document.querySelector( @@ -42,7 +44,35 @@ class Engine { }) } + sortByDate(a, b, asc = true): number { + if (!(a.idx in this.pages)) { + return 1; + } + if (!(b.idx in this.pages)) { + return -1; + } + return this.pages[a.idx].timestamp < this.pages[b.idx].timestamp ? (asc ? -1 : 1) : (asc ? 1 : -1) + } + + sortByScore(a, b): number { + return a.score === b.score ? a.idx < b.idx ? -1 : 1 : a.score < b.score ? -1 : 1 + } + search(data: FormData) { + console.log(data.get('sort')) + switch(data.get('sort')) { + case 'date asc': + this.fuse.options.sortFn = (a, b) => this.sortByDate(a, b) + break; + case 'date desc': + this.fuse.options.sortFn = (a, b): number => { + return this.sortByDate(a, b, false) + } + break; + default: + this.fuse.options.sortFn = this.sortByScore + break; + } return new Promise((resolve) => { // delay search for displaying the loading spinner. setTimeout(() => { diff --git a/assets/search/form.ts b/assets/search/form.ts index f039241a7..45f4c8395 100644 --- a/assets/search/form.ts +++ b/assets/search/form.ts @@ -25,6 +25,10 @@ class Form { this.lang.addEventListener('change', () => { this.submit(); }); + + form.querySelector('#sorting-select').addEventListener('change', () => { + this.submit(); + }) } private submit() { diff --git a/i18n/en.toml b/i18n/en.toml index a5f687d77..05f86d1e3 100644 --- a/i18n/en.toml +++ b/i18n/en.toml @@ -200,12 +200,18 @@ other = "Search" [search_advanced] other = "Advanced" +[search_date] +other = "Date" + [search_missing_keywords] other = "Please enter search keywords" [search_no_results] other = "No results found" +[search_sorting] +other = "Sorting" + [search_stat] other = "Found {{ .Total }} results" diff --git a/i18n/zh-cn.toml b/i18n/zh-cn.toml index 3066688cb..cfcd8af9d 100644 --- a/i18n/zh-cn.toml +++ b/i18n/zh-cn.toml @@ -190,12 +190,18 @@ other = "搜索" [search_advanced] other = "高级" +[search_date] +other = "日期" + [search_missing_keywords] other = "请输入搜索关键词" [search_no_results] other = "找不到相关结果" +[search_sorting] +other = "排序" + [search_stat] other = "找到相关结果 {{ .Total }} 个" diff --git a/i18n/zh-hans.toml b/i18n/zh-hans.toml index 3066688cb..cfcd8af9d 100644 --- a/i18n/zh-hans.toml +++ b/i18n/zh-hans.toml @@ -190,12 +190,18 @@ other = "搜索" [search_advanced] other = "高级" +[search_date] +other = "日期" + [search_missing_keywords] other = "请输入搜索关键词" [search_no_results] other = "找不到相关结果" +[search_sorting] +other = "排序" + [search_stat] other = "找到相关结果 {{ .Total }} 个" diff --git a/i18n/zh-hant.toml b/i18n/zh-hant.toml index 8badcac6f..7948ea209 100644 --- a/i18n/zh-hant.toml +++ b/i18n/zh-hant.toml @@ -190,12 +190,18 @@ other = "搜尋" [search_advanced] other = "高級" +[search_date] +other = "日期" + [search_missing_keywords] other = "請輸入搜尋關鍵詞" [search_no_results] other = "找不到相關結果" +[search_sorting] +other = "排序" + [search_stat] other = "找到相關結果 {{ .Total }} 個" diff --git a/i18n/zh-hk.toml b/i18n/zh-hk.toml index 790575d14..82c177b67 100644 --- a/i18n/zh-hk.toml +++ b/i18n/zh-hk.toml @@ -190,12 +190,18 @@ other = "搜索" [search_advanced] other = "高級" +[search_date] +other = "日期" + [search_missing_keywords] other = "請輸入搜索關鍵詞" [search_no_results] other = "找不到相關結果" +[search_sorting] +other = "排序" + [search_stat] other = "找到相關結果 {{ .Total }} 個" diff --git a/i18n/zh-tw.toml b/i18n/zh-tw.toml index 8badcac6f..7948ea209 100644 --- a/i18n/zh-tw.toml +++ b/i18n/zh-tw.toml @@ -190,12 +190,18 @@ other = "搜尋" [search_advanced] other = "高級" +[search_date] +other = "日期" + [search_missing_keywords] other = "請輸入搜尋關鍵詞" [search_no_results] other = "找不到相關結果" +[search_sorting] +other = "排序" + [search_stat] other = "找到相關結果 {{ .Total }} 個" diff --git a/layouts/partials/search/form.html b/layouts/partials/search/form.html index 1ed84c0cd..61dcda893 100644 --- a/layouts/partials/search/form.html +++ b/layouts/partials/search/form.html @@ -16,6 +16,11 @@