|
| 1 | +import type { ReactiveController, ReactiveControllerHost } from 'lit'; |
| 2 | +import type { Ref } from 'lit/directives/ref.js'; |
| 3 | +import { findElementFromEventPath } from '../common/util.js'; |
| 4 | + |
| 5 | +export type ResizeMode = 'immediate' | 'deferred'; |
| 6 | + |
| 7 | +export type ResizeCallbackParams = { |
| 8 | + event: PointerEvent; |
| 9 | + state: { |
| 10 | + initial: DOMRect; |
| 11 | + current: DOMRect; |
| 12 | + dx: number; |
| 13 | + dy: number; |
| 14 | + }; |
| 15 | +}; |
| 16 | + |
| 17 | +type ResizeControllerCallback = (params: ResizeCallbackParams) => unknown; |
| 18 | + |
| 19 | +type ResizeControllerConfig = { |
| 20 | + ref?: Ref<HTMLElement>; |
| 21 | + mode?: ResizeMode; |
| 22 | + deferredFactory?: (element?: HTMLElement) => HTMLElement; |
| 23 | + start?: ResizeControllerCallback; |
| 24 | + resize?: ResizeControllerCallback; |
| 25 | + end?: ResizeControllerCallback; |
| 26 | +}; |
| 27 | + |
| 28 | +class ResizeController implements ReactiveController { |
| 29 | + private static auxiliaryEvents = [ |
| 30 | + 'pointermove', |
| 31 | + 'lostpointercapture', |
| 32 | + ] as const; |
| 33 | + |
| 34 | + private static createDefaultGhost(host: HTMLElement): HTMLElement { |
| 35 | + const { width, height } = host.getBoundingClientRect(); |
| 36 | + const ghostElement = document.createElement('div'); |
| 37 | + |
| 38 | + Object.assign(ghostElement.style, { |
| 39 | + position: 'absolute', |
| 40 | + top: 0, |
| 41 | + left: 0, |
| 42 | + zIndex: 1000, |
| 43 | + background: 'pink', |
| 44 | + opacity: 0.85, |
| 45 | + width: `${width}px`, |
| 46 | + height: `${height}px`, |
| 47 | + }); |
| 48 | + |
| 49 | + return ghostElement; |
| 50 | + } |
| 51 | + |
| 52 | + private _host: ReactiveControllerHost & HTMLElement; |
| 53 | + private _config: ResizeControllerConfig = {}; |
| 54 | + private _id!: number; |
| 55 | + |
| 56 | + private _ghost: HTMLElement | null = null; |
| 57 | + protected _initialState!: DOMRect; |
| 58 | + private _state!: DOMRect; |
| 59 | + |
| 60 | + protected get _element() { |
| 61 | + return this._config?.ref ? this._config.ref.value! : this._host; |
| 62 | + } |
| 63 | + |
| 64 | + constructor( |
| 65 | + host: ReactiveControllerHost & HTMLElement, |
| 66 | + config?: ResizeControllerConfig |
| 67 | + ) { |
| 68 | + this._host = host; |
| 69 | + this._host.addController(this); |
| 70 | + |
| 71 | + this.setConfig(config); |
| 72 | + } |
| 73 | + |
| 74 | + // Internal state helpers |
| 75 | + |
| 76 | + private _createGhost() { |
| 77 | + if (this._config.mode !== 'deferred') { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + this._ghost = this._config.deferredFactory |
| 82 | + ? this._config.deferredFactory() |
| 83 | + : ResizeController.createDefaultGhost(this._host); |
| 84 | + this._host.append(this._ghost); |
| 85 | + } |
| 86 | + |
| 87 | + private _disposeGhost() { |
| 88 | + if (this._ghost) { |
| 89 | + this._ghost.remove(); |
| 90 | + this._ghost = null; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private _setInitialState(event: PointerEvent) { |
| 95 | + const rect = this._host.getBoundingClientRect(); |
| 96 | + this._initialState = structuredClone(rect); |
| 97 | + this._state = rect; |
| 98 | + this._id = event.pointerId; |
| 99 | + } |
| 100 | + |
| 101 | + private _createCallbackParams(event: PointerEvent): ResizeCallbackParams { |
| 102 | + return { |
| 103 | + event, |
| 104 | + state: { |
| 105 | + initial: this._initialState, |
| 106 | + current: this._state, |
| 107 | + dx: this._state.width - this._initialState.width, |
| 108 | + dy: this._state.height - this._initialState.height, |
| 109 | + }, |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + private _toggleSubsequentEvents(set: boolean) { |
| 114 | + const method = set |
| 115 | + ? this._host.addEventListener |
| 116 | + : this._host.removeEventListener; |
| 117 | + for (const type of ResizeController.auxiliaryEvents) { |
| 118 | + method(type, this); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private _shouldSkip(event: PointerEvent): boolean { |
| 123 | + return !findElementFromEventPath((e) => e === this._element, event); |
| 124 | + } |
| 125 | + |
| 126 | + // Event handlers |
| 127 | + |
| 128 | + private _handlePointerDown(event: PointerEvent) { |
| 129 | + // Non-primary buttons are ignored |
| 130 | + if (event.button) { |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + if (this._config?.start) { |
| 135 | + this._setInitialState(event); |
| 136 | + this._config.start.call(this._host, this._createCallbackParams(event)); |
| 137 | + |
| 138 | + this._createGhost(); |
| 139 | + |
| 140 | + this._element.setPointerCapture(this._id); |
| 141 | + this._toggleSubsequentEvents(true); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private _handlePointerMove(event: PointerEvent) { |
| 146 | + if (!this._element.hasPointerCapture(this._id)) { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + // REVIEW: Sequencing |
| 151 | + |
| 152 | + if (this._config?.resize) { |
| 153 | + this._state.width = event.clientX; |
| 154 | + this._state.height = event.clientY; |
| 155 | + |
| 156 | + this._config.resize.call(this._host, this._createCallbackParams(event)); |
| 157 | + } |
| 158 | + |
| 159 | + const target = this._config.mode === 'deferred' ? this._ghost! : this._host; |
| 160 | + |
| 161 | + Object.assign(target.style, { |
| 162 | + width: `${this._state.width}px`, |
| 163 | + height: `${this._state.height}px`, |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + private _handlePointerEnd(event: PointerEvent) { |
| 168 | + Object.assign(this._host.style, { |
| 169 | + width: `${this._state.width}px`, |
| 170 | + height: `${this._state.height}px`, |
| 171 | + }); |
| 172 | + |
| 173 | + if (this._config?.end) { |
| 174 | + this._config.end.call(this._host, this._createCallbackParams(event)); |
| 175 | + } |
| 176 | + |
| 177 | + this.dispose(); |
| 178 | + } |
| 179 | + |
| 180 | + public handleEvent(event: PointerEvent) { |
| 181 | + if (this._shouldSkip(event)) { |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + switch (event.type) { |
| 186 | + case 'touchstart': |
| 187 | + return event.preventDefault(); |
| 188 | + |
| 189 | + case 'pointerdown': |
| 190 | + return this._handlePointerDown(event); |
| 191 | + case 'pointermove': |
| 192 | + return this._handlePointerMove(event); |
| 193 | + case 'lostpointercapture': |
| 194 | + return this._handlePointerEnd(event); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + // Public API |
| 199 | + |
| 200 | + public setConfig(config?: ResizeControllerConfig) { |
| 201 | + Object.assign(this._config, config); |
| 202 | + } |
| 203 | + |
| 204 | + public dispose() { |
| 205 | + this._disposeGhost(); |
| 206 | + this._toggleSubsequentEvents(false); |
| 207 | + this._element.releasePointerCapture(this._id); |
| 208 | + } |
| 209 | + |
| 210 | + public hostConnected(): void { |
| 211 | + this._host.addEventListener('pointerdown', this); |
| 212 | + this._host.addEventListener('touchstart', this, { passive: false }); |
| 213 | + } |
| 214 | + |
| 215 | + public hostDisconnected(): void { |
| 216 | + this._host.removeEventListener('pointerdown', this); |
| 217 | + this._host.removeEventListener('touchstart', this); |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +export function addResizeController( |
| 222 | + host: ReactiveControllerHost & HTMLElement, |
| 223 | + config?: ResizeControllerConfig |
| 224 | +) { |
| 225 | + return new ResizeController(host, config); |
| 226 | +} |
0 commit comments