-
Notifications
You must be signed in to change notification settings - Fork 804
Expand file tree
/
Copy pathplugin.ts
More file actions
195 lines (163 loc) · 5.33 KB
/
Copy pathplugin.ts
File metadata and controls
195 lines (163 loc) · 5.33 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
192
193
194
195
import type Interaction from '@interactjs/core/Interaction'
import type { Scope, Plugin } from '@interactjs/core/scope'
import type { Element, OptionMethod } from '@interactjs/core/types'
import domObjects from '@interactjs/utils/domObjects'
import { parentNode } from '@interactjs/utils/domUtils'
import extend from '@interactjs/utils/extend'
import is from '@interactjs/utils/is'
import isNonNativeEvent from '@interactjs/utils/isNonNativeEvent'
import normalizeListeners from '@interactjs/utils/normalizeListeners'
import * as win from '@interactjs/utils/window'
declare module '@interactjs/core/scope' {
interface Scope {
logger: Logger
}
}
declare module '@interactjs/core/options' {
interface BaseDefaults {
devTools?: DevToolsOptions
}
}
declare module '@interactjs/core/Interactable' {
interface Interactable {
devTools: OptionMethod<DevToolsOptions>
}
}
export interface DevToolsOptions {
ignore: { [P in keyof typeof CheckName]?: boolean }
}
export interface Logger {
warn: (...args: any[]) => void
error: (...args: any[]) => void
log: (...args: any[]) => void
}
export interface Check {
name: CheckName
text: string
perform: (interaction: Interaction) => boolean
getInfo: (interaction: Interaction) => any[]
}
enum CheckName {
touchAction = 'touchAction',
boxSizing = 'boxSizing',
noListeners = 'noListeners',
}
const prefix = '[interact.js] '
const links = {
touchAction: 'https://developer.mozilla.org/en-US/docs/Web/CSS/touch-action',
boxSizing: 'https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing',
}
// eslint-disable-next-line no-undef
const isProduction = process.env.NODE_ENV === 'production'
function install(scope: Scope, { logger }: { logger?: Logger } = {}) {
const { Interactable, defaults } = scope
scope.logger = logger || console
defaults.base.devTools = {
ignore: {},
}
Interactable.prototype.devTools = function (options?: object) {
if (options) {
extend(this.options.devTools, options)
return this
}
return this.options.devTools
}
// can't set native events on non string targets without `addEventListener` prop
const { _onOff } = Interactable.prototype
Interactable.prototype._onOff = function (method, typeArg, listenerArg, options, filter) {
if (is.string(this.target) || this.target.addEventListener) {
return _onOff.call(this, method, typeArg, listenerArg, options, filter)
}
if (is.object(typeArg) && !is.array(typeArg)) {
options = listenerArg
listenerArg = null
}
const normalizedListeners = normalizeListeners(typeArg, listenerArg, filter)
for (const type in normalizedListeners) {
if (isNonNativeEvent(type, scope.actions)) continue
scope.logger.warn(
prefix +
`Can't add native "${type}" event listener to target without \`addEventListener(type, listener, options)\` prop.`,
)
}
return _onOff.call(this, method, normalizedListeners, options)
}
}
const checks: Check[] = [
{
name: CheckName.touchAction,
perform({ element }) {
return !!element && !parentHasStyle(element, 'touchAction', /pan-|pinch|none/)
},
getInfo({ element }) {
return [element, links.touchAction]
},
text: 'Consider adding CSS "touch-action: none" to this element\n',
},
{
name: CheckName.boxSizing,
perform(interaction) {
const { element } = interaction
return (
interaction.prepared.name === 'resize' &&
element instanceof domObjects.HTMLElement &&
!hasStyle(element, 'boxSizing', /border-box/)
)
},
text: 'Consider adding CSS "box-sizing: border-box" to this resizable element',
getInfo({ element }) {
return [element, links.boxSizing]
},
},
{
name: CheckName.noListeners,
perform(interaction) {
const actionName = interaction.prepared.name
const moveListeners = interaction.interactable?.events.types[`${actionName}move`] || []
return !moveListeners.length
},
getInfo(interaction) {
return [interaction.prepared.name, interaction.interactable]
},
text: 'There are no listeners set for this action',
},
]
function hasStyle(element: HTMLElement, prop: keyof CSSStyleDeclaration, styleRe: RegExp) {
const value = element.style[prop] || win.window.getComputedStyle(element)[prop]
return styleRe.test((value || '').toString())
}
function parentHasStyle(element: Element, prop: keyof CSSStyleDeclaration, styleRe: RegExp) {
let parent = element as HTMLElement
while (is.element(parent)) {
if (hasStyle(parent, prop, styleRe)) {
return true
}
parent = parentNode(parent) as HTMLElement
}
return false
}
const id = 'dev-tools'
const defaultExport: Plugin = isProduction
? { id, install: () => {} }
: {
id,
install,
listeners: {
'interactions:action-start': ({ interaction }, scope) => {
for (const check of checks) {
const options = interaction.interactable && interaction.interactable.options
if (
!(options && options.devTools && options.devTools.ignore[check.name]) &&
check.perform(interaction)
) {
scope.logger.warn(prefix + check.text, ...check.getInfo(interaction))
}
}
},
},
checks,
CheckName,
links,
prefix,
}
export default defaultExport