trilium/src/public/javascripts/services/utils.js

182 lines
3.7 KiB
JavaScript
Raw Normal View History

function reloadApp() {
window.location.reload(true);
}
function parseDate(str) {
try {
return new Date(Date.parse(str));
2018-03-25 11:37:55 +08:00
}
catch (e) {
throw new Error("Can't parse date from " + str + ": " + e.stack);
2018-03-25 11:37:55 +08:00
}
}
2018-03-25 11:37:55 +08:00
function padNum(num) {
return (num <= 9 ? "0" : "") + num;
}
2018-03-25 11:37:55 +08:00
function formatTime(date) {
return padNum(date.getHours()) + ":" + padNum(date.getMinutes());
}
2018-03-25 11:37:55 +08:00
function formatTimeWithSeconds(date) {
return padNum(date.getHours()) + ":" + padNum(date.getMinutes()) + ":" + padNum(date.getSeconds());
}
function formatDate(date) {
// return padNum(date.getDate()) + ". " + padNum(date.getMonth() + 1) + ". " + date.getFullYear();
// instead of european format we'll just use ISO as that's pretty unambiguous
return formatDateISO(date);
}
function formatDateISO(date) {
return date.getFullYear() + "-" + padNum(date.getMonth() + 1) + "-" + padNum(date.getDate());
}
function formatDateTime(date) {
return formatDate(date) + " " + formatTime(date);
}
function now() {
return formatTimeWithSeconds(new Date());
}
function isElectron() {
return window && window.process && window.process.type;
}
function isMac() {
return navigator.platform.indexOf('Mac') > -1;
}
function assertArguments() {
for (const i in arguments) {
if (!arguments[i]) {
console.trace(`Argument idx#${i} should not be falsy: ${arguments[i]}`);
2018-03-25 11:37:55 +08:00
}
}
}
function isTopLevelNode(node) {
return isRootNode(node.getParent());
}
2018-03-25 11:37:55 +08:00
function isRootNode(node) {
return node.data.noteId === "root";
}
2018-03-25 11:37:55 +08:00
function escapeHtml(str) {
return $('<div/>').text(str).html();
}
2018-03-25 11:37:55 +08:00
async function stopWatch(what, func) {
const start = new Date();
2018-03-25 11:37:55 +08:00
const ret = await func();
2018-02-05 09:23:30 +08:00
const tookMs = new Date().getTime() - start.getTime();
2018-02-05 09:23:30 +08:00
console.log(`${what} took ${tookMs}ms`);
2018-02-05 09:23:30 +08:00
return ret;
}
function formatValueWithWhitespace(val) {
return /[^\w_-]/.test(val) ? '"' + val + '"' : val;
}
function formatLabel(label) {
let str = "@" + formatValueWithWhitespace(label.name);
2018-03-25 11:37:55 +08:00
if (label.value !== "") {
str += "=" + formatValueWithWhitespace(label.value);
}
return str;
}
function getHost() {
const url = new URL(window.location.href);
return url.protocol + "//" + url.hostname + ":" + url.port;
}
2018-02-25 23:55:21 +08:00
function download(url) {
if (isElectron()) {
const remote = require('electron').remote;
2018-02-25 23:55:21 +08:00
remote.getCurrentWebContents().downloadURL(url);
2018-02-25 23:55:21 +08:00
}
else {
window.location.href = url;
}
}
function toObject(array, fn) {
const obj = {};
for (const item of array) {
2018-03-26 11:25:17 +08:00
const [key, value] = fn(item);
2018-03-25 11:37:55 +08:00
2018-03-26 11:25:17 +08:00
obj[key] = value;
}
return obj;
}
function randomString(len) {
let text = "";
const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < len; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
function bindShortcut(keyboardShortcut, handler) {
2018-12-25 01:39:31 +08:00
if (isDesktop()) {
$(document).bind('keydown', keyboardShortcut, e => {
handler();
2018-12-25 01:39:31 +08:00
e.preventDefault();
});
}
}
function isMobile() {
return window.device === "mobile";
}
function isDesktop() {
return window.device === "desktop";
}
export default {
reloadApp,
parseDate,
padNum,
formatTime,
formatTimeWithSeconds,
formatDate,
formatDateISO,
formatDateTime,
now,
isElectron,
isMac,
assertArguments,
isTopLevelNode,
isRootNode,
escapeHtml,
stopWatch,
formatValueWithWhitespace,
formatLabel,
getHost,
download,
toObject,
randomString,
bindShortcut,
isMobile,
isDesktop
};