Skip to content

Commit

Permalink
feat(wasm-api-dom): support more event types/fns, minor zig updates
Browse files Browse the repository at this point in the history
- add support for focus & wheel events
- add stopPropagation()
- update zig error fn return types
  • Loading branch information
postspectacular committed Oct 21, 2022
1 parent 6ec6135 commit d05803d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
15 changes: 10 additions & 5 deletions packages/wasm-api-dom/include/dom.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub const DOMError = error{
var eventListeners: ManagedIndex(*const EventListener, u16) = undefined;
var rafListeners: ManagedIndex(*const RAFListener, u16) = undefined;

pub fn init(allocator: std.mem.Allocator) anyerror!void {
pub fn init(allocator: std.mem.Allocator) !void {
eventListeners = ManagedIndex(*const EventListener, u16).init(allocator);
rafListeners = ManagedIndex(*const RAFListener, u16).init(allocator);
}
Expand Down Expand Up @@ -99,7 +99,7 @@ export fn dom_callListener(listenerID: u16, event: *const dom.Event) void {

pub extern "dom" fn _addListener(elementID: i32, name: [*]const u8, listenerID: u16) void;

pub fn addListener(elementID: i32, name: []const u8, listener: *const EventListener) anyerror!u16 {
pub fn addListener(elementID: i32, name: []const u8, listener: *const EventListener) !u16 {
const listenerID = try eventListeners.add(listener);
_addListener(elementID, name.ptr, listenerID);
return listenerID;
Expand All @@ -112,17 +112,22 @@ pub fn removeListener(listenerID: u16) void {
_removeListener(listenerID);
}

/// calls .preventDefault() on currently processed event
/// Calls .preventDefault() on currently processed event
/// (only to be called from an EventListener!)
pub extern "dom" fn preventDefault() void;

/// calls .stopImmediatePropagation() on currently processed event
/// Calls .stopPropagation() on currently processed event
/// (only to be called from an EventListener!)
pub extern "dom" fn stopPropagation() void;

/// Calls .stopImmediatePropagation() on currently processed event
/// (only to be called from an EventListener!)
pub extern "dom" fn stopImmediatePropagation() void;

pub extern "dom" fn _requestAnimationFrame(listenerID: u16) void;

pub fn requestAnimationFrame(listener: *const RAFListener) anyerror!u16 {
/// Registers given listener for next animation frame
pub fn requestAnimationFrame(listener: *const RAFListener) !u16 {
const id = try rafListeners.add(listener);
_requestAnimationFrame(id);
return id;
Expand Down
17 changes: 16 additions & 1 deletion packages/wasm-api-dom/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ export class WasmDom implements IWasmAPI<DOMExports> {
16,
true
);
} else if (e instanceof WheelEvent) {
event.type = EventType.WHEEL;
event.deltaX = e.deltaX;
event.deltaY = e.deltaY;
event.buttons = e.buttons;
} else if (e instanceof FocusEvent) {
event.type =
e.type === "focus"
? EventType.FOCUS
: EventType.BLUR;
} else if (e.type === "change" || e.type === "input") {
event.type = EventType.INPUT;
const el = <HTMLInputElement>e.target;
Expand All @@ -195,7 +205,8 @@ export class WasmDom implements IWasmAPI<DOMExports> {
if (
isTouch ||
e instanceof MouseEvent ||
e instanceof KeyboardEvent
e instanceof KeyboardEvent ||
e instanceof WheelEvent
) {
event.modifiers =
(e.shiftKey ? 1 : 0) |
Expand Down Expand Up @@ -229,6 +240,10 @@ export class WasmDom implements IWasmAPI<DOMExports> {
this.currEvent && this.currEvent.preventDefault();
},

stopPropagation: () => {
this.currEvent && this.currEvent.stopPropagation();
},

stopImmediatePropagation: () => {
this.currEvent && this.currEvent.stopImmediatePropagation();
},
Expand Down

0 comments on commit d05803d

Please sign in to comment.