Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(doc): edit content
DR-Univera700909516e9
diff --git a/packages/core/src/Docs/Apply/Common.ts b/packages/core/src/Docs/Apply/Common.tsindex e75323012c..dc45747011 100644--- a/packages/core/src/Docs/Apply/Common.ts+++ b/packages/core/src/Docs/Apply/Common.ts@@ -2,6 +2,7 @@ import {IBlockElement,IDocumentData,IElement,+ ITextRun,} from '../../Interfaces/IDocumentData';export function getTextIndexByCursor(index: number, isBack: boolean = false) {@@ -12,9 +13,11 @@ export function insertTextToContent(content: string, start: number, text: stringreturn content.slice(0, start) + text + content.slice(start);}-export function deleteContent(content: string, start: number, length: number) {- const startDeleteIndex = start - length < 0 ? 0 : start - length;- return content.slice(0, startDeleteIndex) + content.slice(start);+export function deleteContent(content: string, start: number, end: number) {+ if (start > end) {+ return content;+ }+ return content.slice(0, start) + content.slice(end);}export function moveElementCharIndex(element?: IElement, moveIndex: number = 0) {@@ -52,3 +55,11 @@ export function getDocsUpdateBody(model: IDocumentData, segmentId?: string) {return body;}++export function isSameStyleTextRun(tr1: ITextRun, tr2: ITextRun) {+ return false;+}++export function mergeSameTextRun(mainTr: ITextRun, mergeTr: ITextRun) {+ const isSame = isSameStyleTextRun(mainTr, mergeTr);+}diff --git a/packages/core/src/Docs/Apply/DeleteTextApply.ts b/packages/core/src/Docs/Apply/DeleteTextApply.tsindex 760811d7bc..18722d8fd5 100644--- a/packages/core/src/Docs/Apply/DeleteTextApply.ts+++ b/packages/core/src/Docs/Apply/DeleteTextApply.ts@@ -7,7 +7,12 @@ import {} from '../../Interfaces/IDocumentData';import { ITextSelectionRangeParam } from '../../Interfaces/ISelectionData';import { DocumentModel } from '../Domain/DocumentModel';-import { deleteContent, getDocsUpdateBody, getTextIndexByCursor } from './Common';+import {+ deleteContent,+ getDocsUpdateBody,+ getTextIndexByCursor,+ mergeSameTextRun,+} from './Common';export function DeleteTextApply(document: DocumentModel,@@ -52,44 +57,66 @@ function deleteText(const textEnd = getTextIndexByCursor(cursorEnd, isEndBack);- const { st, ed } = blockElement;+ const { st: blockStartIndex, ed: blockEndIndex } = blockElement;- if (cursorStart > ed || cursorEnd < st) {+ if (cursorStart > blockEndIndex || cursorEnd < blockStartIndex) {return;}const { elements } = paragraph;+ let preIsDeleted = false;++ let index = 0;+for (let element of elements) {const { et: paragraphElementType } = element;if (paragraphElementType === ParagraphElementType.DRAWING) {+ index++;continue;}- const { st, ed, tr } = element;+ const { st: elementStartIndex, ed: elementEndIndex, tr } = element;- if (tr == null) {+ if (tr == null || tr.tab === BooleanNumber.TRUE) {+ index++;continue;}- if (textStart < st || textStart > ed) {+ if (textEnd < elementStartIndex || textStart > elementEndIndex) {+ index++;continue;}if (paragraphElementType === ParagraphElementType.TEXT_RUN) {- let relative = textStart - st + 1;+ let relativeStart = textStart - elementStartIndex + 1;++ let relativeEnd = textEnd - elementStartIndex + 1;- if (textStart === 0) {- relative = 0;+ if (relativeStart <= 0) {+ relativeStart = 0;}- if (tr.tab === BooleanNumber.TRUE) {- continue;+ if (relativeEnd >= elementEndIndex - elementStartIndex - 1) {+ relativeStart = elementEndIndex - elementStartIndex - 1;}- const newContent = deleteContent(tr.ct || '', relative, length);+ const newContent = deleteContent(+ tr.ct || '',+ relativeStart,+ relativeEnd+ );tr.ct = newContent;}++ if (tr.ct?.length === 0) {+ elements.splice(index, 1);+ preIsDeleted = true;+ } else if (preIsDeleted === true) {+ const m = mergeSameTextRun(tr, tr);+ }++ index++;}}