Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,11 @@ export const handleEvent = curry((event, root) => {
forEach(handleEvent(event))(root.children);
}
} else {
// TODO: _instance is not defined for _system nodes such as Rect, add test for that
// if (
// event.type === Events.KEYPRESS &&
// root._instance &&
// root._instance.getGlobalKeyPressHandler()
// ) {
// root._instance.getGlobalKeyPressHandler()(event);
// } else if (
// event.type === Events.KEYDOWN &&
// root._instance &&
// root._instance.getGlobalKeyDownHandler()
// ) {
// root._instance.getGlobalKeyDownHandler()(event);
// }

if (event.type === Events.KEYPRESS) {
forEach((f) => f(event))(keyPressListeners);
return;
} else if (event.type === Events.KEYDOWN) {
forEach((f) => f(event))(keyDownListeners);
return;
}


forEach(handleEvent(event))(root.children);
}
});

Expand Down
175 changes: 93 additions & 82 deletions src/events.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { handleEvent, fromDOMEvent } from "./events";
import {
handleEvent,
fromDOMEvent,
onGlobalKeyPress,
onGlobalKeyDown,
} from "./events";
import { reconcile, enhance } from "./core";
import Rect from "./nodes/rect";
import { createElement } from "./create-element";

/** @jsx createElement */

const withGlobalEventHandler = (handler, eventName) =>
enhance((features) => features[eventName](handler));
enhance(() => {
if (eventName === "onGlobalKeyDown") {
onGlobalKeyDown(handler);
} else if (eventName === "onGlobalKeyPress") {
onGlobalKeyPress(handler);
}
});

// TODO: Rewrite tests with reconcile to have a real tree
// TODO: Move global events to instance method instead.
Expand Down Expand Up @@ -154,113 +165,113 @@ describe("events", () => {
}).not.toThrow();
});

// test("handleEvent should call onGlobalKeyPress on node with key event object", () => {
// const keyPressHandler = jest.fn();
// const Root = () => <KeyPressHandler />;
test("handleEvent should call onGlobalKeyPress on node with key event object", () => {
const keyPressHandler = jest.fn();
const Root = () => <KeyPressHandler />;

// const KeyPressHandler = withGlobalEventHandler(
// keyPressHandler,
// "onGlobalKeyPress"
// )(null, () => {});
const KeyPressHandler = withGlobalEventHandler(
keyPressHandler,
"onGlobalKeyPress"
)(null, () => {});

// const root = reconcile({}, <Root />);
const root = reconcile({}, <Root />);

// const event = {
// type: "keypress",
// modifiers: [],
// key: "d",
// keyCode: 100,
// };
const event = {
type: "keypress",
modifiers: [],
key: "d",
keyCode: 100,
};

// handleEvent(event, root);
handleEvent(event, root);

// expect(keyPressHandler).toHaveBeenCalledWith(event);
// });
expect(keyPressHandler).toHaveBeenCalledWith(event);
});

// test("handleEvent should call onGlobalKeyPress on any node that defines it", () => {
// const keyPressHandler1 = jest.fn();
// const keyPressHandler2 = jest.fn();
test("handleEvent should call onGlobalKeyPress on any node that defines it", () => {
const keyPressHandler1 = jest.fn();
const keyPressHandler2 = jest.fn();

// const Root = () => <KeyPressHandler1 />;
const Root = () => <KeyPressHandler1 />;

// const KeyPressHandler1 = withGlobalEventHandler(
// keyPressHandler1,
// "onGlobalKeyPress"
// )(null, () => <KeyPressHandler2 />);
const KeyPressHandler1 = withGlobalEventHandler(
keyPressHandler1,
"onGlobalKeyPress"
)(null, () => <KeyPressHandler2 />);

// const KeyPressHandler2 = withGlobalEventHandler(
// keyPressHandler2,
// "onGlobalKeyPress"
// )(null, () => {});
const KeyPressHandler2 = withGlobalEventHandler(
keyPressHandler2,
"onGlobalKeyPress"
)(null, () => {});

// const root = reconcile({}, <Root />);
const root = reconcile({}, <Root />);

// const event = {
// type: "keypress",
// modifiers: [],
// key: "d",
// keyCode: 100,
// };
const event = {
type: "keypress",
modifiers: [],
key: "d",
keyCode: 100,
};

// handleEvent(event, root);
handleEvent(event, root);

// expect(keyPressHandler1).toHaveBeenCalledWith(event);
// expect(keyPressHandler2).toHaveBeenCalledWith(event);
// });
expect(keyPressHandler1).toHaveBeenCalledWith(event);
expect(keyPressHandler2).toHaveBeenCalledWith(event);
});

