ui: 移除对媒体文件的显示圆角, 并修正大小 (块级元素)

This commit is contained in:
CrescentLeaf
2025-11-09 16:01:38 +08:00
parent 6ce8acdb2e
commit a7df2c689a
3 changed files with 67 additions and 35 deletions

View File

@@ -7,7 +7,7 @@ customElements.define('chat-file', class extends HTMLElement {
connectedCallback() { connectedCallback() {
const e = new DOMParser().parseFromString(` const e = new DOMParser().parseFromString(`
<a style="width: 100%;height: 100%;"> <a style="width: 100%;height: 100%;">
<mdui-card clickable style="display: flex;align-items: center;"> <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>

View File

@@ -1,39 +1,59 @@
import openImageViewer from "../openImageViewer.ts" import openImageViewer from "../openImageViewer.ts"
import { snackbar } from "../snackbar.ts"
import { $ } from 'mdui/jq' import { $ } from 'mdui/jq'
customElements.define('chat-image', class extends HTMLElement { customElements.define('chat-image', class extends HTMLElement {
static observedAttributes = ['src', 'show-error']
declare img: HTMLImageElement
declare error: HTMLElement
constructor() { constructor() {
super() super()
this.attachShadow({ mode: 'open' })
}
update() {
if (this.img == null) return
this.img.src = $(this).attr('src') as string
const error = $(this).attr('show-error') == 'true'
this.img.style.display = error ? 'none' : 'block'
this.error.style.display = error ? '' : 'none'
}
attributeChangedCallback(_name: string, _oldValue: unknown, _newValue: unknown) {
this.update()
} }
connectedCallback() { connectedCallback() {
this.style.display = 'block' this.img = new Image()
const e = new Image() this.img.style.width = '100%'
e.style.maxWidth = "400px" this.img.style.maxHeight = "300px"
e.style.maxHeight = "300px" this.img.style.objectFit = 'cover'
e.style.marginTop = '5px' // this.img.style.borderRadius = "var(--mdui-shape-corner-medium)"
e.style.marginBottom = '5px' this.shadowRoot!.appendChild(this.img)
e.style.borderRadius = "var(--mdui-shape-corner-medium)"
e.alt = $(this).attr('alt') || "" this.error = new DOMParser().parseFromString(`<mdui-icon name="broken_image" style="font-size: 2rem;"></mdui-icon>`, 'text/html').body.firstChild as HTMLElement
e.onerror = () => { this.shadowRoot!.appendChild(this.error)
const src = $(this).attr('src')
$(this).html(`<mdui-icon name="broken_image" style="font-size: 2rem;"></mdui-icon>`) this.img.addEventListener('error', () => {
$(this).attr('alt', '无法加载: ' + $(this).attr('alt')) $(this).attr('show-error', 'true')
$(this).on('click', () => { })
snackbar({ this.error.addEventListener('click', (event) => {
message: `图片 (${src}) 无法加载!`, event.stopPropagation()
placement: 'top' const img = this.img
}) this.img = new Image()
}) this.img.style.width = '100%'
} this.img.style.maxHeight = "300px"
e.src = $(this).attr('src') as string this.img.style.objectFit = 'cover'
e.onclick = (event) => { this.shadowRoot!.replaceChild(img, this.img)
$(this).attr('show-error', undefined)
})
this.img.addEventListener('click', (event) => {
event.stopPropagation() event.stopPropagation()
openImageViewer($(this).attr('src') as string) openImageViewer($(this).attr('src') as string)
} })
this.appendChild(e)
this.update()
} }
}) })

View File

@@ -1,19 +1,31 @@
import { $ } from 'mdui/jq' import { $ } from 'mdui/jq'
customElements.define('chat-video', class extends HTMLElement { customElements.define('chat-video', class extends HTMLElement {
static observedAttributes = ['src']
declare video: HTMLVideoElement
constructor() { constructor() {
super() super()
this.attachShadow({ mode: 'open' })
}
update() {
if (this.video == null) return
this.video.src = $(this).attr('src') as string
}
attributeChangedCallback(_name: string, _oldValue: unknown, _newValue: unknown) {
this.update()
} }
connectedCallback() { connectedCallback() {
this.style.display = 'block' this.video = new DOMParser().parseFromString(`<video controls></video>`, 'text/html').body.firstChild as HTMLVideoElement
const e = new DOMParser().parseFromString(`<video controls></video>`, 'text/html').body.firstChild as HTMLVideoElement this.video.style.maxWidth = "400px"
e.style.maxWidth = "400px" this.video.style.maxHeight = "300px"
e.style.maxHeight = "300px" this.video.style.width = "100%"
e.style.width = "100%" this.video.style.height = "100%"
e.style.height = "100%" this.video.style.display = 'block'
e.style.borderRadius = "var(--mdui-shape-corner-medium)" // e.style.borderRadius = "var(--mdui-shape-corner-medium)"
e.src = $(this).attr('src') as string
e.onclick = (e) => e.stopPropagation() this.video.onclick = (e) => e.stopPropagation()
this.appendChild(e) this.shadowRoot!.appendChild(this.video)
} }
}) })