feat:add checkbox control interface

This commit is contained in:
黄云飞
2022-04-15 17:49:55 +08:00
parent 464c8e3c34
commit 4d48eb2cde
10 changed files with 191 additions and 38 deletions
+9 -2
View File
@@ -39,6 +39,7 @@ import { Control } from './control/Control'
import { zipElementList } from '../../utils/element'
import { CheckboxParticle } from './particle/CheckboxParticle'
import { DeepRequired } from '../../interface/Common'
import { ControlComponent } from '../../dataset/enum/Control'
export class Draw {
@@ -567,7 +568,10 @@ export class Draw {
element.width = innerWidth
metrics.width = innerWidth
metrics.height = this.options.defaultSize
} else if (element.type === ElementType.CHECKBOX) {
} else if (
element.type === ElementType.CHECKBOX ||
element.controlComponent === ControlComponent.CHECKBOX
) {
const { width, height, gap } = this.options.checkbox
const elementWidth = (width + gap * 2) * scale
element.width = elementWidth
@@ -707,7 +711,10 @@ export class Draw {
if (this.mode !== EditorMode.CLEAN) {
this.pageBreakParticle.render(ctx, element, x, y)
}
} else if (element.type === ElementType.CHECKBOX) {
} else if (
element.type === ElementType.CHECKBOX ||
element.controlComponent === ControlComponent.CHECKBOX
) {
this.textParticle.complete()
this.checkboxParticle.render(ctx, element, x, y + offsetY)
} else {
+3
View File
@@ -7,6 +7,7 @@ import { pickElementAttr, zipElementList } from '../../../utils/element'
import { Listener } from '../../listener/Listener'
import { RangeManager } from '../../range/RangeManager'
import { Draw } from '../Draw'
import { CheckboxControl } from './checkbox/CheckboxControl'
import { SelectControl } from './select/SelectControl'
import { TextControl } from './text/TextControl'
@@ -103,6 +104,8 @@ export class Control {
const selectControl = new SelectControl(element, this)
this.activeControl = selectControl
selectControl.awake()
} else if (control.type === ControlType.CHECKBOX) {
this.activeControl = new CheckboxControl(element, this)
}
// 激活控件回调
setTimeout(() => {
@@ -0,0 +1,85 @@
import { ControlComponent } from '../../../../dataset/enum/Control'
import { KeyMap } from '../../../../dataset/enum/Keymap'
import { IControlInstance } from '../../../../interface/Control'
import { IElement } from '../../../../interface/Element'
import { Control } from '../Control'
export class CheckboxControl implements IControlInstance {
private element: IElement
private control: Control
constructor(element: IElement, control: Control) {
this.element = element
this.control = control
}
public getElement(): IElement {
return this.element
}
public getCode(): string | null {
return this.element.control?.code || null
}
public getValue(): IElement[] {
const elementList = this.control.getElementList()
const { startIndex } = this.control.getRange()
const startElement = elementList[startIndex]
const data: IElement[] = []
// 向左查找
let preIndex = startIndex
while (preIndex > 0) {
const preElement = elementList[preIndex]
if (
preElement.controlId !== startElement.controlId ||
preElement.controlComponent === ControlComponent.PREFIX
) {
break
}
if (preElement.controlComponent === ControlComponent.VALUE) {
data.unshift(preElement)
}
preIndex--
}
// 向右查找
let nextIndex = startIndex + 1
while (nextIndex < elementList.length) {
const nextElement = elementList[nextIndex]
if (
nextElement.controlId !== startElement.controlId ||
nextElement.controlComponent === ControlComponent.POSTFIX
) {
break
}
if (nextElement.controlComponent === ControlComponent.VALUE) {
data.push(nextElement)
}
nextIndex++
}
return data
}
public setValue(): number {
const { endIndex } = this.control.getRange()
return endIndex
}
public keydown(evt: KeyboardEvent): number {
const range = this.control.getRange()
// 收缩边界到Value内
this.control.shrinkBoundary()
const { startIndex, endIndex } = range
// 删除
if (evt.key === KeyMap.Backspace || evt.key === KeyMap.Delete) {
return this.control.removeControl(startIndex)
}
return endIndex
}
public cut(): number {
const { endIndex } = this.control.getRange()
return endIndex
}
}
+1
View File
@@ -202,6 +202,7 @@ export class CanvasEvent {
// 设置位置上下文
this.position.setPositionContext({
isTable: isTable || false,
isCheckbox: isCheckbox || false,
isControl: isControl || false,
index,
trIndex,
+7 -2
View File
@@ -1,5 +1,6 @@
import { ElementType } from '../..'
import { ZERO } from '../../dataset/constant/Common'
import { ControlComponent } from '../../dataset/enum/Control'
import { IEditorOption } from '../../interface/Editor'
import { IElementPosition } from '../../interface/Element'
import { ICurrentPosition, IGetPositionByXYPayload, IPositionContext } from '../../interface/Position'
@@ -97,7 +98,8 @@ export class Position {
const tdValueElement = td.value[tdValueIndex]
return {
index,
isCheckbox: tdValueElement.type === ElementType.CHECKBOX,
isCheckbox: tdValueElement.type === ElementType.CHECKBOX ||
tdValueElement.controlComponent === ControlComponent.CHECKBOX,
isControl: tdValueElement.type === ElementType.CONTROL,
isImage: tablePosition.isImage,
isDirectHit: tablePosition.isDirectHit,
@@ -121,7 +123,10 @@ export class Position {
isImage: true
}
}
if (element.type === ElementType.CHECKBOX) {
if (
element.type === ElementType.CHECKBOX ||
element.controlComponent === ControlComponent.CHECKBOX
) {
return {
index: curPositionIndex,
isDirectHit: true,