2021-02-26 22:08:34 -06:00
|
|
|
|
// TODO: なんでもかんでもos.tsに突っ込むのやめたいのでよしなに分割する
|
|
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
|
import { Component, defineAsyncComponent, markRaw, reactive, Ref, ref } from 'vue';
|
|
|
|
|
import { EventEmitter } from 'eventemitter3';
|
2021-02-26 22:08:34 -06:00
|
|
|
|
import insertTextAtCursor from 'insert-text-at-cursor';
|
2021-05-27 03:15:08 -05:00
|
|
|
|
import * as Misskey from 'misskey-js';
|
2021-01-08 06:43:56 -06:00
|
|
|
|
import * as Sentry from '@sentry/browser';
|
2021-11-11 11:02:25 -06:00
|
|
|
|
import { apiUrl, debug, url } from '@/config';
|
|
|
|
|
import MkPostFormDialog from '@/components/post-form-dialog.vue';
|
|
|
|
|
import MkWaitingDialog from '@/components/waiting-dialog.vue';
|
|
|
|
|
import { resolve } from '@/router';
|
|
|
|
|
import { $i } from '@/account';
|
|
|
|
|
import { defaultStore } from '@/store';
|
2020-10-17 06:12:00 -05:00
|
|
|
|
|
2021-11-27 03:29:23 -06:00
|
|
|
|
export let isScreenTouching = false;
|
|
|
|
|
|
|
|
|
|
window.addEventListener('touchstart', () => {
|
|
|
|
|
isScreenTouching = true;
|
|
|
|
|
}, { passive: true });
|
|
|
|
|
|
|
|
|
|
window.addEventListener('touchend', () => {
|
|
|
|
|
isScreenTouching = false;
|
|
|
|
|
}, { passive: true });
|
|
|
|
|
|
2021-05-27 03:15:08 -05:00
|
|
|
|
export const stream = markRaw(new Misskey.Stream(url, $i));
|
2020-10-17 06:12:00 -05:00
|
|
|
|
|
|
|
|
|
export const pendingApiRequestsCount = ref(0);
|
2020-11-01 07:43:19 -06:00
|
|
|
|
let apiRequestsCount = 0; // for debug
|
2020-11-01 07:09:16 -06:00
|
|
|
|
export const apiRequests = ref([]); // for debug
|
2020-10-17 06:12:00 -05:00
|
|
|
|
|
|
|
|
|
export const windows = new Map();
|
|
|
|
|
|
2021-05-27 03:15:08 -05: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 06:12:00 -05:00
|
|
|
|
pendingApiRequestsCount.value++;
|
|
|
|
|
|
|
|
|
|
const onFinally = () => {
|
|
|
|
|
pendingApiRequestsCount.value--;
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-01 07:09:16 -06:00
|
|
|
|
const log = debug ? reactive({
|
2020-11-01 07:43:19 -06:00
|
|
|
|
id: ++apiRequestsCount,
|
2020-11-01 07:09:16 -06:00
|
|
|
|
endpoint,
|
2020-11-01 07:43:19 -06:00
|
|
|
|
req: markRaw(data),
|
|
|
|
|
res: null,
|
|
|
|
|
state: 'pending',
|
2020-11-01 07:09:16 -06:00
|
|
|
|
}) : null;
|
|
|
|
|
if (debug) {
|
|
|
|
|
apiRequests.value.push(log);
|
2020-11-01 07:43:19 -06:00
|
|
|
|
if (apiRequests.value.length > 128) apiRequests.value.shift();
|
2020-11-01 07:09:16 -06:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
|
// Append a credential
|
2020-12-18 19:55:52 -06:00
|
|
|
|
if ($i) (data as any).i = $i.token;
|
2020-10-17 06:12:00 -05:00
|
|
|
|
if (token !== undefined) (data as any).i = token;
|
|
|
|
|
|
|
|
|
|
// Send request
|
|
|
|
|
fetch(endpoint.indexOf('://') > -1 ? endpoint : `${apiUrl}/${endpoint}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
credentials: 'omit',
|
|
|
|
|
cache: 'no-cache'
|
|
|
|
|
}).then(async (res) => {
|
|
|
|
|
const body = res.status === 204 ? null : await res.json();
|
|
|
|
|
|
|
|
|
|
if (res.status === 200) {
|
|
|
|
|
resolve(body);
|
2020-11-01 07:09:16 -06:00
|
|
|
|
if (debug) {
|
2021-05-30 21:05:37 -05:00
|
|
|
|
log!.res = markRaw(JSON.parse(JSON.stringify(body)));
|
2021-01-08 06:43:56 -06:00
|
|
|
|
log!.state = 'success';
|
2020-11-01 07:09:16 -06:00
|
|
|
|
}
|
2020-10-17 06:12:00 -05:00
|
|
|
|
} else if (res.status === 204) {
|
|
|
|
|
resolve();
|
2020-11-01 07:09:16 -06:00
|
|
|
|
if (debug) {
|
2021-01-08 06:43:56 -06:00
|
|
|
|
log!.state = 'success';
|
2020-11-01 07:09:16 -06:00
|
|
|
|
}
|
2020-10-17 06:12:00 -05:00
|
|
|
|
} else {
|
|
|
|
|
reject(body.error);
|
2020-11-01 07:09:16 -06:00
|
|
|
|
if (debug) {
|
2021-01-08 06:43:56 -06:00
|
|
|
|
log!.res = markRaw(body.error);
|
|
|
|
|
log!.state = 'failed';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (defaultStore.state.reportError && !_DEV_) {
|
|
|
|
|
Sentry.withScope((scope) => {
|
|
|
|
|
scope.setTag('api_endpoint', endpoint);
|
|
|
|
|
scope.setContext('api params', data);
|
|
|
|
|
scope.setContext('api error info', body.info);
|
|
|
|
|
scope.setTag('api_error_id', body.id);
|
|
|
|
|
scope.setTag('api_error_code', body.code);
|
|
|
|
|
scope.setTag('api_error_kind', body.kind);
|
|
|
|
|
scope.setLevel(Sentry.Severity.Error);
|
|
|
|
|
Sentry.captureMessage('API error');
|
|
|
|
|
});
|
2020-11-01 07:09:16 -06:00
|
|
|
|
}
|
2020-10-17 06:12:00 -05:00
|
|
|
|
}
|
|
|
|
|
}).catch(reject);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
promise.then(onFinally, onFinally);
|
|
|
|
|
|
|
|
|
|
return promise;
|
2021-05-27 03:15:08 -05:00
|
|
|
|
}) as typeof apiClient.request;
|
2020-10-17 06:12:00 -05:00
|
|
|
|
|
2021-05-27 03:15:08 -05:00
|
|
|
|
export const apiWithDialog = ((
|
2020-10-17 20:11:34 -05:00
|
|
|
|
endpoint: string,
|
|
|
|
|
data: Record<string, any> = {},
|
|
|
|
|
token?: string | null | undefined,
|
2021-05-27 03:15:08 -05:00
|
|
|
|
) => {
|
2020-10-17 20:11:34 -05:00
|
|
|
|
const promise = api(endpoint, data, token);
|
2021-05-27 03:15:08 -05:00
|
|
|
|
promiseDialog(promise, null, (e) => {
|
2021-11-18 08:36:04 -06:00
|
|
|
|
alert({
|
2020-10-17 20:21:02 -05:00
|
|
|
|
type: 'error',
|
2020-10-28 08:47:57 -05:00
|
|
|
|
text: e.message + '\n' + (e as any).id,
|
2020-10-17 20:21:02 -05:00
|
|
|
|
});
|
|
|
|
|
});
|
2020-10-17 20:11:34 -05:00
|
|
|
|
|
|
|
|
|
return promise;
|
2021-05-27 03:15:08 -05:00
|
|
|
|
}) as typeof api;
|
2020-10-17 20:11:34 -05:00
|
|
|
|
|
|
|
|
|
export function promiseDialog<T extends Promise<any>>(
|
|
|
|
|
promise: T,
|
2021-01-03 08:58:24 -06:00
|
|
|
|
onSuccess?: ((res: any) => void) | null,
|
|
|
|
|
onFailure?: ((e: Error) => void) | null,
|
2020-10-17 20:11:34 -05:00
|
|
|
|
text?: string,
|
|
|
|
|
): T {
|
2020-10-17 06:12:00 -05:00
|
|
|
|
const showing = ref(true);
|
2020-10-17 20:11:34 -05:00
|
|
|
|
const success = ref(false);
|
2020-10-17 06:12:00 -05:00
|
|
|
|
|
|
|
|
|
promise.then(res => {
|
|
|
|
|
if (onSuccess) {
|
|
|
|
|
showing.value = false;
|
|
|
|
|
onSuccess(res);
|
|
|
|
|
} else {
|
2020-10-17 20:11:34 -05:00
|
|
|
|
success.value = true;
|
2020-10-17 06:12:00 -05:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
showing.value = false;
|
|
|
|
|
}, 1000);
|
|
|
|
|
}
|
|
|
|
|
}).catch(e => {
|
|
|
|
|
showing.value = false;
|
|
|
|
|
if (onFailure) {
|
|
|
|
|
onFailure(e);
|
|
|
|
|
} else {
|
2021-11-18 08:36:04 -06:00
|
|
|
|
alert({
|
2020-10-17 06:12:00 -05:00
|
|
|
|
type: 'error',
|
|
|
|
|
text: e
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-19 05:29:04 -05:00
|
|
|
|
// NOTE: dynamic importすると挙動がおかしくなる(showingの変更が伝播しない)
|
|
|
|
|
popup(MkWaitingDialog, {
|
2020-10-17 20:11:34 -05:00
|
|
|
|
success: success,
|
|
|
|
|
showing: showing,
|
|
|
|
|
text: text,
|
2020-10-17 06:12:00 -05:00
|
|
|
|
}, {}, 'closed');
|
|
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isModule(x: any): x is typeof import('*.vue') {
|
|
|
|
|
return x.default != null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-31 23:38:48 -05:00
|
|
|
|
let popupIdCount = 0;
|
2020-10-17 06:12:00 -05:00
|
|
|
|
export const popups = ref([]) as Ref<{
|
|
|
|
|
id: any;
|
|
|
|
|
component: any;
|
|
|
|
|
props: Record<string, any>;
|
|
|
|
|
}[]>;
|
|
|
|
|
|
2020-11-03 00:22:55 -06:00
|
|
|
|
export async function popup(component: Component | typeof import('*.vue') | Promise<Component | typeof import('*.vue')>, props: Record<string, any>, events = {}, disposeEvent?: string) {
|
|
|
|
|
if (component.then) component = await component;
|
|
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
|
if (isModule(component)) component = component.default;
|
|
|
|
|
markRaw(component);
|
|
|
|
|
|
2020-10-31 23:38:48 -05:00
|
|
|
|
const id = ++popupIdCount;
|
2020-10-17 06:12:00 -05:00
|
|
|
|
const dispose = () => {
|
|
|
|
|
if (_DEV_) console.log('os:popup close', id, component, props, events);
|
|
|
|
|
// このsetTimeoutが無いと挙動がおかしくなる(autocompleteが閉じなくなる)。Vueのバグ?
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
popups.value = popups.value.filter(popup => popup.id !== id);
|
|
|
|
|
}, 0);
|
|
|
|
|
};
|
|
|
|
|
const state = {
|
|
|
|
|
component,
|
|
|
|
|
props,
|
|
|
|
|
events: disposeEvent ? {
|
|
|
|
|
...events,
|
|
|
|
|
[disposeEvent]: dispose
|
|
|
|
|
} : events,
|
|
|
|
|
id,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (_DEV_) console.log('os:popup open', id, component, props, events);
|
|
|
|
|
popups.value.push(state);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
dispose,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-02 19:06:19 -06:00
|
|
|
|
export function pageWindow(path: string) {
|
|
|
|
|
const { component, props } = resolve(path);
|
2021-11-11 11:02:25 -06:00
|
|
|
|
popup(import('@/components/page-window.vue'), {
|
2020-11-02 19:06:19 -06:00
|
|
|
|
initialPath: path,
|
2020-10-17 06:12:00 -05:00
|
|
|
|
initialComponent: markRaw(component),
|
|
|
|
|
initialProps: props,
|
|
|
|
|
}, {}, 'closed');
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 07:09:35 -05:00
|
|
|
|
export function modalPageWindow(path: string) {
|
|
|
|
|
const { component, props } = resolve(path);
|
2021-11-11 11:02:25 -06:00
|
|
|
|
popup(import('@/components/modal-page-window.vue'), {
|
2021-04-11 07:09:35 -05:00
|
|
|
|
initialPath: path,
|
|
|
|
|
initialComponent: markRaw(component),
|
|
|
|
|
initialProps: props,
|
|
|
|
|
}, {}, 'closed');
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 03:45:58 -06:00
|
|
|
|
export function alert(props: {
|
|
|
|
|
type?: 'error' | 'info' | 'success' | 'warning' | 'waiting' | 'question';
|
2021-08-21 23:16:23 -05:00
|
|
|
|
title?: string | null;
|
|
|
|
|
text?: string | null;
|
2021-11-18 03:45:58 -06:00
|
|
|
|
}): Promise<void> {
|
2020-10-17 06:12:00 -05:00
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-11-11 11:02:25 -06:00
|
|
|
|
popup(import('@/components/dialog.vue'), props, {
|
2021-11-18 03:45:58 -06: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) => {
|
|
|
|
|
popup(import('@/components/dialog.vue'), {
|
|
|
|
|
...props,
|
|
|
|
|
showCancelButton: true,
|
|
|
|
|
}, {
|
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result ? result : { canceled: true });
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
|
popup(import('@/components/dialog.vue'), {
|
|
|
|
|
title: props.title,
|
|
|
|
|
text: props.text,
|
|
|
|
|
input: {
|
|
|
|
|
type: props.type,
|
|
|
|
|
placeholder: props.placeholder,
|
|
|
|
|
default: props.default,
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
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) => {
|
|
|
|
|
popup(import('@/components/dialog.vue'), {
|
|
|
|
|
title: props.title,
|
|
|
|
|
text: props.text,
|
|
|
|
|
input: {
|
|
|
|
|
type: 'number',
|
|
|
|
|
placeholder: props.placeholder,
|
|
|
|
|
default: props.default,
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
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) => {
|
|
|
|
|
popup(import('@/components/dialog.vue'), {
|
|
|
|
|
title: props.title,
|
|
|
|
|
text: props.text,
|
|
|
|
|
input: {
|
|
|
|
|
type: 'date',
|
|
|
|
|
placeholder: props.placeholder,
|
|
|
|
|
default: props.default,
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result ? { result: new Date(result.result), canceled: false } : { canceled: true });
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function select(props: {
|
|
|
|
|
title?: string | null;
|
|
|
|
|
text?: string | null;
|
|
|
|
|
default?: string | null;
|
|
|
|
|
items?: {
|
|
|
|
|
value: string;
|
|
|
|
|
text: string;
|
|
|
|
|
}[];
|
|
|
|
|
groupedItems?: {
|
|
|
|
|
label: string;
|
|
|
|
|
items: {
|
|
|
|
|
value: string;
|
|
|
|
|
text: string;
|
|
|
|
|
}[];
|
|
|
|
|
}[];
|
|
|
|
|
}): Promise<{ canceled: true; result: undefined; } | {
|
|
|
|
|
canceled: false; result: string;
|
|
|
|
|
}> {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
popup(import('@/components/dialog.vue'), {
|
|
|
|
|
title: props.title,
|
|
|
|
|
text: props.text,
|
|
|
|
|
select: {
|
|
|
|
|
items: props.items,
|
|
|
|
|
groupedItems: props.groupedItems,
|
|
|
|
|
default: props.default,
|
|
|
|
|
}
|
|
|
|
|
}, {
|
2020-10-17 06:12:00 -05:00
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result ? result : { canceled: true });
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function success() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const showing = ref(true);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
showing.value = false;
|
|
|
|
|
}, 1000);
|
2021-11-11 11:02:25 -06:00
|
|
|
|
popup(import('@/components/waiting-dialog.vue'), {
|
2020-10-17 20:11:34 -05:00
|
|
|
|
success: true,
|
2020-10-17 06:12:00 -05:00
|
|
|
|
showing: showing
|
|
|
|
|
}, {
|
|
|
|
|
done: () => resolve(),
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function waiting() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const showing = ref(true);
|
2021-11-11 11:02:25 -06:00
|
|
|
|
popup(import('@/components/waiting-dialog.vue'), {
|
2020-10-17 20:11:34 -05:00
|
|
|
|
success: false,
|
2020-10-17 06:12:00 -05:00
|
|
|
|
showing: showing
|
|
|
|
|
}, {
|
|
|
|
|
done: () => resolve(),
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function form(title, form) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-11-11 11:02:25 -06:00
|
|
|
|
popup(import('@/components/form-dialog.vue'), { title, form }, {
|
2020-10-17 06:12:00 -05:00
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result);
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function selectUser() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-11-11 11:02:25 -06:00
|
|
|
|
popup(import('@/components/user-select-dialog.vue'), {}, {
|
2020-10-17 06:12:00 -05:00
|
|
|
|
ok: user => {
|
|
|
|
|
resolve(user);
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function selectDriveFile(multiple: boolean) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-11-11 11:02:25 -06:00
|
|
|
|
popup(import('@/components/drive-select-dialog.vue'), {
|
2020-10-17 06:12:00 -05:00
|
|
|
|
type: 'file',
|
|
|
|
|
multiple
|
|
|
|
|
}, {
|
|
|
|
|
done: files => {
|
|
|
|
|
if (files) {
|
|
|
|
|
resolve(multiple ? files : files[0]);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function selectDriveFolder(multiple: boolean) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-11-11 11:02:25 -06:00
|
|
|
|
popup(import('@/components/drive-select-dialog.vue'), {
|
2020-10-17 06:12:00 -05:00
|
|
|
|
type: 'folder',
|
|
|
|
|
multiple
|
|
|
|
|
}, {
|
|
|
|
|
done: folders => {
|
|
|
|
|
if (folders) {
|
|
|
|
|
resolve(multiple ? folders : folders[0]);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 23:32:01 -06:00
|
|
|
|
export async function pickEmoji(src?: HTMLElement, opts) {
|
2020-10-17 06:12:00 -05:00
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-11-11 11:02:25 -06:00
|
|
|
|
popup(import('@/components/emoji-picker-dialog.vue'), {
|
2020-11-13 23:32:01 -06:00
|
|
|
|
src,
|
|
|
|
|
...opts
|
2020-10-17 06:12:00 -05:00
|
|
|
|
}, {
|
|
|
|
|
done: emoji => {
|
|
|
|
|
resolve(emoji);
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 22:08:34 -06:00
|
|
|
|
type AwaitType<T> =
|
|
|
|
|
T extends Promise<infer U> ? U :
|
2021-02-28 04:09:08 -06:00
|
|
|
|
T extends (...args: any[]) => Promise<infer V> ? V :
|
2021-02-26 22:08:34 -06: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-26 22:16:59 -06: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-26 22:08:34 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
observer.observe(document.body, {
|
|
|
|
|
childList: true,
|
|
|
|
|
subtree: true,
|
|
|
|
|
attributes: false,
|
|
|
|
|
characterData: false,
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-11 11:02:25 -06:00
|
|
|
|
openingEmojiPicker = await popup(import('@/components/emoji-picker-window.vue'), {
|
2021-02-26 22:08:34 -06:00
|
|
|
|
src,
|
|
|
|
|
...opts
|
|
|
|
|
}, {
|
|
|
|
|
chosen: emoji => {
|
|
|
|
|
insertTextAtCursor(activeTextarea, emoji);
|
|
|
|
|
},
|
|
|
|
|
closed: () => {
|
|
|
|
|
openingEmojiPicker!.dispose();
|
|
|
|
|
openingEmojiPicker = null;
|
|
|
|
|
observer.disconnect();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 16:23:23 -05:00
|
|
|
|
export function popupMenu(items: any[] | Ref<any[]>, src?: HTMLElement, options?: {
|
|
|
|
|
align?: string;
|
|
|
|
|
width?: number;
|
|
|
|
|
viaKeyboard?: boolean;
|
|
|
|
|
}) {
|
2020-10-17 06:12:00 -05:00
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-11-03 00:22:55 -06:00
|
|
|
|
let dispose;
|
2021-11-11 11:02:25 -06:00
|
|
|
|
popup(import('@/components/ui/popup-menu.vue'), {
|
2020-10-17 06:12:00 -05:00
|
|
|
|
items,
|
|
|
|
|
src,
|
2021-10-21 16:23:23 -05:00
|
|
|
|
width: options?.width,
|
2020-10-17 06:12:00 -05:00
|
|
|
|
align: options?.align,
|
|
|
|
|
viaKeyboard: options?.viaKeyboard
|
|
|
|
|
}, {
|
|
|
|
|
closed: () => {
|
|
|
|
|
resolve();
|
|
|
|
|
dispose();
|
|
|
|
|
},
|
2020-11-03 01:21:28 -06:00
|
|
|
|
}).then(res => {
|
|
|
|
|
dispose = res.dispose;
|
2020-10-17 06:12:00 -05:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function contextMenu(items: any[], ev: MouseEvent) {
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-11-03 00:22:55 -06:00
|
|
|
|
let dispose;
|
2021-11-11 11:02:25 -06:00
|
|
|
|
popup(import('@/components/ui/context-menu.vue'), {
|
2020-10-17 06:12:00 -05:00
|
|
|
|
items,
|
|
|
|
|
ev,
|
|
|
|
|
}, {
|
|
|
|
|
closed: () => {
|
|
|
|
|
resolve();
|
|
|
|
|
dispose();
|
|
|
|
|
},
|
2020-11-03 01:21:28 -06:00
|
|
|
|
}).then(res => {
|
|
|
|
|
dispose = res.dispose;
|
2020-10-17 06:12:00 -05:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function post(props: Record<string, any>) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
// NOTE: MkPostFormDialogをdynamic importするとiOSでテキストエリアに自動フォーカスできない
|
2020-10-19 00:46:32 -05:00
|
|
|
|
// NOTE: ただ、dynamic importしない場合、MkPostFormDialogインスタンスが使いまわされ、
|
|
|
|
|
// Vueが渡されたコンポーネントに内部的に__propsというプロパティを生やす影響で、
|
|
|
|
|
// 複数のpost formを開いたときに場合によってはエラーになる
|
|
|
|
|
// もちろん複数のpost formを開けること自体Misskeyサイドのバグなのだが
|
2020-11-03 00:22:55 -06:00
|
|
|
|
let dispose;
|
|
|
|
|
popup(MkPostFormDialog, props, {
|
2020-10-17 06:12:00 -05:00
|
|
|
|
closed: () => {
|
|
|
|
|
resolve();
|
|
|
|
|
dispose();
|
|
|
|
|
},
|
2020-11-03 01:21:28 -06:00
|
|
|
|
}).then(res => {
|
|
|
|
|
dispose = res.dispose;
|
2020-10-17 06:12:00 -05:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const deckGlobalEvents = new EventEmitter();
|
|
|
|
|
|
2021-11-19 03:57:34 -06:00
|
|
|
|
export const uploads = ref<{
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
progressMax: number | undefined;
|
|
|
|
|
progressValue: number | undefined;
|
|
|
|
|
img: string;
|
|
|
|
|
}[]>([]);
|
2020-10-17 06:12:00 -05:00
|
|
|
|
|
|
|
|
|
export function upload(file: File, folder?: any, name?: string) {
|
|
|
|
|
if (folder && typeof folder == 'object') folder = folder.id;
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-11-19 03:57:34 -06:00
|
|
|
|
const id = Math.random().toString();
|
2020-10-17 06:12:00 -05:00
|
|
|
|
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onload = (e) => {
|
|
|
|
|
const ctx = reactive({
|
|
|
|
|
id: id,
|
|
|
|
|
name: name || file.name || 'untitled',
|
|
|
|
|
progressMax: undefined,
|
|
|
|
|
progressValue: undefined,
|
|
|
|
|
img: window.URL.createObjectURL(file)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
uploads.value.push(ctx);
|
|
|
|
|
|
|
|
|
|
const data = new FormData();
|
2020-12-18 19:55:52 -06:00
|
|
|
|
data.append('i', $i.token);
|
2020-10-17 06:12:00 -05:00
|
|
|
|
data.append('force', 'true');
|
|
|
|
|
data.append('file', file);
|
|
|
|
|
|
|
|
|
|
if (folder) data.append('folderId', folder);
|
|
|
|
|
if (name) data.append('name', name);
|
|
|
|
|
|
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
|
xhr.open('POST', apiUrl + '/drive/files/create', true);
|
2021-11-19 03:57:34 -06:00
|
|
|
|
xhr.onload = (ev) => {
|
|
|
|
|
if (ev.target == null || ev.target.response == null) {
|
|
|
|
|
// TODO: 消すのではなくて再送できるようにしたい
|
|
|
|
|
uploads.value = uploads.value.filter(x => x.id != id);
|
|
|
|
|
|
|
|
|
|
alert({
|
|
|
|
|
type: 'error',
|
|
|
|
|
text: 'upload failed'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
reject();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const driveFile = JSON.parse(ev.target.response);
|
2020-10-17 06:12:00 -05:00
|
|
|
|
|
|
|
|
|
resolve(driveFile);
|
|
|
|
|
|
|
|
|
|
uploads.value = uploads.value.filter(x => x.id != id);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
xhr.upload.onprogress = e => {
|
|
|
|
|
if (e.lengthComputable) {
|
|
|
|
|
ctx.progressMax = e.total;
|
|
|
|
|
ctx.progressValue = e.loaded;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
xhr.send(data);
|
|
|
|
|
};
|
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}*/
|