Files
timi-tdesign-mobile/src/components/tiptap-editor/TiptapTableButton.vue
T
2026-08-28 16:38:20 +08:00

184 lines
3.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<t-popover
v-if="active"
v-model="actionVisible"
class="popover"
placement="bottom"
theme="light"
:show-arrow="false"
>
<t-button variant="base" theme="light">
<t-icon name="table" />
</t-button>
<template #content>
<div class="actions">
<t-button
class="action"
size="extra-small"
variant="text"
block
@click="onContinueInsert"
>继续插入</t-button>
<t-button
class="action"
size="extra-small"
variant="text"
theme="danger"
block
@click="onDeleteTable"
>删除表格</t-button>
</div>
</template>
</t-popover>
<t-button v-else v-popup="'插入表格'" variant="text" @click="openPopup">
<t-icon name="table" />
</t-button>
<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">
<button
v-for="item in tablePickerItems"
:key="item.key"
class="cell"
:class="{ active: item.active }"
type="button"
@mousemove="onHover(item.row, item.col)"
@click="onSelect(item.row, item.col)"
/>
</div>
</div>
</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"
});
const props = defineProps<{
editor?: TiptapEditorInstance;
active: boolean;
}>();
const visible = ref(false);
const actionVisible = ref(false);
const hoverRow = ref(1);
const hoverCol = ref(1);
const tablePickerItems = computed(() => {
const items: { key: string; row: number; col: number; active: boolean }[] = [];
for (let row = 1; row < TABLE_PICKER_SIZE + 1; row += 1) {
for (let col = 1; col < TABLE_PICKER_SIZE + 1; col += 1) {
items.push({
key: `${row}-${col}`,
row,
col,
active: row < hoverRow.value + 1 && col < hoverCol.value + 1
});
}
}
return items;
});
function openPopup() {
hoverRow.value = 1;
hoverCol.value = 1;
visible.value = true;
}
function onContinueInsert() {
actionVisible.value = false;
openPopup();
}
function onDeleteTable() {
actionVisible.value = false;
props.editor?.chain().focus().deleteTable().run();
}
function onReset() {
hoverRow.value = 1;
hoverCol.value = 1;
}
function onHover(row: number, col: number) {
hoverRow.value = row;
hoverCol.value = col;
}
function onSelect(row: number, col: number) {
if (!props.editor) {
return;
}
props.editor.chain().focus().insertTable({ rows: row, cols: col, withHeaderRow: true }).run();
}
watch(() => props.active, (active) => {
if (!active) {
actionVisible.value = false;
}
});
</script>
<style lang="less" scoped>
.table-picker {
padding: 0 1rem calc(1rem + env(safe-area-inset-bottom));
display: flex;
box-sizing: border-box;
background: var(--td-bg-color-container, #fff);
justify-content: center;
flex-direction: column;
.tip {
height: 1.5rem;
color: var(--td-text-color-secondary, #666);
font-size: .75rem;
text-align: center;
line-height: 1.5rem;
}
.grid {
gap: .5rem;
width: fit-content;
display: grid;
margin: 0 auto;
grid-template-columns: repeat(7, 1fr);
.cell {
width: 2rem;
height: 2rem;
border: 1px solid var(--td-component-border, #dcdcdc);
padding: 0;
background: var(--td-bg-color-container, #fff);
&.active {
border-color: var(--td-brand-color, #0052d9);
background: var(--td-brand-color-light, #e0edff);
}
}
}
}
.actions {
padding: 0 1rem;
.action {
height: 2rem !important;
}
}
</style>