-
Notifications
You must be signed in to change notification settings - Fork 443
Expand file tree
/
Copy pathaction_descriptor.ts
More file actions
101 lines (84 loc) · 2.68 KB
/
action_descriptor.ts
File metadata and controls
101 lines (84 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import type { Controller } from "./controller"
export type ActionDescriptorFilters = Record<string, ActionDescriptorFilter>
export type ActionDescriptorFilter = (options: ActionDescriptorFilterOptions) => boolean
type ActionDescriptorFilterOptions = {
name: string
value: boolean
event: Event
element: Element
controller: Controller<Element>
}
export const defaultActionDescriptorFilters: ActionDescriptorFilters = {
stop({ event, value }) {
if (value) event.stopPropagation()
return true
},
prevent({ event, value }) {
if (value) event.preventDefault()
return true
},
self({ event, value, element }) {
if (value) {
return element === event.target
} else {
return true
}
},
input({ event, value }) {
if (value) return true
const target = event.target
if (!(target instanceof Element)) return true
const isInput =
target.tagName === "INPUT" ||
target.tagName === "TEXTAREA" ||
(target instanceof HTMLElement && target.isContentEditable)
return !isInput
},
}
export interface ActionDescriptor {
eventTarget: EventTarget
eventOptions: AddEventListenerOptions
eventName: string
identifier: string
methodName: string
keyFilter: string
}
// capture nos.: 1 1 2 2 3 3 4 4 5 5 6 6 7 7
const descriptorPattern = /^(?:(?:([^.]+?)\+)?(.+?)(?:\.(.+?))?(?:@(window|document))?->)?(.+?)(?:#([^:]+?))(?::(.+))?$/
export function parseActionDescriptorString(descriptorString: string): Partial<ActionDescriptor> {
const source = descriptorString.trim()
const matches = source.match(descriptorPattern) || []
let eventName = matches[2]
let keyFilter = matches[3]
if (keyFilter && !["keydown", "keyup", "keypress"].includes(eventName)) {
eventName += `.${keyFilter}`
keyFilter = ""
}
return {
eventTarget: parseEventTarget(matches[4]),
eventName,
eventOptions: matches[7] ? parseEventOptions(matches[7]) : {},
identifier: matches[5],
methodName: matches[6],
keyFilter: matches[1] || keyFilter,
}
}
function parseEventTarget(eventTargetName: string): EventTarget | undefined {
if (eventTargetName == "window") {
return window
} else if (eventTargetName == "document") {
return document
}
}
function parseEventOptions(eventOptions: string): AddEventListenerOptions {
return eventOptions
.split(":")
.reduce((options, token) => Object.assign(options, { [token.replace(/^!/, "")]: !/^!/.test(token) }), {})
}
export function stringifyEventTarget(eventTarget: EventTarget) {
if (eventTarget == window) {
return "window"
} else if (eventTarget == document) {
return "document"
}
}