2021-02-27 13:08:34 +09:00
|
|
|
|
// TODO: なんでもかんでもos.tsに突っ込むのやめたいのでよしなに分割する
|
|
|
|
|
|
2022-05-02 00:55:17 +09:00
|
|
|
|
import { Component, markRaw, Ref, ref, defineAsyncComponent } from 'vue';
|
2020-10-17 20:12:00 +09:00
|
|
|
|
import { EventEmitter } from 'eventemitter3';
|
2021-02-27 13:08:34 +09:00
|
|
|
|
import insertTextAtCursor from 'insert-text-at-cursor';
|
2022-12-12 12:24:12 +09:00
|
|
|
|
import * as Misskey from 'calckey-js';
|
2022-01-07 15:05:45 +09:00
|
|
|
|
import { apiUrl, url } from '@/config';
|
2022-08-31 00:24:33 +09:00
|
|
|
|
import MkPostFormDialog from '@/components/MkPostFormDialog.vue';
|
|
|
|
|
import MkWaitingDialog from '@/components/MkWaitingDialog.vue';
|
2022-01-30 14:11:52 +09:00
|
|
|
|
import { MenuItem } from '@/types/menu';
|
2021-11-12 02:02:25 +09:00
|
|
|
|
import { $i } from '@/account';
|
2020-10-17 20:12:00 +09:00
|
|
|
|
|
|
|
|
|
export const pendingApiRequestsCount = ref(0);
|
|
|
|
|
|
2021-05-27 17:15:08 +09:00
|
|
|
|
const apiClient = new Misskey.api.APIClient({
|
|
|
|
|
origin: url,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const api = ((endpoint: string, data: Record<string, any> = {}, token?: string | null | undefined) => {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
pendingApiRequestsCount.value++;
|
|
|
|
|
|
|
|
|
|
const onFinally = () => {
|
|
|
|
|
pendingApiRequestsCount.value--;
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-19 07:49:40 +09:00
|
|
|
|
const authorizationToken = token ?? $i?.token ?? undefined;
|
|
|
|
|
const authorization = authorizationToken ? `Bearer ${authorizationToken}` : undefined;
|
2020-10-17 20:12:00 +09:00
|
|
|
|
|
2022-07-19 07:49:40 +09:00
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
fetch(endpoint.indexOf('://') > -1 ? endpoint : `${apiUrl}/${endpoint}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
credentials: 'omit',
|
2022-06-11 15:45:44 +09:00
|
|
|
|
cache: 'no-cache',
|
2022-07-21 01:40:23 +09:00
|
|
|
|
headers: authorization ? { authorization } : {},
|
2020-10-17 20:12:00 +09:00
|
|
|
|
}).then(async (res) => {
|
|
|
|
|
const body = res.status === 204 ? null : await res.json();
|
|
|
|
|
|
|
|
|
|
if (res.status === 200) {
|
|
|
|
|
resolve(body);
|
|
|
|
|
} else if (res.status === 204) {
|
|
|
|
|
resolve();
|
|
|
|
|
} else {
|
|
|
|
|
reject(body.error);
|
|
|
|
|
}
|
|
|
|
|
}).catch(reject);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
promise.then(onFinally, onFinally);
|
|
|
|
|
|
|
|
|
|
return promise;
|
2021-05-27 17:15:08 +09:00
|
|
|
|
}) as typeof apiClient.request;
|
2022-06-25 18:26:31 +09:00
|
|
|
|
|
2022-07-19 07:49:40 +09:00
|
|
|
|
export const apiGet = ((endpoint: string, data: Record<string, any> = {}, token?: string | null | undefined) => {
|
2022-06-25 18:26:31 +09:00
|
|
|
|
pendingApiRequestsCount.value++;
|
|
|
|
|
|
|
|
|
|
const onFinally = () => {
|
|
|
|
|
pendingApiRequestsCount.value--;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const query = new URLSearchParams(data);
|
|
|
|
|
|
2022-07-19 07:49:40 +09:00
|
|
|
|
const authorizationToken = token ?? $i?.token ?? undefined;
|
|
|
|
|
const authorization = authorizationToken ? `Bearer ${authorizationToken}` : undefined;
|
|
|
|
|
|
2022-06-25 18:26:31 +09:00
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
|
// Send request
|
|
|
|
|
fetch(`${apiUrl}/${endpoint}?${query}`, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
credentials: 'omit',
|
|
|
|
|
cache: 'default',
|
2022-07-21 01:40:23 +09:00
|
|
|
|
headers: authorization ? { authorization } : {},
|
2022-06-25 18:26:31 +09:00
|
|
|
|
}).then(async (res) => {
|
|
|
|
|
const body = res.status === 204 ? null : await res.json();
|
|
|
|
|
|
|
|
|
|
if (res.status === 200) {
|
|
|
|
|
resolve(body);
|
|
|
|
|
} else if (res.status === 204) {
|
|
|
|
|
resolve();
|
|
|
|
|
} else {
|
|
|
|
|
reject(body.error);
|
|
|
|
|
}
|
|
|
|
|
}).catch(reject);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
promise.then(onFinally, onFinally);
|
|
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
}) as typeof apiClient.request;
|
2020-10-17 20:12:00 +09:00
|
|
|
|
|
2021-05-27 17:15:08 +09:00
|
|
|
|
export const apiWithDialog = ((
|
2020-10-18 10:11:34 +09:00
|
|
|
|
endpoint: string,
|
|
|
|
|
data: Record<string, any> = {},
|
|
|
|
|
token?: string | null | undefined,
|
2021-05-27 17:15:08 +09:00
|
|
|
|
) => {
|
2020-10-18 10:11:34 +09:00
|
|
|
|
const promise = api(endpoint, data, token);
|
2022-05-26 22:53:09 +09:00
|
|
|
|
promiseDialog(promise, null, (err) => {
|
2021-11-18 23:36:04 +09:00
|
|
|
|
alert({
|
2020-10-18 10:21:02 +09:00
|
|
|
|
type: 'error',
|
2022-05-26 22:53:09 +09:00
|
|
|
|
text: err.message + '\n' + (err as any).id,
|
2020-10-18 10:21:02 +09:00
|
|
|
|
});
|
|
|
|
|
});
|
2020-10-18 10:11:34 +09:00
|
|
|
|
|
|
|
|
|
return promise;
|
2021-05-27 17:15:08 +09:00
|
|
|
|
}) as typeof api;
|
2020-10-18 10:11:34 +09:00
|
|
|
|
|
|
|
|
|
export function promiseDialog<T extends Promise<any>>(
|
|
|
|
|
promise: T,
|
2021-01-03 23:58:24 +09:00
|
|
|
|
onSuccess?: ((res: any) => void) | null,
|
2022-05-26 22:53:09 +09:00
|
|
|
|
onFailure?: ((err: Error) => void) | null,
|
2020-10-18 10:11:34 +09:00
|
|
|
|
text?: string,
|
|
|
|
|
): T {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
const showing = ref(true);
|
2020-10-18 10:11:34 +09:00
|
|
|
|
const success = ref(false);
|
2020-10-17 20:12:00 +09:00
|
|
|
|
|
|
|
|
|
promise.then(res => {
|
|
|
|
|
if (onSuccess) {
|
|
|
|
|
showing.value = false;
|
|
|
|
|
onSuccess(res);
|
|
|
|
|
} else {
|
2020-10-18 10:11:34 +09:00
|
|
|
|
success.value = true;
|
2022-01-16 10:14:14 +09:00
|
|
|
|
window.setTimeout(() => {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
showing.value = false;
|
|
|
|
|
}, 1000);
|
|
|
|
|
}
|
2022-05-26 22:53:09 +09:00
|
|
|
|
}).catch(err => {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
showing.value = false;
|
|
|
|
|
if (onFailure) {
|
2022-05-26 22:53:09 +09:00
|
|
|
|
onFailure(err);
|
2020-10-17 20:12:00 +09:00
|
|
|
|
} else {
|
2021-11-18 23:36:04 +09:00
|
|
|
|
alert({
|
2020-10-17 20:12:00 +09:00
|
|
|
|
type: 'error',
|
2022-05-26 22:53:09 +09:00
|
|
|
|
text: err,
|
2020-10-17 20:12:00 +09:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-19 19:29:04 +09:00
|
|
|
|
// NOTE: dynamic importすると挙動がおかしくなる(showingの変更が伝播しない)
|
|
|
|
|
popup(MkWaitingDialog, {
|
2020-10-18 10:11:34 +09:00
|
|
|
|
success: success,
|
|
|
|
|
showing: showing,
|
|
|
|
|
text: text,
|
2020-10-17 20:12:00 +09:00
|
|
|
|
}, {}, 'closed');
|
|
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 13:38:48 +09:00
|
|
|
|
let popupIdCount = 0;
|
2020-10-17 20:12:00 +09:00
|
|
|
|
export const popups = ref([]) as Ref<{
|
|
|
|
|
id: any;
|
|
|
|
|
component: any;
|
|
|
|
|
props: Record<string, any>;
|
|
|
|
|
}[]>;
|
|
|
|
|
|
2021-12-18 12:12:47 +09:00
|
|
|
|
const zIndexes = {
|
|
|
|
|
low: 1000000,
|
|
|
|
|
middle: 2000000,
|
|
|
|
|
high: 3000000,
|
|
|
|
|
};
|
2021-12-18 12:14:27 +09:00
|
|
|
|
export function claimZIndex(priority: 'low' | 'middle' | 'high' = 'low'): number {
|
2021-12-18 12:12:47 +09:00
|
|
|
|
zIndexes[priority] += 100;
|
|
|
|
|
return zIndexes[priority];
|
2021-12-10 18:20:41 +09:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 22:51:07 +09:00
|
|
|
|
export async function popup(component: Component, props: Record<string, any>, events = {}, disposeEvent?: string) {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
markRaw(component);
|
|
|
|
|
|
2020-11-01 13:38:48 +09:00
|
|
|
|
const id = ++popupIdCount;
|
2020-10-17 20:12:00 +09:00
|
|
|
|
const dispose = () => {
|
|
|
|
|
// このsetTimeoutが無いと挙動がおかしくなる(autocompleteが閉じなくなる)。Vueのバグ?
|
2022-01-16 10:14:14 +09:00
|
|
|
|
window.setTimeout(() => {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
popups.value = popups.value.filter(popup => popup.id !== id);
|
|
|
|
|
}, 0);
|
|
|
|
|
};
|
|
|
|
|
const state = {
|
|
|
|
|
component,
|
|
|
|
|
props,
|
|
|
|
|
events: disposeEvent ? {
|
|
|
|
|
...events,
|
2022-06-11 15:45:44 +09:00
|
|
|
|
[disposeEvent]: dispose,
|
2020-10-17 20:12:00 +09:00
|
|
|
|
} : events,
|
|
|
|
|
id,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
popups.value.push(state);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
dispose,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 10:06:19 +09:00
|
|
|
|
export function pageWindow(path: string) {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkPageWindow.vue')), {
|
2020-11-03 10:06:19 +09:00
|
|
|
|
initialPath: path,
|
2020-10-17 20:12:00 +09:00
|
|
|
|
}, {}, 'closed');
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 21:09:35 +09:00
|
|
|
|
export function modalPageWindow(path: string) {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkModalPageWindow.vue')), {
|
2021-04-11 21:09:35 +09:00
|
|
|
|
initialPath: path,
|
|
|
|
|
}, {}, 'closed');
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 20:07:37 +09:00
|
|
|
|
export function toast(message: string) {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkToast.vue')), {
|
2022-06-11 15:45:44 +09:00
|
|
|
|
message,
|
2021-12-18 14:55:53 +09:00
|
|
|
|
}, {}, 'closed');
|
2021-11-28 20:07:37 +09:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 18:45:58 +09:00
|
|
|
|
export function alert(props: {
|
|
|
|
|
type?: 'error' | 'info' | 'success' | 'warning' | 'waiting' | 'question';
|
2021-08-22 13:16:23 +09:00
|
|
|
|
title?: string | null;
|
|
|
|
|
text?: string | null;
|
2021-11-18 18:45:58 +09:00
|
|
|
|
}): Promise<void> {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), props, {
|
2021-11-18 18:45:58 +09:00
|
|
|
|
done: result => {
|
|
|
|
|
resolve();
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function confirm(props: {
|
|
|
|
|
type: 'error' | 'info' | 'success' | 'warning' | 'waiting' | 'question';
|
|
|
|
|
title?: string | null;
|
|
|
|
|
text?: string | null;
|
|
|
|
|
}): Promise<{ canceled: boolean }> {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), {
|
2021-11-18 18:45:58 +09:00
|
|
|
|
...props,
|
|
|
|
|
showCancelButton: true,
|
|
|
|
|
}, {
|
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result ? result : { canceled: true });
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 14:44:38 +09:00
|
|
|
|
export function yesno(props: {
|
|
|
|
|
type: 'error' | 'info' | 'success' | 'warning' | 'waiting' | 'question';
|
|
|
|
|
title?: string | null;
|
|
|
|
|
text?: string | null;
|
|
|
|
|
}): Promise<{ canceled: boolean }> {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), {
|
|
|
|
|
...props,
|
|
|
|
|
showCancelButton: true,
|
|
|
|
|
isYesNo: true,
|
|
|
|
|
}, {
|
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result ? result : { canceled: true });
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 18:45:58 +09:00
|
|
|
|
export function 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) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), {
|
2021-11-18 18:45:58 +09:00
|
|
|
|
title: props.title,
|
|
|
|
|
text: props.text,
|
|
|
|
|
input: {
|
|
|
|
|
type: props.type,
|
|
|
|
|
placeholder: props.placeholder,
|
|
|
|
|
default: props.default,
|
2022-06-11 15:45:44 +09:00
|
|
|
|
},
|
2021-11-18 18:45:58 +09:00
|
|
|
|
}, {
|
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result ? result : { canceled: true });
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function 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) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), {
|
2021-11-18 18:45:58 +09:00
|
|
|
|
title: props.title,
|
|
|
|
|
text: props.text,
|
|
|
|
|
input: {
|
|
|
|
|
type: 'number',
|
|
|
|
|
placeholder: props.placeholder,
|
|
|
|
|
default: props.default,
|
2022-06-11 15:45:44 +09:00
|
|
|
|
},
|
2021-11-18 18:45:58 +09:00
|
|
|
|
}, {
|
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result ? result : { canceled: true });
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function 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) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), {
|
2021-11-18 18:45:58 +09:00
|
|
|
|
title: props.title,
|
|
|
|
|
text: props.text,
|
|
|
|
|
input: {
|
|
|
|
|
type: 'date',
|
|
|
|
|
placeholder: props.placeholder,
|
|
|
|
|
default: props.default,
|
2022-06-11 15:45:44 +09:00
|
|
|
|
},
|
2021-11-18 18:45:58 +09:00
|
|
|
|
}, {
|
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result ? { result: new Date(result.result), canceled: false } : { canceled: true });
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 14:36:55 +09:00
|
|
|
|
export function select<C = any>(props: {
|
2021-11-18 18:45:58 +09:00
|
|
|
|
title?: string | null;
|
|
|
|
|
text?: string | null;
|
|
|
|
|
default?: string | null;
|
2022-03-21 03:11:14 +09:00
|
|
|
|
} & ({
|
|
|
|
|
items: {
|
|
|
|
|
value: C;
|
2021-11-18 18:45:58 +09:00
|
|
|
|
text: string;
|
|
|
|
|
}[];
|
2022-03-21 03:11:14 +09:00
|
|
|
|
} | {
|
|
|
|
|
groupedItems: {
|
2021-11-18 18:45:58 +09:00
|
|
|
|
label: string;
|
|
|
|
|
items: {
|
2022-03-21 03:11:14 +09:00
|
|
|
|
value: C;
|
2021-11-18 18:45:58 +09:00
|
|
|
|
text: string;
|
|
|
|
|
}[];
|
|
|
|
|
}[];
|
2022-03-21 03:11:14 +09:00
|
|
|
|
})): Promise<{ canceled: true; result: undefined; } | {
|
|
|
|
|
canceled: false; result: C;
|
2021-11-18 18:45:58 +09:00
|
|
|
|
}> {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkDialog.vue')), {
|
2021-11-18 18:45:58 +09:00
|
|
|
|
title: props.title,
|
|
|
|
|
text: props.text,
|
|
|
|
|
select: {
|
|
|
|
|
items: props.items,
|
|
|
|
|
groupedItems: props.groupedItems,
|
|
|
|
|
default: props.default,
|
2022-06-11 15:45:44 +09:00
|
|
|
|
},
|
2021-11-18 18:45:58 +09:00
|
|
|
|
}, {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result ? result : { canceled: true });
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function success() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const showing = ref(true);
|
2022-01-16 10:14:14 +09:00
|
|
|
|
window.setTimeout(() => {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
showing.value = false;
|
|
|
|
|
}, 1000);
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkWaitingDialog.vue')), {
|
2020-10-18 10:11:34 +09:00
|
|
|
|
success: true,
|
2022-06-11 15:45:44 +09:00
|
|
|
|
showing: showing,
|
2020-10-17 20:12:00 +09:00
|
|
|
|
}, {
|
|
|
|
|
done: () => resolve(),
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function waiting() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const showing = ref(true);
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkWaitingDialog.vue')), {
|
2020-10-18 10:11:34 +09:00
|
|
|
|
success: false,
|
2022-06-11 15:45:44 +09:00
|
|
|
|
showing: showing,
|
2020-10-17 20:12:00 +09:00
|
|
|
|
}, {
|
|
|
|
|
done: () => resolve(),
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function form(title, form) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkFormDialog.vue')), { title, form }, {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result);
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function selectUser() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkUserSelectDialog.vue')), {}, {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
ok: user => {
|
|
|
|
|
resolve(user);
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function selectDriveFile(multiple: boolean) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkDriveSelectDialog.vue')), {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
type: 'file',
|
2022-06-11 15:45:44 +09:00
|
|
|
|
multiple,
|
2020-10-17 20:12:00 +09:00
|
|
|
|
}, {
|
|
|
|
|
done: files => {
|
|
|
|
|
if (files) {
|
|
|
|
|
resolve(multiple ? files : files[0]);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function selectDriveFolder(multiple: boolean) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkDriveSelectDialog.vue')), {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
type: 'folder',
|
2022-06-11 15:45:44 +09:00
|
|
|
|
multiple,
|
2020-10-17 20:12:00 +09:00
|
|
|
|
}, {
|
|
|
|
|
done: folders => {
|
|
|
|
|
if (folders) {
|
|
|
|
|
resolve(multiple ? folders : folders[0]);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-28 12:20:42 +09:00
|
|
|
|
export async function pickEmoji(src: HTMLElement | null, opts) {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkEmojiPickerDialog.vue')), {
|
2020-11-14 14:32:01 +09:00
|
|
|
|
src,
|
2022-06-11 15:45:44 +09:00
|
|
|
|
...opts,
|
2020-10-17 20:12:00 +09:00
|
|
|
|
}, {
|
|
|
|
|
done: emoji => {
|
|
|
|
|
resolve(emoji);
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-11 15:45:44 +09:00
|
|
|
|
export async function cropImage(image: Misskey.entities.DriveFile, options: {
|
|
|
|
|
aspectRatio: number;
|
|
|
|
|
}): Promise<Misskey.entities.DriveFile> {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkCropperDialog.vue')), {
|
2022-06-11 15:45:44 +09:00
|
|
|
|
file: image,
|
|
|
|
|
aspectRatio: options.aspectRatio,
|
|
|
|
|
}, {
|
|
|
|
|
ok: x => {
|
|
|
|
|
resolve(x);
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-27 13:08:34 +09:00
|
|
|
|
type AwaitType<T> =
|
|
|
|
|
T extends Promise<infer U> ? U :
|
2021-02-28 19:09:08 +09:00
|
|
|
|
T extends (...args: any[]) => Promise<infer V> ? V :
|
2021-02-27 13:08:34 +09:00
|
|
|
|
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) {
|
|
|
|
|
if (openingEmojiPicker) return;
|
|
|
|
|
|
|
|
|
|
activeTextarea = initialTextarea;
|
|
|
|
|
|
|
|
|
|
const textareas = document.querySelectorAll('textarea, input');
|
|
|
|
|
for (const textarea of Array.from(textareas)) {
|
|
|
|
|
textarea.addEventListener('focus', () => {
|
|
|
|
|
activeTextarea = textarea;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const observer = new MutationObserver(records => {
|
|
|
|
|
for (const record of records) {
|
2021-02-27 13:16:59 +09:00
|
|
|
|
for (const node of Array.from(record.addedNodes).filter(node => node instanceof HTMLElement) as HTMLElement[]) {
|
|
|
|
|
const textareas = node.querySelectorAll('textarea, input') as NodeListOf<NonNullable<typeof activeTextarea>>;
|
|
|
|
|
for (const textarea of Array.from(textareas).filter(textarea => textarea.dataset.preventEmojiInsert == null)) {
|
|
|
|
|
if (document.activeElement === textarea) activeTextarea = textarea;
|
|
|
|
|
textarea.addEventListener('focus', () => {
|
|
|
|
|
activeTextarea = textarea;
|
|
|
|
|
});
|
2021-02-27 13:08:34 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
observer.observe(document.body, {
|
|
|
|
|
childList: true,
|
|
|
|
|
subtree: true,
|
|
|
|
|
attributes: false,
|
|
|
|
|
characterData: false,
|
|
|
|
|
});
|
|
|
|
|
|
2022-11-07 13:08:28 +09:00
|
|
|
|
openingEmojiPicker = await popup(defineAsyncComponent(() => import('@/components/MkEmojiPickerDialog.vue')), {
|
2021-02-27 13:08:34 +09:00
|
|
|
|
src,
|
2022-06-11 15:45:44 +09:00
|
|
|
|
...opts,
|
2021-02-27 13:08:34 +09:00
|
|
|
|
}, {
|
|
|
|
|
chosen: emoji => {
|
|
|
|
|
insertTextAtCursor(activeTextarea, emoji);
|
|
|
|
|
},
|
2022-11-07 13:17:59 +09:00
|
|
|
|
done: emoji => {
|
|
|
|
|
insertTextAtCursor(activeTextarea, emoji);
|
|
|
|
|
},
|
2021-02-27 13:08:34 +09:00
|
|
|
|
closed: () => {
|
|
|
|
|
openingEmojiPicker!.dispose();
|
|
|
|
|
openingEmojiPicker = null;
|
|
|
|
|
observer.disconnect();
|
2022-06-11 15:45:44 +09:00
|
|
|
|
},
|
2021-02-27 13:08:34 +09:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-30 14:11:52 +09:00
|
|
|
|
export function popupMenu(items: MenuItem[] | Ref<MenuItem[]>, src?: HTMLElement, options?: {
|
2021-10-22 06:23:23 +09:00
|
|
|
|
align?: string;
|
|
|
|
|
width?: number;
|
|
|
|
|
viaKeyboard?: boolean;
|
|
|
|
|
}) {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-11-03 15:22:55 +09:00
|
|
|
|
let dispose;
|
2022-09-06 18:21:49 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkPopupMenu.vue')), {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
items,
|
|
|
|
|
src,
|
2021-10-22 06:23:23 +09:00
|
|
|
|
width: options?.width,
|
2020-10-17 20:12:00 +09:00
|
|
|
|
align: options?.align,
|
2022-06-11 15:45:44 +09:00
|
|
|
|
viaKeyboard: options?.viaKeyboard,
|
2020-10-17 20:12:00 +09:00
|
|
|
|
}, {
|
|
|
|
|
closed: () => {
|
|
|
|
|
resolve();
|
|
|
|
|
dispose();
|
|
|
|
|
},
|
2020-11-03 16:21:28 +09:00
|
|
|
|
}).then(res => {
|
|
|
|
|
dispose = res.dispose;
|
2020-10-17 20:12:00 +09:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-30 14:11:52 +09:00
|
|
|
|
export function contextMenu(items: MenuItem[] | Ref<MenuItem[]>, ev: MouseEvent) {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
ev.preventDefault();
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-11-03 15:22:55 +09:00
|
|
|
|
let dispose;
|
2022-09-06 18:21:49 +09:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkContextMenu.vue')), {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
items,
|
|
|
|
|
ev,
|
|
|
|
|
}, {
|
|
|
|
|
closed: () => {
|
|
|
|
|
resolve();
|
|
|
|
|
dispose();
|
|
|
|
|
},
|
2020-11-03 16:21:28 +09:00
|
|
|
|
}).then(res => {
|
|
|
|
|
dispose = res.dispose;
|
2020-10-17 20:12:00 +09:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 22:09:40 +09:00
|
|
|
|
export function post(props: Record<string, any> = {}) {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
// NOTE: MkPostFormDialogをdynamic importするとiOSでテキストエリアに自動フォーカスできない
|
2020-10-19 14:46:32 +09:00
|
|
|
|
// NOTE: ただ、dynamic importしない場合、MkPostFormDialogインスタンスが使いまわされ、
|
|
|
|
|
// Vueが渡されたコンポーネントに内部的に__propsというプロパティを生やす影響で、
|
|
|
|
|
// 複数のpost formを開いたときに場合によってはエラーになる
|
|
|
|
|
// もちろん複数のpost formを開けること自体Misskeyサイドのバグなのだが
|
2020-11-03 15:22:55 +09:00
|
|
|
|
let dispose;
|
|
|
|
|
popup(MkPostFormDialog, props, {
|
2020-10-17 20:12:00 +09:00
|
|
|
|
closed: () => {
|
|
|
|
|
resolve();
|
|
|
|
|
dispose();
|
|
|
|
|
},
|
2020-11-03 16:21:28 +09:00
|
|
|
|
}).then(res => {
|
|
|
|
|
dispose = res.dispose;
|
2020-10-17 20:12:00 +09:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const deckGlobalEvents = new EventEmitter();
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
export function checkExistence(fileData: ArrayBuffer): Promise<any> {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const data = new FormData();
|
|
|
|
|
data.append('md5', getMD5(fileData));
|
|
|
|
|
|
|
|
|
|
os.api('drive/files/find-by-hash', {
|
|
|
|
|
md5: getMD5(fileData)
|
|
|
|
|
}).then(resp => {
|
|
|
|
|
resolve(resp.length > 0 ? resp[0] : null);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}*/
|