Skip to content

Commit

Permalink
feat: add custom events
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenrui committed Feb 26, 2024
1 parent 49935d9 commit 3b9864b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
24 changes: 24 additions & 0 deletions demo/custom-event.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Mini Custom Events</title>
<script src="/dist/minijs.umd.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="p-40">
<script>
MiniJS.extendEvents({ ':htmx:afteronload': 'htmx:afterOnLoad' })
</script>

<button
:htmx:afteronload="console.log('custom event!', event)"
:click="const event = document.createEvent('CustomEvent');
event.initCustomEvent('htmx:afterOnLoad', true, true, { name: 'htmx:afterOnLoad' });
this.dispatchEvent(event)"
>
Click Me
</button>
</body>
</html>
17 changes: 17 additions & 0 deletions lib/entity/events-extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export class EventsExtensions {
static instance
static USER_CUSTOM_EVENTS = {}

constructor() {
if (EventsExtensions.instance) return EventsExtensions.instance

EventsExtensions.instance = this
}

extend(events) {
EventsExtensions.USER_CUSTOM_EVENTS = {
...EventsExtensions.USER_CUSTOM_EVENTS,
...events,
}
}
}
10 changes: 8 additions & 2 deletions lib/entity/events.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Interpreter } from '../generators/interpreter'
import { camelToKebabCase } from '../helpers/strings'
import { State } from '../state'
import { EventsExtensions } from './events-extensions'

const ELEMENTS = [
'div',
Expand Down Expand Up @@ -83,6 +84,8 @@ export class Events {
static isValidEvent(event) {
if (!event.startsWith(':')) return false
if (event === ':keyevents') return false
if (event in EventsExtensions.USER_CUSTOM_EVENTS) return true

return (
Events.validEvents.includes(event) ||
Events.CUSTOM_KEY_EVENTS.some((key) => event.startsWith(key + '.'))
Expand Down Expand Up @@ -399,7 +402,10 @@ export class Events {
const expr = el.getAttribute(attr)
if (!expr) return

const nativeEventName = attr.substring(1)
const nativeEventName =
attr in EventsExtensions.USER_CUSTOM_EVENTS
? EventsExtensions.USER_CUSTOM_EVENTS[attr]
: attr.substring(1)

this.listener[attr] = {
el,
Expand Down Expand Up @@ -441,7 +447,7 @@ export class Events {
const ids = {
$: 'document-querySelector',
el: `proxyWindow['${this.base.id}']`,
// this is set under the interpreter as bind context
// "this" is set under the interpreter as bind context
}

if (this.base.parent) ids.parent = `proxyWindow['${this.base.parent.id}']`
Expand Down
5 changes: 5 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { Lexer } from './generators/lexer'
import { Observer } from './generators/observer'
import { State } from './state'
import { Events } from './entity/events'
import { EventsExtensions } from './entity/events-extensions'

let nativeProps = Object.getOwnPropertyNames(window)

const MiniJS = (() => {
let _debug = false
const state = new State()
const observer = new Observer(state)
const eventExtensions = new EventsExtensions()

async function init() {
// Automatically initialize when the script is loaded
Expand Down Expand Up @@ -100,6 +102,9 @@ const MiniJS = (() => {
get observer() {
return observer
},
extendEvents: (events) => {
eventExtensions.extend(events)
},
tryFromLocal,
}
})()
Expand Down

0 comments on commit 3b9864b

Please sign in to comment.