refactor editor
This commit is contained in:
@@ -0,0 +1,405 @@
|
||||
<template>
|
||||
<div class="toolbar">
|
||||
<div class="toolbar-head">
|
||||
<div class="history" :class="{ 'toolbar-disabled': sourceCodeMode }">
|
||||
<t-button v-popup="'撤销'" variant="outline" :disabled="!canExec('undo')" @click="run('undo')">
|
||||
<t-icon name="rollback" />
|
||||
</t-button>
|
||||
<t-button v-popup="'重做'" variant="outline" :disabled="!canExec('redo')" @click="run('redo')">
|
||||
<t-icon name="rollfront" />
|
||||
</t-button>
|
||||
<t-button v-popup="'清除格式'" variant="outline" @click="run('clear')">
|
||||
<t-icon name="clear-formatting" />
|
||||
</t-button>
|
||||
</div>
|
||||
<t-button
|
||||
v-popup="sourceCodeMode ? '富文本' : '源码'"
|
||||
class="source-btn"
|
||||
variant="outline"
|
||||
:theme="sourceCodeMode ? 'primary' : '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="outline" :theme="isHeadingActive ? 'primary' : 'default'" @click="headingVisible = true">
|
||||
<t-icon name="text" />
|
||||
</t-button>
|
||||
<t-popup v-model="headingVisible" placement="bottom" 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>
|
||||
</t-popup>
|
||||
<t-button v-popup="'加粗'" variant="outline" :theme="isActive('bold') ? 'primary' : 'default'" @click="run('bold')">
|
||||
<t-icon name="textformat-bold" />
|
||||
</t-button>
|
||||
<t-button v-popup="'斜体'" variant="outline" :theme="isActive('italic') ? 'primary' : 'default'" @click="run('italic')">
|
||||
<t-icon name="textformat-italic" />
|
||||
</t-button>
|
||||
<t-button v-popup="'下划线'" variant="outline" :theme="isActive('underline') ? 'primary' : 'default'" @click="run('underline')">
|
||||
<t-icon name="textformat-underline" />
|
||||
</t-button>
|
||||
<t-button v-popup="'删除线'" variant="outline" :theme="isActive('strike') ? 'primary' : 'default'" @click="run('strike')">
|
||||
<t-icon name="textformat-strikethrough" />
|
||||
</t-button>
|
||||
<t-button v-popup="'前景色'" variant="outline" @click="pickColor('text')">
|
||||
<t-icon name="textformat-color" />
|
||||
</t-button>
|
||||
<t-button v-popup="'背景色'" variant="outline" @click="pickColor('bg')">
|
||||
<t-icon name="fill-color-1" />
|
||||
</t-button>
|
||||
<t-button v-popup="'有序列表'" variant="outline" :theme="isActive('orderedList') ? 'primary' : 'default'" @click="run('orderedList')">
|
||||
<t-icon name="order-list" />
|
||||
</t-button>
|
||||
<tiptap-table-button :editor="editor" :active="isActive('table')" />
|
||||
<t-button v-popup="'无序列表'" variant="outline" :theme="isActive('bulletList') ? 'primary' : 'default'" @click="run('bulletList')">
|
||||
<t-icon name="bulletpoint" />
|
||||
</t-button>
|
||||
<t-button v-popup="'行内代码'" variant="outline" :theme="isActive('code') ? 'primary' : '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="outline" :theme="isActive('subscript') ? 'primary' : 'default'" @click="run('subscript')">
|
||||
<t-icon name="subscript" />
|
||||
</t-button>
|
||||
<t-button v-popup="'上标'" variant="outline" :theme="isActive('superscript') ? 'primary' : 'default'" @click="run('superscript')">
|
||||
<t-icon name="superscript" />
|
||||
</t-button>
|
||||
<t-button v-popup="'左对齐'" variant="outline" :theme="isAlignActive('left') ? 'primary' : 'default'" @click="setAlign('left')">
|
||||
<t-icon name="format-vertical-align-left" />
|
||||
</t-button>
|
||||
<t-button v-popup="'居中对齐'" variant="outline" :theme="isAlignActive('center') ? 'primary' : 'default'" @click="setAlign('center')">
|
||||
<t-icon name="format-vertical-align-center" />
|
||||
</t-button>
|
||||
<t-button v-popup="'右对齐'" variant="outline" :theme="isAlignActive('right') ? 'primary' : 'default'" @click="setAlign('right')">
|
||||
<t-icon name="format-vertical-align-right" />
|
||||
</t-button>
|
||||
<t-button v-popup="'两端对齐'" variant="outline" :theme="isAlignActive('justify') ? 'primary' : 'default'" @click="setAlign('justify')">
|
||||
<t-icon name="expand-horizontal" />
|
||||
</t-button>
|
||||
<t-button v-popup="'分割线'" variant="outline" @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>
|
||||
<input ref="textColorRef" class="hidden" type="color" :value="textColor" @input="onTextColorChange" />
|
||||
<input ref="bgColorRef" class="hidden" type="color" :value="bgColor" @input="onBgColorChange" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
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";
|
||||
|
||||
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";
|
||||
|
||||
const textColorRef = ref<HTMLInputElement>();
|
||||
const bgColorRef = ref<HTMLInputElement>();
|
||||
const textColor = ref("#000000");
|
||||
const bgColor = ref("#ffff00");
|
||||
const headingVisible = ref(false);
|
||||
|
||||
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 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") {
|
||||
if (type === "text") {
|
||||
textColorRef.value?.click();
|
||||
return;
|
||||
}
|
||||
bgColorRef.value?.click();
|
||||
}
|
||||
|
||||
function onTextColorChange(event: Event) {
|
||||
if (!props.editor) {
|
||||
return;
|
||||
}
|
||||
const target = event.target as HTMLInputElement;
|
||||
textColor.value = target.value;
|
||||
props.editor.chain().focus().setColor(target.value).run();
|
||||
}
|
||||
|
||||
function onBgColorChange(event: Event) {
|
||||
if (!props.editor) {
|
||||
return;
|
||||
}
|
||||
const target = event.target as HTMLInputElement;
|
||||
bgColor.value = target.value;
|
||||
props.editor.chain().focus().setMark("textStyle", { backgroundColor: target.value }).run();
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.toolbar {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
.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 + env(safe-area-inset-bottom));
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user