Skip to content

Commit

Permalink
fix: control front and back operation boundary error
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Mar 28, 2024
1 parent 291ea26 commit 1bb7a58
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 92 deletions.
30 changes: 15 additions & 15 deletions src/editor/core/command/CommandAdapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,14 @@ export class CommandAdapt {

public applyPainterStyle() {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
this.canvasEvent.applyPainterStyle()
}

public format() {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const selection = this.range.getSelectionElementList()
// 选区设置或设置换行处样式
Expand Down Expand Up @@ -310,7 +310,7 @@ export class CommandAdapt {

public font(payload: string) {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const selection = this.range.getSelectionElementList()
if (selection?.length) {
Expand All @@ -333,7 +333,7 @@ export class CommandAdapt {
const { minSize, maxSize, defaultSize } = this.options
if (payload < minSize || payload > maxSize) return
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
// 选区设置或设置换行处样式
let renderOption: IDrawOption = {}
Expand Down Expand Up @@ -370,7 +370,7 @@ export class CommandAdapt {

public sizeAdd() {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const selection = this.range.getTextLikeSelectionElementList()
// 选区设置或设置换行处样式
Expand Down Expand Up @@ -410,7 +410,7 @@ export class CommandAdapt {

public sizeMinus() {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const selection = this.range.getTextLikeSelectionElementList()
// 选区设置或设置换行处样式
Expand Down Expand Up @@ -450,7 +450,7 @@ export class CommandAdapt {

public bold() {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const selection = this.range.getSelectionElementList()
if (selection?.length) {
Expand All @@ -472,7 +472,7 @@ export class CommandAdapt {

public italic() {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const selection = this.range.getSelectionElementList()
if (selection?.length) {
Expand All @@ -494,7 +494,7 @@ export class CommandAdapt {

public underline(textDecoration?: ITextDecoration) {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const selection = this.range.getSelectionElementList()
if (selection?.length) {
Expand Down Expand Up @@ -533,7 +533,7 @@ export class CommandAdapt {

public strikeout() {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const selection = this.range.getSelectionElementList()
if (selection?.length) {
Expand All @@ -558,7 +558,7 @@ export class CommandAdapt {

public superscript() {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const selection = this.range.getSelectionElementList()
if (!selection) return
Expand Down Expand Up @@ -588,7 +588,7 @@ export class CommandAdapt {

public subscript() {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const selection = this.range.getSelectionElementList()
if (!selection) return
Expand Down Expand Up @@ -618,7 +618,7 @@ export class CommandAdapt {

public color(payload: string | null) {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const selection = this.range.getSelectionElementList()
if (selection?.length) {
Expand Down Expand Up @@ -650,7 +650,7 @@ export class CommandAdapt {

public highlight(payload: string | null) {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const selection = this.range.getSelectionElementList()
if (selection?.length) {
Expand Down Expand Up @@ -1782,7 +1782,7 @@ export class CommandAdapt {

public image(payload: IDrawImagePayload) {
const isDisabled =
this.draw.isReadonly() || this.control.isDisabledControl()
this.draw.isReadonly() || this.control.getIsDisabledControl()
if (isDisabled) return
const { startIndex, endIndex } = this.range.getRange()
if (!~startIndex && !~endIndex) return
Expand Down
10 changes: 4 additions & 6 deletions src/editor/core/draw/Draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export class Draw {
case EditorMode.PRINT:
return true
case EditorMode.FORM:
return !this.control.isRangeWithinControl()
return !this.control.getIsRangeWithinControl()
default:
return false
}
Expand Down Expand Up @@ -551,9 +551,7 @@ export class Draw {
}

public insertElementList(payload: IElement[]) {
if (!payload.length) return
const isRangeCanInput = this.control.isRangeCanInput()
if (!isRangeCanInput) return
if (!payload.length || !this.range.getIsCanInput()) return
const { startIndex, endIndex } = this.range.getRange()
if (!~startIndex && !~endIndex) return
formatElementList(payload, {
Expand All @@ -564,11 +562,11 @@ export class Draw {
// 判断是否在控件内
let activeControl = this.control.getActiveControl()
// 光标在控件内如果当前没有被激活,需要手动激活
if (!activeControl && this.control.isRangeWithinControl()) {
if (!activeControl && this.control.getIsRangeWithinControl()) {
this.control.initControl()
activeControl = this.control.getActiveControl()
}
if (activeControl && !this.control.isRangInPostfix()) {
if (activeControl && this.control.getIsRangeWithinControl()) {
curIndex = activeControl.setValue(payload, undefined, {
isIgnoreDisabledRule: true
})
Expand Down
42 changes: 16 additions & 26 deletions src/editor/core/draw/control/Control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,34 @@ export class Control {
})
}

// 判断选区部分在控件边界外
public isPartRangeInControlOutside(): boolean {
// 是否属于控件可以捕获事件的选区
public getIsRangeCanCaptureEvent(): boolean {
if (!this.activeControl) return false
const { startIndex, endIndex } = this.getRange()
if (!~startIndex && !~endIndex) return false
const elementList = this.getElementList()
const startElement = elementList[startIndex]
// 闭合光标在后缀处
if (
startIndex === endIndex &&
startElement.controlComponent === ControlComponent.POSTFIX
) {
return true
}
// 在控件内
const endElement = elementList[endIndex]
if (
startElement.controlId &&
startElement.controlId !== endElement.controlId
startElement.controlId === endElement.controlId &&
endElement.controlComponent !== ControlComponent.POSTFIX
) {
return true
}
return false
}

// 判断选区是否在后缀处
public isRangInPostfix(): boolean {
public getIsRangeInPostfix(): boolean {
if (!this.activeControl) return false
const { startIndex, endIndex } = this.getRange()
if (startIndex !== endIndex) return false
Expand All @@ -143,7 +153,7 @@ export class Control {
}

// 判断选区是否在控件内
public isRangeWithinControl(): boolean {
public getIsRangeWithinControl(): boolean {
const { startIndex, endIndex } = this.getRange()
if (!~startIndex && !~endIndex) return false
const elementList = this.getElementList()
Expand All @@ -159,27 +169,7 @@ export class Control {
return false
}

// 判断是否在控件可输入的地方
public isRangeCanInput(): boolean {
const { startIndex, endIndex } = this.getRange()
if (!~startIndex && !~endIndex) return false
if (startIndex === endIndex) return true
const elementList = this.getElementList()
const startElement = elementList[startIndex]
const endElement = elementList[endIndex]
// 选区前后不是控件 || 选区前不在控件内&&选区后是后缀 || 选区前是控件&&选区后在控件内
return (
(!startElement.controlId && !endElement.controlId) ||
((!startElement.controlId ||
startElement.controlComponent === ControlComponent.POSTFIX) &&
endElement.controlComponent === ControlComponent.POSTFIX) ||
(!!startElement.controlId &&
endElement.controlId === startElement.controlId &&
endElement.controlComponent !== ControlComponent.POSTFIX)
)
}

public isDisabledControl(): boolean {
public getIsDisabledControl(): boolean {
return !!this.activeControl?.getElement().control?.disabled
}

Expand Down
4 changes: 2 additions & 2 deletions src/editor/core/draw/control/checkbox/CheckboxControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class CheckboxControl implements IControlInstance {
options: IControlRuleOption = {}
) {
// 校验是否可以设置
if (!options.isIgnoreDisabledRule && this.control.isDisabledControl()) {
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
return
}
const { control } = this.element
Expand Down Expand Up @@ -117,7 +117,7 @@ export class CheckboxControl implements IControlInstance {
}

public keydown(evt: KeyboardEvent): number | null {
if (this.control.isDisabledControl()) {
if (this.control.getIsDisabledControl()) {
return null
}
const range = this.control.getRange()
Expand Down
10 changes: 5 additions & 5 deletions src/editor/core/draw/control/select/SelectControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class SelectControl implements IControlInstance {
}

public keydown(evt: KeyboardEvent): number | null {
if (this.control.isDisabledControl()) {
if (this.control.getIsDisabledControl()) {
return null
}
const elementList = this.control.getElementList()
Expand Down Expand Up @@ -134,7 +134,7 @@ export class SelectControl implements IControlInstance {
}

public cut(): number {
if (this.control.isDisabledControl()) {
if (this.control.getIsDisabledControl()) {
return -1
}
this.control.shrinkBoundary()
Expand All @@ -151,7 +151,7 @@ export class SelectControl implements IControlInstance {
options: IControlRuleOption = {}
): number {
// 校验是否可以设置
if (!options.isIgnoreDisabledRule && this.control.isDisabledControl()) {
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
return -1
}
const elementList = context.elementList || this.control.getElementList()
Expand Down Expand Up @@ -201,7 +201,7 @@ export class SelectControl implements IControlInstance {
options: IControlRuleOption = {}
) {
// 校验是否可以设置
if (!options.isIgnoreDisabledRule && this.control.isDisabledControl()) {
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
return
}
const control = this.element.control!
Expand Down Expand Up @@ -291,7 +291,7 @@ export class SelectControl implements IControlInstance {
}

public awake() {
if (this.isPopup || this.control.isDisabledControl()) return
if (this.isPopup || this.control.getIsDisabledControl()) return
const { startIndex } = this.control.getRange()
const elementList = this.control.getElementList()
if (elementList[startIndex + 1]?.controlId !== this.element.controlId) {
Expand Down
8 changes: 4 additions & 4 deletions src/editor/core/draw/control/text/TextControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class TextControl implements IControlInstance {
options: IControlRuleOption = {}
): number {
// 校验是否可以设置
if (!options.isIgnoreDisabledRule && this.control.isDisabledControl()) {
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
return -1
}
const elementList = context.elementList || this.control.getElementList()
Expand Down Expand Up @@ -111,7 +111,7 @@ export class TextControl implements IControlInstance {
options: IControlRuleOption = {}
): number {
// 校验是否可以设置
if (!options.isIgnoreDisabledRule && this.control.isDisabledControl()) {
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
return -1
}
const elementList = context.elementList || this.control.getElementList()
Expand All @@ -128,7 +128,7 @@ export class TextControl implements IControlInstance {
}

public keydown(evt: KeyboardEvent): number | null {
if (this.control.isDisabledControl()) {
if (this.control.getIsDisabledControl()) {
return null
}
const elementList = this.control.getElementList()
Expand Down Expand Up @@ -209,7 +209,7 @@ export class TextControl implements IControlInstance {
}

public cut(): number {
if (this.control.isDisabledControl()) {
if (this.control.getIsDisabledControl()) {
return -1
}
this.control.shrinkBoundary()
Expand Down
11 changes: 4 additions & 7 deletions src/editor/core/event/handlers/cut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ export function cut(host: CanvasEvent) {
const rangeManager = draw.getRange()
const { startIndex, endIndex } = rangeManager.getRange()
if (!~startIndex && !~startIndex) return
const isReadonly = draw.isReadonly()
if (isReadonly) return
const control = draw.getControl()
const isPartRangeInControlOutside = control.isPartRangeInControlOutside()
if (isPartRangeInControlOutside) return
const activeControl = control.getActiveControl()
if (draw.isReadonly() || !rangeManager.getIsCanInput()) return

const elementList = draw.getElementList()
let start = startIndex
let end = endIndex
Expand All @@ -37,8 +33,9 @@ export function cut(host: CanvasEvent) {
const options = draw.getOptions()
// 写入粘贴板
writeElementList(elementList.slice(start + 1, end + 1), options)
const control = draw.getControl()
let curIndex: number
if (activeControl) {
if (control.getActiveControl() && control.getIsRangeWithinControl()) {
curIndex = control.cut()
} else {
draw.spliceElementList(elementList, start + 1, end - start)
Expand Down
Loading

0 comments on commit 1bb7a58

Please sign in to comment.