feat: add line-height setting feature for the default editor (#5681)

#### What type of PR is this?

/kind feature
/area editor
/milestone 2.15.x

#### What this PR does / why we need it:

此 PR 为默认富文本编辑器中的 P 标签设置了 `line-height` 属性,因此可以支持为某一行设置行高。

#### How to test it?

测试行高功能是否正常。测试在不同组件下功能是否正常。

#### Which issue(s) this PR fixes:

Fixes #5660 

#### Does this PR introduce a user-facing change?
```release-note
为默认富文本编辑器增加行高设置
```
pull/5685/head^2
Takagi 2024-04-11 16:02:10 +08:00 committed by GitHub
parent 26db6036a7
commit fc79de70fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 65 additions and 3 deletions

View File

@ -1,8 +1,33 @@
import TiptapParagraph from "@tiptap/extension-paragraph"; import TiptapParagraph from "@tiptap/extension-paragraph";
import type { ParagraphOptions } from "@tiptap/extension-paragraph"; import type { ParagraphOptions } from "@tiptap/extension-paragraph";
import type { ExtensionOptions } from "@/types"; import type { ExtensionOptions, ToolbarItem as TypeToolbarItem } from "@/types";
import type { Editor } from "@/tiptap";
import { markRaw } from "vue";
import ToolbarItem from "@/components/toolbar/ToolbarItem.vue";
import TablerLineHeight from "~icons/tabler/line-height";
import { i18n } from "@/locales";
import ToolbarSubItem from "@/components/toolbar/ToolbarSubItem.vue";
const Paragraph = TiptapParagraph.extend<ExtensionOptions & ParagraphOptions>({ const Paragraph = TiptapParagraph.extend<ExtensionOptions & ParagraphOptions>({
addAttributes() {
return {
lineHeight: {
default: null,
parseHTML: (element) => {
return element.style.lineHeight;
},
renderHTML: (attributes) => {
const lineHeight = attributes.lineHeight;
if (!lineHeight) {
return {};
}
return {
style: `line-height: ${lineHeight}`,
};
},
},
};
},
addOptions() { addOptions() {
return { return {
...this.parent?.(), ...this.parent?.(),
@ -23,6 +48,41 @@ const Paragraph = TiptapParagraph.extend<ExtensionOptions & ParagraphOptions>({
allowPropagationDownward: true, allowPropagationDownward: true,
}; };
}, },
getToolbarItems({ editor }: { editor: Editor }): TypeToolbarItem {
return {
priority: 220,
component: markRaw(ToolbarItem),
props: {
editor,
isActive: !!editor.getAttributes(Paragraph.name)?.lineHeight,
icon: markRaw(TablerLineHeight),
title: i18n.global.t("editor.common.line_height"),
},
children: [0, 1, 1.5, 2, 2.5, 3].map((lineHeight) => {
return {
priority: lineHeight,
component: markRaw(ToolbarSubItem),
props: {
editor,
isActive:
editor.getAttributes(Paragraph.name)?.lineHeight ===
lineHeight,
title: !lineHeight
? i18n.global.t("editor.common.text.default")
: String(lineHeight),
action: () =>
editor
.chain()
.focus()
.updateAttributes(Paragraph.name, {
lineHeight,
})
.run(),
},
};
}),
};
},
}; };
}, },
}); });

View File

@ -59,7 +59,7 @@ const SearchAndReplace = Extension.create<ExtensionOptions>({
getToolbarItems({ editor }: { editor: Editor }) { getToolbarItems({ editor }: { editor: Editor }) {
return [ return [
{ {
priority: 210, priority: 230,
component: markRaw(ToolbarItem), component: markRaw(ToolbarItem),
props: { props: {
editor, editor,

View File

@ -26,7 +26,7 @@ editor:
audio: Audio audio: Audio
table: Table table: Table
no_results: No results no_results: No results
placeholder: "Enter / to select input type" placeholder: Enter / to select input type
link: link:
add_link: Add link add_link: Add link
edit_link: Edit link edit_link: Edit link
@ -131,3 +131,4 @@ editor:
restore_default: Restore default restore_default: Restore default
text: text:
default: Default default: Default
line_height: Line height

View File

@ -131,3 +131,4 @@ editor:
restore_default: 恢复为默认 restore_default: 恢复为默认
text: text:
default: 默认 default: 默认
line_height: 行高