Skip to content

Commit d023d40

Browse files
author
Simon he
committed
🎨
1 parent fabb6df commit d023d40

16 files changed

+53
-47
lines changed

playground/src/main.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createApp } from 'vue'
22
import { createRouter, createWebHistory } from 'vue-router'
33
import routes from 'virtual:generated-pages'
4-
import { copy, log, monitorPef, timeCost, vFetch } from 'simon-js-tool'
4+
import { copy, monitorPef, vFetch } from 'simon-js-tool'
55
import App from './App.vue'
66

77
import '@unocss/reset/tailwind.css'
@@ -15,7 +15,6 @@ const router = createRouter({
1515
})
1616
app.use(router)
1717
app.mount('#app')
18-
1918
vFetch.interceptors.request.use((response) => {
2019
// console.log(response)
2120
return response
@@ -26,7 +25,6 @@ vFetch.interceptors.request.use((response) => {
2625
const instance = vFetch.create({
2726
baseURL: 'http://localhost:5001/',
2827
})
29-
3028
instance({
3129
url: 'test',
3230
}).then((res: any) => {
@@ -38,10 +36,10 @@ monitorPef()
3836
window.onclick = () => {
3937
copy('hello')
4038
}
41-
timeCost(() => {
42-
for (let i = 0; i < 1000; i++)
43-
log(i)
44-
})
39+
// timeCost(() => {
40+
// for (let i = 0; i < 1000; i++)
41+
// log(i)
42+
// })
4543

4644
// console.log(calNum.sub(0.1, 0.2, 0.2))
4745
// console.log(calNum.mul(0.1, 0.2, 0.2))

src/DotImageCanvas.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { memorizeFn } from './memorizeFn'
22
import { idleCallbackWrapper } from './idleCallbackWrapper'
3+
import { createElement } from './createElement'
34

45
export class DotImageCanvas {
56
canvas: HTMLCanvasElement = document.createElement('canvas')
@@ -53,12 +54,13 @@ export class DotImageCanvas {
5354
this.getCanvas(imagePointSet)
5455
return
5556
}
56-
const img = new Image()
57+
const img = createElement('img', {
58+
crossOrigin: 'anonymous',
59+
src: this.originSrc,
60+
})
5761
return new Promise((resolve) => {
58-
img.setAttribute('crossorigin', 'anonymous')
59-
img.src = this.originSrc
6062
img.onload = () => {
61-
this.getCanvas(this.createDotImage(img))
63+
this.getCanvas(this.createDotImage(img as HTMLImageElement))
6264
resolve(img)
6365
}
6466
img.onerror = () => {

src/DotTextCanvas.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export class DotTextCanvas {
2727
ctx.font = `${size}px SimSun`
2828
ctx.fillText(text, 0, 14 * pRatio)
2929
const { data: imageData, width, height } = ctx.getImageData(0, 0, size, size)
30-
3130
const textPointSet = []
3231
for (let i = 0; i < height; i++) {
3332
const temp: number[] = []

src/animationFrameWrapper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export function animationFrameWrapper(fn: () => void, delta = 1000, autoStop = false): (() => void) {
2-
let start: number; let work = true
2+
let start: number
3+
let work = true
34
const animationFrame = window.requestAnimationFrame
45
|| window.webkitRequestAnimationFrame
56
|| window.mozRequestAnimationFrame

src/copy.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { createElement } from './createElement'
12
export function copy(s: string): boolean {
23
try {
3-
const dom = document.createElement('textarea')
4-
dom.setAttribute('readonly', 'readonly')
5-
dom.value = s
6-
document.body.appendChild(dom)
7-
dom.select()
4+
const textarea: any = createElement('textarea', {
5+
readonly: 'readonly',
6+
})
7+
textarea.innerHTML = s
8+
document.body.appendChild(textarea)
9+
textarea.select()
810
const res = document.execCommand('copy')
9-
document.body.removeChild(dom)
11+
document.body.removeChild(textarea)
1012
return res
1113
}
1214
catch (error: any) {

src/createElement.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
export function createElement(tag: string, attributes: Record<string, string>) {
1+
export function createElement(tag: string, attributes?: Record<string, string>) {
22
const el = document.createElement(tag)
3+
if (!attributes)
4+
return el
35
for (const key in attributes)
46
el.setAttribute(key, attributes[key])
57

src/download.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { createElement } from './createElement'
12
export function download(url: string) {
23
try {
3-
const a: HTMLAnchorElement = document.createElement('a')
4-
a.href = url
5-
a.download = url.substring(url.lastIndexOf('/') + 1, url.length)
6-
a.click()
4+
createElement('a', {
5+
href: url,
6+
download: url.substring(url.lastIndexOf('/') + 1, url.length),
7+
}).click()
78
}
89
catch (error: any) {
910
throw new Error(error)

src/fileSplice.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import type { FileChunk, FileMD5 } from './types'
33

44
export async function fileSplice(_file: File, _chunkSize: number = 1024 * 100): Promise<FileChunk[]> {
55
const { HASH, suffix } = await getMD5(_file)
6+
const chunks: FileChunk[] = []
67
// 实现切片处理 [固定切片大小 & 数量]
78
let max = _chunkSize // 100kb
89
let count = Math.ceil(_file.size / max)
910
let index = 0
10-
const chunks: FileChunk[] = []
1111

1212
if (count > 100) { // 如果切片的数量大于100,可以固定切片的数量为100,来调整每一个切片的大小
1313
count = 100
@@ -29,8 +29,8 @@ function getMD5(_file: File): Promise<FileMD5> {
2929
return new Promise((resolve, reject) => {
3030
try {
3131
fileReader.onload = function (e: any) {
32-
const buffer = e?.target.result // buffer编码
33-
const spark = new SparkMD5.ArrayBuffer()
32+
const buffer = e?.target.result; const // buffer编码
33+
spark = new SparkMD5.ArrayBuffer()
3434
spark.append(buffer)
3535
const HASH = spark.end()
3636
const suffix = /\.([a-zA-Z0-9]+)$/.exec(_file.name)![1]

src/idleCallbackWrapper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export function idleCallbackWrapper(tasks: Function[], timeout: Timeout = 2000,
1818
return Math.max(0, 50.0 - (Date.now() - startTime))
1919
},
2020
}), 1)
21-
}; const idleCancel = window.cancelIdleCallback || clearTimeout
21+
}
22+
const idleCancel = window.cancelIdleCallback || clearTimeout
2223
const animationId = idleCallback(async function animationCallback(deadline: Deadline) {
2324
if (!work)
2425
return

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,5 @@ export { animationFrameWrapper } from './animationFrameWrapper'
8989
export { idleCallbackWrapper } from './idleCallbackWrapper'
9090
export { htmlTransform } from './htmlTransform'
9191
export { sleep } from './sleep'
92+
export { createElement } from './createElement'
93+
export { addLink } from './addLink'

0 commit comments

Comments
 (0)