// test("handleEvent should call onGlobalKeyDown on node with key event object", () => {
// const keyDownHandler = jest.fn();
// const Root = () => <KeyDownHandler />;
test("handleEvent should call onGlobalKeyDown on node with key event object", () => {
const keyDownHandler = jest.fn();
const Root = () => <KeyDownHandler />;

// const KeyDownHandler = withGlobalEventHandler(
// keyDownHandler,
// "onGlobalKeyDown"
// )(null, () => {});
const KeyDownHandler = withGlobalEventHandler(
keyDownHandler,
"onGlobalKeyDown"
)(null, () => {});

// const root = reconcile({}, <Root />);
const root = reconcile({}, <Root />);

// const event = {
// type: "keydown",
// modifiers: [],
// key: "d",
// keyCode: 100,
// };
const event = {
type: "keydown",
modifiers: [],
key: "d",
keyCode: 100,
};

// handleEvent(event, root);
handleEvent(event, root);

// expect(keyDownHandler).toHaveBeenCalledWith(event);
// });
expect(keyDownHandler).toHaveBeenCalledWith(event);
});

// test("handleEvent should call onGlobalKeyDown on any node that defines it", () => {
// const keyDownHandler1 = jest.fn();
// const keyDownHandler2 = jest.fn();
test("handleEvent should call onGlobalKeyDown on any node that defines it", () => {
const keyDownHandler1 = jest.fn();
const keyDownHandler2 = jest.fn();

// const Root = () => <KeyPressHandler1 />;
const Root = () => <KeyPressHandler1 />;

// const KeyPressHandler1 = withGlobalEventHandler(
// keyDownHandler1,
// "onGlobalKeyDown"
// )(null, () => <KeyPressHandler2 />);
const KeyPressHandler1 = withGlobalEventHandler(
keyDownHandler1,
"onGlobalKeyDown"
)(null, () => <KeyPressHandler2 />);

// const KeyPressHandler2 = withGlobalEventHandler(
// keyDownHandler2,
// "onGlobalKeyDown"
// )(null, () => {});
const KeyPressHandler2 = withGlobalEventHandler(
keyDownHandler2,
"onGlobalKeyDown"
)(null, () => {});

// const root = reconcile({}, <Root />);
const root = reconcile({}, <Root />);

// const event = {
// type: "keydown",
// modifiers: [],
// key: "d",
// keyCode: 100,
// };
const event = {
type: "keydown",
modifiers: [],
key: "d",
keyCode: 100,
};

// handleEvent(event, root);
handleEvent(event, root);

// expect(keyDownHandler1).toHaveBeenCalledWith(event);
// expect(keyDownHandler2).toHaveBeenCalledWith(event);
// });
expect(keyDownHandler1).toHaveBeenCalledWith(event);
expect(keyDownHandler2).toHaveBeenCalledWith(event);
});

test("fromDOMEvent should build an event object with projected coordinates", () => {
const domEvent = {
Expand Down
14 changes: 0 additions & 14 deletions src/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ export const Spark = (vnode) => {
let _dirty = true;
let _lastRender = null;
let _dynamic = false;
let globalKeyPressHandler = null;
let globalKeyDownHandler = null;

const onGlobalKeyPress = (fn) => {
globalKeyPressHandler = fn;
};

const onGlobalKeyDown = (fn) => {
globalKeyDownHandler = fn;
};

const setState = (newState) => {
nextState = newState;
Expand Down Expand Up @@ -89,8 +79,6 @@ export const Spark = (vnode) => {
mounted,
unmounted,
dynamic,
onGlobalKeyPress,
onGlobalKeyDown,
}
);
} else {
Expand Down Expand Up @@ -118,8 +106,6 @@ export const Spark = (vnode) => {
isDirty,
makeDirty,
isDynamic,
getGlobalKeyPressHandler: () => globalKeyPressHandler,
getGlobalKeyDownHandler: () => globalKeyDownHandler,
});
};

Expand Down