fix: resolve error when adding tables in default editor (#6587)

#### What type of PR is this?

/kind bug
/area ui
/milestone 2.20.x

#### What this PR does / why we need it:

在过滤默认编辑器重复扩展时,移除将扩展扁平化的操作。并且若扩展没有 name,则不对其进行过滤,用于解决一些潜在的问题。

#### How to test it?

测试在默认编辑器中新增 table ,是否会出现报错。

#### Which issue(s) this PR fixes:

Fixes #6585 

#### Does this PR introduce a user-facing change?
```release-note
解决在默认编辑器中添加表格报错的问题
```
pull/6590/head
Takagi 2024-09-04 22:11:44 +08:00 committed by GitHub
parent 7991ef8cf1
commit 733679d5ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 37 deletions

View File

@ -1,3 +1,4 @@
import { randomUUID } from "@/utils/id";
import { import {
getExtensionField, getExtensionField,
type AnyConfig, type AnyConfig,
@ -10,12 +11,17 @@ export function useExtension() {
if (!extensions) { if (!extensions) {
return; return;
} }
const resolvedExtensions = sort(extensions);
const resolvedExtensions = sort(flatten(extensions));
const map = new Map<string, AnyExtension>(); const map = new Map<string, AnyExtension>();
resolvedExtensions.forEach((extension) => { resolvedExtensions.forEach((extension) => {
if (!extension.name) {
console.warn(
`Extension name is missing for Extension, type: ${extension.type}.`
);
const key = randomUUID().toString();
map.set(key, extension);
return;
}
const key = `${extension.type}-${extension.name}`; const key = `${extension.type}-${extension.name}`;
if (map.has(key)) { if (map.has(key)) {
console.warn( console.warn(
@ -24,42 +30,9 @@ export function useExtension() {
} }
map.set(key, extension); map.set(key, extension);
}); });
return Array.from(map.values()); return Array.from(map.values());
}; };
/**
* Create a flattened array of extensions by traversing the `addExtensions` field.
* @param extensions An array of Tiptap extensions
* @returns A flattened array of Tiptap extensions
*/
const flatten = (extensions: Extensions): Extensions => {
return (
extensions
.map((extension) => {
const context = {
name: extension.name,
options: extension.options,
storage: extension.storage,
};
const addExtensions = getExtensionField<AnyConfig["addExtensions"]>(
extension,
"addExtensions",
context
);
if (addExtensions) {
return [extension, ...flatten(addExtensions())];
}
return extension;
})
// `Infinity` will break TypeScript so we set a number that is probably high enough
.flat(10)
);
};
/** /**
* Sort extensions by priority. * Sort extensions by priority.
* @param extensions An array of Tiptap extensions * @param extensions An array of Tiptap extensions