From 466835a614930e172d9621d2e14e58f0d6733c5d Mon Sep 17 00:00:00 2001 From: kakkokari-gtyih <67428053+kakkokari-gtyih@users.noreply.github.com> Date: Mon, 8 Apr 2024 20:21:56 +0900 Subject: [PATCH] use options --- packages/frontend/src/scripts/select-file.ts | 27 +++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/frontend/src/scripts/select-file.ts b/packages/frontend/src/scripts/select-file.ts index daa3f760ac..c1e9a5230f 100644 --- a/packages/frontend/src/scripts/select-file.ts +++ b/packages/frontend/src/scripts/select-file.ts @@ -13,6 +13,12 @@ import { i18n } from '@/i18n.js'; import { defaultStore } from '@/store.js'; import { uploadFile } from '@/scripts/upload.js'; +type SelectFileOptions = { + multiple?: boolean; + excludeSensitive?: boolean; + additionalMenu?: MenuItem[]; +}; + export function chooseFileFromPc(multiple: boolean, keepOriginal = false): Promise { return new Promise((res, rej) => { const input = document.createElement('input'); @@ -81,12 +87,19 @@ export function chooseFileFromUrl(): Promise { }); } -function select(src: any, label: string | null, multiple: boolean, excludeSensitive = false, additionalMenu: MenuItem[] = []): Promise { +function select(src: any, label: string | null, options?: SelectFileOptions): Promise { + const _options = { + multiple: false, + excludeSensitive: false, + additionalMenu: [], + ...options, + }; + return new Promise((res, rej) => { const keepOriginal = ref(defaultStore.state.keepOriginalUploading); function _resolve(files: Misskey.entities.DriveFile[]) { - if (excludeSensitive && files.some(file => file.isSensitive)) { + if (_options.excludeSensitive && files.some(file => file.isSensitive)) { os.alert({ title: i18n.ts.cannotSelectSensitiveMedia, text: i18n.ts.cannotSelectSensitiveMediaDescription, @@ -108,23 +121,23 @@ function select(src: any, label: string | null, multiple: boolean, excludeSensit }, { text: i18n.ts.upload, icon: 'ti ti-upload', - action: () => chooseFileFromPc(multiple, keepOriginal.value).then(files => _resolve(files)), + action: () => chooseFileFromPc(_options.multiple, keepOriginal.value).then(files => _resolve(files)), }, { text: i18n.ts.fromDrive, icon: 'ti ti-cloud', - action: () => chooseFileFromDrive(multiple, excludeSensitive).then(files => _resolve(files)), + action: () => chooseFileFromDrive(_options.multiple, _options.excludeSensitive).then(files => _resolve(files)), }, { text: i18n.ts.fromUrl, icon: 'ti ti-link', action: () => chooseFileFromUrl().then(file => _resolve([file])), - }, ...additionalMenu], src); + }, ..._options.additionalMenu], src); }); } export function selectFile(src: any, label: string | null = null, options?: { excludeSensitive?: boolean; additionalMenu?: MenuItem[]; }): Promise { - return select(src, label, false, options?.excludeSensitive, options?.additionalMenu).then(files => files[0]); + return select(src, label, { ...options, multiple: false }).then(files => files[0]); } export function selectFiles(src: any, label: string | null = null, options?: { excludeSensitive?: boolean; additionalMenu?: MenuItem[]; }): Promise { - return select(src, label, true, options?.excludeSensitive, options?.additionalMenu); + return select(src, label, { ...options, multiple: true }); }