Skip to content

Commit 2144d3a

Browse files
author
Simon he
committed
feature: make funciton more powerful
1 parent 7bace7f commit 2144d3a

File tree

5 files changed

+59
-23
lines changed

5 files changed

+59
-23
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
## 此文是介绍封装的工具函数的文档[simon-js-tool](https://www.npmjs.com/package/simon-js-tool)
99
目前整理了<strong>90+</strong>的常用函数,还在持续更新中...,你的认可是对我最大的鼓励!
1010

11+
## 亮点
12+
- 纯js的工具函数,不依赖vue,react,angular
13+
- dom操作的api封装,如在vue中使用是不需要onMounted获取dom节点的,可以直接使用class或者id传入
14+
- 副作用函数,可以在函数执行的结果去stop,也会在页面销毁时被自动stop
15+
- api设计简单、实用
16+
1117
## 更多
1218
- 导出函数 [exports-function](https://github.com/SimonHe1995/exportsFunction)
1319
- threejs [@simon_he/s-three](https://github.com/SimonHe1995/sThree)
@@ -132,14 +138,15 @@ export default defineConfig({
132138
- 插入dom元素
133139
- 参数:
134140
- parent: string | HTMLElement 父元素
135-
- element: 插入元素
141+
- element: string | HTMLElement 插入元素
136142
- target: 插入位置 (默认插入到第一个节点)
137143
```js
138144
const div = createElement('div', {
139-
id: 'main',
145+
id: 'test',
140146
style: 'background: red;font-size:20px',
141147
})
142148
insertElement('#main', div) // 插入到第一个节点
149+
insertElement('#main', '#test') // 插入到第一个节点
143150
insertElement('#main', div, null) // 插入到最后
144151
```
145152

@@ -969,13 +976,13 @@ vFetch(options:Record<string,string>).then(res =>{
969976
})
970977
```
971978
972-
973979
## stringify
974980
- 参数:
975981
- obj: 待转换的对象
976982
```javascript
977983
console.log(stringify({ user: 'simon', age: '18' })) // 'user=simon&age=18'
978984
```
985+
979986
## parse
980987
- 参数:
981988
- str: 待转换的字符串

README_en.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
## This article is documentation that describes the encapsulated utility functions [simon-js-tool](https://www.npmjs.com/package/simon-js-tool)
99
At present, I have sorted out <strong>90+</strong> commonly used functions, and I am still updating..., and your recognition is the biggest encouragement to me!
1010

11+
## Highlights
12+
- Pure js tool functions, not dependent on vue, react, angular
13+
- 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
14+
- Side effect functions, which can be destop as a result of function execution, are also automatically stopped when the page is destroyed
15+
- Api design is simple and practical
16+
1117
## More
1218
- Export function [exports-function](https://github.com/SimonHe1995/exportsFunction)
1319
- threejs [@simon_he/s-three](https://github.com/SimonHe1995/sThree)
@@ -133,7 +139,7 @@ export default defineConfig({
133139
- Insert a dom element
134140
- params:
135141
- parent: string | HTMLElement /* The parent element */
136-
- element: HTMLElement /* Insert elements */
142+
- element: string | HTMLElement /* inserts the element */
137143
- target?: null | HTMLElement /* Insertion position (inserted to first node by default) */
138144
```js
139145
const div = createElement('div', {

src/CreateSignatureCanvas.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import type { ISignature } from './types'
2+
import { addEventListener } from './addEventListener'
3+
import { insertElement } from './insertElement'
4+
import { removeElement } from './removeElement'
25
export class CreateSignatureCanvas implements ISignature {
36
canvas: HTMLCanvasElement = document.createElement('canvas')
47
ctx: CanvasRenderingContext2D = this.canvas.getContext('2d')!
8+
stop: (() => void)[] = []
59
constructor(w = 400, h = 400) {
610
this.createCanvas(w, h)
11+
window.onunload = () => this.unmount()
712
}
813

914
createCanvas(w = 400, h = 400) {
@@ -16,41 +21,48 @@ export class CreateSignatureCanvas implements ISignature {
1621
this.ctx.lineCap = 'round'
1722
let offsetY = 0
1823
let offsetX = 0
19-
this.canvas.addEventListener('touchstart', (e) => {
24+
this.stop.push(addEventListener(this.canvas, 'touchstart', (e: TouchEvent) => {
2025
offsetY = this.canvas.offsetTop
2126
offsetX = this.canvas.offsetLeft
2227
this.ctx.beginPath()
2328
this.ctx.moveTo(e.changedTouches[0].pageX + 2 - offsetX, e.changedTouches[0].pageY - offsetY)
24-
}, false)
29+
}, false))
2530
let down = false
26-
this.canvas.addEventListener('mousedown', (e) => {
31+
this.stop.push(addEventListener(this.canvas, 'mousedown', (e: MouseEvent) => {
2732
offsetY = this.canvas.offsetTop
2833
offsetX = this.canvas.offsetLeft
2934
down = true
3035
this.ctx.beginPath()
3136
this.ctx.moveTo(e.pageX + 2 - offsetX, e.pageY - offsetY)
32-
}, false)
33-
34-
this.canvas.addEventListener('mousemove', (e) => {
37+
}, false))
38+
this.stop.push(addEventListener(this.canvas, 'mousemove', (e: MouseEvent) => {
3539
if (!down)
3640
return
3741
this.ctx.lineTo(e.pageX + 2 - offsetX, e.pageY - offsetY)
3842
this.ctx.stroke()
39-
}, false)
40-
this.canvas.addEventListener('mouseup', () => down = false)
41-
42-
this.canvas.addEventListener('mouseout', () => down = false)
43-
44-
this.canvas.addEventListener('touchmove', (e) => {
43+
}, false))
44+
this.stop.push(addEventListener(this.canvas, 'mouseup', () => down = false))
45+
this.stop.push(addEventListener(this.canvas, 'mouseout', () => down = false))
46+
this.stop.push(addEventListener(this.canvas, 'touchmove', (e: TouchEvent) => {
4547
this.ctx.lineTo(e.changedTouches[0].pageX + 2 - offsetX, e.changedTouches[0].pageY - offsetY)
4648
this.ctx.stroke()
47-
}, false)
49+
}, false))
4850
}
4951

5052
clearCanvas() {
5153
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
5254
}
5355

56+
mount(el: HTMLElement | string) {
57+
insertElement(el, this.canvas, null)
58+
return this
59+
}
60+
61+
unmount() {
62+
removeElement(this.canvas)
63+
this.stop.forEach(s => s())
64+
}
65+
5466
save() {
5567
return this.canvas.toDataURL('image/png')
5668
}

src/insertElement.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { addEventListener } from './addEventListener'
22
import { isStr } from './isStr'
33

4-
export function insertElement(parent: HTMLElement | string, element: HTMLElement, target?: HTMLElement | null): void {
4+
export function insertElement(parent: HTMLElement | string, element: HTMLElement | string, target?: HTMLElement | null): void {
55
let isMounted = false
66
let hasMounted = false
77
update()
@@ -11,10 +11,14 @@ export function insertElement(parent: HTMLElement | string, element: HTMLElement
1111
return
1212
if (isStr(parent))
1313
parent = document.querySelector(parent as string) as HTMLElement || parent
14-
if (!isMounted && isStr(parent))
14+
if (isStr(element))
15+
element = document.querySelector(element as string) as HTMLElement || element
16+
if (!isMounted && (isStr(parent) || isStr(element)))
1517
return isMounted = true
1618
if (isStr(parent))
17-
throw new Error(`${parent} is not a HTMLElement`);
19+
throw new Error(`${parent} is not a HTMLElement`)
20+
if (isStr(element))
21+
throw new Error(`${element} is not a HTMLElement`);
1822
(parent as HTMLElement).insertBefore(element as HTMLElement, target === undefined ? (parent as HTMLElement).firstChild : target)
1923
hasMounted = true
2024
}

src/removeElement.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
export function removeElement(el: HTMLElement | ChildNode): void {
2-
if (el.parentNode)
3-
el.parentNode.removeChild(el)
1+
import { isStr } from './isStr'
2+
3+
export function removeElement(el: HTMLElement | ChildNode | string): void {
4+
if (isStr(el))
5+
el = document.querySelector(el as string) as HTMLElement || el
6+
if (isStr(el))
7+
throw new Error(`${el} is not a element`)
8+
const p = (el as HTMLElement).parentNode
9+
if (p)
10+
p.removeChild(el as HTMLElement)
411
}

0 commit comments

Comments
 (0)