wip
This commit is contained in:
parent
0b1bcf614b
commit
8056497809
3 changed files with 31 additions and 22 deletions
|
|
@ -9,6 +9,7 @@
|
|||
<textarea :class="{ with: (files.length != 0 || poll) }"
|
||||
ref="text" v-model="text" :disabled="posting"
|
||||
@keydown="onKeydown" @paste="onPaste" :placeholder="placeholder"
|
||||
v-autocomplete
|
||||
></textarea>
|
||||
<div class="medias" :class="{ with: poll }" v-show="files.length != 0">
|
||||
<x-draggable :list="files" :options="{ animation: 150 }">
|
||||
|
|
@ -38,7 +39,6 @@
|
|||
<script lang="ts">
|
||||
import Vue from 'vue';
|
||||
import * as XDraggable from 'vuedraggable';
|
||||
import Autocomplete from '../../scripts/autocomplete';
|
||||
import getKao from '../../../common/scripts/get-kao';
|
||||
|
||||
export default Vue.extend({
|
||||
|
|
@ -96,9 +96,6 @@ export default Vue.extend({
|
|||
},
|
||||
mounted() {
|
||||
this.$nextTick(() => {
|
||||
this.autocomplete = new Autocomplete(this.$refs.text);
|
||||
this.autocomplete.attach();
|
||||
|
||||
// 書きかけの投稿を復元
|
||||
const draft = JSON.parse(localStorage.getItem('drafts') || '{}')[this.draftId];
|
||||
if (draft) {
|
||||
|
|
|
|||
142
src/web/app/desktop/views/directives/autocomplete.ts
Normal file
142
src/web/app/desktop/views/directives/autocomplete.ts
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
import * as getCaretCoordinates from 'textarea-caret';
|
||||
import MkAutocomplete from '../components/autocomplete.vue';
|
||||
|
||||
export default {
|
||||
bind(el, binding, vn) {
|
||||
const self = el._userPreviewDirective_ = {} as any;
|
||||
self.x = new Autocomplete(el);
|
||||
self.x.attach();
|
||||
},
|
||||
|
||||
unbind(el, binding, vn) {
|
||||
const self = el._userPreviewDirective_;
|
||||
self.x.close();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* オートコンプリートを管理するクラス。
|
||||
*/
|
||||
class Autocomplete {
|
||||
private suggestion: any;
|
||||
private textarea: any;
|
||||
|
||||
/**
|
||||
* 対象のテキストエリアを与えてインスタンスを初期化します。
|
||||
*/
|
||||
constructor(textarea) {
|
||||
// BIND ---------------------------------
|
||||
this.onInput = this.onInput.bind(this);
|
||||
this.complete = this.complete.bind(this);
|
||||
this.close = this.close.bind(this);
|
||||
// --------------------------------------
|
||||
|
||||
this.suggestion = null;
|
||||
this.textarea = textarea;
|
||||
}
|
||||
|
||||
/**
|
||||
* このインスタンスにあるテキストエリアの入力のキャプチャを開始します。
|
||||
*/
|
||||
public attach() {
|
||||
this.textarea.addEventListener('input', this.onInput);
|
||||
}
|
||||
|
||||
/**
|
||||
* このインスタンスにあるテキストエリアの入力のキャプチャを解除します。
|
||||
*/
|
||||
public detach() {
|
||||
this.textarea.removeEventListener('input', this.onInput);
|
||||
this.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* テキスト入力時
|
||||
*/
|
||||
private onInput() {
|
||||
this.close();
|
||||
|
||||
const caret = this.textarea.selectionStart;
|
||||
const text = this.textarea.value.substr(0, caret);
|
||||
|
||||
const mentionIndex = text.lastIndexOf('@');
|
||||
|
||||
if (mentionIndex == -1) return;
|
||||
|
||||
const username = text.substr(mentionIndex + 1);
|
||||
|
||||
if (!username.match(/^[a-zA-Z0-9-]+$/)) return;
|
||||
|
||||
this.open('user', username);
|
||||
}
|
||||
|
||||
/**
|
||||
* サジェストを提示します。
|
||||
*/
|
||||
private open(type, q) {
|
||||
// 既に開いているサジェストは閉じる
|
||||
this.close();
|
||||
|
||||
// サジェスト要素作成
|
||||
this.suggestion = new MkAutocomplete({
|
||||
propsData: {
|
||||
textarea: this.textarea,
|
||||
complete: this.complete,
|
||||
close: this.close,
|
||||
type: type,
|
||||
q: q
|
||||
}
|
||||
}).$mount();
|
||||
|
||||
// ~ サジェストを表示すべき位置を計算 ~
|
||||
|
||||
const caretPosition = getCaretCoordinates(this.textarea, this.textarea.selectionStart);
|
||||
|
||||
const rect = this.textarea.getBoundingClientRect();
|
||||
|
||||
const x = rect.left + window.pageXOffset + caretPosition.left;
|
||||
const y = rect.top + window.pageYOffset + caretPosition.top;
|
||||
|
||||
this.suggestion.$el.style.left = x + 'px';
|
||||
this.suggestion.$el.style.top = y + 'px';
|
||||
|
||||
// 要素追加
|
||||
document.body.appendChild(this.suggestion.$el);
|
||||
}
|
||||
|
||||
/**
|
||||
* サジェストを閉じます。
|
||||
*/
|
||||
private close() {
|
||||
if (this.suggestion == null) return;
|
||||
|
||||
this.suggestion.$destroy();
|
||||
this.suggestion = null;
|
||||
|
||||
this.textarea.focus();
|
||||
}
|
||||
|
||||
/**
|
||||
* オートコンプリートする
|
||||
*/
|
||||
private complete(user) {
|
||||
this.close();
|
||||
|
||||
const value = user.username;
|
||||
|
||||
const caret = this.textarea.selectionStart;
|
||||
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();
|
||||
const pos = caret + value.length;
|
||||
this.textarea.setSelectionRange(pos, pos);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
import Vue from 'vue';
|
||||
|
||||
import userPreview from './user-preview';
|
||||
import autocomplete from './autocomplete';
|
||||
|
||||
Vue.directive('userPreview', userPreview);
|
||||
Vue.directive('user-preview', userPreview);
|
||||
Vue.directive('autocomplete', autocomplete);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue