feat: add control deletable rule #301
This commit is contained in:
@@ -81,6 +81,7 @@ interface IElement {
|
||||
minWidth?: number;
|
||||
underline?: boolean;
|
||||
extension?: unknown;
|
||||
deletable?: boolean;
|
||||
code: string | null;
|
||||
min?: number;
|
||||
max?: number;
|
||||
|
||||
@@ -81,6 +81,7 @@ interface IElement {
|
||||
minWidth?: number;
|
||||
underline?: boolean;
|
||||
extension?: unknown;
|
||||
deletable?: boolean;
|
||||
code: string | null;
|
||||
min?: number;
|
||||
max?: number;
|
||||
|
||||
@@ -1945,6 +1945,7 @@ export class CommandAdapt {
|
||||
// 删除控件
|
||||
const control = this.draw.getControl()
|
||||
const newIndex = control.removeControl(startIndex)
|
||||
if (newIndex === null) return
|
||||
// 重新渲染
|
||||
this.range.setRange(newIndex, newIndex)
|
||||
this.draw.render({
|
||||
|
||||
@@ -301,9 +301,11 @@ export class Control {
|
||||
}
|
||||
}
|
||||
|
||||
public removeControl(startIndex: number): number {
|
||||
public removeControl(startIndex: number): number | null {
|
||||
const elementList = this.getElementList()
|
||||
const startElement = elementList[startIndex]
|
||||
const { deletable = true } = startElement.control!
|
||||
if (!deletable) return null
|
||||
let leftIndex = -1
|
||||
let rightIndex = -1
|
||||
// 向左查找
|
||||
@@ -395,7 +397,7 @@ export class Control {
|
||||
return this.activeControl.setValue(data)
|
||||
}
|
||||
|
||||
public keydown(evt: KeyboardEvent): number {
|
||||
public keydown(evt: KeyboardEvent): number | null {
|
||||
if (!this.activeControl) {
|
||||
throw new Error('active control is null')
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ export class CheckboxControl implements IControlInstance {
|
||||
control!.code = data.join(',')
|
||||
}
|
||||
|
||||
public keydown(evt: KeyboardEvent): number {
|
||||
public keydown(evt: KeyboardEvent): number | null {
|
||||
const range = this.control.getRange()
|
||||
// 收缩边界到Value内
|
||||
this.control.shrinkBoundary()
|
||||
|
||||
@@ -78,7 +78,7 @@ export class SelectControl implements IControlInstance {
|
||||
return -1
|
||||
}
|
||||
|
||||
public keydown(evt: KeyboardEvent): number {
|
||||
public keydown(evt: KeyboardEvent): number | null {
|
||||
const elementList = this.control.getElementList()
|
||||
const range = this.control.getRange()
|
||||
// 收缩边界到Value内
|
||||
|
||||
@@ -108,7 +108,7 @@ export class TextControl implements IControlInstance {
|
||||
return startIndex
|
||||
}
|
||||
|
||||
public keydown(evt: KeyboardEvent): number {
|
||||
public keydown(evt: KeyboardEvent): number | null {
|
||||
const elementList = this.control.getElementList()
|
||||
const range = this.control.getRange()
|
||||
// 收缩边界到Value内
|
||||
|
||||
@@ -28,7 +28,7 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) {
|
||||
const activeControl = control.getActiveControl()
|
||||
if (evt.key === KeyMap.Backspace) {
|
||||
if (isReadonly || isPartRangeInControlOutside) return
|
||||
let curIndex: number
|
||||
let curIndex: number | null
|
||||
if (activeControl) {
|
||||
curIndex = control.keydown(evt)
|
||||
} else {
|
||||
@@ -58,11 +58,12 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) {
|
||||
}
|
||||
curIndex = isCollapsed ? index - 1 : startIndex
|
||||
}
|
||||
if (curIndex === null) return
|
||||
rangeManager.setRange(curIndex, curIndex)
|
||||
draw.render({ curIndex })
|
||||
} else if (evt.key === KeyMap.Delete) {
|
||||
if (isReadonly || isPartRangeInControlOutside) return
|
||||
let curIndex: number
|
||||
let curIndex: number | null
|
||||
if (activeControl) {
|
||||
curIndex = control.keydown(evt)
|
||||
} else if (elementList[endIndex + 1]?.type === ElementType.CONTROL) {
|
||||
@@ -79,6 +80,7 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) {
|
||||
}
|
||||
curIndex = isCollapsed ? index : startIndex
|
||||
}
|
||||
if (curIndex === null) return
|
||||
rangeManager.setRange(curIndex, curIndex)
|
||||
draw.render({ curIndex })
|
||||
} else if (evt.key === KeyMap.Enter) {
|
||||
|
||||
@@ -21,6 +21,10 @@ export interface IControlCheckbox {
|
||||
checkbox?: ICheckbox
|
||||
}
|
||||
|
||||
export interface IControlRule {
|
||||
deletable?: boolean
|
||||
}
|
||||
|
||||
export interface IControlBasic {
|
||||
type: ControlType
|
||||
value: IElement[] | null
|
||||
@@ -34,6 +38,7 @@ export interface IControlBasic {
|
||||
}
|
||||
|
||||
export type IControl = IControlBasic &
|
||||
IControlRule &
|
||||
Partial<IControlSelect> &
|
||||
Partial<IControlCheckbox>
|
||||
|
||||
@@ -63,7 +68,7 @@ export interface IControlInstance {
|
||||
|
||||
setValue(data: IElement[]): number
|
||||
|
||||
keydown(evt: KeyboardEvent): number
|
||||
keydown(evt: KeyboardEvent): number | null
|
||||
|
||||
cut(): number
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user