Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
feat: add tooltip to menus
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Nov 27, 2022
1 parent dbd185b commit b3e0f07
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 18 deletions.
5 changes: 5 additions & 0 deletions example/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
ExtensionSubscript,
ExtensionSuperscript,
ExtensionPlaceholder,
ExtensionHighlight,
ExtensionCommands,
ExtensionIframe,
CommandsSuggestion,
Expand Down Expand Up @@ -68,6 +69,7 @@ import {
BulletListMenuItem,
OrderedListMenuItem,
TaskListMenuItem,
HighlightMenuItem,
} from "@halo-dev/richtext-editor";
const content = ref("");
Expand Down Expand Up @@ -116,6 +118,7 @@ const editor = useEditor({
ExtensionPlaceholder.configure({
placeholder: "输入 / 以选择输入类型",
}),
ExtensionHighlight,
ExtensionCommands.configure({
suggestion: {
...CommandsSuggestion,
Expand Down Expand Up @@ -169,6 +172,7 @@ const toolbarMenuItems = computed(() => {
ItalicMenuItem(editor.value),
UnderlineMenuItem(editor.value),
StrikeMenuItem(editor.value),
HighlightMenuItem(editor.value),
QuoteMenuItem(editor.value),
CodeMenuItem(editor.value),
SuperScriptMenuItem(editor.value),
Expand All @@ -192,6 +196,7 @@ const bubbleMenuItems = computed(() => {
ItalicMenuItem(editor.value),
UnderlineMenuItem(editor.value),
StrikeMenuItem(editor.value),
HighlightMenuItem(editor.value),
QuoteMenuItem(editor.value),
CodeMenuItem(editor.value),
CodeBlockMenuItem(editor.value),
Expand Down
1 change: 1 addition & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@tiptap/extension-gapcursor": "^2.0.0-beta.202",
"@tiptap/extension-hard-break": "^2.0.0-beta.202",
"@tiptap/extension-heading": "^2.0.0-beta.202",
"@tiptap/extension-highlight": "2.0.0-beta.204",
"@tiptap/extension-history": "^2.0.0-beta.202",
"@tiptap/extension-horizontal-rule": "^2.0.0-beta.202",
"@tiptap/extension-image": "2.0.0-beta.202",
Expand Down
4 changes: 3 additions & 1 deletion packages/editor/src/components/EditorBubbleMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import "tippy.js/dist/svg-arrow.css";
import type { PropType } from "vue";
import { BubbleMenu, Editor } from "@tiptap/vue-3";
import type { MenuItem } from "@/types";
import { VTooltip } from "floating-vue";
defineProps({
editor: {
type: Object as PropType<Editor>,
Expand Down Expand Up @@ -32,7 +34,7 @@ defineProps({
class="text-gray-600 text-lg hover:bg-gray-100 p-0.5 rounded-sm"
@click="menuItem.action?.()"
>
<component :is="menuItem.icon" />
<component :is="menuItem.icon" v-tooltip="menuItem.title" />
</button>
</div>
</bubble-menu>
Expand Down
3 changes: 2 additions & 1 deletion packages/editor/src/components/EditorHeader.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts" setup>
import type { PropType } from "vue";
import { Menu as VMenu } from "floating-vue";
import { Menu as VMenu, VTooltip } from "floating-vue";
import type { MenuItem } from "@/types";
defineProps({
menuItems: {
Expand All @@ -21,6 +21,7 @@ defineProps({
>
<button
v-if="!menuItem.children?.length"
v-tooltip="menuItem.title"
:class="{ 'bg-gray-200': menuItem.isActive?.() }"
class="hover:bg-gray-100 p-1 rounded-sm"
@click="menuItem.action"
Expand Down
3 changes: 3 additions & 0 deletions packages/editor/src/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import ExtensionUnderline from "@tiptap/extension-underline";
import ExtensionSubscript from "@tiptap/extension-subscript";
import ExtensionSuperscript from "@tiptap/extension-superscript";
import ExtensionPlaceholder from "@tiptap/extension-placeholder";
import ExtensionHighlight from "@tiptap/extension-highlight";

// Custom extensions
import {
Expand Down Expand Up @@ -56,6 +57,7 @@ const allExtensions = [
ExtensionImage,
ExtensionTaskList,
ExtensionTaskItem,
ExtensionHighlight,
ExtensionLink.configure({
autolink: true,
openOnClick: false,
Expand Down Expand Up @@ -110,6 +112,7 @@ export {
ExtensionSubscript,
ExtensionSuperscript,
ExtensionPlaceholder,
ExtensionHighlight,
ExtensionCommands,
CommandsSuggestion,
ExtensionCodeBlock,
Expand Down
43 changes: 27 additions & 16 deletions packages/editor/src/menus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import MdiTableSplitCell from "~icons/mdi/table-split-cell";
import MdiFormatListBulleted from "~icons/mdi/format-list-bulleted";
import MdiFormatListCheckbox from "~icons/mdi/format-list-checkbox";
import MdiFormatListNumbered from "~icons/mdi/format-list-numbered";
import MdiFormatColorHighlight from "~icons/mdi/format-color-highlight";

export interface MenuItem {
type: "button" | "separator";
Expand All @@ -54,7 +55,7 @@ export function UndoMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiUndoVariant,
title: "Undo",
title: "撤销",
action: () => editor.chain().undo().run(),
isActive: () => false,
};
Expand All @@ -64,7 +65,7 @@ export function RedoMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiRedoVariant,
title: "Redo",
title: "恢复",
action: () => editor.chain().redo().run(),
isActive: () => false,
};
Expand All @@ -74,7 +75,7 @@ export function BoldMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatBold,
title: "Bold",
title: "粗体",
action: () => editor.chain().focus().toggleBold().run(),
isActive: () => editor.isActive("bold"),
};
Expand All @@ -84,7 +85,7 @@ export function ItalicMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatItalic,
title: "Italic",
title: "斜体",
action: () => editor.chain().focus().toggleItalic().run(),
isActive: () => editor.isActive("italic"),
};
Expand All @@ -94,7 +95,7 @@ export function UnderlineMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatUnderline,
title: "Underline",
title: "下划线",
action: () => editor.chain().focus().toggleUnderline().run(),
isActive: () => editor.isActive("underline"),
};
Expand All @@ -104,7 +105,7 @@ export function StrikeMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatStrikethrough,
title: "Strikethrough",
title: "删除线",
action: () => editor.chain().focus().toggleStrike().run(),
isActive: () => editor.isActive("strike"),
};
Expand All @@ -114,7 +115,7 @@ export function QuoteMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatQuoteOpen,
title: "Quote",
title: "引用",
action: () => editor.chain().focus().toggleBlockquote().run(),
isActive: () => editor.isActive("blockquote"),
};
Expand All @@ -124,7 +125,7 @@ export function CodeMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiCodeTags,
title: "Code",
title: "行内代码",
action: () => editor.chain().focus().toggleCode().run(),
isActive: () => editor.isActive("code"),
};
Expand All @@ -134,7 +135,7 @@ export function SuperScriptMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatSuperscript,
title: "Superscript",
title: "上角标",
action: () => editor.chain().focus().toggleSuperscript().run(),
isActive: () => editor.isActive("superscript"),
};
Expand All @@ -144,7 +145,7 @@ export function SubScriptMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatSubscript,
title: "Subscript",
title: "下角标",
action: () => editor.chain().focus().toggleSubscript().run(),
isActive: () => editor.isActive("subscript"),
};
Expand All @@ -154,7 +155,7 @@ export function CodeBlockMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiCodeBracesBox,
title: "Code Block",
title: "代码块",
action: () => editor.chain().focus().toggleCodeBlock().run(),
isActive: () => editor.isActive("codeBlock"),
};
Expand All @@ -164,7 +165,7 @@ export function HeadingMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatHeaderPound,
title: "普通文本",
title: "文本类型",
isActive: () => editor.isActive("heading") || editor.isActive("paragraph"),
children: [
{
Expand Down Expand Up @@ -224,7 +225,7 @@ export function AlignLeftMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatAlignLeft,
title: "Align Left",
title: "左对齐",
action: () => editor.chain().focus().setTextAlign("left").run(),
isActive: () => editor.isActive({ textAlign: "left" }),
};
Expand All @@ -234,7 +235,7 @@ export function AlignCenterMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatAlignCenter,
title: "Align Center",
title: "居中",
action: () => editor.chain().focus().setTextAlign("center").run(),
isActive: () => editor.isActive({ textAlign: "center" }),
};
Expand All @@ -244,7 +245,7 @@ export function AlignRightMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatAlignRight,
title: "Align Right",
title: "右对齐",
action: () => editor.chain().focus().setTextAlign("right").run(),
isActive: () => editor.isActive({ textAlign: "right" }),
};
Expand All @@ -254,7 +255,7 @@ export function AlignJustifyMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatAlignJustify,
title: "Align Justify",
title: "两端对齐",
action: () => editor.chain().focus().setTextAlign("justify").run(),
isActive: () => editor.isActive({ textAlign: "justify" }),
};
Expand Down Expand Up @@ -389,3 +390,13 @@ export function TaskListMenuItem(editor: Editor): MenuItem {
isActive: () => editor.isActive("taskList"),
};
}

export function HighlightMenuItem(editor: Editor): MenuItem {
return {
type: "button",
icon: MdiFormatColorHighlight,
title: "高亮",
action: () => editor.chain().focus().toggleHighlight().run(),
isActive: () => editor.isActive("highlight"),
};
}
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b3e0f07

Please sign in to comment.