feat: add executeLocationControl api #592

pr675
Hufe921 2 years ago
parent bf322dfea3
commit 53701fc46c

@ -912,6 +912,16 @@ Usage:
instance.command.executeSetControlHighlight(payload: ISetControlHighlightOption) instance.command.executeSetControlHighlight(payload: ISetControlHighlightOption)
``` ```
## executeLocationControl
Feature: Positioning and activating control
Usage:
```javascript
instance.command.executeLocationControl(controlId: string)
```
## executeUpdateOptions ## executeUpdateOptions
Feature: Update options Feature: Update options

@ -912,6 +912,16 @@ instance.command.executeSetControlProperties(payload: ISetControlProperties)
instance.command.executeSetControlHighlight(payload: ISetControlHighlightOption) instance.command.executeSetControlHighlight(payload: ISetControlHighlightOption)
``` ```
## executeLocationControl
功能:定位并激活控件
用法:
```javascript
instance.command.executeLocationControl(controlId: string)
```
## executeUpdateOptions ## executeUpdateOptions
功能:修改配置 功能:修改配置

@ -90,6 +90,7 @@ export class Command {
public executeSetControlExtension: CommandAdapt['setControlExtension'] public executeSetControlExtension: CommandAdapt['setControlExtension']
public executeSetControlProperties: CommandAdapt['setControlProperties'] public executeSetControlProperties: CommandAdapt['setControlProperties']
public executeSetControlHighlight: CommandAdapt['setControlHighlight'] public executeSetControlHighlight: CommandAdapt['setControlHighlight']
public executeLocationControl: CommandAdapt['locationControl']
public executeUpdateOptions: CommandAdapt['updateOptions'] public executeUpdateOptions: CommandAdapt['updateOptions']
public executeInsertTitle: CommandAdapt['insertTitle'] public executeInsertTitle: CommandAdapt['insertTitle']
public getCatalog: CommandAdapt['getCatalog'] public getCatalog: CommandAdapt['getCatalog']
@ -236,5 +237,6 @@ export class Command {
this.executeSetControlHighlight = adapt.setControlHighlight.bind(adapt) this.executeSetControlHighlight = adapt.setControlHighlight.bind(adapt)
this.getControlValue = adapt.getControlValue.bind(adapt) this.getControlValue = adapt.getControlValue.bind(adapt)
this.getControlList = adapt.getControlList.bind(adapt) this.getControlList = adapt.getControlList.bind(adapt)
this.executeLocationControl = adapt.locationControl.bind(adapt)
} }
} }

@ -56,6 +56,7 @@ import {
import { IElement, IElementStyle } from '../../interface/Element' import { IElement, IElementStyle } from '../../interface/Element'
import { IPasteOption } from '../../interface/Event' import { IPasteOption } from '../../interface/Event'
import { IMargin } from '../../interface/Margin' import { IMargin } from '../../interface/Margin'
import { ILocationPosition } from '../../interface/Position'
import { IRange, RangeContext, RangeRect } from '../../interface/Range' import { IRange, RangeContext, RangeRect } from '../../interface/Range'
import { IColgroup } from '../../interface/table/Colgroup' import { IColgroup } from '../../interface/table/Colgroup'
import { ITd } from '../../interface/table/Td' import { ITd } from '../../interface/table/Td'
@ -2446,6 +2447,85 @@ export class CommandAdapt {
return this.draw.getControl().getList() return this.draw.getControl().getList()
} }
public locationControl(controlId: string) {
function location(
elementList: IElement[],
zone: EditorZone
): ILocationPosition | null {
let i = 0
while (i < elementList.length) {
const element = elementList[i]
i++
if (element.type === ElementType.TABLE) {
const trList = element.trList!
for (let r = 0; r < trList.length; r++) {
const tr = trList[r]
for (let d = 0; d < tr.tdList.length; d++) {
const td = tr.tdList[d]
const locationContext = location(td.value, zone)
if (locationContext) {
return {
...locationContext,
positionContext: {
isTable: true,
index: i - 1,
trIndex: r,
tdIndex: d,
tdId: element.tdId,
trId: element.trId,
tableId: element.tableId
}
}
}
}
}
}
if (element?.controlId !== controlId) continue
const curIndex = i - 1
return {
zone,
range: {
startIndex: curIndex,
endIndex: curIndex
},
positionContext: {
isTable: false
}
}
}
return null
}
const data = [
{
zone: EditorZone.HEADER,
elementList: this.draw.getHeaderElementList()
},
{
zone: EditorZone.MAIN,
elementList: this.draw.getOriginalMainElementList()
},
{
zone: EditorZone.FOOTER,
elementList: this.draw.getFooterElementList()
}
]
for (const context of data) {
const locationContext = location(context.elementList, context.zone)
if (locationContext) {
// 设置区域、上下文、光标信息
this.setZone(locationContext.zone)
this.position.setPositionContext(locationContext.positionContext)
this.range.replaceRange(locationContext.range)
this.draw.render({
curIndex: locationContext.range.startIndex,
isCompute: false,
isSubmitHistory: false
})
break
}
}
}
public getContainer(): HTMLDivElement { public getContainer(): HTMLDivElement {
return this.draw.getContainer() return this.draw.getContainer()
} }

