From dd1b53ee6297bc72b0064c09522b597118919c50 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Tue, 6 Aug 2024 20:18:19 +0800 Subject: [PATCH] feat: control related apis support the control id property --- src/editor/core/command/CommandAdapt.ts | 8 +-- src/editor/core/draw/control/Control.ts | 64 +++++++++++++------ .../draw/control/checkbox/CheckboxControl.ts | 5 +- .../core/draw/control/date/DateControl.ts | 12 +++- .../draw/control/interactive/ControlSearch.ts | 9 ++- .../core/draw/control/radio/RadioControl.ts | 5 +- .../core/draw/control/select/SelectControl.ts | 7 +- .../core/draw/control/text/TextControl.ts | 10 ++- src/editor/interface/Control.ts | 15 +++-- 9 files changed, 95 insertions(+), 40 deletions(-) diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index a25c823f6..000baed54 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -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) { diff --git a/src/editor/core/draw/control/Control.ts b/src/editor/core/draw/control/Control.ts index 4420246dd..7c3d97faa 100644 --- a/src/editor/core/draw/control/Control.ts +++ b/src/editor/core/draw/control/Control.ts @@ -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 @@ -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) { @@ -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) { @@ -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 @@ -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! // 当前控件结束索引 @@ -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) { @@ -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 @@ -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 @@ -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, diff --git a/src/editor/core/draw/control/checkbox/CheckboxControl.ts b/src/editor/core/draw/control/checkbox/CheckboxControl.ts index 75850dd07..ac744be65 100644 --- a/src/editor/core/draw/control/checkbox/CheckboxControl.ts +++ b/src/editor/core/draw/control/checkbox/CheckboxControl.ts @@ -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 diff --git a/src/editor/core/draw/control/date/DateControl.ts b/src/editor/core/draw/control/date/DateControl.ts index 964ec0f55..6e13a9994 100644 --- a/src/editor/core/draw/control/date/DateControl.ts +++ b/src/editor/core/draw/control/date/DateControl.ts @@ -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() @@ -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) @@ -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() diff --git a/src/editor/core/draw/control/interactive/ControlSearch.ts b/src/editor/core/draw/control/interactive/ControlSearch.ts index f7090c862..5c50ec420 100644 --- a/src/editor/core/draw/control/interactive/ControlSearch.ts +++ b/src/editor/core/draw/control/interactive/ControlSearch.ts @@ -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 // 搜索后控件结束索引 diff --git a/src/editor/core/draw/control/radio/RadioControl.ts b/src/editor/core/draw/control/radio/RadioControl.ts index 9965379f9..0b066a13e 100644 --- a/src/editor/core/draw/control/radio/RadioControl.ts +++ b/src/editor/core/draw/control/radio/RadioControl.ts @@ -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 diff --git a/src/editor/core/draw/control/select/SelectControl.ts b/src/editor/core/draw/control/select/SelectControl.ts index d2bba27e2..a45de987d 100644 --- a/src/editor/core/draw/control/select/SelectControl.ts +++ b/src/editor/core/draw/control/select/SelectControl.ts @@ -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() @@ -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() diff --git a/src/editor/core/draw/control/text/TextControl.ts b/src/editor/core/draw/control/text/TextControl.ts index ef08b3e17..82c1fa6ef 100644 --- a/src/editor/core/draw/control/text/TextControl.ts +++ b/src/editor/core/draw/control/text/TextControl.ts @@ -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() @@ -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() diff --git a/src/editor/interface/Control.ts b/src/editor/interface/Control.ts index 6a18fd52e..018d4cd70 100644 --- a/src/editor/interface/Control.ts +++ b/src/editor/interface/Control.ts @@ -40,7 +40,8 @@ export interface IControlHighlightRule { export interface IControlHighlight { ruleList: IControlHighlightRule[] - conceptId: string + id?: string + conceptId?: string } export interface IControlRule { @@ -124,7 +125,8 @@ export interface IControlRuleOption { } export interface IGetControlValueOption { - conceptId: string + id?: string + conceptId?: string } export type IGetControlValueResult = (Omit & { @@ -134,19 +136,22 @@ export type IGetControlValueResult = (Omit & { })[] 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> }