mirror of https://github.com/halo-dev/halo
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
parent
f5dcc622f8
commit
551f2ae5ed
|
@ -45,6 +45,10 @@ import {
|
||||||
ExtensionNodeSelected,
|
ExtensionNodeSelected,
|
||||||
ExtensionTrailingNode,
|
ExtensionTrailingNode,
|
||||||
ToolbarItem,
|
ToolbarItem,
|
||||||
|
Plugin,
|
||||||
|
PluginKey,
|
||||||
|
Decoration,
|
||||||
|
DecorationSet,
|
||||||
} from "@halo-dev/richtext-editor";
|
} from "@halo-dev/richtext-editor";
|
||||||
import {
|
import {
|
||||||
IconCalendar,
|
IconCalendar,
|
||||||
|
@ -67,7 +71,6 @@ import RiLayoutRightLine from "~icons/ri/layout-right-line";
|
||||||
import {
|
import {
|
||||||
inject,
|
inject,
|
||||||
markRaw,
|
markRaw,
|
||||||
nextTick,
|
|
||||||
ref,
|
ref,
|
||||||
watch,
|
watch,
|
||||||
onMounted,
|
onMounted,
|
||||||
|
@ -86,8 +89,8 @@ import { usePluginModuleStore } from "@/stores/plugin";
|
||||||
import type { PluginModule } from "@halo-dev/console-shared";
|
import type { PluginModule } from "@halo-dev/console-shared";
|
||||||
import { useDebounceFn, useLocalStorage } from "@vueuse/core";
|
import { useDebounceFn, useLocalStorage } from "@vueuse/core";
|
||||||
import { onBeforeUnmount } from "vue";
|
import { onBeforeUnmount } from "vue";
|
||||||
import { generateAnchor } from "@/utils/anchor";
|
|
||||||
import { usePermission } from "@/utils/permission";
|
import { usePermission } from "@/utils/permission";
|
||||||
|
import { generateAnchor } from "@/utils/anchor";
|
||||||
|
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
const { currentUserHasPermission } = usePermission();
|
const { currentUserHasPermission } = usePermission();
|
||||||
|
@ -288,13 +291,49 @@ onMounted(() => {
|
||||||
ExtensionColumn,
|
ExtensionColumn,
|
||||||
ExtensionNodeSelected,
|
ExtensionNodeSelected,
|
||||||
ExtensionTrailingNode,
|
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",
|
autofocus: "start",
|
||||||
onUpdate: () => {
|
onUpdate: () => {
|
||||||
debounceOnUpdate();
|
debounceOnUpdate();
|
||||||
nextTick(() => {
|
|
||||||
handleGenerateTableOfContent();
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
editorProps: {
|
editorProps: {
|
||||||
handleDrop: (view, event: DragEvent, _, moved) => {
|
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) => {
|
const handleSelectHeadingNode = (node: HeadingNode) => {
|
||||||
selectedHeadingNode.value = node;
|
selectedHeadingNode.value = node;
|
||||||
document.getElementById(node.id)?.scrollIntoView({ behavior: "smooth" });
|
document.getElementById(node.id)?.scrollIntoView({ behavior: "smooth" });
|
||||||
|
@ -459,9 +459,6 @@ watch(
|
||||||
() => {
|
() => {
|
||||||
if (props.raw !== editor.value?.getHTML()) {
|
if (props.raw !== editor.value?.getHTML()) {
|
||||||
editor.value?.commands.setContent(props.raw);
|
editor.value?.commands.setContent(props.raw);
|
||||||
nextTick(() => {
|
|
||||||
handleGenerateTableOfContent();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue