diff --git a/assets/misuzu.js/main.js b/assets/misuzu.js/main.js index 3f1fa04..d3888eb 100644 --- a/assets/misuzu.js/main.js +++ b/assets/misuzu.js/main.js @@ -1,5 +1,7 @@ +#include sakuya.js + var Misuzu = function() { - timeago.render($qa('time')); + Sakuya.trackElements($qa('time')); hljs.initHighlighting(); MszEmbed.init(location.protocol + '//uiharu.' + location.host); diff --git a/assets/misuzu.js/sakuya.js b/assets/misuzu.js/sakuya.js new file mode 100644 index 0000000..cb3b3c3 --- /dev/null +++ b/assets/misuzu.js/sakuya.js @@ -0,0 +1,44 @@ +const Sakuya = (function() { + const formatter = new Intl.RelativeTimeFormat(undefined, { numeric: 'auto' }); + const divisions = [ + { amount: 60, name: 'seconds' }, + { amount: 60, name: 'minutes' }, + { amount: 24, name: 'hours' }, + { amount: 7, name: 'days' }, + { amount: 4.34524, name: 'weeks' }, + { amount: 12, name: 'months' }, + { amount: Infinity, name: 'years' }, + ]; + + const formatTimeAgo = function(date) { + let duration = (date - new Date()) / 1000; + + for(const div of divisions) { + if(Math.abs(duration) < div.amount) + return formatter.format(Math.round(duration), div.name); + duration /= div.amount; + } + }; + + // todo: update at intervals + const trackElement = function(elem) { + if(elem.dateTime === undefined) + return; + + try { + const dateTime = new Date(elem.dateTime); + elem.textContent = formatTimeAgo(dateTime); + } catch {} + }; + + const trackElements = function(elems) { + for(const elem of elems) + trackElement(elem); + }; + + return { + formatTimeAgo: formatTimeAgo, + trackElement: trackElement, + trackElements: trackElements, + }; +})(); diff --git a/public/vendor/timeago/LICENSE b/public/vendor/timeago/LICENSE deleted file mode 100644 index 5690493..0000000 --- a/public/vendor/timeago/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2016 Hust.cc - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file diff --git a/public/vendor/timeago/timeago.locales.min.js b/public/vendor/timeago/timeago.locales.min.js deleted file mode 100644 index 5378ec4..0000000 --- a/public/vendor/timeago/timeago.locales.min.js +++ /dev/null @@ -1 +0,0 @@ -!function(){"use strict";var t=[["ثانية","ثانيتين","%s ثوان","%s ثانية"],["دقيقة","دقيقتين","%s دقائق","%s دقيقة"],["ساعة","ساعتين","%s ساعات","%s ساعة"],["يوم","يومين","%s أيام","%s يوماً"],["أسبوع","أسبوعين","%s أسابيع","%s أسبوعاً"],["شهر","شهرين","%s أشهر","%s شهراً"],["عام","عامين","%s أعوام","%s عاماً"]];var e=s.bind(null,"секунду","%s секунду","%s секунды","%s секунд"),a=s.bind(null,"хвіліну","%s хвіліну","%s хвіліны","%s хвілін"),u=s.bind(null,"гадзіну","%s гадзіну","%s гадзіны","%s гадзін"),i=s.bind(null,"дзень","%s дзень","%s дні","%s дзён"),r=s.bind(null,"тыдзень","%s тыдзень","%s тыдні","%s тыдняў"),o=s.bind(null,"месяц","%s месяц","%s месяцы","%s месяцаў"),m=s.bind(null,"год","%s год","%s гады","%s гадоў");function s(s,n,e,a,u){var t=u%10,i=a;return 1===u?i=s:1===t&&20 window.addEventListener('DOMContentLoaded', function() { Misuzu(); }); - -