fix: can not input title using Chinese IME in editor (#4975)

#### What type of PR is this?

/kind bug
/area console
/milestone 2.12.x

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

修复在默认编辑器中无法使用拼音输入法输入标题的问题,此问题由 https://github.com/halo-dev/halo/pull/4909 引发。

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

Fixes #4970 

#### Special notes for your reviewer:

在编辑器中,使用拼音输入法输入标题(heading),观察是否可以正常输入。

#### Does this PR introduce a user-facing change?

```release-note
修复在默认编辑器中无法使用拼音输入法输入标题的问题。
```
pull/4983/head
Ryan Wang 2023-12-02 22:30:07 +08:00 committed by GitHub
parent f5dcc622f8
commit 551f2ae5ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 47 deletions

View File

@ -45,6 +45,10 @@ import {
ExtensionNodeSelected,
ExtensionTrailingNode,
ToolbarItem,
Plugin,
PluginKey,
Decoration,
DecorationSet,
} from "@halo-dev/richtext-editor";
import {
IconCalendar,
@ -67,7 +71,6 @@ import RiLayoutRightLine from "~icons/ri/layout-right-line";
import {
inject,
markRaw,
nextTick,
ref,
watch,
onMounted,
@ -86,8 +89,8 @@ import { usePluginModuleStore } from "@/stores/plugin";
import type { PluginModule } from "@halo-dev/console-shared";
import { useDebounceFn, useLocalStorage } from "@vueuse/core";
import { onBeforeUnmount } from "vue";
import { generateAnchor } from "@/utils/anchor";
import { usePermission } from "@/utils/permission";
import { generateAnchor } from "@/utils/anchor";
const { t } = useI18n();
const { currentUserHasPermission } = usePermission();
@ -288,13 +291,49 @@ onMounted(() => {
ExtensionColumn,
ExtensionNodeSelected,
ExtensionTrailingNode,
Extension.create({
addProseMirrorPlugins() {
return [
new Plugin({
key: new PluginKey("generate-heading-id"),
props: {
decorations: (state) => {
const headings: HeadingNode[] = [];
const { doc } = state;
const decorations: Decoration[] = [];
doc.descendants((node, pos) => {
if (node.type.name === ExtensionHeading.name) {
const id = generateAnchor(node.textContent);
if (node.attrs.id !== id) {
decorations.push(
Decoration.node(pos, pos + node.nodeSize, {
id,
})
);
}
headings.push({
level: node.attrs.level,
text: node.textContent,
id,
});
}
});
headingNodes.value = headings;
if (!selectedHeadingNode.value) {
selectedHeadingNode.value = headings[0];
}
return DecorationSet.create(doc, decorations);
},
},
}),
];
},
}),
],
autofocus: "start",
onUpdate: () => {
debounceOnUpdate();
nextTick(() => {
handleGenerateTableOfContent();
});
},
editorProps: {
handleDrop: (view, event: DragEvent, _, moved) => {
@ -408,45 +447,6 @@ async function asyncWorker(arg: Task): Promise<void> {
}
}
const handleGenerateTableOfContent = () => {
if (!editor.value) {
return;
}
const headings: HeadingNode[] = [];
const transaction = editor.value.state.tr;
editor.value.state.doc.descendants((node, pos) => {
if (node.type.name === "heading") {
const id = generateAnchor(node.textContent);
if (node.attrs.id !== id) {
transaction?.setNodeMarkup(pos, undefined, {
...node.attrs,
id,
});
}
headings.push({
level: node.attrs.level,
text: node.textContent,
id,
});
}
});
transaction.setMeta("addToHistory", false);
transaction.setMeta("preventUpdate", true);
editor.value.view.dispatch(transaction);
headingNodes.value = headings;
if (!selectedHeadingNode.value) {
selectedHeadingNode.value = headings[0];
}
};
const handleSelectHeadingNode = (node: HeadingNode) => {
selectedHeadingNode.value = node;
document.getElementById(node.id)?.scrollIntoView({ behavior: "smooth" });
@ -459,9 +459,6 @@ watch(
() => {
if (props.raw !== editor.value?.getHTML()) {
editor.value?.commands.setContent(props.raw);
nextTick(() => {
handleGenerateTableOfContent();
});
}
},
{