feat:add select control
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { ControlComponent, ControlType } from '../../../dataset/enum/Control'
|
||||
import { ElementType } from '../../../dataset/enum/Element'
|
||||
import { IControlInitOption, IControlInstance, IControlOption } from '../../../interface/Control'
|
||||
import { IElement } from '../../../interface/Element'
|
||||
import { IElement, IElementPosition } from '../../../interface/Element'
|
||||
import { RangeManager } from '../../range/RangeManager'
|
||||
import { Draw } from '../Draw'
|
||||
import { SelectControl } from './select/SelectControl'
|
||||
import { TextControl } from './text/TextControl'
|
||||
|
||||
interface IMoveCursorResult {
|
||||
@@ -40,14 +41,34 @@ export class Control {
|
||||
return false
|
||||
}
|
||||
|
||||
public getContainer(): HTMLDivElement {
|
||||
return this.draw.getContainer()
|
||||
}
|
||||
|
||||
public getElementList(): IElement[] {
|
||||
return this.draw.getElementList()
|
||||
}
|
||||
|
||||
public getPosition(): IElementPosition | null {
|
||||
const positionList = this.draw.getPosition().getPositionList()
|
||||
const { endIndex } = this.range.getRange()
|
||||
return positionList[endIndex] || null
|
||||
}
|
||||
|
||||
public getPreY(): number {
|
||||
const height = this.draw.getHeight()
|
||||
const pageGap = this.draw.getPageGap()
|
||||
return this.draw.getPageNo() * (height + pageGap)
|
||||
}
|
||||
|
||||
public getRange() {
|
||||
return this.range.getRange()
|
||||
}
|
||||
|
||||
public shrinkBoundary() {
|
||||
this.range.shrinkBoundary()
|
||||
}
|
||||
|
||||
public getActiveControl(): IControlInstance | null {
|
||||
return this.activeControl
|
||||
}
|
||||
@@ -58,23 +79,42 @@ export class Control {
|
||||
const element = elementList[range.startIndex]
|
||||
// 判断控件是否已经激活
|
||||
if (this.activeControl) {
|
||||
// 列举控件唤醒下拉弹窗
|
||||
if (this.activeControl instanceof SelectControl) {
|
||||
this.activeControl.awake()
|
||||
}
|
||||
const controlElement = this.activeControl.getElement()
|
||||
if (element.controlId === controlElement.controlId) return
|
||||
}
|
||||
// 销毁旧激活控件
|
||||
this.destroyControl()
|
||||
// 激活控件
|
||||
if (element.control!.type === ControlType.TEXT) {
|
||||
const control = element.control!
|
||||
if (control.type === ControlType.TEXT) {
|
||||
this.activeControl = new TextControl(element, this)
|
||||
} else if (control.type === ControlType.SELECT) {
|
||||
const selectControl = new SelectControl(element, this)
|
||||
this.activeControl = selectControl
|
||||
selectControl.awake()
|
||||
}
|
||||
}
|
||||
|
||||
public destroyControl() {
|
||||
if (this.activeControl) {
|
||||
if (this.activeControl instanceof SelectControl) {
|
||||
this.activeControl.destroy()
|
||||
}
|
||||
this.activeControl = null
|
||||
}
|
||||
}
|
||||
|
||||
public repaintControl(curIndex: number) {
|
||||
this.range.setRange(curIndex, curIndex)
|
||||
this.draw.render({
|
||||
curIndex
|
||||
})
|
||||
}
|
||||
|
||||
public moveCursor(position: IControlInitOption): IMoveCursorResult {
|
||||
const { index, trIndex, tdIndex, tdValueIndex } = position
|
||||
let elementList = this.draw.getOriginalElementList()
|
||||
@@ -169,97 +209,12 @@ export class Control {
|
||||
}
|
||||
nextIndex++
|
||||
}
|
||||
if (!~leftIndex || !~rightIndex) return -1
|
||||
if (!~leftIndex || !~rightIndex) return startIndex
|
||||
// 删除元素
|
||||
elementList.splice(leftIndex + 1, rightIndex - leftIndex)
|
||||
return leftIndex
|
||||
}
|
||||
|
||||
public shrinkBoundary() {
|
||||
const elementList = this.getElementList()
|
||||
const range = this.getRange()
|
||||
const { startIndex, endIndex } = range
|
||||
const startElement = elementList[startIndex]
|
||||
const endElement = elementList[endIndex]
|
||||
if (startIndex === endIndex) {
|
||||
if (startElement.controlComponent === ControlComponent.PLACEHOLDER) {
|
||||
// 找到第一个placeholder字符
|
||||
let index = startIndex - 1
|
||||
while (index > 0) {
|
||||
const preElement = elementList[index]
|
||||
if (
|
||||
preElement.controlId !== startElement.controlId ||
|
||||
preElement.controlComponent === ControlComponent.PREFIX
|
||||
) {
|
||||
console.log(index)
|
||||
range.startIndex = index
|
||||
range.endIndex = index
|
||||
break
|
||||
}
|
||||
index--
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 首、尾为占位符时,收缩到最后一个前缀字符后
|
||||
if (
|
||||
startElement.controlComponent === ControlComponent.PLACEHOLDER ||
|
||||
endElement.controlComponent === ControlComponent.PLACEHOLDER
|
||||
) {
|
||||
let index = endIndex - 1
|
||||
while (index > 0) {
|
||||
const preElement = elementList[index]
|
||||
if (
|
||||
preElement.controlId !== endElement.controlId
|
||||
|| preElement.controlComponent === ControlComponent.PREFIX
|
||||
) {
|
||||
range.startIndex = index
|
||||
range.endIndex = index
|
||||
return
|
||||
}
|
||||
index--
|
||||
}
|
||||
}
|
||||
// 向右查找到第一个Value
|
||||
if (startElement.controlComponent === ControlComponent.PREFIX) {
|
||||
let index = startIndex + 1
|
||||
while (index < elementList.length) {
|
||||
const nextElement = elementList[index]
|
||||
if (
|
||||
nextElement.controlId !== startElement.controlId
|
||||
|| nextElement.controlComponent === ControlComponent.VALUE
|
||||
) {
|
||||
range.startIndex = index - 1
|
||||
break
|
||||
} else if (nextElement.controlComponent === ControlComponent.PLACEHOLDER) {
|
||||
range.startIndex = index - 1
|
||||
range.endIndex = index - 1
|
||||
return
|
||||
}
|
||||
index++
|
||||
}
|
||||
}
|
||||
// 向左查找到第一个Value
|
||||
if (endElement.controlComponent !== ControlComponent.VALUE) {
|
||||
let index = startIndex - 1
|
||||
while (index > 0) {
|
||||
const preElement = elementList[index]
|
||||
if (
|
||||
preElement.controlId !== startElement.controlId
|
||||
|| preElement.controlComponent === ControlComponent.VALUE
|
||||
) {
|
||||
range.startIndex = index
|
||||
break
|
||||
} else if (preElement.controlComponent === ControlComponent.PLACEHOLDER) {
|
||||
range.startIndex = index
|
||||
range.endIndex = index
|
||||
return
|
||||
}
|
||||
index--
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public removePlaceholder(startIndex: number) {
|
||||
const elementList = this.getElementList()
|
||||
const startElement = elementList[startIndex]
|
||||
|
||||
@@ -1,3 +1,254 @@
|
||||
export class SelectControl {
|
||||
import { EDITOR_COMPONENT } from '../../../../dataset/constant/Editor'
|
||||
import { ControlComponent } from '../../../../dataset/enum/Control'
|
||||
import { EditorComponent } from '../../../../dataset/enum/Editor'
|
||||
import { KeyMap } from '../../../../dataset/enum/Keymap'
|
||||
import { IControlInstance } from '../../../../interface/Control'
|
||||
import { IElement } from '../../../../interface/Element'
|
||||
import { Control } from '../Control'
|
||||
|
||||
export class SelectControl implements IControlInstance {
|
||||
|
||||
private element: IElement
|
||||
private control: Control
|
||||
private isPopup: boolean
|
||||
private selectDom: HTMLDivElement | null
|
||||
|
||||
constructor(element: IElement, control: Control) {
|
||||
this.element = element
|
||||
this.control = control
|
||||
this.isPopup = false
|
||||
this.selectDom = null
|
||||
}
|
||||
|
||||
public getElement(): IElement {
|
||||
return this.element
|
||||
}
|
||||
|
||||
public getCode(): string | null {
|
||||
return this.element.control?.code || null
|
||||
}
|
||||
|
||||
public getValue(): IElement[] {
|
||||
const elementList = this.control.getElementList()
|
||||
const { startIndex } = this.control.getRange()
|
||||
const startElement = elementList[startIndex]
|
||||
const data: IElement[] = []
|
||||
// 向左查找
|
||||
let preIndex = startIndex
|
||||
while (preIndex > 0) {
|
||||
const preElement = elementList[preIndex]
|
||||
if (
|
||||
preElement.controlId !== startElement.controlId ||
|
||||
preElement.controlComponent === ControlComponent.PREFIX
|
||||
) {
|
||||
break
|
||||
}
|
||||
if (preElement.controlComponent === ControlComponent.VALUE) {
|
||||
data.unshift(preElement)
|
||||
}
|
||||
preIndex--
|
||||
}
|
||||
// 向右查找
|
||||
let nextIndex = startIndex + 1
|
||||
while (nextIndex < elementList.length) {
|
||||
const nextElement = elementList[nextIndex]
|
||||
if (
|
||||
nextElement.controlId !== startElement.controlId ||
|
||||
nextElement.controlComponent === ControlComponent.POSTFIX
|
||||
) {
|
||||
break
|
||||
}
|
||||
if (nextElement.controlComponent === ControlComponent.VALUE) {
|
||||
data.push(nextElement)
|
||||
}
|
||||
nextIndex++
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
public setValue(): number {
|
||||
const range = this.control.getRange()
|
||||
return range.endIndex
|
||||
}
|
||||
|
||||
public keydown(evt: KeyboardEvent): number {
|
||||
const elementList = this.control.getElementList()
|
||||
const range = this.control.getRange()
|
||||
// 收缩边界到Value内
|
||||
this.control.shrinkBoundary()
|
||||
const { startIndex, endIndex } = range
|
||||
const startElement = elementList[startIndex]
|
||||
const endElement = elementList[endIndex]
|
||||
// backspace
|
||||
if (evt.key === KeyMap.Backspace) {
|
||||
// 清空选项
|
||||
if (startIndex !== endIndex) {
|
||||
return this.clearSelect()
|
||||
} else {
|
||||
if (
|
||||
startElement.controlComponent === ControlComponent.PREFIX ||
|
||||
endElement.controlComponent === ControlComponent.POSTFIX ||
|
||||
startElement.controlComponent === ControlComponent.PLACEHOLDER
|
||||
) {
|
||||
// 前缀、后缀、占位符
|
||||
return this.control.removeControl(startIndex)
|
||||
} else {
|
||||
// 清空选项
|
||||
return this.clearSelect()
|
||||
}
|
||||
}
|
||||
} else if (evt.key === KeyMap.Delete) {
|
||||
// 移除选区元素
|
||||
if (startIndex !== endIndex) {
|
||||
// 清空选项
|
||||
return this.clearSelect()
|
||||
} else {
|
||||
const endNextElement = elementList[endIndex + 1]
|
||||
if (startElement.controlComponent === ControlComponent.PREFIX ||
|
||||
endNextElement.controlComponent === ControlComponent.POSTFIX ||
|
||||
startElement.controlComponent === ControlComponent.PLACEHOLDER
|
||||
) {
|
||||
// 前缀、后缀、占位符
|
||||
return this.control.removeControl(startIndex)
|
||||
} else {
|
||||
// 清空选项
|
||||
return this.clearSelect()
|
||||
}
|
||||
}
|
||||
}
|
||||
return endIndex
|
||||
}
|
||||
|
||||
public cut(): number {
|
||||
this.control.shrinkBoundary()
|
||||
const { startIndex, endIndex } = this.control.getRange()
|
||||
if (startIndex === endIndex) {
|
||||
return startIndex
|
||||
}
|
||||
// 清空选项
|
||||
return this.clearSelect()
|
||||
}
|
||||
|
||||
public clearSelect(): number {
|
||||
const elementList = this.control.getElementList()
|
||||
const { startIndex } = this.control.getRange()
|
||||
const startElement = elementList[startIndex]
|
||||
let leftIndex = -1
|
||||
let rightIndex = -1
|
||||
// 向左查找
|
||||
let preIndex = startIndex
|
||||
while (preIndex > 0) {
|
||||
const preElement = elementList[preIndex]
|
||||
if (
|
||||
preElement.controlId !== startElement.controlId ||
|
||||
preElement.controlComponent === ControlComponent.PREFIX
|
||||
) {
|
||||
leftIndex = preIndex
|
||||
break
|
||||
}
|
||||
preIndex--
|
||||
}
|
||||
// 向右查找
|
||||
let nextIndex = startIndex + 1
|
||||
while (nextIndex < elementList.length) {
|
||||
const nextElement = elementList[nextIndex]
|
||||
if (
|
||||
nextElement.controlId !== startElement.controlId ||
|
||||
nextElement.controlComponent === ControlComponent.POSTFIX
|
||||
) {
|
||||
rightIndex = nextIndex - 1
|
||||
break
|
||||
}
|
||||
nextIndex++
|
||||
}
|
||||
if (!~leftIndex || !~rightIndex) return -1
|
||||
// 删除元素
|
||||
elementList.splice(leftIndex + 1, rightIndex - leftIndex)
|
||||
// 增加占位符
|
||||
this.control.addPlaceholder(preIndex)
|
||||
this.element.control!.code = null
|
||||
return preIndex
|
||||
}
|
||||
|
||||
public setSelect(code: string) {
|
||||
const control = this.element.control!
|
||||
const valueSets = control.valueSets
|
||||
if (!Array.isArray(valueSets) || !valueSets.length) return
|
||||
// 转换code
|
||||
const valueSet = valueSets.find(v => v.code === code)
|
||||
if (!valueSet) return
|
||||
// 清空选项
|
||||
const startIndex = this.clearSelect()
|
||||
this.control.removePlaceholder(startIndex)
|
||||
// 插入
|
||||
const elementList = this.control.getElementList()
|
||||
const startElement = elementList[startIndex]
|
||||
const start = startIndex + 1
|
||||
const data = valueSet.value.split('')
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
elementList.splice(start + i, 0, {
|
||||
...startElement,
|
||||
value: data[i],
|
||||
controlComponent: ControlComponent.VALUE
|
||||
})
|
||||
}
|
||||
// render
|
||||
const newIndex = start + data.length - 1
|
||||
this.control.repaintControl(newIndex)
|
||||
// 设置状态
|
||||
this.element.control!.code = code
|
||||
this.destroy()
|
||||
}
|
||||
|
||||
private _createSelectPopupDom() {
|
||||
const control = this.element.control!
|
||||
const valueSets = control.valueSets
|
||||
if (!Array.isArray(valueSets) || !valueSets.length) return
|
||||
const position = this.control.getPosition()
|
||||
if (!position) return
|
||||
// dom树:<div><ul><li>item</li></ul></div>
|
||||
const selectPopupContainer = document.createElement('div')
|
||||
selectPopupContainer.classList.add('select-control-popup')
|
||||
selectPopupContainer.setAttribute(EDITOR_COMPONENT, EditorComponent.POPUP)
|
||||
const ul = document.createElement('ul')
|
||||
for (let v = 0; v < valueSets.length; v++) {
|
||||
const valueSet = valueSets[v]
|
||||
const li = document.createElement('li')
|
||||
const code = this.getCode()
|
||||
if (code === valueSet.code) {
|
||||
li.classList.add('active')
|
||||
}
|
||||
li.onclick = () => {
|
||||
this.setSelect(valueSet.code)
|
||||
}
|
||||
li.append(document.createTextNode(valueSet.value))
|
||||
ul.append(li)
|
||||
}
|
||||
selectPopupContainer.append(ul)
|
||||
// 定位
|
||||
const { coordinate: { leftTop: [left, top] }, lineHeight } = position
|
||||
const preY = this.control.getPreY()
|
||||
selectPopupContainer.style.left = `${left}px`
|
||||
selectPopupContainer.style.top = `${top + preY + lineHeight}px`
|
||||
// 追加至container
|
||||
const container = this.control.getContainer()
|
||||
container.append(selectPopupContainer)
|
||||
this.selectDom = selectPopupContainer
|
||||
}
|
||||
|
||||
public awake() {
|
||||
if (this.isPopup) return
|
||||
const { startIndex } = this.control.getRange()
|
||||
const elementList = this.control.getElementList()
|
||||
if (elementList[startIndex + 1]?.controlId !== this.element.controlId) return
|
||||
this._createSelectPopupDom()
|
||||
this.isPopup = true
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
if (!this.isPopup) return
|
||||
this.selectDom?.remove()
|
||||
this.isPopup = false
|
||||
}
|
||||
|
||||
}
|
||||
@@ -146,7 +146,7 @@ export class TextControl implements IControlInstance {
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1
|
||||
return endIndex
|
||||
}
|
||||
|
||||
public cut(): number {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ElementType } from '../..'
|
||||
import { ControlComponent } from '../../dataset/enum/Control'
|
||||
import { IEditorOption } from '../../interface/Editor'
|
||||
import { IElement } from '../../interface/Element'
|
||||
import { IRange } from '../../interface/Range'
|
||||
@@ -137,6 +138,90 @@ export class RangeManager {
|
||||
})
|
||||
}
|
||||
|
||||
public shrinkBoundary() {
|
||||
const elementList = this.draw.getElementList()
|
||||
const range = this.getRange()
|
||||
const { startIndex, endIndex } = range
|
||||
const startElement = elementList[startIndex]
|
||||
const endElement = elementList[endIndex]
|
||||
if (startIndex === endIndex) {
|
||||
if (startElement.controlComponent === ControlComponent.PLACEHOLDER) {
|
||||
// 找到第一个placeholder字符
|
||||
let index = startIndex - 1
|
||||
while (index > 0) {
|
||||
const preElement = elementList[index]
|
||||
if (
|
||||
preElement.controlId !== startElement.controlId ||
|
||||
preElement.controlComponent === ControlComponent.PREFIX
|
||||
) {
|
||||
range.startIndex = index
|
||||
range.endIndex = index
|
||||
break
|
||||
}
|
||||
index--
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 首、尾为占位符时,收缩到最后一个前缀字符后
|
||||
if (
|
||||
startElement.controlComponent === ControlComponent.PLACEHOLDER ||
|
||||
endElement.controlComponent === ControlComponent.PLACEHOLDER
|
||||
) {
|
||||
let index = endIndex - 1
|
||||
while (index > 0) {
|
||||
const preElement = elementList[index]
|
||||
if (
|
||||
preElement.controlId !== endElement.controlId
|
||||
|| preElement.controlComponent === ControlComponent.PREFIX
|
||||
) {
|
||||
range.startIndex = index
|
||||
range.endIndex = index
|
||||
return
|
||||
}
|
||||
index--
|
||||
}
|
||||
}
|
||||
// 向右查找到第一个Value
|
||||
if (startElement.controlComponent === ControlComponent.PREFIX) {
|
||||
let index = startIndex + 1
|
||||
while (index < elementList.length) {
|
||||
const nextElement = elementList[index]
|
||||
if (
|
||||
nextElement.controlId !== startElement.controlId
|
||||
|| nextElement.controlComponent === ControlComponent.VALUE
|
||||
) {
|
||||
range.startIndex = index - 1
|
||||
break
|
||||
} else if (nextElement.controlComponent === ControlComponent.PLACEHOLDER) {
|
||||
range.startIndex = index - 1
|
||||
range.endIndex = index - 1
|
||||
return
|
||||
}
|
||||
index++
|
||||
}
|
||||
}
|
||||
// 向左查找到第一个Value
|
||||
if (endElement.controlComponent !== ControlComponent.VALUE) {
|
||||
let index = startIndex - 1
|
||||
while (index > 0) {
|
||||
const preElement = elementList[index]
|
||||
if (
|
||||
preElement.controlId !== startElement.controlId
|
||||
|| preElement.controlComponent === ControlComponent.VALUE
|
||||
) {
|
||||
range.startIndex = index
|
||||
break
|
||||
} else if (preElement.controlComponent === ControlComponent.PLACEHOLDER) {
|
||||
range.startIndex = index
|
||||
range.endIndex = index
|
||||
return
|
||||
}
|
||||
index--
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public render(ctx: CanvasRenderingContext2D, x: number, y: number, width: number, height: number) {
|
||||
ctx.save()
|
||||
ctx.globalAlpha = this.options.rangeAlpha
|
||||
|
||||
Reference in New Issue
Block a user