Skip to content

Commit

Permalink
refactor: remove legacy forward delete (#7987)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone authored Aug 15, 2024
1 parent c273e17 commit 1b2b60c
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 330 deletions.
1 change: 1 addition & 0 deletions packages/affine/shared/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export * from './rect.js';
export * from './reordering.js';
export * from './scroll-container.js';
export * from './string.js';
export * from './title.js';
export * from './url.js';
export * from './zod.js';
32 changes: 32 additions & 0 deletions packages/affine/shared/src/utils/title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { EditorHost } from '@blocksuite/block-std';
import type { InlineEditor } from '@blocksuite/inline';

function getDocTitleByEditorHost(editorHost: EditorHost): HTMLElement | null {
const docViewport = editorHost.closest('.affine-page-viewport');
if (!docViewport) return null;
return docViewport.querySelector('doc-title');
}

export function getDocTitleInlineEditor(
editorHost: EditorHost
): InlineEditor | null {
const docTitle = getDocTitleByEditorHost(editorHost);
if (!docTitle) return null;
const titleRichText = docTitle.querySelector<
HTMLElement & { inlineEditor: InlineEditor }
>('rich-text');
if (!titleRichText || !titleRichText.inlineEditor) return null;
return titleRichText.inlineEditor;
}

export function focusTitle(editorHost: EditorHost, index = Infinity, len = 0) {
const titleInlineEditor = getDocTitleInlineEditor(editorHost);
if (!titleInlineEditor) {
return;
}

if (index > titleInlineEditor.yText.length) {
index = titleInlineEditor.yText.length;
}
titleInlineEditor.setInlineRange({ index, length: len });
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
textFormatConfigs,
} from '@blocksuite/affine-components/rich-text';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { IS_MAC } from '@blocksuite/global/env';
import { INLINE_ROOT_ATTR, type InlineRootElement } from '@blocksuite/inline';

import type { RootBlockComponent } from '../../../../root-block/types.js';
Expand All @@ -25,7 +24,7 @@ import {
handleUnindent,
} from '../rich-text-operations.js';
import { bracketPairs } from './bracket-pairs.js';
import { hardEnter, onForwardDelete } from './legacy.js';
import { hardEnter } from './legacy.js';

// FIXME: use selection manager to set selection
export const bindContainerHotkey = (block: BlockComponent) => {
Expand Down Expand Up @@ -310,10 +309,6 @@ export const bindContainerHotkey = (block: BlockComponent) => {
}
return true;
},
Delete: ctx => handleDelete(ctx),
'Control-d': ctx => {
if (IS_MAC) handleDelete(ctx);
},
});

