mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-19 15:26:59 +08:00
Initial bug report: Issue https://github.com/toeverything/AFFiNE/issues/13966 Description of bug: When a database header/title is in focus and the user presses ENTER, a new record is created and shown to the user. Expected outcome: When the user presses enter in the header title field, the new title should be applied and then the title field should loose focus. Short summary of fix: When the ENTER key is pressed within the title, the `onPressEnterKey()` function is called. As of now, this calls the function `this.dataViewLogic.addRow?.('start');` which creates a new record. In this fix, this has been changed to `this.input.blur()` which instead essentially switches focus away from the title field and does not create a new record, as expected. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Modified Enter key behavior in the database title field. Pressing Enter now blurs the input instead of automatically inserting a new row. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
200 lines
5.3 KiB
TypeScript
200 lines
5.3 KiB
TypeScript
import { unsafeCSSVarV2 } from '@blocksuite/affine-shared/theme';
|
|
import { stopPropagation } from '@blocksuite/affine-shared/utils';
|
|
import type { DataViewUILogicBase } from '@blocksuite/data-view';
|
|
import { SignalWatcher, WithDisposable } from '@blocksuite/global/lit';
|
|
import { ShadowlessElement } from '@blocksuite/std';
|
|
import type { Text } from '@blocksuite/store';
|
|
import { signal } from '@preact/signals-core';
|
|
import { css, html } from 'lit';
|
|
import { property, query } from 'lit/decorators.js';
|
|
import { classMap } from 'lit/directives/class-map.js';
|
|
import { styleMap } from 'lit/directives/style-map.js';
|
|
|
|
import type { DatabaseBlockComponent } from '../../database-block.js';
|
|
|
|
export class DatabaseTitle extends SignalWatcher(
|
|
WithDisposable(ShadowlessElement)
|
|
) {
|
|
static override styles = css`
|
|
.affine-database-title {
|
|
position: relative;
|
|
flex: 1;
|
|
font-family: inherit;
|
|
font-size: 20px;
|
|
line-height: 28px;
|
|
font-weight: 600;
|
|
color: var(--affine-text-primary-color);
|
|
overflow: hidden;
|
|
}
|
|
|
|
.affine-database-title textarea {
|
|
font-size: inherit;
|
|
line-height: inherit;
|
|
font-weight: inherit;
|
|
letter-spacing: inherit;
|
|
font-family: inherit;
|
|
border: none;
|
|
background-color: transparent;
|
|
padding: 0;
|
|
position: absolute;
|
|
left: 0;
|
|
top: 0;
|
|
bottom: 0;
|
|
right: 0;
|
|
outline: none;
|
|
resize: none;
|
|
scrollbar-width: none;
|
|
}
|
|
|
|
.affine-database-title .text {
|
|
user-select: none;
|
|
opacity: 0;
|
|
white-space: pre-wrap;
|
|
}
|
|
|
|
.affine-database-title[data-title-focus='false'] textarea {
|
|
opacity: 0;
|
|
}
|
|
|
|
.affine-database-title[data-title-focus='false'] .text {
|
|
text-overflow: ellipsis;
|
|
overflow: hidden;
|
|
opacity: 1;
|
|
white-space: pre;
|
|
}
|
|
|
|
.affine-database-title [data-title-empty='true']::before {
|
|
content: 'Untitled';
|
|
position: absolute;
|
|
pointer-events: none;
|
|
color: var(--affine-text-primary-color);
|
|
}
|
|
|
|
.affine-database-title [data-title-focus='true']::before {
|
|
color: var(--affine-placeholder-color);
|
|
}
|
|
|
|
.affine-database-title.comment-highlighted {
|
|
border-bottom: 2px solid
|
|
${unsafeCSSVarV2('block/comment/highlightUnderline')};
|
|
background-color: ${unsafeCSSVarV2('block/comment/highlightActive')};
|
|
}
|
|
`;
|
|
|
|
private readonly compositionEnd = () => {
|
|
this.isComposing$.value = false;
|
|
this.titleText.replace(0, this.titleText.length, this.input.value);
|
|
};
|
|
|
|
private readonly onBlur = () => {
|
|
this.isFocus$.value = false;
|
|
};
|
|
|
|
private readonly onFocus = () => {
|
|
this.isFocus$.value = true;
|
|
if (this.dataViewLogic.selection$.value) {
|
|
this.dataViewLogic.setSelection(undefined);
|
|
}
|
|
};
|
|
|
|
private readonly onInput = (e: InputEvent) => {
|
|
this.text$.value = this.input.value;
|
|
if (!e.isComposing) {
|
|
this.titleText.replace(0, this.titleText.length, this.input.value);
|
|
}
|
|
};
|
|
|
|
private readonly onKeyDown = (event: KeyboardEvent) => {
|
|
event.stopPropagation();
|
|
if (event.key === 'Enter' && !event.isComposing) {
|
|
event.preventDefault();
|
|
this.onPressEnterKey?.();
|
|
return;
|
|
}
|
|
};
|
|
|
|
updateText = () => {
|
|
if (!this.isFocus$.value) {
|
|
this.input.value = this.titleText.toString();
|
|
this.text$.value = this.input.value;
|
|
}
|
|
};
|
|
|
|
get database() {
|
|
return this.closest<DatabaseBlockComponent>('affine-database');
|
|
}
|
|
|
|
override connectedCallback() {
|
|
super.connectedCallback();
|
|
requestAnimationFrame(() => {
|
|
this.updateText();
|
|
});
|
|
this.titleText.yText.observe(this.updateText);
|
|
this.disposables.add(() => {
|
|
this.titleText.yText.unobserve(this.updateText);
|
|
});
|
|
}
|
|
|
|
override render() {
|
|
const isEmpty = !this.text$.value;
|
|
|
|
const classList = classMap({
|
|
'affine-database-title': true,
|
|
ellipsis: !this.isFocus$.value,
|
|
'comment-highlighted': this.database?.isCommentHighlighted ?? false,
|
|
});
|
|
const untitledStyle = styleMap({
|
|
height: isEmpty ? 'auto' : 0,
|
|
opacity: isEmpty && !this.isFocus$.value ? 1 : 0,
|
|
});
|
|
return html` <div
|
|
class="${classList}"
|
|
data-title-empty="${isEmpty}"
|
|
data-title-focus="${this.isFocus$.value}"
|
|
>
|
|
<div class="text" style="${untitledStyle}">Untitled</div>
|
|
<div class="text">${this.text$.value}</div>
|
|
<textarea
|
|
.disabled="${this.readonly$.value}"
|
|
@input="${this.onInput}"
|
|
@keydown="${this.onKeyDown}"
|
|
@copy="${stopPropagation}"
|
|
@paste="${stopPropagation}"
|
|
@focus="${this.onFocus}"
|
|
@blur="${this.onBlur}"
|
|
@compositionend="${this.compositionEnd}"
|
|
data-block-is-database-title="true"
|
|
title="${this.titleText.toString()}"
|
|
></textarea>
|
|
</div>`;
|
|
}
|
|
|
|
@query('textarea')
|
|
private accessor input!: HTMLTextAreaElement;
|
|
|
|
private readonly isComposing$ = signal(false);
|
|
private readonly isFocus$ = signal(false);
|
|
|
|
private onPressEnterKey() {
|
|
this.input.blur();
|
|
}
|
|
|
|
get readonly$() {
|
|
return this.dataViewLogic.view.readonly$;
|
|
}
|
|
|
|
private readonly text$ = signal('');
|
|
|
|
@property({ attribute: false })
|
|
accessor titleText!: Text;
|
|
|
|
@property({ attribute: false })
|
|
accessor dataViewLogic!: DataViewUILogicBase;
|
|
}
|
|
|
|
declare global {
|
|
interface HTMLElementTagNameMap {
|
|
'affine-database-title': DatabaseTitle;
|
|
}
|
|
}
|