Skip to content

Commit

Permalink
feat: add pointerEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Sep 20, 2024
1 parent 086d9ca commit ab55274
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 1 deletion.
22 changes: 22 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -11065,6 +11065,28 @@ const stooges = [
pluck(stooges, 'name'); // -> ['moe', 'larry', 'curly']
```

## pointerEvent

Get the pointer event name, use touch and mouse events as a fallback if not supported.

<details>
<summary>Type Definition</summary>

```typescript
function pointerEvent(type: 'down' | 'move' | 'up'): string;
```

</details>

|Name |Desc |
|------|----------------------------|
|type |Event type, down, move or up|
|return|Pointer event name |

```javascript
pointerEvent('down'); // -> 'pointerdown' if supported
```

## precision

Find decimal precision of a given number.
Expand Down
22 changes: 22 additions & 0 deletions DOC_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -11056,6 +11056,28 @@ const stooges = [
pluck(stooges, 'name'); // -> ['moe', 'larry', 'curly']
```

## pointerEvent

获取 pointer 事件名,如果不支持使用 touch 或 mouse 事件替代。

<details>
<summary>类型定义</summary>

```typescript
function pointerEvent(type: 'down' | 'move' | 'up'): string;
```

</details>

|参数名|说明|
|-----|---|
|type|事件类型,down,move 或 up|
|返回值|pointer 事件名|

```javascript
pointerEvent('down'); // -> 'pointerdown' if supported
```

## precision

获取数字的精度。
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@
"pick",
"pipe",
"pluck",
"pointerEvent",
"precision",
"prefetch",
"prefix",
Expand Down
8 changes: 8 additions & 0 deletions i18n/pointerEvent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## CN

获取 pointer 事件名,如果不支持使用 touch 或 mouse 事件替代。

|参数名|说明|
|-----|---|
|type|事件类型,down,move 或 up|
|返回值|pointer 事件名|
12 changes: 12 additions & 0 deletions index.json
Original file line number Diff line number Diff line change
Expand Up @@ -5196,6 +5196,18 @@
"browser"
]
},
"pointerEvent": {
"dependencies": [
"root"
],
"description": "Get the pointer event name, use touch and mouse events as a fallback if not supported.",
"env": [
"browser"
],
"test": [
"browser"
]
},
"precision": {
"dependencies": [],
"description": "Find decimal precision of a given number.",
Expand Down
2 changes: 1 addition & 1 deletion src/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ exports = Emitter.extend({
},
isConnected(connection) {
if (connection === this) {
throw new Error('Connection cannot be connected to itself.');
throw new Error('Channel cannot be connected to itself.');
}

return some(this._connections, item => item === connection);
Expand Down
50 changes: 50 additions & 0 deletions src/pointerEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Get the pointer event name, use touch and mouse events as a fallback if not supported.
*
* |Name |Desc |
* |------|----------------------------|
* |type |Event type, down, move or up|
* |return|Pointer event name |
*/

/* example
* pointerEvent('down'); // -> 'pointerdown' if supported
*/

/* module
* env: browser
*/

/* typescript
* export declare function pointerEvent(type: 'down' | 'move' | 'up'): string;
*/

_('root');

const touchEvents = {
down: 'touchstart',
move: 'touchmove',
up: 'touchend'
};

const mouseEvents = {
down: 'mousedown',
move: 'mousemove',
up: 'mouseup'
};

const pointerEvents = {
down: 'pointerdown',
move: 'pointermove',
up: 'pointerup'
};

const hasPointerSupport = 'PointerEvent' in root;
const hasTouchSupport = 'ontouchstart' in root;

exports = function(type) {
if (hasPointerSupport) {
return pointerEvents[type];
}

return hasTouchSupport ? touchEvents[type] : mouseEvents[type];
};
5 changes: 5 additions & 0 deletions test/pointerEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tests([
['up', 'pointerup'],
['down', 'pointerdown'],
['move', 'pointermove']
]);

0 comments on commit ab55274

Please sign in to comment.