forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
191 lines (160 loc) · 4.78 KB
/
utils.ts
File metadata and controls
191 lines (160 loc) · 4.78 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import * as snabbdom from 'snabbdom'
import attributesModule from 'snabbdom/modules/attributes'
import classModule from 'snabbdom/modules/class'
import eventlistenersModule from 'snabbdom/modules/eventlisteners'
import propsModule from 'snabbdom/modules/props'
import styleModule from 'snabbdom/modules/style'
export const SELF = 'self'
export const patch = snabbdom.init([
classModule,
propsModule,
attributesModule,
styleModule,
eventlistenersModule,
])
let _app
export const setApp = (app) => {
_app = app
}
export const getApp = () => {
return _app
}
export function flatten(array) {
return array.reduce(
(aggr, item) => aggr.concat(Array.isArray(item) ? flatten(item) : item),
[]
)
}
export function getName(name) {
return name.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase()
}
export function isEvent(key) {
return (
key.length > 2 && key.startsWith('on') && key[2] === key[2].toUpperCase()
)
}
export function getEvent(key) {
const eventName = key.toLowerCase().substr(2)
if (eventName === 'change') {
return 'input'
}
if (eventName === 'doubleclick') {
return 'dblclick'
}
if (eventName === 'update') {
return 'postpatch'
}
return eventName
}
let isHoldingCmdCTRL = false
if (process.env.NODE_ENV === 'development') {
}
const pathDescriptions = document.createElement('div')
pathDescriptions.style.position = 'absolute'
pathDescriptions.style.zIndex = '9999999999999'
pathDescriptions.style.backgroundColor = '#333'
pathDescriptions.style.padding = '0.5rem'
pathDescriptions.style.fontFamily = 'Arial'
pathDescriptions.style.maxWidth = '300px'
pathDescriptions.style.color = '#FAFAFA'
pathDescriptions.style.display = 'none'
document.body.appendChild(pathDescriptions)
export function normalizeAttrs(tag, attrs) {
const normalizedAttrs = Object.keys(attrs || {}).reduce(
(aggr, key) => {
if (key === 'onMount') {
aggr.hook = aggr.hook || {}
let unmount
aggr.hook.insert = (vnode) => {
unmount = attrs[key](vnode.elm)
}
aggr.hook.destroy = () => {
if (unmount) {
unmount()
}
}
return aggr
}
if (key === 'onEnter') {
aggr.hook = aggr.hook || {}
let onLeave
aggr.hook.insert = (vnode) =>
requestAnimationFrame(() => {
onLeave = attrs[key](vnode.elm || vnode)
})
aggr.hook.remove = (vnode, done) => {
if (onLeave) {
vnode.elm.addEventListener(
'transitionend',
function transitionEnd() {
vnode.elm.removeEventListener('transitionEnd', transitionEnd)
done()
}
)
onLeave(vnode.elm)
} else {
done()
}
}
return aggr
}
if (isEvent(key)) {
aggr.on = aggr.on || {}
aggr.on[getEvent(key)] = attrs[key]
return aggr
}
if (key === 'key' || key === 'style' || key === 'class') {
aggr[key] = attrs[key]
return aggr
}
if (
key === 'value' ||
key === 'checked' ||
key === 'selected' ||
key === 'className'
) {
aggr.props = aggr.props || {}
aggr.props[key] = attrs[key]
return aggr
}
aggr.attrs = aggr.attrs || {}
aggr.attrs[key.startsWith('html') ? key.substr(4) : key] = attrs[key]
return aggr
},
{} as any
)
/*
if (tag === SELF) {
normalizedAttrs.hook = normalizedAttrs.hook || {};
if (process.env.NODE_ENV === "development") {
normalizedAttrs.hook.mouseover = event => {
event.stopPropagation();
event.currentTarget.__prevDisplay = window
.getComputedStyle(event.currentTarget)
.getPropertyValue("display");
event.currentTarget.style.display = event.currentTarget.__prevDisplay
? event.currentTarget.__prevDisplay
: "block";
event.currentTarget.style.outline = "solid";
const boundingRect = event.currentTarget.getBoundingClientRect();
pathDescriptions.style.display = "block";
pathDescriptions.style.top = boundingRect.bottom + "px";
pathDescriptions.style.left = boundingRect.left + "px";
pathDescriptions.innerHTML = `
<h3>${event.currentTarget.__overmind.name}</h3>
<div>${Array.from(
event.currentTarget.__overmind.tree.pathDependencies
).join(", ")}</div>
`;
};
normalizedAttrs.hook.mouseout = event => {
event.stopPropagation();
pathDescriptions.style.display = "none";
event.currentTarget.style.display = event.currentTarget.__prevDisplay;
event.currentTarget.style.outline = null;
};
}
}
*/
return normalizedAttrs
}