-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(items): add action and delete interactions
refs: #4
- Loading branch information
1 parent
9641b33
commit 43fbbd3
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { LitElement, ReactiveController } from "lit"; | ||
import { ActionEvent, DeleteEvent } from "../core/events.js"; | ||
|
||
export class ItemInteractionsController implements ReactiveController { | ||
constructor(private readonly host: LitElement) { | ||
host.addController(this); | ||
} | ||
|
||
hostConnected() { | ||
this.host.addEventListener("keydown", this.handleKeydown); | ||
this.host.addEventListener("dblclick", this.handleDoubleClick); | ||
} | ||
|
||
hostDisconnected() { | ||
this.host.removeEventListener("keydown", this.handleKeydown); | ||
this.host.removeEventListener("dblclick", this.handleDoubleClick); | ||
} | ||
|
||
private handleKeydown = (e: KeyboardEvent) => { | ||
switch (e.key) { | ||
case "Enter": | ||
if (!e.repeat) this.action(); | ||
break; | ||
case "Delete": | ||
if (!e.repeat) this.delete(); | ||
break; | ||
} | ||
}; | ||
|
||
private handleDoubleClick = () => { | ||
this.action(); | ||
}; | ||
|
||
private delete() { | ||
this.host.dispatchEvent(new DeleteEvent()); | ||
} | ||
|
||
private action() { | ||
this.host.dispatchEvent(new ActionEvent()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
abstract class MuttiEvent extends Event { | ||
static match(_: Event): _ is MuttiEvent { | ||
throw new TypeError( | ||
"Static 'match' method must be implemented by each subclass." | ||
); | ||
} | ||
} | ||
|
||
declare global { | ||
interface GlobalEventHandlersEventMap { | ||
[ActionEvent.type]: ActionEvent; | ||
[DeleteEvent.type]: DeleteEvent; | ||
} | ||
} | ||
|
||
export class ActionEvent extends MuttiEvent { | ||
static type = "action" as const; | ||
|
||
constructor() { | ||
super(ActionEvent.type); | ||
} | ||
|
||
static override match(e: Event): e is ActionEvent { | ||
return e instanceof ActionEvent; | ||
} | ||
} | ||
|
||
export class DeleteEvent extends MuttiEvent { | ||
static type = "delete" as const; | ||
|
||
constructor() { | ||
super(DeleteEvent.type); | ||
} | ||
|
||
static override match(e: Event): e is DeleteEvent { | ||
return e instanceof DeleteEvent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters