refactor: post-form-attaches to composition api #60

Merged
Johann150 merged 5 commits from :refactor/post-form-attaches.vue into main 2022-08-11 09:17:17 +00:00

View file

@ -14,84 +14,74 @@
</div>
</template>
<script lang="ts">
import { defineComponent, defineAsyncComponent } from 'vue';
<script lang="ts" setup>
import { defineAsyncComponent, computed } from 'vue';
import { DriveFile } from 'misskey-js/built/entities';
import MkDriveFileThumbnail from './drive-file-thumbnail.vue';
import * as os from '@/os';
import { i18n } from '@/i18n';
export default defineComponent({
components: {
XDraggable: defineAsyncComponent(() => import('vuedraggable').then(x => x.default)),
MkDriveFileThumbnail,
},
const XDraggable = defineAsyncComponent(() => import('vuedraggable').then(x => x.default));
props: {
files: {
type: Array,
required: true,
},
detachMediaFn: {
type: Function,
required: false,
},
},
const props = defineProps<{
files: DriveFile[];
detachMediaFn?: (id: string) => void;
}>();
emits: ['updated', 'detach', 'changeSensitive', 'changeName'],
const emit = defineEmits<{
(ev: 'updated', value: DriveFile[]): void;
(ev: 'detach', id: string): void;
(ev: 'changeSensitive', file: DriveFile, isSensitive: boolean): void;
(ev: 'changeName', file: DriveFile, name: string): void;
}>();
data() {
return {
menu: null as Promise<null> | null,
};
},
computed: {
_files: {
let menu: Promise<null> | null = $ref(null);
const _files = computed({
get() {
return this.files;
},
set(value) {
this.$emit('updated', value);
},
return props.files;
},
set(value: DriveFile[]) {
emit('updated', value);
},
});
methods: {
detachMedia(id) {
if (this.detachMediaFn) {
this.detachMediaFn(id);
function detachMedia(id: string): void {
if (props.detachMediaFn) {
props.detachMediaFn(id);
} else {
this.$emit('detach', id);
emit('detach', id);
}
},
toggleSensitive(file) {
}
function toggleSensitive(file: DriveFile): void {
os.api('drive/files/update', {
fileId: file.id,
isSensitive: !file.isSensitive,
}).then(() => {
this.$emit('changeSensitive', file, !file.isSensitive);
emit('changeSensitive', file, !file.isSensitive);
});
},
async rename(file) {
}
async function rename(file: DriveFile): Promise<void> {
const { canceled, result } = await os.inputText({
title: this.$ts.enterFileName,
title: i18n.ts.enterFileName,
default: file.name,
allowEmpty: false,
});
if (canceled) return;
os.api('drive/files/update', {
fileId: file.id,
name: result,
}).then(() => {
this.$emit('changeName', file, result);
emit('changeName', file, result);
file.name = result;
});
},
}
async describe(file) {
async function describe(file: DriveFile): Promise<void> {
os.popup(defineAsyncComponent(() => import('@/components/media-caption.vue')), {
title: this.$ts.describeFile,
title: i18n.ts.describeFile,
input: {
placeholder: this.$ts.inputNewDescription,
placeholder: i18n.ts.inputNewDescription,
default: file.comment !== null ? file.comment : '',
},
image: file,
@ -107,30 +97,28 @@ export default defineComponent({
});
},
}, 'closed');
},
}
showFileMenu(file, ev: MouseEvent) {
if (this.menu) return;
this.menu = os.popupMenu([{
text: this.$ts.renameFile,
function showFileMenu(file: DriveFile, ev: MouseEvent): void {
if (menu) return;
menu = os.popupMenu([{
text: i18n.ts.renameFile,
icon: 'fas fa-i-cursor',
action: () => { this.rename(file); },
action: () => { rename(file); },
}, {
text: file.isSensitive ? this.$ts.unmarkAsSensitive : this.$ts.markAsSensitive,
text: file.isSensitive ? i18n.ts.unmarkAsSensitive : i18n.ts.markAsSensitive,
icon: file.isSensitive ? 'fas fa-eye-slash' : 'fas fa-eye',
action: () => { this.toggleSensitive(file); },
action: () => { toggleSensitive(file); },
}, {
text: this.$ts.describeFile,
text: i18n.ts.describeFile,
icon: 'fas fa-i-cursor',
action: () => { this.describe(file); },
action: () => { describe(file); },
}, {
text: this.$ts.attachCancel,
text: i18n.ts.attachCancel,
icon: 'fas fa-times-circle',
action: () => { this.detachMedia(file.id); },
}], ev.currentTarget ?? ev.target).then(() => this.menu = null);
},
},
});
action: () => { detachMedia(file.id); },
}], ev.currentTarget ?? ev.target).then(() => menu = null);
}
</script>
<style lang="scss" scoped>