textFormatConfigs.forEach(config => {
Expand Down Expand Up @@ -349,17 +344,6 @@ export const bindContainerHotkey = (block: BlockComponent) => {
return;
}

function handleDelete(ctx: UIEventStateContext) {
if (!block.selected?.is('text')) return;
const state = ctx.get('keyboardState');
const inlineEditor = _getInlineEditor();
if (!inlineEditor) return;
if (!onForwardDelete(editorHost, model, state.raw, inlineEditor)) {
_preventDefault(ctx);
}
return true;
}

function tryConvertToLinkedDoc() {
const root = model.doc.root;
if (!root) return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
import type { AffineInlineEditor } from '@blocksuite/affine-components/rich-text';
import type { EditorHost } from '@blocksuite/block-std';
import type { BlockModel } from '@blocksuite/store';

import { assertExists } from '@blocksuite/global/utils';
import {
type InlineRange,
KEYBOARD_ALLOW_DEFAULT,
KEYBOARD_PREVENT_DEFAULT,
} from '@blocksuite/inline';
import { type InlineRange, KEYBOARD_PREVENT_DEFAULT } from '@blocksuite/inline';

import {
handleBlockEndEnter,
handleBlockSplit,
handleLineEndForwardDelete,
} from '../rich-text-operations.js';

function isCollapsedAtBlockEnd(inlineEditor: AffineInlineEditor) {
const inlineRange = inlineEditor.getInlineRange();
return (
inlineRange?.index === inlineEditor.yText.length &&
inlineRange?.length === 0
);
}

export function hardEnter(
editorHost: EditorHost,
model: BlockModel,
Expand All @@ -40,17 +26,3 @@ export function hardEnter(
handleBlockSplit(editorHost, model, range.index, range.length);
return KEYBOARD_PREVENT_DEFAULT;
}

export function onForwardDelete(
editorHost: EditorHost,
model: BlockModel,
e: KeyboardEvent,
inlineEditor: AffineInlineEditor
) {
e.stopPropagation();
if (isCollapsedAtBlockEnd(inlineEditor)) {
handleLineEndForwardDelete(editorHost, model);
return KEYBOARD_PREVENT_DEFAULT;
}
return KEYBOARD_ALLOW_DEFAULT;
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
import type {
ListBlockModel,
ParagraphBlockModel,
RootBlockModel,
} from '@blocksuite/affine-model';
import type { EditorHost } from '@blocksuite/block-std';
import type { Doc } from '@blocksuite/store';

import { focusTextModel } from '@blocksuite/affine-components/rich-text';
import {
getNextContentBlock,
isInsideBlockByFlavour,
matchFlavours,
} from '@blocksuite/affine-shared/utils';
import { matchFlavours } from '@blocksuite/affine-shared/utils';
import { type BlockModel, Text } from '@blocksuite/store';

import type { ExtendedModel } from '../../types.js';

import {
getBlockComponentByModel,
getDocTitleByEditorHost,
} from '../../../_common/utils/query.js';
import { focusTitle } from '../../../_common/utils/selection.js';
import { EMBED_BLOCK_FLAVOUR_LIST } from '../../consts.js';

/**
* Whether the block supports rendering its children.
*/
Expand Down Expand Up @@ -329,227 +316,3 @@ export function handleRemoveAllIndentForMultiBlocks(
}
}
}

function handleDatabaseBlockForwardDelete(model: ExtendedModel) {
const doc = model.doc;
return isInsideBlockByFlavour(doc, model, 'affine:database');
}

export function handleNoPreviousSibling(
editorHost: EditorHost,
model: ExtendedModel
) {
const doc = model.doc;
const text = model.text;
const parent = doc.getParent(model);
if (!parent) return false;
const titleElement = getDocTitleByEditorHost(
editorHost
) as HTMLTextAreaElement | null;
// Probably no title, e.g. in edgeless mode
if (!titleElement) {
if (
matchFlavours(parent, ['affine:edgeless-text']) ||
model.children.length > 0
) {
doc.deleteBlock(model, {
bringChildrenTo: parent,
});
return true;
}
return false;
}

const rootModel = model.doc.root as RootBlockModel;
const title = rootModel.title;

doc.captureSync();
let textLength = 0;
if (text) {
textLength = text.length;
title.join(text);
}

// Preserve at least one block to be able to focus on container click
if (doc.getNext(model) || model.children.length > 0) {
const parent = doc.getParent(model);
if (!parent) return false;
doc.deleteBlock(model, {
bringChildrenTo: parent,
});
} else {
text?.clear();
}
focusTitle(editorHost, title.length - textLength);
return true;
}

function handleParagraphBlockForwardDelete(
editorHost: EditorHost,
model: ExtendedModel
) {
const doc = model.doc;
function handleParagraphOrList(
doc: Doc,
model: ExtendedModel,
nextSibling: ExtendedModel | null,
firstChild: ExtendedModel | null
) {
function handleParagraphOrListSibling(
doc: Doc,
model: ExtendedModel,
nextSibling: ExtendedModel | null
) {
if (nextSibling && matchFlavours(nextSibling, ['affine:paragraph'])) {
model.text?.join(nextSibling.text as Text);
if (nextSibling.children) {
const parent = doc.getParent(nextSibling);
if (!parent) return false;
doc.moveBlocks(nextSibling.children, parent, model, false);
doc.deleteBlock(nextSibling);
return true;
} else {
doc.deleteBlock(nextSibling);
return true;
}
} else {
const nextBlock = getNextContentBlock(editorHost, model);
if (!nextBlock || !matchFlavours(nextBlock, ['affine:paragraph']))
return false;
model.text?.join(nextBlock.text as Text);
if (nextBlock.children) {
const parent = doc.getParent(nextBlock);
if (!parent) return false;
doc.moveBlocks(
nextBlock.children,
parent,
doc.getParent(model),
false
);
doc.deleteBlock(nextBlock);
return true;
} else {
doc.deleteBlock(nextBlock);
return true;
}
}
}
function handleParagraphOrListChild(
doc: Doc,
model: ExtendedModel,
firstChild: ExtendedModel | null
) {
if (!firstChild || !matchFlavours(firstChild, ['affine:paragraph'])) {
return false;
}
const grandChildren = firstChild.children;
model.text?.join(firstChild.text as Text);
if (grandChildren) {
doc.moveBlocks(grandChildren, model);
}
doc.deleteBlock(firstChild);
return true;
}
const nextBlock = getNextContentBlock(editorHost, model);
if (!firstChild && !nextBlock) return true;
return (
handleParagraphOrListChild(doc, model, firstChild) ||
handleParagraphOrListSibling(doc, model, nextSibling)
);
}
function handleEmbedDividerCode(
nextSibling: ExtendedModel | null,
firstChild: ExtendedModel | null
) {
function handleEmbedDividerCodeChild(firstChild: ExtendedModel | null) {
if (
!firstChild ||
!matchFlavours(firstChild, [
'affine:image',
'affine:divider',
'affine:code',
])
)
return false;
focusTextModel(
editorHost.std,
firstChild.id,
firstChild.text?.yText.length
);
return true;
}
function handleEmbedDividerCodeSibling(nextSibling: ExtendedModel | null) {
if (matchFlavours(nextSibling, ['affine:divider'])) {
const nextSiblingComponent = getBlockComponentByModel(
editorHost,
nextSibling
);
if (!nextSiblingComponent) return false;
editorHost.selection.setGroup('note', [
editorHost.selection.create('block', {
blockId: nextSiblingComponent.blockId,
}),
]);
return true;
}

if (
!nextSibling ||
!matchFlavours(nextSibling, ['affine:image', 'affine:code'])
)
return false;
focusTextModel(
editorHost.std,
nextSibling.id,
nextSibling.text?.yText.length
);
return true;
}
return (
handleEmbedDividerCodeChild(firstChild) ||
handleEmbedDividerCodeSibling(nextSibling)
);
}

if (!matchFlavours(model, ['affine:paragraph'])) return false;

const parent = doc.getParent(model);
if (!parent) return false;
const nextSibling = doc.getNext(model);
const firstChild = model.firstChild();
if (matchFlavours(parent, ['affine:database'])) {
// TODO
return false;
} else {
const ignoreForwardDeleteFlavourList: BlockSuite.Flavour[] = [
'affine:database',
'affine:image',
'affine:code',
'affine:attachment',
'affine:bookmark',
...EMBED_BLOCK_FLAVOUR_LIST,
];

if (
nextSibling &&
matchFlavours(nextSibling, ignoreForwardDeleteFlavourList)
) {
return true;
}

return (
handleParagraphOrList(doc, model, nextSibling, firstChild) ||
handleEmbedDividerCode(nextSibling, firstChild)
);
}
}

export function handleLineEndForwardDelete(
editorHost: EditorHost,
model: ExtendedModel
) {
if (handleParagraphBlockForwardDelete(editorHost, model)) {
handleDatabaseBlockForwardDelete(model);
return;
}
}
1 change: 0 additions & 1 deletion packages/blocks/src/_common/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from '../types.js';
export * from './drag-and-drop.js';
export * from './init.js';
export * from './query.js';
export * from './selection.js';
export {
Rect,
clearMarksOnDiscontinuousInput,
Expand Down
Loading

0 comments on commit 1b2b60c

Please sign in to comment.