feat: support for insert more elements into control #306

pr675
Hufe921 2 years ago
parent 7fb0150635
commit d2d649b011

@ -366,8 +366,6 @@ export class CommandAdapt {
public superscript() { public superscript() {
const isReadonly = this.draw.isReadonly() const isReadonly = this.draw.isReadonly()
if (isReadonly) return if (isReadonly) return
const activeControl = this.control.getActiveControl()
if (activeControl) return
const selection = this.range.getSelectionElementList() const selection = this.range.getSelectionElementList()
if (!selection) return if (!selection) return
const superscriptIndex = selection.findIndex( const superscriptIndex = selection.findIndex(
@ -397,8 +395,6 @@ export class CommandAdapt {
public subscript() { public subscript() {
const isReadonly = this.draw.isReadonly() const isReadonly = this.draw.isReadonly()
if (isReadonly) return if (isReadonly) return
const activeControl = this.control.getActiveControl()
if (activeControl) return
const selection = this.range.getSelectionElementList() const selection = this.range.getSelectionElementList()
if (!selection) return if (!selection) return
const subscriptIndex = selection.findIndex( const subscriptIndex = selection.findIndex(
@ -1947,7 +1943,7 @@ export class CommandAdapt {
if (startIndex !== endIndex) return if (startIndex !== endIndex) return
const elementList = this.draw.getElementList() const elementList = this.draw.getElementList()
const element = elementList[startIndex] const element = elementList[startIndex]
if (element.type !== ElementType.CONTROL) return if (!element.controlId) return
// 删除控件 // 删除控件
const control = this.draw.getControl() const control = this.draw.getControl()
const newIndex = control.removeControl(startIndex) const newIndex = control.removeControl(startIndex)

@ -1,5 +1,4 @@
import { INTERNAL_CONTEXT_MENU_KEY } from '../../../dataset/constant/ContextMenu' import { INTERNAL_CONTEXT_MENU_KEY } from '../../../dataset/constant/ContextMenu'
import { ElementType } from '../../../dataset/enum/Element'
import { IRegisterContextMenu } from '../../../interface/contextmenu/ContextMenu' import { IRegisterContextMenu } from '../../../interface/contextmenu/ContextMenu'
import { Command } from '../../command/Command' import { Command } from '../../command/Command'
const { const {
@ -14,7 +13,7 @@ export const controlMenus: IRegisterContextMenu[] = [
return ( return (
!payload.isReadonly && !payload.isReadonly &&
!payload.editorHasSelection && !payload.editorHasSelection &&
payload.startElement?.type === ElementType.CONTROL !!payload.startElement?.controlId
) )
}, },
callback: (command: Command) => { callback: (command: Command) => {

@ -68,7 +68,7 @@ export class Control {
} }
} }
} }
if (element.type !== ElementType.CONTROL || element.control?.minWidth) { if (!element.controlId || element.control?.minWidth) {
return true return true
} }
return ( return (
@ -87,8 +87,7 @@ export class Control {
const startElement = elementList[startIndex] const startElement = elementList[startIndex]
const endElement = elementList[endIndex] const endElement = elementList[endIndex]
if ( if (
(startElement?.type === ElementType.CONTROL || startElement.controlId &&
endElement?.type === ElementType.CONTROL) &&
startElement.controlId !== endElement.controlId startElement.controlId !== endElement.controlId
) { ) {
return true return true
@ -114,10 +113,9 @@ export class Control {
const startElement = elementList[startIndex] const startElement = elementList[startIndex]
const endElement = elementList[endIndex] const endElement = elementList[endIndex]
if ( if (
(startElement.type === ElementType.CONTROL || startElement.controlId &&
endElement.type === ElementType.CONTROL) && startElement.controlId === endElement.controlId &&
endElement.controlComponent !== ControlComponent.POSTFIX && endElement.controlComponent !== ControlComponent.POSTFIX
startElement.controlId === endElement.controlId
) { ) {
return true return true
} }

@ -1,4 +1,4 @@
import { EDITOR_ELEMENT_STYLE_ATTR } from '../../../../dataset/constant/Element' import { TEXTLIKE_ELEMENT_TYPE } from '../../../../dataset/constant/Element'
import { ControlComponent } from '../../../../dataset/enum/Control' import { ControlComponent } from '../../../../dataset/enum/Control'
import { KeyMap } from '../../../../dataset/enum/KeyMap' import { KeyMap } from '../../../../dataset/enum/KeyMap'
import { import {
@ -6,7 +6,7 @@ import {
IControlInstance IControlInstance
} from '../../../../interface/Control' } from '../../../../interface/Control'
import { IElement } from '../../../../interface/Element' import { IElement } from '../../../../interface/Element'
import { omitObject } from '../../../../utils' import { omitObject, pickObject } from '../../../../utils'
import { formatElementContext } from '../../../../utils/element' import { formatElementContext } from '../../../../utils/element'
import { Control } from '../Control' import { Control } from '../Control'
@ -75,12 +75,15 @@ export class TextControl implements IControlInstance {
// 移除空白占位符 // 移除空白占位符
this.control.removePlaceholder(startIndex, context) this.control.removePlaceholder(startIndex, context)
} }
// 插入 // 非文本类元素或前缀过渡掉样式属性
const startElement = elementList[startIndex] const startElement = elementList[startIndex]
const anchorElement = const anchorElement =
(startElement.type &&
!TEXTLIKE_ELEMENT_TYPE.includes(startElement.type)) ||
startElement.controlComponent === ControlComponent.PREFIX startElement.controlComponent === ControlComponent.PREFIX
? omitObject(startElement, EDITOR_ELEMENT_STYLE_ATTR) ? pickObject(startElement, ['control', 'controlId'])
: startElement : omitObject(startElement, ['type'])
// 插入起始位置
const start = range.startIndex + 1 const start = range.startIndex + 1
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
const newElement: IElement = { const newElement: IElement = {

@ -65,7 +65,7 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) {
let curIndex: number | null let curIndex: number | null
if (activeControl) { if (activeControl) {
curIndex = control.keydown(evt) curIndex = control.keydown(evt)
} else if (elementList[endIndex + 1]?.type === ElementType.CONTROL) { } else if (elementList[endIndex + 1]?.controlId) {
curIndex = control.removeControl(endIndex + 1) curIndex = control.removeControl(endIndex + 1)
} else { } else {
if (!isCollapsed) { if (!isCollapsed) {

@ -97,7 +97,7 @@ export function mousedown(evt: MouseEvent, host: CanvasEvent) {
// 预览工具组件 // 预览工具组件
const previewer = draw.getPreviewer() const previewer = draw.getPreviewer()
previewer.clearResizer() previewer.clearResizer()
if (isDirectHitImage && !isReadonly) { if (isDirectHitImage) {
previewer.drawResizer( previewer.drawResizer(
curElement, curElement,
positionList[curIndex], positionList[curIndex],

@ -50,17 +50,15 @@ export function mouseup(evt: MouseEvent, host: CanvasEvent) {
cacheRange.startIndex + 1, cacheRange.startIndex + 1,
cacheRange.endIndex + 1 cacheRange.endIndex + 1
) )
const isContainControl = dragElementList.find( const isContainControl = dragElementList.find(element => element.controlId)
element => element.type === ElementType.CONTROL
)
if (isContainControl) { if (isContainControl) {
// 仅允许 (最前/后元素不是控件 || 在控件前后 || 文本控件且是值) 拖拽 // 仅允许 (最前/后元素不是控件 || 在控件前后 || 文本控件且是值) 拖拽
const cacheStartElement = cacheElementList[cacheRange.startIndex + 1] const cacheStartElement = cacheElementList[cacheRange.startIndex + 1]
const cacheEndElement = cacheElementList[cacheRange.endIndex] const cacheEndElement = cacheElementList[cacheRange.endIndex]
const isAllowDragControl = const isAllowDragControl =
((cacheStartElement.type !== ElementType.CONTROL || ((!cacheStartElement.controlId ||
cacheStartElement.controlComponent === ControlComponent.PREFIX) && cacheStartElement.controlComponent === ControlComponent.PREFIX) &&
(cacheEndElement.type !== ElementType.CONTROL || (!cacheEndElement.controlId ||
cacheEndElement.controlComponent === ControlComponent.POSTFIX)) || cacheEndElement.controlComponent === ControlComponent.POSTFIX)) ||
(cacheStartElement.controlId === cacheEndElement.controlId && (cacheStartElement.controlId === cacheEndElement.controlId &&
cacheStartElement.controlComponent === ControlComponent.PREFIX && cacheStartElement.controlComponent === ControlComponent.PREFIX &&
@ -152,7 +150,7 @@ export function mouseup(evt: MouseEvent, host: CanvasEvent) {
) )
const cacheEndElement = cacheElementList[cacheRangeEndIndex] const cacheEndElement = cacheElementList[cacheRangeEndIndex]
if ( if (
cacheEndElement.type === ElementType.CONTROL && cacheEndElement.controlId &&
cacheEndElement.controlComponent !== ControlComponent.POSTFIX cacheEndElement.controlComponent !== ControlComponent.POSTFIX
) { ) {
rangeManager.replaceRange({ rangeManager.replaceRange({

@ -315,7 +315,7 @@ export class Position {
tdValueElement.type === ElementType.CHECKBOX || tdValueElement.type === ElementType.CHECKBOX ||
tdValueElement.controlComponent === tdValueElement.controlComponent ===
ControlComponent.CHECKBOX, ControlComponent.CHECKBOX,
isControl: tdValueElement.type === ElementType.CONTROL, isControl: !!tdValueElement.controlId,
isImage: tablePosition.isImage, isImage: tablePosition.isImage,
isDirectHit: tablePosition.isDirectHit, isDirectHit: tablePosition.isDirectHit,
isTable: true, isTable: true,
@ -366,7 +366,7 @@ export class Position {
return { return {
hitLineStartIndex, hitLineStartIndex,
index: curPositionIndex, index: curPositionIndex,
isControl: element.type === ElementType.CONTROL isControl: !!element.controlId
} }
} }
} }
@ -471,7 +471,7 @@ export class Position {
return { return {
hitLineStartIndex, hitLineStartIndex,
index: curPositionIndex, index: curPositionIndex,
isControl: elementList[curPositionIndex]?.type === ElementType.CONTROL isControl: !!elementList[curPositionIndex]?.controlId
} }
} }

@ -275,7 +275,7 @@ export class RangeManager {
if (~startIndex && ~endIndex) { if (~startIndex && ~endIndex) {
const elementList = this.draw.getElementList() const elementList = this.draw.getElementList()
const element = elementList[startIndex] const element = elementList[startIndex]
if (element?.type === ElementType.CONTROL) { if (element?.controlId) {
control.initControl() control.initControl()
return return
} }

@ -44,7 +44,7 @@ function pickText(elementList: IElement[]): string {
e++ e++
} }
text += pickText(valueList) text += pickText(valueList)
} else if (element.type === ElementType.CONTROL) { } else if (element.controlId) {
const controlId = element.controlId const controlId = element.controlId
const valueList: IElement[] = [] const valueList: IElement[] = []
while (e < elementList.length) { while (e < elementList.length) {
@ -54,7 +54,7 @@ function pickText(elementList: IElement[]): string {
break break
} }
if (controlE.controlComponent === ControlComponent.VALUE) { if (controlE.controlComponent === ControlComponent.VALUE) {
delete controlE.type delete controlE.controlId
valueList.push(controlE) valueList.push(controlE)
} }
e++ e++

@ -538,7 +538,7 @@ export function zipElementList(payload: IElement[]): IElement[] {
} }
dateElement.valueList = zipElementList(valueList) dateElement.valueList = zipElementList(valueList)
element = dateElement element = dateElement
} else if (element.type === ElementType.CONTROL) { } else if (element.controlId) {
// 控件处理 // 控件处理
const controlId = element.controlId const controlId = element.controlId
const control = element.control! const control = element.control!
@ -555,8 +555,8 @@ export function zipElementList(payload: IElement[]): IElement[] {
break break
} }
if (controlE.controlComponent === ControlComponent.VALUE) { if (controlE.controlComponent === ControlComponent.VALUE) {
delete controlE.type
delete controlE.control delete controlE.control
delete controlE.controlId
valueList.push(controlE) valueList.push(controlE)
} }
e++ e++
@ -837,8 +837,14 @@ export function createDomFromElementList(
TEXTLIKE_ELEMENT_TYPE.includes(element.type) TEXTLIKE_ELEMENT_TYPE.includes(element.type)
) { ) {
let text = '' let text = ''
if (element.type === ElementType.CONTROL) { if (element.controlId) {
text = element.control!.value?.[0]?.value || '' text =
element
.control!.value?.filter(
v => !v.type || TEXTLIKE_ELEMENT_TYPE.includes(v.type)
)
.map(v => v.value)
.join('') || ''
} else if (element.type === ElementType.DATE) { } else if (element.type === ElementType.DATE) {
text = element.valueList?.map(v => v.value).join('') || '' text = element.valueList?.map(v => v.value).join('') || ''
} else { } else {

@ -1481,16 +1481,10 @@ window.onload = function () {
instance.listener.controlChange = function (payload) { instance.listener.controlChange = function (payload) {
const disableMenusInControlContext = [ const disableMenusInControlContext = [
'superscript',
'subscript',
'table', 'table',
'image',
'hyperlink', 'hyperlink',
'separator', 'separator',
'codeblock', 'page-break'
'page-break',
'control',
'checkbox'
] ]
// 菜单操作权限 // 菜单操作权限
disableMenusInControlContext.forEach(menu => { disableMenusInControlContext.forEach(menu => {

Loading…
Cancel
Save