diff --git a/client/style.css b/client/style.css index d523361..38832b0 100644 --- a/client/style.css +++ b/client/style.css @@ -70,4 +70,4 @@ html { a { color: rgb(var(--mdui-color-primary)); -} +} \ No newline at end of file diff --git a/client/ui/MduiPatchedTextAreaElement.ts b/client/ui/MduiPatchedTextAreaElement.ts index 7cf2855..a028b8e 100644 --- a/client/ui/MduiPatchedTextAreaElement.ts +++ b/client/ui/MduiPatchedTextAreaElement.ts @@ -1,8 +1,10 @@ import { $ } from "mdui" export default class MduiPatchedTextAreaElement extends HTMLElement { - static observedAttributes = ['user-id'] - declare inputDiv: HTMLDivElement + static observedAttributes = ['value', 'placeholder'] + declare inputDiv?: HTMLDivElement + declare inputPlaceHolderDiv?: HTMLDivElement + declare inputContainerDiv?: HTMLDivElement constructor() { super() @@ -13,11 +15,51 @@ export default class MduiPatchedTextAreaElement extends HTMLElement { connectedCallback() { const shadow = this.shadowRoot as ShadowRoot - this.inputDiv = new DOMParser().parseFromString(` -
- `, 'text/html').body.firstChild as HTMLDivElement - this.inputDiv.contentEditable = 'true' + this.inputContainerDiv = new DOMParser().parseFromString(` +
+
+
+ +
+ `, 'text/html').body.firstChild as HTMLDivElement + + console.log(this.inputContainerDiv.children) + this.inputDiv = this.inputContainerDiv.children[0] as HTMLDivElement + this.inputPlaceHolderDiv = this.inputContainerDiv.children[1] as HTMLDivElement + + this.inputDiv.addEventListener('input', () => { + // TODO: 修复 placeholder + this.inputPlaceHolderDiv!.style.display = this.value == '' ? '' : 'none' + this.inputDiv!.style.display = this.value == '' ? 'none' : '' + }) this.inputDiv.addEventListener('blur', () => { if (this._lastValue !== this.value) { this._lastValue = this.value || '' @@ -31,7 +73,7 @@ export default class MduiPatchedTextAreaElement extends HTMLElement { this.inputDiv.style.width = '100%' - shadow.appendChild(this.inputDiv) + shadow.appendChild(this.inputContainerDiv) } attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) { switch (name) { @@ -39,19 +81,27 @@ export default class MduiPatchedTextAreaElement extends HTMLElement { this.value = newValue || '' break } + case 'placeholder': { + this.inputPlaceHolderDiv && (this.inputPlaceHolderDiv.innerText = newValue || '') + break + } } } focus() { - this.inputDiv.focus() + this.inputDiv?.focus() } blur() { - this.inputDiv.blur() + this.inputDiv?.blur() + } + checkValidity() { + // TODO: implment this method + return true } get value() { - return this.inputDiv.textContent + return this.inputDiv?.textContent || '' } set value(v) { - this.inputDiv.textContent = v + this.inputDiv && (this.inputDiv.textContent = v) } } diff --git a/client/ui/chat-fragment/ChatFragment.tsx b/client/ui/chat-fragment/ChatFragment.tsx index db5db23..d76889e 100644 --- a/client/ui/chat-fragment/ChatFragment.tsx +++ b/client/ui/chat-fragment/ChatFragment.tsx @@ -160,7 +160,7 @@ export default function ChatFragment({ }} onDrop={(e) => { // 文件拽入 }}> - { + { if (inputRef.current?.value.trim() == '') { // 清空缓存的文件 } diff --git a/mdui_patched/components/text-field/index.js b/mdui_patched/components/text-field/index.js index b7f9454..8b228fb 100644 --- a/mdui_patched/components/text-field/index.js +++ b/mdui_patched/components/text-field/index.js @@ -398,9 +398,15 @@ let TextField = class TextField extends FocusableMixin(MduiElement) { 'is-firefox': navigator.userAgent.includes('Firefox'), ...invalidClassNameObj, }); - return html `
${this.renderPrefix()}
${this.renderLabel()} ${!hasInputSlot ? (this.isTextarea - ? this.renderTextArea(hasInputSlot) - : this.renderInput(hasInputSlot)) : ''} ${when(hasInputSlot, () => html ``)}
${this.renderSuffix()}${this.renderClearButton(hasClearButton)} ${this.renderTogglePasswordButton(hasTogglePasswordButton)} ${this.renderRightIcon(hasErrorIcon)}
${when(hasError || hasHelper || hasCounter, () => html `
${this.renderHelper(hasError, hasHelper)} ${this.renderCounter(hasCounter)}
`)}`; + return html`
${this.renderPrefix()}
${this.renderLabel()} ${!hasInputSlot ? ( + this.getAttribute("use-patched-textarea") + ? this.renderPatchedTextArea(hasInputSlot) + : ( + this.isTextarea + ? this.renderTextArea(hasInputSlot) + : this.renderInput(hasInputSlot) + ) + ) : ''} ${when(hasInputSlot, () => html``)}
${this.renderSuffix()}${this.renderClearButton(hasClearButton)} ${this.renderTogglePasswordButton(hasTogglePasswordButton)} ${this.renderRightIcon(hasErrorIcon)}
${when(hasError || hasHelper || hasCounter, () => html`
${this.renderHelper(hasError, hasHelper)} ${this.renderCounter(hasCounter)}
`)}`; } setCustomValidityInternal(message) { this.inputRef.value.setCustomValidity(message); @@ -482,54 +488,54 @@ let TextField = class TextField extends FocusableMixin(MduiElement) { } renderLabel() { return this.label - ? html `` + ? html`` : nothingTemplate; } renderPrefix() { - return html `${this.icon - ? html `` + return html`${this.icon + ? html`` : nothingTemplate}${this.prefix}`; } renderSuffix() { - return html `${this.suffix}`; + return html`${this.suffix}`; } renderRightIcon(hasErrorIcon) { return hasErrorIcon - ? html `${this.errorIcon - ? html `` - : html ``}` - : html `${this.endIcon - ? html `` + ? html`${this.errorIcon + ? html`` + : html``}` + : html`${this.endIcon + ? html`` : nothingTemplate}`; } renderClearButton(hasClearButton) { - return when(hasClearButton, () => html `${this.clearIcon - ? html `` - : html ``}`); + return when(hasClearButton, () => html`${this.clearIcon + ? html`` + : html``}`); } renderTogglePasswordButton(hasTogglePasswordButton) { - return when(hasTogglePasswordButton, () => html `${this.isPasswordVisible - ? html `${this.showPasswordIcon - ? html `` - : html ``}` - : html `${this.hidePasswordIcon - ? html `` - : html ``}`}`); + return when(hasTogglePasswordButton, () => html`${this.isPasswordVisible + ? html`${this.showPasswordIcon + ? html`` + : html``}` + : html`${this.hidePasswordIcon + ? html`` + : html``}`}`); } renderInput(hasInputSlot) { - return html ``; + ? this.placeholder + : undefined)}" ?readonly="${this.readonly}" ?disabled="${this.disabled}" ?required="${this.required}" minlength="${ifDefined(this.minlength)}" maxlength="${ifDefined(this.maxlength)}" min="${ifDefined(this.min)}" max="${ifDefined(this.max)}" step="${ifDefined(this.step)}" autocapitalize="${ifDefined(this.type === 'password' ? 'off' : this.autocapitalize)}" autocomplete="${this.autocomplete}" autocorrect="${ifDefined(this.type === 'password' ? 'off' : this.autocorrect)}" spellcheck="${ifDefined(this.spellcheck)}" pattern="${ifDefined(this.pattern)}" enterkeyhint="${ifDefined(this.enterkeyhint)}" inputmode="${ifDefined(this.inputmode)}" @change="${this.onChange}" @input="${this.onInput}" @invalid="${this.onInvalid}" @keydown="${this.onKeyDown}">`; } renderTextArea(hasInputSlot) { - return html ``; } renderPatchedTextArea(hasInputSlot) { - return html ``; } @@ -539,15 +545,15 @@ let TextField = class TextField extends FocusableMixin(MduiElement) { */ renderHelper(hasError, hasHelper) { return hasError - ? html `
${this.error || this.inputRef.value.validationMessage}
` + ? html`
${this.error || this.inputRef.value.validationMessage}
` : hasHelper - ? html `${this.helper}` + ? html`${this.helper}` : // 右边有 counter,需要占位 - html ``; + html``; } renderCounter(hasCounter) { return hasCounter - ? html `
${this.value.length}/${this.maxlength}
` + ? html`
${this.value.length}/${this.maxlength}
` : nothingTemplate; } };