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
|
|
|
|
*/
|
|
|
|
|
2021-10-08 15:03:06 +02:00
|
|
|
import { Directive } from 'vue';
|
|
|
|
|
2022-02-06 02:59:36 +01:00
|
|
|
const mountings = new Map<Element, {
|
|
|
|
resize: ResizeObserver;
|
|
|
|
intersection?: IntersectionObserver;
|
|
|
|
fn: (w: number, h: number) => void;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
function calc(src: Element) {
|
|
|
|
const info = mountings.get(src);
|
|
|
|
const height = src.clientHeight;
|
|
|
|
const width = src.clientWidth;
|
|
|
|
|
|
|
|
if (!info) return;
|
|
|
|
|
|
|
|
// アクティベート前などでsrcが描画されていない場合
|
|
|
|
if (!height) {
|
|
|
|
// IntersectionObserverで表示検出する
|
|
|
|
if (!info.intersection) {
|
|
|
|
info.intersection = new IntersectionObserver(entries => {
|
|
|
|
if (entries.some(entry => entry.isIntersecting)) calc(src);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
info.intersection.observe(src);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (info.intersection) {
|
2022-06-10 07:36:55 +02:00
|
|
|
info.intersection.disconnect();
|
2022-02-06 02:59:36 +01:00
|
|
|
delete info.intersection;
|
2022-06-10 07:36:55 +02:00
|
|
|
}
|
2021-10-08 15:03:06 +02:00
|
|
|
|
2022-02-06 02:59:36 +01:00
|
|
|
info.fn(width, height);
|
2022-06-10 07:36:55 +02:00
|
|
|
}
|
2021-10-08 15:03:06 +02:00
|
|
|
|
2022-02-06 02:59:36 +01:00
|
|
|
export default {
|
|
|
|
mounted(src, binding, vn) {
|
|
|
|
const resize = new ResizeObserver((entries, observer) => {
|
|
|
|
calc(src);
|
2021-10-08 15:03:06 +02:00
|
|
|
});
|
2022-02-06 02:59:36 +01:00
|
|
|
resize.observe(src);
|
2021-10-08 15:03:06 +02:00
|
|
|
|
2022-12-22 08:01:59 +01:00
|
|
|
mountings.set(src, { resize, fn: binding.value });
|
2022-02-06 02:59:36 +01:00
|
|
|
calc(src);
|
2021-10-08 15:03:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
unmounted(src, binding, vn) {
|
|
|
|
binding.value(0, 0);
|
2022-02-06 02:59:36 +01:00
|
|
|
const info = mountings.get(src);
|
|
|
|
if (!info) return;
|
|
|
|
info.resize.disconnect();
|
|
|
|
if (info.intersection) info.intersection.disconnect();
|
|
|
|
mountings.delete(src);
|
2022-12-22 08:01:59 +01:00
|
|
|
},
|
2022-02-06 02:59:36 +01:00
|
|
|
} as Directive<Element, (w: number, h: number) => void>;
|