feat: 表格支持主动调整高度(#77)

This commit is contained in:
pipipi-pikachu 2022-09-11 19:51:53 +08:00
parent 9135d0a77b
commit 6bc30dc4c3
9 changed files with 34 additions and 20 deletions

View File

@ -164,6 +164,7 @@ export default () => {
colHeader: false, colHeader: false,
colFooter: false, colFooter: false,
}, },
cellMinHeight: 36,
}) })
} }

View File

@ -517,6 +517,8 @@ export interface TableTheme {
* *
* colWidths: 列宽数组[30, 50, 20]30%, 50%, 20% * colWidths: 列宽数组[30, 50, 20]30%, 50%, 20%
* *
* cellMinHeight: 单元格最小高度
*
* data: 表格数据 * data: 表格数据
*/ */
export interface PPTTableElement extends PPTBaseElement { export interface PPTTableElement extends PPTBaseElement {
@ -524,6 +526,7 @@ export interface PPTTableElement extends PPTBaseElement {
outline: PPTElementOutline outline: PPTElementOutline
theme?: TableTheme theme?: TableTheme
colWidths: number[] colWidths: number[]
cellMinHeight: number
data: TableCell[][] data: TableCell[][]
} }

View File

@ -10,7 +10,7 @@
<template v-if="handlerVisible"> <template v-if="handlerVisible">
<ResizeHandler <ResizeHandler
class="operate-resize-handler" class="operate-resize-handler"
v-for="point in textElementResizeHandlers" v-for="point in resizeHandlers"
:key="point.direction" :key="point.direction"
:type="point.direction" :type="point.direction"
:rotate="elementInfo.rotate" :rotate="elementInfo.rotate"
@ -70,5 +70,5 @@ const outlineWidth = computed(() => props.elementInfo.outline.width || 1)
const scaleWidth = computed(() => (props.elementInfo.width + outlineWidth.value) * canvasScale.value) const scaleWidth = computed(() => (props.elementInfo.width + outlineWidth.value) * canvasScale.value)
const scaleHeight = computed(() => props.elementInfo.height * canvasScale.value) const scaleHeight = computed(() => props.elementInfo.height * canvasScale.value)
const { textElementResizeHandlers, borderLines } = useCommonOperate(scaleWidth, scaleHeight) const { resizeHandlers, borderLines } = useCommonOperate(scaleWidth, scaleHeight)
</script> </script>

View File

@ -118,6 +118,8 @@ export default (
const elOriginTop = element.top const elOriginTop = element.top
const elOriginWidth = element.width const elOriginWidth = element.width
const elOriginHeight = element.height const elOriginHeight = element.height
const originTableCellMinHeight = element.type === 'table' ? element.cellMinHeight : 0
const elRotate = ('rotate' in element && element.rotate) ? element.rotate : 0 const elRotate = ('rotate' in element && element.rotate) ? element.rotate : 0
const rotateRadian = Math.PI * elRotate / 180 const rotateRadian = Math.PI * elRotate / 180
@ -412,6 +414,16 @@ export default (
path, path,
} }
} }
if (el.type === 'table') {
let cellMinHeight = originTableCellMinHeight + (height - elOriginHeight) / el.data.length
cellMinHeight = cellMinHeight < 36 ? 36 : cellMinHeight
if (cellMinHeight === originTableCellMinHeight) return { ...el, left, width }
return {
...el, left, top, width, height,
cellMinHeight: cellMinHeight < 36 ? 36 : cellMinHeight,
}
}
return { ...el, left, top, width, height } return { ...el, left, top, width, height }
}) })
} }

View File

@ -82,7 +82,7 @@
:min="minSize" :min="minSize"
:max="800" :max="800"
:step="5" :step="5"
:disabled="isHorizontalText" :disabled="isHorizontalText || handleElement!.type === 'table'"
:value="height" :value="height"
@change="value => updateHeight(value as number)" @change="value => updateHeight(value as number)"
style="flex: 4;" style="flex: 4;"

View File

@ -15,6 +15,7 @@
<StaticTable <StaticTable
:data="elementInfo.data" :data="elementInfo.data"
:width="elementInfo.width" :width="elementInfo.width"
:cellMinHeight="elementInfo.cellMinHeight"
:colWidths="elementInfo.colWidths" :colWidths="elementInfo.colWidths"
:outline="elementInfo.outline" :outline="elementInfo.outline"
:theme="elementInfo.theme" :theme="elementInfo.theme"

View File

