-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78a7b77
commit bfd9f6b
Showing
8 changed files
with
718 additions
and
95 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
developer-extension/src/panel/components/tabs/eventsTab/columnUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export type EventListColumn = | ||
| { type: 'date' } | ||
| { type: 'description' } | ||
| { type: 'type' } | ||
| { type: 'field'; path: string } | ||
|
||
export const DEFAULT_COLUMNS: EventListColumn[] = [{ type: 'date' }, { type: 'type' }, { type: 'description' }] | ||
|
||
export function includesColumn(existingColumns: EventListColumn[], newColumn: EventListColumn) { | ||
return existingColumns.some((column) => { | ||
if (column.type === 'field' && newColumn.type === 'field') { | ||
return column.path === newColumn.path | ||
} | ||
return column.type === newColumn.type | ||
}) | ||
} | ||
|
||
export function getColumnTitle(column: EventListColumn) { | ||
return column.type === 'date' | ||
? 'Date' | ||
: column.type === 'description' | ||
? 'Description' | ||
: column.type === 'type' | ||
? 'Type' | ||
: column.path | ||
} |
121 changes: 121 additions & 0 deletions
121
developer-extension/src/panel/components/tabs/eventsTab/drag.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
export interface Coordinates { | ||
x: number | ||
y: number | ||
} | ||
|
||
/** | ||
* This is a framework agnostic drag implementation that works as a state machine: | ||
* | ||
* ``` | ||
* (init) | ||
* | | ||
* [onStart] | ||
* | \______________ | ||
* | \ | ||
* <returns true> <returns false> | ||
* | ____ | | ||
* | / \ (end) | ||
* [onMove] ) | ||
* | \ \____/ | ||
* | \______________ | ||
* | \ | ||
* <drop in the window> | | ||
* | <stop() called or drop out of the window> | ||
* | | | ||
* [onDrop] [onAbort] | ||
* | _________________/ | ||
* |/ | ||
* (end) | ||
* ``` | ||
*/ | ||
export function initDrag({ | ||
target, | ||
onStart, | ||
onMove, | ||
onAbort, | ||
onDrop, | ||
}: { | ||
target: HTMLElement | ||
onStart: (event: { target: HTMLElement; position: Coordinates }) => boolean | void | ||
onMove: (event: { position: Coordinates }) => void | ||
onDrop: () => void | ||
onAbort: () => void | ||
}) { | ||
type DragState = | ||
| { isDragging: false } | ||
| { | ||
isDragging: true | ||
removeListeners: () => void | ||
} | ||
|
||
let state: DragState = { | ||
isDragging: false, | ||
} | ||
|
||
target.addEventListener('pointerdown', onPointerDown, { capture: true }) | ||
|
||
return { | ||
stop: () => { | ||
endDrag(true) | ||
target.removeEventListener('pointerdown', onPointerDown, { capture: true }) | ||
}, | ||
} | ||
|
||
function onPointerDown(event: PointerEvent) { | ||
if ( | ||
state.isDragging || | ||
event.buttons !== 1 || | ||
onStart({ target: event.target as HTMLElement, position: { x: event.clientX, y: event.clientY } }) === false | ||
) { | ||
return | ||
} | ||
|
||
event.preventDefault() | ||
|
||
state = { | ||
isDragging: true, | ||
removeListeners: () => { | ||
removeEventListener('pointerup', onPointerUp, { capture: true }) | ||
removeEventListener('pointermove', onPointerMove, { capture: true }) | ||
}, | ||
} | ||
|
||
addEventListener('pointerup', onPointerUp, { capture: true }) | ||
addEventListener('pointermove', onPointerMove, { capture: true }) | ||
} | ||
|
||
function onPointerUp(_event: PointerEvent) { | ||
endDrag(false) | ||
} | ||
|
||
function onPointerMove(event: PointerEvent) { | ||
if (!state.isDragging) { | ||
return | ||
} | ||
|
||
if (event.buttons !== 1) { | ||
// The user might have released the click outside of the window | ||
endDrag(true) | ||
return | ||
} | ||
|
||
onMove({ | ||
position: { | ||
x: event.clientX, | ||
y: event.clientY, | ||
}, | ||
}) | ||
} | ||
|
||
function endDrag(abort: boolean) { | ||
if (state.isDragging) { | ||
if (abort) { | ||
onAbort() | ||
} else { | ||
onDrop() | ||
} | ||
state.removeListeners() | ||
state = { isDragging: false } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.