Skip to content

Commit

Permalink
fix: paste list element boundary error
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Apr 24, 2023
1 parent 10ada8c commit 5935eb7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
9 changes: 3 additions & 6 deletions src/editor/core/event/handlers/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EDITOR_ELEMENT_COPY_ATTR } from '../../../dataset/constant/Element'
import { ElementType } from '../../../dataset/enum/Element'
import { IElement } from '../../../interface/Element'
import { splitText } from '../../../utils'
import { formatElementContext } from '../../../utils/element'
import { formatElementContext, getAnchorElement } from '../../../utils/element'
import { CanvasEvent } from '../CanvasEvent'

export function input(data: string, host: CanvasEvent) {
Expand Down Expand Up @@ -34,11 +34,8 @@ export function input(data: string, host: CanvasEvent) {
const { startIndex, endIndex } = rangeManager.getRange()
// 格式化元素
const elementList = draw.getElementList()
const endElement = elementList[endIndex]
const endNextElement = elementList[endIndex + 1]
const copyElement = endElement.value === ZERO && endNextElement
? endNextElement
: endElement
const copyElement = getAnchorElement(elementList, endIndex)
if (!copyElement) return
const inputData: IElement[] = splitText(text).map(value => {
const newElement: IElement = {
value
Expand Down
6 changes: 5 additions & 1 deletion src/editor/core/event/handlers/keydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) {
const enterText: IElement = {
value: ZERO
}
formatElementContext(elementList, [enterText], startIndex)
// 标题结尾处回车无需格式化
const endElement = elementList[endIndex]
if (!(endElement.titleId && endElement.titleId !== elementList[endIndex + 1]?.titleId)) {
formatElementContext(elementList, [enterText], startIndex)
}
let curIndex: number
if (activeControl && !control.isRangInPostfix()) {
curIndex = control.setValue([enterText])
Expand Down
9 changes: 3 additions & 6 deletions src/editor/core/range/RangeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ControlComponent } from '../../dataset/enum/Control'
import { IEditorOption } from '../../interface/Editor'
import { IElement } from '../../interface/Element'
import { IRange, RangeRowArray, RangeRowMap } from '../../interface/Range'
import { getAnchorElement } from '../../utils/element'
import { Draw } from '../draw/Draw'
import { HistoryManager } from '../history/HistoryManager'
import { Listener } from '../listener/Listener'
Expand Down Expand Up @@ -219,7 +220,7 @@ export class RangeManager {
if (!this.listener.rangeStyleChange) return
// 结束光标位置
const { endIndex, isCrossRowCol } = this.range
let curElement: IElement
let curElement: IElement | null
if (isCrossRowCol) {
// 单元格选择以当前表格定位
const originalElementList = this.draw.getOriginalElementList()
Expand All @@ -229,11 +230,7 @@ export class RangeManager {
const index = ~endIndex ? endIndex : 0
// 行首以第一个非换行符元素定位
const elementList = this.draw.getElementList()
const endElement = elementList[index]
const endNextElement = elementList[index + 1]
curElement = endElement.value === ZERO && endNextElement && endNextElement.value !== ZERO
? endNextElement
: endElement
curElement = getAnchorElement(elementList, index)
}
if (!curElement) return
// 选取元素列表
Expand Down
16 changes: 12 additions & 4 deletions src/editor/utils/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,15 +543,23 @@ export function isTextLikeElement(element: IElement): boolean {
return !element.type || TEXTLIKE_ELEMENT_TYPE.includes(element.type)
}

export function formatElementContext(sourceElementList: IElement[], formatElementList: IElement[], anchorIndex: number) {
const anchorElement = sourceElementList[anchorIndex]
const anchorNextElement = sourceElementList[anchorIndex + 1]
const copyElement = anchorElement?.value === ZERO && anchorNextElement && anchorNextElement.value !== ZERO
export function getAnchorElement(elementList: IElement[], anchorIndex: number): IElement | null {
const anchorElement = elementList[anchorIndex]
if (!anchorElement) return null
const anchorNextElement = elementList[anchorIndex + 1]
// 非列表元素 && 当前元素是换行符 && 下一个元素不是换行符 则以下一个元素作为参考元素
return !anchorElement.listId && anchorElement.value === ZERO && anchorNextElement && anchorNextElement.value !== ZERO
? anchorNextElement
: anchorElement
}

export function formatElementContext(sourceElementList: IElement[], formatElementList: IElement[], anchorIndex: number) {
const copyElement = getAnchorElement(sourceElementList, anchorIndex)
if (!copyElement) return
for (let e = 0; e < formatElementList.length; e++) {
const targetElement = formatElementList[e]
// 定位元素非列表,无需处理粘贴列表的上下文
if (!copyElement.listId && targetElement.type === ElementType.LIST) continue
if (targetElement.valueList && targetElement.valueList.length) {
formatElementContext(sourceElementList, targetElement.valueList, anchorIndex)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1097,13 +1097,13 @@ window.onload = function () {
// 列表
listOptionDom.querySelectorAll<HTMLLIElement>('li').forEach(li => li.classList.remove('active'))
if (payload.listType) {
listDom.classList.add('active')
const listType = payload.listType
const listStyle = payload.listType === ListType.OL ? ListStyle.DECIMAL : payload.listType
const curListDom = listOptionDom
.querySelector<HTMLLIElement>(`[data-list-type='${listType}'][data-list-style='${listStyle}']`)
if (curListDom) {
curListDom.classList.add('active')
listDom.classList.add('active')
}
} else {
listDom.classList.remove('active')
Expand Down

0 comments on commit 5935eb7

Please sign in to comment.