@ -26,7 +26,7 @@
<col span="1" v-for="(width, index) in colSizeList" :key="index" :width="width"> <col span="1" v-for="(width, index) in colSizeList" :key="index" :width="width">
</colgroup> </colgroup>
<tbody> <tbody>
<tr v-for="(rowCells, rowIndex) in tableCells" :key="rowIndex"> <tr v-for="(rowCells, rowIndex) in tableCells" :key="rowIndex" :style="{ height: cellMinHeight + 'px' }">
<td <td
class="cell" class="cell"
:class="{ :class="{
@ -53,11 +53,12 @@
v-if="activedCell === `${rowIndex}_${colIndex}`" v-if="activedCell === `${rowIndex}_${colIndex}`"
class="cell-text" class="cell-text"
:class="{ 'active': activedCell === `${rowIndex}_${colIndex}` }" :class="{ 'active': activedCell === `${rowIndex}_${colIndex}` }"
:style="{ minHeight: (cellMinHeight - 4) + 'px' }"
:value="cell.text" :value="cell.text"
@updateValue="value => handleInput(value, rowIndex, colIndex)" @updateValue="value => handleInput(value, rowIndex, colIndex)"
@insertExcelData="value => insertExcelData(value, rowIndex, colIndex)" @insertExcelData="value => insertExcelData(value, rowIndex, colIndex)"
/> />
<div v-else class="cell-text" v-html="formatText(cell.text)" /> <div v-else class="cell-text" :style="{ minHeight: (cellMinHeight - 4) + 'px' }" v-html="formatText(cell.text)" />
</td> </td>
</tr> </tr>
</tbody> </tbody>
@ -89,6 +90,10 @@ const props = defineProps({
type: Number, type: Number,
required: true, required: true,
}, },
cellMinHeight: {
type: Number,
required: true,
},
colWidths: { colWidths: {
type: Array as PropType<number[]>, type: Array as PropType<number[]>,
required: true, required: true,
@ -700,10 +705,6 @@ table {
} }
} }
tr {
height: 36px;
}
.cell { .cell {
position: relative; position: relative;
white-space: normal; white-space: normal;
@ -724,7 +725,6 @@ table {
} }
.cell-text { .cell-text {
min-height: 32px;
padding: 5px; padding: 5px;
line-height: 1.5; line-height: 1.5;
user-select: none; user-select: none;

View File

@ -17,10 +17,7 @@
<col span="1" v-for="(width, index) in colSizeList" :key="index" :width="width"> <col span="1" v-for="(width, index) in colSizeList" :key="index" :width="width">
</colgroup> </colgroup>
<tbody> <tbody>
<tr <tr v-for="(rowCells, rowIndex) in data" :key="rowIndex" :style="{ height: cellMinHeight + 'px' }">
v-for="(rowCells, rowIndex) in data"
:key="rowIndex"
>
<td <td
class="cell" class="cell"
:style="{ :style="{
@ -35,7 +32,7 @@
:colspan="cell.colspan" :colspan="cell.colspan"
v-show="!hideCells.includes(`${rowIndex}_${colIndex}`)" v-show="!hideCells.includes(`${rowIndex}_${colIndex}`)"
> >
<div class="cell-text" v-html="formatText(cell.text)" /> <div class="cell-text" :style="{ minHeight: (cellMinHeight - 4) + 'px' }" v-html="formatText(cell.text)" />
</td> </td>
</tr> </tr>
</tbody> </tbody>
@ -59,6 +56,10 @@ const props = defineProps({
type: Number, type: Number,
required: true, required: true,
}, },
cellMinHeight: {
type: Number,
required: true,
},
colWidths: { colWidths: {
type: Array as PropType<number[]>, type: Array as PropType<number[]>,
required: true, required: true,
@ -144,10 +145,6 @@ table {
} }
} }
tr {
height: 36px;
}
.cell { .cell {
position: relative; position: relative;
white-space: normal; white-space: normal;
@ -156,7 +153,6 @@ table {
} }
.cell-text { .cell-text {
min-height: 32px;
padding: 5px; padding: 5px;
line-height: 1.5; line-height: 1.5;
} }

View File

@ -21,6 +21,7 @@
@mousedown.stop @mousedown.stop
:data="elementInfo.data" :data="elementInfo.data"
:width="elementInfo.width" :width="elementInfo.width"
:cellMinHeight="elementInfo.cellMinHeight"
:colWidths="elementInfo.colWidths" :colWidths="elementInfo.colWidths"
:outline="elementInfo.outline" :outline="elementInfo.outline"
:theme="elementInfo.theme" :theme="elementInfo.theme"