-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from alienzhou/feat/mobile
feat(interaction): support highlighting on mobile device
- Loading branch information
Showing
10 changed files
with
152 additions
and
46 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
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
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
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
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
* @author [email protected] | ||
*/ | ||
|
||
import {RootElement} from '../types'; | ||
import { | ||
WRAP_TAG, | ||
ID_DIVISION, | ||
|
@@ -31,7 +32,7 @@ export const getHighlightId = ($node: HTMLElement): string => { | |
/** | ||
* get all highlight wrapping nodes nodes from a root node | ||
*/ | ||
export const getHighlightsByRoot = ($roots: HTMLElement | Array<HTMLElement>): Array<HTMLElement> => { | ||
export const getHighlightsByRoot = ($roots: RootElement | Array<RootElement>): Array<HTMLElement> => { | ||
if (!Array.isArray($roots)) { | ||
$roots = [$roots]; | ||
} | ||
|
@@ -47,7 +48,7 @@ export const getHighlightsByRoot = ($roots: HTMLElement | Array<HTMLElement>): A | |
/** | ||
* get all highlight wrapping nodes by highlight id from a root node | ||
*/ | ||
export const getHighlightById = ($root: HTMLElement, id: String): Array<HTMLElement> => { | ||
export const getHighlightById = ($root: RootElement, id: String): Array<HTMLElement> => { | ||
const $highlights = []; | ||
const reg = new RegExp(`(${id}\\${ID_DIVISION}|\\${ID_DIVISION}?${id}$)`); | ||
const $list = $root.querySelectorAll(`${WRAP_TAG}[data-${DATASET_IDENTIFIER}]`); | ||
|
@@ -72,3 +73,28 @@ export const forEach = ($nodes: NodeList, cb: Function): void => { | |
cb($nodes[i], i, $nodes); | ||
} | ||
}; | ||
|
||
/** | ||
* maybe be need some polyfill methods later | ||
* provide unified dom methods for compatibility | ||
*/ | ||
export const addEventListener = ($el: RootElement, evt: string, fn: EventListenerOrEventListenerObject): Function => { | ||
$el.addEventListener(evt, fn); | ||
return () => removeEventListener($el, evt, fn); | ||
}; | ||
|
||
export const removeEventListener = ($el: RootElement, evt: string, fn: EventListenerOrEventListenerObject): void => { | ||
$el.removeEventListener(evt, fn); | ||
}; | ||
|
||
export const addClass = ($el: HTMLElement, className: string): void => { | ||
$el.classList.add(className); | ||
}; | ||
|
||
export const removeClass = ($el: HTMLElement, className: string): void => { | ||
$el.classList.remove(className); | ||
}; | ||
|
||
export const hasClass = ($el: HTMLElement, className: string): boolean => { | ||
return $el.classList.contains(className); | ||
}; |
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,16 @@ | ||
/** | ||
* adapter for mobile and desktop events | ||
*/ | ||
|
||
import {IInteraction, UserInputEvent} from '../types'; | ||
import detectMobile from './is.mobile'; | ||
|
||
const isMobile = detectMobile(window.navigator.userAgent); | ||
const interaction: IInteraction = { | ||
PointerEnd: isMobile ? UserInputEvent.touchend : UserInputEvent.mouseup, | ||
PointerTap: isMobile ? UserInputEvent.touchstart : UserInputEvent.click, | ||
// hover and click will be the same event in mobile | ||
PointerOver: isMobile ? UserInputEvent.touchstart : UserInputEvent.mouseover, | ||
}; | ||
|
||
export default interaction; |
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,8 @@ | ||
/** | ||
* is mobile device? | ||
*/ | ||
|
||
const regMobile: RegExp = /Android|iPhone|BlackBerry|BB10|Opera Mini|Phone|Mobile|Silk|Windows Phone|Mobile(?:.+)Firefox\b/i; | ||
export default function (userAgent: string): boolean { | ||
return regMobile.test(userAgent); | ||
} |