summaryrefslogtreecommitdiff
path: root/src/client/app/desktop/views/widgets/post-form.vue
blob: 622e5c33459a186183f97a145048cc2a23d7d52b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<template>
<div>
	<mk-widget-container :show-header="props.design == 0">
		<template slot="header"><fa icon="pencil-alt"/>{{ $t('title') }}</template>

		<div class="lhcuptdmcdkfwmipgazeawoiuxpzaclc-body"
			@dragover.stop="onDragover"
			@drop.stop="onDrop"
		>
			<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>
</div>
</template>

<script lang="ts">
import define from '../../../common/define-widget';
import i18n from '../../../i18n';
import insertTextAtCursor from 'insert-text-at-cursor';
import * as XDraggable from 'vuedraggable';

export default define({
	name: 'post-form',
	props: () => ({
		design: 0
	})
}).extend({
	i18n: i18n('desktop/views/widgets/post-form.vue'),

	components: {
		XDraggable
	},

	data() {
		return {
			posting: false,
			text: '',
			files: [],
		};
	},

	computed: {
		placeholder(): string {
			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')
			];
			return xs[Math.floor(Math.random() * xs.length)];
		}
	},

	methods: {
		func() {
			if (this.props.design == 1) {
				this.props.design = 0;
			} else {
				this.props.design++;
			}
			this.save();
		},

		chooseFile() {
			(this.$refs.file as any).click();
		},

		chooseFileFromDrive() {
			this.$chooseDriveFile({
				multiple: true
			}).then(files => {
				for (const x of files) this.attachMedia(x);
			});
		},

		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);
		},

		onKeydown(e) {
			if ((e.which == 10 || e.which == 13) && (e.ctrlKey || e.metaKey) && !this.posting && this.text) this.post();
		},

		onPaste(e) {
			for (const item of Array.from(e.clipboardData.items)) {
				if (item.kind == 'file') {
					this.upload(item.getAsFile());
				}
			}
		},

		onChangeFile() {
			for (const x of Array.from((this.$refs.file as any).files)) this.upload(x);
		},

		upload(file) {
			(this.$refs.uploader as any).upload(file);
		},

		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();
				for (const x of Array.from(e.dataTransfer.files)) this.upload(x);
				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
		},

		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);
			});
		},

		post() {
			this.posting = true;

			this.$root.api('notes/create', {
				text: this.text == '' ? undefined : this.text,
				fileIds: this.files.length > 0 ? this.files.map(f => f.id) : undefined,
			}).then(data => {
				this.clear();
			}).catch(err => {
				alert('Something happened');
			}).then(() => {
				this.posting = false;
			});
		},

		clear() {
			this.text = '';
			this.files = [];
		}
	}
});
</script>

<style lang="stylus" scoped>
.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)
			padding-right 30px

			&: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

			&:hover
				background var(--primaryLighten10) !important

			&:active
				background var(--primaryDarken10) !important
				transition background 0s ease

</style>