feat: add radio element #494
Co-authored-by: jeffycai <caixiaobing@live.cn> Co-authored-by: Hufe921 <huangyunfeihufe@hotmail.com>
This commit is contained in:
co-authored by
jeffycai
Hufe921
parent
2e1403507f
commit
c6d9cffc27
@@ -65,6 +65,7 @@ import {
|
||||
import { Control } from './control/Control'
|
||||
import { getSlimCloneElementList, zipElementList } from '../../utils/element'
|
||||
import { CheckboxParticle } from './particle/CheckboxParticle'
|
||||
import { RadioParticle } from './particle/RadioParticle'
|
||||
import { DeepRequired, IPadding } from '../../interface/Common'
|
||||
import {
|
||||
ControlComponent,
|
||||
@@ -142,6 +143,7 @@ export class Draw {
|
||||
private superscriptParticle: SuperscriptParticle
|
||||
private subscriptParticle: SubscriptParticle
|
||||
private checkboxParticle: CheckboxParticle
|
||||
private radioParticle: RadioParticle
|
||||
private blockParticle: BlockParticle
|
||||
private listParticle: ListParticle
|
||||
private lineBreakParticle: LineBreakParticle
|
||||
@@ -216,6 +218,7 @@ export class Draw {
|
||||
this.superscriptParticle = new SuperscriptParticle()
|
||||
this.subscriptParticle = new SubscriptParticle()
|
||||
this.checkboxParticle = new CheckboxParticle(this)
|
||||
this.radioParticle = new RadioParticle(this)
|
||||
this.blockParticle = new BlockParticle(this)
|
||||
this.listParticle = new ListParticle(this)
|
||||
this.lineBreakParticle = new LineBreakParticle(this)
|
||||
@@ -751,6 +754,10 @@ export class Draw {
|
||||
return this.checkboxParticle
|
||||
}
|
||||
|
||||
public getRadioParticle(): RadioParticle {
|
||||
return this.radioParticle
|
||||
}
|
||||
|
||||
public getControl(): Control {
|
||||
return this.control
|
||||
}
|
||||
@@ -1414,6 +1421,15 @@ export class Draw {
|
||||
element.width = availableWidth / scale
|
||||
metrics.width = availableWidth
|
||||
metrics.height = defaultSize
|
||||
} else if (
|
||||
element.type === ElementType.RADIO ||
|
||||
element.controlComponent === ControlComponent.RADIO
|
||||
) {
|
||||
const { width, height, gap } = this.options.radio
|
||||
const elementWidth = width + gap * 2
|
||||
element.width = elementWidth
|
||||
metrics.width = elementWidth * scale
|
||||
metrics.height = height * scale
|
||||
} else if (
|
||||
element.type === ElementType.CHECKBOX ||
|
||||
element.controlComponent === ControlComponent.CHECKBOX
|
||||
@@ -1805,6 +1821,12 @@ export class Draw {
|
||||
) {
|
||||
this.textParticle.complete()
|
||||
this.checkboxParticle.render(ctx, element, x, y + offsetY)
|
||||
} else if (
|
||||
element.type === ElementType.RADIO ||
|
||||
element.controlComponent === ControlComponent.RADIO
|
||||
) {
|
||||
this.textParticle.complete()
|
||||
this.radioParticle.render(ctx, element, x, y + offsetY)
|
||||
} else if (element.type === ElementType.TAB) {
|
||||
this.textParticle.complete()
|
||||
} else if (element.rowFlex === RowFlex.ALIGNMENT) {
|
||||
|
||||
@@ -32,6 +32,7 @@ import { Listener } from '../../listener/Listener'
|
||||
import { RangeManager } from '../../range/RangeManager'
|
||||
import { Draw } from '../Draw'
|
||||
import { CheckboxControl } from './checkbox/CheckboxControl'
|
||||
import { RadioControl } from './radio/RadioControl'
|
||||
import { ControlSearch } from './interactive/ControlSearch'
|
||||
import { ControlBorder } from './richtext/Border'
|
||||
import { SelectControl } from './select/SelectControl'
|
||||
@@ -236,6 +237,8 @@ export class Control {
|
||||
selectControl.awake()
|
||||
} else if (control.type === ControlType.CHECKBOX) {
|
||||
this.activeControl = new CheckboxControl(element, this)
|
||||
} else if (control.type === ControlType.RADIO) {
|
||||
this.activeControl = new RadioControl(element, this)
|
||||
}
|
||||
// 激活控件回调
|
||||
nextTick(() => {
|
||||
@@ -522,7 +525,8 @@ export class Control {
|
||||
})
|
||||
} else if (
|
||||
type === ControlType.SELECT ||
|
||||
type === ControlType.CHECKBOX
|
||||
type === ControlType.CHECKBOX ||
|
||||
type === ControlType.RADIO
|
||||
) {
|
||||
const innerText = code
|
||||
?.split(',')
|
||||
@@ -629,6 +633,10 @@ export class Control {
|
||||
const checkbox = new CheckboxControl(element, this)
|
||||
const codes = value?.split(',') || []
|
||||
checkbox.setSelect(codes, controlContext, controlRule)
|
||||
} else if (type === ControlType.RADIO) {
|
||||
const radio = new RadioControl(element, this)
|
||||
const codes = value ? [value] : []
|
||||
radio.setSelect(codes, controlContext, controlRule)
|
||||
}
|
||||
// 修改后控件结束索引
|
||||
let newEndIndex = i
|
||||
|
||||
@@ -9,8 +9,8 @@ import { IElement } from '../../../../interface/Element'
|
||||
import { Control } from '../Control'
|
||||
|
||||
export class CheckboxControl implements IControlInstance {
|
||||
private element: IElement
|
||||
private control: Control
|
||||
protected element: IElement
|
||||
protected control: Control
|
||||
|
||||
constructor(element: IElement, control: Control) {
|
||||
this.element = element
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { ControlComponent } from '../../../../dataset/enum/Control'
|
||||
import {
|
||||
IControlContext,
|
||||
IControlRuleOption
|
||||
} from '../../../../interface/Control'
|
||||
import { CheckboxControl } from '../checkbox/CheckboxControl'
|
||||
|
||||
export class RadioControl extends CheckboxControl {
|
||||
public setSelect(
|
||||
codes: string[],
|
||||
context: IControlContext = {},
|
||||
options: IControlRuleOption = {}
|
||||
) {
|
||||
// 校验是否可以设置
|
||||
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
|
||||
return
|
||||
}
|
||||
const { control } = this.element
|
||||
const elementList = context.elementList || this.control.getElementList()
|
||||
const { startIndex } = context.range || this.control.getRange()
|
||||
const startElement = elementList[startIndex]
|
||||
// 向左查找
|
||||
let preIndex = startIndex
|
||||
while (preIndex > 0) {
|
||||
const preElement = elementList[preIndex]
|
||||
if (
|
||||
preElement.controlId !== startElement.controlId ||
|
||||
preElement.controlComponent === ControlComponent.PREFIX
|
||||
) {
|
||||
break
|
||||
}
|
||||
if (preElement.controlComponent === ControlComponent.RADIO) {
|
||||
const radio = preElement.radio!
|
||||
radio.value = codes.includes(radio.code!)
|
||||
}
|
||||
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.RADIO) {
|
||||
const radio = nextElement.radio!
|
||||
radio.value = codes.includes(radio.code!)
|
||||
}
|
||||
nextIndex++
|
||||
}
|
||||
control!.code = codes.join(',')
|
||||
this.control.repaintControl()
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ export class CheckboxParticle {
|
||||
y: number
|
||||
) {
|
||||
const {
|
||||
checkbox: { gap, lineWidth, fillStyle, fontStyle },
|
||||
checkbox: { gap, lineWidth, fillStyle, strokeStyle },
|
||||
scale
|
||||
} = this.options
|
||||
const { metrics, checkbox } = element
|
||||
@@ -60,7 +60,7 @@ export class CheckboxParticle {
|
||||
ctx.fillRect(left, top, width, height)
|
||||
// 勾选对号
|
||||
ctx.beginPath()
|
||||
ctx.strokeStyle = fontStyle
|
||||
ctx.strokeStyle = strokeStyle
|
||||
ctx.lineWidth = lineWidth * 2 * scale
|
||||
ctx.moveTo(left + 2 * scale, top + height / 2)
|
||||
ctx.lineTo(left + width / 2, top + height - 3 * scale)
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import { DeepRequired } from '../../../interface/Common'
|
||||
import { IEditorOption } from '../../../interface/Editor'
|
||||
import { IElement } from '../../../interface/Element'
|
||||
import { IRowElement } from '../../../interface/Row'
|
||||
import { Draw } from '../Draw'
|
||||
|
||||
export class RadioParticle {
|
||||
private draw: Draw
|
||||
private options: DeepRequired<IEditorOption>
|
||||
|
||||
constructor(draw: Draw) {
|
||||
this.draw = draw
|
||||
this.options = draw.getOptions()
|
||||
}
|
||||
|
||||
public setSelect(element: IElement) {
|
||||
const { radio } = element
|
||||
if (radio) {
|
||||
radio.value = !radio.value
|
||||
} else {
|
||||
element.radio = {
|
||||
value: true
|
||||
}
|
||||
}
|
||||
this.draw.render({
|
||||
isCompute: false,
|
||||
isSetCursor: false
|
||||
})
|
||||
}
|
||||
|
||||
public render(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
element: IRowElement,
|
||||
x: number,
|
||||
y: number
|
||||
) {
|
||||
const {
|
||||
radio: { gap, lineWidth, fillStyle, strokeStyle },
|
||||
scale
|
||||
} = this.options
|
||||
const { metrics, radio } = element
|
||||
// left top 四舍五入避免1像素问题
|
||||
const left = Math.round(x + gap * scale)
|
||||
const top = Math.round(y - metrics.height + lineWidth)
|
||||
const width = metrics.width - gap * 2 * scale
|
||||
const height = metrics.height
|
||||
ctx.save()
|
||||
ctx.beginPath()
|
||||
ctx.translate(0.5, 0.5)
|
||||
// 边框
|
||||
ctx.strokeStyle = radio?.value ? fillStyle : strokeStyle
|
||||
ctx.lineWidth = lineWidth
|
||||
ctx.arc(left + width / 2, top + height / 2, width / 2, 0, Math.PI * 2)
|
||||
ctx.stroke()
|
||||
// 填充选中色
|
||||
if (radio?.value) {
|
||||
ctx.beginPath()
|
||||
ctx.fillStyle = fillStyle
|
||||
ctx.arc(left + width / 2, top + height / 2, width / 3, 0, Math.PI * 2)
|
||||
ctx.fill()
|
||||
}
|
||||
ctx.closePath()
|
||||
ctx.restore()
|
||||
}
|
||||
}
|
||||
@@ -124,8 +124,13 @@ function dblclick(host: CanvasEvent, evt: MouseEvent) {
|
||||
return
|
||||
}
|
||||
}
|
||||
// 复选框双击时是切换选择状态,禁用扩选
|
||||
if (positionContext.isCheckbox && positionContext.isDirectHit) return
|
||||
// 复选/单选框双击时是切换选择状态,禁用扩选
|
||||
if (
|
||||
(positionContext.isCheckbox || positionContext.isRadio) &&
|
||||
positionContext.isDirectHit
|
||||
) {
|
||||
return
|
||||
}
|
||||
// 自动扩选文字-分词处理,优先使用分词器否则降级使用光标所在位置
|
||||
const rangeManager = draw.getRange()
|
||||
const segmenterRange =
|
||||
|
||||
@@ -4,6 +4,7 @@ import { MouseEventButton } from '../../../dataset/enum/Event'
|
||||
import { deepClone } from '../../../utils'
|
||||
import { isMod } from '../../../utils/hotkey'
|
||||
import { CheckboxControl } from '../../draw/control/checkbox/CheckboxControl'
|
||||
import { RadioControl } from '../../draw/control/radio/RadioControl'
|
||||
import { CanvasEvent } from '../CanvasEvent'
|
||||
|
||||
export function setRangeCache(host: CanvasEvent) {
|
||||
@@ -54,6 +55,7 @@ export function mousedown(evt: MouseEvent, host: CanvasEvent) {
|
||||
index,
|
||||
isDirectHit,
|
||||
isCheckbox,
|
||||
isRadio,
|
||||
isImage,
|
||||
isTable,
|
||||
tdValueIndex,
|
||||
@@ -73,11 +75,14 @@ export function mousedown(evt: MouseEvent, host: CanvasEvent) {
|
||||
// 绘制
|
||||
const isDirectHitImage = !!(isDirectHit && isImage)
|
||||
const isDirectHitCheckbox = !!(isDirectHit && isCheckbox)
|
||||
const isDirectHitRadio = !!(isDirectHit && isRadio)
|
||||
if (~index) {
|
||||
rangeManager.setRange(curIndex, curIndex)
|
||||
position.setCursorPosition(positionList[curIndex])
|
||||
// 复选框
|
||||
const isSetCheckbox = isDirectHitCheckbox && !isReadonly
|
||||
// 单选框
|
||||
const isSetRadio = isDirectHitRadio && !isReadonly
|
||||
if (isSetCheckbox) {
|
||||
const { checkbox, control } = curElement
|
||||
// 复选框不在控件内独立控制
|
||||
@@ -98,12 +103,25 @@ export function mousedown(evt: MouseEvent, host: CanvasEvent) {
|
||||
activeControl.setSelect(codes)
|
||||
}
|
||||
}
|
||||
} else if (isSetRadio) {
|
||||
const { control, radio } = curElement
|
||||
// 单选框不在控件内独立控制
|
||||
if (!control) {
|
||||
draw.getRadioParticle().setSelect(curElement)
|
||||
} else {
|
||||
const codes = radio?.code ? [radio.code] : []
|
||||
const activeControl = draw.getControl().getActiveControl()
|
||||
if (activeControl instanceof RadioControl) {
|
||||
activeControl.setSelect(codes)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
draw.render({
|
||||
curIndex,
|
||||
isCompute: false,
|
||||
isSubmitHistory: false,
|
||||
isSetCursor: !isDirectHitImage && !isDirectHitCheckbox
|
||||
isSetCursor:
|
||||
!isDirectHitImage && !isDirectHitCheckbox && !isDirectHitRadio
|
||||
})
|
||||
}
|
||||
// 首字需定位到行首,非上一行最后一个字后
|
||||
|
||||
@@ -394,6 +394,9 @@ export class Position {
|
||||
tdValueElement.type === ElementType.CHECKBOX ||
|
||||
tdValueElement.controlComponent ===
|
||||
ControlComponent.CHECKBOX,
|
||||
isRadio:
|
||||
tdValueElement.type === ElementType.RADIO ||
|
||||
tdValueElement.controlComponent === ControlComponent.RADIO,
|
||||
isControl: !!tdValueElement.controlId,
|
||||
isImage: tablePosition.isImage,
|
||||
isDirectHit: tablePosition.isDirectHit,
|
||||
@@ -431,6 +434,16 @@ export class Position {
|
||||
isCheckbox: true
|
||||
}
|
||||
}
|
||||
if (
|
||||
element.type === ElementType.RADIO ||
|
||||
element.controlComponent === ControlComponent.RADIO
|
||||
) {
|
||||
return {
|
||||
index: curPositionIndex,
|
||||
isDirectHit: true,
|
||||
isRadio: true
|
||||
}
|
||||
}
|
||||
let hitLineStartIndex: number | undefined
|
||||
// 判断是否在文字中间前后
|
||||
if (elementList[index].value !== ZERO) {
|
||||
@@ -651,6 +664,7 @@ export class Position {
|
||||
const {
|
||||
index,
|
||||
isCheckbox,
|
||||
isRadio,
|
||||
isControl,
|
||||
isTable,
|
||||
trIndex,
|
||||
@@ -663,6 +677,7 @@ export class Position {
|
||||
this.setPositionContext({
|
||||
isTable: isTable || false,
|
||||
isCheckbox: isCheckbox || false,
|
||||
isRadio: isRadio || false,
|
||||
isControl: isControl || false,
|
||||
index,
|
||||
trIndex,
|
||||
|
||||
@@ -12,6 +12,7 @@ enum ElementType {
|
||||
PAGE_BREAK = 'pageBreak',
|
||||
CONTROL = 'control',
|
||||
CHECKBOX = 'checkbox',
|
||||
RADIO = 'radio',
|
||||
LATEX = 'latex',
|
||||
TAB = 'tab',
|
||||
DATE = 'date',
|
||||
|
||||
Reference in New Issue
Block a user