2020-12-30 01:32:46 +00:00
|
|
|
import {
|
|
|
|
Ref,
|
|
|
|
ref,
|
|
|
|
inject,
|
|
|
|
computed,
|
2022-03-11 16:50:50 +00:00
|
|
|
onBeforeUnmount,
|
2020-12-30 01:32:46 +00:00
|
|
|
} from 'vue';
|
|
|
|
|
2021-03-08 15:29:35 +00:00
|
|
|
export const SymActiveBlock = Symbol('Schlechtenburg active block');
|
|
|
|
export function useActivation(currentBlockId: string|null = null) {
|
|
|
|
const activeBlockId: Ref<string|null> = inject(SymActiveBlock, ref(null));
|
2022-03-11 16:50:50 +00:00
|
|
|
|
2020-12-30 01:32:46 +00:00
|
|
|
const isActive = computed(() => activeBlockId.value === currentBlockId);
|
2022-03-11 16:50:50 +00:00
|
|
|
|
|
|
|
const deactivate = (id: string|null = currentBlockId) => {
|
|
|
|
if (activeBlockId.value !== id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
activeBlockId.value = null;
|
2020-12-30 01:32:46 +00:00
|
|
|
};
|
2022-03-11 16:50:50 +00:00
|
|
|
|
|
|
|
const deactivateCb = (_:Event) => deactivate();
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
document.removeEventListener('click', deactivateCb);
|
|
|
|
})
|
|
|
|
|
|
|
|
const activate = (id: string|null = currentBlockId) => {
|
|
|
|
document.addEventListener('click', deactivateCb, { once: true });
|
|
|
|
activeBlockId.value = id;
|
|
|
|
};
|
|
|
|
|
2020-12-30 01:32:46 +00:00
|
|
|
const requestActivation = () => {
|
|
|
|
if (activeBlockId.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
activate();
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2021-03-07 17:47:28 +00:00
|
|
|
activeBlockId,
|
2020-12-30 01:32:46 +00:00
|
|
|
isActive,
|
|
|
|
activate,
|
2022-03-11 16:50:50 +00:00
|
|
|
deactivate,
|
2020-12-30 01:32:46 +00:00
|
|
|
requestActivation,
|
|
|
|
};
|
|
|
|
}
|