This commit is contained in:
syuilo 2018-02-25 01:55:49 +09:00
parent 36c11e1f0f
commit 83fde7c131
6 changed files with 121 additions and 85 deletions

View File

@ -1,26 +1,40 @@
<template> <template>
<div class="mk-autocomplete"> <div class="mk-autocomplete" @contextmenu.prevent="() => {}">
<ol class="users" ref="users" v-if="users.length > 0"> <ol class="users" ref="suggests" v-if="users.length > 0">
<li v-for="user in users" @click="complete(user)" @keydown="onKeydown" tabindex="-1"> <li v-for="user in users" @click="complete(type, user)" @keydown="onKeydown" tabindex="-1">
<img class="avatar" :src="`${user.avatar_url}?thumbnail&size=32`" alt=""/> <img class="avatar" :src="`${user.avatar_url}?thumbnail&size=32`" alt=""/>
<span class="name">{{ user.name }}</span> <span class="name">{{ user.name }}</span>
<span class="username">@{{ user.username }}</span> <span class="username">@{{ user.username }}</span>
</li> </li>
</ol> </ol>
<ol class="emojis" ref="suggests" v-if="emojis.length > 0">
<li v-for="emoji in emojis" @click="complete(type, pictograph.dic[emoji])" @keydown="onKeydown" tabindex="-1">
<span class="emoji">{{ pictograph.dic[emoji] }}</span>
<span class="name" v-html="emoji.replace(q, `<b>${q}</b>`)"></span>
</li>
</ol>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts">
import Vue from 'vue'; import Vue from 'vue';
import * as pictograph from 'pictograph';
import contains from '../../../common/scripts/contains'; import contains from '../../../common/scripts/contains';
export default Vue.extend({ export default Vue.extend({
props: ['q', 'textarea', 'complete', 'close'], props: ['type', 'q', 'textarea', 'complete', 'close'],
data() { data() {
return { return {
fetching: true, fetching: true,
users: [], users: [],
select: -1 emojis: [],
select: -1,
pictograph
}
},
computed: {
items(): HTMLCollection {
return (this.$refs.suggests as Element).children;
} }
}, },
mounted() { mounted() {
@ -30,6 +44,7 @@ export default Vue.extend({
el.addEventListener('mousedown', this.onMousedown); el.addEventListener('mousedown', this.onMousedown);
}); });
if (this.type == 'user') {
(this as any).api('users/search_by_username', { (this as any).api('users/search_by_username', {
query: this.q, query: this.q,
limit: 30 limit: 30
@ -37,6 +52,11 @@ export default Vue.extend({
this.users = users; this.users = users;
this.fetching = false; this.fetching = false;
}); });
} else if (this.type == 'emoji') {
const emojis = Object.keys(pictograph.dic).sort((a, b) => a.length - b.length);
const matched = emojis.filter(e => e.indexOf(this.q) > -1);
this.emojis = matched.filter((x, i) => i <= 30);
}
}, },
beforeDestroy() { beforeDestroy() {
this.textarea.removeEventListener('keydown', this.onKeydown); this.textarea.removeEventListener('keydown', this.onKeydown);
@ -61,7 +81,7 @@ export default Vue.extend({
case 13: // [ENTER] case 13: // [ENTER]
if (this.select !== -1) { if (this.select !== -1) {
cancel(); cancel();
this.complete(this.users[this.select]); (this.items[this.select] as any).click();
} else { } else {
this.close(); this.close();
} }
@ -93,24 +113,22 @@ export default Vue.extend({
}, },
selectNext() { selectNext() {
if (++this.select >= this.users.length) this.select = 0; if (++this.select >= this.items.length) this.select = 0;
this.applySelect(); this.applySelect();
}, },
selectPrev() { selectPrev() {
if (--this.select < 0) this.select = this.users.length - 1; if (--this.select < 0) this.select = this.items.length - 1;
this.applySelect(); this.applySelect();
}, },
applySelect() { applySelect() {
const els = (this.$refs.users as Element).children; Array.from(this.items).forEach(el => {
Array.from(els).forEach(el => {
el.removeAttribute('data-selected'); el.removeAttribute('data-selected');
}); });
els[this.select].setAttribute('data-selected', 'true'); this.items[this.select].setAttribute('data-selected', 'true');
(els[this.select] as any).focus(); (this.items[this.select] as any).focus();
} }
} }
}); });
@ -126,7 +144,7 @@ export default Vue.extend({
border solid 1px rgba(0, 0, 0, 0.1) border solid 1px rgba(0, 0, 0, 0.1)
border-radius 4px border-radius 4px
> .users > ol
display block display block
margin 0 margin 0
padding 4px 0 padding 4px 0
@ -149,24 +167,18 @@ export default Vue.extend({
&:hover &:hover
&[data-selected='true'] &[data-selected='true']
color #fff
background $theme-color background $theme-color
.name &, *
color #fff color #fff !important
.username
color #fff
&:active &:active
color #fff
background darken($theme-color, 10%) background darken($theme-color, 10%)
.name &, *
color #fff color #fff !important
.username > .users > li
color #fff
.avatar .avatar
vertical-align middle vertical-align middle
@ -187,4 +199,15 @@ export default Vue.extend({
font-weight normal font-weight normal
color rgba(0, 0, 0, 0.3) color rgba(0, 0, 0, 0.3)
> .emojis > li
.emoji
display inline-block
margin 0 4px 0 0
width 24px
.name
font-weight normal
color rgba(0, 0, 0, 0.8)
</style> </style>

View File

@ -1,6 +1,6 @@
<template> <template>
<div class="mk-messaging-form"> <div class="mk-messaging-form">
<textarea v-model="text" @keypress="onKeypress" @paste="onPaste" placeholder="%i18n:common.input-message-here%"></textarea> <textarea v-model="text" @keypress="onKeypress" @paste="onPaste" placeholder="%i18n:common.input-message-here%" v-autocomplete></textarea>
<div class="file" v-if="file">{{ file.name }}</div> <div class="file" v-if="file">{{ file.name }}</div>
<mk-uploader ref="uploader"/> <mk-uploader ref="uploader"/>
<button class="send" @click="send" :disabled="sending" title="%i18n:common.send%"> <button class="send" @click="send" :disabled="sending" title="%i18n:common.send%">

View File

@ -25,11 +25,11 @@ class Autocomplete {
* *
*/ */
constructor(textarea) { constructor(textarea) {
// BIND --------------------------------- //#region BIND
this.onInput = this.onInput.bind(this); this.onInput = this.onInput.bind(this);
this.complete = this.complete.bind(this); this.complete = this.complete.bind(this);
this.close = this.close.bind(this); this.close = this.close.bind(this);
// -------------------------------------- //#endregion
this.suggestion = null; this.suggestion = null;
this.textarea = textarea; this.textarea = textarea;
@ -60,16 +60,21 @@ class Autocomplete {
const text = this.textarea.value.substr(0, caret); const text = this.textarea.value.substr(0, caret);
const mentionIndex = text.lastIndexOf('@'); const mentionIndex = text.lastIndexOf('@');
const emojiIndex = text.lastIndexOf(':');
if (mentionIndex == -1) return; if (mentionIndex != -1 && mentionIndex > emojiIndex) {
const username = text.substr(mentionIndex + 1); const username = text.substr(mentionIndex + 1);
if (!username.match(/^[a-zA-Z0-9-]+$/)) return; if (!username.match(/^[a-zA-Z0-9-]+$/)) return;
this.open('user', username); this.open('user', username);
} }
if (emojiIndex != -1 && emojiIndex > mentionIndex) {
const emoji = text.substr(emojiIndex + 1);
if (!emoji.match(/^[\+\-a-z_]+$/)) return;
this.open('emoji', emoji);
}
}
/** /**
* *
*/ */
@ -88,14 +93,14 @@ class Autocomplete {
} }
}).$mount(); }).$mount();
// ~ サジェストを表示すべき位置を計算 ~ //#region サジェストを表示すべき位置を計算
const caretPosition = getCaretCoordinates(this.textarea, this.textarea.selectionStart); const caretPosition = getCaretCoordinates(this.textarea, this.textarea.selectionStart);
const rect = this.textarea.getBoundingClientRect(); const rect = this.textarea.getBoundingClientRect();
const x = rect.left + window.pageXOffset + caretPosition.left; const x = rect.left + window.pageXOffset + caretPosition.left - this.textarea.scrollLeft;
const y = rect.top + window.pageYOffset + caretPosition.top; const y = rect.top + window.pageYOffset + caretPosition.top - this.textarea.scrollTop;
//#endregion
this.suggestion.$el.style.left = x + 'px'; this.suggestion.$el.style.left = x + 'px';
this.suggestion.$el.style.top = y + 'px'; this.suggestion.$el.style.top = y + 'px';
@ -119,24 +124,39 @@ class Autocomplete {
/** /**
* *
*/ */
private complete(user) { private complete(type, value) {
this.close(); this.close();
const value = user.username;
const caret = this.textarea.selectionStart; const caret = this.textarea.selectionStart;
if (type == 'user') {
const source = this.textarea.value; const source = this.textarea.value;
const before = source.substr(0, caret); const before = source.substr(0, caret);
const trimmedBefore = before.substring(0, before.lastIndexOf('@')); const trimmedBefore = before.substring(0, before.lastIndexOf('@'));
const after = source.substr(caret); const after = source.substr(caret);
// 結果を挿入する // 挿入
this.textarea.value = trimmedBefore + '@' + value + ' ' + after; this.textarea.value = trimmedBefore + '@' + value.username + ' ' + after;
// キャレットを戻す
this.textarea.focus();
const pos = caret + value.username.length;
this.textarea.setSelectionRange(pos, pos);
} else if (type == 'emoji') {
const source = this.textarea.value;
const before = source.substr(0, caret);
const trimmedBefore = before.substring(0, before.lastIndexOf(':'));
const after = source.substr(caret);
// 挿入
this.textarea.value = trimmedBefore + value + after;
// キャレットを戻す // キャレットを戻す
this.textarea.focus(); this.textarea.focus();
const pos = caret + value.length; const pos = caret + value.length;
this.textarea.setSelectionRange(pos, pos); this.textarea.setSelectionRange(pos, pos);
} }
}
} }

View File

@ -1,5 +0,0 @@
export default {
inserted(el) {
el.focus();
}
};

View File

@ -1,5 +1,5 @@
import Vue from 'vue'; import Vue from 'vue';
import focus from './focus'; import autocomplete from './autocomplete';
Vue.directive('focus', focus); Vue.directive('autocomplete', autocomplete);

View File

@ -1,8 +1,6 @@
import Vue from 'vue'; import Vue from 'vue';
import userPreview from './user-preview'; import userPreview from './user-preview';
import autocomplete from './autocomplete';
Vue.directive('userPreview', userPreview); Vue.directive('userPreview', userPreview);
Vue.directive('user-preview', userPreview); Vue.directive('user-preview', userPreview);
Vue.directive('autocomplete', autocomplete);