-
Notifications
You must be signed in to change notification settings - Fork 443
Expand file tree
/
Copy pathaction.ts
More file actions
139 lines (117 loc) · 4.16 KB
/
action.ts
File metadata and controls
139 lines (117 loc) · 4.16 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { ActionDescriptor, parseActionDescriptorString, stringifyEventTarget } from "./action_descriptor"
import { Token } from "../mutation-observers"
import { Schema } from "./schema"
import { camelize } from "./string_helpers"
import { hasProperty } from "./utils"
const allModifiers = ["meta", "ctrl", "alt", "shift", "mod"]
export class Action {
readonly element: Element
readonly index: number
readonly eventTarget: EventTarget
readonly eventName: string
readonly eventOptions: AddEventListenerOptions
readonly identifier: string
readonly methodName: string
readonly keyFilter: string
readonly schema: Schema
static forToken(token: Token, schema: Schema) {
return new this(token.element, token.index, parseActionDescriptorString(token.content), schema)
}
constructor(element: Element, index: number, descriptor: Partial<ActionDescriptor>, schema: Schema) {
this.element = element
this.index = index
this.eventTarget = descriptor.eventTarget || element
this.eventName = descriptor.eventName || getDefaultEventNameForElement(element) || error("missing event name")
this.eventOptions = descriptor.eventOptions || {}
this.identifier = descriptor.identifier || error("missing identifier")
this.methodName = descriptor.methodName || error("missing method name")
this.keyFilter = descriptor.keyFilter || ""
this.schema = schema
}
toString() {
const eventFilter = this.keyFilter ? `.${this.keyFilter}` : ""
const eventTarget = this.eventTargetName ? `@${this.eventTargetName}` : ""
return `${this.eventName}${eventFilter}${eventTarget}->${this.identifier}#${this.methodName}`
}
shouldIgnoreKeyboardEvent(event: KeyboardEvent): boolean {
if (!this.keyFilter) {
return false
}
const filters = this.keyFilter.split("+")
if (this.keyFilterDissatisfied(event, filters)) {
return true
}
const standardFilter = filters.filter((key) => !allModifiers.includes(key))[0]
if (!standardFilter) {
// missing non modifier key
return false
}
if (!hasProperty(this.keyMappings, standardFilter)) {
error(`contains unknown key filter: ${this.keyFilter}`)
}
return this.keyMappings[standardFilter].toLowerCase() !== event.key.toLowerCase()
}
shouldIgnoreMouseEvent(event: MouseEvent): boolean {
if (!this.keyFilter) {
return false
}
const filters = [this.keyFilter]
if (this.keyFilterDissatisfied(event, filters)) {
return true
}
return false
}
get params() {
const params: { [key: string]: any } = {}
const pattern = new RegExp(`^data-${this.identifier}-(.+)-param$`, "i")
for (const { name, value } of Array.from(this.element.attributes)) {
const match = name.match(pattern)
const key = match && match[1]
if (key) {
params[camelize(key)] = typecast(value)
}
}
return params
}
private get eventTargetName() {
return stringifyEventTarget(this.eventTarget)
}
private get keyMappings() {
return this.schema.keyMappings
}
private keyFilterDissatisfied(event: KeyboardEvent | MouseEvent, filters: Array<string>): boolean {
const [meta, ctrl, alt, shift, mod] = allModifiers.map((modifier) => filters.includes(modifier))
const modKey = mod && this.keyMappings.mod
return (
event.metaKey !== (meta || modKey === "Meta") ||
event.ctrlKey !== (ctrl || modKey === "Control") ||
event.altKey !== alt ||
event.shiftKey !== shift
)
}
}
const defaultEventNames: { [tagName: string]: (element: Element) => string } = {
a: () => "click",
button: () => "click",
form: () => "submit",
details: () => "toggle",
input: (e) => (e.getAttribute("type") == "submit" ? "click" : "input"),
select: () => "change",
textarea: () => "input",
}
export function getDefaultEventNameForElement(element: Element): string | undefined {
const tagName = element.tagName.toLowerCase()
if (tagName in defaultEventNames) {
return defaultEventNames[tagName](element)
}
}
function error(message: string): never {
throw new Error(message)
}
function typecast(value: any): any {
try {
return JSON.parse(value)
} catch (o_O) {
return value
}
}