From eb28d2059a5f91e751e7541958ceb4f1a13092fe Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Tue, 5 Apr 2022 18:44:36 +0800 Subject: [PATCH] feat:text control copy and cut --- src/editor/core/draw/control/Control.ts | 7 +++++ .../core/draw/control/text/TextControl.ts | 15 +++++++++++ src/editor/core/event/CanvasEvent.ts | 27 ++++++++++++++----- src/editor/dataset/constant/Element.ts | 3 ++- src/editor/interface/Control.ts | 2 ++ 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/editor/core/draw/control/Control.ts b/src/editor/core/draw/control/Control.ts index 1ebdde5..f0748ec 100644 --- a/src/editor/core/draw/control/Control.ts +++ b/src/editor/core/draw/control/Control.ts @@ -313,4 +313,11 @@ export class Control { return this.activeControl.keydown(evt) } + public cut(): number { + if (!this.activeControl) { + throw new Error('active control is null') + } + return this.activeControl.cut() + } + } \ No newline at end of file diff --git a/src/editor/core/draw/control/text/TextControl.ts b/src/editor/core/draw/control/text/TextControl.ts index b515370..7068ac1 100644 --- a/src/editor/core/draw/control/text/TextControl.ts +++ b/src/editor/core/draw/control/text/TextControl.ts @@ -149,4 +149,19 @@ export class TextControl implements IControlInstance { return -1 } + public cut(): number { + this.control.shrinkBoundary() + const { startIndex, endIndex } = this.control.getRange() + if (startIndex === endIndex) { + return startIndex + } + const elementList = this.control.getElementList() + elementList.splice(startIndex + 1, endIndex - startIndex) + const value = this.getValue() + if (!value.length) { + this.control.addPlaceholder(startIndex) + } + return startIndex + } + } \ No newline at end of file diff --git a/src/editor/core/event/CanvasEvent.ts b/src/editor/core/event/CanvasEvent.ts index ab11b40..dfd25df 100644 --- a/src/editor/core/event/CanvasEvent.ts +++ b/src/editor/core/event/CanvasEvent.ts @@ -314,7 +314,7 @@ export class CanvasEvent { this.range.setRange(curIndex, curIndex) this.draw.render({ curIndex }) } else if (evt.key === KeyMap.Enter) { - if (isReadonly) return + if (isReadonly || isPartRangeInControlOutside) return // 表格需要上下文信息 const positionContext = this.position.getPositionContext() let restArg = {} @@ -326,12 +326,17 @@ export class CanvasEvent { value: ZERO, ...restArg } - if (isCollapsed) { - elementList.splice(index + 1, 0, enterText) + let curIndex: number + if (activeControl) { + curIndex = this.control.setValue([enterText]) } else { - elementList.splice(startIndex + 1, endIndex - startIndex, enterText) + if (isCollapsed) { + elementList.splice(index + 1, 0, enterText) + } else { + elementList.splice(startIndex + 1, endIndex - startIndex, enterText) + } + curIndex = index + 1 } - const curIndex = index + 1 this.range.setRange(curIndex, curIndex) this.draw.render({ curIndex }) } else if (evt.key === KeyMap.Left) { @@ -540,12 +545,20 @@ export class CanvasEvent { public cut() { const isReadonly = this.draw.isReadonly() if (isReadonly) return + const isPartRangeInControlOutside = this.control.isPartRangeInControlOutside() + if (isPartRangeInControlOutside) return + const activeControl = this.control.getActiveControl() const { startIndex, endIndex } = this.range.getRange() const elementList = this.draw.getElementList() if (startIndex !== endIndex) { writeTextByElementList(elementList.slice(startIndex + 1, endIndex + 1)) - elementList.splice(startIndex + 1, endIndex - startIndex) - const curIndex = startIndex + let curIndex: number + if (activeControl) { + curIndex = this.control.cut() + } else { + elementList.splice(startIndex + 1, endIndex - startIndex) + curIndex = startIndex + } this.range.setRange(curIndex, curIndex) this.draw.render({ curIndex }) } diff --git a/src/editor/dataset/constant/Element.ts b/src/editor/dataset/constant/Element.ts index 9c108fa..3edd78f 100644 --- a/src/editor/dataset/constant/Element.ts +++ b/src/editor/dataset/constant/Element.ts @@ -53,5 +53,6 @@ export const TEXTLIKE_ELEMENT_TYPE: ElementType[] = [ ElementType.TEXT, ElementType.HYPERLINK, ElementType.SUBSCRIPT, - ElementType.SUPERSCRIPT + ElementType.SUPERSCRIPT, + ElementType.CONTROL ] \ No newline at end of file diff --git a/src/editor/interface/Control.ts b/src/editor/interface/Control.ts index 3554ecf..b7ededd 100644 --- a/src/editor/interface/Control.ts +++ b/src/editor/interface/Control.ts @@ -48,4 +48,6 @@ export interface IControlInstance { setValue(data: IElement[]): number; keydown(evt: KeyboardEvent): number; + + cut(): number; } \ No newline at end of file