diff --git a/docs/en/guide/command-execute.md b/docs/en/guide/command-execute.md index c04f5f0..964d216 100644 --- a/docs/en/guide/command-execute.md +++ b/docs/en/guide/command-execute.md @@ -71,12 +71,40 @@ instance.command.executeBackspace() ## executeSetRange -Feature: Set selection +Feature: Set range Usage: ```javascript -instance.command.executeSetRange(startIndex: number , endIndex: number) +instance.command.executeSetRange( + startIndex: number, + endIndex: number, + tableId?: string, + startTdIndex?: number, + endTdIndex?: number, + startTrIndex?: number, + endTrIndex?: number +) +``` + +## executeReplaceRange + +Feature: Replace range + +Usage: + +```javascript +instance.command.executeReplaceRange(range: IRange) +``` + +## executeSetPositionContext + +Feature: Set position context + +Usage: + +```javascript +instance.command.executeSetPositionContext(range: IRange) ``` ## executeForceUpdate diff --git a/docs/en/guide/command-get.md b/docs/en/guide/command-get.md index 40ae035..6960bf1 100644 --- a/docs/en/guide/command-get.md +++ b/docs/en/guide/command-get.md @@ -59,7 +59,7 @@ const wordCount = await instance.command.getWordCount() ## getRangeText -Feature: Get selection text +Feature: Get range text Usage: @@ -69,7 +69,7 @@ const rangeText = instance.command.getRangeText() ## getRangeContext -Feature: Get selection context +Feature: Get range context Usage: @@ -79,7 +79,7 @@ const rangeContext = instance.command.getRangeContext() ## getRangeRow -Feature: Get selection row element list +Feature: Get range row element list Usage: @@ -87,9 +87,19 @@ Usage: const rowElementList = instance.command.getRangeRow() ``` +## getKeywordRangeList + +Feature: Get range list by keyword + +Usage: + +```javascript +const rangeList = instance.command.getKeywordRangeList() +``` + ## getRangeParagraph -Feature: Get selection paragraph element list +Feature: Get range paragraph element list Usage: diff --git a/docs/guide/command-execute.md b/docs/guide/command-execute.md index 1205da0..5676d05 100644 --- a/docs/guide/command-execute.md +++ b/docs/guide/command-execute.md @@ -76,7 +76,35 @@ instance.command.executeBackspace() 用法: ```javascript -instance.command.executeSetRange(startIndex: number , endIndex: number) +instance.command.executeSetRange( + startIndex: number, + endIndex: number, + tableId?: string, + startTdIndex?: number, + endTdIndex?: number, + startTrIndex?: number, + endTrIndex?: number +) +``` + +## executeReplaceRange + +功能:替换选区 + +用法: + +```javascript +instance.command.executeReplaceRange(range: IRange) +``` + +## executeSetPositionContext + +功能:设置位置上下文 + +用法: + +```javascript +instance.command.executeSetPositionContext(range: IRange) ``` ## executeForceUpdate diff --git a/docs/guide/command-get.md b/docs/guide/command-get.md index a6c6a03..c126c09 100644 --- a/docs/guide/command-get.md +++ b/docs/guide/command-get.md @@ -87,6 +87,16 @@ const rangeContext = instance.command.getRangeContext() const rowElementList = instance.command.getRangeRow() ``` +## getKeywordRangeList + +功能:获取关键词所在选区列表 + +用法: + +```javascript +const rangeList = instance.command.getKeywordRangeList() +``` + ## getRangeParagraph 功能:获取选区所在段落元素列表 diff --git a/src/editor/core/command/Command.ts b/src/editor/core/command/Command.ts index 792e035..7b7e950 100644 --- a/src/editor/core/command/Command.ts +++ b/src/editor/core/command/Command.ts @@ -9,6 +9,8 @@ export class Command { public executeSelectAll: CommandAdapt['selectAll'] public executeBackspace: CommandAdapt['backspace'] public executeSetRange: CommandAdapt['setRange'] + public executeReplaceRange: CommandAdapt['replaceRange'] + public executeSetPositionContext: CommandAdapt['setPositionContext'] public executeForceUpdate: CommandAdapt['forceUpdate'] public executeBlur: CommandAdapt['blur'] public executeUndo: CommandAdapt['undo'] @@ -96,6 +98,7 @@ export class Command { public getRangeContext: CommandAdapt['getRangeContext'] public getRangeRow: CommandAdapt['getRangeRow'] public getRangeParagraph: CommandAdapt['getRangeParagraph'] + public getKeywordRangeList: CommandAdapt['getKeywordRangeList'] public getPaperMargin: CommandAdapt['getPaperMargin'] public getSearchNavigateInfo: CommandAdapt['getSearchNavigateInfo'] public getLocale: CommandAdapt['getLocale'] @@ -112,6 +115,8 @@ export class Command { this.executeSelectAll = adapt.selectAll.bind(adapt) this.executeBackspace = adapt.backspace.bind(adapt) this.executeSetRange = adapt.setRange.bind(adapt) + this.executeReplaceRange = adapt.replaceRange.bind(adapt) + this.executeSetPositionContext = adapt.setPositionContext.bind(adapt) this.executeForceUpdate = adapt.forceUpdate.bind(adapt) this.executeBlur = adapt.blur.bind(adapt) // 撤销、重做、格式刷、清除格式 @@ -204,6 +209,7 @@ export class Command { this.getRangeContext = adapt.getRangeContext.bind(adapt) this.getRangeRow = adapt.getRangeRow.bind(adapt) this.getRangeParagraph = adapt.getRangeParagraph.bind(adapt) + this.getKeywordRangeList = adapt.getKeywordRangeList.bind(adapt) this.getCatalog = adapt.getCatalog.bind(adapt) this.getPaperMargin = adapt.getPaperMargin.bind(adapt) this.getSearchNavigateInfo = adapt.getSearchNavigateInfo.bind(adapt) diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index 46fcc1c..7169661 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -42,7 +42,7 @@ import { import { IElement, IElementStyle } from '../../interface/Element' import { IPasteOption } from '../../interface/Event' import { IMargin } from '../../interface/Margin' -import { RangeContext, RangeRect } from '../../interface/Range' +import { IRange, RangeContext, RangeRect } from '../../interface/Range' import { IColgroup } from '../../interface/table/Colgroup' import { ITd } from '../../interface/table/Td' import { ITr } from '../../interface/table/Tr' @@ -150,9 +150,25 @@ export class CommandAdapt { this.draw.render({ curIndex }) } - public setRange(startIndex: number, endIndex: number) { + public setRange( + startIndex: number, + endIndex: number, + tableId?: string, + startTdIndex?: number, + endTdIndex?: number, + startTrIndex?: number, + endTrIndex?: number + ) { if (startIndex < 0 || endIndex < 0 || endIndex < startIndex) return - this.range.setRange(startIndex, endIndex) + this.range.setRange( + startIndex, + endIndex, + tableId, + startTdIndex, + endTdIndex, + startTrIndex, + endTrIndex + ) const isCollapsed = startIndex === endIndex this.draw.render({ curIndex: isCollapsed ? startIndex : undefined, @@ -162,6 +178,43 @@ export class CommandAdapt { }) } + public replaceRange(range: IRange) { + this.setRange( + range.startIndex, + range.endIndex, + range.tableId, + range.startTdIndex, + range.endTdIndex, + range.startTrIndex, + range.endTrIndex + ) + } + + public setPositionContext(range: IRange) { + const { tableId, startTrIndex, startTdIndex } = range + const elementList = this.draw.getOriginalElementList() + if (tableId) { + const tableElementIndex = elementList.findIndex(el => el.id === tableId) + if (!~tableElementIndex) return + const tableElement = elementList[tableElementIndex] + const tr = tableElement.trList![startTrIndex!] + const td = tr.tdList[startTdIndex!] + this.position.setPositionContext({ + isTable: true, + index: tableElementIndex, + trIndex: startTrIndex, + tdIndex: startTdIndex, + tdId: td.id, + trId: tr.id, + tableId + }) + } else { + this.position.setPositionContext({ + isTable: false + }) + } + } + public forceUpdate(options?: IForceUpdateOption) { const { isSubmitHistory = false } = options || {} this.range.clearRange() @@ -1890,6 +1943,10 @@ export class CommandAdapt { return paragraphElementList ? zipElementList(paragraphElementList) : null } + public getKeywordRangeList(payload: string): IRange[] { + return this.range.getKeywordRangeList(payload) + } + public pageMode(payload: PageMode) { this.draw.setPageMode(payload) } diff --git a/src/editor/core/draw/interactive/Search.ts b/src/editor/core/draw/interactive/Search.ts index 1c15c52..fff7976 100644 --- a/src/editor/core/draw/interactive/Search.ts +++ b/src/editor/core/draw/interactive/Search.ts @@ -153,7 +153,7 @@ export class Search { } } - public compute(payload: string) { + public getMatchList(payload: string): ISearchResult[] { const keyword = payload.toLocaleLowerCase() const searchMatchList: ISearchResult[] = [] // 分组 @@ -245,6 +245,7 @@ export class Search { for (let d = 0; d < tr.tdList.length; d++) { const td = tr.tdList[d] const restArgs: ISearchResultRestArgs = { + tableId: tableElement.id, tableIndex: group.index, trIndex: t, tdIndex: d, @@ -259,7 +260,11 @@ export class Search { }) } } - this.searchMatchList = searchMatchList + return searchMatchList + } + + public compute(payload: string) { + this.searchMatchList = this.getMatchList(payload) } public render(ctx: CanvasRenderingContext2D, pageIndex: number) { diff --git a/src/editor/core/range/RangeManager.ts b/src/editor/core/range/RangeManager.ts index 5ade74c..d32b97a 100644 --- a/src/editor/core/range/RangeManager.ts +++ b/src/editor/core/range/RangeManager.ts @@ -2,6 +2,7 @@ import { ElementType } from '../..' import { ZERO } from '../../dataset/constant/Common' import { TEXTLIKE_ELEMENT_TYPE } from '../../dataset/constant/Element' import { ControlComponent } from '../../dataset/enum/Control' +import { EditorContext } from '../../dataset/enum/Editor' import { IEditorOption } from '../../interface/Editor' import { IElement } from '../../interface/Element' import { EventBusMap } from '../../interface/EventBus' @@ -247,6 +248,36 @@ export class RangeManager { return false } + public getKeywordRangeList(payload: string): IRange[] { + const searchMatchList = this.draw.getSearch().getMatchList(payload) + const searchRangeMap: Map = new Map() + for (const searchMatch of searchMatchList) { + const searchRange = searchRangeMap.get(searchMatch.groupId) + if (searchRange) { + searchRange.endIndex += 1 + } else { + const { type, groupId, tableId, index, tdIndex, trIndex } = searchMatch + const range: IRange = { + startIndex: index - 1, + endIndex: index + } + if (type === EditorContext.TABLE) { + range.tableId = tableId + range.startTdIndex = tdIndex + range.endTdIndex = tdIndex + range.startTrIndex = trIndex + range.endTrIndex = trIndex + } + searchRangeMap.set(groupId, range) + } + } + const rangeList: IRange[] = [] + searchRangeMap.forEach(searchRange => { + rangeList.push(searchRange) + }) + return rangeList + } + public setRange( startIndex: number, endIndex: number, diff --git a/src/editor/index.ts b/src/editor/index.ts index 47bdd82..12909b0 100644 --- a/src/editor/index.ts +++ b/src/editor/index.ts @@ -67,6 +67,7 @@ import { defaultPageBreakOption } from './dataset/constant/PageBreak' import { IPageBreak } from './interface/PageBreak' import { LETTER_CLASS } from './dataset/constant/Common' import { INTERNAL_CONTEXT_MENU_KEY } from './dataset/constant/ContextMenu' +import { IRange } from './interface/Range' export default class Editor { public command: Command @@ -291,5 +292,6 @@ export type { ILang, ICatalog, ICatalogItem, + IRange, IRangeStyle } diff --git a/src/editor/interface/Search.ts b/src/editor/interface/Search.ts index 72ea175..b995c8a 100644 --- a/src/editor/interface/Search.ts +++ b/src/editor/interface/Search.ts @@ -7,6 +7,7 @@ export interface ISearchResultBasic { } export interface ISearchResultRestArgs { + tableId?: string tableIndex?: number trIndex?: number tdIndex?: number