@ -849,7 +849,9 @@ export class Control {
for (const elementList of data) { for (const elementList of data) {
getControlElementList(elementList) getControlElementList(elementList)
} }
return zipElementList(controlElementList) return zipElementList(controlElementList, {
extraPickAttrs: ['controlId']
})
} }
public recordBorderInfo(x: number, y: number, width: number, height: number) { public recordBorderInfo(x: number, y: number, width: number, height: number) {

@ -1,4 +1,4 @@
import { IElement, ImageDisplay } from '..' import { IElement, ImageDisplay, IRange } from '..'
import { EditorZone } from '../dataset/enum/Editor' import { EditorZone } from '../dataset/enum/Editor'
import { IElementPosition } from './Element' import { IElementPosition } from './Element'
import { IRow } from './Row' import { IRow } from './Row'
@ -93,3 +93,9 @@ export interface IFloatPosition {
tdValueIndex?: number tdValueIndex?: number
zone?: EditorZone zone?: EditorZone
} }
export interface ILocationPosition {
zone: EditorZone
range: IRange
positionContext: IPositionContext
}

@ -487,12 +487,22 @@ export function isSameElementExceptValue(
} }
return true return true
} }
interface IPickElementOption {
export function pickElementAttr(payload: IElement): IElement { extraPickAttrs?: Array<keyof IElement>
}
export function pickElementAttr(
payload: IElement,
option: IPickElementOption = {}
): IElement {
const { extraPickAttrs } = option
const zipAttrs = EDITOR_ELEMENT_ZIP_ATTR
if (extraPickAttrs) {
zipAttrs.push(...extraPickAttrs)
}
const element: IElement = { const element: IElement = {
value: payload.value === ZERO ? `\n` : payload.value value: payload.value === ZERO ? `\n` : payload.value
} }
EDITOR_ELEMENT_ZIP_ATTR.forEach(attr => { zipAttrs.forEach(attr => {
const value = payload[attr] as never const value = payload[attr] as never
if (value !== undefined) { if (value !== undefined) {
element[attr] = value element[attr] = value
@ -501,7 +511,14 @@ export function pickElementAttr(payload: IElement): IElement {
return element return element
} }
export function zipElementList(payload: IElement[]): IElement[] { interface IZipElementListOption {
extraPickAttrs?: Array<keyof IElement>
}
export function zipElementList(
payload: IElement[],
options: IZipElementListOption = {}
): IElement[] {
const { extraPickAttrs } = options
const elementList = deepClone(payload) const elementList = deepClone(payload)
const zipElementListData: IElement[] = [] const zipElementListData: IElement[] = []
let e = 0 let e = 0
@ -541,7 +558,7 @@ export function zipElementList(payload: IElement[]): IElement[] {
valueList.push(titleE) valueList.push(titleE)
e++ e++
} }
titleElement.valueList = zipElementList(valueList) titleElement.valueList = zipElementList(valueList, options)
element = titleElement element = titleElement
} }
} else if (element.listId && element.listType) { } else if (element.listId && element.listType) {
@ -569,7 +586,7 @@ export function zipElementList(payload: IElement[]): IElement[] {
valueList.push(listE) valueList.push(listE)
e++ e++
} }
listElement.valueList = zipElementList(valueList) listElement.valueList = zipElementList(valueList, options)
element = listElement element = listElement
} }
} else if (element.type === ElementType.TABLE) { } else if (element.type === ElementType.TABLE) {
@ -599,7 +616,7 @@ export function zipElementList(payload: IElement[]): IElement[] {
const zipTd: ITd = { const zipTd: ITd = {
colspan: td.colspan, colspan: td.colspan,
rowspan: td.rowspan, rowspan: td.rowspan,
value: zipElementList(td.value) value: zipElementList(td.value, options)
} }
// 压缩单元格属性 // 压缩单元格属性
TABLE_TD_ZIP_ATTR.forEach(attr => { TABLE_TD_ZIP_ATTR.forEach(attr => {
@ -633,7 +650,7 @@ export function zipElementList(payload: IElement[]): IElement[] {
valueList.push(hyperlinkE) valueList.push(hyperlinkE)
e++ e++
} }
hyperlinkElement.valueList = zipElementList(valueList) hyperlinkElement.valueList = zipElementList(valueList, options)
element = hyperlinkElement element = hyperlinkElement
} }
} else if (element.type === ElementType.DATE) { } else if (element.type === ElementType.DATE) {
@ -656,7 +673,7 @@ export function zipElementList(payload: IElement[]): IElement[] {
valueList.push(dateE) valueList.push(dateE)
e++ e++
} }
dateElement.valueList = zipElementList(valueList) dateElement.valueList = zipElementList(valueList, options)
element = dateElement element = dateElement
} }
} else if (element.controlId) { } else if (element.controlId) {
@ -675,7 +692,8 @@ export function zipElementList(payload: IElement[]): IElement[] {
...pickObject(element, EDITOR_ROW_ATTR), ...pickObject(element, EDITOR_ROW_ATTR),
type: ElementType.CONTROL, type: ElementType.CONTROL,
value: '', value: '',
control control,
controlId
} }
const valueList: IElement[] = [] const valueList: IElement[] = []
while (e < elementList.length) { while (e < elementList.length) {
@ -691,12 +709,12 @@ export function zipElementList(payload: IElement[]): IElement[] {
} }
e++ e++
} }
controlElement.control!.value = zipElementList(valueList) controlElement.control!.value = zipElementList(valueList, options)
element = controlElement element = pickElementAttr(controlElement, { extraPickAttrs })
} }
} }
// 组合元素 // 组合元素
const pickElement = pickElementAttr(element) const pickElement = pickElementAttr(element, { extraPickAttrs })
if ( if (
!element.type || !element.type ||
element.type === ElementType.TEXT || element.type === ElementType.TEXT ||
@ -708,7 +726,10 @@ export function zipElementList(payload: IElement[]): IElement[] {
e++ e++
if ( if (
nextElement && nextElement &&
isSameElementExceptValue(pickElement, pickElementAttr(nextElement)) isSameElementExceptValue(
pickElement,
pickElementAttr(nextElement, { extraPickAttrs })
)
) { ) {
const nextValue = const nextValue =
nextElement.value === ZERO ? '\n' : nextElement.value nextElement.value === ZERO ? '\n' : nextElement.value

Loading…
Cancel
Save