Skip to content

Commit

Permalink
fix: wrap node behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
WindRunnerMax committed May 26, 2024
1 parent 755cd9e commit 8097086
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/utils/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const getAboveNode = (
) => {
// 这里与`getBlockNode`最大区别是会有`self`的判断
// 这是很重要的行为 特别是在类似于`Normalizer`的场景中
// https://github.com/ianstormtaylor/slate/blob/25be3b/packages/slate/src/interfaces/editor.ts#L334
const { at = editor.selection, match, self = true } = options || {};
if (!at) return void 0;
const path = Editor.path(editor, at);
Expand Down
9 changes: 4 additions & 5 deletions packages/utils/src/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ export const setBlockNode = (
const { at: location, key } = options;
if (!node) {
// 注意`setNodes Match`的查找顺序可能与直觉不一致 顺序是由顶`Editor`至底`Node`
// https://github.com/ianstormtaylor/slate/blob/25be3b/packages/slate/src/transforms/node.ts#L565
// 因此这里需要使用`Editor.above`实现更精确的查找 再将`node`直接传入来精确变换
// https://github.com/ianstormtaylor/slate/blob/25be3b/packages/slate/src/interfaces/editor.ts#L334
const above = getAboveNode(editor, {
at: location,
match: node => isBlock(editor, node) && (key ? existKey(node, key) : true),
});
if (above && above.node) node = above.node as BlockElement;
}
if (!node) return void 0;
// https://github.com/ianstormtaylor/slate/blob/25be3b/packages/slate/src/transforms/node.ts#L565
Transforms.setNodes(editor, config, { match: n => n === node, at: location });
};

Expand Down Expand Up @@ -92,10 +91,10 @@ export const setWrapNodes = (
} = {}
) => {
const { at } = options;
const config = { ...wrapConfig } as BlockElement;
Editor.withoutNormalizing(editor, () => {
Transforms.wrapNodes(editor, config, { match: n => isBlock(editor, n), at });
setBlockNode(editor, itemConfig, { at });
// 配合`Normalizer`在此处的规则是新建内层`Node`
setBlockNode(editor, wrapConfig, { at });
Transforms.wrapNodes(editor, itemConfig as BlockElement, { match: isText, at });
});
};

Expand Down

0 comments on commit 8097086

Please sign in to comment.