2020-10-17 11:12:00 +00:00
|
|
|
import { Directive } from 'vue';
|
|
|
|
|
2022-02-06 01:59:36 +00:00
|
|
|
type Value = { max?: number[]; min?: number[]; };
|
|
|
|
|
2020-10-17 11:12:00 +00:00
|
|
|
//const observers = new Map<Element, ResizeObserver>();
|
2022-02-06 01:59:36 +00:00
|
|
|
const mountings = new Map<Element, {
|
|
|
|
value: Value;
|
|
|
|
resize: ResizeObserver;
|
|
|
|
intersection?: IntersectionObserver;
|
|
|
|
previousWidth: number;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
type ClassOrder = {
|
|
|
|
add: string[];
|
|
|
|
remove: string[];
|
|
|
|
};
|
|
|
|
|
2022-12-25 23:40:13 +00:00
|
|
|
const isContainerQueriesSupported = ('container' in document.documentElement.style);
|
|
|
|
|
2022-02-06 01:59:36 +00:00
|
|
|
const cache = new Map<string, ClassOrder>();
|
|
|
|
|
|
|
|
function getClassOrder(width: number, queue: Value): ClassOrder {
|
|
|
|
const getMaxClass = (v: number) => `max-width_${v}px`;
|
|
|
|
const getMinClass = (v: number) => `min-width_${v}px`;
|
|
|
|
|
|
|
|
return {
|
|
|
|
add: [
|
|
|
|
...(queue.max ? queue.max.filter(v => width <= v).map(getMaxClass) : []),
|
|
|
|
...(queue.min ? queue.min.filter(v => width >= v).map(getMinClass) : []),
|
|
|
|
],
|
|
|
|
remove: [
|
2022-07-10 10:47:29 +00:00
|
|
|
...(queue.max ? queue.max.filter(v => width > v).map(getMaxClass) : []),
|
|
|
|
...(queue.min ? queue.min.filter(v => width < v).map(getMinClass) : []),
|
2022-12-22 07:01:59 +00:00
|
|
|
],
|
2022-02-06 01:59:36 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function applyClassOrder(el: Element, order: ClassOrder) {
|
|
|
|
el.classList.add(...order.add);
|
|
|
|
el.classList.remove(...order.remove);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOrderName(width: number, queue: Value): string {
|
|
|
|
return `${width}|${queue.max ? queue.max.join(',') : ''}|${queue.min ? queue.min.join(',') : ''}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function calc(el: Element) {
|
|
|
|
const info = mountings.get(el);
|
|
|
|
const width = el.clientWidth;
|
|
|
|
|
|
|
|
if (!info || info.previousWidth === width) return;
|
|
|
|
|
|
|
|
// アクティベート前などでsrcが描画されていない場合
|
|
|
|
if (!width) {
|
|
|
|
// IntersectionObserverで表示検出する
|
|
|
|
if (!info.intersection) {
|
|
|
|
info.intersection = new IntersectionObserver(entries => {
|
|
|
|
if (entries.some(entry => entry.isIntersecting)) calc(el);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
info.intersection.observe(el);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (info.intersection) {
|
2022-06-10 05:36:55 +00:00
|
|
|
info.intersection.disconnect();
|
2022-02-06 01:59:36 +00:00
|
|
|
delete info.intersection;
|
2022-06-10 05:36:55 +00:00
|
|
|
}
|
2022-02-06 01:59:36 +00:00
|
|
|
|
|
|
|
mountings.set(el, Object.assign(info, { previousWidth: width }));
|
|
|
|
|
|
|
|
const cached = cache.get(getOrderName(width, info.value));
|
|
|
|
if (cached) {
|
|
|
|
applyClassOrder(el, cached);
|
|
|
|
} else {
|
|
|
|
const order = getClassOrder(width, info.value);
|
|
|
|
cache.set(getOrderName(width, info.value), order);
|
|
|
|
applyClassOrder(el, order);
|
|
|
|
}
|
|
|
|
}
|
2020-10-17 11:12:00 +00:00
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
export default {
|
2020-10-17 11:12:00 +00:00
|
|
|
mounted(src, binding, vn) {
|
2022-12-25 23:40:13 +00:00
|
|
|
if (isContainerQueriesSupported) return;
|
|
|
|
|
2022-02-06 01:59:36 +00:00
|
|
|
const resize = new ResizeObserver((entries, observer) => {
|
|
|
|
calc(src);
|
|
|
|
});
|
|
|
|
|
|
|
|
mountings.set(src, {
|
|
|
|
value: binding.value,
|
|
|
|
resize,
|
|
|
|
previousWidth: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
calc(src);
|
|
|
|
resize.observe(src);
|
|
|
|
},
|
|
|
|
|
|
|
|
updated(src, binding, vn) {
|
2022-12-25 23:40:13 +00:00
|
|
|
if (isContainerQueriesSupported) return;
|
|
|
|
|
2022-02-06 01:59:36 +00:00
|
|
|
mountings.set(src, Object.assign({}, mountings.get(src), { value: binding.value }));
|
|
|
|
calc(src);
|
2020-10-17 11:12:00 +00:00
|
|
|
},
|
2020-08-09 06:51:02 +00:00
|
|
|
|
2020-10-17 11:12:00 +00:00
|
|
|
unmounted(src, binding, vn) {
|
2022-12-25 23:40:13 +00:00
|
|
|
if (isContainerQueriesSupported) return;
|
|
|
|
|
2022-02-06 01:59:36 +00:00
|
|
|
const info = mountings.get(src);
|
|
|
|
if (!info) return;
|
|
|
|
info.resize.disconnect();
|
|
|
|
if (info.intersection) info.intersection.disconnect();
|
|
|
|
mountings.delete(src);
|
2022-12-22 07:01:59 +00:00
|
|
|
},
|
2022-02-06 01:59:36 +00:00
|
|
|
} as Directive<Element, Value>;
|