This commit is contained in:
syuilo 2018-02-25 17:38:16 +09:00
parent 7ac0630858
commit 341618a9d5
3 changed files with 73 additions and 43 deletions

View file

@ -1,7 +1,7 @@
{ {
"name": "misskey", "name": "misskey",
"author": "syuilo <i@syuilo.com>", "author": "syuilo <i@syuilo.com>",
"version": "0.0.3878", "version": "0.0.3880",
"license": "MIT", "license": "MIT",
"description": "A miniblog-based SNS", "description": "A miniblog-based SNS",
"bugs": "https://github.com/syuilo/misskey/issues", "bugs": "https://github.com/syuilo/misskey/issues",

View file

@ -87,22 +87,9 @@ export default Vue.extend({
el.addEventListener('mousedown', this.onMousedown); el.addEventListener('mousedown', this.onMousedown);
}); });
if (this.type == 'user') { this.exec();
(this as any).api('users/search_by_username', {
query: this.q, this.$watch('q', this.exec);
limit: 30
}).then(users => {
this.users = users;
this.fetching = false;
});
} else if (this.type == 'emoji') {
const matched = [];
emjdb.some(x => {
if (x.name.indexOf(this.q) > -1 && !matched.some(y => y.emoji == x.emoji)) matched.push(x);
return matched.length == 30;
});
this.emojis = matched;
}
}, },
beforeDestroy() { beforeDestroy() {
this.textarea.removeEventListener('keydown', this.onKeydown); this.textarea.removeEventListener('keydown', this.onKeydown);
@ -112,6 +99,35 @@ export default Vue.extend({
}); });
}, },
methods: { methods: {
exec() {
if (this.type == 'user') {
const cache = sessionStorage.getItem(this.q);
if (cache) {
const users = JSON.parse(cache);
this.users = users;
this.fetching = false;
} else {
(this as any).api('users/search_by_username', {
query: this.q,
limit: 30
}).then(users => {
this.users = users;
this.fetching = false;
//
sessionStorage.setItem(this.q, JSON.stringify(users));
});
}
} else if (this.type == 'emoji') {
const matched = [];
emjdb.some(x => {
if (x.name.indexOf(this.q) > -1 && !matched.some(y => y.emoji == x.emoji)) matched.push(x);
return matched.length == 30;
});
this.emojis = matched;
}
},
onMousedown(e) { onMousedown(e) {
if (!contains(this.$el, e.target) && (this.$el != e.target)) this.close(); if (!contains(this.$el, e.target) && (this.$el != e.target)) this.close();
}, },
@ -152,9 +168,6 @@ export default Vue.extend({
cancel(); cancel();
this.selectNext(); this.selectNext();
break; break;
default:
this.close();
} }
}, },
@ -189,6 +202,7 @@ export default Vue.extend({
background #fff background #fff
border solid 1px rgba(0, 0, 0, 0.1) border solid 1px rgba(0, 0, 0, 0.1)
border-radius 4px border-radius 4px
transition top 0.1s ease, left 0.1s ease
> ol > ol
display block display block

View file

@ -6,7 +6,6 @@ export default {
const self = el._autoCompleteDirective_ = {} as any; const self = el._autoCompleteDirective_ = {} as any;
self.x = new Autocomplete(el, vn.context, binding.value); self.x = new Autocomplete(el, vn.context, binding.value);
self.x.attach(); self.x.attach();
console.log(vn.context);
}, },
unbind(el, binding, vn) { unbind(el, binding, vn) {
@ -23,6 +22,7 @@ class Autocomplete {
private textarea: any; private textarea: any;
private vm: any; private vm: any;
private model: any; private model: any;
private currentType: string;
private get text(): string { private get text(): string {
return this.vm[this.model]; return this.vm[this.model];
@ -67,24 +67,32 @@ class Autocomplete {
* *
*/ */
private onInput() { private onInput() {
this.close();
const caret = this.textarea.selectionStart; const caret = this.textarea.selectionStart;
const text = this.text.substr(0, caret); const text = this.text.substr(0, caret);
const mentionIndex = text.lastIndexOf('@'); const mentionIndex = text.lastIndexOf('@');
const emojiIndex = text.lastIndexOf(':'); const emojiIndex = text.lastIndexOf(':');
let opened = false;
if (mentionIndex != -1 && mentionIndex > emojiIndex) { 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 != '' && username.match(/^[a-zA-Z0-9-]+$/)) {
this.open('user', username); this.open('user', username);
opened = true;
}
} }
if (emojiIndex != -1 && emojiIndex > mentionIndex) { if (emojiIndex != -1 && emojiIndex > mentionIndex) {
const emoji = text.substr(emojiIndex + 1); const emoji = text.substr(emojiIndex + 1);
if (!emoji.match(/^[\+\-a-z0-9_]+$/)) return; if (emoji != '' && emoji.match(/^[\+\-a-z0-9_]+$/)) {
this.open('emoji', emoji); this.open('emoji', emoji);
opened = true;
}
}
if (!opened) {
this.close();
} }
} }
@ -92,8 +100,10 @@ class Autocomplete {
* *
*/ */
private open(type, q) { private open(type, q) {
// 既に開いているサジェストは閉じる if (type != this.currentType) {
this.close(); this.close();
}
this.currentType = type;
//#region サジェストを表示すべき位置を計算 //#region サジェストを表示すべき位置を計算
const caretPosition = getCaretCoordinates(this.textarea, this.textarea.selectionStart); const caretPosition = getCaretCoordinates(this.textarea, this.textarea.selectionStart);
@ -104,21 +114,27 @@ class Autocomplete {
const y = rect.top + caretPosition.top - this.textarea.scrollTop; const y = rect.top + caretPosition.top - this.textarea.scrollTop;
//#endregion //#endregion
// サジェスト要素作成 if (this.suggestion) {
this.suggestion = new MkAutocomplete({ this.suggestion.x = x;
propsData: { this.suggestion.y = y;
textarea: this.textarea, this.suggestion.q = q;
complete: this.complete, } else {
close: this.close, // サジェスト要素作成
type: type, this.suggestion = new MkAutocomplete({
q: q, propsData: {
x, textarea: this.textarea,
y complete: this.complete,
} close: this.close,
}).$mount(); type: type,
q: q,
x,
y
}
}).$mount();
// 要素追加 // 要素追加
document.body.appendChild(this.suggestion.$el); document.body.appendChild(this.suggestion.$el);
}
} }
/** /**