mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
mobile: refactor
This commit is contained in:
committed by
Abdullah Atta
parent
c881e74aae
commit
fb616a246e
24
packages/common/src/database.ts
Normal file
24
packages/common/src/database.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
This file is part of the Notesnook project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
//@ts-ignore
|
||||
import Database from "@notesnook/core/api/index";
|
||||
|
||||
export const database = new Database();
|
||||
88
packages/common/src/hooks/use-time-ago.ts
Normal file
88
packages/common/src/hooks/use-time-ago.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
This file is part of the Notesnook project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { TDate, format, register } from "timeago.js";
|
||||
const shortLocale: [string, string][] = [
|
||||
["now", "now"],
|
||||
["%ss", "in %ss"],
|
||||
["1m", "in 1m"],
|
||||
["%sm", "in %sm"],
|
||||
["1h", "in 1h"],
|
||||
["%sh", "in %sh"],
|
||||
["1d", "in 1d"],
|
||||
["%sd", "in %sd"],
|
||||
["1w", "in 1w"],
|
||||
["%sw", "in %sw"],
|
||||
["1mo", "in 1mo"],
|
||||
["%smo", "in %smo"],
|
||||
["1yr", "in 1yr"],
|
||||
["%syr", "in %syr"]
|
||||
];
|
||||
|
||||
const enShortLocale: [string, string][] = [
|
||||
["now", "now"],
|
||||
["%ss ago", "in %ss"],
|
||||
["1m ago", "in 1m"],
|
||||
["%sm ago", "in %sm"],
|
||||
["1h ago", "in 1h"],
|
||||
["%sh ago", "in %sh"],
|
||||
["1d ago", "in 1d"],
|
||||
["%sd ago", "in %sd"],
|
||||
["1w ago", "in 1w"],
|
||||
["%sw ago", "in %sw"],
|
||||
["1mo ago", "in 1mo"],
|
||||
["%smo ago", "in %smo"],
|
||||
["1yr ago", "in 1yr"],
|
||||
["%syr ago", "in %syr"]
|
||||
];
|
||||
register("short", (_n, index) => shortLocale[index]);
|
||||
register("en_short", (_n, index) => enShortLocale[index]);
|
||||
|
||||
export function getTimeAgo(datetime: TDate, locale = "short") {
|
||||
return format(datetime, locale);
|
||||
}
|
||||
|
||||
type TimeAgoOptions = {
|
||||
locale?: "short" | "en_short";
|
||||
live?: boolean;
|
||||
interval?: number;
|
||||
onUpdate?: (timeAgo: string) => void;
|
||||
};
|
||||
|
||||
export function useTimeAgo(
|
||||
datetime: TDate,
|
||||
{ locale = "short", live = true, interval = 60000, onUpdate }: TimeAgoOptions
|
||||
) {
|
||||
const [timeAgo, setTimeAgo] = useState(getTimeAgo(datetime, locale));
|
||||
|
||||
useEffect(() => {
|
||||
if (!live) return;
|
||||
const reset = setInterval(() => {
|
||||
const value = getTimeAgo(datetime, locale);
|
||||
onUpdate?.(value);
|
||||
setTimeAgo(value);
|
||||
}, interval);
|
||||
return () => {
|
||||
clearInterval(reset);
|
||||
};
|
||||
}, [datetime, interval, locale, live, onUpdate]);
|
||||
|
||||
return timeAgo;
|
||||
}
|
||||
27
packages/common/src/index.ts
Normal file
27
packages/common/src/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
This file is part of the Notesnook project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export * from "./database";
|
||||
|
||||
export * from "./utils/file";
|
||||
export * from "./utils/number";
|
||||
export * from "./utils/total-notes";
|
||||
export * from "./utils/time";
|
||||
|
||||
export * from "./hooks/use-time-ago";
|
||||
87
packages/common/src/utils/file.ts
Normal file
87
packages/common/src/utils/file.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
This file is part of the Notesnook project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export function formatBytes(bytes: number, decimals = 2) {
|
||||
if (bytes === 0) return "0 Bytes";
|
||||
|
||||
const k = 1024;
|
||||
const dm = decimals < 0 ? 0 : decimals;
|
||||
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
||||
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
|
||||
}
|
||||
|
||||
/*jshint node:true*/
|
||||
|
||||
/**
|
||||
* Replaces characters in strings that are illegal/unsafe for filenames.
|
||||
* Unsafe characters are either removed or replaced by a substitute set
|
||||
* in the optional `options` object.
|
||||
*
|
||||
* Illegal Characters on Various Operating Systems
|
||||
* / ? < > \ : * | "
|
||||
* https://kb.acronis.com/content/39790
|
||||
*
|
||||
* Unicode Control codes
|
||||
* C0 0x00-0x1f & C1 (0x80-0x9f)
|
||||
* http://en.wikipedia.org/wiki/C0_and_C1_control_codes
|
||||
*
|
||||
* Reserved filenames on Unix-based systems (".", "..")
|
||||
* Reserved filenames in Windows ("CON", "PRN", "AUX", "NUL", "COM1",
|
||||
* "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
|
||||
* "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", and
|
||||
* "LPT9") case-insesitively and with or without filename extensions.
|
||||
*
|
||||
* Capped at 255 characters in length.
|
||||
* http://unix.stackexchange.com/questions/32795/what-is-the-maximum-allowed-filename-and-folder-size-with-ecryptfs
|
||||
*
|
||||
* @param {String} input Original filename
|
||||
* @param {Object} options {replacement: String | Function }
|
||||
* @return {String} Sanitized filename
|
||||
*/
|
||||
|
||||
const illegalRe = /[/?<>\\:*|"]/g;
|
||||
//var controlRe = /[x00-x1f\x80-\x9f]/g;
|
||||
const reservedRe = /^\.+$/;
|
||||
const windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
|
||||
const windowsTrailingRe = /[. ]+$/;
|
||||
const whitespace = /\s+/g;
|
||||
|
||||
function sanitize(input: string, replacement: string) {
|
||||
if (typeof input !== "string") {
|
||||
throw new Error("Input must be string");
|
||||
}
|
||||
const sanitized = input
|
||||
.replace(whitespace, replacement)
|
||||
.replace(illegalRe, replacement)
|
||||
.replace(reservedRe, replacement)
|
||||
.replace(windowsReservedRe, replacement)
|
||||
.replace(windowsTrailingRe, replacement);
|
||||
|
||||
return sanitized.slice(0, 254).toLowerCase();
|
||||
}
|
||||
|
||||
export function sanitizeFilename(
|
||||
input: string,
|
||||
options?: { replacement: string }
|
||||
) {
|
||||
const replacement = (options && options.replacement) || "";
|
||||
return sanitize(input, replacement);
|
||||
}
|
||||
26
packages/common/src/utils/number.ts
Normal file
26
packages/common/src/utils/number.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
This file is part of the Notesnook project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export function nth(number: number) {
|
||||
return (
|
||||
["st", "nd", "rd"][
|
||||
(((((number < 0 ? -number : number) + 90) % 100) - 10) % 10) - 1
|
||||
] || "th"
|
||||
);
|
||||
}
|
||||
40
packages/common/src/utils/time.ts
Normal file
40
packages/common/src/utils/time.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
This file is part of the Notesnook project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { formatDate } from "@notesnook/core/utils/date";
|
||||
import { database } from "../database";
|
||||
import { formatReminderTime } from "@notesnook/core/collections/reminders";
|
||||
|
||||
export function getFormattedDate(
|
||||
date: string | number | Date,
|
||||
type: "time" | "date-time" | "date" = "date-time"
|
||||
) {
|
||||
return formatDate(date, {
|
||||
dateFormat: database.settings?.getDateFormat() as string,
|
||||
timeFormat: database.settings?.getTimeFormat() as string,
|
||||
type: type
|
||||
});
|
||||
}
|
||||
|
||||
export function getFormattedReminderTime(reminder: any, short = false) {
|
||||
return formatReminderTime(reminder, short, {
|
||||
dateFormat: database.settings?.getDateFormat() as string,
|
||||
timeFormat: database.settings?.getTimeFormat() as string
|
||||
});
|
||||
}
|
||||
30
packages/common/src/utils/total-notes.ts
Normal file
30
packages/common/src/utils/total-notes.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
This file is part of the Notesnook project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import { database } from "../database";
|
||||
|
||||
export function getTotalNotes(item?: any) {
|
||||
if (!item || (item.type !== "notebook" && item.type !== "topic")) return 0;
|
||||
if (item.type === "topic") {
|
||||
return (
|
||||
database.notebooks?.notebook(item.notebookId)?.topics.topic(item.id)
|
||||
?.totalNotes || 0
|
||||
);
|
||||
}
|
||||
return database.notebooks?.notebook(item.id)?.totalNotes || 0;
|
||||
}
|
||||
Reference in New Issue
Block a user