Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 1147x 1147x 1147x 1147x 1x 2x 1147x 11x 8x 3x 3x 1147x 1147x 1147x 17x 2x 2x 3x 7x 7x 1x 9x 23x 23x 75x 1147x 4538x 4369x 169x 169x 1147x 1147x 1147x 1x 1x 2x 2x 1x 7x 1x 1x 1x 1x 1x 3x 1x 1x 1147x 138x 114x 24x 24x 1147x 1147x 1147x 1147x 4946x 1147x 8x 8x 7x 8x 2x 6x 5x 6x 1147x 43x 21x 22x 11x 11x 1147x 138x 138x | import * as timeago from 'timeago.js'; import { languageCode, s__ } from '~/locale'; import { DEFAULT_DATE_TIME_FORMAT, localeDateFormat } from '~/lib/utils/datetime/locale_dateformat'; /** * Timeago uses underscores instead of dashes to separate language from country code. * * see https://github.com/hustcc/timeago.js/tree/v3.0.0/locales */ export const timeagoLanguageCode = languageCode().replace(/-/g, '_'); /** * Registers timeago locales */ const memoizedLocaleRemaining = () => { const cache = []; const timeAgoLocaleRemaining = [ () => [s__('Timeago|just now'), s__('Timeago|right now')], () => [s__('Timeago|just now'), s__('Timeago|%s seconds remaining')], () => [s__('Timeago|1 minute ago'), s__('Timeago|1 minute remaining')], () => [s__('Timeago|%s minutes ago'), s__('Timeago|%s minutes remaining')], () => [s__('Timeago|1 hour ago'), s__('Timeago|1 hour remaining')], () => [s__('Timeago|%s hours ago'), s__('Timeago|%s hours remaining')], () => [s__('Timeago|1 day ago'), s__('Timeago|1 day remaining')], () => [s__('Timeago|%s days ago'), s__('Timeago|%s days remaining')], () => [s__('Timeago|1 week ago'), s__('Timeago|1 week remaining')], () => [s__('Timeago|%s weeks ago'), s__('Timeago|%s weeks remaining')], () => [s__('Timeago|1 month ago'), s__('Timeago|1 month remaining')], () => [s__('Timeago|%s months ago'), s__('Timeago|%s months remaining')], () => [s__('Timeago|1 year ago'), s__('Timeago|1 year remaining')], () => [s__('Timeago|%s years ago'), s__('Timeago|%s years remaining')], ]; return (number, index) => { if (cache[index]) { return cache[index]; } cache[index] = timeAgoLocaleRemaining[index] && timeAgoLocaleRemaining[index](); return cache[index]; }; }; const memoizedLocale = () => { const cache = []; const timeAgoLocale = [ () => [s__('Timeago|just now'), s__('Timeago|right now')], () => [s__('Timeago|just now'), s__('Timeago|in %s seconds')], () => [s__('Timeago|1 minute ago'), s__('Timeago|in 1 minute')], () => [s__('Timeago|%s minutes ago'), s__('Timeago|in %s minutes')], () => [s__('Timeago|1 hour ago'), s__('Timeago|in 1 hour')], () => [s__('Timeago|%s hours ago'), s__('Timeago|in %s hours')], () => [s__('Timeago|1 day ago'), s__('Timeago|in 1 day')], () => [s__('Timeago|%s days ago'), s__('Timeago|in %s days')], () => [s__('Timeago|1 week ago'), s__('Timeago|in 1 week')], () => [s__('Timeago|%s weeks ago'), s__('Timeago|in %s weeks')], () => [s__('Timeago|1 month ago'), s__('Timeago|in 1 month')], () => [s__('Timeago|%s months ago'), s__('Timeago|in %s months')], () => [s__('Timeago|1 year ago'), s__('Timeago|in 1 year')], () => [s__('Timeago|%s years ago'), s__('Timeago|in %s years')], ]; return (number, index) => { if (cache[index]) { return cache[index]; } cache[index] = timeAgoLocale[index] && timeAgoLocale[index](); return cache[index]; }; }; /** * Registers timeago time duration */ const memoizedLocaleDuration = () => { const cache = []; const durations = [ () => [s__('Duration|%s seconds')], () => [s__('Duration|%s seconds')], () => [s__('Duration|1 minute')], () => [s__('Duration|%s minutes')], () => [s__('Duration|1 hour')], () => [s__('Duration|%s hours')], () => [s__('Duration|1 day')], () => [s__('Duration|%s days')], () => [s__('Duration|1 week')], () => [s__('Duration|%s weeks')], () => [s__('Duration|1 month')], () => [s__('Duration|%s months')], () => [s__('Duration|1 year')], () => [s__('Duration|%s years')], ]; return (_, index) => { if (cache[index]) { return cache[index]; } cache[index] = durations[index] && durations[index](); return cache[index]; }; }; timeago.register(timeagoLanguageCode, memoizedLocale()); timeago.register(`${timeagoLanguageCode}-remaining`, memoizedLocaleRemaining()); timeago.register(`${timeagoLanguageCode}-duration`, memoizedLocaleDuration()); export const getTimeago = (formatName) => window.gon?.time_display_relative === false ? localeDateFormat[formatName] ?? localeDateFormat[DEFAULT_DATE_TIME_FORMAT] : timeago; /** * For the given elements, sets a tooltip with a formatted date. * @param {Array<Node>|NodeList} elements * @param {Boolean} updateTooltip */ export const localTimeAgo = (elements, updateTooltip = true) => { const { format } = getTimeago(); elements.forEach((el) => { el.innerText = format(el.dateTime, timeagoLanguageCode); }); if (!updateTooltip) { return; } function addTimeAgoTooltip() { elements.forEach((el) => { // Recreate with custom template el.setAttribute('title', localeDateFormat.asDateTimeFull.format(el.dateTime)); }); } requestIdleCallback(addTimeAgoTooltip); }; /** * Returns remaining or passed time over the given time. * @param {*} time * @param {*} expiredLabel */ export const timeFor = (time, expiredLabel) => { if (!time) { return ''; } if (new Date(time) < new Date()) { return expiredLabel || s__('Timeago|Past due'); } return timeago.format(time, `${timeagoLanguageCode}-remaining`).trim(); }; /** * Returns a duration of time given an amount. * * @param {number} milliseconds - Duration in milliseconds. * @returns {string} A formatted duration, e.g. "10 minutes". */ export const duration = (milliseconds) => { const now = new Date(); return timeago .format(now.getTime() - Math.abs(milliseconds), `${timeagoLanguageCode}-duration`) .trim(); }; |