mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: move lib/misc to ui
This commit is contained in:
1 parent
08462cf870
commit
baab7bb68c
10 files changed
+62
-69
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import AnsiUp from 'ansi_up';
|
||||
import moment from 'moment-timezone';
|
||||
import { ObjectID } from 'mongodb';
|
||||
|
||||
export { size, formatSeconds } from '@hydrooj/utils/lib/utils';
|
||||
|
||||
const AU = new AnsiUp();
|
||||
|
||||
export function ansiToHtml(str: string, whiteToBlack = true) {
|
||||
const res = AU.ansi_to_html(str);
|
||||
return whiteToBlack ? res.replace(/style="color:rgb\(255,255,255\)"/g, 'style="color:black"') : res;
|
||||
}
|
||||
|
||||
export function datetimeSpan(dt: Date | ObjectID, relative = true, format = 'YYYY-M-D H:mm:ss', tz = 'Asia/Shanghai') {
|
||||
if (!dt) return 'DATETIME_SPAN_ERROR';
|
||||
if (dt instanceof ObjectID) dt = new Date(dt.generationTime * 1000);
|
||||
else if (typeof dt === 'string' && ObjectID.isValid(dt)) dt = new Date(new ObjectID(dt).generationTime * 1000);
|
||||
else if (typeof dt === 'number' || typeof dt === 'string') dt = new Date(dt);
|
||||
return '<span class="time{0}" data-timestamp="{1}">{2}</span>'.format(
|
||||
relative ? ' relative' : '',
|
||||
dt.getTime() / 1000,
|
||||
moment(dt).tz(tz).format(format),
|
||||
);
|
||||
}
|
||||
|
||||
export function* paginate(page: number, numPages: number) {
|
||||
const radius = 5;
|
||||
let first: number;
|
||||
let last: number;
|
||||
if (page > 1) {
|
||||
if (page > 2) yield ['first', 1];
|
||||
yield ['previous', page - 1];
|
||||
}
|
||||
if (page <= radius) [first, last] = [1, Math.min(1 + radius * 2, numPages)];
|
||||
else if (page >= numPages - radius) {
|
||||
[first, last] = [Math.max(1, numPages - radius * 2), numPages];
|
||||
} else {
|
||||
[first, last] = [page - radius, page + radius];
|
||||
}
|
||||
if (first > 1) yield ['ellipsis', 0];
|
||||
for (let page0 = first; page0 < last + 1; page0++) {
|
||||
if (page0 !== page) yield ['page', page0];
|
||||
else yield ['current', page];
|
||||
}
|
||||
if (last < numPages) yield ['ellipsis', 0];
|
||||
if (page < numPages) {
|
||||
yield ['next', page + 1];
|
||||
if (page < numPages - 1) yield ['last', numPages];
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,9 @@ const { findFileSync } = require('@hydrooj/utils/lib/utils');
|
||||
const status = require('@hydrooj/utils/lib/status');
|
||||
const markdown = require('./markdown');
|
||||
const { xss, ensureTag } = require('./markdown-it-xss');
|
||||
const misc = require('./misc');
|
||||
|
||||
const { misc, buildContent, avatar } = global.Hydro.lib;
|
||||
const { buildContent, avatar } = global.Hydro.lib;
|
||||
|
||||
let { template } = argv.options;
|
||||
if (template && typeof template !== 'string') template = findFileSync('@hydrooj/ui-default/templates');
|
||||
|
||||
@@ -29,7 +29,6 @@
|
||||
"@types/qrcode": "^1.4.2",
|
||||
"@types/react-dom": "^18.0.6",
|
||||
"@types/redux-logger": "^3.0.9",
|
||||
"ansi_up": "^5.1.0",
|
||||
"autoprefixer": "^9.8.8",
|
||||
"browser-update": "^3.3.38",
|
||||
"chalk": "^5.0.1",
|
||||
@@ -112,6 +111,7 @@
|
||||
"webpackbar": "^5.0.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"ansi_up": "^5.1.0",
|
||||
"esbuild": "0.14.3",
|
||||
"fs-extra": "^10.1.0",
|
||||
"js-yaml": "^4.1.0",
|
||||
|
||||
Reference in new issue
Block a user