Skip to content

Commit

Permalink
feat: add executeFocus api #796
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Aug 28, 2024
1 parent 3c598ba commit 3c17631
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/en/guide/command-execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -961,3 +961,13 @@ Usage:
```javascript
instance.command.executeInsertTitle(payload: IElement)
```

## executeFocus

Feature: focus

Usage:

```javascript
instance.command.executeFocus(payload?: IFocusOption)
```
10 changes: 10 additions & 0 deletions docs/guide/command-execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -961,3 +961,13 @@ instance.command.executeUpdateOptions(payload: IUpdateOption)
```javascript
instance.command.executeInsertTitle(payload: IElement)
```

## executeFocus

功能:光标聚焦

用法:

```javascript
instance.command.executeFocus(payload?: IFocusOption)
```
2 changes: 2 additions & 0 deletions src/editor/core/command/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class Command {
public executeInsertControl: CommandAdapt['insertControl']
public executeUpdateOptions: CommandAdapt['updateOptions']
public executeInsertTitle: CommandAdapt['insertTitle']
public executeFocus: CommandAdapt['focus']
public getCatalog: CommandAdapt['getCatalog']
public getImage: CommandAdapt['getImage']
public getOptions: CommandAdapt['getOptions']
Expand Down Expand Up @@ -214,6 +215,7 @@ export class Command {
this.executeSetZone = adapt.setZone.bind(adapt)
this.executeUpdateOptions = adapt.updateOptions.bind(adapt)
this.executeInsertTitle = adapt.insertTitle.bind(adapt)
this.executeFocus = adapt.focus.bind(adapt)
// 获取
this.getImage = adapt.getImage.bind(adapt)
this.getOptions = adapt.getOptions.bind(adapt)
Expand Down
21 changes: 21 additions & 0 deletions src/editor/core/command/CommandAdapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { ElementType } from '../../dataset/enum/Element'
import { ElementStyleKey } from '../../dataset/enum/ElementStyle'
import { ListStyle, ListType } from '../../dataset/enum/List'
import { MoveDirection } from '../../dataset/enum/Observer'
import { RowFlex } from '../../dataset/enum/Row'
import { TableBorder, TdBorder, TdSlash } from '../../dataset/enum/table/Table'
import { TitleLevel } from '../../dataset/enum/Title'
Expand Down Expand Up @@ -52,6 +53,7 @@ import {
IEditorOption,
IEditorResult,
IEditorText,
IFocusOption,
ISetValueOption,
IUpdateOption
} from '../../interface/Editor'
Expand Down Expand Up @@ -2726,4 +2728,23 @@ export class CommandAdapt {
// 插入标题
this.draw.insertElementList([cloneElement])
}

public focus(payload?: IFocusOption) {
const { position = LocationPosition.AFTER } = payload || {}
const curIndex =
position === LocationPosition.BEFORE
? 0
: this.draw.getOriginalMainElementList().length - 1
this.range.setRange(curIndex, curIndex)
this.draw.render({
curIndex,
isCompute: false,
isSubmitHistory: false
})
const positionList = this.draw.getPosition().getPositionList()
this.draw.getCursor().moveCursorToVisible({
cursorPosition: positionList[curIndex],
direction: MoveDirection.DOWN
})
}
}
6 changes: 5 additions & 1 deletion src/editor/interface/Editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IElement } from '..'
import { IElement, LocationPosition } from '..'
import {
EditorMode,
PageMode,
Expand Down Expand Up @@ -125,3 +125,7 @@ export type IUpdateOption = Omit<
export interface ISetValueOption {
isSetCursor?: boolean
}

export interface IFocusOption {
position?: LocationPosition
}

0 comments on commit 3c17631

Please sign in to comment.