refactor timi-web

This commit is contained in:
Timi
2026-08-27 16:04:06 +08:00
parent 13bd61326d
commit 7ef7653deb
33 changed files with 1193 additions and 1097 deletions
@@ -1,30 +1,26 @@
<template>
<t-button v-popup="'代码块'" variant="outline" :theme="active ? 'primary' : 'default'" @click="openDialog">
<t-button v-popup="'代码块'" :variant="active ? 'base' : 'text'" :theme="active ? 'light' : 'default'" @click="openDialog">
<t-icon name="code" />
</t-button>
<t-popup
<bottom-popup
v-model="visible"
placement="bottom"
title="插入代码块"
left-button-text="取消"
right-button-text="插入"
@left-button-click="visible = false"
@right-button-click="onConfirm"
destroy-on-close
>
<div class="form-popup">
<div class="popup-head">
<span class="title">插入代码块</span>
<t-button shape="square" size="small" variant="text" title="关闭" @click="visible = false">
<t-icon name="close" />
</t-button>
</div>
<t-input v-model="codeLanguage" placeholder="代码语言,可留空" />
<div class="popup-actions">
<t-button variant="outline" block @click="visible = false">取消</t-button>
<t-button theme="primary" block @click="onConfirm">插入</t-button>
</div>
</div>
</t-popup>
</bottom-popup>
</template>
<script lang="ts" setup>
import type { TiptapEditorInstance } from "timi-web/editor";
import BottomPopup from "../bottom-popup/index.vue";
defineOptions({
name: "TiptapCodeBlockButton"
@@ -69,27 +65,5 @@ function onConfirm() {
overflow-y: auto;
background: var(--td-bg-color-container);
.popup-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 1rem;
.title {
font-size: 1rem;
font-weight: 600;
color: var(--td-text-color-primary, #000);
}
}
.popup-actions {
gap: .75rem;
display: flex;
margin-top: 1rem;
:deep(.t-button) {
flex: 1;
}
}
}
</style>
@@ -1,19 +1,18 @@
<template>
<t-button v-popup="'插入内嵌网页'" variant="outline" :theme="active ? 'primary' : 'default'" @click="openDialog">
<t-button v-popup="'插入内嵌网页'" :variant="active ? 'base' : 'text'" :theme="active ? 'light' : 'default'" @click="openDialog">
<t-icon name="web" />
</t-button>
<t-popup
<bottom-popup
v-model="visible"
placement="bottom"
title="插入内嵌网页"
left-button-text="取消"
right-button-text="插入"
@left-button-click="visible = false"
@right-button-click="onConfirm"
destroy-on-close
>
<div class="form-popup">
<div class="popup-head">
<span class="title">插入内嵌网页</span>
<t-button shape="square" size="small" variant="text" title="关闭" @click="visible = false">
<t-icon name="close" />
</t-button>
</div>
<t-form class="form-content" label-align="left" label-width="auto">
<t-form-item label="网页地址">
<t-input v-model="iframeForm.src" placeholder="请输入网页地址" />
@@ -28,17 +27,14 @@
<t-input v-model="iframeForm.height" placeholder="请输入高度,默认 28rem" />
</t-form-item>
</t-form>
<div class="popup-actions">
<t-button variant="outline" block @click="visible = false">取消</t-button>
<t-button theme="primary" block @click="onConfirm">插入</t-button>
</div>
</div>
</t-popup>
</bottom-popup>
</template>
<script lang="ts" setup>
import { showPlatformMessage } from "../message";
import type { TiptapEditorInstance } from "timi-web/editor";
import BottomPopup from "../bottom-popup/index.vue";
defineOptions({
name: "TiptapIframeButton"
@@ -117,33 +113,11 @@ async function onConfirm() {
overflow-y: auto;
background: var(--td-bg-color-container);
.popup-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 1rem;
.title {
font-size: 1rem;
font-weight: 600;
color: var(--td-text-color-primary, #000);
}
}
.form-content {
:deep(.t-form__item) {
margin-bottom: .875rem;
}
}
.popup-actions {
gap: .75rem;
display: flex;
margin-top: 1rem;
:deep(.t-button) {
flex: 1;
}
}
}
</style>
@@ -1,34 +1,30 @@
<template>
<t-button v-popup="'插入链接'" variant="outline" :theme="active ? 'primary' : 'default'" @click="openDialog">
<t-button v-popup="'插入链接'" :variant="active ? 'base' : 'text'" :theme="active ? 'light' : 'default'" @click="openDialog">
<t-icon name="link-1" />
</t-button>
<t-popup
<bottom-popup
v-model="visible"
placement="bottom"
title="插入链接"
left-button-text="取消"
right-button-text="插入"
@left-button-click="visible = false"
@right-button-click="onConfirm"
destroy-on-close
>
<div class="form-popup">
<div class="popup-head">
<span class="title">插入链接</span>
<t-button shape="square" size="small" variant="text" title="关闭" @click="visible = false">
<t-icon name="close" />
</t-button>
</div>
<div class="link-form">
<t-input v-model="linkForm.href" placeholder="请输入链接地址" />
<t-input v-model="linkForm.text" placeholder="请输入显示文本,可留空" />
<t-checkbox v-model="linkForm.syncToHref">显示文本同步为链接</t-checkbox>
</div>
<div class="popup-actions">
<t-button variant="outline" block @click="visible = false">取消</t-button>
<t-button theme="primary" block @click="onConfirm">插入</t-button>
</div>
</div>
</t-popup>
</bottom-popup>
</template>
<script lang="ts" setup>
import type { TiptapEditorInstance } from "timi-web/editor";
import BottomPopup from "../bottom-popup/index.vue";
defineOptions({
name: "TiptapLinkButton"
@@ -105,27 +101,5 @@ function onConfirm() {
overflow-y: auto;
background: var(--td-bg-color-container);
.popup-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 1rem;
.title {
font-size: 1rem;
font-weight: 600;
color: var(--td-text-color-primary, #000);
}
}
.popup-actions {
gap: .75rem;
display: flex;
margin-top: 1rem;
:deep(.t-button) {
flex: 1;
}
}
}
</style>
@@ -1,19 +1,30 @@
<template>
<t-button variant="outline" :theme="active ? 'primary' : 'default'" :loading="uploading" @click="openDialog">
<t-button :variant="active ? 'base' : 'text'" :theme="active ? 'light' : 'default'" :loading="uploading" @click="openDialog">
<t-icon :name="iconName" />
</t-button>
<t-popup
<bottom-popup
v-model="visible"
placement="bottom"
:title="`插入${typeLabel}`"
left-button-text="取消"
right-button-text="插入"
@left-button-click="visible = false"
@right-button-click="onConfirm"
destroy-on-close
>
<template #rightButton="{ onClick }">
<t-button
theme="primary"
variant="text"
size="small"
:loading="uploading"
:disabled="uploading"
@click="onClick"
>
插入
</t-button>
</template>
<div class="form-popup">
<div class="popup-head">
<span class="title" v-text="`插入${typeLabel}`" />
<t-button shape="square" size="small" variant="text" title="关闭" @click="visible = false">
<t-icon name="close" />
</t-button>
</div>
<t-form class="form-content" label-align="left" label-width="auto">
<t-form-item label="文件">
<t-button size="small" variant="outline" @click="pickFile">
@@ -31,18 +42,15 @@
<t-input v-model="mediaForm.height" placeholder="请输入高度,默认 auto" />
</t-form-item>
</t-form>
<div class="popup-actions">
<t-button variant="outline" block :disabled="uploading" @click="visible = false">取消</t-button>
<t-button theme="primary" block :loading="uploading" @click="onConfirm">插入</t-button>
</div>
</div>
</t-popup>
</bottom-popup>
<input ref="fileInputRef" class="file-input" type="file" :accept="accept" @change="onFileChange" />
</template>
<script lang="ts" setup>
import { showPlatformMessage } from "../message";
import type { TiptapEditorInstance, UploadResult } from "timi-web/editor";
import BottomPopup from "../bottom-popup/index.vue";
defineOptions({
name: "TiptapMediaButton"
@@ -180,34 +188,12 @@ async function onConfirm() {
overflow-y: auto;
background: var(--td-bg-color-container);
.popup-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 1rem;
.title {
font-size: 1rem;
font-weight: 600;
color: var(--td-text-color-primary, #000);
}
}
.form-content {
:deep(.t-form__item) {
margin-bottom: .875rem;
}
}
.popup-actions {
gap: .75rem;
display: flex;
margin-top: 1rem;
:deep(.t-button) {
flex: 1;
}
}
}
.file-input {
@@ -1,8 +1,17 @@
<template>
<t-button v-popup="'插入表格'" variant="outline" :theme="active ? 'primary' : 'default'" @click="openPopup">
<t-button v-popup="'插入表格'" :variant="active ? 'base' : 'text'" :theme="active ? 'light' : 'default'" @click="openPopup">
<t-icon name="table" />
</t-button>
<t-popup v-model="visible" placement="bottom" destroy-on-close>
<bottom-popup
v-model="visible"
placement="bottom"
title="插入表格"
left-button-text="取消"
right-button-text="确定"
@left-button-click="visible = false"
@right-button-click="visible = false"
destroy-on-close
>
<div class="table-picker" @mouseleave="onReset">
<div class="tip" v-text="`${hoverRow} × ${hoverCol}`" />
<div class="grid">
@@ -17,12 +26,13 @@
/>
</div>
</div>
</t-popup>
</bottom-popup>
</template>
<script lang="ts" setup>
import { TABLE_PICKER_SIZE } from "timi-web/editor";
import type { TiptapEditorInstance } from "timi-web/editor";
import BottomPopup from "../bottom-popup/index.vue";
defineOptions({
name: "TiptapTableButton"
+139 -44
View File
@@ -2,21 +2,21 @@
<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-button v-popup="'撤销'" variant="text" :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-button v-popup="'重做'" variant="text" :disabled="!canExec('redo')" @click="run('redo')">
<t-icon name="rollfront" />
</t-button>
<t-button v-popup="'清除格式'" variant="outline" @click="run('clear')">
<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="outline"
:theme="sourceCodeMode ? 'primary' : 'default'"
:variant="sourceCodeMode ? 'base' : 'text'"
:theme="sourceCodeMode ? 'light' : 'default'"
@click="emit('toggle-source-code')"
>
<t-icon v-if="sourceCodeMode" name="edit-1" />
@@ -25,10 +25,19 @@
</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-button v-popup="'标题'" :variant="isHeadingActive ? 'base' : 'text'" :theme="isHeadingActive ? 'light' : 'default'" @click="headingVisible = true">
<t-icon name="text" />
</t-button>
<t-popup v-model="headingVisible" placement="bottom" destroy-on-close>
<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"
@@ -40,55 +49,60 @@
<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')">
</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="outline" :theme="isActive('italic') ? 'primary' : 'default'" @click="run('italic')">
<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="outline" :theme="isActive('underline') ? 'primary' : 'default'" @click="run('underline')">
<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="outline" :theme="isActive('strike') ? 'primary' : 'default'" @click="run('strike')">
<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="outline" @click="pickColor('text')">
<t-button v-popup="'前景色'" variant="text" @click="pickColor('text')">
<t-icon name="textformat-color" />
</t-button>
<t-button v-popup="'背景色'" variant="outline" @click="pickColor('bg')">
<t-button v-popup="'背景色'" variant="text" @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-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="isActive('table')" />
<t-button v-popup="'无序列表'" variant="outline" :theme="isActive('bulletList') ? 'primary' : 'default'" @click="run('bulletList')">
<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="outline" :theme="isActive('code') ? 'primary' : 'default'" @click="run('code')">
<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="outline" :theme="isActive('subscript') ? 'primary' : 'default'" @click="run('subscript')">
<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="outline" :theme="isActive('superscript') ? 'primary' : 'default'" @click="run('superscript')">
<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="outline" :theme="isAlignActive('left') ? 'primary' : 'default'" @click="setAlign('left')">
<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="outline" :theme="isAlignActive('center') ? 'primary' : 'default'" @click="setAlign('center')">
<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="outline" :theme="isAlignActive('right') ? 'primary' : 'default'" @click="setAlign('right')">
<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="outline" :theme="isAlignActive('justify') ? 'primary' : 'default'" @click="setAlign('justify')">
<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="outline" @click="run('horizontalRule')">
<t-button v-popup="'分割线'" variant="text" @click="run('horizontalRule')">
<t-icon name="divider-1" />
</t-button>
<tiptap-link-button :editor="editor" :active="isActive('link')" />
@@ -97,12 +111,46 @@
<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" />
<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";
@@ -110,6 +158,7 @@ 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"
@@ -143,11 +192,27 @@ type RunType =
| "horizontalRule"
| "clear";
const textColorRef = ref<HTMLInputElement>();
const bgColorRef = ref<HTMLInputElement>();
const textColor = ref("#000000");
const bgColor = ref("#ffff00");
const textColorDraft = ref("#000000");
const bgColorDraft = ref("#ffff00");
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) {
@@ -264,29 +329,46 @@ function onHeadingSelect(option: { value?: string | number | Record<string, unkn
}
function pickColor(type: "text" | "bg") {
const textStyleAttributes = props.editor?.getAttributes("textStyle") ?? {};
if (type === "text") {
textColorRef.value?.click();
return;
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";
}
bgColorRef.value?.click();
colorPickerType.value = type;
colorPickerVisible.value = true;
}
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 onTextColorChange(value: string) {
textColorDraft.value = value;
}
function onBgColorChange(event: Event) {
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 target = event.target as HTMLInputElement;
bgColor.value = target.value;
props.editor.chain().focus().setMark("textStyle", { backgroundColor: target.value }).run();
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>
@@ -372,6 +454,15 @@ function onBgColorChange(event: Event) {
}
}
: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;
@@ -402,4 +493,8 @@ function onBgColorChange(event: Event) {
width: min(22rem, calc(100vw - 2rem));
padding: .5rem .75rem calc(.5rem + env(safe-area-inset-bottom));
}
.color-picker-popup {
background: var(--td-bg-color-container);
}
</style>
+2 -2
View File
@@ -58,7 +58,7 @@ import {
buildSearchRegex,
replaceMatchText,
type TextSearchOptions
} from "timi-web/editor";
} from "timi-web/editor";
import { Toolkit } from "timi-web";
import type { PendingMediaItem, UploadMediaType, UploadResult } from "timi-web/editor";
import TiptapToolbar from "./TiptapToolbar.vue";
@@ -69,7 +69,7 @@ import {
TEMP_FILE_ID_ATTR,
isLocalMediaId,
normalizeContent
} from "timi-web/editor";
} from "timi-web/editor";
defineOptions({
name: "TiptapEditor"