From 6f03209e93647765454f7a9b7864069fa823a161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=A1=E6=A2=A6=E6=98=9F=E5=B0=98?= Date: Sun, 11 Sep 2022 22:40:38 +0800 Subject: [PATCH] :lipstick: Support site information widget. close #30, #34 --- .../outline/sidebar/sidebar-card-widget.scss | 4 +- assets/js/next-boot.js | 2 + assets/js/utils.js | 49 +++++++++++++++++++ i18n/en.yaml | 8 ++- i18n/zh-cn.yaml | 21 ++++++-- layouts/partials/_funs/cal_siteinfo.html | 16 ++++++ layouts/partials/init.html | 6 +++ layouts/partials/sidebar.html | 2 +- layouts/partials/sidebar/siteinfo.html | 48 ++++++++++++++++++ layouts/partials/sidebar/webinfo.html | 34 ------------- 10 files changed, 146 insertions(+), 44 deletions(-) create mode 100644 layouts/partials/_funs/cal_siteinfo.html create mode 100644 layouts/partials/sidebar/siteinfo.html delete mode 100644 layouts/partials/sidebar/webinfo.html diff --git a/assets/css/_common/outline/sidebar/sidebar-card-widget.scss b/assets/css/_common/outline/sidebar/sidebar-card-widget.scss index b989cc3..5dd9cea 100644 --- a/assets/css/_common/outline/sidebar/sidebar-card-widget.scss +++ b/assets/css/_common/outline/sidebar/sidebar-card-widget.scss @@ -16,11 +16,11 @@ } } - .webinfo { + .siteinfo { font-size: $font-size-small; text-align: left; - .webinfo-item { + .siteinfo-item { display: flex; -webkit-box-align: center; align-items: center; diff --git a/assets/js/next-boot.js b/assets/js/next-boot.js index cad11e4..efe436e 100644 --- a/assets/js/next-boot.js +++ b/assets/js/next-boot.js @@ -36,6 +36,8 @@ NexT.boot.refresh = function() { if (!NexT.CONFIG.page.isPage) return; NexT.utils.registerSidebarTOC(); + NexT.utils.calSiteInfo(); + NexT.utils.replacePostCRLink(); NexT.utils.registerCopyCode(); NexT.utils.registerPostReward(); diff --git a/assets/js/utils.js b/assets/js/utils.js index 9c083a1..906fb60 100644 --- a/assets/js/utils.js +++ b/assets/js/utils.js @@ -23,6 +23,55 @@ HTMLElement.prototype.wrap = function(wrapper) { NexT.utils = { + calSiteInfo:() => { + const $runtimeCount = document.getElementById('runTimes'); + if ($runtimeCount) { + const publishDate = $runtimeCount.getAttribute('data-publishDate'); + $runtimeCount.innerText = NexT.utils.diffDate(publishDate) + NexT.CONFIG.i18n.ds_days; + } + const lastPushDate = document.getElementById('last-push-date'); + if (lastPushDate) { + const pushStr = NexT.utils.diffDate(lastPushDate.getAttribute('data-lastPushDate'), true); + lastPushDate.innerHTML = pushStr; + } + }, + + diffDate: (d, more = false) => { + const dateNow = new Date() + const datePost = new Date(d) + const dateDiff = dateNow.getTime() - datePost.getTime() + const minute = 1000 * 60 + const hour = minute * 60 + const day = hour * 24 + const month = day * 30 + + let result + if (more) { + const monthCount = dateDiff / month + const dayCount = dateDiff / day + const hourCount = dateDiff / hour + const minuteCount = dateDiff / minute + + if (monthCount > 12) { + result = datePost.toLocaleDateString().replace(/\//g, '-') + } else if (monthCount >= 1) { + result = parseInt(monthCount) + NexT.CONFIG.i18n.ds_month + } else if (dayCount >= 1) { + result = parseInt(dayCount) + NexT.CONFIG.i18n.ds_day + } else if (hourCount >= 1) { + result = parseInt(hourCount) + NexT.CONFIG.i18n.ds_hour + } else if (minuteCount >= 1) { + result = parseInt(minuteCount) + NexT.CONFIG.i18n.ds_min + } else { + result = NexT.CONFIG.i18n.ds_just + } + } else { + result = parseInt(dateDiff / day) + } + + return result + }, + checkDOMExist: function(selector) { return document.querySelector(selector) != null; }, diff --git a/i18n/en.yaml b/i18n/en.yaml index 65f0158..fdd2132 100644 --- a/i18n/en.yaml +++ b/i18n/en.yaml @@ -135,9 +135,13 @@ PostCommentTitle: other: Comments VisitorsLabel: - other: Total Visitors + other: "Visitors: " PageViewsLabel: - other: Total Page Views + other: "Page Views: " +WordsLabel: + other: "Words: " +ReadTimesLabel: + other: "ReadTimes: " FooterPowerby: other: "Power by %s" diff --git a/i18n/zh-cn.yaml b/i18n/zh-cn.yaml index b72ee34..4c11c51 100644 --- a/i18n/zh-cn.yaml +++ b/i18n/zh-cn.yaml @@ -135,10 +135,13 @@ RewardBitcoin: PostCommentTitle: other: 评论交流 -VisitorsLabel: - other: 总访客数: -PageViewsLabel: - other: 页面浏览: +SiteInfoItems: + runTimes: "已运行:" + words: "总字数:" + readTimes: "耗时约:" + visitors: "总访客数:" + pageViews: "页面浏览:" + lastUpdate: "最后更新于:" FooterPowerby: other: 由 %s 强力驱动 @@ -150,4 +153,12 @@ SearchEmpty: SearchHits: hits: "找到 ${hits} 个搜索结果" SearchHitsTime: - other: "找到 ${hits} 个搜索结果(用时 ${time} 毫秒)" \ No newline at end of file + other: "找到 ${hits} 个搜索结果(用时 ${time} 毫秒)" + +DateSuffix: + days: " 天" + month: " 个月前" + day: " 天前" + hour: " 小时前" + min: " 分钟前" + just: "刚刚" \ No newline at end of file diff --git a/layouts/partials/_funs/cal_siteinfo.html b/layouts/partials/_funs/cal_siteinfo.html new file mode 100644 index 0000000..b1fe5ab --- /dev/null +++ b/layouts/partials/_funs/cal_siteinfo.html @@ -0,0 +1,16 @@ +{{ $scratch := newScratch }} +{{ $pages := (where .Page.Site.RegularPages "Section" "in" .Site.Params.mainSections).ByLastmod.Reverse }} + +{{ range $pages }} + {{ $scratch.Add "totalWords" .WordCount }} + {{ $scratch.Add "totalTimes" .ReadingTime }} +{{ end }} + +{{ range first 1 $pages }} + {{ $scratch.Set "first" (time.Format .Site.Params.timeFormat .Date) }} +{{ end }} +{{ range last 1 $pages }} + {{ $scratch.Set "last" (time.Format .Site.Params.timeFormat .Date) }} +{{ end }} + +{{ return $scratch }} \ No newline at end of file diff --git a/layouts/partials/init.html b/layouts/partials/init.html index a2ac596..b8253f1 100644 --- a/layouts/partials/init.html +++ b/layouts/partials/init.html @@ -40,6 +40,12 @@ "empty" (T "SearchEmpty") "hits_time" (T "SearchHitsTime") "hits" (T "SearchHits") + "ds_days" (T "DateSuffix.days") + "ds_month" (T "DateSuffix.month") + "ds_day" (T "DateSuffix.day") + "ds_hour" (T "DateSuffix.hour") + "ds_min" (T "DateSuffix.min") + "ds_just" (T "DateSuffix.just") ) }} diff --git a/layouts/partials/sidebar.html b/layouts/partials/sidebar.html index 5f76fce..2331580 100644 --- a/layouts/partials/sidebar.html +++ b/layouts/partials/sidebar.html @@ -34,7 +34,7 @@ - {{ partial "sidebar/webinfo.html" . }} + {{ partial "sidebar/siteinfo.html" . }} {{- if and .Site.Params.backTop.enable .Site.Params.backTop.sidebar }}