Massive restructure of plugin API

This commit is contained in:
Zef Hemel
2022-10-14 15:11:33 +02:00
parent 982623fc38
commit 7d28b53b75
70 changed files with 826 additions and 969 deletions
+36
View File
@@ -0,0 +1,36 @@
import { editor } from "$sb/silverbullet-syscall/mod.ts";
export async function replaceAsync(
str: string,
regex: RegExp,
asyncFn: (match: string, ...args: any[]) => Promise<string>,
) {
const promises: Promise<string>[] = [];
str.replace(regex, (match: string, ...args: any[]): string => {
const promise = asyncFn(match, ...args);
promises.push(promise);
return "";
});
const data = await Promise.all(promises);
return str.replace(regex, () => data.shift()!);
}
export function isServer() {
return (
typeof window === "undefined" || typeof window.document === "undefined"
); // if something defines window the same way as the browser, this will fail.
}
// this helps keep if's condition as positive
export function isBrowser() {
return !isServer();
}
export function notifyUser(message: string, type?: "info" | "error") {
if (isBrowser()) {
return editor.flashNotification(message, type);
}
const log = type === "error" ? console.error : console.log;
log(message); // we should end up sending the message to the user, users dont read logs.
return;
}