sharkey/packages/frontend/src/scripts/get-drive-file-menu.ts

140 lines
3.4 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
2023-03-20 13:20:21 +09:00
import * as Misskey from 'misskey-js';
import { defineAsyncComponent } from 'vue';
2023-09-19 16:37:43 +09:00
import { i18n } from '@/i18n.js';
import copyToClipboard from '@/scripts/copy-to-clipboard.js';
import * as os from '@/os.js';
import { MenuItem } from '@/types/menu.js';
import { defaultStore } from '@/store.js';
2023-03-20 13:20:21 +09:00
function rename(file: Misskey.entities.DriveFile) {
os.inputText({
title: i18n.ts.renameFile,
placeholder: i18n.ts.inputNewFileName,
default: file.name,
}).then(({ canceled, result: name }) => {
if (canceled) return;
os.api('drive/files/update', {
fileId: file.id,
name: name,
});
});
}
function describe(file: Misskey.entities.DriveFile) {
os.popup(defineAsyncComponent(() => import('@/components/MkFileCaptionEditWindow.vue')), {
default: file.comment ?? '',
2023-03-20 13:20:21 +09:00
file: file,
}, {
done: caption => {
os.api('drive/files/update', {
fileId: file.id,
comment: caption.length === 0 ? null : caption,
});
},
}, 'closed');
}
function toggleSensitive(file: Misskey.entities.DriveFile) {
os.api('drive/files/update', {
fileId: file.id,
isSensitive: !file.isSensitive,
}).catch(err => {
os.alert({
type: 'error',
title: i18n.ts.error,
text: err.message,
});
2023-03-20 13:20:21 +09:00
});
}
function copyUrl(file: Misskey.entities.DriveFile) {
copyToClipboard(file.url);
os.success();
}
2023-10-29 16:09:20 +09:00
2023-03-20 13:20:21 +09:00
/*
function addApp() {
alert('not implemented yet');
}
*/
async function deleteFile(file: Misskey.entities.DriveFile) {
const { canceled } = await os.confirm({
type: 'warning',
text: i18n.t('driveFileDeleteConfirm', { name: file.name }),
});
if (canceled) return;
os.api('drive/files/delete', {
fileId: file.id,
});
}
export function getDriveFileMenu(file: Misskey.entities.DriveFile, folder?: Misskey.entities.DriveFolder | null): MenuItem[] {
const isImage = file.type.startsWith('image/');
let menu;
menu = [{
2023-03-20 13:20:21 +09:00
text: i18n.ts.rename,
2023-10-01 04:53:52 +09:00
icon: 'ph-textbox ph-bold ph-lg',
action: () => rename(file),
2023-03-20 13:20:21 +09:00
}, {
text: file.isSensitive ? i18n.ts.unmarkAsSensitive : i18n.ts.markAsSensitive,
2023-10-01 04:53:52 +09:00
icon: file.isSensitive ? 'ph-eye ph-bold ph-lg' : 'ph-eye-closed ph-bold ph-lg',
action: () => toggleSensitive(file),
2023-03-20 13:20:21 +09:00
}, {
text: i18n.ts.describeFile,
2023-10-01 04:53:52 +09:00
icon: 'ph-text-indent ph-bold ph-lg',
action: () => describe(file),
}, ...isImage ? [{
text: i18n.ts.cropImage,
2023-10-01 04:53:52 +09:00
icon: 'ph-crop ph-bold ph-lg',
action: () => os.cropImage(file, {
aspectRatio: NaN,
uploadFolder: folder ? folder.id : folder,
}),
}] : [], null, {
text: i18n.ts.createNoteFromTheFile,
2023-10-01 04:53:52 +09:00
icon: 'ph-pencil ph-bold ph-lg',
action: () => os.post({
initialFiles: [file],
}),
}, {
2023-03-20 13:20:21 +09:00
text: i18n.ts.copyUrl,
2023-10-01 04:53:52 +09:00
icon: 'ph-link ph-bold ph-lg',
action: () => copyUrl(file),
2023-03-20 13:20:21 +09:00
}, {
type: 'a',
href: file.url,
target: '_blank',
text: i18n.ts.download,
2023-10-01 04:53:52 +09:00
icon: 'ph-download ph-bold ph-lg',
2023-03-20 13:20:21 +09:00
download: file.name,
}, null, {
type: 'link',
to: `/my/drive/file/${file.id}`,
text: i18n.ts._fileViewer.title,
2023-10-14 02:01:17 +09:00
icon: 'ph-file-text ph-bold pg-lg',
2023-03-20 13:20:21 +09:00
}, null, {
text: i18n.ts.delete,
2023-10-01 04:53:52 +09:00
icon: 'ph-trash ph-bold ph-lg',
2023-03-20 13:20:21 +09:00
danger: true,
action: () => deleteFile(file),
2023-03-20 13:20:21 +09:00
}];
if (defaultStore.state.devMode) {
menu = menu.concat([null, {
2023-10-01 04:53:52 +09:00
icon: 'ph-identification-card ph-bold ph-lg',
text: i18n.ts.copyFileId,
action: () => {
copyToClipboard(file.id);
},
}]);
}
return menu;
2023-03-20 13:20:21 +09:00
}