完成loading组件的重构

master
yutent 2023-11-21 14:16:17 +08:00
parent 7f90d6fcd5
commit 4d3afb5e31
2 changed files with 62 additions and 55 deletions

View File

@ -138,7 +138,7 @@ class ImagePreview extends Component {
this.setZoom(this.#state.scale / 1.2) this.setZoom(this.#state.scale / 1.2)
} }
setZoom(val) { setZoom(val) {
const { maxZoom, minZoom } = this let { maxZoom, minZoom } = this
val = Math.max(val, minZoom) val = Math.max(val, minZoom)
val = Math.min(val, maxZoom) val = Math.min(val, maxZoom)
val = val.toFixed(2) val = val.toFixed(2)
@ -195,7 +195,7 @@ class ImagePreview extends Component {
bind(this.$refs.images, 'mousedown', e => { bind(this.$refs.images, 'mousedown', e => {
let { clientX: startX, clientY: startY } = e let { clientX: startX, clientY: startY } = e
let { x, y } = this.#state let { x, y } = this.#state
const onmousemove = bind(document, 'mousemove', e => { let onmousemove = bind(document, 'mousemove', e => {
e.preventDefault() e.preventDefault()
let { clientX, clientY } = e let { clientX, clientY } = e
let deltaX = clientX - startX let deltaX = clientX - startX
@ -206,7 +206,7 @@ class ImagePreview extends Component {
y: deltaY + y y: deltaY + y
}) })
}) })
const onmouseup = bind(document, 'mouseup', _ => { let onmouseup = bind(document, 'mouseup', _ => {
unbind(document, 'mousemove', onmousemove) unbind(document, 'mousemove', onmousemove)
unbind(document, 'mouseup', onmouseup) unbind(document, 'mouseup', onmouseup)
}) })

View File

@ -4,23 +4,28 @@
* @date 2023/04/26 15:20:28 * @date 2023/04/26 15:20:28
*/ */
import { html, css, Component, styleMap, nextTick } from 'wkit' import { html, css, Component, styleMap } from 'wkit'
class Loading extends Component { class Loading extends Component {
static props = { static props = {
progress: 'num!0',
duration: 3000,
value: { value: {
type: Boolean, type: Boolean,
default: false,
observer(v) { observer(v) {
if (v && this.loading) { if (this.duration > 0) {
this.#start = void 0 if (v) {
this.value = false requestAnimationFrame(this.#loading.bind(this))
this.#progress = 0 }
requestAnimationFrame(this.loading) } else {
if (v) {
this.setAttribute('loading', '')
} else {
this.removeAttribute('loading')
}
}
} }
} }
},
duration: 3000
} }
static styles = [ static styles = [
@ -37,58 +42,60 @@ class Loading extends Component {
.thumb { .thumb {
width: 0; width: 0;
height: 1px; height: var(--wc-loading-height, 2px);
background: var(--color-teal-1); background: var(--wc-loading-background, var(--color-teal-1));
box-shadow: 0 0 5px var(--color-teal-1); box-shadow: 0 0 5px var(--wc-loading-background, var(--color-teal-1));
will-change: width;
transition: width 0.1s linear;
}
:host([loading]) {
.thumb {
width: 25% !important;
animation: autoplay 1s infinite ease-in-out;
}
}
@keyframes autoplay {
from,
to {
transform: translateX(0);
}
50% {
transform: translateX(calc(100vw - 100%));
}
} }
` `
] ]
#force2end = false #start
#start = 0
#progress = 0
end() { #loading(stamp) {
this.#force2end = true
}
mounted() {
this.loading = timestamp => {
if (this.#force2end) {
if (this.#progress === 100) {
this.#force2end = false
this.#progress = 0
this.$requestUpdate()
return
}
this.#progress = 100
this.$requestUpdate()
} else {
if (this.#start === void 0) { if (this.#start === void 0) {
this.#start = timestamp this.#start = stamp
} }
let _time = timestamp - this.#start let time = stamp - this.#start
let tmp = ~~(time / (this.duration / 100))
let tmp = ~~(_time / (this.duration / 100)) if (tmp !== this.progress) {
if (tmp !== this.#progress) { this.progress = tmp
this.#progress = tmp
this.$requestUpdate()
} }
if (_time > this.duration) { if (time > this.duration) {
this.#progress = 0 this.progress = 0
this.#start = void 0
this.value = false
this.$emit('input')
this.$emit('change')
return return
} }
} requestAnimationFrame(this.#loading.bind(this))
requestAnimationFrame(this.loading)
}
} }
render() { render() {
let pc = styleMap({ width: this.#progress + '%' }) let style = styleMap({ width: this.progress + '%' })
return html` <span class="thumb" style=${pc}></span> ` return html` <span class="thumb" style=${style}></span> `
} }
} }