feat: add range and position context api

This commit is contained in:
Hufe921
2023-11-27 21:46:11 +08:00
parent 90efe1020f
commit 8acce15e76
10 changed files with 190 additions and 12 deletions
+6
View File
@@ -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)
+60 -3
View File
@@ -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)
}
+7 -2
View File
@@ -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) {
+31
View File
@@ -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<string, IRange> = 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,
+2
View File
@@ -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
}
+1
View File
@@ -7,6 +7,7 @@ export interface ISearchResultBasic {
}
export interface ISearchResultRestArgs {
tableId?: string
tableIndex?: number
trIndex?: number
tdIndex?: number