Skip to content

Commit 9a59b11

Browse files
author
Simon he
committed
refactor: enhance type
1 parent f2a8780 commit 9a59b11

20 files changed

+108
-57
lines changed

README.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,41 @@
1111
目前整理了<strong>100+</strong>的常用函数,还在持续更新中...,你的认可是对我最大的鼓励 :hearts:
1212

1313

14-
## 🔥 亮点
15-
- 纯js的工具函数,不依赖vue,react,angular
16-
- dom操作的api封装,如在vue中使用是不需要onMounted获取dom节点的,可以直接使用class或者id传入
17-
- 副作用函数,可以在函数执行的结果去stop,也会在页面销毁时被自动stop
18-
- api设计简单、实用
14+
## :100: 亮点
15+
- 纯js的工具函数,可使用在任何可执行js的环境
16+
- 大量减少`ref<HTMLElment>``onMounted`的使用,可以script标签直接调用
17+
- 所有的副作用函数都能返回一个stop函数,可以在任意地方停止事件的执行,并且在页面销毁时自动销毁事件
18+
- api设计简单、实用、类型友好
19+
20+
## &#x270B; 例子
21+
```js
22+
import { addEventListener, animationFrameWrapper, insertElement, useMutationObserver } from 'simon-js-tool'
23+
// 监听container的变化, 你不在需要const container = ref<HTMLElement>
24+
useMutationObserver('#container', (mutationsList, observer) => {
25+
console.log(mutationsList)
26+
})
27+
// requestAnimationFrame
28+
animationFrameWrapper((timestamp) => {
29+
// 每针相隔1s执行
30+
console.log('animationFrame', timestamp)
31+
}, 1000, true /* 只执行一次后被销毁 */)
32+
// 注册事件
33+
addEventListener('#container', 'click', () => {
34+
console.log('click')
35+
})
36+
// 插入元素
37+
insertElement('#container', '.content')
38+
// 删除元素
39+
removeElement('.content')
40+
```
41+
```html
42+
<div id="container"></div>
43+
<div class="content">hello world</div>
44+
```
1945

