优化代码结构

master
yutent 2023-04-19 17:37:02 +08:00
parent d03912e26c
commit 3607cc3513
2 changed files with 21 additions and 9 deletions

View File

@ -7,6 +7,7 @@
import { css, html, Component, styleMap } from '@bd/core' import { css, html, Component, styleMap } from '@bd/core'
import '../icon/index.js' import '../icon/index.js'
import './preview.js' import './preview.js'
class Image extends Component { class Image extends Component {
static props = { static props = {
src: '', src: '',
@ -23,7 +24,7 @@ class Image extends Component {
attribute: false attribute: false
} }
} }
status = 'loading'
static styles = css` static styles = css`
:host { :host {
position: relative; position: relative;
@ -47,6 +48,9 @@ class Image extends Component {
align-items: center; align-items: center;
} }
` `
status = 'loading'
onError() { onError() {
this.status = 'error' this.status = 'error'
this.$refs.wrapper.removeChild(this.$refs.img) this.$refs.wrapper.removeChild(this.$refs.img)
@ -76,6 +80,7 @@ class Image extends Component {
cursor: previewSrcList.length ? 'pointer' : 'default' cursor: previewSrcList.length ? 'pointer' : 'default'
}) })
let $slot = '' let $slot = ''
if (status === 'loading') { if (status === 'loading') {
$slot = html`<slot name="placeholder"></slot>` $slot = html`<slot name="placeholder"></slot>`
} else if (status === 'error') { } else if (status === 'error') {
@ -83,6 +88,7 @@ class Image extends Component {
<div class="error">FAILED</div> <div class="error">FAILED</div>
</slot>` </slot>`
} }
return html` return html`
<div class="wc-image" ref="wrapper" @click=${this.onClick}> <div class="wc-image" ref="wrapper" @click=${this.onClick}>
<img <img

View File

@ -6,6 +6,7 @@
import { css, html, Component, styleMap, bind, unbind } from '@bd/core' import { css, html, Component, styleMap, bind, unbind } from '@bd/core'
import '../icon/index.js' import '../icon/index.js'
class ImagePreview extends Component { class ImagePreview extends Component {
static props = { static props = {
list: { list: {
@ -17,7 +18,7 @@ class ImagePreview extends Component {
'max-zoom': 3, 'max-zoom': 3,
'min-zoom': 1 / 3 'min-zoom': 1 / 3
} }
active = 0
static styles = css` static styles = css`
:host { :host {
z-index: 1; z-index: 1;
@ -103,6 +104,15 @@ class ImagePreview extends Component {
} }
} }
` `
active = 0
#state = {
scale: 1,
rotate: 0,
duration: 0.2,
x: 0,
y: 0
}
pre() { pre() {
this.toggleImage(this.active - 1) this.toggleImage(this.active - 1)
} }
@ -166,17 +176,12 @@ class ImagePreview extends Component {
close() { close() {
this.visible = false this.visible = false
} }
#state = {
scale: 1,
rotate: 0,
duration: 0.2,
x: 0,
y: 0
}
#setState(data) { #setState(data) {
Object.assign(this.#state, data) Object.assign(this.#state, data)
this.$requestUpdate() this.$requestUpdate()
} }
mounted() { mounted() {
this.$on('wheel', e => { this.$on('wheel', e => {
e.preventDefault() e.preventDefault()
@ -208,6 +213,7 @@ class ImagePreview extends Component {
}) })
}) })
} }
render() { render() {
let { scale, rotate, duration, x, y } = this.#state let { scale, rotate, duration, x, y } = this.#state
let styles = styleMap({ let styles = styleMap({