-
Notifications
You must be signed in to change notification settings - Fork 804
Expand file tree
/
Copy pathplugin.ts
More file actions
186 lines (158 loc) · 5.09 KB
/
Copy pathplugin.ts
File metadata and controls
186 lines (158 loc) · 5.09 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
import type { Interactable } from '@interactjs/core/Interactable'
import type { DoAnyPhaseArg, Interaction } from '@interactjs/core/Interaction'
import type { Scope, Plugin } from '@interactjs/core/scope'
import type { ActionName, ActionProps, Element } from '@interactjs/core/types'
import * as arr from '@interactjs/utils/arr'
import { copyAction } from '@interactjs/utils/misc'
import * as pointerUtils from '@interactjs/utils/pointerUtils'
import { tlbrToXywh } from '@interactjs/utils/rect'
declare module '@interactjs/core/scope' {
interface SignalArgs {
'interactions:before-action-reflow': Omit<DoAnyPhaseArg, 'iEvent'>
'interactions:action-reflow': DoAnyPhaseArg
'interactions:after-action-reflow': DoAnyPhaseArg
}
}
declare module '@interactjs/core/Interactable' {
interface Interactable {
/**
* ```js
* const interactable = interact(target)
* const drag = { name: drag, axis: 'x' }
* const resize = { name: resize, edges: { left: true, bottom: true }
*
* interactable.reflow(drag)
* interactable.reflow(resize)
* ```
*
* Start an action sequence to re-apply modifiers, check drops, etc.
*
* @param { Object } action The action to begin
* @param { string } action.name The name of the action
* @returns { Promise } A promise that resolves to the `Interactable` when actions on all targets have ended
*/
reflow<T extends ActionName>(action: ActionProps<T>): ReturnType<typeof doReflow>
}
}
declare module '@interactjs/core/Interaction' {
interface Interaction {
_reflowPromise: Promise<void>
_reflowResolve: (...args: unknown[]) => void
}
}
declare module '@interactjs/core/InteractEvent' {
interface PhaseMap {
reflow?: true
}
}
function install(scope: Scope) {
const { Interactable } = scope
scope.actions.phases.reflow = true
Interactable.prototype.reflow = function (action: ActionProps) {
return doReflow(this, action, scope)
}
}
function doReflow<T extends ActionName>(
interactable: Interactable,
action: ActionProps<T>,
scope: Scope,
): Promise<Interactable> {
const elements = interactable.getAllElements()
// tslint:disable-next-line variable-name
const Promise = (scope.window as any).Promise
const promises: Array<Promise<null>> | null = Promise ? [] : null
for (const element of elements) {
const rect = interactable.getRect(element as HTMLElement | SVGElement)
if (!rect) {
break
}
const runningInteraction = arr.find(scope.interactions.list, (interaction: Interaction) => {
return (
interaction.interacting() &&
interaction.interactable === interactable &&
interaction.element === element &&
interaction.prepared.name === action.name
)
})
let reflowPromise: Promise<null>
if (runningInteraction) {
runningInteraction.move()
if (promises) {
reflowPromise =
runningInteraction._reflowPromise ||
new Promise((resolve: any) => {
runningInteraction._reflowResolve = resolve
})
}
} else {
const xywh = tlbrToXywh(rect)
const coords = {
page: { x: xywh.x, y: xywh.y },
client: { x: xywh.x, y: xywh.y },
timeStamp: scope.now(),
}
const event = pointerUtils.coordsToEvent(coords)
reflowPromise = startReflow<T>(scope, interactable, element, action, event)
}
if (promises) {
promises.push(reflowPromise)
}
}
return promises && Promise.all(promises).then(() => interactable)
}
function startReflow<T extends ActionName>(
scope: Scope,
interactable: Interactable,
element: Element,
action: ActionProps<T>,
event: any,
) {
const interaction = scope.interactions.new({ pointerType: 'reflow' })
const signalArg = {
interaction,
event,
pointer: event,
eventTarget: element,
phase: 'reflow',
} as const
interaction.interactable = interactable
interaction.element = element
interaction.prevEvent = event
interaction.updatePointer(event, event, element, true)
pointerUtils.setZeroCoords(interaction.coords.delta)
copyAction(interaction.prepared, action)
interaction._doPhase(signalArg)
const { Promise } = scope.window as unknown as { Promise: PromiseConstructor }
const reflowPromise = Promise
? new Promise<undefined>((resolve) => {
interaction._reflowResolve = resolve
})
: undefined
interaction._reflowPromise = reflowPromise
interaction.start(action, interactable, element)
if (interaction._interacting) {
interaction.move(signalArg)
interaction.end(event)
} else {
interaction.stop()
interaction._reflowResolve()
}
interaction.removePointer(event, event)
return reflowPromise
}
const reflow: Plugin = {
id: 'reflow',
install,
listeners: {
// remove completed reflow interactions
'interactions:stop': ({ interaction }, scope) => {
if (interaction.pointerType === 'reflow') {
if (interaction._reflowResolve) {
interaction._reflowResolve()
}
arr.remove(scope.interactions.list, interaction)
}
},
},
}
export default reflow