1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-25 20:18:28 +09:00
misskey/src/client/scripts/select-file.ts

88 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-02-14 01:09:39 +09:00
import { faUpload, faCloud } from '@fortawesome/free-solid-svg-icons';
2020-01-30 07:13:36 +09:00
import { selectDriveFile } from './select-drive-file';
import { apiUrl } from '../config';
2020-02-14 01:09:39 +09:00
export function selectFile(component: any, src: any, label: string | null, multiple = false) {
2020-01-30 07:13:36 +09:00
return new Promise((res, rej) => {
const chooseFileFromPc = () => {
const input = document.createElement('input');
input.type = 'file';
input.multiple = multiple;
input.onchange = () => {
const dialog = component.$root.dialog({
type: 'waiting',
text: component.$t('uploading') + '...',
showOkButton: false,
showCancelButton: false,
cancelableByBgClick: false
});
const promises = Array.from(input.files).map(file => new Promise((ok, err) => {
const data = new FormData();
data.append('file', file);
data.append('i', component.$store.state.i.token);
fetch(apiUrl + '/drive/files/create', {
method: 'POST',
body: data
})
.then(response => response.json())
.then(ok)
.catch(err);
}));
Promise.all(promises).then(driveFiles => {
res(multiple ? driveFiles : driveFiles[0]);
}).catch(e => {
component.$root.dialog({
type: 'error',
text: e
});
}).finally(() => {
dialog.close();
});
// 一応廃棄
(window as any).__misskey_input_ref__ = null;
2020-01-30 07:13:36 +09:00
};
// https://qiita.com/fukasawah/items/b9dc732d95d99551013d
// iOS Safari で正常に動かす為のおまじない
(window as any).__misskey_input_ref__ = input;
2020-01-30 07:13:36 +09:00
input.click();
};
const chooseFileFromDrive = () => {
selectDriveFile(component.$root, multiple).then(files => {
res(files);
});
};
// TODO
2020-01-30 07:13:36 +09:00
const chooseFileFromUrl = () => {
};
component.$root.menu({
2020-02-14 01:09:39 +09:00
items: [label ? {
2020-01-30 07:13:36 +09:00
text: label,
type: 'label'
2020-02-14 01:09:39 +09:00
} : undefined, {
2020-01-30 07:13:36 +09:00
text: component.$t('upload'),
icon: faUpload,
action: chooseFileFromPc
}, {
text: component.$t('fromDrive'),
icon: faCloud,
action: chooseFileFromDrive
}, /*{
text: component.$t('fromUrl'),
icon: faLink,
action: chooseFileFromUrl
}*/],
source: src
});
});
}