fix(editor): enumerate ancestors (#9540)

This commit is contained in:
fourdim
2025-01-07 11:40:10 +00:00
parent 440239809c
commit ebaceb481d
3 changed files with 64 additions and 2 deletions

View File

@@ -3,9 +3,10 @@ import {
BlockHtmlAdapterExtension,
type BlockHtmlAdapterMatcher,
HastUtils,
type HtmlAST,
} from '@blocksuite/affine-shared/adapters';
import type { DeltaInsert } from '@blocksuite/inline';
import { nanoid } from '@blocksuite/store';
import { nanoid, type NodeProps } from '@blocksuite/store';
const paragraphBlockMatchTags = new Set([
'p',
@@ -22,6 +23,20 @@ const paragraphBlockMatchTags = new Set([
'footer',
]);
const tagsInAncestor = (o: NodeProps<HtmlAST>, tagNames: Array<string>) => {
let parent = o.parent;
while (parent) {
if (
HastUtils.isElement(parent.node) &&
tagNames.includes(parent.node.tagName)
) {
return true;
}
parent = parent.parent;
}
return false;
};
export const paragraphBlockHtmlAdapterMatcher: BlockHtmlAdapterMatcher = {
flavour: ParagraphBlockSchema.model.flavour,
toMatch: o =>
@@ -70,7 +85,7 @@ export const paragraphBlockHtmlAdapterMatcher: BlockHtmlAdapterMatcher = {
case 'footer': {
if (
o.parent?.node.type === 'element' &&
!['li', 'p'].includes(o.parent.node.tagName) &&
!tagsInAncestor(o, ['p', 'li']) &&
HastUtils.isParagraphLike(o.node)
) {
walkerContext