2023-07-27 08:31:52 +03:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2021-12-02 13:58:23 +02:00
|
|
|
// TODO: useTooltip関数使うようにしたい
|
|
|
|
// ただディレクティブ内でonUnmountedなどのcomposition api使えるのか不明
|
|
|
|
|
2022-05-01 16:51:07 +03:00
|
|
|
import { defineAsyncComponent, Directive, ref } from 'vue';
|
2023-09-19 10:37:43 +03:00
|
|
|
import { isTouchUsing } from '@/scripts/touch.js';
|
|
|
|
import { popup, alert } from '@/os.js';
|
2020-06-03 07:30:17 +03:00
|
|
|
|
2023-05-15 08:29:35 +03:00
|
|
|
const start = isTouchUsing ? 'touchstart' : 'mouseenter';
|
2021-12-05 06:10:19 +02:00
|
|
|
const end = isTouchUsing ? 'touchend' : 'mouseleave';
|
2020-06-03 07:30:17 +03:00
|
|
|
|
|
|
|
export default {
|
2020-10-17 14:12:00 +03:00
|
|
|
mounted(el: HTMLElement, binding, vn) {
|
2022-07-16 07:49:23 +03:00
|
|
|
const delay = binding.modifiers.noDelay ? 0 : 100;
|
|
|
|
|
2020-06-03 07:30:17 +03:00
|
|
|
const self = (el as any)._tooltipDirective_ = {} as any;
|
|
|
|
|
|
|
|
self.text = binding.value as string;
|
2020-10-17 14:12:00 +03:00
|
|
|
self._close = null;
|
2020-06-03 07:30:17 +03:00
|
|
|
self.showTimer = null;
|
|
|
|
self.hideTimer = null;
|
|
|
|
self.checkTimer = null;
|
|
|
|
|
|
|
|
self.close = () => {
|
2020-10-17 14:12:00 +03:00
|
|
|
if (self._close) {
|
2022-01-16 03:14:14 +02:00
|
|
|
window.clearInterval(self.checkTimer);
|
2020-10-17 14:12:00 +03:00
|
|
|
self._close();
|
|
|
|
self._close = null;
|
2020-06-03 07:30:17 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-22 07:16:15 +03:00
|
|
|
if (binding.arg === 'dialog') {
|
|
|
|
el.addEventListener('click', (ev) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
2021-11-18 16:36:04 +02:00
|
|
|
alert({
|
2021-08-22 10:18:53 +03:00
|
|
|
type: 'info',
|
2021-08-22 07:16:15 +03:00
|
|
|
text: binding.value,
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-29 21:07:47 +03:00
|
|
|
self.show = () => {
|
2020-06-03 07:30:17 +03:00
|
|
|
if (!document.body.contains(el)) return;
|
2020-10-17 14:12:00 +03:00
|
|
|
if (self._close) return;
|
|
|
|
if (self.text == null) return;
|
2020-06-03 07:30:17 +03:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
const showing = ref(true);
|
2022-09-06 12:21:49 +03:00
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkTooltip.vue')), {
|
2020-10-17 14:12:00 +03:00
|
|
|
showing,
|
|
|
|
text: self.text,
|
2022-06-16 10:05:43 +03:00
|
|
|
asMfm: binding.modifiers.mfm,
|
2022-07-05 10:07:53 +03:00
|
|
|
direction: binding.modifiers.left ? 'left' : binding.modifiers.right ? 'right' : binding.modifiers.top ? 'top' : binding.modifiers.bottom ? 'bottom' : 'top',
|
2022-01-31 14:07:33 +02:00
|
|
|
targetElement: el,
|
2020-10-17 14:12:00 +03:00
|
|
|
}, {}, 'closed');
|
2020-06-03 07:30:17 +03:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
self._close = () => {
|
|
|
|
showing.value = false;
|
|
|
|
};
|
2020-06-03 07:30:17 +03:00
|
|
|
};
|
|
|
|
|
2022-01-31 14:07:33 +02:00
|
|
|
el.addEventListener('selectstart', ev => {
|
|
|
|
ev.preventDefault();
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
|
|
|
|
2023-05-15 08:29:35 +03:00
|
|
|
el.addEventListener(start, (ev) => {
|
2022-01-16 03:14:14 +02:00
|
|
|
window.clearTimeout(self.showTimer);
|
|
|
|
window.clearTimeout(self.hideTimer);
|
2023-05-15 08:29:35 +03:00
|
|
|
if (delay === 0) {
|
|
|
|
self.show();
|
|
|
|
} else {
|
|
|
|
self.showTimer = window.setTimeout(self.show, delay);
|
|
|
|
}
|
2020-10-17 14:12:00 +03:00
|
|
|
}, { passive: true });
|
2020-06-03 07:30:17 +03:00
|
|
|
|
|
|
|
el.addEventListener(end, () => {
|
2022-01-16 03:14:14 +02:00
|
|
|
window.clearTimeout(self.showTimer);
|
|
|
|
window.clearTimeout(self.hideTimer);
|
2023-05-15 08:29:35 +03:00
|
|
|
if (delay === 0) {
|
|
|
|
self.close();
|
|
|
|
} else {
|
|
|
|
self.hideTimer = window.setTimeout(self.close, delay);
|
|
|
|
}
|
2020-10-17 14:12:00 +03:00
|
|
|
}, { passive: true });
|
2020-06-03 07:30:17 +03:00
|
|
|
|
|
|
|
el.addEventListener('click', () => {
|
2022-01-16 03:14:14 +02:00
|
|
|
window.clearTimeout(self.showTimer);
|
2020-06-03 07:30:17 +03:00
|
|
|
self.close();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-09-29 21:07:47 +03:00
|
|
|
updated(el, binding) {
|
|
|
|
const self = el._tooltipDirective_;
|
|
|
|
self.text = binding.value as string;
|
|
|
|
},
|
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
unmounted(el, binding, vn) {
|
2020-06-03 07:30:17 +03:00
|
|
|
const self = el._tooltipDirective_;
|
2022-01-16 03:14:14 +02:00
|
|
|
window.clearInterval(self.checkTimer);
|
2020-06-03 07:30:17 +03:00
|
|
|
},
|
2020-10-17 14:12:00 +03:00
|
|
|
} as Directive;
|