commit
7ae8f0775a
@ -0,0 +1,47 @@
|
|||||||
|
import Editor, { ControlType, ElementType } from '../../../src/editor'
|
||||||
|
|
||||||
|
describe('控件-复选框', () => {
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
cy.visit('http://localhost:3000/canvas-editor/')
|
||||||
|
|
||||||
|
cy.get('canvas').first().as('canvas').should('have.length', 1)
|
||||||
|
})
|
||||||
|
|
||||||
|
const elementType: ElementType = <ElementType>'control'
|
||||||
|
const controlType: ControlType = <ControlType>'checkbox'
|
||||||
|
|
||||||
|
it('复选框', () => {
|
||||||
|
cy.getEditor().then((editor: Editor) => {
|
||||||
|
editor.listener.saved = function (payload) {
|
||||||
|
const data = payload.data[0]
|
||||||
|
|
||||||
|
expect(data.control!.code).to.be.eq('98175')
|
||||||
|
}
|
||||||
|
|
||||||
|
editor.command.executeSelectAll()
|
||||||
|
|
||||||
|
editor.command.executeBackspace()
|
||||||
|
|
||||||
|
editor.command.executeInsertElementList([{
|
||||||
|
type: elementType,
|
||||||
|
value: '',
|
||||||
|
control: {
|
||||||
|
code: '98175',
|
||||||
|
type: controlType,
|
||||||
|
value: null,
|
||||||
|
valueSets: [{
|
||||||
|
value: '有',
|
||||||
|
code: '98175'
|
||||||
|
}, {
|
||||||
|
value: '无',
|
||||||
|
code: '98176'
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}])
|
||||||
|
|
||||||
|
cy.get('@canvas').type('{ctrl}s')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
@ -0,0 +1,130 @@
|
|||||||
|
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 setSelect() {
|
||||||
|
const { control } = this.element
|
||||||
|
const elementList = this.control.getElementList()
|
||||||
|
const { startIndex } = this.control.getRange()
|
||||||
|
const startElement = elementList[startIndex]
|
||||||
|
const data: string[] = []
|
||||||
|
// 向左查找
|
||||||
|
let preIndex = startIndex
|
||||||
|
while (preIndex > 0) {
|
||||||
|
const preElement = elementList[preIndex]
|
||||||
|
if (
|
||||||
|
preElement.controlId !== startElement.controlId ||
|
||||||
|
preElement.controlComponent === ControlComponent.PREFIX
|
||||||
|
) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if (preElement.controlComponent === ControlComponent.CHECKBOX) {
|
||||||
|
const checkbox = preElement.checkbox
|
||||||
|
if (checkbox && checkbox.value && checkbox.code) {
|
||||||
|
data.unshift(checkbox.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.CHECKBOX) {
|
||||||
|
const checkbox = nextElement.checkbox
|
||||||
|
if (checkbox && checkbox.value && checkbox.code) {
|
||||||
|
data.push(checkbox.code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nextIndex++
|
||||||
|
}
|
||||||
|
control!.code = data.join(',')
|
||||||
|
}
|
||||||
|
|
||||||
|
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,11 +1,13 @@
|
|||||||
export enum ControlType {
|
export enum ControlType {
|
||||||
TEXT = 'text',
|
TEXT = 'text',
|
||||||
SELECT = 'select'
|
SELECT = 'select',
|
||||||
|
CHECKBOX = 'checkbox'
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum ControlComponent {
|
export enum ControlComponent {
|
||||||
PREFIX = 'prefix',
|
PREFIX = 'prefix',
|
||||||
POSTFIX = 'postfix',
|
POSTFIX = 'postfix',
|
||||||
PLACEHOLDER = 'placeholder',
|
PLACEHOLDER = 'placeholder',
|
||||||
VALUE = 'value'
|
VALUE = 'value',
|
||||||
|
CHECKBOX = 'checkbox'
|
||||||
}
|
}
|
||||||
Loading…
Reference in new issue