Skip to content

Commit

Permalink
[release-2.11] fix: can not input title using Chinese IME in editor (#…
Browse files Browse the repository at this point in the history
…4977)

* fix: can not input title using Chinese IME in editor

Signed-off-by: Ryan Wang <[email protected]>

* implement the handleGenerateTableOfContent using a decorator

* Rename plugin name

Signed-off-by: Ryan Wang <[email protected]>

---------

Signed-off-by: Ryan Wang <[email protected]>
Co-authored-by: Ryan Wang <[email protected]>
Co-authored-by: Li <[email protected]>
  • Loading branch information
3 people authored Dec 2, 2023
1 parent 276cbb5 commit 54f77d5
Showing 1 changed file with 44 additions and 47 deletions.
91 changes: 44 additions & 47 deletions console/src/components/editor/DefaultEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ import {
ExtensionNodeSelected,
ExtensionTrailingNode,
ToolbarItem,
Plugin,
PluginKey,
Decoration,
DecorationSet,
} from "@halo-dev/richtext-editor";
import {
IconCalendar,
Expand All @@ -67,7 +71,6 @@ import RiLayoutRightLine from "~icons/ri/layout-right-line";
import {
inject,
markRaw,
nextTick,
ref,
watch,
onMounted,
Expand All @@ -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();
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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" });
Expand All @@ -459,9 +459,6 @@ watch(
() => {
if (props.raw !== editor.value?.getHTML()) {
editor.value?.commands.setContent(props.raw);
nextTick(() => {
handleGenerateTableOfContent();
});
}
},
{
Expand Down

0 comments on commit 54f77d5

Please sign in to comment.