feat: add bounding rect to getRangeContext api

pr675
Hufe921 3 years ago
parent 1ce2e2f44f
commit 319da3fca1

@ -40,7 +40,7 @@ import {
} from '../../interface/Editor'
import { IElement, IElementStyle } from '../../interface/Element'
import { IMargin } from '../../interface/Margin'
import { RangeContext } from '../../interface/Range'
import { RangeContext, RangeRect } from '../../interface/Range'
import { IColgroup } from '../../interface/table/Colgroup'
import { ITd } from '../../interface/table/Td'
import { ITr } from '../../interface/table/Tr'
@ -1808,12 +1808,52 @@ export class CommandAdapt {
const positionList = this.position.getPositionList()
const startPageNo = positionList[startIndex].pageNo
const endPageNo = positionList[endIndex].pageNo
// 坐标信息(相对编辑器书写区)
const rangeRects: RangeRect[] = []
const selectionPositionList = this.position.getSelectionPositionList()
if (selectionPositionList) {
const height = this.draw.getOriginalHeight()
const pageGap = this.draw.getOriginalPageGap()
// 起始信息及x坐标
let currentRowNo: number | null = null
let currentX = 0
let rangeRect: RangeRect | null = null
for (let p = 0; p < selectionPositionList.length; p++) {
const {
rowNo,
pageNo,
coordinate: { leftTop, rightTop },
lineHeight
} = selectionPositionList[p]
// 起始行变化追加选区信息
if (currentRowNo === null || currentRowNo !== rowNo) {
if (rangeRect) {
rangeRects.push(rangeRect)
}
rangeRect = {
x: leftTop[0],
y: leftTop[1] + pageNo * (height + pageGap),
width: rightTop[0] - leftTop[0],
height: lineHeight
}
currentRowNo = rowNo
currentX = leftTop[0]
} else {
rangeRect!.width = rightTop[0] - currentX
// 最后一个元素结束追加选区信息
if (p === selectionPositionList.length - 1 && rangeRect) {
rangeRects.push(rangeRect)
}
}
}
}
return deepClone({
isCollapsed,
startElement,
endElement,
startPageNo,
endPageNo
endPageNo,
rangeRects
})
}

@ -352,6 +352,10 @@ export class Draw {
return this.options.pageGap * this.options.scale
}
public getOriginalPageGap(): number {
return this.options.pageGap
}
public getPageNumberBottom(): number {
const {
pageNumber: { bottom },

@ -74,6 +74,13 @@ export class Position {
return this.positionList
}
public getSelectionPositionList(): IElementPosition[] | null {
const { startIndex, endIndex } = this.draw.getRange().getRange()
if (startIndex === endIndex) return null
const positionList = this.getPositionList()
return positionList.slice(startIndex + 1, endIndex + 1)
}
public setPositionList(payload: IElementPosition[]) {
this.positionList = payload
}

@ -1,5 +1,5 @@
import { EditorZone } from '../dataset/enum/Editor'
import { IElement } from './Element'
import { IElement, IElementFillRect } from './Element'
export interface IRange {
startIndex: number
@ -17,10 +17,13 @@ export type RangeRowArray = Map<number, number[]>
export type RangeRowMap = Map<number, Set<number>>
export type RangeRect = IElementFillRect
export type RangeContext = {
isCollapsed: boolean
startElement: IElement
endElement: IElement
startPageNo: number
endPageNo: number
rangeRects: RangeRect[]
}

Loading…
Cancel
Save