Skip to content

Commit f2a8780

Browse files
committed
chore: update
1 parent c67a1b0 commit f2a8780

File tree

4 files changed

+20
-38
lines changed

4 files changed

+20
-38
lines changed

src/addStyle.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ export function addStyle(s: string): () => void {
99
try {
1010
const style = createElement('style', {
1111
type: 'text/css',
12-
})
13-
if (isNm(s))
14-
s = fs.readFileSync(path.resolve(process.cwd(), 'node_modules', s), 'utf8')
15-
if (isRelative(s))
16-
s = fs.readFileSync(path.resolve(process.cwd(), s), 'utf8')
17-
style.innerHTML = s
12+
}, isNm(s)
13+
? fs.readFileSync(path.resolve(process.cwd(), 'node_modules', s), 'utf8')
14+
: isRelative(s)
15+
? fs.readFileSync(path.resolve(process.cwd(), s), 'utf8')
16+
: s)
1817
document.head.appendChild(style)
1918
return () => document.head.removeChild(style)
2019
}

src/copy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ export function copy(s: string): boolean {
33
try {
44
const textarea: any = createElement('textarea', {
55
readonly: 'readonly',
6-
})
7-
textarea.innerHTML = s
6+
}, s)
87
document.body.appendChild(textarea)
98
textarea.select()
109
const res = document.execCommand('copy')

src/createElement.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
export function createElement(tag: string, attributes?: Record<string, string>) {
1+
export function createElement(tag: string, attributes?: Record<string, string>, innerHTML?: string) {
22
const el = document.createElement(tag)
33
if (!attributes)
44
return el
55
for (const key in attributes)
66
el.setAttribute(key, attributes[key])
7+
if (innerHTML)
8+
el.innerHTML = innerHTML
79
return el
810
}

src/waterfall.ts

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,25 @@
1-
import { isStr } from './isStr'
21
import { preload } from './preload'
32
import { addEventListener } from './addEventListener'
43
import { createElement } from './createElement'
54
import { isNum } from './isNum'
6-
import { findElement } from './findElement'
5+
import { mount } from './mount'
76

8-
export function waterfall(imageList: string[], container: string | HTMLElement | number, width = 200, space = 20) {
9-
let mounted = false
10-
let hasMounted = false
11-
if (isNum(container)) {
12-
width = container as number
13-
container = 'body'
7+
export function waterfall(imageList: string[], target: string | HTMLElement | number, width = 200, space = 20) {
8+
if (isNum(target)) {
9+
width = target as number
10+
target = 'body'
1411
}
15-
if (!container)
16-
container = 'body'
17-
if (isStr(container))
18-
container = findElement(container) || container
19-
if (isStr(container))
20-
throw new Error(`${container} is not a HTMLElement`)
12+
if (!target)
13+
target = 'body'
2114
const imagesElement = preload(imageList, `width:${width}px;position:absolute;`)
2215
const wrapper = createElement('div', {
2316
id: 'simon-waterfall',
2417
style: 'position:relative;width:100%;height:100%;',
2518
})
2619

27-
update()
28-
addEventListener(window, 'resize', () => {
29-
hasMounted = false
30-
update()
31-
})
32-
async function update() {
33-
if (hasMounted)
34-
return
20+
addEventListener(window, 'resize', () => update(target as HTMLElement))
3521

36-
if (isStr(container) && !mounted)
37-
return mounted = true
38-
if (isStr(container))
39-
throw new Error(`${container} is not a HTMLElement`)
22+
async function update(container: Element) {
4023
const realWidth = width + space
4124
const n = Math.floor((container as HTMLElement).offsetWidth / realWidth)
4225
const H = new Array(n).fill(0)
@@ -75,14 +58,13 @@ export function waterfall(imageList: string[], container: string | HTMLElement |
7558
(await promiseElements()).forEach(image => wrapper.appendChild(image))
7659
removeWrapper(container as HTMLElement);
7760
(container as HTMLElement).appendChild(wrapper)
78-
hasMounted = true
7961
}
62+
mount(target, container => update(target = container as HTMLElement))
8063

8164
return (imageList: string[]) => {
8265
const appendElement = preload(imageList, `width:${width}px;position:absolute;`)
8366
imagesElement.push(...appendElement)
84-
hasMounted = false
85-
update()
67+
update(target as HTMLElement)
8668
}
8769

8870
function removeWrapper(container: HTMLElement) {

0 commit comments

Comments
 (0)