Skip to content

Commit

Permalink
feat: add control deletable rule #301
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Oct 25, 2023
1 parent 22c69ee commit e5acf6e
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/en/guide/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ interface IElement {
minWidth?: number;
underline?: boolean;
extension?: unknown;
deletable?: boolean;
code: string | null;
min?: number;
max?: number;
Expand Down
1 change: 1 addition & 0 deletions docs/guide/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ interface IElement {
minWidth?: number;
underline?: boolean;
extension?: unknown;
deletable?: boolean;
code: string | null;
min?: number;
max?: number;
Expand Down
1 change: 1 addition & 0 deletions src/editor/core/command/CommandAdapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
6 changes: 4 additions & 2 deletions src/editor/core/draw/control/Control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
// 向左查找
Expand Down Expand Up @@ -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')
}
Expand Down
2 changes: 1 addition & 1 deletion src/editor/core/draw/control/checkbox/CheckboxControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/editor/core/draw/control/select/SelectControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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内
Expand Down
2 changes: 1 addition & 1 deletion src/editor/core/draw/control/text/TextControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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内
Expand Down
6 changes: 4 additions & 2 deletions src/editor/core/event/handlers/keydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion src/editor/interface/Control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export interface IControlCheckbox {
checkbox?: ICheckbox
}

export interface IControlRule {
deletable?: boolean
}

export interface IControlBasic {
type: ControlType
value: IElement[] | null
Expand All @@ -34,6 +38,7 @@ export interface IControlBasic {
}

export type IControl = IControlBasic &
IControlRule &
Partial<IControlSelect> &
Partial<IControlCheckbox>

Expand Down Expand Up @@ -63,7 +68,7 @@ export interface IControlInstance {

setValue(data: IElement[]): number

keydown(evt: KeyboardEvent): number
keydown(evt: KeyboardEvent): number | null

cut(): number
}
Expand Down

0 comments on commit e5acf6e

Please sign in to comment.