2018-02-20 13:29:52 +02:00
|
|
|
<template>
|
2018-02-24 18:55:49 +02:00
|
|
|
<div class="mk-autocomplete" @contextmenu.prevent="() => {}">
|
|
|
|
<ol class="users" ref="suggests" v-if="users.length > 0">
|
|
|
|
<li v-for="user in users" @click="complete(type, user)" @keydown="onKeydown" tabindex="-1">
|
2018-07-24 17:43:14 +03:00
|
|
|
<img class="avatar" :src="user.avatarUrl" alt=""/>
|
2018-04-09 12:52:29 +03:00
|
|
|
<span class="name">{{ user | userName }}</span>
|
|
|
|
<span class="username">@{{ user | acct }}</span>
|
2018-02-20 13:29:52 +02:00
|
|
|
</li>
|
|
|
|
</ol>
|
2018-07-18 01:19:24 +03:00
|
|
|
<ol class="hashtags" ref="suggests" v-if="hashtags.length > 0">
|
|
|
|
<li v-for="hashtag in hashtags" @click="complete(type, hashtag)" @keydown="onKeydown" tabindex="-1">
|
|
|
|
<span class="name">{{ hashtag }}</span>
|
|
|
|
</li>
|
|
|
|
</ol>
|
2018-02-24 18:55:49 +02:00
|
|
|
<ol class="emojis" ref="suggests" v-if="emojis.length > 0">
|
2018-02-25 10:03:39 +02:00
|
|
|
<li v-for="emoji in emojis" @click="complete(type, emoji.emoji)" @keydown="onKeydown" tabindex="-1">
|
2018-11-05 12:20:35 +02:00
|
|
|
<span class="emoji" v-if="emoji.isCustomEmoji"><img :src="emoji.url" :alt="emoji.emoji"/></span>
|
|
|
|
<span class="emoji" v-else-if="!useOsDefaultEmojis"><img :src="emoji.url" :alt="emoji.emoji"/></span>
|
2018-11-01 04:51:49 +02:00
|
|
|
<span class="emoji" v-else>{{ emoji.emoji }}</span>
|
2018-02-25 10:03:39 +02:00
|
|
|
<span class="name" v-html="emoji.name.replace(q, `<b>${q}</b>`)"></span>
|
2018-11-01 14:28:39 +02:00
|
|
|
<span class="alias" v-if="emoji.aliasOf">({{ emoji.aliasOf }})</span>
|
2018-02-24 18:55:49 +02:00
|
|
|
</li>
|
|
|
|
</ol>
|
2018-02-20 13:29:52 +02:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import Vue from 'vue';
|
2018-02-25 10:03:39 +02:00
|
|
|
import * as emojilib from 'emojilib';
|
2018-02-20 13:29:52 +02:00
|
|
|
import contains from '../../../common/scripts/contains';
|
|
|
|
|
2018-11-01 14:28:39 +02:00
|
|
|
type EmojiDef = {
|
|
|
|
emoji: string;
|
|
|
|
name: string;
|
|
|
|
aliasOf?: string;
|
|
|
|
url?: string;
|
2018-11-05 12:20:35 +02:00
|
|
|
isCustomEmoji?: boolean;
|
2018-11-01 14:28:39 +02:00
|
|
|
};
|
|
|
|
|
2018-02-25 10:03:39 +02:00
|
|
|
const lib = Object.entries(emojilib.lib).filter((x: any) => {
|
|
|
|
return x[1].category != 'flags';
|
|
|
|
});
|
2018-04-09 12:52:29 +03:00
|
|
|
|
2018-11-01 14:28:39 +02:00
|
|
|
const emjdb: EmojiDef[] = lib.map((x: any) => ({
|
2018-11-05 12:20:35 +02:00
|
|
|
emoji: x[1].char,
|
2018-02-25 10:03:39 +02:00
|
|
|
name: x[0],
|
2018-11-05 04:19:40 +02:00
|
|
|
aliasOf: null,
|
|
|
|
url: `https://twemoji.maxcdn.com/2/svg/${x[1].char.codePointAt(0).toString(16)}.svg`
|
2018-02-25 10:03:39 +02:00
|
|
|
}));
|
2018-04-09 12:52:29 +03:00
|
|
|
|
2018-02-25 10:03:39 +02:00
|
|
|
lib.forEach((x: any) => {
|
|
|
|
if (x[1].keywords) {
|
|
|
|
x[1].keywords.forEach(k => {
|
|
|
|
emjdb.push({
|
2018-11-05 12:20:35 +02:00
|
|
|
emoji: x[1].char,
|
2018-02-25 10:03:39 +02:00
|
|
|
name: k,
|
2018-11-05 04:19:40 +02:00
|
|
|
aliasOf: x[0],
|
|
|
|
url: `https://twemoji.maxcdn.com/2/svg/${x[1].char.codePointAt(0).toString(16)}.svg`
|
2018-02-25 10:03:39 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-04-09 12:52:29 +03:00
|
|
|
|
2018-02-25 10:03:39 +02:00
|
|
|
emjdb.sort((a, b) => a.name.length - b.name.length);
|
|
|
|
|
2018-02-20 13:29:52 +02:00
|
|
|
export default Vue.extend({
|
2018-02-24 19:57:19 +02:00
|
|
|
props: ['type', 'q', 'textarea', 'complete', 'close', 'x', 'y'],
|
2018-07-18 01:19:24 +03:00
|
|
|
|
2018-02-20 13:29:52 +02:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
fetching: true,
|
|
|
|
users: [],
|
2018-07-18 01:19:24 +03:00
|
|
|
hashtags: [],
|
2018-02-24 18:55:49 +02:00
|
|
|
emojis: [],
|
|
|
|
select: -1,
|
2018-11-01 14:28:39 +02:00
|
|
|
emojilib,
|
|
|
|
emojiDb: [] as EmojiDef[]
|
2018-02-24 18:55:49 +02:00
|
|
|
}
|
|
|
|
},
|
2018-07-18 01:19:24 +03:00
|
|
|
|
2018-02-24 18:55:49 +02:00
|
|
|
computed: {
|
|
|
|
items(): HTMLCollection {
|
|
|
|
return (this.$refs.suggests as Element).children;
|
2018-11-05 12:20:35 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
useOsDefaultEmojis(): boolean {
|
|
|
|
return this.$store.state.device.useOsDefaultEmojis;
|
2018-02-20 13:29:52 +02:00
|
|
|
}
|
|
|
|
},
|
2018-07-18 01:19:24 +03:00
|
|
|
|
2018-02-24 19:57:19 +02:00
|
|
|
updated() {
|
|
|
|
//#region 位置調整
|
2018-07-18 01:19:24 +03:00
|
|
|
if (this.x + this.$el.offsetWidth > window.innerWidth) {
|
|
|
|
this.$el.style.left = (window.innerWidth - this.$el.offsetWidth) + 'px';
|
2018-02-24 19:57:19 +02:00
|
|
|
} else {
|
|
|
|
this.$el.style.left = this.x + 'px';
|
|
|
|
}
|
|
|
|
|
2018-07-18 01:19:24 +03:00
|
|
|
if (this.y + this.$el.offsetHeight > window.innerHeight) {
|
2018-02-24 19:57:19 +02:00
|
|
|
this.$el.style.top = (this.y - this.$el.offsetHeight) + 'px';
|
|
|
|
this.$el.style.marginTop = '0';
|
|
|
|
} else {
|
|
|
|
this.$el.style.top = this.y + 'px';
|
|
|
|
this.$el.style.marginTop = 'calc(1em + 8px)';
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
},
|
2018-07-18 01:19:24 +03:00
|
|
|
|
2018-02-20 13:29:52 +02:00
|
|
|
mounted() {
|
2018-11-01 14:28:39 +02:00
|
|
|
//#region Construct Emoji DB
|
|
|
|
const customEmojis = (this.os.getMetaSync() || { emojis: [] }).emojis || [];
|
|
|
|
const emojiDefinitions: EmojiDef[] = [];
|
|
|
|
|
|
|
|
customEmojis.forEach(x => {
|
|
|
|
emojiDefinitions.push({
|
|
|
|
name: x.name,
|
|
|
|
emoji: `:${x.name}:`,
|
2018-11-05 12:20:35 +02:00
|
|
|
url: x.url,
|
|
|
|
isCustomEmoji: true
|
2018-11-01 14:28:39 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (x.aliases) {
|
|
|
|
x.aliases.forEach(alias => {
|
|
|
|
emojiDefinitions.push({
|
|
|
|
name: alias,
|
|
|
|
aliasOf: x.name,
|
|
|
|
emoji: `:${x.name}:`,
|
2018-11-05 12:20:35 +02:00
|
|
|
url: x.url,
|
|
|
|
isCustomEmoji: true
|
2018-11-01 14:28:39 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
emojiDefinitions.sort((a, b) => a.name.length - b.name.length);
|
|
|
|
|
|
|
|
this.emojiDb = emojiDefinitions.concat(emjdb);
|
|
|
|
//#endregion
|
|
|
|
|
2018-02-20 13:29:52 +02:00
|
|
|
this.textarea.addEventListener('keydown', this.onKeydown);
|
|
|
|
|
|
|
|
Array.from(document.querySelectorAll('body *')).forEach(el => {
|
|
|
|
el.addEventListener('mousedown', this.onMousedown);
|
|
|
|
});
|
|
|
|
|
2018-02-25 11:40:39 +02:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.exec();
|
2018-02-25 10:38:16 +02:00
|
|
|
|
2018-02-25 11:40:39 +02:00
|
|
|
this.$watch('q', () => {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.exec();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-02-20 13:29:52 +02:00
|
|
|
},
|
2018-07-18 01:19:24 +03:00
|
|
|
|
2018-02-20 13:29:52 +02:00
|
|
|
beforeDestroy() {
|
|
|
|
this.textarea.removeEventListener('keydown', this.onKeydown);
|
|
|
|
|
|
|
|
Array.from(document.querySelectorAll('body *')).forEach(el => {
|
|
|
|
el.removeEventListener('mousedown', this.onMousedown);
|
|
|
|
});
|
|
|
|
},
|
2018-07-18 01:19:24 +03:00
|
|
|
|
2018-02-20 13:29:52 +02:00
|
|
|
methods: {
|
2018-02-25 10:38:16 +02:00
|
|
|
exec() {
|
2018-02-25 11:40:39 +02:00
|
|
|
this.select = -1;
|
|
|
|
if (this.$refs.suggests) {
|
|
|
|
Array.from(this.items).forEach(el => {
|
|
|
|
el.removeAttribute('data-selected');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-25 10:38:16 +02:00
|
|
|
if (this.type == 'user') {
|
2018-09-01 17:12:51 +03:00
|
|
|
const cacheKey = `autocomplete:user:${this.q}`;
|
2018-07-18 01:19:24 +03:00
|
|
|
const cache = sessionStorage.getItem(cacheKey);
|
2018-02-25 10:38:16 +02:00
|
|
|
if (cache) {
|
|
|
|
const users = JSON.parse(cache);
|
|
|
|
this.users = users;
|
|
|
|
this.fetching = false;
|
|
|
|
} else {
|
2018-08-06 12:28:27 +03:00
|
|
|
(this as any).api('users/search', {
|
2018-02-25 10:38:16 +02:00
|
|
|
query: this.q,
|
|
|
|
limit: 30
|
|
|
|
}).then(users => {
|
|
|
|
this.users = users;
|
|
|
|
this.fetching = false;
|
|
|
|
|
|
|
|
// キャッシュ
|
2018-07-18 01:19:24 +03:00
|
|
|
sessionStorage.setItem(cacheKey, JSON.stringify(users));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (this.type == 'hashtag') {
|
2018-07-20 21:15:31 +03:00
|
|
|
if (this.q == null || this.q == '') {
|
|
|
|
this.hashtags = JSON.parse(localStorage.getItem('hashtags') || '[]');
|
2018-07-18 01:19:24 +03:00
|
|
|
this.fetching = false;
|
|
|
|
} else {
|
2018-09-01 17:12:51 +03:00
|
|
|
const cacheKey = `autocomplete:hashtag:${this.q}`;
|
2018-07-20 21:15:31 +03:00
|
|
|
const cache = sessionStorage.getItem(cacheKey);
|
|
|
|
if (cache) {
|
|
|
|
const hashtags = JSON.parse(cache);
|
2018-07-18 01:19:24 +03:00
|
|
|
this.hashtags = hashtags;
|
|
|
|
this.fetching = false;
|
2018-07-20 21:15:31 +03:00
|
|
|
} else {
|
|
|
|
(this as any).api('hashtags/search', {
|
|
|
|
query: this.q,
|
|
|
|
limit: 30
|
|
|
|
}).then(hashtags => {
|
|
|
|
this.hashtags = hashtags;
|
|
|
|
this.fetching = false;
|
|
|
|
|
|
|
|
// キャッシュ
|
|
|
|
sessionStorage.setItem(cacheKey, JSON.stringify(hashtags));
|
|
|
|
});
|
|
|
|
}
|
2018-02-25 10:38:16 +02:00
|
|
|
}
|
|
|
|
} else if (this.type == 'emoji') {
|
2018-11-05 19:05:16 +02:00
|
|
|
if (this.q == null || this.q == '') {
|
2018-11-06 01:02:19 +02:00
|
|
|
this.emojis = this.emojiDb.filter(x => x.isCustomEmoji && !x.aliasOf).sort((a, b) => {
|
|
|
|
var textA = a.name.toUpperCase();
|
|
|
|
var textB = b.name.toUpperCase();
|
|
|
|
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
|
|
|
|
});
|
2018-11-05 19:05:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-25 10:38:16 +02:00
|
|
|
const matched = [];
|
2018-11-01 04:51:49 +02:00
|
|
|
const max = 30;
|
|
|
|
|
2018-11-01 14:28:39 +02:00
|
|
|
this.emojiDb.some(x => {
|
|
|
|
if (x.name.startsWith(this.q) && !x.aliasOf && !matched.some(y => y.emoji == x.emoji)) matched.push(x);
|
2018-11-01 04:51:49 +02:00
|
|
|
return matched.length == max;
|
2018-02-25 10:38:16 +02:00
|
|
|
});
|
2018-11-01 04:51:49 +02:00
|
|
|
if (matched.length < max) {
|
2018-11-01 14:28:39 +02:00
|
|
|
this.emojiDb.some(x => {
|
2018-11-01 14:01:47 +02:00
|
|
|
if (x.name.startsWith(this.q) && !matched.some(y => y.emoji == x.emoji)) matched.push(x);
|
2018-11-01 04:51:49 +02:00
|
|
|
return matched.length == max;
|
2018-02-27 07:11:18 +02:00
|
|
|
});
|
|
|
|
}
|
2018-11-01 04:51:49 +02:00
|
|
|
if (matched.length < max) {
|
2018-11-01 14:28:39 +02:00
|
|
|
this.emojiDb.some(x => {
|
2018-11-01 14:01:47 +02:00
|
|
|
if (x.name.includes(this.q) && !matched.some(y => y.emoji == x.emoji)) matched.push(x);
|
2018-11-01 04:51:49 +02:00
|
|
|
return matched.length == max;
|
2018-02-27 07:11:18 +02:00
|
|
|
});
|
|
|
|
}
|
2018-11-01 04:51:49 +02:00
|
|
|
|
2018-02-25 10:38:16 +02:00
|
|
|
this.emojis = matched;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-20 13:29:52 +02:00
|
|
|
onMousedown(e) {
|
|
|
|
if (!contains(this.$el, e.target) && (this.$el != e.target)) this.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
onKeydown(e) {
|
|
|
|
const cancel = () => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (e.which) {
|
|
|
|
case 10: // [ENTER]
|
|
|
|
case 13: // [ENTER]
|
|
|
|
if (this.select !== -1) {
|
|
|
|
cancel();
|
2018-02-24 18:55:49 +02:00
|
|
|
(this.items[this.select] as any).click();
|
2018-02-20 13:29:52 +02:00
|
|
|
} else {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 27: // [ESC]
|
|
|
|
cancel();
|
|
|
|
this.close();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 38: // [↑]
|
|
|
|
if (this.select !== -1) {
|
|
|
|
cancel();
|
|
|
|
this.selectPrev();
|
|
|
|
} else {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 9: // [TAB]
|
|
|
|
case 40: // [↓]
|
|
|
|
cancel();
|
|
|
|
this.selectNext();
|
|
|
|
break;
|
2018-02-25 11:40:39 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
e.stopPropagation();
|
|
|
|
this.textarea.focus();
|
2018-02-20 13:29:52 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
selectNext() {
|
2018-02-24 18:55:49 +02:00
|
|
|
if (++this.select >= this.items.length) this.select = 0;
|
2018-02-20 13:29:52 +02:00
|
|
|
this.applySelect();
|
|
|
|
},
|
|
|
|
|
|
|
|
selectPrev() {
|
2018-02-24 18:55:49 +02:00
|
|
|
if (--this.select < 0) this.select = this.items.length - 1;
|
2018-02-20 13:29:52 +02:00
|
|
|
this.applySelect();
|
|
|
|
},
|
|
|
|
|
|
|
|
applySelect() {
|
2018-02-24 18:55:49 +02:00
|
|
|
Array.from(this.items).forEach(el => {
|
2018-02-20 13:29:52 +02:00
|
|
|
el.removeAttribute('data-selected');
|
|
|
|
});
|
|
|
|
|
2018-02-24 18:55:49 +02:00
|
|
|
this.items[this.select].setAttribute('data-selected', 'true');
|
|
|
|
(this.items[this.select] as any).focus();
|
2018-02-20 13:29:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="stylus" scoped>
|
2018-09-26 19:44:05 +03:00
|
|
|
.mk-autocomplete
|
2018-02-24 19:57:19 +02:00
|
|
|
position fixed
|
2018-02-20 13:29:52 +02:00
|
|
|
z-index 65535
|
2018-07-18 21:25:37 +03:00
|
|
|
max-width 100%
|
2018-02-20 13:29:52 +02:00
|
|
|
margin-top calc(1em + 8px)
|
|
|
|
overflow hidden
|
2018-09-26 14:28:13 +03:00
|
|
|
background var(--faceHeader)
|
2018-04-29 02:51:17 +03:00
|
|
|
border solid 1px rgba(#000, 0.1)
|
2018-02-20 13:29:52 +02:00
|
|
|
border-radius 4px
|
2018-02-25 10:38:16 +02:00
|
|
|
transition top 0.1s ease, left 0.1s ease
|
2018-02-20 13:29:52 +02:00
|
|
|
|
2018-02-24 18:55:49 +02:00
|
|
|
> ol
|
2018-02-20 13:29:52 +02:00
|
|
|
display block
|
|
|
|
margin 0
|
|
|
|
padding 4px 0
|
|
|
|
max-height 190px
|
|
|
|
max-width 500px
|
|
|
|
overflow auto
|
|
|
|
list-style none
|
|
|
|
|
|
|
|
> li
|
2018-07-18 22:50:46 +03:00
|
|
|
display flex
|
|
|
|
align-items center
|
2018-02-20 13:29:52 +02:00
|
|
|
padding 4px 12px
|
|
|
|
white-space nowrap
|
|
|
|
overflow hidden
|
|
|
|
font-size 0.9em
|
2018-04-29 02:51:17 +03:00
|
|
|
color rgba(#000, 0.8)
|
2018-02-20 13:29:52 +02:00
|
|
|
cursor default
|
|
|
|
|
|
|
|
&, *
|
|
|
|
user-select none
|
|
|
|
|
2018-07-18 22:50:46 +03:00
|
|
|
*
|
|
|
|
overflow hidden
|
|
|
|
text-overflow ellipsis
|
|
|
|
|
2018-02-20 13:29:52 +02:00
|
|
|
&:hover
|
2018-09-26 19:44:05 +03:00
|
|
|
background var(--autocompleteItemHoverBg)
|
2018-07-18 01:19:24 +03:00
|
|
|
|
2018-02-20 13:29:52 +02:00
|
|
|
&[data-selected='true']
|
2018-09-26 14:19:35 +03:00
|
|
|
background var(--primary)
|
2018-02-20 13:29:52 +02:00
|
|
|
|
2018-02-24 18:55:49 +02:00
|
|
|
&, *
|
|
|
|
color #fff !important
|
2018-02-20 13:29:52 +02:00
|
|
|
|
|
|
|
&:active
|
2018-09-26 14:19:35 +03:00
|
|
|
background var(--primaryDarken10)
|
2018-02-20 13:29:52 +02:00
|
|
|
|
2018-02-24 18:55:49 +02:00
|
|
|
&, *
|
|
|
|
color #fff !important
|
|
|
|
|
|
|
|
> .users > li
|
|
|
|
|
|
|
|
.avatar
|
|
|
|
min-width 28px
|
|
|
|
min-height 28px
|
|
|
|
max-width 28px
|
|
|
|
max-height 28px
|
|
|
|
margin 0 8px 0 0
|
|
|
|
border-radius 100%
|
|
|
|
|
|
|
|
.name
|
|
|
|
margin 0 8px 0 0
|
2018-09-26 19:44:05 +03:00
|
|
|
color var(--autocompleteItemText)
|
2018-02-24 18:55:49 +02:00
|
|
|
|
|
|
|
.username
|
2018-09-26 19:44:05 +03:00
|
|
|
color var(--autocompleteItemTextSub)
|
2018-02-24 18:55:49 +02:00
|
|
|
|
2018-07-18 01:19:24 +03:00
|
|
|
> .hashtags > li
|
|
|
|
|
|
|
|
.name
|
2018-09-26 19:44:05 +03:00
|
|
|
color var(--autocompleteItemText)
|
2018-07-18 01:19:24 +03:00
|
|
|
|
2018-02-24 18:55:49 +02:00
|
|
|
> .emojis > li
|
|
|
|
|
|
|
|
.emoji
|
|
|
|
display inline-block
|
|
|
|
margin 0 4px 0 0
|
|
|
|
width 24px
|
|
|
|
|
2018-11-01 04:51:49 +02:00
|
|
|
> img
|
|
|
|
width 24px
|
|
|
|
vertical-align bottom
|
|
|
|
|
2018-02-24 18:55:49 +02:00
|
|
|
.name
|
2018-09-26 19:44:05 +03:00
|
|
|
color var(--autocompleteItemText)
|
2018-02-20 13:29:52 +02:00
|
|
|
|
2018-02-25 10:03:39 +02:00
|
|
|
.alias
|
|
|
|
margin 0 0 0 8px
|
2018-09-26 19:44:05 +03:00
|
|
|
color var(--autocompleteItemTextSub)
|
2018-02-20 13:29:52 +02:00
|
|
|
</style>
|