Skip to content

Commit

Permalink
function => const
Browse files Browse the repository at this point in the history
  • Loading branch information
tamaina committed Jan 10, 2023
1 parent 722f138 commit 34f2feb
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions packages/frontend/src/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export const apiWithDialog = ((
return promise;
}) as typeof api;

export function promiseDialog<T extends Promise<any>>(
export const promiseDialog = <T extends Promise<any>>(
promise: T,
onSuccess?: ((res: any) => void) | null,
onFailure?: ((err: Error) => void) | null,
text?: string,
): T {
): T => {
const showing = ref(true);
const success = ref(false);

Expand Down Expand Up @@ -80,12 +80,13 @@ const zIndexes = {
middle: 2000000,
high: 3000000,
};
export function claimZIndex(priority: keyof typeof zIndexes = 'low'): number {

export const claimZIndex = (priority: keyof typeof zIndexes = 'low'): number => {
zIndexes[priority] += 100;
return zIndexes[priority];
}

export async function popup(component: Component, props: Record<string, any>, events = {}, disposeEvent?: string) {
export const popup = async (component: Component, props: Record<string, any>, events = {}, disposeEvent?: string) => {
markRaw(component);

const id = ++popupIdCount;
Expand All @@ -112,29 +113,29 @@ export async function popup(component: Component, props: Record<string, any>, ev
};
}

export function pageWindow(path: string) {
export const pageWindow = (path: string) => {
popup(defineAsyncComponent(() => import('@/components/MkPageWindow.vue')), {
initialPath: path,
}, {}, 'closed');
}

export function modalPageWindow(path: string) {
export const modalPageWindow = (path: string) => {
popup(defineAsyncComponent(() => import('@/components/MkModalPageWindow.vue')), {
initialPath: path,
}, {}, 'closed');
}

export function toast(message: string) {
export const toast = (message: string) => {
popup(defineAsyncComponent(() => import('@/components/MkToast.vue')), {
message,
}, {}, 'closed');
}

export function alert(props: {
export const alert = (props: {
type?: 'error' | 'info' | 'success' | 'warning' | 'waiting' | 'question';
title?: string | null;
text?: string | null;
}): Promise<void> {
}): Promise<void> => {
return new Promise((resolve, reject) => {
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), props, {
done: result => {
Expand All @@ -144,11 +145,11 @@ export function alert(props: {
});
}

export function confirm(props: {
export const confirm = (props: {
type: 'error' | 'info' | 'success' | 'warning' | 'waiting' | 'question';
title?: string | null;
text?: string | null;
}): Promise<{ canceled: boolean }> {
}): Promise<{ canceled: boolean }> => {
return new Promise((resolve, reject) => {
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), {
...props,
Expand All @@ -161,15 +162,15 @@ export function confirm(props: {
});
}

export function inputText(props: {
export const inputText = (props: {
type?: 'text' | 'email' | 'password' | 'url';
title?: string | null;
text?: string | null;
placeholder?: string | null;
default?: string | null;
}): Promise<{ canceled: true; result: undefined; } | {
canceled: false; result: string;
}> {
}> => {
return new Promise((resolve, reject) => {
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), {
title: props.title,
Expand All @@ -187,14 +188,14 @@ export function inputText(props: {
});
}

export function inputNumber(props: {
export const inputNumber = (props: {
title?: string | null;
text?: string | null;
placeholder?: string | null;
default?: number | null;
}): Promise<{ canceled: true; result: undefined; } | {
canceled: false; result: number;
}> {
}> => {
return new Promise((resolve, reject) => {
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), {
title: props.title,
Expand All @@ -212,14 +213,14 @@ export function inputNumber(props: {
});
}

export function inputDate(props: {
export const inputDate = (props: {
title?: string | null;
text?: string | null;
placeholder?: string | null;
default?: Date | null;
}): Promise<{ canceled: true; result: undefined; } | {
canceled: false; result: Date;
}> {
}> => {
return new Promise((resolve, reject) => {
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), {
title: props.title,
Expand All @@ -237,7 +238,7 @@ export function inputDate(props: {
});
}

export function select<C = any>(props: {
export const select = <C = any>(props: {
title?: string | null;
text?: string | null;
default?: string | null;
Expand All @@ -256,7 +257,7 @@ export function select<C = any>(props: {
}[];
})): Promise<{ canceled: true; result: undefined; } | {
canceled: false; result: C;
}> {
}> => {
return new Promise((resolve, reject) => {
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), {
title: props.title,
Expand All @@ -274,7 +275,7 @@ export function select<C = any>(props: {
});
}

export function success() {
export const success = () => {
return new Promise((resolve, reject) => {
const showing = ref(true);
window.setTimeout(() => {
Expand All @@ -289,7 +290,7 @@ export function success() {
});
}

export function waiting() {
export const waiting = () => {
return new Promise((resolve, reject) => {
const showing = ref(true);
popup(defineAsyncComponent(() => import('@/components/MkWaitingDialog.vue')), {
Expand All @@ -301,7 +302,7 @@ export function waiting() {
});
}

export function form(title, form) {
export const form = (title, form) => {
return new Promise((resolve, reject) => {
popup(defineAsyncComponent(() => import('@/components/MkFormDialog.vue')), { title, form }, {
done: result => {
Expand All @@ -311,7 +312,7 @@ export function form(title, form) {
});
}

export async function selectUser() {
export const selectUser = () => {
return new Promise((resolve, reject) => {
popup(defineAsyncComponent(() => import('@/components/MkUserSelectDialog.vue')), {}, {
ok: user => {
Expand All @@ -321,7 +322,7 @@ export async function selectUser() {
});
}

export async function selectDriveFile(multiple: boolean) {
export const selectDriveFile = (multiple: boolean) => {
return new Promise((resolve, reject) => {
popup(defineAsyncComponent(() => import('@/components/MkDriveSelectDialog.vue')), {
type: 'file',
Expand All @@ -336,7 +337,7 @@ export async function selectDriveFile(multiple: boolean) {
});
}

export async function selectDriveFolder(multiple: boolean) {
export const selectDriveFolder = (multiple: boolean) => {
return new Promise((resolve, reject) => {
popup(defineAsyncComponent(() => import('@/components/MkDriveSelectDialog.vue')), {
type: 'folder',
Expand All @@ -351,7 +352,7 @@ export async function selectDriveFolder(multiple: boolean) {
});
}

export async function pickEmoji(src: HTMLElement | null, opts) {
export const pickEmoji = (src: HTMLElement | null, opts) => {
return new Promise((resolve, reject) => {
popup(defineAsyncComponent(() => import('@/components/MkEmojiPickerDialog.vue')), {
src,
Expand All @@ -364,9 +365,9 @@ export async function pickEmoji(src: HTMLElement | null, opts) {
});
}

export async function cropImage(image: Misskey.entities.DriveFile, options: {
export const cropImage = (image: Misskey.entities.DriveFile, options: {
aspectRatio: number;
}): Promise<Misskey.entities.DriveFile> {
}): Promise<Misskey.entities.DriveFile> => {
return new Promise((resolve, reject) => {
popup(defineAsyncComponent(() => import('@/components/MkCropperDialog.vue')), {
file: image,
Expand All @@ -385,7 +386,7 @@ type AwaitType<T> =
T;
let openingEmojiPicker: AwaitType<ReturnType<typeof popup>> | null = null;
let activeTextarea: HTMLTextAreaElement | HTMLInputElement | null = null;
export async function openEmojiPicker(src?: HTMLElement, opts, initialTextarea: typeof activeTextarea) {
export const openEmojiPicker = async (src?: HTMLElement, opts, initialTextarea: typeof activeTextarea) => {
if (openingEmojiPicker) return;

activeTextarea = initialTextarea;
Expand Down Expand Up @@ -433,12 +434,12 @@ export async function openEmojiPicker(src?: HTMLElement, opts, initialTextarea:
});
}

export function popupMenu(items: MenuItem[] | Ref<MenuItem[]>, src?: HTMLElement, options?: {
export const popupMenu = (items: MenuItem[] | Ref<MenuItem[]>, src?: HTMLElement, options?: {
align?: string;
width?: number;
viaKeyboard?: boolean;
onClosing?: () => void;
}) {
}) => {
return new Promise((resolve, reject) => {
let dispose;
popup(defineAsyncComponent(() => import('@/components/MkPopupMenu.vue')), {
Expand All @@ -461,7 +462,7 @@ export function popupMenu(items: MenuItem[] | Ref<MenuItem[]>, src?: HTMLElement
});
}

export function contextMenu(items: MenuItem[] | Ref<MenuItem[]>, ev: MouseEvent) {
export const contextMenu = (items: MenuItem[] | Ref<MenuItem[]>, ev: MouseEvent) => {
ev.preventDefault();
return new Promise((resolve, reject) => {
let dispose;
Expand All @@ -479,7 +480,7 @@ export function contextMenu(items: MenuItem[] | Ref<MenuItem[]>, ev: MouseEvent)
});
}

export function post(props: Record<string, any> = {}) {
export const post = (props: Record<string, any> = {}) => {
return new Promise((resolve, reject) => {
// NOTE: MkPostFormDialogをdynamic importするとiOSでテキストエリアに自動フォーカスできない
// NOTE: ただ、dynamic importしない場合、MkPostFormDialogインスタンスが使いまわされ、
Expand All @@ -501,7 +502,7 @@ export function post(props: Record<string, any> = {}) {
export const deckGlobalEvents = new EventEmitter();

/*
export function checkExistence(fileData: ArrayBuffer): Promise<any> {
export const checkExistence = (fileData: ArrayBuffer): Promise<any> => {
return new Promise((resolve, reject) => {
const data = new FormData();
data.append('md5', getMD5(fileData));
Expand Down

0 comments on commit 34f2feb

Please sign in to comment.