Skip to content

Commit

Permalink
feat:table cell vertical align
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Mar 21, 2023
1 parent e68c0be commit 665e201
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/editor/assets/css/contextmenu/contextmenu.css
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,20 @@

.ce-contextmenu-merge-cancel-cell {
background-image: url(../../../assets/images/merge-cancel-cell.svg);
}

.ce-contextmenu-vertical-align {
background-image: url(../../../assets/images/vertical-align.svg);
}

.ce-contextmenu-vertical-align-top {
background-image: url(../../../assets/images/vertical-align-top.svg);
}

.ce-contextmenu-vertical-align-middle {
background-image: url(../../../assets/images/vertical-align-middle.svg);
}

.ce-contextmenu-vertical-align-bottom {
background-image: url(../../../assets/images/vertical-align-bottom.svg);
}
1 change: 1 addition & 0 deletions src/editor/assets/images/vertical-align-bottom.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/editor/assets/images/vertical-align-middle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/editor/assets/images/vertical-align-top.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/editor/assets/images/vertical-align.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion src/editor/core/command/Command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IElement, ImageDisplay, INavigateInfo } from '../..'
import { IElement, ImageDisplay, INavigateInfo, VerticalAlign } from '../..'
import { EditorMode, PageMode, PaperDirection } from '../../dataset/enum/Editor'
import { RowFlex } from '../../dataset/enum/Row'
import { IDrawImagePayload, IPainterOptions } from '../../interface/Draw'
Expand Down Expand Up @@ -47,6 +47,7 @@ export class Command {
private static deleteTable: CommandAdapt['deleteTable']
private static mergeTableCell: CommandAdapt['mergeTableCell']
private static cancelMergeTableCell: CommandAdapt['cancelMergeTableCell']
private static tableTdVerticalAlign: CommandAdapt['tableTdVerticalAlign']
private static image: CommandAdapt['image']
private static hyperlink: CommandAdapt['hyperlink']
private static deleteHyperlink: CommandAdapt['deleteHyperlink']
Expand Down Expand Up @@ -120,6 +121,7 @@ export class Command {
Command.deleteTable = adapt.deleteTable.bind(adapt)
Command.mergeTableCell = adapt.mergeTableCell.bind(adapt)
Command.cancelMergeTableCell = adapt.cancelMergeTableCell.bind(adapt)
Command.tableTdVerticalAlign = adapt.tableTdVerticalAlign.bind(adapt)
Command.image = adapt.image.bind(adapt)
Command.hyperlink = adapt.hyperlink.bind(adapt)
Command.deleteHyperlink = adapt.deleteHyperlink.bind(adapt)
Expand Down Expand Up @@ -311,6 +313,10 @@ export class Command {
return Command.cancelMergeTableCell()
}

public executeTableTdVerticalAlign(payload: VerticalAlign) {
return Command.tableTdVerticalAlign(payload)
}

public executeHyperlink(payload: IElement) {
return Command.hyperlink(payload)
}
Expand Down
25 changes: 25 additions & 0 deletions src/editor/core/command/CommandAdapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { EditorContext, EditorMode, PageMode, PaperDirection } from '../../datas
import { ElementType } from '../../dataset/enum/Element'
import { ElementStyleKey } from '../../dataset/enum/ElementStyle'
import { RowFlex } from '../../dataset/enum/Row'
import { VerticalAlign } from '../../dataset/enum/VerticalAlign'
import { IDrawImagePayload, IPainterOptions } from '../../interface/Draw'
import { IEditorOption, IEditorResult } from '../../interface/Editor'
import { IElement, IElementStyle } from '../../interface/Element'
Expand Down Expand Up @@ -997,6 +998,30 @@ export class CommandAdapt {
this.tableTool.render(element, position[index!])
}

public tableTdVerticalAlign(payload: VerticalAlign) {
const isReadonly = this.draw.isReadonly()
if (isReadonly) return
const positionContext = this.position.getPositionContext()
if (!positionContext.isTable) return
const { index, trIndex, tdIndex } = positionContext
const originalElementList = this.draw.getOriginalElementList()
const element = originalElementList[index!]
const curTd = element?.trList?.[trIndex!]?.tdList?.[tdIndex!]
if (
!curTd
|| curTd.verticalAlign === payload
|| (!curTd.verticalAlign && payload === VerticalAlign.TOP)
) {
return
}
// 重设垂直对齐方式
curTd.verticalAlign = payload
const { endIndex } = this.range.getRange()
this.draw.render({
curIndex: endIndex
})
}

public hyperlink(payload: IElement) {
const isReadonly = this.draw.isReadonly()
if (isReadonly) return
Expand Down
34 changes: 34 additions & 0 deletions src/editor/core/contextmenu/menus/tableMenus.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
import { VerticalAlign } from '../../../dataset/enum/VerticalAlign'
import { IRegisterContextMenu } from '../../../interface/contextmenu/ContextMenu'
import { Command } from '../../command/Command'

export const tableMenus: IRegisterContextMenu[] = [
{
isDivider: true
},
{
i18nPath: 'contextmenu.table.verticalAlign',
icon: 'vertical-align',
when: (payload) => {
return !payload.isReadonly && payload.isInTable
},
childMenus: [
{
i18nPath: 'contextmenu.table.verticalAlignTop',
icon: 'vertical-align-top',
when: () => true,
callback: (command: Command) => {
command.executeTableTdVerticalAlign(VerticalAlign.TOP)
}
},
{
i18nPath: 'contextmenu.table.verticalAlignMiddle',
icon: 'vertical-align-middle',
when: () => true,
callback: (command: Command) => {
command.executeTableTdVerticalAlign(VerticalAlign.MIDDLE)
}
},
{
i18nPath: 'contextmenu.table.verticalAlignBottom',
icon: 'vertical-align-bottom',
when: () => true,
callback: (command: Command) => {
command.executeTableTdVerticalAlign(VerticalAlign.BOTTOM)
}
}
]
},
{
i18nPath: 'contextmenu.table.insertRowCol',
icon: 'insert-row-col',
Expand Down
6 changes: 5 additions & 1 deletion src/editor/core/i18n/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
"deleteCol": "Delete 1 col",
"deleteTable": "Delete table",
"mergeCell": "Merge cell",
"mergeCancelCell": "Cancel merge cell"
"mergeCancelCell": "Cancel merge cell",
"verticalAlign": "Vertical align",
"verticalAlignTop": "top",
"verticalAlignMiddle": "middle",
"verticalAlignBottom": "bottom"
}
},
"datePicker": {
Expand Down
6 changes: 5 additions & 1 deletion src/editor/core/i18n/lang/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
"deleteCol": "删除1列",
"deleteTable": "删除整个表格",
"mergeCell": "合并单元格",
"mergeCancelCell": "取消合并"
"mergeCancelCell": "取消合并",
"verticalAlign": "垂直对齐",
"verticalAlignTop": "顶端对齐",
"verticalAlignMiddle": "垂直居中",
"verticalAlignBottom": "底端对齐"
}
},
"datePicker": {
Expand Down
23 changes: 21 additions & 2 deletions src/editor/core/position/Position.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ElementType, RowFlex } from '../..'
import { ElementType, RowFlex, VerticalAlign } from '../..'
import { ZERO } from '../../dataset/constant/Common'
import { ControlComponent, ImageDisplay } from '../../dataset/enum/Control'
import { IComputePageRowPositionPayload, IComputePageRowPositionResult } from '../../interface/Position'
Expand Down Expand Up @@ -112,15 +112,34 @@ export class Position {
for (let d = 0; d < tr.tdList!.length; d++) {
const td = tr.tdList[d]
td.positionList = []
const rowList = td.rowList!
const drawRowResult = this.computePageRowPosition({
positionList: td.positionList,
rowList: td.rowList!,
rowList,
pageNo,
startIndex: 0,
startX: (td.x! + tdPadding) * scale + tablePreX,
startY: td.y! * scale + tablePreY,
innerWidth: (td.width! - tdGap) * scale
})
// 垂直对齐方式
if (
td.verticalAlign === VerticalAlign.MIDDLE
|| td.verticalAlign == VerticalAlign.BOTTOM
) {
const rowsHeight = rowList.reduce((pre, cur) => pre + cur.height, 0)
const blankHeight = td.height! - tdGap - rowsHeight
const offsetHeight = td.verticalAlign === VerticalAlign.MIDDLE ? blankHeight / 2 : blankHeight
if (Math.floor(offsetHeight) > 0) {
td.positionList.forEach(tdPosition => {
const { coordinate: { leftTop, leftBottom, rightBottom, rightTop } } = tdPosition
leftTop[1] += offsetHeight
leftBottom[1] += offsetHeight
rightBottom[1] += offsetHeight
rightTop[1] += offsetHeight
})
}
}
x = drawRowResult.x
y = drawRowResult.y
}
Expand Down
5 changes: 5 additions & 0 deletions src/editor/dataset/enum/VerticalAlign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum VerticalAlign {
TOP = 'top',
MIDDLE = 'middle',
BOTTOM = 'bottom'
}
2 changes: 2 additions & 0 deletions src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { ICursorOption } from './interface/Cursor'
import { defaultCursorOption } from './dataset/constant/Cursor'
import { IPageNumber } from './interface/PageNumber'
import { defaultPageNumberOption } from './dataset/constant/PageNumber'
import { VerticalAlign } from './dataset/enum/VerticalAlign'

export default class Editor {

Expand Down Expand Up @@ -161,6 +162,7 @@ export default class Editor {
export {
Editor,
RowFlex,
VerticalAlign,
EditorZone,
EditorMode,
ElementType,
Expand Down
2 changes: 2 additions & 0 deletions src/editor/interface/table/Td.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { VerticalAlign } from '../../dataset/enum/VerticalAlign'
import { IElement, IElementPosition } from '../Element'
import { IRow } from '../Row'

Expand All @@ -17,4 +18,5 @@ export interface ITd {
colIndex?: number;
rowList?: IRow[];
positionList?: IElementPosition[];
verticalAlign?: VerticalAlign;
}
7 changes: 6 additions & 1 deletion src/editor/utils/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ZERO } from '../dataset/constant/Common'
import { defaultControlOption } from '../dataset/constant/Control'
import { EDITOR_ELEMENT_ZIP_ATTR } from '../dataset/constant/Element'
import { ControlComponent, ControlType } from '../dataset/enum/Control'
import { ITd } from '../interface/table/Td'

interface IFormatElementListOption {
isHandleFirstElement?: boolean;
Expand Down Expand Up @@ -308,11 +309,15 @@ export function zipElementList(payload: IElement[]): IElement[] {
delete tr.id
for (let d = 0; d < tr.tdList.length; d++) {
const td = tr.tdList[d]
tr.tdList[d] = {
const zipTd: ITd = {
colspan: td.colspan,
rowspan: td.rowspan,
value: zipElementList(td.value)
}
if (td.verticalAlign) {
zipTd.verticalAlign = td.verticalAlign
}
tr.tdList[d] = zipTd
}
}
}
Expand Down

0 comments on commit 665e201

Please sign in to comment.