feat:add search navigate
This commit is contained in:
@@ -56,6 +56,8 @@ export class Command {
|
||||
private static addWatermark: CommandAdapt['addWatermark']
|
||||
private static deleteWatermark: CommandAdapt['deleteWatermark']
|
||||
private static search: CommandAdapt['search']
|
||||
private static searchNavigatePre: CommandAdapt['searchNavigatePre']
|
||||
private static searchNavigateNext: CommandAdapt['searchNavigateNext']
|
||||
private static replace: CommandAdapt['replace']
|
||||
private static print: CommandAdapt['print']
|
||||
private static replaceImageElement: CommandAdapt['replaceImageElement']
|
||||
@@ -122,6 +124,8 @@ export class Command {
|
||||
Command.addWatermark = adapt.addWatermark.bind(adapt)
|
||||
Command.deleteWatermark = adapt.deleteWatermark.bind(adapt)
|
||||
Command.search = adapt.search.bind(adapt)
|
||||
Command.searchNavigatePre = adapt.searchNavigatePre.bind(adapt)
|
||||
Command.searchNavigateNext = adapt.searchNavigateNext.bind(adapt)
|
||||
Command.replace = adapt.replace.bind(adapt)
|
||||
Command.print = adapt.print.bind(adapt)
|
||||
Command.replaceImageElement = adapt.replaceImageElement.bind(adapt)
|
||||
@@ -333,6 +337,14 @@ export class Command {
|
||||
return Command.search(payload)
|
||||
}
|
||||
|
||||
public executeSearchNavigatePre() {
|
||||
return Command.searchNavigatePre()
|
||||
}
|
||||
|
||||
public executeSearchNavigateNext() {
|
||||
return Command.searchNavigateNext()
|
||||
}
|
||||
|
||||
public executeReplace(payload: string) {
|
||||
return Command.replace(payload)
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import { formatElementList } from '../../utils/element'
|
||||
import { printImageBase64 } from '../../utils/print'
|
||||
import { Control } from '../draw/control/Control'
|
||||
import { Draw } from '../draw/Draw'
|
||||
import { Search } from '../draw/interactive/Search'
|
||||
import { TableTool } from '../draw/particle/table/TableTool'
|
||||
import { CanvasEvent } from '../event/CanvasEvent'
|
||||
import { HistoryManager } from '../history/HistoryManager'
|
||||
@@ -40,6 +41,7 @@ export class CommandAdapt {
|
||||
private options: Required<IEditorOption>
|
||||
private control: Control
|
||||
private workerManager: WorkerManager
|
||||
private searchManager: Search
|
||||
|
||||
constructor(draw: Draw) {
|
||||
this.draw = draw
|
||||
@@ -51,6 +53,7 @@ export class CommandAdapt {
|
||||
this.options = draw.getOptions()
|
||||
this.control = draw.getControl()
|
||||
this.workerManager = draw.getWorkerManager()
|
||||
this.searchManager = draw.getSearch()
|
||||
}
|
||||
|
||||
public mode(payload: EditorMode) {
|
||||
@@ -1211,7 +1214,25 @@ export class CommandAdapt {
|
||||
}
|
||||
|
||||
public search(payload: string | null) {
|
||||
this.draw.setSearchKeyword(payload)
|
||||
this.searchManager.setSearchKeyword(payload)
|
||||
this.draw.render({
|
||||
isSetCursor: false,
|
||||
isSubmitHistory: false
|
||||
})
|
||||
}
|
||||
|
||||
public searchNavigatePre() {
|
||||
const index = this.searchManager.searchNavigatePre()
|
||||
if (index === null) return
|
||||
this.draw.render({
|
||||
isSetCursor: false,
|
||||
isSubmitHistory: false
|
||||
})
|
||||
}
|
||||
|
||||
public searchNavigateNext() {
|
||||
const index = this.searchManager.searchNavigateNext()
|
||||
if (index === null) return
|
||||
this.draw.render({
|
||||
isSetCursor: false,
|
||||
isSubmitHistory: false
|
||||
|
||||
@@ -92,7 +92,6 @@ export class Draw {
|
||||
private rowList: IRow[]
|
||||
private painterStyle: IElementStyle | null
|
||||
private painterOptions: IPainterOptions | null
|
||||
private searchKeyword: string | null
|
||||
private visiblePageNoList: number[]
|
||||
private intersectionPageNo: number
|
||||
|
||||
@@ -155,7 +154,6 @@ export class Draw {
|
||||
this.rowList = []
|
||||
this.painterStyle = null
|
||||
this.painterOptions = null
|
||||
this.searchKeyword = null
|
||||
this.visiblePageNoList = []
|
||||
this.intersectionPageNo = 0
|
||||
|
||||
@@ -417,14 +415,6 @@ export class Draw {
|
||||
}
|
||||
}
|
||||
|
||||
public getSearchKeyword(): string | null {
|
||||
return this.searchKeyword
|
||||
}
|
||||
|
||||
public setSearchKeyword(payload: string | null) {
|
||||
this.searchKeyword = payload
|
||||
}
|
||||
|
||||
public setDefaultRange() {
|
||||
if (!this.elementList.length) return
|
||||
setTimeout(() => {
|
||||
@@ -995,7 +985,7 @@ export class Draw {
|
||||
// 绘制页码
|
||||
this.pageNumber.render(ctx, pageNo)
|
||||
// 搜索匹配绘制
|
||||
if (this.searchKeyword) {
|
||||
if (this.search.getSearchKeyword()) {
|
||||
this.search.render(ctx, pageNo)
|
||||
}
|
||||
// 绘制水印
|
||||
@@ -1017,8 +1007,9 @@ export class Draw {
|
||||
// 计算行信息
|
||||
if (isComputeRowList) {
|
||||
this.rowList = this._computeRowList(innerWidth, this.elementList)
|
||||
if (this.searchKeyword) {
|
||||
this.search.compute(this.searchKeyword)
|
||||
const searchKeyword = this.search.getSearchKeyword()
|
||||
if (searchKeyword) {
|
||||
this.search.compute(searchKeyword)
|
||||
}
|
||||
}
|
||||
// 清除光标等副作用
|
||||
|
||||
@@ -15,15 +15,56 @@ export class Search {
|
||||
private draw: Draw
|
||||
private options: Required<IEditorOption>
|
||||
private position: Position
|
||||
private searchKeyword: string | null
|
||||
private searchNavigateIndex: number | null
|
||||
|
||||
private searchMatchList: ISearchResult[]
|
||||
|
||||
constructor(draw: Draw) {
|
||||
this.draw = draw
|
||||
this.options = draw.getOptions()
|
||||
this.position = draw.getPosition()
|
||||
this.searchKeyword = null
|
||||
this.searchNavigateIndex = null
|
||||
this.searchMatchList = []
|
||||
}
|
||||
|
||||
public getSearchKeyword(): string | null {
|
||||
return this.searchKeyword
|
||||
}
|
||||
|
||||
public setSearchKeyword(payload: string | null) {
|
||||
this.searchKeyword = payload
|
||||
if (!payload) {
|
||||
this.searchNavigateIndex = null
|
||||
}
|
||||
}
|
||||
|
||||
public searchNavigatePre(): number | null {
|
||||
if (!this.searchMatchList.length) return null
|
||||
if (this.searchNavigateIndex === null) {
|
||||
this.searchNavigateIndex = 0
|
||||
} else if (this.searchNavigateIndex === 0) {
|
||||
this.searchNavigateIndex = this.searchMatchList.length - 1
|
||||
} else {
|
||||
this.searchNavigateIndex -= 1
|
||||
}
|
||||
return this.searchNavigateIndex
|
||||
}
|
||||
|
||||
public searchNavigateNext(): number | null {
|
||||
if (!this.searchMatchList.length) return null
|
||||
if (
|
||||
this.searchNavigateIndex === null
|
||||
|| this.searchNavigateIndex + 1 === this.searchMatchList.length
|
||||
) {
|
||||
this.searchNavigateIndex = 0
|
||||
} else {
|
||||
this.searchNavigateIndex += 1
|
||||
}
|
||||
return this.searchNavigateIndex
|
||||
}
|
||||
|
||||
public getSearchMatchList(): ISearchResult[] {
|
||||
return this.searchMatchList
|
||||
}
|
||||
@@ -122,12 +163,11 @@ export class Search {
|
||||
|
||||
public render(ctx: CanvasRenderingContext2D, pageIndex: number) {
|
||||
if (!this.searchMatchList || !this.searchMatchList.length) return
|
||||
const { searchMatchAlpha, searchMatchColor } = this.options
|
||||
const { searchMatchAlpha, searchMatchColor, searchNavigateMatchColor } = this.options
|
||||
const positionList = this.position.getOriginalPositionList()
|
||||
const elementList = this.draw.getOriginalElementList()
|
||||
ctx.save()
|
||||
ctx.globalAlpha = searchMatchAlpha
|
||||
ctx.fillStyle = searchMatchColor
|
||||
for (let s = 0; s < this.searchMatchList.length; s++) {
|
||||
const searchMatch = this.searchMatchList[s]
|
||||
let position: IElementPosition | null = null
|
||||
@@ -140,6 +180,12 @@ export class Search {
|
||||
if (!position) continue
|
||||
const { coordinate: { leftTop, leftBottom, rightTop }, pageNo } = position
|
||||
if (pageNo !== pageIndex) continue
|
||||
// 高亮当前搜索词
|
||||
if (s === this.searchNavigateIndex) {
|
||||
ctx.fillStyle = searchNavigateMatchColor
|
||||
} else {
|
||||
ctx.fillStyle = searchMatchColor
|
||||
}
|
||||
const x = leftTop[0]
|
||||
const y = leftTop[1]
|
||||
const width = rightTop[0] - leftTop[0]
|
||||
|
||||
Reference in New Issue
Block a user