Co-authored-by: MeiMei <30769358+mei23@users.noreply.github.com>
Co-authored-by: Satsuki Yanagi <17376330+u1-liquid@users.noreply.github.com>
This commit is contained in:
syuilo
2020-01-30 04:37:25 +09:00
committed by GitHub
parent a5955c1123
commit f6154dc0af
871 changed files with 26140 additions and 71950 deletions

View File

@@ -0,0 +1,248 @@
import * as getCaretCoordinates from 'textarea-caret';
import { toASCII } from 'punycode';
export default {
bind(el, binding, vn) {
const self = el._autoCompleteDirective_ = {} as any;
self.x = new Autocomplete(el, vn.context, binding.value);
self.x.attach();
},
unbind(el, binding, vn) {
const self = el._autoCompleteDirective_;
self.x.detach();
}
};
/**
* オートコンプリートを管理するクラス。
*/
class Autocomplete {
private suggestion: any;
private textarea: any;
private vm: any;
private currentType: string;
private opts: {
model: string;
};
private opening: boolean;
private get text(): string {
return this.vm[this.opts.model];
}
private set text(text: string) {
this.vm[this.opts.model] = text;
}
/**
* 対象のテキストエリアを与えてインスタンスを初期化します。
*/
constructor(textarea, vm, opts) {
//#region BIND
this.onInput = this.onInput.bind(this);
this.complete = this.complete.bind(this);
this.close = this.close.bind(this);
//#endregion
this.suggestion = null;
this.textarea = textarea;
this.vm = vm;
this.opts = opts;
this.opening = false;
}
/**
* このインスタンスにあるテキストエリアの入力のキャプチャを開始します。
*/
public attach() {
this.textarea.addEventListener('input', this.onInput);
}
/**
* このインスタンスにあるテキストエリアの入力のキャプチャを解除します。
*/
public detach() {
this.textarea.removeEventListener('input', this.onInput);
this.close();
}
/**
* テキスト入力時
*/
private onInput() {
const caretPos = this.textarea.selectionStart;
const text = this.text.substr(0, caretPos).split('\n').pop();
const mentionIndex = text.lastIndexOf('@');
const hashtagIndex = text.lastIndexOf('#');
const emojiIndex = text.lastIndexOf(':');
const max = Math.max(
mentionIndex,
hashtagIndex,
emojiIndex);
if (max == -1) {
this.close();
return;
}
const isMention = mentionIndex != -1;
const isHashtag = hashtagIndex != -1;
const isEmoji = emojiIndex != -1;
let opened = false;
if (isMention) {
const username = text.substr(mentionIndex + 1);
if (username != '' && username.match(/^[a-zA-Z0-9_]+$/)) {
this.open('user', username);
opened = true;
}
}
if (isHashtag && !opened) {
const hashtag = text.substr(hashtagIndex + 1);
if (!hashtag.includes(' ')) {
this.open('hashtag', hashtag);
opened = true;
}
}
if (isEmoji && !opened) {
const emoji = text.substr(emojiIndex + 1);
if (!emoji.includes(' ')) {
this.open('emoji', emoji);
opened = true;
}
}
if (!opened) {
this.close();
}
}
/**
* サジェストを提示します。
*/
private async open(type, q) {
if (type != this.currentType) {
this.close();
}
if (this.opening) return;
this.opening = true;
this.currentType = type;
//#region サジェストを表示すべき位置を計算
const caretPosition = getCaretCoordinates(this.textarea, this.textarea.selectionStart);
const rect = this.textarea.getBoundingClientRect();
const x = rect.left + caretPosition.left - this.textarea.scrollLeft;
const y = rect.top + caretPosition.top - this.textarea.scrollTop;
//#endregion
if (this.suggestion) {
this.suggestion.x = x;
this.suggestion.y = y;
this.suggestion.q = q;
this.opening = false;
} else {
const MkAutocomplete = await import('../components/autocomplete.vue').then(m => m.default);
// サジェスト要素作成
this.suggestion = new MkAutocomplete({
parent: this.vm,
propsData: {
textarea: this.textarea,
complete: this.complete,
close: this.close,
type: type,
q: q,
x,
y
}
}).$mount();
// 要素追加
document.body.appendChild(this.suggestion.$el);
this.opening = false;
}
}
/**
* サジェストを閉じます。
*/
private close() {
if (this.suggestion == null) return;
this.suggestion.destroyDom();
this.suggestion = null;
this.textarea.focus();
}
/**
* オートコンプリートする
*/
private complete(type, value) {
this.close();
const caret = this.textarea.selectionStart;
if (type == 'user') {
const source = this.text;
const before = source.substr(0, caret);
const trimmedBefore = before.substring(0, before.lastIndexOf('@'));
const after = source.substr(caret);
const acct = value.host === null ? value.username : `${value.username}@${toASCII(value.host)}`;
// 挿入
this.text = `${trimmedBefore}@${acct} ${after}`;
// キャレットを戻す
this.vm.$nextTick(() => {
this.textarea.focus();
const pos = trimmedBefore.length + (acct.length + 2);
this.textarea.setSelectionRange(pos, pos);
});
} else if (type == 'hashtag') {
const source = this.text;
const before = source.substr(0, caret);
const trimmedBefore = before.substring(0, before.lastIndexOf('#'));
const after = source.substr(caret);
// 挿入
this.text = `${trimmedBefore}#${value} ${after}`;
// キャレットを戻す
this.vm.$nextTick(() => {
this.textarea.focus();
const pos = trimmedBefore.length + (value.length + 2);
this.textarea.setSelectionRange(pos, pos);
});
} else if (type == 'emoji') {
const source = this.text;
const before = source.substr(0, caret);
const trimmedBefore = before.substring(0, before.lastIndexOf(':'));
const after = source.substr(caret);
// 挿入
this.text = trimmedBefore + value + after;
// キャレットを戻す
this.vm.$nextTick(() => {
this.textarea.focus();
const pos = trimmedBefore.length + value.length;
this.textarea.setSelectionRange(pos, pos);
});
}
}
}

