From 69750a115a5adcc5ab61c69846fcde402d1019b3 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Fri, 28 Apr 2023 21:30:50 +0800 Subject: [PATCH] feat: wrap within list item --- src/editor/core/draw/Draw.ts | 2 +- src/editor/core/draw/particle/ListParticle.ts | 2 +- src/editor/core/event/handlers/keydown.ts | 7 ++++- src/editor/dataset/constant/Element.ts | 3 +- src/editor/interface/Element.ts | 1 + src/editor/utils/clipboard.ts | 31 +++++++++++++------ 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/editor/core/draw/Draw.ts b/src/editor/core/draw/Draw.ts index bcfbfb7..06d7070 100644 --- a/src/editor/core/draw/Draw.ts +++ b/src/editor/core/draw/Draw.ts @@ -1043,7 +1043,7 @@ export class Draw { if (element.listId) { if (element.listId !== listId) { listIndex = 0 - } else if (element.value === ZERO) { + } else if (element.value === ZERO && !element.listWrap) { listIndex++ } } diff --git a/src/editor/core/draw/particle/ListParticle.ts b/src/editor/core/draw/particle/ListParticle.ts index 119882f..e87fb4d 100644 --- a/src/editor/core/draw/particle/ListParticle.ts +++ b/src/editor/core/draw/particle/ListParticle.ts @@ -75,7 +75,7 @@ export class ListParticle { public drawListStyle(ctx: CanvasRenderingContext2D, row: IRow, position: IElementPosition) { const { elementList, offsetX, listIndex, ascent } = row const startElement = elementList[0] - if (startElement.value !== ZERO) return + if (startElement.value !== ZERO || startElement.listWrap) return let text = '' if (startElement.listType === ListType.UL) { if (startElement.listStyle) { diff --git a/src/editor/core/event/handlers/keydown.ts b/src/editor/core/event/handlers/keydown.ts index 5f294fb..79bf51d 100644 --- a/src/editor/core/event/handlers/keydown.ts +++ b/src/editor/core/event/handlers/keydown.ts @@ -77,8 +77,13 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) { const enterText: IElement = { value: ZERO } - // 标题结尾处回车无需格式化 + const startElement = elementList[startIndex] const endElement = elementList[endIndex] + // 列表块内换行 + if (evt.shiftKey && startElement.listId) { + enterText.listWrap = true + } + // 标题结尾处回车无需格式化 if (!(endElement.titleId && endElement.titleId !== elementList[endIndex + 1]?.titleId)) { formatElementContext(elementList, [enterText], startIndex) } diff --git a/src/editor/dataset/constant/Element.ts b/src/editor/dataset/constant/Element.ts index 95ea996..f7be868 100644 --- a/src/editor/dataset/constant/Element.ts +++ b/src/editor/dataset/constant/Element.ts @@ -55,7 +55,8 @@ export const EDITOR_ELEMENT_ZIP_ATTR: Array = [ 'block', 'level', 'listType', - 'listStyle' + 'listStyle', + 'listWrap' ] export const EDITOR_ELEMENT_CONTEXT_ATTR: Array = [ diff --git a/src/editor/interface/Element.ts b/src/editor/interface/Element.ts index d42347d..28e6eed 100644 --- a/src/editor/interface/Element.ts +++ b/src/editor/interface/Element.ts @@ -43,6 +43,7 @@ export interface IListElement { listType?: ListType; listStyle?: ListStyle; listId?: string; + listWrap?: boolean; } export interface ITableAttr { diff --git a/src/editor/utils/clipboard.ts b/src/editor/utils/clipboard.ts index f03d907..f72624a 100644 --- a/src/editor/utils/clipboard.ts +++ b/src/editor/utils/clipboard.ts @@ -110,18 +110,24 @@ export function writeElementList(elementList: IElement[], options: DeepRequired< const zipList = zipElementList(element.valueList!) for (let z = 0; z < zipList.length; z++) { const zipElement = zipList[z] - const zipValueList = zipElement.value.split('\n') - for (let c = 0; c < zipValueList.length; c++) { - if (c > 0) { - curListIndex += 1 - } - const value = zipValueList[c] + if (zipElement.listWrap) { const listElementList = listElementListMap.get(curListIndex) || [] - listElementList.push({ - ...zipElement, - value, - }) + listElementList.push(zipElement) listElementListMap.set(curListIndex, listElementList) + } else { + const zipValueList = zipElement.value.split('\n') + for (let c = 0; c < zipValueList.length; c++) { + if (c > 0) { + curListIndex += 1 + } + const value = zipValueList[c] + const listElementList = listElementListMap.get(curListIndex) || [] + listElementList.push({ + ...zipElement, + value, + }) + listElementListMap.set(curListIndex, listElementList) + } } } listElementListMap.forEach(listElementList => { @@ -264,6 +270,11 @@ export function getElementListByHTML(htmlText: string, options: IGetElementListB } listNode.querySelectorAll('li').forEach(li => { const liValueList = getElementListByHTML(li.innerHTML, options) + liValueList.forEach(list => { + if (list.value === '\n') { + list.listWrap = true + } + }) liValueList.unshift({ value: '\n' })