From 3fb843570680d1607834b47c5ad86781ea4f5f14 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Fri, 29 Dec 2023 22:32:56 +0800 Subject: [PATCH] fix: checkbox cannot be selected #382 --- src/editor/core/draw/Draw.ts | 4 +++ .../core/draw/particle/CheckboxParticle.ts | 18 +++++++++++++ src/editor/core/event/handlers/mousedown.ts | 25 +++++++++++-------- src/main.ts | 3 +++ 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/editor/core/draw/Draw.ts b/src/editor/core/draw/Draw.ts index b1ca401..384c4c3 100644 --- a/src/editor/core/draw/Draw.ts +++ b/src/editor/core/draw/Draw.ts @@ -703,6 +703,10 @@ export class Draw { return this.listParticle } + public getCheckboxParticle(): CheckboxParticle { + return this.checkboxParticle + } + public getControl(): Control { return this.control } diff --git a/src/editor/core/draw/particle/CheckboxParticle.ts b/src/editor/core/draw/particle/CheckboxParticle.ts index ff02ed7..d453038 100644 --- a/src/editor/core/draw/particle/CheckboxParticle.ts +++ b/src/editor/core/draw/particle/CheckboxParticle.ts @@ -1,15 +1,33 @@ 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 CheckboxParticle { + private draw: Draw private options: DeepRequired constructor(draw: Draw) { + this.draw = draw this.options = draw.getOptions() } + public setSelect(element: IElement) { + const { checkbox } = element + if (checkbox) { + checkbox.value = !checkbox.value + } else { + element.checkbox = { + value: true + } + } + this.draw.render({ + isCompute: false, + isSetCursor: false + }) + } + public render( ctx: CanvasRenderingContext2D, element: IRowElement, diff --git a/src/editor/core/event/handlers/mousedown.ts b/src/editor/core/event/handlers/mousedown.ts index 18f1352..217bf47 100644 --- a/src/editor/core/event/handlers/mousedown.ts +++ b/src/editor/core/event/handlers/mousedown.ts @@ -68,18 +68,23 @@ export function mousedown(evt: MouseEvent, host: CanvasEvent) { const isSetCheckbox = isDirectHitCheckbox && !isReadonly if (isSetCheckbox) { const { checkbox, control } = curElement - const codes = control?.code?.split(',') || [] - if (checkbox?.value) { - const codeIndex = codes.findIndex(c => c === checkbox.code) - codes.splice(codeIndex, 1) + // 复选框不在控件内独立控制 + if (!control) { + draw.getCheckboxParticle().setSelect(curElement) } else { - if (checkbox?.code) { - codes.push(checkbox.code) + const codes = control?.code?.split(',') || [] + if (checkbox?.value) { + const codeIndex = codes.findIndex(c => c === checkbox.code) + codes.splice(codeIndex, 1) + } else { + if (checkbox?.code) { + codes.push(checkbox.code) + } + } + const activeControl = draw.getControl().getActiveControl() + if (activeControl instanceof CheckboxControl) { + activeControl.setSelect(codes) } - } - const activeControl = draw.getControl().getActiveControl() - if (activeControl instanceof CheckboxControl) { - activeControl.setSelect(codes) } } else { draw.render({ diff --git a/src/main.ts b/src/main.ts index e07f017..0602747 100644 --- a/src/main.ts +++ b/src/main.ts @@ -749,6 +749,9 @@ window.onload = function () { instance.command.executeInsertElementList([ { type: ElementType.CHECKBOX, + checkbox: { + value: false + }, value: '' } ])