Skip to content

Commit

Permalink
feat: control related apis support the control id property
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Aug 6, 2024
1 parent d0390cc commit dd1b53e
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 40 deletions.
8 changes: 4 additions & 4 deletions src/editor/core/command/CommandAdapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2435,19 +2435,19 @@ export class CommandAdapt {
public getControlValue(
payload: IGetControlValueOption
): IGetControlValueResult | null {
return this.draw.getControl().getValueByConceptId(payload)
return this.draw.getControl().getValueById(payload)
}

public setControlValue(payload: ISetControlValueOption) {
this.draw.getControl().setValueByConceptId(payload)
this.draw.getControl().setValueById(payload)
}

public setControlExtension(payload: ISetControlExtensionOption) {
this.draw.getControl().setExtensionByConceptId(payload)
this.draw.getControl().setExtensionById(payload)
}

public setControlProperties(payload: ISetControlProperties) {
this.draw.getControl().setPropertiesByConceptId(payload)
this.draw.getControl().setPropertiesById(payload)
}

public setControlHighlight(payload: ISetControlHighlightOption) {
Expand Down
64 changes: 45 additions & 19 deletions src/editor/core/draw/control/Control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ export class Control {
return prefixCount === postfixCount
}

public getIsDisabledControl(): boolean {
public getIsDisabledControl(context: IControlContext = {}): boolean {
if (!this.activeControl) return false
const { startIndex, endIndex } = this.range.getRange()
if (startIndex === endIndex) {
const elementList = this.getElementList()
const { startIndex, endIndex } = context.range || this.range.getRange()
if (startIndex === endIndex && ~startIndex && ~endIndex) {
const elementList = context.elementList || this.getElementList()
const startElement = elementList[startIndex]
if (startElement.controlComponent === ControlComponent.POSTFIX) {
return false
Expand Down Expand Up @@ -569,11 +569,10 @@ export class Control {
return this.activeControl.cut()
}

public getValueByConceptId(
payload: IGetControlValueOption
): IGetControlValueResult {
const { conceptId } = payload
public getValueById(payload: IGetControlValueOption): IGetControlValueResult {
const { id, conceptId } = payload
const result: IGetControlValueResult = []
if (!id && !conceptId) return result
const getValue = (elementList: IElement[], zone: EditorZone) => {
let i = 0
while (i < elementList.length) {
Expand All @@ -590,8 +589,14 @@ export class Control {
}
}
}
if (element?.control?.conceptId !== conceptId) continue
const { type, code, valueSets } = element.control!
if (
!element.control ||
(id && element.controlId !== id) ||
(conceptId && element.control.conceptId !== conceptId)
) {
continue
}
const { type, code, valueSets } = element.control
let j = i
let textControlValue = ''
while (j < elementList.length) {
Expand Down Expand Up @@ -655,9 +660,10 @@ export class Control {
return result
}

public setValueByConceptId(payload: ISetControlValueOption) {
public setValueById(payload: ISetControlValueOption) {
let isExistSet = false
const { conceptId, value } = payload
const { id, conceptId, value } = payload
if (!id && !conceptId) return
// 设置值
const setValue = (elementList: IElement[]) => {
let i = 0
Expand All @@ -675,7 +681,13 @@ export class Control {
}
}
}
if (element?.control?.conceptId !== conceptId) continue
if (
!element.control ||
(id && element.controlId !== id) ||
(conceptId && element.control.conceptId !== conceptId)
) {
continue
}
isExistSet = true
const { type } = element.control!
// 当前控件结束索引
Expand Down Expand Up @@ -767,8 +779,9 @@ export class Control {
}
}

public setExtensionByConceptId(payload: ISetControlExtensionOption) {
const { conceptId, extension } = payload
public setExtensionById(payload: ISetControlExtensionOption) {
const { id, conceptId, extension } = payload
if (!id && !conceptId) return
const setExtension = (elementList: IElement[]) => {
let i = 0
while (i < elementList.length) {
Expand All @@ -785,7 +798,13 @@ export class Control {
}
}
}
if (element?.control?.conceptId !== conceptId) continue
if (
!element.control ||
(id && element.controlId !== id) ||
(conceptId && element.control.conceptId !== conceptId)
) {
continue
}
element.control.extension = extension
// 修改后控件结束索引
let newEndIndex = i
Expand All @@ -807,8 +826,9 @@ export class Control {
}
}

public setPropertiesByConceptId(payload: ISetControlProperties) {
const { conceptId, properties } = payload
public setPropertiesById(payload: ISetControlProperties) {
const { id, conceptId, properties } = payload
if (!id && !conceptId) return
let isExistUpdate = false
function setProperties(elementList: IElement[]) {
let i = 0
Expand All @@ -825,7 +845,13 @@ export class Control {
}
}
}
if (element?.control?.conceptId !== conceptId) continue
if (
!element.control ||
(id && element.controlId !== id) ||
(conceptId && element.control.conceptId !== conceptId)
) {
continue
}
isExistUpdate = true
element.control = {
...element.control,
Expand Down
5 changes: 4 additions & 1 deletion src/editor/core/draw/control/checkbox/CheckboxControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ export class CheckboxControl implements IControlInstance {
options: IControlRuleOption = {}
) {
// 校验是否可以设置
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
if (
!options.isIgnoreDisabledRule &&
this.control.getIsDisabledControl(context)
) {
return
}
const { control } = this.element
Expand Down
12 changes: 9 additions & 3 deletions src/editor/core/draw/control/date/DateControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ export class DateControl implements IControlInstance {
options: IControlRuleOption = {}
): number {
// 校验是否可以设置
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
if (
!options.isIgnoreDisabledRule &&
this.control.getIsDisabledControl(context)
) {
return -1
}
const elementList = context.elementList || this.control.getElementList()
Expand Down Expand Up @@ -147,7 +150,7 @@ export class DateControl implements IControlInstance {
): number {
const { isIgnoreDisabledRule = false, isAddPlaceholder = true } = options
// 校验是否可以设置
if (!isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
if (!isIgnoreDisabledRule && this.control.getIsDisabledControl(context)) {
return -1
}
const range = this.getValueRange(context)
Expand All @@ -171,7 +174,10 @@ export class DateControl implements IControlInstance {
options: IControlRuleOption = {}
) {
// 校验是否可以设置
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
if (
!options.isIgnoreDisabledRule &&
this.control.getIsDisabledControl(context)
) {
return
}
const elementList = context.elementList || this.control.getElementList()
Expand Down
9 changes: 6 additions & 3 deletions src/editor/core/draw/control/interactive/ControlSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ export class ControlSearch {
}
}
}
const controlConceptId = element?.control?.conceptId
if (!controlConceptId) continue
const currentControl = element?.control
if (!currentControl) continue
const highlightIndex = this.highlightList.findIndex(
highlight => highlight.conceptId === controlConceptId
highlight =>
highlight.id === element.controlId ||
(currentControl.conceptId &&
currentControl.conceptId === highlight.conceptId)
)
if (!~highlightIndex) continue
// 搜索后控件结束索引
Expand Down
5 changes: 4 additions & 1 deletion src/editor/core/draw/control/radio/RadioControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export class RadioControl extends CheckboxControl {
options: IControlRuleOption = {}
) {
// 校验是否可以设置
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
if (
!options.isIgnoreDisabledRule &&
this.control.getIsDisabledControl(context)
) {
return
}
const { control } = this.element
Expand Down
7 changes: 5 additions & 2 deletions src/editor/core/draw/control/select/SelectControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class SelectControl implements IControlInstance {
): number {
const { isIgnoreDisabledRule = false, isAddPlaceholder = true } = options
// 校验是否可以设置
if (!isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
if (!isIgnoreDisabledRule && this.control.getIsDisabledControl(context)) {
return -1
}
const elementList = context.elementList || this.control.getElementList()
Expand Down Expand Up @@ -215,7 +215,10 @@ export class SelectControl implements IControlInstance {
options: IControlRuleOption = {}
) {
// 校验是否可以设置
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
if (
!options.isIgnoreDisabledRule &&
this.control.getIsDisabledControl(context)
) {
return
}
const elementList = context.elementList || this.control.getElementList()
Expand Down
10 changes: 8 additions & 2 deletions src/editor/core/draw/control/text/TextControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export class TextControl implements IControlInstance {
options: IControlRuleOption = {}
): number {
// 校验是否可以设置
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
if (
!options.isIgnoreDisabledRule &&
this.control.getIsDisabledControl(context)
) {
return -1
}
const elementList = context.elementList || this.control.getElementList()
Expand Down Expand Up @@ -122,7 +125,10 @@ export class TextControl implements IControlInstance {
options: IControlRuleOption = {}
): number {
// 校验是否可以设置
if (!options.isIgnoreDisabledRule && this.control.getIsDisabledControl()) {
if (
!options.isIgnoreDisabledRule &&
this.control.getIsDisabledControl(context)
) {
return -1
}
const elementList = context.elementList || this.control.getElementList()
Expand Down
15 changes: 10 additions & 5 deletions src/editor/interface/Control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export interface IControlHighlightRule {

export interface IControlHighlight {
ruleList: IControlHighlightRule[]
conceptId: string
id?: string
conceptId?: string
}

export interface IControlRule {
Expand Down Expand Up @@ -124,7 +125,8 @@ export interface IControlRuleOption {
}

export interface IGetControlValueOption {
conceptId: string
id?: string
conceptId?: string
}

export type IGetControlValueResult = (Omit<IControl, 'value'> & {
Expand All @@ -134,19 +136,22 @@ export type IGetControlValueResult = (Omit<IControl, 'value'> & {
})[]

export interface ISetControlValueOption {
conceptId: string
id?: string
conceptId?: string
value: string
}

export interface ISetControlExtensionOption {
conceptId: string
id?: string
conceptId?: string
extension: unknown
}

export type ISetControlHighlightOption = IControlHighlight[]

export type ISetControlProperties = {
conceptId: string
id?: string
conceptId?: string
properties: Partial<Omit<IControl, 'value'>>
}

Expand Down

0 comments on commit dd1b53e

Please sign in to comment.