-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Engine supporting 1-level Marking Menu.
- Loading branch information
1 parent
f44b65e
commit 39a04f1
Showing
10 changed files
with
470 additions
and
4 deletions.
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,99 @@ | ||
import { Observable } from 'rxjs'; | ||
|
||
// Create a custom pointer event from a touch event. | ||
const createPEventFromTouchEvent = touchEvt => { | ||
const touchList = Array.from(touchEvt.targetTouches); | ||
const sumX = touchList.reduce((acc, t) => acc + t.clientX, 0); | ||
const sumY = touchList.reduce((acc, t) => acc + t.clientY, 0); | ||
const meanX = sumX / touchList.length; | ||
const meanY = sumY / touchList.length; | ||
return { | ||
originalEvent: touchEvt, | ||
clientX: meanX, | ||
clientY: meanY | ||
}; | ||
}; | ||
|
||
// Create a custom pointer from a mouse event. | ||
const createPEventFromMouseEvent = mouseEvt => ({ | ||
originalEvent: mouseEvt, | ||
clientX: mouseEvt.clientX, | ||
clientY: mouseEvt.clientY | ||
}); | ||
|
||
/** | ||
* Create the marking menu controller. | ||
* @param {HTMLElement} parentDOM the element where to listen for events. | ||
*/ | ||
const createEngine = parentDOM => { | ||
// Higher order observable tracking mouse drags. | ||
const mouseDrag$ = Observable.fromEvent(parentDOM, 'mousedown') | ||
.map(downEvt => | ||
Observable.fromEvent(parentDOM, 'mousemove') | ||
// Make sure we include the first mouse down event. | ||
.startWith(downEvt) | ||
.takeUntil(Observable.fromEvent(parentDOM, 'mouseup')) | ||
) | ||
.map(o => o.map(createPEventFromMouseEvent)); | ||
|
||
// Higher order observable tracking touch drags. | ||
const touchDrag$ = Observable.fromEvent(parentDOM, 'touchstart') | ||
// Menu is supposed to have pointer-events: none so we can safely rely on | ||
// targetTouches. | ||
.filter(evt => evt.targetTouches.length === 1) | ||
.map(firstEvent => | ||
Observable.fromEvent(parentDOM, 'touchmove') | ||
.startWith(firstEvent) | ||
.takeUntil( | ||
Observable.merge( | ||
Observable.fromEvent(parentDOM, 'touchend'), | ||
Observable.fromEvent(parentDOM, 'touchcancel'), | ||
Observable.fromEvent(parentDOM, 'touchstart') | ||
).filter(evt => evt.targetTouches.length !== 1) | ||
) | ||
) | ||
.map(o => o.map(createPEventFromTouchEvent)); | ||
|
||
// Higher order observable tracking drags. | ||
const drag$ = Observable.merge(touchDrag$, mouseDrag$); | ||
|
||
// Higher order observable tracking angular drags. | ||
// Emits { center, alpha } where center is the drag start location | ||
// and alpha is the angle of the center to current drag position vector. | ||
const angleDrag$ = drag$.map(o => | ||
o.scan((acc, evt) => { | ||
const center = acc | ||
? acc.center | ||
: { clientX: evt.clientX, clientY: evt.clientY }; | ||
const alpha = | ||
Math.atan2(evt.clientY - center.clientY, evt.clientX - center.clientX) * | ||
360 / | ||
(2 * Math.PI); | ||
return { center, alpha }; | ||
}, null) | ||
); | ||
|
||
const menuEvent$ = angleDrag$.exhaustMap(o => | ||
Observable.concat( | ||
o.first().map(e => { | ||
// Adjust the center position | ||
const parentBCR = parentDOM.getBoundingClientRect(); | ||
const centerX = e.center.clientX - parentBCR.left; | ||
const centerY = e.center.clientY - parentBCR.top; | ||
return { | ||
type: 'open', | ||
position: [centerX, centerY] | ||
}; | ||
}), | ||
o.map(e => ({ | ||
type: 'change', | ||
alpha: e.alpha | ||
})), | ||
Observable.from([{ type: 'close' }]) | ||
) | ||
); | ||
|
||
return menuEvent$; | ||
}; | ||
|
||
export default createEngine; |
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 |
---|---|---|
@@ -1 +1,30 @@ | ||
export { default } from './menu'; | ||
import createEngine from './engine'; | ||
import createMenu from './menu'; | ||
|
||
export default (items, parentDOM) => { | ||
let menu = null; | ||
// Create the engine. | ||
const menuEvent$ = createEngine(parentDOM); | ||
// Open the menu in function of engine events. | ||
menuEvent$.subscribe(evt => { | ||
try { | ||
switch (evt.type) { | ||
case 'open': | ||
menu = createMenu(items, parentDOM, evt.position); | ||
break; | ||
case 'change': | ||
menu.setActiveByNearestAngle(evt.alpha); | ||
break; | ||
case 'close': | ||
menu.remove(); | ||
menu = null; | ||
break; | ||
default: | ||
throw new Error(`Invalid engine type: ${evt.type}`); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
throw e; | ||
} | ||
}); | ||
}; |
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
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