diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index 52833f0..2309215 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -201,7 +201,7 @@ export class CommandAdapt { public rowFlex(payload: RowFlex) { const { startIndex, endIndex } = this.range.getRange() - if (startIndex === 0 && endIndex === 0) return + if (!~startIndex && !~endIndex) return const pageNo = this.draw.getPageNo() const positionList = this.position.getPositionList() // 开始/结束行号 @@ -225,7 +225,7 @@ export class CommandAdapt { public rowMargin(payload: number) { const { startIndex, endIndex } = this.range.getRange() - if (startIndex === 0 && endIndex === 0) return + if (!~startIndex && !~endIndex) return const pageNo = this.draw.getPageNo() const positionList = this.position.getPositionList() // 开始/结束行号 @@ -249,7 +249,7 @@ export class CommandAdapt { public insertTable(row: number, col: number) { const { startIndex, endIndex } = this.range.getRange() - if (startIndex === 0 && endIndex === 0) return + if (!~startIndex && !~endIndex) return const elementList = this.draw.getElementList() const { width, margins } = this.options const innerWidth = width - margins[1] - margins[3] @@ -580,7 +580,9 @@ export class CommandAdapt { }) this.range.setRange(0, 0) // 重新渲染 - this.draw.render({ isSetCursor: false }) + this.draw.render({ + curIndex: positionContext.index + }) this.tableTool.dispose() } @@ -634,7 +636,9 @@ export class CommandAdapt { }) this.range.setRange(0, 0) // 重新渲染 - this.draw.render({ isSetCursor: false }) + this.draw.render({ + curIndex: positionContext.index + }) this.tableTool.dispose() } @@ -656,7 +660,7 @@ export class CommandAdapt { public hyperlink(payload: IElement) { const { startIndex, endIndex } = this.range.getRange() - if (startIndex === 0 && endIndex === 0) return + if (!~startIndex && !~endIndex) return const elementList = this.draw.getElementList() const { valueList, url } = payload const hyperlinkId = getUUID() @@ -680,7 +684,7 @@ export class CommandAdapt { public image(payload: IDrawImagePayload) { const { startIndex, endIndex } = this.range.getRange() - if (startIndex === 0 && endIndex === 0) return + if (!~startIndex && !~endIndex) return const elementList = this.draw.getElementList() const { value, width, height } = payload const element: IElement = { @@ -703,29 +707,40 @@ export class CommandAdapt { public search(payload: string | null) { if (payload) { let searchMatchList: ISearchResult[] = [] - // elementList按table分隔 + // 分组 const elementListGroup: { type: EditorContext, elementList: IElement[], index: number }[] = [] const originalElementList = this.draw.getOriginalElementList() - let lastTextElementList: IElement[] = [] - for (let e = 0; e < originalElementList.length; e++) { + const originalElementListLength = originalElementList.length + // 查找表格所在位置 + const tabeleIndexList = [] + for (let e = 0; e < originalElementListLength; e++) { const element = originalElementList[e] if (element.type === ElementType.TABLE) { - if (lastTextElementList.length) { - elementListGroup.push({ - index: e, - type: EditorContext.PAGE, - elementList: lastTextElementList - }) - lastTextElementList = [] - } + tabeleIndexList.push(e) + } + } + let i = 0 + let elementIndex = 0 + while (elementIndex < originalElementListLength - 1) { + const endIndex = tabeleIndexList.length ? tabeleIndexList[i] : originalElementListLength + const pageElement = originalElementList.slice(elementIndex, endIndex) + if (pageElement.length) { + elementListGroup.push({ + index: elementIndex, + type: EditorContext.PAGE, + elementList: pageElement + }) + } + const tableElement = originalElementList[endIndex] + if (tableElement) { elementListGroup.push({ - index: e, + index: endIndex, type: EditorContext.TABLE, - elementList: [element] + elementList: [tableElement] }) - } else { - lastTextElementList.push(element) } + elementIndex += endIndex + 1 + i++ } // 搜索文本 function searchClosure(payload: string | null, type: EditorContext, elementList: IElement[], restArgs?: ISearchResultRestArgs) { @@ -743,7 +758,7 @@ export class CommandAdapt { for (let m = 0; m < matchStartIndexList.length; m++) { const startIndex = matchStartIndexList[m] for (let i = 0; i < payload.length; i++) { - const index = startIndex + i + const index = startIndex + i + (restArgs?.startIndex || 0) searchMatchList.push({ type, index, @@ -769,7 +784,9 @@ export class CommandAdapt { } } } else { - searchClosure(payload, group.type, group.elementList) + searchClosure(payload, group.type, group.elementList, { + startIndex: group.index + }) } } this.draw.setSearchMatch(searchMatchList) diff --git a/src/editor/core/contextmenu/ContextMenu.ts b/src/editor/core/contextmenu/ContextMenu.ts index d1a544a..adac4fc 100644 --- a/src/editor/core/contextmenu/ContextMenu.ts +++ b/src/editor/core/contextmenu/ContextMenu.ts @@ -83,7 +83,7 @@ export class ContextMenu { private _getContext(): IContextMenuContext { const { startIndex, endIndex } = this.range.getRange() // 是否存在焦点 - const editorTextFocus = startIndex !== 0 || endIndex !== 0 + const editorTextFocus = !!(~startIndex || ~endIndex) // 是否存在选区 const editorHasSelection = editorTextFocus && startIndex !== endIndex // 是否在表格内 diff --git a/src/editor/core/event/CanvasEvent.ts b/src/editor/core/event/CanvasEvent.ts index c5ce430..2ee70d5 100644 --- a/src/editor/core/event/CanvasEvent.ts +++ b/src/editor/core/event/CanvasEvent.ts @@ -1,5 +1,6 @@ import { ElementType } from "../.." import { ZERO } from "../../dataset/constant/Common" +import { EDITOR_ELEMENT_COPY_ATTR } from "../../dataset/constant/Element" import { ElementStyleKey } from "../../dataset/enum/ElementStyle" import { MouseEventButton } from "../../dataset/enum/Event" import { KeyMap } from "../../dataset/enum/Keymap" @@ -100,8 +101,8 @@ export class CanvasEvent { if (start > end) { [start, end] = [end, start] } - this.range.setRange(start, end) if (start === end) return + this.range.setRange(start, end) // 绘制 this.draw.render({ isSubmitHistory: false, @@ -347,23 +348,16 @@ export class CanvasEvent { ...restArg } if ( - !element.type - || element.type === TEXT + element.type === TEXT + || (!element.type && element.value !== ZERO) || (element.type === HYPERLINK && elementList[endIndex + 1]?.type === HYPERLINK) ) { - if (element.type) { - newElement.type = element.type - } - newElement.font = element.font - newElement.size = element.size - newElement.bold = element.bold - newElement.color = element.color - newElement.highlight = element.highlight - newElement.italic = element.italic - newElement.underline = element.underline - newElement.strikeout = element.strikeout - newElement.url = element.url - newElement.hyperlinkId = element.hyperlinkId + EDITOR_ELEMENT_COPY_ATTR.forEach(attr => { + const value = element[attr] as never + if (value !== undefined) { + newElement[attr] = value + } + }) } return newElement }) diff --git a/src/editor/core/event/GlobalEvent.ts b/src/editor/core/event/GlobalEvent.ts index 2cb015f..a5d1f8a 100644 --- a/src/editor/core/event/GlobalEvent.ts +++ b/src/editor/core/event/GlobalEvent.ts @@ -70,7 +70,7 @@ export class GlobalEvent { } this.cursor.recoveryCursor() this.range.recoveryRangeStyle() - this.range.setRange(0, 0) + this.range.setRange(-1, -1) this.imageParticle.clearResizer() this.tableTool.dispose() this.hyperlinkParticle.clearHyperlinkPopup() diff --git a/src/editor/core/range/RangeManager.ts b/src/editor/core/range/RangeManager.ts index f0e1af0..dab76f2 100644 --- a/src/editor/core/range/RangeManager.ts +++ b/src/editor/core/range/RangeManager.ts @@ -19,8 +19,8 @@ export class RangeManager { this.listener = draw.getListener() this.historyManager = draw.getHistoryManager() this.range = { - startIndex: 0, - endIndex: 0 + startIndex: -1, + endIndex: -1 } } @@ -46,7 +46,8 @@ export class RangeManager { if (!curElementList) { const elementList = this.draw.getElementList() const { endIndex } = this.range - curElementList = [elementList[endIndex]] + const index = ~endIndex ? endIndex : 0 + curElementList = [elementList[index]] } const curElement = curElementList[curElementList.length - 1] // 富文本 diff --git a/src/editor/dataset/constant/Element.ts b/src/editor/dataset/constant/Element.ts index 305820c..38e8f29 100644 --- a/src/editor/dataset/constant/Element.ts +++ b/src/editor/dataset/constant/Element.ts @@ -1 +1,26 @@ -export const EDITOR_ELEMENT_STYLE = ['bold', 'color', 'highlight', 'font', 'size', 'italic', 'underline', 'strikeout'] \ No newline at end of file +import { IElement } from "../../interface/Element" + +export const EDITOR_ELEMENT_STYLE = [ + 'bold', + 'color', + 'highlight', + 'font', + 'size', + 'italic', + 'underline', + 'strikeout' +] + +export const EDITOR_ELEMENT_COPY_ATTR: Array = [ + 'type', + 'font', + 'size', + 'bold', + 'color', + 'italic', + 'highlight', + 'underline', + 'strikeout', + 'url', + 'hyperlinkId' +] \ No newline at end of file diff --git a/src/editor/interface/Search.ts b/src/editor/interface/Search.ts index 6f79eb6..adc825e 100644 --- a/src/editor/interface/Search.ts +++ b/src/editor/interface/Search.ts @@ -9,6 +9,7 @@ export interface ISearchResultRestArgs { tableIndex?: number; trIndex?: number; tdIndex?: number; + startIndex?: number; } export type ISearchResult = ISearchResultBasic & ISearchResultRestArgs \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index f9b3331..481e157 100644 --- a/src/main.ts +++ b/src/main.ts @@ -48,7 +48,7 @@ window.onload = function () { if (colorIndex.includes(index)) { return { value, - color: 'red', + color: '#FF0000', size: 16 } }