Skip to content

Commit 8b75715

Browse files
committed
feat: add range option to executeFocus api #1090
1 parent 02cf3f3 commit 8b75715

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/editor/core/command/CommandAdapt.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,29 +2440,47 @@ export class CommandAdapt {
24402440
}
24412441

24422442
public focus(payload?: IFocusOption) {
2443-
const { position = LocationPosition.AFTER, isMoveCursorToVisible = true } =
2444-
payload || {}
2445-
let curIndex = 0
2446-
const rowNo = payload?.rowNo
2447-
if (isNumber(rowNo)) {
2443+
const {
2444+
position = LocationPosition.AFTER,
2445+
isMoveCursorToVisible = true,
2446+
rowNo,
2447+
range
2448+
} = payload || {}
2449+
let curIndex = -1
2450+
if (range) {
2451+
// 根据选区定位
2452+
this.range.replaceRange(range)
2453+
curIndex =
2454+
position === LocationPosition.BEFORE ? range.startIndex : range.endIndex
2455+
} else if (isNumber(rowNo)) {
2456+
// 根据行号定位
24482457
const rowList = this.draw.getOriginalRowList()
24492458
curIndex =
24502459
position === LocationPosition.BEFORE
24512460
? rowList[rowNo]?.startIndex
24522461
: rowList[rowNo + 1]?.startIndex - 1
2462+
if (!isNumber(curIndex)) return
2463+
this.range.setRange(curIndex, curIndex)
24532464
} else {
2465+
// 默认文档首尾
24542466
curIndex =
24552467
position === LocationPosition.BEFORE
24562468
? 0
24572469
: this.draw.getOriginalMainElementList().length - 1
2470+
this.range.setRange(curIndex, curIndex)
24582471
}
2459-
if (!isNumber(curIndex)) return
2460-
this.range.setRange(curIndex, curIndex)
2461-
this.draw.render({
2462-
curIndex,
2472+
// 光标存在且闭合时定位
2473+
const renderParams: IDrawOption = {
24632474
isCompute: false,
2475+
isSetCursor: false,
24642476
isSubmitHistory: false
2465-
})
2477+
}
2478+
if (~curIndex && this.range.getIsCollapsed()) {
2479+
renderParams.curIndex = curIndex
2480+
renderParams.isSetCursor = true
2481+
}
2482+
this.draw.render(renderParams)
2483+
// 移动滚动条到可见区域
24662484
if (isMoveCursorToVisible) {
24672485
const positionList = this.draw.getPosition().getPositionList()
24682486
this.draw.getCursor().moveCursorToVisible({

src/editor/interface/Editor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { IPageBorderOption } from './PageBorder'
2828
import { IBadgeOption } from './Badge'
2929
import { IElement } from './Element'
3030
import { LocationPosition } from '../dataset/enum/Common'
31+
import { IRange } from './Range'
3132

3233
export interface IEditorData {
3334
header?: IElement[]
@@ -132,8 +133,9 @@ export interface ISetValueOption {
132133
}
133134

134135
export interface IFocusOption {
135-
position?: LocationPosition
136136
rowNo?: number
137+
range?: IRange
138+
position?: LocationPosition
137139
isMoveCursorToVisible?: boolean
138140
}
139141

0 commit comments

Comments
 (0)