Skip to content

Commit

Permalink
feat: add option to executeReplace api #969
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Jan 18, 2025
1 parent 21a71c0 commit 3fbaa3b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/en/guide/command-execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ Feature: Search for replacement
Usage:

```javascript
instance.command.executeReplace(newWord: string)
instance.command.executeReplace(newWord: string, option?: IReplaceOption)
```

## executePrint
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/command-execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ instance.command.executeSearchNavigateNext()
用法:

```javascript
instance.command.executeReplace(newWord: string)
instance.command.executeReplace(newWord: string, option?: IReplaceOption)
```

## executePrint
Expand Down
27 changes: 23 additions & 4 deletions src/editor/core/command/CommandAdapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ import {
import { IMargin } from '../../interface/Margin'
import { ILocationPosition } from '../../interface/Position'
import { IRange, RangeContext, RangeRect } from '../../interface/Range'
import { ISearchResultContext } from '../../interface/Search'
import {
IReplaceOption,
ISearchResult,
ISearchResultContext
} from '../../interface/Search'
import { ITextDecoration } from '../../interface/Text'
import {
IGetTitleValueOption,
Expand All @@ -86,6 +90,7 @@ import {
deepClone,
downloadFile,
getUUID,
isNumber,
isObjectEqual
} from '../../utils'
import {
Expand Down Expand Up @@ -1285,12 +1290,26 @@ export class CommandAdapt {
return this.searchManager.getSearchNavigateInfo()
}

public replace(payload: string) {
public replace(payload: string, option?: IReplaceOption) {
const isReadonly = this.draw.isReadonly()
if (isReadonly) return
if (!payload || new RegExp(`${ZERO}`, 'g').test(payload)) return
const matchList = this.draw.getSearch().getSearchMatchList()
if (!matchList.length) return
let matchList = this.draw.getSearch().getSearchMatchList()
// 替换搜索项
const replaceIndex = option?.index
if (isNumber(replaceIndex)) {
const matchGroup: ISearchResult[][] = []
matchList.forEach(match => {
const last = matchGroup[matchGroup.length - 1]
if (!last || last[0].groupId !== match.groupId) {
matchGroup.push([match])
} else {
last.push(match)
}
})
matchList = matchGroup[replaceIndex]
}
if (!matchList?.length) return
// 匹配index变化的差值
let pageDiffCount = 0
let tableDiffCount = 0
Expand Down
4 changes: 4 additions & 0 deletions src/editor/interface/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ export interface ISearchResultContext {
startPosition: IElementPosition
endPosition: IElementPosition
}

export interface IReplaceOption {
index?: number
}
4 changes: 4 additions & 0 deletions src/editor/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ export function isArray(type: unknown): type is Array<unknown> {
return Array.isArray(type)
}

export function isNumber(type: unknown): type is Array<unknown> {
return Object.prototype.toString.call(type) === '[object Number]'
}

export function mergeObject<T>(source: T, target: T): T {
if (isObject(source) && isObject(target)) {
const objectTarget = <Record<string, unknown>>target
Expand Down

0 comments on commit 3fbaa3b

Please sign in to comment.