Skip to content

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.

fix(doc): debug

DR-Univer1cf9f7a6dfec
diff --git a/examples/univer-doc-ts/index.html b/examples/univer-doc-ts/index.html
index bb6e897055..a14c559a5e 100644
--- a/examples/univer-doc-ts/index.html
+++ b/examples/univer-doc-ts/index.html
@@ -1,24 +1,22 @@
<!DOCTYPE html>
<html lang="en">
+ <head>
+ <meta charset="UTF-8" />
+ <link rel="icon" type="image/svg+xml" href="favicon.svg" />
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ <title>UniverDoc</title>
+ <style>
+ html,
+ body {
+ height: 100%;
+ margin: 0;
+ }
+ </style>
+ </head>
-<head>
- <meta charset="UTF-8" />
- <link rel="icon" type="image/svg+xml" href="favicon.svg" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>UniverDoc</title>
- <style>
- html,
- body {
- height: 100%;
- margin: 0;
- }
- </style>
-</head>
-
-<body>
- <div id="app" style="z-index: 1; position: fixed; font-size: 20px; color: green;"></div>
- <div id="univerdoc" style="height: 100vh;width: 300px;"></div>
- <script type="module" src="./src/main.tsx"></script>
-</body>
-
-</html>
\ No newline at end of file
+ <body>
+ <div id="app" style="z-index: 1; position: fixed; font-size: 20px; color: green"></div>
+ <div id="univerdoc" style="height: 100vh; width: 100%"></div>
+ <script type="module" src="./src/main.tsx"></script>
+ </body>
+</html>
diff --git a/examples/univer-doc-ts/src/main.tsx b/examples/univer-doc-ts/src/main.tsx
index a65d29fe18..80ca0866c1 100644
--- a/examples/univer-doc-ts/src/main.tsx
+++ b/examples/univer-doc-ts/src/main.tsx
@@ -5,6 +5,7 @@ import { DocPlugin } from '@univerjs/base-docs';
import { DEFAULT_DOCUMENT_DATA_EN } from '@univerjs/common-plugin-data';
import { DocUIPlugin } from '@univerjs/ui-plugin-docs';
import { UIPlugin } from '@univerjs/base-ui';
+import { DocsView } from '@univerjs/base-docs/src/View/Render/Views';
// univer
const univer = new Univer();
@@ -16,7 +17,7 @@ univer.install(new RenderEngine());
const univerdoc = UniverDoc.newInstance(DEFAULT_DOCUMENT_DATA_EN);
univer.addUniverDoc(univerdoc);
-univer.install(new UIPlugin())
+univer.install(new UIPlugin());
univerdoc.installPlugin(new DocPlugin());
univer.install(
new DocUIPlugin({
@@ -29,7 +30,7 @@ univer.install(
})
);
-document.querySelector('#univerdoc').style.width = '100%'
+// document.querySelector('#univerdoc').style.width = '100%';
// setTimeout(() => {
// univerdoc._context
@@ -40,11 +41,14 @@ document.querySelector('#univerdoc').style.width = '100%'
// univerdoc.installPlugin(new DocPlugin());
-
-window.addEventListener('resize', function (event) {
- console.log('resize doc')
- univerdoc._context
- .getPluginManager()
- .getRequirePluginByName('document').getDocsView().scrollToCenter();
- // .getRequirePluginByName('document').getDocsView().getDocs().calculatePagePosition();
-}, true);
+window.addEventListener(
+ 'resize',
+ function (event) {
+ console.log('resize doc');
+ const docPlugin = univerdoc.context.getPluginManager().getRequirePluginByName('document') as DocPlugin;
+ const docsView = docPlugin.getDocsView() as DocsView;
+ docsView.scrollToCenter();
+ // .getRequirePluginByName('document').getDocsView().getDocs().calculatePagePosition();
+ },
+ true
+);
diff --git a/packages/core/src/Docs/Action/DeleteTextAction.ts b/packages/core/src/Docs/Action/DeleteTextAction.ts
index 6c0e6fe5c2..f3922c98a5 100644
--- a/packages/core/src/Docs/Action/DeleteTextAction.ts
+++ b/packages/core/src/Docs/Action/DeleteTextAction.ts
@@ -5,11 +5,13 @@ import { IInsertTextActionData } from './InsertTextAction';
import { InsertTextApply } from '../Apply/InsertTextApply';
import { ITextSelectionRangeParam } from '../../Interfaces/ISelectionData';
import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';
+import { IElement } from '../../Interfaces/IDocumentData';
export interface IDeleteTextActionData
extends IDocActionData,
ITextSelectionRangeParam {
- text: string;
+ text: string | IElement[];
+ textLength: number;
}
export class DeleteTextAction extends DocActionBase<
@@ -26,10 +28,11 @@ export class DeleteTextAction extends DocActionBase<
super(actionData, commandUnit, observers);
this._doActionData = { ...actionData };
this.do();
- const { text, cursorStart, isStartBack, segmentId } = actionData;
+ const { text, cursorStart, isStartBack, textLength, segmentId } = actionData;
this._oldActionData = {
actionName: DOC_ACTION_NAMES.INSERT_TEXT_ACTION_NAME,
text,
+ textLength,
cursorStart,
isStartBack,
segmentId,
@@ -65,8 +68,12 @@ export class DeleteTextAction extends DocActionBase<
// TODO ...
const actionData = this.getOldActionData();
const document = this.getDocument();
- const { text, cursorStart, isStartBack, segmentId } = actionData;
- InsertTextApply(document, text, { cursorStart, isStartBack, segmentId });
+ const { text, textLength, cursorStart, isStartBack, segmentId } = actionData;
+ InsertTextApply(document, text, textLength, {
+ cursorStart,
+ isStartBack,
+ segmentId,
+ });
}
validate(): boolean {
diff --git a/packages/core/src/Docs/Action/InsertTextAction.ts b/packages/core/src/Docs/Action/InsertTextAction.ts
index 91b3107c7b..b527a5fc6f 100644
--- a/packages/core/src/Docs/Action/InsertTextAction.ts
+++ b/packages/core/src/Docs/Action/InsertTextAction.ts
@@ -5,13 +5,15 @@ import {
CommandManager,
CommandUnit,
} from '../../Command';
+import { IElement } from '../../Interfaces/IDocumentData';
import { InsertTextApply } from '../Apply/InsertTextApply';
import { IDeleteTextActionData } from './DeleteTextAction';
import { DeleteTextApply } from '../Apply/DeleteTextApply';
import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';
export interface IInsertTextActionData extends IDocActionData {
- text: string;
+ text: string | IElement[];
+ textLength: number;
cursorStart: number;
isStartBack: boolean;
segmentId?: string; //The ID of the header, footer or footnote the location is in. An empty segment ID signifies the document's body.
@@ -31,11 +33,11 @@ export class InsertTextAction extends DocActionBase<
super(actionData, commandUnit, observers);
this._doActionData = { ...actionData };
this.do();
- const { cursorStart, isStartBack, text } = actionData;
+ const { cursorStart, isStartBack, text, textLength } = actionData;
this._oldActionData = {
...actionData,
actionName: DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME,
- cursorEnd: cursorStart + text.length,
+ cursorEnd: cursorStart + textLength,
isEndBack: isStartBack,
isCollapse: true,
};
@@ -48,8 +50,12 @@ export class InsertTextAction extends DocActionBase<
do(): void {
const actionData = this.getDoActionData();
const document = this.getDocument();
- const { text, cursorStart, isStartBack, segmentId } = actionData;
- InsertTextApply(document, text, { cursorStart, isStartBack, segmentId });
+ const { text, cursorStart, isStartBack, textLength, segmentId } = actionData;
+ InsertTextApply(document, text, textLength, {
+ cursorStart,
+ isStartBack,
+ segmentId,
+ });
this._observers.notifyObservers({
type: ActionType.REDO,
diff --git a/packages/core/src/Docs/Apply/Common.ts b/packages/core/src/Docs/Apply/Common.ts
index 2b50d6f6ee..a39a6746b1 100644
--- a/packages/core/src/Docs/Apply/Common.ts
+++ b/packages/core/src/Docs/Apply/Common.ts
@@ -79,3 +79,14 @@ export function mergeSameTextRun(mainTr: ITextRun, mergeTr: ITextRun) {
return mainTr;
}
+
+export function getTextLength(text: string | ITextRun[]) {
+ if (text instanceof Object) {
+ let len = 0;
+ for (let textRun of text) {
+ len += textRun.ct?.length || 0;
+ }
+ return len;
+ }
+ return text.length;
+}
diff --git a/packages/core/src/Docs/Apply/InsertTextApply.ts b/packages/core/src/Docs/Apply/InsertTextApply.ts
index 8f091d452b..bcf92fd6e9 100644
--- a/packages/core/src/Docs/Apply/InsertTextApply.ts
+++ b/packages/core/src/Docs/Apply/InsertTextApply.ts
@@ -4,6 +4,7 @@ import {
IBlockElement,
IParagraph,
ParagraphElementType,
+ IElement,
} from '../../Interfaces/IDocumentData';
import { ITextSelectionRangeStartParam } from '../../Interfaces/ISelectionData';
import { DocumentModel } from '../Domain/DocumentModel';
@@ -15,7 +16,8 @@ import {
export function InsertTextApply(
document: DocumentModel,
- text: string,
+ text: string | IElement[],
+ textLength: number,
collapseRange: ITextSelectionRangeStartParam
) {
const doc = document.getSnapshot();
@@ -24,7 +26,7 @@ export function InsertTextApply(
const body = getDocsUpdateBody(doc, segmentId);
- if (text.length === 0) {
+ if (textLength === 0) {
return;
}
@@ -46,6 +48,7 @@ export function InsertTextApply(
blockElement.paragraph &&
insertText(
text,
+ textLength,
blockElement,
blockElement.paragraph,
collapseRange
@@ -55,7 +58,8 @@ export function InsertTextApply(
}
function insertText(
- text: string,
+ text: string | IElement[],
+ textLength: number,
blockElement: IBlockElement,
paragraph: IParagraph,
collapseRange: ITextSelectionRangeStartParam
@@ -72,19 +76,24 @@ function insertText(
const { elements } = paragraph;
+ let index = 0;
+
for (let element of elements) {
const { et: paragraphElementType } = element;
if (paragraphElementType === ParagraphElementType.DRAWING) {
+ index++;
continue;
}
const { st: elementStartIndex, ed: elementEndIndex, tr } = element;
if (tr == null || tr.tab === BooleanNumber.TRUE) {
+ index++;
continue;
}
if (textStart < elementStartIndex || textStart > elementEndIndex) {
+ index++;
continue;
}
@@ -104,9 +113,60 @@ function insertText(
relativeStart = 0;
}
- const newContent = insertTextToContent(tr.ct || '', relativeStart, text);
-
- tr.ct = newContent;
+ if (text instanceof Object) {
+ const nextEle = elements[index + 1];
+ const insertedElements = splitTextRun(
+ text,
+ textLength,
+ relativeStart,
+ element,
+ nextEle
+ );
+ } else {
+ const newContent = insertTextToContent(
+ tr.ct || '',
+ relativeStart,
+ text as string
+ );
+
+ tr.ct = newContent;
+
+ index++;
+ }
}
}
}
+
+function splitTextRun(
+ insertTextRunList: IElement[],
+ textLength: number,
+ relativeStart: number,
+ currentElement: IElement,
+ nextElement: IElement
+) {
+ const trLen = insertTextRunList.length;
+
+ const insertFirstEle = insertTextRunList[0];
+
+ const insertLastTrEle = insertTextRunList[trLen - 1];
+
+ const {
+ st: currentStartIndex,
+ ed: currentEndIndex,
+ tr: currentTr,
+ } = currentElement;
+
+ const currentContent = currentTr?.ct || '';
+
+ const newElements: IElement[] = [];
+
+ if (trLen === 1) {
+ const splitLeft = currentContent.slice(0, relativeStart);
+
+ const splitRight = currentContent.slice(relativeStart);
+ } else if (trLen > 1) {
+ const splitLeft = currentContent.slice(0, relativeStart);
+
+ const splitRight = currentContent.slice(relativeStart);
+ }
+}
diff --git a/packages/core/src/Docs/Domain/DocumentModel.ts b/packages/core/src/Docs/Domain/DocumentModel.ts
index 0b197b3ab4..4287ae8572 100644
--- a/packages/core/src/Docs/Domain/DocumentModel.ts
+++ b/packages/core/src/Docs/Domain/DocumentModel.ts
@@ -1,6 +1,11 @@
import { DocContext } from '../../Basics';
import { Command } from '../../Command';
-import { BlockType, IDocumentData, ITextSelectionRange } from '../../Interfaces';
+import {
+ BlockType,
+ IDocumentData,
+ ITextRun,
+ ITextSelectionRange,
+} from '../../Interfaces';
import { DEFAULT_DOC } from '../../Const';
import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';
import { getDocsUpdateBody } from '../Apply/Common';
@@ -28,7 +33,7 @@ export class DocumentModel {
}
insertText(
- text: string,
+ text: string | ITextRun,
range: ITextSelectionRange,
segmentId?: string
): DocumentModel {