Skip to content

Commit

Permalink
fix(blocks): focus ai input after position updated (#8981)
Browse files Browse the repository at this point in the history
  • Loading branch information
akumatus committed Dec 16, 2024
1 parent 8540737 commit 68fd625
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/blocks/src/root-block/widgets/ai-panel/ai-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ export class AffineAIPanelWidget extends WidgetComponent {
.then(({ x, y }) => {
this.style.left = `${x}px`;
this.style.top = `${y}px`;
setTimeout(() => {
const input = this.shadowRoot?.querySelector('ai-panel-input');
input?.textarea?.focus();
}, 0);
})
.catch(console.error);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ export class AIPanelInput extends WithDisposable(LitElement) {
`;

private _onInput = () => {
this._textarea.style.height = 'auto';
this._textarea.style.height = this._textarea.scrollHeight + 'px';
this.textarea.style.height = 'auto';
this.textarea.style.height = this.textarea.scrollHeight + 'px';

this.onInput?.(this._textarea.value);
const value = this._textarea.value.trim();
this.onInput?.(this.textarea.value);
const value = this.textarea.value.trim();
if (value.length > 0) {
this._arrow.dataset.active = '';
this._hasContent = true;
Expand All @@ -107,20 +107,14 @@ export class AIPanelInput extends WithDisposable(LitElement) {
};

private _sendToAI = () => {
const value = this._textarea.value.trim();
const value = this.textarea.value.trim();
if (value.length === 0) return;

this.onFinish?.(value);
this.remove();
};

override render() {
this.updateComplete
.then(() => {
this._textarea.focus();
})
.catch(console.error);

return html`<div class="root">
<div class="icon">${AIStarIcon}</div>
<div class="textarea-container">
Expand Down Expand Up @@ -153,7 +147,7 @@ export class AIPanelInput extends WithDisposable(LitElement) {

override updated(_changedProperties: Map<PropertyKey, unknown>): void {
const result = super.updated(_changedProperties);
this._textarea.style.height = this._textarea.scrollHeight + 'px';
this.textarea.style.height = this.textarea.scrollHeight + 'px';
return result;
}

Expand All @@ -163,14 +157,14 @@ export class AIPanelInput extends WithDisposable(LitElement) {
@state()
private accessor _hasContent = false;

@query('textarea')
private accessor _textarea!: HTMLTextAreaElement;

@property({ attribute: false })
accessor onFinish: ((input: string) => void) | undefined = undefined;

@property({ attribute: false })
accessor onInput: ((input: string) => void) | undefined = undefined;

@query('textarea')
accessor textarea!: HTMLTextAreaElement;
}

declare global {
Expand Down

0 comments on commit 68fd625

Please sign in to comment.