Skip to content

Commit

Permalink
feat: add control indentation option #345
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Dec 7, 2023
1 parent 6de3ad8 commit 5f1cf3a
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/en/guide/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ interface IElement {
minWidth?: number;
underline?: boolean;
extension?: unknown;
indentation?: ControlIndentation;
deletable?: boolean;
disabled?: boolean;
code: string | null;
Expand Down
1 change: 1 addition & 0 deletions docs/guide/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ interface IElement {
minWidth?: number;
underline?: boolean;
extension?: unknown;
indentation?: ControlIndentation;
deletable?: boolean;
disabled?: boolean;
code: string | null;
Expand Down
34 changes: 32 additions & 2 deletions src/editor/core/draw/Draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ import { Control } from './control/Control'
import { zipElementList } from '../../utils/element'
import { CheckboxParticle } from './particle/CheckboxParticle'
import { DeepRequired, IPadding } from '../../interface/Common'
import { ControlComponent, ImageDisplay } from '../../dataset/enum/Control'
import {
ControlComponent,
ControlIndentation,
ImageDisplay
} from '../../dataset/enum/Control'
import { formatElementList } from '../../utils/element'
import { WorkerManager } from '../worker/WorkerManager'
import { Previewer } from './particle/previewer/Previewer'
Expand Down Expand Up @@ -1083,7 +1087,10 @@ export class Draw {
boundingBoxDescent: 0
}
// 实际可用宽度
const offsetX = element.listId ? listStyleMap.get(element.listId) || 0 : 0
const offsetX =
curRow.offsetX ||
(element.listId && listStyleMap.get(element.listId)) ||
0
const availableWidth = innerWidth - offsetX
if (
element.type === ElementType.IMAGE ||
Expand Down Expand Up @@ -1440,6 +1447,29 @@ export class Draw {
rowFlex: elementList[i + 1]?.rowFlex,
isPageBreak: element.type === ElementType.PAGE_BREAK
}
// 控件缩进
if (
rowElement.controlComponent !== ControlComponent.PREFIX &&
rowElement.control?.indentation === ControlIndentation.VALUE_START
) {
// 查找到非前缀的第一个元素位置
const preStartIndex = curRow.elementList.findIndex(
el =>
el.controlId === rowElement.controlId &&
el.controlComponent !== ControlComponent.PREFIX
)
if (~preStartIndex) {
const preRowPositionList = this.position.computeRowPosition({
row: curRow,
innerWidth: this.getInnerWidth()
})
const valueStartPosition = preRowPositionList[preStartIndex]
if (valueStartPosition) {
row.offsetX = valueStartPosition.coordinate.leftTop[0]
}
}
}
// 列表缩进
if (element.listId) {
row.isList = true
row.offsetX = listStyleMap.get(element.listId!)
Expand Down
28 changes: 23 additions & 5 deletions src/editor/core/position/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { ZERO } from '../../dataset/constant/Common'
import { ControlComponent, ImageDisplay } from '../../dataset/enum/Control'
import {
IComputePageRowPositionPayload,
IComputePageRowPositionResult
IComputePageRowPositionResult,
IComputeRowPositionPayload
} from '../../interface/Position'
import { IEditorOption } from '../../interface/Editor'
import { IElement, IElementPosition } from '../../interface/Element'
Expand All @@ -14,6 +15,7 @@ import {
} from '../../interface/Position'
import { Draw } from '../draw/Draw'
import { EditorMode, EditorZone } from '../../dataset/enum/Editor'
import { deepClone } from '../../utils'

export class Position {
private cursorPosition: IElementPosition | null
Expand Down Expand Up @@ -110,10 +112,8 @@ export class Position {
} else if (curRow.rowFlex === RowFlex.RIGHT) {
x += innerWidth - curRow.width
}
// 列表向右移动-留出列表样式位置
if (curRow.isList) {
x += curRow.offsetX || 0
}
// 当前行X轴偏移量
x += curRow.offsetX || 0
// 当前td所在位置
const tablePreX = x
const tablePreY = y
Expand Down Expand Up @@ -244,6 +244,24 @@ export class Position {
}
}

public computeRowPosition(
payload: IComputeRowPositionPayload
): IElementPosition[] {
const { row, innerWidth } = payload
const positionList: IElementPosition[] = []
this.computePageRowPosition({
positionList,
innerWidth,
rowList: [deepClone(row)],
pageNo: 0,
startX: 0,
startY: 0,
startIndex: 0,
startRowIndex: 0
})
return positionList
}

public setCursorPosition(position: IElementPosition | null) {
this.cursorPosition = position
}
Expand Down
6 changes: 6 additions & 0 deletions src/editor/dataset/enum/Control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ export enum ImageDisplay {
INLINE = 'inline',
BLOCK = 'block'
}

// 控件内容缩进方式
export enum ControlIndentation {
ROW_START = 'rowStart', // 从行起始位置缩进
VALUE_START = 'valueStart' // 从值起始位置缩进
}
9 changes: 7 additions & 2 deletions src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import { IHeader } from './interface/Header'
import { IWatermark } from './interface/Watermark'
import { defaultHeaderOption } from './dataset/constant/Header'
import { defaultWatermarkOption } from './dataset/constant/Watermark'
import { ControlType, ImageDisplay } from './dataset/enum/Control'
import {
ControlIndentation,
ControlType,
ImageDisplay
} from './dataset/enum/Control'
import { defaultControlOption } from './dataset/constant/Control'
import { IControlOption } from './interface/Control'
import { ICheckboxOption } from './interface/Checkbox'
Expand Down Expand Up @@ -279,7 +283,8 @@ export {
TitleLevel,
ListType,
ListStyle,
WordBreak
WordBreak,
ControlIndentation
}

// 对外类型
Expand Down
3 changes: 2 additions & 1 deletion src/editor/interface/Control.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ControlType } from '../dataset/enum/Control'
import { ControlType, ControlIndentation } from '../dataset/enum/Control'
import { ICheckbox } from './Checkbox'
import { IElement } from './Element'
import { IRange } from './Range'
Expand Down Expand Up @@ -36,6 +36,7 @@ export interface IControlBasic {
minWidth?: number
underline?: boolean
extension?: unknown
indentation?: ControlIndentation
}

export type IControl = IControlBasic &
Expand Down
5 changes: 5 additions & 0 deletions src/editor/interface/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export interface IPositionContext {
tableId?: string
}

export interface IComputeRowPositionPayload {
row: IRow
innerWidth: number
}

export interface IComputePageRowPositionPayload {
positionList: IElementPosition[]
rowList: IRow[]
Expand Down
6 changes: 3 additions & 3 deletions src/editor/utils/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export function formatElementList(
elementList.splice(i, 0, {
...valueStyleList[valueStyleIndex],
controlId,
value,
value: value === '\n' ? ZERO : value,
letterSpacing: isLastLetter ? checkboxOption.gap : 0,
control: el.control,
controlComponent: ControlComponent.VALUE
Expand Down Expand Up @@ -295,7 +295,7 @@ export function formatElementList(
elementList.splice(i, 0, {
...element,
controlId,
value,
value: value === '\n' ? ZERO : value,
type: element.type || ElementType.TEXT,
control: el.control,
controlComponent: ControlComponent.VALUE
Expand All @@ -315,7 +315,7 @@ export function formatElementList(
const value = placeholderStrList[p]
elementList.splice(i, 0, {
controlId,
value,
value: value === '\n' ? ZERO : value,
type: el.type,
control: el.control,
controlComponent: ControlComponent.PLACEHOLDER,
Expand Down

0 comments on commit 5f1cf3a

Please sign in to comment.