31 lines
931 B
JavaScript
Raw Normal View History

2024-08-02 18:19:39 +08:00
import { onBeforeMount, onBeforeUnmount, ref } from 'vue';
2024-01-29 09:26:07 +08:00
import { isBrowser } from "../env/is-browser.mjs";
const isComposingRef = ref(false);
2024-08-02 18:19:39 +08:00
function compositionStartHandler() {
2024-01-29 09:26:07 +08:00
isComposingRef.value = true;
2024-08-02 18:19:39 +08:00
}
function compositionEndHandler() {
2024-01-29 09:26:07 +08:00
isComposingRef.value = false;
2024-08-02 18:19:39 +08:00
}
2024-01-29 09:26:07 +08:00
let mountedCount = 0;
2024-08-02 18:19:39 +08:00
export function useIsComposing() {
2024-01-29 09:26:07 +08:00
if (isBrowser) {
onBeforeMount(() => {
if (!mountedCount) {
window.addEventListener('compositionstart', compositionStartHandler);
window.addEventListener('compositionend', compositionEndHandler);
}
mountedCount++;
});
onBeforeUnmount(() => {
if (mountedCount <= 1) {
window.removeEventListener('compositionstart', compositionStartHandler);
window.removeEventListener('compositionend', compositionEndHandler);
mountedCount = 0;
} else {
mountedCount--;
}
});
}
return isComposingRef;
2024-08-02 18:19:39 +08:00
}