Skip to content

Commit

Permalink
upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
cuuupid committed Nov 21, 2023
1 parent c5d3137 commit e7ae1dc
Show file tree
Hide file tree
Showing 19 changed files with 153 additions and 68 deletions.
8 changes: 6 additions & 2 deletions dist/api.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export declare const post: (url: string, data: any, token: string) => Promise<any>;
/** Sets the API URL. */
export declare const setAPI: (url: string) => string;
/** Sends a POST request to an endpoint under the API (set using setAPI). */
export declare const POST: (endpoint: string, data: any, token: string) => Promise<any>;
declare global {
interface Window {
post: (url: string, data: any, token: string) => Promise<any>;
POST: (endpoint: string, data: any, token: string) => Promise<any>;
setAPI: (url: string) => void;
}
}
17 changes: 11 additions & 6 deletions dist/api.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.post = void 0;
const API = 'https://mail.helloaiko.com';
const post = async (url, data, token) => {
const s = await fetch(API + url, {
exports.POST = exports.setAPI = void 0;
let API = 'https://mail.helloaiko.com';
/** Sets the API URL. */
const setAPI = (url) => API = url;
exports.setAPI = setAPI;
/** Sends a POST request to an endpoint under the API (set using setAPI). */
const POST = async (endpoint, data, token) => {
const s = await fetch(API + endpoint, {
method: 'POST',
body: JSON.stringify(data),
headers: {
Expand All @@ -28,5 +32,6 @@ const post = async (url, data, token) => {
throw d.error;
return d;
};
exports.post = post;
window.post = exports.post;
exports.POST = POST;
window.POST = exports.POST;
window.setAPI = exports.setAPI;
3 changes: 3 additions & 0 deletions dist/array.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ declare const _default: {};
export default _default;
declare global {
interface Array<T> {
/** Returns the last n elements from an array. */
tail: (n: number) => T;
/** Returns the very last element in an array (without popping). */
last: () => T;
/** Returns a random element from an array. */
random: () => T;
}
}
12 changes: 12 additions & 0 deletions dist/converters.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ declare const _default: {};
export default _default;
declare global {
interface Window {
/** Unescapes HTML to a string representation */
unescapeHTML: (s: string) => string;
/** Parses text from HTML string */
html2Text: (html: string) => string;
/** Converts HTML to string to a valid DOM element */
html2Element: (html: string) => ChildNode | null;
/** Converts an ObjectID (e.g. from MongoDB) to a Date */
objectId2Date: (id: string) => Date;
/** Converts an RGB channel (0-255) to a 2-digit hex */
channel2Hex: (c: number) => string;
/** Converts RGB channels to a 6-digit hex code, without leading `#` */
rgb2Hex: (r: number, g: number, b: number) => string;
/** Returns `true` if the color is considered "dark" for contrast purposes */
rgbIsDark: (r: number, g: number, b: number) => boolean;
/** Identifies the accent color (returned as hex code without leading `#`). */
image2Color: (url: string, dark?: boolean) => Promise<string | null>;
/** Converts a file extension to a FontAwesom icon. */
ext2FontAwesomeIcon: (ext: string) => string;
/** Converts a file extension to an SVG icon. */
ext2SVGIcon: (ext: string) => string;
/** Decodes a JWT token to a JSON object */
decodeJWT: (token: string) => any;
ColorThief: any;
}
}
7 changes: 7 additions & 0 deletions dist/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,10 @@ window.ext2SVGIcon = (ext) => {
return 'file-file.svg';
}
};
window.decodeJWT = (token) => {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/gim, '+').replace(/_/gim, '/');
return JSON.parse(decodeURIComponent(window.atob(base64).split('').map((c) => {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join('')));
};
39 changes: 39 additions & 0 deletions dist/date.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
declare global {
interface Date {
/** Converts a numerical index (0-11) to the matching month string. */
toMonth: () => string;
/** Converts a numerical index (0-6) to the matching day of the week. */
toDay: () => string;
/** Adds a number of days to a date and returns the result (**immutable**). */
addDays: (days: number) => Date;
/** Parses a JS Date to an Americanized date (`MM/DD/YYYY`) */
toDate: () => string;
/** Parses a JS Date to a (nice) Americanized date (`Day Mon DD`) */
toNiceDate: () => string;
/** Parses a JS Date to a (nicer) Americanized date (`DayOfWeek Month DD`) */
toNicerDate: () => string;
/** Parses a JS Date to 12-hour time (e.g. `12:34 PM`) */
toTime: () => string;
/** Parses a JS Date to Americanized date and 12-hour time (`MM/DD/YYYY 12:34 PM`) */
toDateTime: () => string;
/** Converts a JS Date to a friendly representation, relative to **today**.
*
* e.g.
*
* `Today, 12:34 PM`
*
* `Yesterday, 12:34 PM`
*
* `Tomorrow, 12:34 PM`
*
* `Last Monday, 12:34 PM`
*
* `12/31/2020, 12:34 PM`
*/
toNicerDateTime: () => string;
/** Converts a JS Date to a friendly representation, relative to **now**.
*
* e.g.
*
* `12:34 PM`
*
* `Mon 12:34 PM`
*
* `Fri 12:34 PM`
*
* `9/4`
*
* `12/31/2020`
*/
toNiceDateTime: (n?: number) => string;
}
}
Expand Down
24 changes: 22 additions & 2 deletions dist/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,23 @@ Date.prototype.toMonth = function () {
'June',
'July',
'August',
'September'
'September',
'October',
'November',
'December'
][this.getMonth()];
};
Date.prototype.toDay = function () {
return [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
][this.getDay()];
};
//! immutable
Date.prototype.addDays = function (days) {
const date = new Date(this.valueOf());
Expand All @@ -23,8 +37,14 @@ Date.prototype.addDays = function (days) {
Date.prototype.toDate = function () {
return `${this.getMonth() + 1}/${this.getDate()}/${this.getFullYear()}`;
};
Date.prototype.toNiceDate = function () {
return `${this.toDay().slice(0, 3)} ${this.toMonth().slice(0, 3)} ${this.getDate()}`;
};
Date.prototype.toNicerDate = function () {
return `${this.toDay()} ${this.toMonth()} ${this.getDate()}`;
};
Date.prototype.toTime = function () {
return this.toISOString().substr(11, 8);
return this.toISOString().slice(11, 11 + 8);
};
Date.prototype.toDateTime = function () {
return this.toDate() + ' ' + this.toTime();
Expand Down
6 changes: 4 additions & 2 deletions dist/misc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ declare const _default: null;
export default _default;
declare global {
interface Window {
downloadAndFillImage: (url: string, imgId: string, tries?: number) => Promise<any>;
app: any;
/** Copies a string to the clipboard. */
copy: (t: string) => void;
/** Inserts an element at the cursor caret. */
insertElementAtCursor: (el: Node) => void;
/** Converts a string into a data URL */
toDataURL: (src: string, outputFormat: string) => Promise<string>;
}
const Vue: any;
}
16 changes: 1 addition & 15 deletions dist/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,7 @@ const toDataURL = (src, outputFormat) => new Promise((s, _) => {
img.src = src;
}
});
window.downloadAndFillImage = async (url, imgId, tries = 10) => {
const el = document.getElementById(imgId);
if (!el && tries > 0)
return setTimeout(() => window.downloadAndFillImage(url, imgId, tries - 1), 500);
await Vue.nextTick();
const dataURL = await toDataURL(url, "image/png");
el.src = dataURL;
el.removeAttribute('id');
el.removeAttribute('data-formula');
const htmlContent = document.getElementsByClassName('editor')[0].children[0].innerHTML;
// FIXME: below is hard without typings
window.app.html = htmlContent;
window.app.editor.setContent(htmlContent);
return;
};
window.toDataURL = toDataURL;
window.copy = (t) => {
const el = document.createElement('textarea');
el.value = t;
Expand Down
2 changes: 2 additions & 0 deletions dist/number.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ declare const _default: null;
export default _default;
declare global {
interface Number {
/** Converts a number of seconds to 12-hour time (`12:34 PM`). */
secondsToTimestring: () => string;
/** Converts a number of bytes to a human-readable bytes (power-of-2). */
toFilesize: () => string;
}
}
21 changes: 14 additions & 7 deletions dist/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = null;
Number.prototype.secondsToTimestring = function () {
return new Date(this.valueOf() * 1000).toISOString().substr(11, 8);
return new Date(this.valueOf() * 1000).toISOString().slice(11, 11 + 8);
};
Number.prototype.toFilesize = function () {
const me = this.valueOf();
const byte = 1;
const kilobyte = byte * 1024;
const megabyte = kilobyte * 1024;
const gigabyte = megabyte * 1024;
if (this > gigabyte)
return (this.valueOf() / gigabyte).toFixed(2) + ' GB';
if (this > megabyte)
return (this.valueOf() / megabyte).toFixed(2) + ' MB';
if (this > kilobyte)
return (this.valueOf() / kilobyte).toFixed(2) + ' KB';
const terabyte = gigabyte * 1024;
const petabyte = terabyte * 1024;
if (me > petabyte)
return (me / gigabyte).toFixed(2) + ' PB';
if (me > terabyte)
return (me / gigabyte).toFixed(2) + ' TB';
if (me > gigabyte)
return (me / gigabyte).toFixed(2) + ' GB';
if (me > megabyte)
return (me / megabyte).toFixed(2) + ' MB';
if (me > kilobyte)
return (me / kilobyte).toFixed(2) + ' KB';
return this + ' bytes';
};
9 changes: 9 additions & 0 deletions dist/string.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ declare const _default: null;
export default _default;
declare global {
interface StringConstructor {
/** Generates a random hexadecimal string of specified length. */
random: (length: number) => string;
}
interface String {
/** Capitalizes the first letter of a string. */
capitalize: () => string;
/** Encodes a string to hexadecimal. */
hexEncode: () => string;
/** Gets the domain of a URL. */
getDomain: () => string | null;
/** Gets the avatar of an email address.
* Requires BoringAvatars and jdenticon to be loaded.
* Relies on a SVG2PNG polyfill.
*/
getAvatar: (opts: {
defaultTo?: string;
useBoringAvatars?: boolean;
Expand All @@ -16,6 +24,7 @@ declare global {
}) => Promise<string | null>;
}
interface Array<T> {
/** Joins up to `to` elements of an array with the specified separator. */
joinTo: (to: number, separator: string) => string;
}
interface Window {
Expand Down
1 change: 1 addition & 0 deletions dist/toast.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export declare const toast: (text: string) => null;
declare global {
interface Window {
/** Placeholder: this function exists for polyfill purposes. */
toast: (text: string) => null;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aiko/dwarfhaven",
"version": "2.2.4",
"version": "2.3.0",
"description": "",
"main": "src/index.ts",
"scripts": {
Expand Down
16 changes: 10 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const API: string = 'https://mail.helloaiko.com'
let API: string = 'https://mail.helloaiko.com'
/** Sets the API URL. */
export const setAPI = (url: string) => API = url

export const post = async (url: string, data: any, token: string) => {
const s = await fetch(API + url, {
/** Sends a POST request to an endpoint under the API (set using setAPI). */
export const POST = async (endpoint: string, data: any, token: string) => {
const s = await fetch(API + endpoint, {
method: 'POST',
body: JSON.stringify(data),
headers: {
Expand All @@ -28,9 +31,10 @@ export const post = async (url: string, data: any, token: string) => {

declare global {
interface Window {
/** Sends a POST request to Aiko Mail's API. */
post: (url: string, data: any, token: string) => Promise<any>
POST: (endpoint: string, data: any, token: string) => Promise<any>
setAPI: (url: string) => void
}
}

window.post = post
window.POST = POST
window.setAPI = setAPI
2 changes: 1 addition & 1 deletion src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare global {
interface Array<T> {
/** Returns the last n elements from an array. */
tail: (n: number) => T
/** Returns the very last element in an array. */
/** Returns the very last element in an array (without popping). */
last: () => T
/** Returns a random element from an array. */
random: () => T
Expand Down
2 changes: 1 addition & 1 deletion src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ window.unescapeHTML = (
return str
}
}
)()
)();

const parser = new DOMParser()

Expand Down
21 changes: 0 additions & 21 deletions src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ export default null

declare global {
interface Window {
/** Downloads an image from a URL and fills it into an image tag (specified by ID).
* @deprecated This function no longer applies to Vue 3 and will be removed or replaced in the future. */
downloadAndFillImage: (url: string, imgId: string, tries?: number, nextTick: any) => Promise<any>
app: any // FIXME: remove once typings complete
/** Copies a string to the clipboard. */
copy: (t: string) => void
/** Inserts an element at the cursor caret. */
Expand Down Expand Up @@ -40,23 +36,6 @@ const toDataURL = (src: string, outputFormat: string): Promise<string> => new Pr

window.toDataURL = toDataURL

window.downloadAndFillImage = async (url, imgId, tries=10, nextTick) => {
const el = document.getElementById(imgId) as HTMLImageElement
if (!el && tries > 0) return setTimeout(() => window.downloadAndFillImage(url, imgId, tries-1, nextTick), 500)

await nextTick()

const dataURL = await toDataURL(url, "image/png")
el.src = dataURL
el.removeAttribute('id')
el.removeAttribute('data-formula')
const htmlContent = document.getElementsByClassName('editor')[0].children[0].innerHTML
// FIXME: below is hard without typings
window.app.html = htmlContent
window.app.editor.setContent(htmlContent)
return
}

window.copy = (t: string) => {
const el = document.createElement('textarea')
el.value = t
Expand Down
Loading

0 comments on commit e7ae1dc

Please sign in to comment.