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