31 lines
943 B
JavaScript
Raw Normal View History

2024-01-29 09:26:07 +08:00
import { ref, onBeforeMount, onBeforeUnmount } from 'vue';
import { isBrowser } from "../env/is-browser.mjs";
const isComposingRef = ref(false);
const compositionStartHandler = () => {
isComposingRef.value = true;
};
const compositionEndHandler = () => {
isComposingRef.value = false;
};
let mountedCount = 0;
export const useIsComposing = () => {
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;
};