diff --git a/client/ui/custom-elements/chat-text.ts b/client/ui/custom-elements/chat-text.ts index 7aeda96..37a2446 100644 --- a/client/ui/custom-elements/chat-text.ts +++ b/client/ui/custom-elements/chat-text.ts @@ -10,7 +10,7 @@ customElements.define('chat-text', class extends HTMLElement { } connectedCallback() { const shadow = this.shadowRoot as ShadowRoot - + this.span = document.createElement('span') this.span.style.whiteSpace = 'pre-wrap' this.span.style.fontSynthesis = 'style weight' @@ -23,8 +23,17 @@ customElements.define('chat-text', class extends HTMLElement { } update() { if (this.span == null) return - - this.span.textContent = this.textContent + + const isFirstElementInParent = this.parentElement?.firstElementChild == this + const isLastElementInParent = this.parentElement?.lastElementChild == this + + // 避免不同的消息类型之间的换行符导致显示异常 + if (isFirstElementInParent) + this.span.textContent = this.textContent.trimStart() + else if (isLastElementInParent) + this.span.textContent = this.textContent.trimEnd() + else + this.span.textContent = this.textContent this.span.style.textDecoration = $(this).attr('underline') ? 'underline' : '' this.span.style.fontStyle = $(this).attr('em') ? 'italic' : '' }