Skip to content

Commit

Permalink
feat: the editor supports command to add pictures (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
LIlGG committed May 6, 2024
1 parent 52b86e6 commit db6dc16
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
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;

0 comments on commit db6dc16

Please sign in to comment.