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

feat: the editor supports command to add pictures #100

Merged
merged 1 commit into from
May 6, 2024
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
4 changes: 2 additions & 2 deletions console/src/components/TextEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
ExtensionOrderedList,
ExtensionStrike,
ExtensionText,
ExtensionImage,
ExtensionTaskList,
ExtensionLink,
ExtensionTextAlign,
Expand Down Expand Up @@ -45,6 +44,7 @@ import {
} from "@halo-dev/richtext-editor";
import { watch } from "vue";
import { TagsExtension } from "@/extensions/tags";
import MomentExtensionImage from "@/extensions/images";

const props = withDefaults(
defineProps<{
Expand Down Expand Up @@ -88,7 +88,7 @@ const editor = useEditor({
ExtensionOrderedList,
ExtensionStrike,
ExtensionText,
ExtensionImage.configure({
MomentExtensionImage.configure({
inline: true,
allowBase64: false,
HTMLAttributes: {
Expand Down
41 changes: 41 additions & 0 deletions console/src/extensions/images/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Editor, ExtensionImage, type Range } from "@halo-dev/richtext-editor";
import type { ExtensionOptions } from "@halo-dev/richtext-editor/dist/types";
import { markRaw } from "vue";
import MdiFileImageBox from "~icons/mdi/file-image-box";

export interface ImageOptions {
inline: boolean;
allowBase64: boolean;
HTMLAttributes: Record<string, unknown>;
}

const MomentExtensionImage = ExtensionImage.extend<
ExtensionOptions & ImageOptions
>({
addOptions() {
return {
...this.parent?.(),
getCommandMenuItems() {
return {
priority: 100,
icon: markRaw(MdiFileImageBox),
title: "图片",
keywords: ["image", "tupian"],
command: ({ editor, range }: { editor: Editor; range: Range }) => {
editor
.chain()
.focus()
.deleteRange(range)
.insertContent([
{ type: "image", attrs: { src: "" } },
{ type: "paragraph", content: "" },
])
.run();
},
};
},
};
},
});

export default MomentExtensionImage;