2
0
mirror of https://github.com/frappe/books.git synced 2025-02-02 20:18:26 +00:00

fix: window-drag on Windows

- remove listener on unmounting outside click
This commit is contained in:
18alantom 2022-07-21 18:10:21 +05:30
parent 5f8663438c
commit af8ffcbf71
3 changed files with 29 additions and 27 deletions

View File

@ -1,15 +1,10 @@
<template> <template>
<div <div
class=" class="px-4 flex justify-between items-center h-row-largest flex-shrink-0"
px-4 :class="[
flex border ? 'border-b' : '',
justify-between platform !== 'Windows' ? 'window-drag' : '',
window-drag ]"
items-center
h-row-largest
flex-shrink-0
"
:class="border ? 'border-b' : ''"
> >
<h1 class="text-xl font-semibold select-none" v-if="title"> <h1 class="text-xl font-semibold select-none" v-if="title">
{{ title }} {{ title }}

View File

@ -1,5 +1,8 @@
<template> <template>
<div class="flex-1 bg-gray-25 flex justify-center items-center window-drag"> <div
class="flex-1 bg-gray-25 flex justify-center items-center window-drag"
:class="{ 'window-drag': platform !== 'Windows' }"
>
<!-- Setup Wizard Slide --> <!-- Setup Wizard Slide -->
<Slide <Slide
:primary-disabled="!valuesFilled || loading" :primary-disabled="!valuesFilled || loading"

View File

@ -1,35 +1,39 @@
import { Directive } from 'vue'; import { Directive } from 'vue';
const instances: OutsideClickCallback[] = [];
type OutsideClickCallback = (e: Event) => void; type OutsideClickCallback = (e: Event) => void;
const instanceMap: Map<HTMLElement, OutsideClickCallback> = new Map();
export const outsideClickDirective: Directive< export const outsideClickDirective: Directive<
HTMLElement, HTMLElement,
OutsideClickCallback OutsideClickCallback
> = { > = {
beforeMount(el, binding) { beforeMount(el, binding) {
el.dataset.outsideClickIndex = String(instances.length); const clickHandler = function (e: Event) {
onDocumentClick(e, el, binding.value);
const fn = binding.value;
const click = function (e: Event) {
onDocumentClick(e, el, fn);
}; };
document.addEventListener('click', click); removeHandlerIfPresent(el);
instances.push(click); instanceMap.set(el, clickHandler);
document.addEventListener('click', clickHandler);
}, },
unmounted(el) { unmounted(el) {
const index = parseInt(el.dataset.outsideClickIndex ?? '0'); removeHandlerIfPresent(el);
const handler = instances[index];
document.addEventListener('click', handler);
instances.splice(index, 1);
}, },
}; };
function onDocumentClick(e: Event, el: HTMLElement, fn: OutsideClickCallback) { function onDocumentClick(e: Event, el: HTMLElement, fn: OutsideClickCallback) {
const target = e.target; const target = e.target as Node;
if (el !== target && !el.contains(target)) {
if (el !== target && !el.contains(target as Node)) { fn?.(e);
fn(e);
} }
} }
function removeHandlerIfPresent(el: HTMLElement) {
const clickHandler = instanceMap.get(el);
if (!clickHandler) {
return;
}
instanceMap.delete(el);
document.removeEventListener('click', clickHandler);
}