2046
## :balloon: 更多
2147
- 导出函数 [exports-function](https://github.com/SimonHe1995/exportsFunction)
22-
- threejs [@simon_he/s-three](https://github.com/SimonHe1995/sThree)
23-
- Echarts [@simon_he/s-chart](https://github.com/SimonHe1995/sCharts)
48+
- threejs [@simon_he/s-three]Charts [@simon_he/s-chart](https://github.com/SimonHe1995/sCharts)
2449
- numsWheel [@simon_he/nums-wheel](https://github.com/SimonHe1995/numsWheel)
2550
- vAxios [@simon_he/v-axios](https://github.com/SimonHe1995/vAxios)
2651

README_en.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,37 @@
1010

1111
At present, I have sorted out <strong>100+</strong> commonly used functions, and I am still updating..., and your recognition is the biggest encouragement to me :hearts:
1212

13-
## 🔥 Highlights
14-
- Pure js tool functions, not dependent on vue, react, angular
15-
- API encapsulation for dom operations, such as those used in vue, does not require onMounted to obtain dom nodes, and can be passed in directly using class or id
16-
- Side effect functions, which can be destop as a result of function execution, are also automatically stopped when the page is destroyed
17-
- Api design is simple and practical
13+
## :100: Highlights
14+
- Pure JS utility functions, Can be used in any environment where JS can be executed
15+
- Greatly reduced the use of `ref<HTMLElment>` and `onMounted`, which can be called directly from the script tag
16+
- All side effect functions can return a stop function, which can stop the execution of the event anywhere, and automatically destroy the event when the page is destroyed
17+
- API design is simple, practical and type-friendly
18+
19+
## &#x270B; Example
20+
```js
21+
import { addEventListener, animationFrameWrapper, insertElement, useMutationObserver } from 'simon-js-tool'
22+
// To listen for container changes, you don't need const container <HTMLElement>= ref
23+
useMutationObserver('#container', (mutationsList, observer) => {
24+
console.log(mutationsList)
25+
})
26+
// requestAnimationFrame
27+
animationFrameWrapper((timestamp) => {
28+
// Each needle is executed 1s apart
29+
console.log('animationFrame', timestamp)
30+
}, 1000, true /* It is destroyed after only one execution */)
31+
// Register for events
32+
addEventListener('#container', 'click', () => {
33+
console.log('click')
34+
})
35+
// Insert an element
36+
insertElement('#container', '.content')
37+
// Delete an element
38+
removeElement('.content')
39+
```
40+
```html
41+
<div id="container"></div>
42+
<div class="content">hello world</div>
43+
```
1844

1945
## :balloon: More
2046
- Export function [exports-function](https://github.com/SimonHe1995/exportsFunction)

src/Canvas.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export class Canvas {
2-
canvas: HTMLCanvasElement = document.createElement('canvas')
3-
ctx: CanvasRenderingContext2D = this.canvas.getContext('2d')!
2+
canvas = document.createElement('canvas')
3+
ctx = this.canvas.getContext('2d')!
44
constructor(width?: number, height?: number) {
55
if (width)
66
this.canvas.width = width

src/CreateSignatureCanvas.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,29 @@ export class CreateSignatureCanvas implements ISignature {
2121
this.ctx.lineCap = 'round'
2222
let offsetY = 0
2323
let offsetX = 0
24-
this.stop.push(addEventListener(this.canvas, 'touchstart', (e: TouchEvent) => {
24+
this.stop.push(addEventListener(this.canvas, 'touchstart', (e) => {
2525
offsetY = this.canvas.offsetTop
2626
offsetX = this.canvas.offsetLeft
2727
this.ctx.beginPath()
2828
this.ctx.moveTo(e.changedTouches[0].pageX + 2 - offsetX, e.changedTouches[0].pageY - offsetY)
2929
}, false))
3030
let down = false
31-
this.stop.push(addEventListener(this.canvas, 'mousedown', (e: MouseEvent) => {
31+
this.stop.push(addEventListener(this.canvas, 'mousedown', (e) => {
3232
offsetY = this.canvas.offsetTop
3333
offsetX = this.canvas.offsetLeft
3434
down = true
3535
this.ctx.beginPath()
3636
this.ctx.moveTo(e.pageX + 2 - offsetX, e.pageY - offsetY)
3737
}, false))
38-
this.stop.push(addEventListener(this.canvas, 'mousemove', (e: MouseEvent) => {
38+
this.stop.push(addEventListener(this.canvas, 'mousemove', (e) => {
3939
if (!down)
4040
return
4141
this.ctx.lineTo(e.pageX + 2 - offsetX, e.pageY - offsetY)
4242
this.ctx.stroke()
4343
}, false))
4444
this.stop.push(addEventListener(this.canvas, 'mouseup', () => down = false))
4545
this.stop.push(addEventListener(this.canvas, 'mouseout', () => down = false))
46-
this.stop.push(addEventListener(this.canvas, 'touchmove', (e: TouchEvent) => {
46+
this.stop.push(addEventListener(this.canvas, 'touchmove', (e) => {
4747
this.ctx.lineTo(e.changedTouches[0].pageX + 2 - offsetX, e.changedTouches[0].pageY - offsetY)
4848
this.ctx.stroke()
4949
}, false))

src/DotImageCanvas.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { insertElement } from './insertElement'
55
import { removeElement } from './removeElement'
66

77
export class DotImageCanvas {
8-
canvas: HTMLCanvasElement = document.createElement('canvas')
9-
ctx: CanvasRenderingContext2D = this.canvas.getContext('2d')!
8+
canvas = document.createElement('canvas')
9+
ctx = this.canvas.getContext('2d')!
1010
points: Map<string, Record<string, any>> = new Map()
1111
originSrc = ''
1212
color = ''
@@ -62,7 +62,7 @@ export class DotImageCanvas {
6262
})
6363
return new Promise((resolve) => {
6464
img.onload = () => {
65-
this.getCanvas(this.createDotImage(img as HTMLImageElement))
65+
this.getCanvas(this.createDotImage(img))
6666
resolve(img)
6767
}
6868
img.onerror = () => {

src/DotTextCanvas.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { idleCallbackWrapper } from './idleCallbackWrapper'
33
import { insertElement } from './insertElement'
44
import { removeElement } from './removeElement'
55
export class DotTextCanvas {
6-
canvas: HTMLCanvasElement = document.createElement('canvas')
7-
ctx: CanvasRenderingContext2D = this.canvas.getContext('2d')!
6+
canvas = document.createElement('canvas')
7+
ctx = this.canvas.getContext('2d')!
88
points: Map<string, Array<number[]>> = new Map()
99
originText: string
1010
fontSize: number

src/addEventListener.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { mount } from './mount'
2+
import { unmount } from './unmount'
23

3-
export function addEventListener(target: Window | Document | Element | string, eventName: string, callback: (e: any) => void, useCapture?: boolean | AddEventListenerOptions, autoRemove?: boolean): (() => void) {
4+
export function addEventListener<T extends keyof WindowEventMap>(target: Window | Document | Element | string, eventName: T, callback: (e: WindowEventMap[T]) => void, useCapture?: boolean | AddEventListenerOptions, autoRemove?: boolean): (() => void) {
45
let stopped = false
56
let stop: () => void
67
let animationStop: (() => void)
78
if (eventName === 'DOMContentLoaded')
89
stopped = true
9-
function event(e: Event) {
10+
function event(e: WindowEventMap[T]) {
1011
try {
1112
callback?.call?.(e.target, e)
1213
}
@@ -17,8 +18,7 @@ export function addEventListener(target: Window | Document | Element | string, e
1718
if (autoRemove)
1819
stop()
1920
}
20-
21-
window.onunload = () => stop?.()
21+
unmount(() => stop?.())
2222

2323
mount(target, (target) => {
2424
const originCall = (target as any)?.[eventName]
@@ -31,15 +31,15 @@ export function addEventListener(target: Window | Document | Element | string, e
3131
catch (error) {
3232
console.error(error)
3333
}
34-
event(e)
34+
event(e as WindowEventMap[T])
3535
}
3636
target.addEventListener(eventName, eventFunction, useCapture)
3737
stop = () => (target as Element).removeEventListener(eventName, eventFunction, useCapture)
3838
if (stopped)
3939
stop?.()
4040
})
4141
return () => {
42-
if (!stop)
42+
if (!stop)
4343
return stopped = true
4444
stop?.()
4545
}

src/copy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createElement } from './createElement'
22
export function copy(s: string): boolean {
33
try {
4-
const textarea: any = createElement('textarea', {
4+
const textarea = createElement('textarea', {
55
readonly: 'readonly',
66
}, s)
77
document.body.appendChild(textarea)

src/createElement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export function createElement(tag: string, attributes?: Record<string, string>, innerHTML?: string) {
2-
const el = document.createElement(tag)
1+
export function createElement<T extends keyof HTMLElementTagNameMap>(tag: T, attributes?: Record<string, string>, innerHTML?: string): HTMLElementTagNameMap[T] {
2+
const el = document.createElement(tag) as HTMLElementTagNameMap[T]
33
if (!attributes)
44
return el
55
for (const key in attributes)

src/diff.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export function diff(array1: any[], array2: any[], options: Options = { compare:
77
const same = array1.filter(item => array2.includes(item))
88
const diff = array1.filter(item => !array2.includes(item)).concat(array2.filter(item => !array1.includes(item)))
99
const { compare, result } = options
10-
console.log(diff)
1110
if (compare === 'same') {
1211
return result === 'value'
1312
? same

0 commit comments

Comments
 (0)