View File

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

View File

@@ -0,0 +1,63 @@
export default {
inserted(el, binding) {
const query = binding.value;
/*
const addClassRecursive = (el: Element, cls: string) => {
el.classList.add(cls);
if (el.children) {
for (const child of el.children) {
addClassRecursive(child, cls);
}
}
};
const removeClassRecursive = (el: Element, cls: string) => {
el.classList.remove(cls);
if (el.children) {
for (const child of el.children) {
removeClassRecursive(child, cls);
}
}
};*/
const addClass = (el: Element, cls: string) => {
el.classList.add(cls);
};
const removeClass = (el: Element, cls: string) => {
el.classList.remove(cls);
};
const calc = () => {
const width = el.clientWidth;
for (const q of query) {
if (q.max) {
if (width <= q.max) {
addClass(el, 'max-width_' + q.max + 'px');
} else {
removeClass(el, 'max-width_' + q.max + 'px');
}
}
if (q.min) {
if (width >= q.min) {
addClass(el, 'min-width_' + q.min + 'px');
} else {
removeClass(el, 'min-width_' + q.min + 'px');
}
}
}
};
calc();
el._sizeResizeCb_ = calc;
window.addEventListener('resize', calc);
},
unbind(el, binding, vn) {
window.removeEventListener('resize', el._sizeResizeCb_);
}
};

View File

@@ -0,0 +1,61 @@
import MkUserPreview from '../components/user-preview.vue';
export default {
bind(el: HTMLElement, binding, vn) {
const self = (el as any)._userPreviewDirective_ = {} as any;
self.user = binding.value;
self.tag = null;
self.showTimer = null;
self.hideTimer = null;
self.close = () => {
if (self.tag) {
self.tag.close();
self.tag = null;
}
};
const show = () => {
if (self.tag) return;
self.tag = new MkUserPreview({
parent: vn.context,
propsData: {
user: self.user,
source: el
}
}).$mount();
self.tag.$on('mouseover', () => {
clearTimeout(self.hideTimer);
});
self.tag.$on('mouseleave', () => {
clearTimeout(self.showTimer);
self.hideTimer = setTimeout(self.close, 500);
});
document.body.appendChild(self.tag.$el);
};
el.addEventListener('mouseover', () => {
clearTimeout(self.showTimer);
clearTimeout(self.hideTimer);
self.showTimer = setTimeout(show, 500);
});
el.addEventListener('mouseleave', () => {
clearTimeout(self.showTimer);
clearTimeout(self.hideTimer);
self.hideTimer = setTimeout(self.close, 500);
});
},
unbind(el, binding, vn) {
const self = el._userPreviewDirective_;
clearTimeout(self.showTimer);
clearTimeout(self.hideTimer);
self.close();
}
};