feat:add search navigate

pr675
Hufe921 4 years ago
parent 2406204ce9
commit 8494f4698f

@ -194,6 +194,12 @@
<div class="menu-item__search__collapse" data-menu="search">
<div class="menu-item__search__collapse__search">
<input type="text" />
<div class="arrow-left">
<i></i>
</div>
<div class="arrow-right">
<i></i>
</div>
<span>×</span>
</div>
<div class="menu-item__search__collapse__replace">

@ -0,0 +1 @@
<svg width="4" height="7" xmlns="http://www.w3.org/2000/svg"><path fill="#6F6F6F" d="M0 3.5L4 0v7z" fill-rule="evenodd"/></svg>

After

Width:  |  Height:  |  Size: 127 B

@ -0,0 +1 @@
<svg width="4" height="7" xmlns="http://www.w3.org/2000/svg"><path fill="#6F6F6F" d="M4 3.5L0 0v7z" fill-rule="evenodd"/></svg>

After

Width:  |  Height:  |  Size: 127 B

@ -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]

@ -70,6 +70,7 @@ export default class Editor {
rangeMinWidth: 5,
searchMatchAlpha: 0.6,
searchMatchColor: '#FFFF00',
searchNavigateMatchColor: '#AAD280',
highlightAlpha: 0.6,
resizerColor: '#4182D9',
resizerSize: 5,

@ -27,6 +27,7 @@ export interface IEditorOption {
rangeAlpha?: number;
rangeMinWidth?: number;
searchMatchColor?: string;
searchNavigateMatchColor?: string;
searchMatchAlpha?: number;
highlightAlpha?: number;
resizerColor?: string;

@ -645,6 +645,12 @@ window.onload = function () {
instance.command.executeReplace(replaceValue)
}
}
searchCollapseDom.querySelector<HTMLDivElement>('.arrow-left')!.onclick = function () {
instance.command.executeSearchNavigatePre()
}
searchCollapseDom.querySelector<HTMLDivElement>('.arrow-right')!.onclick = function () {
instance.command.executeSearchNavigateNext()
}
document.querySelector<HTMLDivElement>('.menu-item__print')!.onclick = function () {
console.log('print')

@ -540,6 +540,52 @@ ul {
background: rgba(25, 55, 88, .04);
}
.menu-item .menu-item__search__collapse__search {
position: relative;
}
.menu-item .menu-item__search__collapse__search>input {
width: 181px;
padding: 5px 60px 5px 5px !important;
}
.menu-item .menu-item__search__collapse__search>div {
width: 28px;
height: 27px;
display: flex;
justify-content: center;
align-items: center;
position: absolute;
border-left: 1px solid #e2e6ed;
transition: all .5s;
}
.menu-item .menu-item__search__collapse__search>div:hover {
background-color: rgba(25, 55, 88, .04);
}
.menu-item .menu-item__search__collapse__search i {
width: 6px;
height: 8px;
transform: translateY(1px);
}
.menu-item .menu-item__search__collapse__search .arrow-left {
right: 76px;
}
.menu-item .menu-item__search__collapse__search .arrow-left i {
background: url(./assets/images/arrow-left.svg) no-repeat;
}
.menu-item .menu-item__search__collapse__search .arrow-right {
right: 48px;
}
.menu-item .menu-item__search__collapse__search .arrow-right i {
background: url(./assets/images/arrow-right.svg) no-repeat;
}
.menu-item__print i {
background-image: url('./assets/images/print.svg');
}

Loading…
Cancel
Save