530 lines
14 KiB
Vue
530 lines
14 KiB
Vue
<template>
|
|
<div class="toolbar">
|
|
<div class="toolbar-head">
|
|
<div class="history" :class="{ 'toolbar-disabled': sourceCodeMode }">
|
|
<t-button v-popup="'撤销'" variant="text" :disabled="!canExec('undo')" @click="run('undo')">
|
|
<t-icon name="rollback" />
|
|
</t-button>
|
|
<t-button v-popup="'重做'" variant="text" :disabled="!canExec('redo')" @click="run('redo')">
|
|
<t-icon name="rollfront" />
|
|
</t-button>
|
|
<t-button v-popup="'清除格式'" variant="text" @click="run('clear')">
|
|
<t-icon name="clear-formatting" />
|
|
</t-button>
|
|
</div>
|
|
<t-button
|
|
v-popup="sourceCodeMode ? '富文本' : '源码'"
|
|
class="source-btn"
|
|
:variant="sourceCodeMode ? 'base' : 'text'"
|
|
:theme="sourceCodeMode ? 'light' : 'default'"
|
|
@click="emit('toggle-source-code')"
|
|
>
|
|
<t-icon v-if="sourceCodeMode" name="edit-1" />
|
|
<t-icon v-else name="braces" />
|
|
</t-button>
|
|
</div>
|
|
<div class="scroll" :class="{ 'toolbar-disabled': sourceCodeMode }">
|
|
<div class="actions">
|
|
<t-button v-popup="'标题'" :variant="isHeadingActive ? 'base' : 'text'" :theme="isHeadingActive ? 'light' : 'default'" @click="headingVisible = true">
|
|
<t-icon name="text" />
|
|
</t-button>
|
|
<bottom-popup
|
|
v-model="headingVisible"
|
|
placement="bottom"
|
|
title="选择标题"
|
|
left-button-text="取消"
|
|
right-button-text="确定"
|
|
@left-button-click="headingVisible = false"
|
|
@right-button-click="headingVisible = false"
|
|
destroy-on-close
|
|
>
|
|
<div class="heading-picker">
|
|
<t-button
|
|
v-for="option in headingMeta"
|
|
:key="option.value"
|
|
variant="text"
|
|
block
|
|
@click="onHeadingSelect(option)"
|
|
>
|
|
<span :class="['tp-heading-option', option.className]" v-text="option.label" />
|
|
</t-button>
|
|
</div>
|
|
</bottom-popup>
|
|
<t-button v-popup="'加粗'" :variant="isActive('bold') ? 'base' : 'text'" :theme="isActive('bold') ? 'light' : 'default'" @click="run('bold')">
|
|
<t-icon name="textformat-bold" />
|
|
</t-button>
|
|
<t-button v-popup="'斜体'" :variant="isActive('italic') ? 'base' : 'text'" :theme="isActive('italic') ? 'light' : 'default'" @click="run('italic')">
|
|
<t-icon name="textformat-italic" />
|
|
</t-button>
|
|
<t-button v-popup="'下划线'" :variant="isActive('underline') ? 'base' : 'text'" :theme="isActive('underline') ? 'light' : 'default'" @click="run('underline')">
|
|
<t-icon name="textformat-underline" />
|
|
</t-button>
|
|
<t-button v-popup="'删除线'" :variant="isActive('strike') ? 'base' : 'text'" :theme="isActive('strike') ? 'light' : 'default'" @click="run('strike')">
|
|
<t-icon name="textformat-strikethrough" />
|
|
</t-button>
|
|
<t-button v-popup="'前景色'" variant="text" @click="pickColor('text')">
|
|
<t-icon name="textformat-color" />
|
|
</t-button>
|
|
<t-button v-popup="'背景色'" variant="text" @click="pickColor('bg')">
|
|
<t-icon name="fill-color-1" />
|
|
</t-button>
|
|
<t-button v-popup="'有序列表'" :variant="isActive('orderedList') ? 'base' : 'text'" :theme="isActive('orderedList') ? 'light' : 'default'" @click="run('orderedList')">
|
|
<t-icon name="order-list" />
|
|
</t-button>
|
|
<tiptap-table-button :editor="editor" :active="tableActive" />
|
|
<t-button v-popup="'无序列表'" :variant="isActive('bulletList') ? 'base' : 'text'" :theme="isActive('bulletList') ? 'light' : 'default'" @click="run('bulletList')">
|
|
<t-icon name="bulletpoint" />
|
|
</t-button>
|
|
<t-button v-popup="'行内代码'" :variant="isActive('code') ? 'base' : 'text'" :theme="isActive('code') ? 'light' : 'default'" @click="run('code')">
|
|
<t-icon name="highlighted-block" />
|
|
</t-button>
|
|
<tiptap-code-block-button :editor="editor" :active="isActive('codeBlock')" />
|
|
<t-button v-popup="'下标'" :variant="isActive('subscript') ? 'base' : 'text'" :theme="isActive('subscript') ? 'light' : 'default'" @click="run('subscript')">
|
|
<t-icon name="subscript" />
|
|
</t-button>
|
|
<t-button v-popup="'上标'" :variant="isActive('superscript') ? 'base' : 'text'" :theme="isActive('superscript') ? 'light' : 'default'" @click="run('superscript')">
|
|
<t-icon name="superscript" />
|
|
</t-button>
|
|
<t-button v-popup="'左对齐'" :variant="isAlignActive('left') ? 'base' : 'text'" :theme="isAlignActive('left') ? 'light' : 'default'" @click="setAlign('left')">
|
|
<t-icon name="format-vertical-align-left" />
|
|
</t-button>
|
|
<t-button v-popup="'居中对齐'" :variant="isAlignActive('center') ? 'base' : 'text'" :theme="isAlignActive('center') ? 'light' : 'default'" @click="setAlign('center')">
|
|
<t-icon name="format-vertical-align-center" />
|
|
</t-button>
|
|
<t-button v-popup="'右对齐'" :variant="isAlignActive('right') ? 'base' : 'text'" :theme="isAlignActive('right') ? 'light' : 'default'" @click="setAlign('right')">
|
|
<t-icon name="format-vertical-align-right" />
|
|
</t-button>
|
|
<t-button
|
|
v-popup="'两端对齐'"
|
|
:variant="isAlignActive('justify') ? 'base' : 'text'"
|
|
:theme="isAlignActive('justify') ? 'light' : 'default'"
|
|
@click="setAlign('justify')"
|
|
>
|
|
<t-icon name="expand-horizontal" />
|
|
</t-button>
|
|
<t-button v-popup="'分割线'" variant="text" @click="run('horizontalRule')">
|
|
<t-icon name="divider-1" />
|
|
</t-button>
|
|
<tiptap-link-button :editor="editor" :active="isActive('link')" />
|
|
<tiptap-media-button :editor="editor" :active="isActive('image')" type="image" :upload="uploadImage" />
|
|
<tiptap-media-button :editor="editor" :active="isActive('video')" type="video" :upload="uploadVideo" />
|
|
<tiptap-iframe-button :editor="editor" :active="isActive('iframe')" />
|
|
</div>
|
|
</div>
|
|
<bottom-popup
|
|
v-model="colorPickerVisible"
|
|
placement="bottom"
|
|
:title="colorPickerType === 'text' ? '前景色' : '背景色'"
|
|
left-button-text="取消"
|
|
right-button-text="确定"
|
|
@left-button-click="colorPickerVisible = false"
|
|
@right-button-click="confirmColor"
|
|
destroy-on-close
|
|
>
|
|
<div class="color-picker-popup">
|
|
<template v-if="colorPickerType === 'text'">
|
|
<t-color-picker
|
|
v-model="textColorDraft"
|
|
type="multiple"
|
|
format="HEX"
|
|
:fixed="true"
|
|
:swatch-colors="colorSwatchColors"
|
|
@change="onTextColorChange"
|
|
@palette-bar-change="onTextPaletteBarChange"
|
|
/>
|
|
</template>
|
|
<template v-else>
|
|
<t-color-picker
|
|
v-model="bgColorDraft"
|
|
type="multiple"
|
|
format="HEX"
|
|
:fixed="true"
|
|
:swatch-colors="colorSwatchColors"
|
|
@change="onBgColorChange"
|
|
@palette-bar-change="onBgPaletteBarChange"
|
|
/>
|
|
</template>
|
|
</div>
|
|
</bottom-popup>
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import type { ColorObject } from "tdesign-mobile-vue";
|
|
import type { AlignType, UploadResult, TiptapEditorInstance } from "timi-web/editor";
|
|
import { headingMeta } from "timi-web/editor";
|
|
import TiptapCodeBlockButton from "./TiptapCodeBlockButton.vue";
|
|
import TiptapIframeButton from "./TiptapIframeButton.vue";
|
|
import TiptapLinkButton from "./TiptapLinkButton.vue";
|
|
import TiptapMediaButton from "./TiptapMediaButton.vue";
|
|
import TiptapTableButton from "./TiptapTableButton.vue";
|
|
import BottomPopup from "../bottom-popup/index.vue";
|
|
|
|
defineOptions({
|
|
name: "TiptapToolbar"
|
|
});
|
|
|
|
const props = withDefaults(defineProps<{
|
|
editor?: TiptapEditorInstance;
|
|
uploadImage?: (file: File) => Promise<UploadResult>;
|
|
uploadVideo?: (file: File) => Promise<UploadResult>;
|
|
sourceCodeMode?: boolean;
|
|
}>(), {
|
|
sourceCodeMode: false
|
|
});
|
|
|
|
const emit = defineEmits<{
|
|
"toggle-source-code": [];
|
|
}>();
|
|
|
|
type RunType =
|
|
| "undo"
|
|
| "redo"
|
|
| "bold"
|
|
| "italic"
|
|
| "underline"
|
|
| "strike"
|
|
| "orderedList"
|
|
| "bulletList"
|
|
| "code"
|
|
| "subscript"
|
|
| "superscript"
|
|
| "horizontalRule"
|
|
| "clear";
|
|
|
|
type TiptapEditorEventApi = TiptapEditorInstance & {
|
|
on: (event: "selectionUpdate" | "transaction", callback: () => void) => void;
|
|
off: (event: "selectionUpdate" | "transaction", callback: () => void) => void;
|
|
};
|
|
|
|
const textColorDraft = ref("#000000");
|
|
const bgColorDraft = ref("#ffff00");
|
|
const tableActive = ref(false);
|
|
const headingVisible = ref(false);
|
|
const colorPickerVisible = ref(false);
|
|
const colorPickerType = ref<"text" | "bg">("text");
|
|
const colorSwatchColors = [
|
|
"#F5222D",
|
|
"#FF7875",
|
|
"#FA541C",
|
|
"#FA8C16",
|
|
"#FADB14",
|
|
"#A0D911",
|
|
"#52C41A",
|
|
"#13C2C2",
|
|
"#1890FF",
|
|
"#2F54EB",
|
|
"#722ED1",
|
|
"#EB2F96",
|
|
"#8C8C8C",
|
|
"#D9D9D9"
|
|
];
|
|
|
|
const currentHeadingValue = computed(() => {
|
|
if (!props.editor) {
|
|
return "p";
|
|
}
|
|
for (let level = 1; level < 7; level += 1) {
|
|
if (props.editor.isActive("heading", { level })) {
|
|
return `h${level}`;
|
|
}
|
|
}
|
|
return "p";
|
|
});
|
|
|
|
const isHeadingActive = computed(() => {
|
|
return currentHeadingValue.value !== "p";
|
|
});
|
|
|
|
function canExec(type: "undo" | "redo") {
|
|
if (!props.editor) {
|
|
return false;
|
|
}
|
|
switch (type) {
|
|
case "undo":
|
|
return props.editor.can().undo();
|
|
case "redo":
|
|
return props.editor.can().redo();
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function isActive(type: string) {
|
|
if (!props.editor) {
|
|
return false;
|
|
}
|
|
return props.editor.isActive(type);
|
|
}
|
|
|
|
function syncTableActive() {
|
|
tableActive.value = props.editor?.isActive("table") ?? false;
|
|
}
|
|
|
|
watch(() => props.editor, (editor, previousEditor) => {
|
|
const previousEventApi = previousEditor as TiptapEditorEventApi | undefined;
|
|
previousEventApi?.off("selectionUpdate", syncTableActive);
|
|
previousEventApi?.off("transaction", syncTableActive);
|
|
|
|
const eventApi = editor as TiptapEditorEventApi | undefined;
|
|
eventApi?.on("selectionUpdate", syncTableActive);
|
|
eventApi?.on("transaction", syncTableActive);
|
|
syncTableActive();
|
|
}, { immediate: true });
|
|
|
|
onBeforeUnmount(() => {
|
|
const eventApi = props.editor as TiptapEditorEventApi | undefined;
|
|
eventApi?.off("selectionUpdate", syncTableActive);
|
|
eventApi?.off("transaction", syncTableActive);
|
|
});
|
|
|
|
function isAlignActive(align: AlignType) {
|
|
if (!props.editor) {
|
|
return false;
|
|
}
|
|
return props.editor.isActive({ textAlign: align });
|
|
}
|
|
|
|
function run(type: RunType) {
|
|
if (!props.editor) {
|
|
return;
|
|
}
|
|
const chain = props.editor.chain().focus();
|
|
switch (type) {
|
|
case "undo":
|
|
chain.undo().run();
|
|
return;
|
|
case "redo":
|
|
chain.redo().run();
|
|
return;
|
|
case "bold":
|
|
chain.toggleBold().run();
|
|
return;
|
|
case "italic":
|
|
chain.toggleItalic().run();
|
|
return;
|
|
case "underline":
|
|
chain.toggleUnderline().run();
|
|
return;
|
|
case "strike":
|
|
chain.toggleStrike().run();
|
|
return;
|
|
case "orderedList":
|
|
chain.toggleOrderedList().run();
|
|
return;
|
|
case "bulletList":
|
|
chain.toggleBulletList().run();
|
|
return;
|
|
case "code":
|
|
chain.toggleCode().run();
|
|
return;
|
|
case "subscript":
|
|
chain.toggleSubscript().run();
|
|
return;
|
|
case "superscript":
|
|
chain.toggleSuperscript().run();
|
|
return;
|
|
case "horizontalRule":
|
|
chain.setHorizontalRule().run();
|
|
return;
|
|
case "clear":
|
|
chain.clearNodes().unsetAllMarks().run();
|
|
return;
|
|
default:
|
|
}
|
|
}
|
|
|
|
function setAlign(align: AlignType) {
|
|
props.editor?.chain().focus().setTextAlign(align).run();
|
|
}
|
|
|
|
function onHeadingSelect(option: { value?: string | number | Record<string, unknown> }) {
|
|
headingVisible.value = false;
|
|
if (!props.editor) {
|
|
return;
|
|
}
|
|
const value = `${option?.value || "p"}`;
|
|
const chain = props.editor.chain().focus();
|
|
if (value === "p") {
|
|
chain.setParagraph().run();
|
|
return;
|
|
}
|
|
const level = Number(value.replace("h", ""));
|
|
if (0 < level && level < 7) {
|
|
chain.setHeading({ level: level as 1 | 2 | 3 | 4 | 5 | 6 }).run();
|
|
}
|
|
}
|
|
|
|
function pickColor(type: "text" | "bg") {
|
|
const textStyleAttributes = props.editor?.getAttributes("textStyle") ?? {};
|
|
if (type === "text") {
|
|
const color = textStyleAttributes.color;
|
|
textColorDraft.value = typeof color === "string" && color ? color : "#000000";
|
|
} else {
|
|
const backgroundColor = textStyleAttributes.backgroundColor;
|
|
bgColorDraft.value = typeof backgroundColor === "string" && backgroundColor ? backgroundColor : "#ffff00";
|
|
}
|
|
colorPickerType.value = type;
|
|
colorPickerVisible.value = true;
|
|
}
|
|
|
|
function onTextColorChange(value: string) {
|
|
textColorDraft.value = value;
|
|
}
|
|
|
|
function onBgColorChange(value: string) {
|
|
bgColorDraft.value = value;
|
|
}
|
|
|
|
function onTextPaletteBarChange(context: { color: ColorObject }) {
|
|
textColorDraft.value = context.color.hex;
|
|
}
|
|
|
|
function onBgPaletteBarChange(context: { color: ColorObject }) {
|
|
bgColorDraft.value = context.color.hex;
|
|
}
|
|
|
|
function confirmColor() {
|
|
if (!props.editor) {
|
|
colorPickerVisible.value = false;
|
|
return;
|
|
}
|
|
const chain = props.editor.chain().focus();
|
|
if (colorPickerType.value === "text") {
|
|
chain.setColor(textColorDraft.value).run();
|
|
} else {
|
|
chain.setMark("textStyle", { backgroundColor: bgColorDraft.value }).run();
|
|
}
|
|
colorPickerVisible.value = false;
|
|
}
|
|
|
|
</script>
|
|
|
|
<style lang="less" scoped>
|
|
.toolbar {
|
|
--td-popover-padding: .25rem .5rem;
|
|
|
|
width: 100%;
|
|
position: relative;
|
|
display: flex;
|
|
flex-direction: column;
|
|
min-width: 0;
|
|
max-width: 100%;
|
|
border-bottom: 1px solid var(--td-component-border);
|
|
background: var(--td-bg-color-container);
|
|
|
|
&:after {
|
|
content: "";
|
|
display: none;
|
|
}
|
|
|
|
.hidden {
|
|
display: none;
|
|
}
|
|
|
|
.toolbar-head {
|
|
width: 100%;
|
|
display: flex;
|
|
gap: .5rem;
|
|
align-items: center;
|
|
min-width: 0;
|
|
min-height: 3.25rem;
|
|
padding: .5rem .625rem;
|
|
box-sizing: border-box;
|
|
border-bottom: 1px solid var(--td-component-border);
|
|
|
|
:deep(.t-button) {
|
|
flex: none;
|
|
width: 2.5rem;
|
|
height: 2.5rem;
|
|
padding: 0;
|
|
}
|
|
}
|
|
|
|
.history {
|
|
display: flex;
|
|
gap: .5rem;
|
|
}
|
|
|
|
.source-btn {
|
|
margin-left: auto;
|
|
}
|
|
|
|
.scroll {
|
|
width: 100%;
|
|
min-width: 0;
|
|
max-width: 100%;
|
|
overflow-x: auto;
|
|
overflow-y: hidden;
|
|
-webkit-overflow-scrolling: touch;
|
|
scrollbar-width: none;
|
|
|
|
&::-webkit-scrollbar {
|
|
display: none;
|
|
}
|
|
}
|
|
|
|
.actions {
|
|
gap: .375rem;
|
|
display: flex;
|
|
width: max-content;
|
|
min-width: 100%;
|
|
padding: .5rem .625rem;
|
|
box-sizing: border-box;
|
|
|
|
> * {
|
|
flex: none;
|
|
}
|
|
|
|
:deep(.t-button) {
|
|
width: 2.5rem;
|
|
height: 2.5rem;
|
|
padding: 0;
|
|
}
|
|
}
|
|
|
|
:deep(.t-button.t-button--text.t-button--hover::after) {
|
|
background: var(--td-brand-color-light);
|
|
}
|
|
|
|
:deep(.t-button--text.t-button--disabled) {
|
|
background: none;
|
|
opacity: .5;
|
|
}
|
|
|
|
.toolbar-disabled {
|
|
opacity: .55;
|
|
pointer-events: none;
|
|
}
|
|
}
|
|
|
|
:global(.tp-heading-option) {
|
|
display: block;
|
|
line-height: 1.2;
|
|
}
|
|
|
|
.heading-size(@name, @size, @weight) {
|
|
:global(.tp-heading-option.@{name}) {
|
|
font-size: @size;
|
|
font-weight: @weight;
|
|
}
|
|
}
|
|
|
|
.heading-size(p, 1rem, 400);
|
|
.heading-size(h1, 2rem, 700);
|
|
.heading-size(h2, 1.5rem, 700);
|
|
.heading-size(h3, 1.17rem, 700);
|
|
.heading-size(h4, 1rem, 700);
|
|
.heading-size(h5, .83rem, 700);
|
|
.heading-size(h6, .67rem, 700);
|
|
|
|
.heading-picker {
|
|
width: min(22rem, calc(100vw - 2rem));
|
|
padding: .5rem .75rem calc(.5rem + var(--tui-safe-bottom, 0px));
|
|
}
|
|
|
|
.color-picker-popup {
|
|
background: var(--td-bg-color-container);
|
|
}
|
|
</style>
|