ui: 以正确方式编写 chat-file 的自定义元素代码
This commit is contained in:
@@ -1,27 +1,39 @@
|
|||||||
import { $ } from 'mdui/jq'
|
import { $ } from 'mdui/jq'
|
||||||
|
|
||||||
customElements.define('chat-file', class extends HTMLElement {
|
customElements.define('chat-file', class extends HTMLElement {
|
||||||
|
static observedAttributes = ['href', 'name']
|
||||||
|
declare anchor: HTMLAnchorElement
|
||||||
|
declare span: HTMLSpanElement
|
||||||
constructor() {
|
constructor() {
|
||||||
super()
|
super()
|
||||||
|
this.attachShadow({ mode: 'open' })
|
||||||
|
}
|
||||||
|
update() {
|
||||||
|
if (this.anchor == null) return
|
||||||
|
|
||||||
|
this.anchor.href = $(this).attr('href') as string
|
||||||
|
this.anchor.download = $(this).attr('href') as string
|
||||||
|
this.span.textContent = $(this).attr("name") as string
|
||||||
|
}
|
||||||
|
attributeChangedCallback(_name: string, _oldValue: unknown, _newValue: unknown) {
|
||||||
|
this.update()
|
||||||
}
|
}
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
const e = new DOMParser().parseFromString(`
|
this.anchor = new DOMParser().parseFromString(`
|
||||||
<a style="width: 100%;height: 100%;">
|
<a style="width: 100%;height: 100%;">
|
||||||
<mdui-card clickable style="display: flex;align-items: center;box-shadow: inherit;border-radius: inherit;">
|
<mdui-card clickable style="display: flex;align-items: center;box-shadow: inherit;border-radius: inherit;">
|
||||||
<mdui-icon name="insert_drive_file" style="margin: 13px;font-size: 34px;"></mdui-icon>
|
<mdui-icon name="insert_drive_file" style="margin: 13px;font-size: 34px;"></mdui-icon>
|
||||||
<span style="margin-right: 13px; word-wrap: break-word; word-break:break-all;white-space:normal; max-width :100%;"></span>
|
<span style="margin-right: 13px; word-wrap: break-word; word-break:break-all;white-space:normal; max-width :100%;"></span>
|
||||||
</mdui-card>
|
</mdui-card>
|
||||||
</a>`, 'text/html').body.firstChild as HTMLElement
|
</a>`, 'text/html').body.firstChild as HTMLAnchorElement
|
||||||
$(e).find('span').text($(this).attr("name"))
|
this.span = $(this.anchor).find('span').get(0)
|
||||||
const href = $(this).attr('href')
|
this.anchor.style.textDecoration = 'none'
|
||||||
$(e).attr('href', href)
|
this.anchor.style.color = 'inherit'
|
||||||
$(e).attr('target', '_blank')
|
this.anchor.onclick = (e) => {
|
||||||
$(e).attr('download', href)
|
|
||||||
e.style.textDecoration = 'none'
|
|
||||||
e.style.color = 'inherit'
|
|
||||||
e.onclick = (e) => {
|
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
}
|
}
|
||||||
this.appendChild(e)
|
this.shadowRoot!.appendChild(this.anchor)
|
||||||
|
|
||||||
|
this.update()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user