Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve the issue of generated titles missing id #4997

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion console/packages/editor/src/extensions/heading/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Editor, Range } from "@/tiptap/vue-3";
import { mergeAttributes, type Editor, type Range } from "@/tiptap/vue-3";
import TiptapParagraph from "@/extensions/paragraph";
import TiptapHeading from "@tiptap/extension-heading";
import type { HeadingOptions } from "@tiptap/extension-heading";
Expand All @@ -15,8 +15,24 @@ import MdiFormatHeader6 from "~icons/mdi/format-header-6";
import { markRaw } from "vue";
import { i18n } from "@/locales";
import type { ExtensionOptions } from "@/types";
import { Decoration, DecorationSet, Plugin, PluginKey } from "@/tiptap";
import { ExtensionHeading } from "..";
import { generateAnchor } from "@/utils";

const Blockquote = TiptapHeading.extend<ExtensionOptions & HeadingOptions>({
renderHTML({ node, HTMLAttributes }) {
const hasLevel = this.options.levels.includes(node.attrs.level);
const level = hasLevel ? node.attrs.level : this.options.levels[0];
const id = generateAnchor(node.textContent);
HTMLAttributes.id = id;

return [
`h${level}`,
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
0,
];
},

addOptions() {
return {
...this.parent?.(),
Expand Down Expand Up @@ -265,6 +281,32 @@ const Blockquote = TiptapHeading.extend<ExtensionOptions & HeadingOptions>({
addExtensions() {
return [TiptapParagraph];
},
addProseMirrorPlugins() {
return [
new Plugin({
key: new PluginKey("generate-heading-id"),
props: {
decorations: (state) => {
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,
})
);
}
}
});
return DecorationSet.create(doc, decorations);
},
},
}),
];
},
});

export default Blockquote;
1 change: 1 addition & 0 deletions console/packages/editor/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./delete-node";
export * from "./anchor";
20 changes: 4 additions & 16 deletions console/src/components/editor/DefaultEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import {
ToolbarItem,
Plugin,
PluginKey,
Decoration,
DecorationSet,
} from "@halo-dev/richtext-editor";
import {
Expand Down Expand Up @@ -90,7 +89,6 @@ import type { PluginModule } from "@halo-dev/console-shared";
import { useDebounceFn, useLocalStorage } from "@vueuse/core";
import { onBeforeUnmount } from "vue";
import { usePermission } from "@/utils/permission";
import { generateAnchor } from "@/utils/anchor";

const { t } = useI18n();
const { currentUserHasPermission } = usePermission();
Expand Down Expand Up @@ -295,35 +293,25 @@ onMounted(() => {
addProseMirrorPlugins() {
return [
new Plugin({
key: new PluginKey("generate-heading-id"),
key: new PluginKey("get-heading-id"),
props: {
decorations: (state) => {
const headings: HeadingNode[] = [];
const { doc } = state;
const decorations: Decoration[] = [];
doc.descendants((node, pos) => {
doc.descendants((node) => {
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,
id: node.attrs.id,
});
}
});
headingNodes.value = headings;
if (!selectedHeadingNode.value) {
selectedHeadingNode.value = headings[0];
}
return DecorationSet.create(doc, decorations);
return DecorationSet.empty;
},
},
}),
Expand Down
28 changes: 0 additions & 28 deletions console/src/utils/__tests__/anchor.spec.ts

This file was deleted.

Loading