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.

feat(doc): editor text

DR-Univer402b0bdb667b
diff --git a/examples/univer-doc-ts/src/main.tsx b/examples/univer-doc-ts/src/main.tsx
index 448dd555ec..cd1a6f40b1 100644
--- a/examples/univer-doc-ts/src/main.tsx
+++ b/examples/univer-doc-ts/src/main.tsx
@@ -15,8 +15,6 @@ univer.install(new RenderEngine());
const univerdoc = UniverDoc.newInstance(DEFAULT_DOCUMENT_DATA_EN);
univer.addUniverDoc(univerdoc);
-univerdoc.installPlugin(new DocPlugin());
-
// univerdoc.installPlugin(new DocPlugin());
univer.install(
new DocUIPlugin({
@@ -28,3 +26,5 @@ univer.install(
},
})
);
+
+univerdoc.installPlugin(new DocPlugin());
diff --git a/packages/base-docs/src/Basics/Observer.ts b/packages/base-docs/src/Basics/Observer.ts
index e2cb227d78..e4f996dd2d 100644
--- a/packages/base-docs/src/Basics/Observer.ts
+++ b/packages/base-docs/src/Basics/Observer.ts
@@ -1,7 +1,5 @@
import { IKeyboardEvent } from '@univerjs/base-render';
import { Observable, Plugin } from '@univerjs/core';
-import { DocContainer } from '../View/UI/DocContainer';
-import { Toolbar } from '../View/UI/Toolbar/Toolbar';
export type DocPluginObserve = {
onAfterChangeFontFamilyObservable: Observable<string>;
@@ -15,10 +13,6 @@ export type DocPluginObserve = {
onChangeCurrentSheetObserver: Observable<string>;
- onToolbarDidMountObservable: Observable<Toolbar>;
-
- onDocContainerDidMountObservable: Observable<DocContainer>;
-
// ready to move to core as onKeyDownObservable
onSpreadsheetKeyDownObservable: Observable<IKeyboardEvent>;
onSpreadsheetKeyUpObservable: Observable<IKeyboardEvent>;
@@ -40,10 +34,8 @@ export function uninstall(plugin: Plugin) {
plugin.deleteObserve('onAfterChangeFontBackgroundObservable');
plugin.deleteObserve('onAfterChangeFontColorObservable');
- plugin.deleteObserve('onToolbarDidMountObservable');
-
plugin.deleteObserve('onChangeCurrentSheetObserver');
- plugin.deleteObserve('onDocContainerDidMountObservable');
+
plugin.deleteObserve('onSpreadsheetKeyDownObservable');
plugin.deleteObserve('onSpreadsheetKeyUpObservable');
plugin.deleteObserve('onSpreadsheetKeyCopyObservable');
@@ -73,13 +65,11 @@ export function install(plugin: Plugin) {
plugin.pushToObserve('onChangeCurrentSheetObserver');
- plugin.pushToObserve('onToolbarDidMountObservable');
-
plugin.pushToObserve('onSheetBarDidMountObservable');
plugin.pushToObserve('onCountBarDidMountObservable');
plugin.pushToObserve('onInfoBarDidMountObservable');
plugin.pushToObserve('onRightMenuDidMountObservable');
- plugin.pushToObserve('onDocContainerDidMountObservable');
+
plugin.pushToObserve('onSpreadsheetKeyDownObservable');
plugin.pushToObserve('onSpreadsheetKeyUpObservable');
plugin.pushToObserve('onSpreadsheetKeyCopyObservable');
diff --git a/packages/base-docs/src/Controller/InputController.ts b/packages/base-docs/src/Controller/InputController.ts
index 13a01ae3db..200eb6d74f 100644
--- a/packages/base-docs/src/Controller/InputController.ts
+++ b/packages/base-docs/src/Controller/InputController.ts
@@ -1,19 +1,16 @@
-import { Documents, IDocumentSkeletonSpan, SpanType } from '@univerjs/base-render';
-import { TextSelection } from '@univerjs/base-render/src/Component/Docs/Common/TextSelection';
+import { Documents, IDocumentSkeletonSpan, SpanType, TextSelection } from '@univerjs/base-render';
import { KeyboardKeyType, Nullable } from '@univerjs/core';
import { DocPlugin } from '../DocPlugin';
export class InputController {
- constructor(private _plugin: DocPlugin) {
- this._plugin.getObserver('onDocContainerDidMountObservable')?.add(() => {
- this._initialize();
- });
- }
-
private _previousIMEContent: string = '';
private _previousIMEStart: number;
+ constructor(private _plugin: DocPlugin) {
+ this._initialize();
+ }
+
private _initialize() {
const events = this._plugin.getInputEvent();
if (!events) {
@@ -40,27 +37,29 @@ export class InputController {
const { cursorStart, cursorEnd, isCollapse, isEndBack, isStartBack } = activeRange;
if (e.key === KeyboardKeyType.backspace) {
- if (isCollapse) {
- let cursor = cursorStart;
+ let cursor = cursorStart;
- if (isStartBack === false) {
- cursor += 1;
- }
+ if (isStartBack === true) {
+ cursor -= 1;
+ }
+
+ if (isCollapse === false) {
+ cursor += 1;
+ }
- const selectionRemain = document.remainActiveSelection();
+ const selectionRemain = document.remainActiveSelection();
- // const content = document.findNodeByCharIndex(cursor - 1)?.content || '';
+ // const content = document.findNodeByCharIndex(cursor - 1)?.content || '';
- docsModel.deleteText(activeRange, segmentId);
+ docsModel.deleteText(activeRange, segmentId);
- skeleton.calculate();
+ skeleton.calculate();
- const span = document.findNodeByCharIndex(cursor - 2);
+ const span = document.findNodeByCharIndex(cursor - 1);
- this._adjustSelection(document, selectionRemain, span);
+ this._adjustSelection(document, selectionRemain, span);
- this._resetIME();
- }
+ this._resetIME();
}
});
diff --git a/packages/base-docs/src/DocPlugin.tsx b/packages/base-docs/src/DocPlugin.tsx
index d0ce078850..ab4b7c6562 100644
--- a/packages/base-docs/src/DocPlugin.tsx
+++ b/packages/base-docs/src/DocPlugin.tsx
@@ -50,8 +50,8 @@ export class DocPlugin extends Plugin<DocPluginObserve, DocContext> {
});
install(this);
- this.initController();
this.initCanvasView();
+ this.initController();
}
initController() {
diff --git a/packages/base-render/src/Component/Docs/Common/index.ts b/packages/base-render/src/Component/Docs/Common/index.ts
index 8311862d1a..52bae0ccf2 100644
--- a/packages/base-render/src/Component/Docs/Common/index.ts
+++ b/packages/base-render/src/Component/Docs/Common/index.ts
@@ -5,3 +5,4 @@ export * from './Section';
export * from './Span';
export * from './Liquid';
export * from './Tools';
+export * from './TextSelection';
diff --git a/packages/core/src/Docs/Action/DeleteTextAction.ts b/packages/core/src/Docs/Action/DeleteTextAction.ts
index fd23336ce9..6c0e6fe5c2 100644
--- a/packages/core/src/Docs/Action/DeleteTextAction.ts
+++ b/packages/core/src/Docs/Action/DeleteTextAction.ts
@@ -1,5 +1,5 @@
import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
-import { ActionObservers, CommandUnit } from '../../Command';
+import { ActionObservers, CommandManager, CommandUnit } from '../../Command';
import { DeleteTextApply } from '../Apply/DeleteTextApply';
import { IInsertTextActionData } from './InsertTextAction';
import { InsertTextApply } from '../Apply/InsertTextApply';
@@ -73,3 +73,5 @@ export class DeleteTextAction extends DocActionBase<
return false;
}
}
+
+CommandManager.register(DeleteTextAction.Name, DeleteTextAction);
diff --git a/packages/core/src/Docs/Action/InsertTextAction.ts b/packages/core/src/Docs/Action/InsertTextAction.ts
index ce4c128aa0..91b3107c7b 100644
--- a/packages/core/src/Docs/Action/InsertTextAction.ts
+++ b/packages/core/src/Docs/Action/InsertTextAction.ts
@@ -1,5 +1,10 @@
import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
-import { ActionObservers, ActionType, CommandUnit } from '../../Command';
+import {
+ ActionObservers,
+ ActionType,
+ CommandManager,
+ CommandUnit,
+} from '../../Command';
import { InsertTextApply } from '../Apply/InsertTextApply';
import { IDeleteTextActionData } from './DeleteTextAction';
import { DeleteTextApply } from '../Apply/DeleteTextApply';
@@ -70,3 +75,5 @@ export class InsertTextAction extends DocActionBase<
return false;
}
}
+
+CommandManager.register(InsertTextAction.NAME, InsertTextAction);
diff --git a/packages/core/src/Docs/Action/TextIndexAdjustAction.ts b/packages/core/src/Docs/Action/TextIndexAdjustAction.ts
index 570f746113..4d23cacff1 100644
--- a/packages/core/src/Docs/Action/TextIndexAdjustAction.ts
+++ b/packages/core/src/Docs/Action/TextIndexAdjustAction.ts
@@ -1,5 +1,10 @@
import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
-import { ActionObservers, ActionType, CommandUnit } from '../../Command';
+import {
+ ActionObservers,
+ ActionType,
+ CommandManager,
+ CommandUnit,
+} from '../../Command';
import { ITextSelectionRangeParam } from '../../Interfaces';
import { textIndexAdjustApply } from '../Apply/TextIndexAdjustApply';
@@ -11,6 +16,8 @@ export class TextIndexAdjustAction extends DocActionBase<
ITextIndexAdjustAction,
ITextIndexAdjustAction
> {
+ static Name = 'TextIndexAdjustAction';
+
constructor(
actionData: ITextIndexAdjustAction,
commandUnit: CommandUnit,
@@ -55,3 +62,5 @@ export class TextIndexAdjustAction extends DocActionBase<
return false;
}
}
+
+CommandManager.register(TextIndexAdjustAction.Name, TextIndexAdjustAction);
diff --git a/packages/core/src/Docs/Apply/Common.ts b/packages/core/src/Docs/Apply/Common.ts
index dc45747011..2b50d6f6ee 100644
--- a/packages/core/src/Docs/Apply/Common.ts
+++ b/packages/core/src/Docs/Apply/Common.ts
@@ -4,6 +4,7 @@ import {
IElement,
ITextRun,
} from '../../Interfaces/IDocumentData';
+import { deepCompare } from '../../Shared/Compare';
export function getTextIndexByCursor(index: number, isBack: boolean = false) {
return isBack ? index - 1 : index;
@@ -17,6 +18,11 @@ export function deleteContent(content: string, start: number, end: number) {
if (start > end) {
return content;
}
+
+ if (start === end) {
+ start -= 1;
+ }
+
return content.slice(0, start) + content.slice(end);
}
@@ -57,9 +63,19 @@ export function getDocsUpdateBody(model: IDocumentData, segmentId?: string) {
}
export function isSameStyleTextRun(tr1: ITextRun, tr2: ITextRun) {
- return false;
+ if (!tr1.ts || !tr2.ts) {
+ return false;
+ }
+ return deepCompare(tr1.ts, tr2.ts);
}
export function mergeSameTextRun(mainTr: ITextRun, mergeTr: ITextRun) {
const isSame = isSameStyleTextRun(mainTr, mergeTr);
+ if (!isSame) {
+ return;
+ }
+
+ mainTr.ct += mergeTr.ct || '';
+
+ return mainTr;
}
diff --git a/packages/core/src/Docs/Apply/DeleteTextApply.ts b/packages/core/src/Docs/Apply/DeleteTextApply.ts
index 18722d8fd5..34edd520fb 100644
--- a/packages/core/src/Docs/Apply/DeleteTextApply.ts
+++ b/packages/core/src/Docs/Apply/DeleteTextApply.ts
@@ -65,8 +65,6 @@ function deleteText(
const { elements } = paragraph;
- let preIsDeleted = false;
-
let index = 0;
for (let element of elements) {
@@ -112,11 +110,21 @@ function deleteText(
if (tr.ct?.length === 0) {
elements.splice(index, 1);
- preIsDeleted = true;
- } else if (preIsDeleted === true) {
- const m = mergeSameTextRun(tr, tr);
- }
+ } else {
+ const preTr = elements[index - 1]?.tr;
+
+ if (!preTr) {
+ index++;
+ continue;
+ }
- index++;
+ const m = mergeSameTextRun(tr, preTr);
+
+ if (m) {
+ elements.splice(index, 1);
+ } else {
+ index++;
+ }
+ }
}
}
diff --git a/packages/core/src/Docs/Apply/InsertTextApply.ts b/packages/core/src/Docs/Apply/InsertTextApply.ts
index 0c921b13e6..8f091d452b 100644
--- a/packages/core/src/Docs/Apply/InsertTextApply.ts
+++ b/packages/core/src/Docs/Apply/InsertTextApply.ts
@@ -24,7 +24,7 @@ export function InsertTextApply(
const body = getDocsUpdateBody(doc, segmentId);
- if (length === 0) {
+ if (text.length === 0) {
return;
}
@@ -60,13 +60,13 @@ function insertText(
paragraph: IParagraph,
collapseRange: ITextSelectionRangeStartParam
) {
- const { st, ed } = blockElement;
+ const { st: blockStartIndex, ed: blockEndIndex } = blockElement;
const { cursorStart, isStartBack, segmentId } = collapseRange;
const textStart = getTextIndexByCursor(cursorStart, isStartBack);
- if (textStart > ed || paragraph == null) {
+ if (textStart > blockEndIndex || paragraph == null) {
return;
}
@@ -79,36 +79,32 @@ function insertText(
continue;
}
- const { st, ed, tr } = element;
+ const { st: elementStartIndex, ed: elementEndIndex, tr } = element;
- if (tr == null) {
+ if (tr == null || tr.tab === BooleanNumber.TRUE) {
continue;
}
- if (textStart < st || textStart > ed) {
+ if (textStart < elementStartIndex || textStart > elementEndIndex) {
continue;
}
console.log(
'paragraphApply',
textStart,
- st,
- ed,
+ elementStartIndex,
+ elementEndIndex,
element,
- textStart < st || textStart > ed
+ textStart < elementStartIndex || textStart > elementEndIndex
);
if (paragraphElementType === ParagraphElementType.TEXT_RUN) {
- let relative = textStart - st + 1;
+ let relativeStart = textStart - elementStartIndex + 1;
- if (textStart === 0) {
- relative = 0;
+ if (relativeStart <= 0) {
+ relativeStart = 0;
}
- if (tr.tab === BooleanNumber.TRUE) {
- continue;
- }
-
- const newContent = insertTextToContent(tr.ct || '', relative, text);
+ const newContent = insertTextToContent(tr.ct || '', relativeStart, text);
tr.ct = newContent;
}
diff --git a/packages/core/src/Docs/Apply/TextIndexAdjustApply.ts b/packages/core/src/Docs/Apply/TextIndexAdjustApply.ts
index d6c9b0e2fd..dc161ea112 100644
--- a/packages/core/src/Docs/Apply/TextIndexAdjustApply.ts
+++ b/packages/core/src/Docs/Apply/TextIndexAdjustApply.ts
@@ -29,7 +29,7 @@ export function textIndexAdjustApply(
const { blockElements } = body;
- let preBlockElement: Nullable<IBlockElement> = null;
+ let preParagraphBlockElement: Nullable<IBlockElement> = null;
for (let blockElement of blockElements) {
if (blockElement == null) {
@@ -45,12 +45,11 @@ export function textIndexAdjustApply(
blockElement,
blockElement.paragraph,
range,
- preBlockElement
+ preParagraphBlockElement
);
}
+ preParagraphBlockElement = blockElement;
}
-
- preBlockElement = blockElement;
}
}
@@ -73,12 +72,12 @@ function paragraphIndexAdjust(
const preBlockEnd = preBlockElement ? preBlockElement.ed : 0;
if (cursorEnd < blockStart) {
- const moveIndex = preBlockEnd - blockStart;
+ const moveIndex = preBlockEnd - blockStart + 1;
- moveBlockCharIndex(blockElement, -moveIndex);
+ moveBlockCharIndex(blockElement, moveIndex);
_iteratorElement(elements, (element) => {
- moveElementCharIndex(element, -moveIndex);
+ moveElementCharIndex(element, moveIndex);
});
} else {
let curStart = preBlockEnd + 1;
diff --git a/packages/core/src/Docs/Domain/DocumentModel.ts b/packages/core/src/Docs/Domain/DocumentModel.ts
index 74aeb826c8..6cc3b40ca8 100644
--- a/packages/core/src/Docs/Domain/DocumentModel.ts
+++ b/packages/core/src/Docs/Domain/DocumentModel.ts
@@ -38,7 +38,6 @@ export class DocumentModel {
actionName: DOC_ACTION_NAMES.INSERT_TEXT_ACTION_NAME,
...range,
text,
- length: text.length,
segmentId,
};
const command = new Command(
@@ -75,25 +74,44 @@ export class DocumentModel {
const { _context } = this;
const _commandManager = _context.getCommandManager();
- const deleteTextAction = {
- actionName: DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME,
- text: oldText,
- start: start + oldText.length,
- length: oldText.length,
- segmentId,
- };
+ // const deleteTextAction = {
+ // actionName: DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME,
+ // text: oldText,
+ // start: start + oldText.length,
+ // length: oldText.length,
+ // segmentId,
+ // };
+ const deleteTextActionList = this._getDeleteTextAction(
+ {
+ cursorStart: start,
+ isStartBack: false,
+ isCollapse: false,
+ cursorEnd: start + oldText.length - 1,
+ isEndBack: false,
+ },
+ segmentId
+ );
+ // const insertTextAction = {
+ // actionName: DOC_ACTION_NAMES.INSERT_TEXT_ACTION_NAME,
+ // text: newText,
+ // start,
+ // length: newText.length,
+ // segmentId,
+ // };
+
const insertTextAction = {
actionName: DOC_ACTION_NAMES.INSERT_TEXT_ACTION_NAME,
+ cursorStart: start,
+ isStartBack: false,
text: newText,
- start,
- length: newText.length,
segmentId,
};
+
const command = new Command(
{
DocumentUnit: this,
},
- deleteTextAction,
+ ...deleteTextActionList,
insertTextAction
);
_commandManager.invoke(command);
diff --git a/packages/core/src/Shared/Compare.ts b/packages/core/src/Shared/Compare.ts
new file mode 100644
index 0000000000..34fe9a3f36
--- /dev/null
+++ b/packages/core/src/Shared/Compare.ts
@@ -0,0 +1,23 @@
+type AnyObject = {
+ [key: number | string]: AnyObject | AnyObject[] | Array<[number | string]> | any;
+};
+
+export function deepCompare(arg1: AnyObject, arg2: AnyObject): boolean {
+ if (
+ Object.prototype.toString.call(arg1) === Object.prototype.toString.call(arg2)
+ ) {
+ if (
+ Object.prototype.toString.call(arg1) === '[object Object]' ||
+ Object.prototype.toString.call(arg1) === '[object Array]'
+ ) {
+ if (Object.keys(arg1).length !== Object.keys(arg2).length) {
+ return false;
+ }
+ return Object.keys(arg1).every((key) =>
+ deepCompare(arg1[key] as AnyObject, arg2[key] as AnyObject)
+ );
+ }
+ return arg1 === arg2;
+ }
+ return false;
+}
diff --git a/packages/core/src/Shared/index.ts b/packages/core/src/Shared/index.ts
index 79f2e81255..7434e8fec5 100644
--- a/packages/core/src/Shared/index.ts
+++ b/packages/core/src/Shared/index.ts
@@ -26,4 +26,5 @@ export * from './Tuples';
export * from './Types';
export * from './TypeStore';
export * from './HashAlgorithm';
+export * from './Compare';
// @endindex
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index baffa42823..18d6620f6e 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -13,6 +13,7 @@
*/
import './Sheets/Action';
+import './Docs/Action';
export * from './Command';
export * from './Basics';
diff --git a/packages/ui-plugin-docs/package.json b/packages/ui-plugin-docs/package.json
index f368e43481..317a954362 100644
--- a/packages/ui-plugin-docs/package.json
+++ b/packages/ui-plugin-docs/package.json
@@ -46,10 +46,9 @@
},
"dependencies": {
"@univerjs/base-render": "workspace:^0.0.1",
- "@univerjs/base-docs": "workspace:^0.0.1",
"@univerjs/base-ui": "workspace:^0.0.1",
"@univerjs/core": "workspace:^0.0.1",
"css-vars-ponyfill": "^2.4.6",
"preact": "^10.12.1"
}
-}
\ No newline at end of file
+}
diff --git a/packages/ui-plugin-docs/src/Basics/Observer.ts b/packages/ui-plugin-docs/src/Basics/Observer.ts
new file mode 100644
index 0000000000..86e17a79cd
--- /dev/null
+++ b/packages/ui-plugin-docs/src/Basics/Observer.ts
@@ -0,0 +1,15 @@
+import { Observable } from '@univerjs/core';
+import { DocUIPlugin } from '../DocUIPlugin';
+import { DocContainer } from '../View';
+
+export type DocUIPluginObserve = {
+ onUIDidMount: Observable<DocContainer>;
+};
+
+export function uninstallObserver(plugin: DocUIPlugin) {
+ plugin.deleteObserve('onUIDidMount');
+}
+
+export function installObserver(plugin: DocUIPlugin) {
+ plugin.pushToObserve('onUIDidMount');
+}
diff --git a/packages/ui-plugin-docs/src/Basics/index.ts b/packages/ui-plugin-docs/src/Basics/index.ts
index f0080f0e06..3490f957d5 100644
--- a/packages/ui-plugin-docs/src/Basics/index.ts
+++ b/packages/ui-plugin-docs/src/Basics/index.ts
@@ -1,3 +1,4 @@
export * from './Const';
export * from '../Locale';
export * from './Interfaces';
+export * from './Observer';
diff --git a/packages/ui-plugin-docs/src/Controller/DocContainerUIController.ts b/packages/ui-plugin-docs/src/Controller/DocContainerUIController.ts
index 2eb42f3376..79e3dc0fbe 100644
--- a/packages/ui-plugin-docs/src/Controller/DocContainerUIController.ts
+++ b/packages/ui-plugin-docs/src/Controller/DocContainerUIController.ts
@@ -101,6 +101,10 @@ export class DocContainerUIController {
this._plugin.getObserver('onUIDidMount')?.add(() => cb(this._docContainer));
}
+ getDocContainer() {
+ return this._docContainer;
+ }
+
private _initialize() {
this._eventManager = new EventManager(this._plugin);
diff --git a/packages/ui-plugin-docs/src/DocUIPlugin.ts b/packages/ui-plugin-docs/src/DocUIPlugin.ts
index 37baf05372..aacc5946de 100644
--- a/packages/ui-plugin-docs/src/DocUIPlugin.ts
+++ b/packages/ui-plugin-docs/src/DocUIPlugin.ts
@@ -1,7 +1,6 @@
import { Plugin, Context, UniverDoc, PLUGIN_NAMES, Tools } from '@univerjs/core';
import { RenderEngine } from '@univerjs/base-render';
import { ComponentManager, getRefElement, RegisterManager } from '@univerjs/base-ui';
-import { DocPlugin } from '@univerjs/base-docs';
import { zh, en } from './Locale';
import { DOC_UI_PLUGIN_NAME } from './Basics/Const/PLUGIN_NAME';
import { DefaultDocUiConfig, IDocUIPluginConfig } from './Basics';
@@ -57,7 +56,7 @@ export class DocUIPlugin extends Plugin<any, Context> {
// mount canvas to DOM container
engine.setContainer(container);
- this.getUniver().getCurrentUniverDocInstance().context.getPluginManager().getRequirePluginByName<DocPlugin>(PLUGIN_NAMES.DOCUMENT).calculatePagePosition();
+ // this.getUniver().getCurrentUniverDocInstance().context.getPluginManager().getRequirePluginByName<DocPlugin>(PLUGIN_NAMES.DOCUMENT).calculatePagePosition();
window.addEventListener('resize', () => {
engine.resize();