2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-12 03:37:45 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-01 15:51:07 +02:00
|
|
|
import { defineAsyncComponent, Directive, ref } from 'vue';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { popup } from '@/os.js';
|
2018-02-16 18:24:10 +01:00
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
export class UserPreview {
|
|
|
|
private el;
|
|
|
|
private user;
|
|
|
|
private showTimer;
|
|
|
|
private hideTimer;
|
|
|
|
private checkTimer;
|
|
|
|
private promise;
|
|
|
|
|
|
|
|
constructor(el, user) {
|
|
|
|
this.el = el;
|
|
|
|
this.user = user;
|
|
|
|
|
2023-03-31 09:41:27 +02:00
|
|
|
this.show = this.show.bind(this);
|
|
|
|
this.close = this.close.bind(this);
|
|
|
|
this.onMouseover = this.onMouseover.bind(this);
|
|
|
|
this.onMouseleave = this.onMouseleave.bind(this);
|
|
|
|
this.onClick = this.onClick.bind(this);
|
|
|
|
this.attach = this.attach.bind(this);
|
|
|
|
this.detach = this.detach.bind(this);
|
2023-04-09 02:44:00 +02:00
|
|
|
|
|
|
|
this.attach();
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 07:22:55 +01:00
|
|
|
private show() {
|
2020-10-17 13:12:00 +02:00
|
|
|
if (!document.body.contains(this.el)) return;
|
|
|
|
if (this.promise) return;
|
|
|
|
|
|
|
|
const showing = ref(true);
|
2018-02-16 18:24:10 +01:00
|
|
|
|
2023-03-03 04:29:34 +01:00
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkUserPopup.vue')), {
|
2020-10-17 13:12:00 +02:00
|
|
|
showing,
|
|
|
|
q: this.user,
|
2022-12-22 08:01:59 +01:00
|
|
|
source: this.el,
|
2020-10-17 13:12:00 +02:00
|
|
|
}, {
|
|
|
|
mouseover: () => {
|
2022-01-16 02:14:14 +01:00
|
|
|
window.clearTimeout(this.hideTimer);
|
2020-10-17 13:12:00 +02:00
|
|
|
},
|
|
|
|
mouseleave: () => {
|
2022-01-16 02:14:14 +01:00
|
|
|
window.clearTimeout(this.showTimer);
|
|
|
|
this.hideTimer = window.setTimeout(this.close, 500);
|
2020-10-17 13:12:00 +02:00
|
|
|
},
|
|
|
|
}, 'closed');
|
|
|
|
|
|
|
|
this.promise = {
|
|
|
|
cancel: () => {
|
|
|
|
showing.value = false;
|
2022-12-22 08:01:59 +01:00
|
|
|
},
|
2018-02-16 18:24:10 +01:00
|
|
|
};
|
|
|
|
|
2022-01-16 02:14:14 +01:00
|
|
|
this.checkTimer = window.setInterval(() => {
|
2020-10-17 13:12:00 +02:00
|
|
|
if (!document.body.contains(this.el)) {
|
2022-01-16 02:14:14 +01:00
|
|
|
window.clearTimeout(this.showTimer);
|
|
|
|
window.clearTimeout(this.hideTimer);
|
2020-10-17 13:12:00 +02:00
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
private close() {
|
|
|
|
if (this.promise) {
|
2022-01-16 02:14:14 +01:00
|
|
|
window.clearInterval(this.checkTimer);
|
2020-10-17 13:12:00 +02:00
|
|
|
this.promise.cancel();
|
|
|
|
this.promise = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private onMouseover() {
|
2022-01-16 02:14:14 +01:00
|
|
|
window.clearTimeout(this.showTimer);
|
|
|
|
window.clearTimeout(this.hideTimer);
|
|
|
|
this.showTimer = window.setTimeout(this.show, 500);
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private onMouseleave() {
|
2022-01-16 02:14:14 +01:00
|
|
|
window.clearTimeout(this.showTimer);
|
|
|
|
window.clearTimeout(this.hideTimer);
|
|
|
|
this.hideTimer = window.setTimeout(this.close, 500);
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private onClick() {
|
2022-01-16 02:14:14 +01:00
|
|
|
window.clearTimeout(this.showTimer);
|
2020-10-17 13:12:00 +02:00
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public attach() {
|
|
|
|
this.el.addEventListener('mouseover', this.onMouseover);
|
|
|
|
this.el.addEventListener('mouseleave', this.onMouseleave);
|
|
|
|
this.el.addEventListener('click', this.onClick);
|
|
|
|
}
|
2018-02-16 18:24:10 +01:00
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
public detach() {
|
|
|
|
this.el.removeEventListener('mouseover', this.onMouseover);
|
|
|
|
this.el.removeEventListener('mouseleave', this.onMouseleave);
|
|
|
|
this.el.removeEventListener('click', this.onClick);
|
2022-01-16 02:14:14 +01:00
|
|
|
window.clearInterval(this.checkTimer);
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
mounted(el: HTMLElement, binding, vn) {
|
|
|
|
if (binding.value == null) return;
|
|
|
|
|
|
|
|
// TODO: 新たにプロパティを作るのをやめMapを使う
|
|
|
|
// ただメモリ的には↓の方が省メモリかもしれないので検討中
|
|
|
|
const self = (el as any)._userPreviewDirective_ = {} as any;
|
|
|
|
|
|
|
|
self.preview = new UserPreview(el, binding.value);
|
2018-02-16 18:24:10 +01:00
|
|
|
},
|
2018-02-18 14:16:36 +01:00
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
unmounted(el, binding, vn) {
|
|
|
|
if (binding.value == null) return;
|
|
|
|
|
2018-02-19 06:29:42 +01:00
|
|
|
const self = el._userPreviewDirective_;
|
2020-10-17 13:12:00 +02:00
|
|
|
self.preview.detach();
|
2022-12-22 08:01:59 +01:00
|
|
|
},
|
2020-10-17 13:12:00 +02:00
|
|
|
} as Directive;
|