refactor: convert to ECMAScript where necessary
Also drop @league-of-foundry-developers/foundry-vtt-types.
This commit is contained in:
parent
df4538f6ed
commit
6277e27056
69 changed files with 1077 additions and 1679 deletions
61
src/ui/notifications.js
Normal file
61
src/ui/notifications.js
Normal file
|
@ -0,0 +1,61 @@
|
|||
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { logger } from "../utils/logger";
|
||||
import { getNotificationsSafe } from "../utils/utils";
|
||||
|
||||
/**
|
||||
* @typedef {Object} NotificationOptions
|
||||
* @property {boolean} [permanent=false]
|
||||
* @property {boolean} [log=false]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {(message: string, options?: NotificationOptions) => void} NotificationFunction
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {"info" | "warn" | "error"} NotificationType
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {NotificationType} type The type of the notification
|
||||
* @returns {NotificationFunction}
|
||||
*/
|
||||
function getNotificationFunction(type) {
|
||||
return (message, { permanent = false, log = false } = {}) => {
|
||||
if (ui.notifications) {
|
||||
ui.notifications[type](message, { permanent });
|
||||
if (log) {
|
||||
logger[type](message);
|
||||
}
|
||||
} else {
|
||||
logger[type](message);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} message
|
||||
* @param {NotificationType} type
|
||||
* @param {NotificationOptions} [options={}]
|
||||
*/
|
||||
function notify(message, type, { permanent = false, log = false } = {}) {
|
||||
const notifications = getNotificationsSafe();
|
||||
if (notifications) {
|
||||
notifications.notify(message, type, { permanent });
|
||||
if (log) {
|
||||
logger.getLoggingFunction(type)(message);
|
||||
}
|
||||
} else {
|
||||
logger.getLoggingFunction(type)(message);
|
||||
}
|
||||
}
|
||||
|
||||
export const notifications = Object.freeze({
|
||||
info: getNotificationFunction("info"),
|
||||
warn: getNotificationFunction("warn"),
|
||||
error: getNotificationFunction("error"),
|
||||
notify,
|
||||
});
|
|
@ -1,38 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { logger } from "../utils/logger";
|
||||
|
||||
function getNotificationFunction(type: "info" | "warn" | "error") {
|
||||
return (message: string, { permanent = false, log = false }: { permanent?: boolean; log?: boolean } = {}): void => {
|
||||
if (ui.notifications) {
|
||||
ui.notifications[type](message, { permanent });
|
||||
if (log) {
|
||||
logger[type](message);
|
||||
}
|
||||
} else {
|
||||
logger[type](message);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export const notifications = Object.freeze({
|
||||
info: getNotificationFunction("info"),
|
||||
warn: getNotificationFunction("warn"),
|
||||
error: getNotificationFunction("error"),
|
||||
notify: (
|
||||
message: string,
|
||||
type: "info" | "warning" | "error" = "info",
|
||||
{ permanent = false, log = false }: { permanent?: boolean; log?: boolean } = {},
|
||||
): void => {
|
||||
if (ui.notifications) {
|
||||
ui.notifications.notify(message, type, { permanent });
|
||||
if (log) {
|
||||
logger.getLoggingFunction(type)(message);
|
||||
}
|
||||
} else {
|
||||
logger.getLoggingFunction(type)(message);
|
||||
}
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue