1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-29 05:58:31 +09:00
misskey/src/client/app/desktop/views/widgets/post-form.vue

302 lines
6.5 KiB
Vue
Raw Normal View History

2018-02-20 06:13:27 +09:00
<template>
2018-11-13 01:04:15 +09:00
<div>
<mk-widget-container :show-header="props.design == 0">
<template slot="header"><fa icon="pencil-alt"/>{{ $t('title') }}</template>
2018-11-13 15:01:05 +09:00
<div class="lhcuptdmcdkfwmipgazeawoiuxpzaclc-body"
@dragover.stop="onDragover"
@drop.stop="onDrop"
>
2018-11-13 01:04:15 +09:00
<div class="textarea">
<textarea
:disabled="posting"
v-model="text"
@keydown="onKeydown"
@paste="onPaste"
:placeholder="placeholder"
ref="text"
v-autocomplete="'text'"
></textarea>
<button class="emoji" @click="emoji" ref="emoji">
<fa :icon="['far', 'laugh']"/>
</button>
</div>
<div class="files" v-show="files.length != 0">
<x-draggable :list="files" :options="{ animation: 150 }">
<div v-for="file in files" :key="file.id">
<div class="img" :style="{ backgroundImage: `url(${file.thumbnailUrl})` }" :title="file.name"></div>
<img class="remove" @click="detachMedia(file.id)" src="/assets/desktop/remove.png" :title="$t('attach-cancel')" alt=""/>
</div>
</x-draggable>
</div>
<input ref="file" type="file" multiple="multiple" tabindex="-1" @change="onChangeFile"/>
<mk-uploader ref="uploader" @uploaded="attachMedia"/>
<footer>
<button @click="chooseFile"><fa icon="upload"/></button>
<button @click="chooseFileFromDrive"><fa icon="cloud"/></button>
<button @click="post" :disabled="posting" class="post">{{ $t('note') }}</button>
</footer>
</div>
</mk-widget-container>
2018-02-20 06:13:27 +09:00
</div>
</template>
<script lang="ts">
2018-02-25 00:18:09 +09:00
import define from '../../../common/define-widget';
import i18n from '../../../i18n';
2018-11-13 01:04:15 +09:00
import insertTextAtCursor from 'insert-text-at-cursor';
import * as XDraggable from 'vuedraggable';
2018-02-20 06:13:27 +09:00
export default define({
name: 'post-form',
2018-02-21 15:30:03 +09:00
props: () => ({
2018-02-20 06:13:27 +09:00
design: 0
2018-02-21 15:30:03 +09:00
})
2018-02-20 06:13:27 +09:00
}).extend({
i18n: i18n('desktop/views/widgets/post-form.vue'),
2018-11-13 01:04:15 +09:00
components: {
XDraggable
},
2018-02-20 06:13:27 +09:00
data() {
return {
posting: false,
2018-11-13 01:04:15 +09:00
text: '',
files: [],
2018-02-20 06:13:27 +09:00
};
},
2018-11-13 01:04:15 +09:00
2018-05-29 01:44:15 +09:00
computed: {
placeholder(): string {
2018-05-29 02:00:45 +09:00
const xs = [
this.$t('@.note-placeholders.a'),
this.$t('@.note-placeholders.b'),
this.$t('@.note-placeholders.c'),
this.$t('@.note-placeholders.d'),
this.$t('@.note-placeholders.e'),
this.$t('@.note-placeholders.f')
2018-05-29 02:00:45 +09:00
];
return xs[Math.floor(Math.random() * xs.length)];
2018-05-29 01:44:15 +09:00
}
},
2018-11-13 01:04:15 +09:00
2018-02-20 06:13:27 +09:00
methods: {
func() {
if (this.props.design == 1) {
this.props.design = 0;
} else {
this.props.design++;
}
2018-04-29 17:17:15 +09:00
this.save();
2018-02-20 06:13:27 +09:00
},
2018-11-13 01:04:15 +09:00
chooseFile() {
(this.$refs.file as any).click();
},
chooseFileFromDrive() {
this.$chooseDriveFile({
multiple: true
}).then(files => {
files.forEach(this.attachMedia);
});
},
attachMedia(driveFile) {
this.files.push(driveFile);
this.$emit('change-attached-files', this.files);
},
detachMedia(id) {
this.files = this.files.filter(x => x.id != id);
this.$emit('change-attached-files', this.files);
},
2018-02-20 06:13:27 +09:00
onKeydown(e) {
2018-07-21 02:14:24 +09:00
if ((e.which == 10 || e.which == 13) && (e.ctrlKey || e.metaKey) && !this.posting && this.text) this.post();
2018-02-20 06:13:27 +09:00
},
2018-11-13 01:04:15 +09:00
onPaste(e) {
Array.from(e.clipboardData.items).forEach((item: any) => {
if (item.kind == 'file') {
this.upload(item.getAsFile());
}
});
},
onChangeFile() {
Array.from((this.$refs.file as any).files).forEach(this.upload);
},
upload(file) {
(this.$refs.uploader as any).upload(file);
},
2018-11-13 15:01:05 +09:00
onDragover(e) {
const isFile = e.dataTransfer.items[0].kind == 'file';
const isDriveFile = e.dataTransfer.types[0] == 'mk_drive_file';
if (isFile || isDriveFile) {
e.preventDefault();
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
}
},
onDrop(e): void {
// ファイルだったら
if (e.dataTransfer.files.length > 0) {
e.preventDefault();
Array.from(e.dataTransfer.files).forEach(this.upload);
return;
}
//#region ドライブのファイル
const driveFile = e.dataTransfer.getData('mk_drive_file');
if (driveFile != null && driveFile != '') {
const file = JSON.parse(driveFile);
this.files.push(file);
e.preventDefault();
}
//#endregion
},
2018-11-13 01:04:15 +09:00
async emoji() {
const Picker = await import('../components/emoji-picker-dialog.vue').then(m => m.default);
const button = this.$refs.emoji;
const rect = button.getBoundingClientRect();
const vm = this.$root.new(Picker, {
x: button.offsetWidth + rect.left + window.pageXOffset,
y: rect.top + window.pageYOffset
});
vm.$once('chosen', emoji => {
insertTextAtCursor(this.$refs.text, emoji);
});
},
2018-02-20 06:13:27 +09:00
post() {
this.posting = true;
2018-11-09 08:13:34 +09:00
this.$root.api('notes/create', {
2018-11-13 01:04:15 +09:00
text: this.text == '' ? undefined : this.text,
fileIds: this.files.length > 0 ? this.files.map(f => f.id) : undefined,
2018-02-20 06:13:27 +09:00
}).then(data => {
this.clear();
}).catch(err => {
2018-08-07 03:20:26 +09:00
alert('Something happened');
2018-02-20 06:13:27 +09:00
}).then(() => {
this.posting = false;
});
},
2018-11-13 01:04:15 +09:00
2018-02-20 06:13:27 +09:00
clear() {
this.text = '';
2018-11-13 01:04:15 +09:00
this.files = [];
2018-02-20 06:13:27 +09:00
}
}
});
</script>
<style lang="stylus" scoped>
2018-11-13 01:04:15 +09:00
.lhcuptdmcdkfwmipgazeawoiuxpzaclc-body
> .textarea
> .emoji
position absolute
top 0
right 0
padding 10px
font-size 18px
color var(--text)
opacity 0.5
&:hover
color var(--textHighlighted)
opacity 1
&:active
color var(--primary)
opacity 1
> textarea
display block
width 100%
max-width 100%
min-width 100%
padding 16px
color var(--desktopPostFormTextareaFg)
outline none
background var(--desktopPostFormTextareaBg)
border none
border-bottom solid 1px var(--faceDivider)
2018-11-16 17:41:52 +09:00
padding-right 30px
2018-11-13 01:04:15 +09:00
&:focus
& + .emoji
opacity 0.7
> .files
> div
padding 4px
&:after
content ""
display block
clear both
> div
float left
border solid 4px transparent
cursor move
&:hover > .remove
display block
> .img
width 64px
height 64px
background-size cover
background-position center center
> .remove
display none
position absolute
top -6px
right -6px
width 16px
height 16px
cursor pointer
> input[type=file]
display none
> footer
display flex
padding 8px
> button:not(.post)
color var(--text)
&:hover
color var(--textHighlighted)
> .post
display block
margin 0 0 0 auto
padding 0 10px
height 28px
color var(--primaryForeground)
background var(--primary) !important
outline none
border none
border-radius 4px
transition background 0.1s ease
cursor pointer
2018-09-26 20:19:35 +09:00
2018-11-13 01:04:15 +09:00
&:hover
background var(--primaryLighten10) !important
2018-03-03 13:47:55 +09:00
2018-11-13 01:04:15 +09:00
&:active
background var(--primaryDarken10) !important
transition background 0s ease
2018-02-20 06:13:27 +09:00
</style>