Skip to content

Commit 545bb4e

Browse files
committed
refactor(core): remove useWindow (#1504)
refactor(core): remove `useWindow`
1 parent 410765b commit 545bb4e

File tree

5 files changed

+11
-39
lines changed

5 files changed

+11
-39
lines changed

packages/core/src/composables/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ export * from './useUpdateNodePositions'
1010
export * from './useViewportHelper'
1111
export * from './useVueFlow'
1212
export * from './useWatchProps'
13-
export * from './useWindow'
1413
export * from './useZoomPanHelper'

packages/core/src/composables/useKeyPress.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { MaybeRefOrGetter } from 'vue'
2-
import { onMounted, ref, toValue, watch } from 'vue'
2+
import { onMounted, ref, toRef, toValue, watch } from 'vue'
33
import type { KeyFilter, KeyPredicate } from '@vueuse/core'
44
import { onKeyStroke, useEventListener } from '@vueuse/core'
5-
import { useWindow } from './useWindow'
65

76
export interface UseKeyPressOptions {
87
actInsideInputWithModifier?: MaybeRefOrGetter<boolean>
9-
target?: MaybeRefOrGetter<EventTarget>
8+
target?: MaybeRefOrGetter<EventTarget | null | undefined>
109
}
1110

1211
export function isInputDOMNode(event: KeyboardEvent): boolean {
@@ -69,19 +68,18 @@ function useKeyOrCode(code: string, keysToWatch: string | string[]) {
6968
return keysToWatch.includes(code) ? 'code' : 'key'
7069
}
7170

72-
const window = useWindow()
73-
7471
/**
7572
* Composable that returns a boolean value if a key is pressed
7673
*
7774
* @public
7875
* @param keyFilter - Can be a boolean, a string, an array of strings or a function that returns a boolean. If it's a boolean, it will act as if the key is always pressed. If it's a string, it will return true if a key matching that string is pressed. If it's an array of strings, it will return true if any of the strings match a key being pressed, or a combination (e.g. ['ctrl+a', 'ctrl+b'])
7976
* @param options - Options object
8077
*/
81-
export function useKeyPress(
82-
keyFilter: MaybeRefOrGetter<KeyFilter | null>,
83-
options: UseKeyPressOptions = { actInsideInputWithModifier: true, target: window },
84-
) {
78+
export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | null>, options?: UseKeyPressOptions) {
79+
const actInsideInputWithModifier = toRef(() => toValue(options?.actInsideInputWithModifier) ?? false)
80+
81+
const target = toRef(() => toValue(options?.target) ?? window)
82+
8583
const isPressed = ref(toValue(keyFilter) === true)
8684

8785
let modifierPressed = false
@@ -114,7 +112,7 @@ export function useKeyPress(
114112
(e) => {
115113
modifierPressed = wasModifierPressed(e)
116114

117-
const preventAction = (!modifierPressed || (modifierPressed && !options.actInsideInputWithModifier)) && isInputDOMNode(e)
115+
const preventAction = (!modifierPressed || (modifierPressed && !actInsideInputWithModifier.value)) && isInputDOMNode(e)
118116

119117
if (preventAction) {
120118
return
@@ -124,14 +122,14 @@ export function useKeyPress(
124122

125123
isPressed.value = true
126124
},
127-
{ eventName: 'keydown', target: options.target },
125+
{ eventName: 'keydown', target },
128126
)
129127

130128
onKeyStroke(
131129
(...args) => currentFilter(...args),
132130
(e) => {
133131
if (isPressed.value) {
134-
const preventAction = (!modifierPressed || (modifierPressed && !options.actInsideInputWithModifier)) && isInputDOMNode(e)
132+
const preventAction = (!modifierPressed || (modifierPressed && !actInsideInputWithModifier.value)) && isInputDOMNode(e)
135133

136134
if (preventAction) {
137135
return
@@ -140,7 +138,7 @@ export function useKeyPress(
140138
reset()
141139
}
142140
},
143-
{ eventName: 'keyup', target: options.target },
141+
{ eventName: 'keyup', target },
144142
)
145143

146144
function reset() {

packages/core/src/composables/useResizeHandler.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { Ref } from 'vue'
22
import { onBeforeUnmount, onMounted } from 'vue'
33
import { ErrorCode, VueFlowError, getDimensions } from '../utils'
44
import { useVueFlow } from './useVueFlow'
5-
import { useWindow } from './useWindow'
65

76
/**
87
* Composable that handles the resize of the viewport.
@@ -11,8 +10,6 @@ import { useWindow } from './useWindow'
1110
* @param viewportEl
1211
*/
1312
export function useResizeHandler(viewportEl: Ref<HTMLDivElement | null>): void {
14-
const window = useWindow()
15-
1613
const { emits, dimensions } = useVueFlow()
1714

1815
let resizeObserver: ResizeObserver

packages/core/src/composables/useWindow.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/core/src/utils/graph.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import type {
2121
XYPosition,
2222
XYZPosition,
2323
} from '../types'
24-
import { useWindow } from '../composables'
2524
import { isDef, warn } from '.'
2625

2726
export function nodeToRect(node: GraphNode): Rect {
@@ -64,8 +63,6 @@ export function getHostForElement(element: HTMLElement): Document {
6463
return doc
6564
}
6665

67-
const window = useWindow()
68-
6966
return window.document
7067
}
7168

0 commit comments

Comments
 (0)