Skip to content

Commit

Permalink
Add document listener
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaitanLyss committed Sep 5, 2024
1 parent 2efaf46 commit 1432ba6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@selenite/commons",
"version": "0.24.12",
"version": "0.25.0",
"repository": "github:ShaitanLyss/selenite-commons",
"license": "MIT",
"keywords": [
Expand Down
26 changes: 26 additions & 0 deletions src/lib/actions/document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Action } from 'svelte/action';

export type DocumentParams = DocumentEventMap & AddEventListenerOptions;
export const documentListener: Action<Element, DocumentParams> = (node, params: DocumentParams) => {
function setup() {
Object.keys(params).forEach((key) => {
// @ts-expect-error Ignore type error
document.addEventListener(key, params[key], params);
});
}
setup()

return {
destroy() {
Object.keys(params).forEach((key) => {
// @ts-expect-error Ignore type error
document.removeEventListener(key, params[key], params);
});
},
update(parameter) {
this.destroy?.()
params = parameter;
setup()
},
};
};
1 change: 1 addition & 0 deletions src/lib/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from './keyboard';
export * from './resizable';
export * from './box-selection'
export * from '@neodrag/svelte';
export * from './document';
let handleFocusLeaveRefCount = 0;
let handleFocusLeaveCallbacks: ((isKeyboard: boolean) => void)[] = [];
function handleKeydown(e: KeyboardEvent) {
Expand Down

0 comments on commit 1432ba6

Please sign in to comment.