Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support serialization of more event data #76

Merged
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
43 changes: 24 additions & 19 deletions js/src/VueRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,34 @@ export function createObjectForNestedModel(model, parentView) {
};
}

// based on https://stackoverflow.com/a/58416333/5397207
function pickSerializable(object, depth=0, max_depth=2) {
// change max_depth to see more levels, for a touch event, 2 is good
if (depth > max_depth)
return 'Object';

const obj = {};
for (let key in object) {
let value = object[key];
if (value instanceof Node)
// specify which properties you want to see from the node
value = {id: value.id};
else if (value instanceof Window)
value = 'Window';
else if (value instanceof Object)
value = pickSerializable(value, depth+1, max_depth);

obj[key] = value;
}

return obj;
}

export function eventToObject(event) {
if (event == null) {
return event;
}
let props;
switch (event.constructor) {
case MouseEvent:
props = ['altKey', 'ctrlKey', 'metaKey', 'shiftKey', 'offsetX', 'offsetY', 'clientX', 'clientY', 'pageX', 'pageY', 'screenX', 'screenY', 'shiftKey', 'x', 'y'];
break;
case WheelEvent:
props = ['altKey', 'ctrlKey', 'metaKey', 'shiftKey', 'offsetX', 'offsetY', 'clientX', 'clientY', 'pageX', 'pageY', 'screenX', 'screenY', 'shiftKey', 'x', 'y', 'wheelDelta', 'wheelDeltaX', 'wheelDeltaY'];
break;
// TODO: More events
default:
return event;
}

return props.reduce(
(result, key) => {
result[key] = event[key]; // eslint-disable-line no-param-reassign
return result;
}, {},
);
return pickSerializable(event);
}

export function vueRender(createElement, model, parentView, slotScopes) {
Expand Down
Loading