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): develop doc

DR-Univer100152a4567b
diff --git a/packages/base-docs/src/Controller/InputController.ts b/packages/base-docs/src/Controller/InputController.ts
index badf7f233c..0a27675461 100644
--- a/packages/base-docs/src/Controller/InputController.ts
+++ b/packages/base-docs/src/Controller/InputController.ts
@@ -24,64 +24,99 @@ export class InputController {
const docsModel = this._plugin.context.getDocument();
onKeydownObservable.add((config) => {
- const { event, document, selection } = config;
+ const { event, document, activeSelection, selectionList } = config;
let e = event as KeyboardEvent;
- const cursor = selection?.getCursor() || 0;
+ const activeRange = activeSelection?.getRange();
+
+ const segmentId = activeSelection?.segmentId;
const skeleton = document.getSkeleton();
- if (!skeleton) {
+ if (!skeleton || !activeRange) {
return;
}
+ const { cursorStart, cursorEnd, isCollapse, isEndBack, isStartBack } = activeRange;
+
if (e.key === KeyboardKeyType.backspace) {
- const selectionRemain = document.remainActiveSelection();
+ if (isCollapse) {
+ let cursor = cursorStart;
- const content = document.findNodeByCharIndex(cursor - 1)?.content || '';
+ if (isStartBack === false) {
+ cursor += 1;
+ }
- docsModel.deleteText(content, cursor);
+ const selectionRemain = document.remainActiveSelection();
- skeleton.calculate();
+ // const content = document.findNodeByCharIndex(cursor - 1)?.content || '';
- const span = document.findNodeByCharIndex(cursor - 2);
+ docsModel.deleteText(activeRange, segmentId);
- this._adjustSelection(document, selectionRemain, span);
+ skeleton.calculate();
+
+ const span = document.findNodeByCharIndex(cursor - 2);
- this._resetIME();
+ this._adjustSelection(document, selectionRemain, span);
+
+ this._resetIME();
+ }
}
});
onInputObservable.add((config) => {
- const { event, content = '', document, selection } = config;
-
- const cursor = selection?.getCursor() || 0;
+ const { event, content = '', document, activeSelection, selectionList } = config;
- console.log('cursor', cursor, selection);
+ const activeRange = activeSelection?.getRange();
const skeleton = document.getSkeleton();
- if (!skeleton) {
+ const segmentId = activeSelection?.segmentId;
+
+ if (!skeleton || !activeRange) {
return;
}
- const selectionRemain = document.remainActiveSelection();
+ const { cursorStart, cursorEnd, isCollapse, isEndBack, isStartBack } = activeRange;
- docsModel.insertText(content, cursor);
+ if (isCollapse) {
+ let cursor = cursorStart;
- skeleton.calculate();
+ if (isStartBack === false) {
+ cursor += 1;
+ }
- const span = document.findNodeByCharIndex(cursor + content.length - 1);
+ const selectionRemain = document.remainActiveSelection();
- this._adjustSelection(document, selectionRemain, span);
+ docsModel.insertText(content, activeRange, segmentId);
+
+ skeleton.calculate();
+
+ const span = document.findNodeByCharIndex(cursor + content.length - 1);
+
+ this._adjustSelection(document, selectionRemain, span);
+ }
});
onCompositionstartObservable.add((config) => {
- const { selection } = config;
+ const { activeSelection, selectionList } = config;
+
+ const activeRange = activeSelection?.getRange();
- const cursor = selection?.getCursor() || 0;
+ if (!activeRange) {
+ return;
+ }
- this._previousIMEStart = cursor;
+ const { cursorStart, cursorEnd, isCollapse, isEndBack, isStartBack } = activeRange;
+
+ if (isCollapse) {
+ let cursor = cursorStart;
+
+ if (isStartBack === false) {
+ cursor += 1;
+ }
+ this._previousIMEStart = cursor;
+ }
// console.log('_previousIMEStart', this._previousIMEStart);
});
diff --git a/packages/base-render/src/Basics/Interfaces.ts b/packages/base-render/src/Basics/Interfaces.ts
index e29c6e5b52..a0d9164dca 100644
--- a/packages/base-render/src/Basics/Interfaces.ts
+++ b/packages/base-render/src/Basics/Interfaces.ts
@@ -116,7 +116,8 @@ export interface IEditorInputConfig {
event: Event | CompositionEvent | KeyboardEvent;
content?: string;
document: Documents;
- selection?: TextSelection;
+ activeSelection?: TextSelection;
+ selectionList?: TextSelection[];
}
// export interface IPageConfig {
diff --git a/packages/base-render/src/Component/Docs/Common/TextSelection.ts b/packages/base-render/src/Component/Docs/Common/TextSelection.ts
index cc8e3f5754..968861cdfd 100644
--- a/packages/base-render/src/Component/Docs/Common/TextSelection.ts
+++ b/packages/base-render/src/Component/Docs/Common/TextSelection.ts
@@ -1,4 +1,4 @@
-import { IPosition, Nullable } from '@univer/core';
+import { IPosition, ITextSelectionRange, Nullable } from '@univer/core';
import { nanoid } from 'nanoid';
import { COLORS } from '../../../Basics/Const';
import {
@@ -53,13 +53,6 @@ interface ICurrentNodePositionState {
const TEXT_RANGE_KEY_PREFIX = '__TestSelectionRange__';
const TEXT_ANCHOR_KEY_PREFIX = '__TestSelectionAnchor__';
-
-interface ISelectionCursor {
- cursorStart: number;
- cursorEnd: number;
- isCollapse: boolean;
-}
-
export interface INodePosition {
page: number;
section: number;
@@ -79,7 +72,7 @@ export class TextSelection {
private _anchorShape: Nullable<Rect>;
- private _cursor: number = 0;
+ private _rangeList: ITextSelectionRange[];
private _currentStartState: ICurrentNodePositionState = {
page: NodePositionStateType.NORMAL,
@@ -99,7 +92,7 @@ export class TextSelection {
span: NodePositionStateType.NORMAL,
};
- constructor(private _scene: Scene, public startNodePosition?: Nullable<INodePosition>, public endNodePosition?: Nullable<INodePosition>) {}
+ constructor(private _scene: Scene, public startNodePosition?: Nullable<INodePosition>, public endNodePosition?: Nullable<INodePosition>, public segmentId?: string) {}
private _resetCurrentNodePositionState() {
this._currentStartState = {
@@ -230,8 +223,8 @@ export class TextSelection {
}
private _getSelectionRuler(typeIndex: number, startPosition: INodePosition, endPosition: INodePosition, nextLength: number, current: number) {
- let start_nest = 0;
- let end_nest = nextLength;
+ let start_next = 0;
+ let end_next = nextLength;
const type = NodePositionType[typeIndex] as unknown;
@@ -239,8 +232,8 @@ export class TextSelection {
if (nextType === null || type === null) {
return {
- start_nest,
- end_nest,
+ start_next,
+ end_next,
};
}
@@ -255,16 +248,16 @@ export class TextSelection {
const preEndNestType = this._checkPreviousNodePositionState(typeIndex, false);
if (preStartNestType === NodePositionStateType.START) {
- start_nest = startPosition[nextType] as number;
+ start_next = startPosition[nextType] as number;
}
if (preEndNestType === NodePositionStateType.END) {
- end_nest = endPosition[nextType] as number;
+ end_next = endPosition[nextType] as number;
}
return {
- start_nest,
- end_nest,
+ start_next,
+ end_next,
};
}
@@ -317,6 +310,10 @@ export class TextSelection {
}
}
+ if (start.isBack !== end.isBack) {
+ return false;
+ }
+
return true;
}
@@ -384,7 +381,7 @@ export class TextSelection {
private _getRangePointData(startOrigin: INodePosition, endOrigin: INodePosition, documents: Documents) {
const pointGroup: IPoint[][] = [];
- const cursorList: ISelectionCursor[] = [];
+ const cursorList: ITextSelectionRange[] = [];
if (startOrigin == null || endOrigin == null) {
return {
@@ -395,11 +392,7 @@ export class TextSelection {
const { start, end } = this._compareNodePosition(startOrigin, endOrigin);
- const isStartBack = start.isBack;
-
- const isEndBack = end.isBack;
-
- this._selectionIterator(start!, end!, documents, (start_sp, end_sp, divide, line) => {
+ this._selectionIterator(start!, end!, documents, (start_sp, end_sp, isFirst, isLast, divide, line) => {
const { lineHeight } = line;
const { spanGroup, st } = divide;
@@ -425,9 +418,19 @@ export class TextSelection {
let cursorEnd = end_sp + st;
+ const isStartBack = start.span === start_sp && isFirst ? start.isBack : true;
+
+ const isEndBack = end.span === end_sp && isLast ? end.isBack : false;
+
+ // let isStartBackFin = isStartBack;
+
+ // let isEndBackFin = isEndBack;
+
const isCollapse = start === end;
if (start_sp === 0 && end_sp === spanGroup.length - 1) {
+ cursorEnd -= hasList ? 1 : 0;
+
position = {
startX: startX + firstSpanLeft + (isCurrentList ? firstSpanWidth : 0),
startY,
@@ -435,37 +438,38 @@ export class TextSelection {
endY: startY + lineHeight,
};
} else {
- const isStartBackFin = (isStartBack || (start_sp === 0 && !isCollapse)) && !isCurrentList;
+ const isStartBackFin = isStartBack && !isCurrentList;
- const isEndBackFin = isEndBack && end_sp !== spanGroup.length - 1;
+ // isEndBackFin = isEndBack;
- cursorStart += isStartBackFin ? 0 : 1;
+ // cursorStart += isStartBackFin ? 0 : 1;
- cursorStart -= hasList ? 1 : 0;
+ cursorStart -= hasList && !isCurrentList ? 1 : 0;
- cursorEnd += isEndBackFin ? 0 : 1;
+ // cursorEnd += isEndBackFin ? 0 : 1;
- cursorEnd -= hasList ? 1 : 0;
+ cursorEnd -= hasList && !isCurrentList ? 1 : 0;
- console.log('span', {
- hasList,
- isStartBackFin,
- isEndBackFin,
- firstSpan,
- lastSpan,
- start_sp,
- end_sp,
- isStartBack,
- isEndBack,
- cursorStart,
- cursorEnd,
- isCurrentList,
- });
+ // console.log('span', {
+ // start,
+ // end,
+ // hasList,
+ // isStartBackFin,
+ // firstSpan,
+ // lastSpan,
+ // start_sp,
+ // end_sp,
+ // isStartBack,
+ // isEndBack,
+ // cursorStart,
+ // cursorEnd,
+ // isCurrentList,
+ // });
position = {
startX: startX + firstSpanLeft + (isStartBackFin ? 0 : firstSpanWidth),
startY,
- endX: startX + lastSpanLeft + (isEndBackFin ? 0 : lastSpanWidth),
+ endX: startX + lastSpanLeft + (isEndBack ? 0 : lastSpanWidth),
endY: startY + lineHeight,
};
@@ -477,6 +481,8 @@ export class TextSelection {
pointGroup.push(this._pushToPoints(position));
cursorList.push({
+ isStartBack,
+ isEndBack,
cursorStart,
cursorEnd,
isCollapse,
@@ -496,6 +502,8 @@ export class TextSelection {
func: (
startSpanIndex: number,
endSpanIndex: number,
+ isFirst: boolean,
+ isLast: boolean,
divide: IDocumentSkeletonDivide,
line: IDocumentSkeletonLine,
column: IDocumentSkeletonColumn,
@@ -514,43 +522,43 @@ export class TextSelection {
const pages = skeletonData.pages;
- const { page } = startPosition;
+ const { page: pageIndex } = startPosition;
- const { page: endPage } = endPosition;
+ const { page: endPageIndex } = endPosition;
this._resetCurrentNodePositionState();
- for (let p = 0; p <= page - 1; p++) {
+ for (let p = 0; p <= pageIndex - 1; p++) {
const page = pages[p];
this._Liquid.translatePage(page, documents.pageLayoutType, documents.pageMarginLeft, documents.pageMarginTop);
}
- for (let p = page; p <= endPage; p++) {
+ for (let p = pageIndex; p <= endPageIndex; p++) {
const page = pages[p];
const sections = page.sections;
- const { start_nest: start_s, end_nest: end_s } = this._getSelectionRuler(NodePositionMap.page, startPosition, endPosition, sections.length - 1, p);
-
+ const { start_next: start_s, end_next: end_s } = this._getSelectionRuler(NodePositionMap.page, startPosition, endPosition, sections.length - 1, p);
+ this._Liquid.translateSave();
this._Liquid.translatePagePadding(page);
for (let s = start_s; s <= end_s; s++) {
const section = sections[s];
const columns = section.columns;
- const { start_nest: start_c, end_nest: end_c } = this._getSelectionRuler(NodePositionMap.section, startPosition, endPosition, columns.length - 1, s);
+ const { start_next: start_c, end_next: end_c } = this._getSelectionRuler(NodePositionMap.section, startPosition, endPosition, columns.length - 1, s);
this._Liquid.translateSection(section);
for (let c = start_c; c <= end_c; c++) {
const column = columns[c];
const lines = column.lines;
- const { start_nest: start_l, end_nest: end_l } = this._getSelectionRuler(NodePositionMap.column, startPosition, endPosition, lines.length - 1, c);
+ const { start_next: start_l, end_next: end_l } = this._getSelectionRuler(NodePositionMap.column, startPosition, endPosition, lines.length - 1, c);
this._Liquid.translateColumn(column);
for (let l = start_l; l <= end_l; l++) {
const line = lines[l];
const { divides, type, lineHeight = 0 } = line;
- const { start_nest: start_d, end_nest: end_d } = this._getSelectionRuler(NodePositionMap.line, startPosition, endPosition, divides.length - 1, l);
+ const { start_next: start_d, end_next: end_d } = this._getSelectionRuler(NodePositionMap.line, startPosition, endPosition, divides.length - 1, l);
this._Liquid.translateSave();
this._Liquid.translateLine(line);
@@ -564,9 +572,21 @@ export class TextSelection {
const spanGroup = divide.spanGroup;
- const { start_nest: start_sp, end_nest: end_sp } = this._getSelectionRuler(NodePositionMap.divide, startPosition, endPosition, spanGroup.length - 1, d);
+ const { start_next: start_sp, end_next: end_sp } = this._getSelectionRuler(NodePositionMap.divide, startPosition, endPosition, spanGroup.length - 1, d);
- func && func(start_sp, end_sp, divide, line, column, section, page);
+ let isFirst = false;
+
+ let isLast = false;
+
+ if (p === pageIndex && s === start_s && c === start_c && l === start_l && d === start_d) {
+ isFirst = true;
+ }
+
+ if (p === endPageIndex && s === end_s && c === end_c && l === end_l && d === end_d) {
+ isLast = true;
+ }
+
+ func && func(start_sp, end_sp, isFirst, isLast, divide, line, column, section, page);
this._Liquid.translateRestore();
}
@@ -575,23 +595,46 @@ export class TextSelection {
}
}
}
+
+ this._Liquid.translateRestore();
+ this._Liquid.translatePage(page, documents.pageLayoutType, documents.pageMarginLeft, documents.pageMarginTop);
}
}
- private _setCursor(cursorList: ISelectionCursor[]) {
- if (cursorList.length === 0) {
+ private _setRangeList(rangeList: ITextSelectionRange[]) {
+ if (rangeList.length === 0) {
return;
}
+ // const firstCursor = cursorList[0];
+
+ this._rangeList = rangeList;
+ }
+
+ private _getCursorPosition(index: number, isBack: boolean) {
+ return index - (isBack === true ? 1 : 0);
+ }
+
+ getRange() {
+ const cursorList = this._rangeList;
+
const firstCursor = cursorList[0];
- this._cursor = firstCursor.cursorStart;
+ const lastCursor = cursorList[cursorList.length - 1];
+
+ let isCollapse = cursorList.length === 1 && firstCursor.isCollapse;
- console.log('_setCursor', firstCursor.cursorStart, firstCursor);
+ return {
+ cursorStart: firstCursor.cursorStart,
+ cursorEnd: lastCursor.cursorEnd,
+ isStartBack: firstCursor.isStartBack,
+ isEndBack: lastCursor.isEndBack,
+ isCollapse,
+ };
}
- getCursor() {
- return this._cursor;
+ getRangeList() {
+ return this._rangeList;
}
getAnchor() {
@@ -662,7 +705,23 @@ export class TextSelection {
}
isIntersection(textSelection: TextSelection) {
- return false;
+ const activeRange = this.getRange();
+
+ const compareRange = textSelection.getRange();
+
+ const activeStart = this._getCursorPosition(activeRange.cursorStart, activeRange.isStartBack);
+
+ const activeEnd = this._getCursorPosition(activeRange.cursorEnd, activeRange.isEndBack);
+
+ const compareStart = this._getCursorPosition(compareRange.cursorStart, compareRange.isStartBack);
+
+ const compareEnd = this._getCursorPosition(compareRange.cursorEnd, compareRange.isEndBack);
+
+ if (activeStart > compareEnd || activeEnd < compareStart) {
+ return false;
+ }
+
+ return true;
}
refresh(documents: Documents) {
@@ -680,14 +739,14 @@ export class TextSelection {
if (this.isCollapsed()) {
const data = this._getRangePointData(start!, start!, documents);
const { pointGroup, cursorList } = data;
- this._setCursor(cursorList);
+ this._setRangeList(cursorList);
pointGroup.length > 0 && this._createAndUpdateAnchor(pointGroup, documents.left, documents.top);
return;
}
const data = this._getRangePointData(start!, end!, documents);
const { pointGroup, cursorList } = data;
- this._setCursor(cursorList);
+ this._setRangeList(cursorList);
pointGroup.length > 0 && this._createAndUpdateRange(pointGroup, documents.left, documents.top);
}
diff --git a/packages/base-render/src/Component/Docs/Document.Editor.ts b/packages/base-render/src/Component/Docs/Document.Editor.ts
index 0791195546..de3a20a927 100644
--- a/packages/base-render/src/Component/Docs/Document.Editor.ts
+++ b/packages/base-render/src/Component/Docs/Document.Editor.ts
@@ -231,6 +231,9 @@ export class DocsEditor {
const newTextSelection: TextSelection[] = [];
let hasIntersection = false;
this._textSelectionList.forEach((textSelection) => {
+ if (textSelection === activeTextSelection) {
+ return true;
+ }
if (!activeTextSelection.isIntersection(textSelection)) {
newTextSelection.push(textSelection);
} else {
@@ -280,15 +283,6 @@ export class DocsEditor {
this._textSelectionList = [lastTextSelection];
}
- private _getActiveTextSelection() {
- const list = this._textSelectionList;
- for (let textSelection of list) {
- if (textSelection.isActive()) {
- return textSelection;
- }
- }
- }
-
private _isEmptyTextSelection() {
return this._textSelectionList.length === 0;
}
@@ -313,7 +307,7 @@ export class DocsEditor {
}
private _syncDomToSelection() {
- const activeTextSelection = this._getActiveTextSelection();
+ const activeTextSelection = this.getActiveTextSelection();
const anchor = activeTextSelection?.getAnchor();
if (!anchor || (anchor && !anchor.visible)) {
return;
@@ -349,13 +343,13 @@ export class DocsEditor {
const endPosition = this._getNodePosition(endNode);
- // console.log('endNode', endNode, { moveOffsetX, moveOffsetY, _viewportScrollY: this._viewportScrollY, scrollX });
+ console.log('endNode', endNode, endPosition, { moveOffsetX, moveOffsetY, _viewportScrollY: this._viewportScrollY, scrollX });
if (!endPosition) {
return;
}
- const activeTextSelection = this._getActiveTextSelection();
+ const activeTextSelection = this.getActiveTextSelection();
if (!activeTextSelection) {
return;
@@ -383,7 +377,7 @@ export class DocsEditor {
return;
}
- const textSelection = this._getActiveTextSelection();
+ const textSelection = this.getActiveTextSelection();
textSelection?.activeStatic();
@@ -398,7 +392,7 @@ export class DocsEditor {
const bounds = viewport.getBounding();
- const textSelection = this._getActiveTextSelection();
+ const textSelection = this.getActiveTextSelection();
const anchor = textSelection?.getAnchor();
@@ -434,7 +428,8 @@ export class DocsEditor {
if (this._isIMEInputApply) {
return;
}
- const selection = this._getActiveTextSelection();
+ const activeSelection = this.getActiveTextSelection();
+ const selectionList = this.getTextSelectionList();
if (this._documents == null) {
return;
}
@@ -443,7 +438,8 @@ export class DocsEditor {
event: e,
content: '',
document: this._documents,
- selection,
+ activeSelection,
+ selectionList,
});
});
@@ -456,7 +452,8 @@ export class DocsEditor {
this._input.innerHTML = '';
- const selection = this._getActiveTextSelection();
+ const activeSelection = this.getActiveTextSelection();
+ const selectionList = this.getTextSelectionList();
if (this._documents == null) {
return;
@@ -466,7 +463,8 @@ export class DocsEditor {
event: e,
content,
document: this._documents,
- selection,
+ activeSelection,
+ selectionList,
});
});
@@ -477,7 +475,8 @@ export class DocsEditor {
this._input.innerHTML = '';
- const selection = this._getActiveTextSelection();
+ const activeSelection = this.getActiveTextSelection();
+ const selectionList = this.getTextSelectionList();
if (this._documents == null) {
return;
@@ -487,7 +486,8 @@ export class DocsEditor {
event: e,
content,
document: this._documents,
- selection,
+ activeSelection,
+ selectionList,
});
});
@@ -497,7 +497,8 @@ export class DocsEditor {
this._input.innerHTML = '';
- const selection = this._getActiveTextSelection();
+ const activeSelection = this.getActiveTextSelection();
+ const selectionList = this.getTextSelectionList();
if (this._documents == null) {
return;
@@ -507,7 +508,8 @@ export class DocsEditor {
event: e,
content,
document: this._documents,
- selection,
+ activeSelection,
+ selectionList,
});
});
@@ -516,7 +518,8 @@ export class DocsEditor {
this._input.innerHTML = '';
- const selection = this._getActiveTextSelection();
+ const activeSelection = this.getActiveTextSelection();
+ const selectionList = this.getTextSelectionList();
if (this._documents == null) {
return;
@@ -526,7 +529,8 @@ export class DocsEditor {
event: e,
content,
document: this._documents,
- selection,
+ activeSelection,
+ selectionList,
});
});
}
@@ -555,7 +559,7 @@ export class DocsEditor {
const position = this._getNodePosition(startNode);
- console.log(startNode, evtOffsetX, evtOffsetY);
+ console.log('startNode', startNode, position, evtOffsetX, evtOffsetY);
if (!position) {
this._deleteAllTextSelection();
@@ -620,17 +624,30 @@ export class DocsEditor {
if (!this._documents) {
return;
}
- const activeSelection = this._getActiveTextSelection();
+ const activeSelection = this.getActiveTextSelection();
activeSelection?.refresh(this._documents);
}
+ getActiveTextSelection() {
+ const list = this._textSelectionList;
+ for (let textSelection of list) {
+ if (textSelection.isActive()) {
+ return textSelection;
+ }
+ }
+ }
+
+ getTextSelectionList() {
+ return this._textSelectionList;
+ }
+
add(textSelection: TextSelection) {
this._addTextSelection(textSelection);
}
remain() {
- const activeSelection = this._getActiveTextSelection();
+ const activeSelection = this.getActiveTextSelection();
if (activeSelection == null) {
return;
}
diff --git a/packages/core/src/Command/RegisterAction.ts b/packages/core/src/Command/RegisterAction.ts
index 0267433fe9..0c50cccafd 100644
--- a/packages/core/src/Command/RegisterAction.ts
+++ b/packages/core/src/Command/RegisterAction.ts
@@ -44,8 +44,28 @@ import {
} from '../Sheets/Action';
import { CommandManager } from './CommandManager';
import { DOC_ACTION_NAMES } from '../Const/DOC_ACTION_NAMES';
-import { InsertTextAction } from '../Docs/Action/InsertTextAction';
-import { DeleteTextAction } from '../Docs';
+import {
+ InsertTextAction,
+ DeleteTextAction,
+ ClearTextStyleAction,
+ DeleteParagraphAction,
+ InsertParagraphAction,
+ MergeParagraphAction,
+ MoveParagraphAction,
+ SetTextStyleAction,
+ SplitParagraphAction,
+ UpdateDocumentAction,
+ UpdateParagraphAction,
+ UpdateSectionAction,
+ DeleteBulletAction,
+ DeleteParagraphBulletAction,
+ DeleteSectionAction,
+ InsertBulletAction,
+ InsertParagraphBulletAction,
+ InsertSectionAction,
+ UpdateBulletAction,
+ UpdateParagraphBulletAction,
+} from '../Docs';
/**
* All Commands are registered statically. For each new Command, a static registration method must be added here.
@@ -131,3 +151,84 @@ CommandManager.register(ACTION_NAMES.SET_ZOOM_RATIO_ACTION, SetZoomRatioAction);
CommandManager.register(DOC_ACTION_NAMES.INSERT_TEXT_ACTION_NAME, InsertTextAction);
CommandManager.register(DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME, DeleteTextAction);
+
+CommandManager.register(
+ DOC_ACTION_NAMES.CLEAR_TEXT_STYLE_ACTION_NAME,
+ ClearTextStyleAction
+);
+CommandManager.register(
+ DOC_ACTION_NAMES.DELETE_PARAGRAPH_ACTION_NAME,
+ DeleteParagraphAction
+);
+CommandManager.register(
+ DOC_ACTION_NAMES.INSERT_PARAGRAPH_ACTION_NAME,
+ InsertParagraphAction
+);
+CommandManager.register(
+ DOC_ACTION_NAMES.MERGE_PARAGRAPH_ACTION_NAME,
+ MergeParagraphAction
+);
+CommandManager.register(
+ DOC_ACTION_NAMES.MOVE_PARAGRAPH_ACTION_NAME,
+ MoveParagraphAction
+);
+CommandManager.register(
+ DOC_ACTION_NAMES.SET_TEXT_STYLE_ACTION_NAME,
+ SetTextStyleAction
+);
+CommandManager.register(
+ DOC_ACTION_NAMES.SPLIT_PARAGRAPH_ACTION_NAME,
+ SplitParagraphAction
+);
+CommandManager.register(
+ DOC_ACTION_NAMES.UPDATE_DOCUMENT_STYLE_ACTION_NAME,
+ UpdateDocumentAction
+);
+CommandManager.register(
+ DOC_ACTION_NAMES.UPDATE_PARAGRAPH_STYLE_ACTION_NAME,
+ UpdateParagraphAction
+);
+CommandManager.register(
+ DOC_ACTION_NAMES.UPDATE_SECTION_ACTION_NAME,
+ UpdateSectionAction
+);
+
+CommandManager.register(
+ DOC_ACTION_NAMES.DELETE_BULLET_ACTION_NAME,
+ DeleteBulletAction
+);
+
+CommandManager.register(
+ DOC_ACTION_NAMES.DELETE_PARAGRAPH_BULLET_ACTION_NAME,
+ DeleteParagraphBulletAction
+);
+
+CommandManager.register(
+ DOC_ACTION_NAMES.DELETE_SECTION_ACTION_NAME,
+ DeleteSectionAction
+);
+
+CommandManager.register(
+ DOC_ACTION_NAMES.INSERT_BULLET_ACTION_NAME,
+ InsertBulletAction
+);
+
+CommandManager.register(
+ DOC_ACTION_NAMES.INSERT_PARAGRAPH_BULLET_ACTION_NAME,
+ InsertParagraphBulletAction
+);
+
+CommandManager.register(
+ DOC_ACTION_NAMES.INSERT_SECTION_ACTION_NAME,
+ InsertSectionAction
+);
+
+CommandManager.register(
+ DOC_ACTION_NAMES.UPDATE_BULLET_ACTION_NAME,
+ UpdateBulletAction
+);
+
+CommandManager.register(
+ DOC_ACTION_NAMES.UPDATE_PARAGRAPH_BULLET_ACTION_NAME,
+ UpdateParagraphBulletAction
+);
diff --git a/packages/core/src/Const/DOC_ACTION_NAMES.ts b/packages/core/src/Const/DOC_ACTION_NAMES.ts
index 824f636df8..c22a670c8f 100644
--- a/packages/core/src/Const/DOC_ACTION_NAMES.ts
+++ b/packages/core/src/Const/DOC_ACTION_NAMES.ts
@@ -1,4 +1,23 @@
export enum DOC_ACTION_NAMES {
INSERT_TEXT_ACTION_NAME = 'InsertTextAction',
DELETE_TEXT_ACTION_NAME = 'DeleteTextAction',
+ CLEAR_TEXT_STYLE_ACTION_NAME = 'ClearTextStyleAction',
+ DELETE_PARAGRAPH_ACTION_NAME = 'DeleteParagraphAction',
+ INSERT_PARAGRAPH_ACTION_NAME = 'InsertParagraphAction',
+ MERGE_PARAGRAPH_ACTION_NAME = 'MergeParagraphAction',
+ MOVE_PARAGRAPH_ACTION_NAME = 'MoveParagraphAction',
+ SET_TEXT_STYLE_ACTION_NAME = 'SetTextStyleAction',
+ SPLIT_PARAGRAPH_ACTION_NAME = 'SplitParagraphAction',
+ UPDATE_DOCUMENT_STYLE_ACTION_NAME = 'UpdateDocumentAction',
+ UPDATE_PARAGRAPH_STYLE_ACTION_NAME = 'UpdateParagraphAction',
+ UPDATE_SECTION_ACTION_NAME = 'UpdateSectionAction',
+
+ DELETE_BULLET_ACTION_NAME = 'DeleteBulletAction',
+ DELETE_PARAGRAPH_BULLET_ACTION_NAME = 'DeleteParagraphBulletAction',
+ DELETE_SECTION_ACTION_NAME = 'DeleteSectionAction',
+ INSERT_BULLET_ACTION_NAME = 'InsertBulletAction',
+ INSERT_PARAGRAPH_BULLET_ACTION_NAME = 'InsertParagraphBulletAction',
+ INSERT_SECTION_ACTION_NAME = 'InsertSectionAction',
+ UPDATE_BULLET_ACTION_NAME = 'UpdateBulletAction',
+ UPDATE_PARAGRAPH_BULLET_ACTION_NAME = 'UpdateParagraphBulletAction',
}
diff --git a/packages/core/src/Docs/Action/DeleteBulletAction.ts b/packages/core/src/Docs/Action/DeleteBulletAction.ts
new file mode 100644
index 0000000000..1f9a22d06c
--- /dev/null
+++ b/packages/core/src/Docs/Action/DeleteBulletAction.ts
@@ -0,0 +1,41 @@
+import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
+import { ActionObservers, CommandUnit } from '../../Command';
+
+export interface IDeleteBulletAction extends IDocActionData {
+ text: string;
+}
+
+export class DeleteBulletAction extends DocActionBase<
+ IDeleteBulletAction,
+ IDeleteBulletAction
+> {
+ constructor(
+ actionData: IDeleteBulletAction,
+ commandUnit: CommandUnit,
+ observers: ActionObservers
+ ) {
+ super(actionData, commandUnit, observers);
+ this._doActionData = { ...actionData };
+ this.do();
+ this._oldActionData = { ...actionData };
+ }
+
+ redo(): void {
+ this.do();
+ }
+
+ do(): void {
+ const actionData = this.getDoActionData();
+ const document = this.getDocument();
+ // InsertTextApply(document, actionData.text);
+ }
+
+ undo(): void {
+ // TODO ...
+ // ...
+ }
+
+ validate(): boolean {
+ return false;
+ }
+}
diff --git a/packages/core/src/Docs/Action/DeleteParagraphBulletAction.ts b/packages/core/src/Docs/Action/DeleteParagraphBulletAction.ts
new file mode 100644
index 0000000000..f96fc75c9c
--- /dev/null
+++ b/packages/core/src/Docs/Action/DeleteParagraphBulletAction.ts
@@ -0,0 +1,41 @@
+import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
+import { ActionObservers, CommandUnit } from '../../Command';
+
+export interface IDeleteParagraphBulletAction extends IDocActionData {
+ text: string;
+}
+
+export class DeleteParagraphBulletAction extends DocActionBase<
+ IDeleteParagraphBulletAction,
+ IDeleteParagraphBulletAction
+> {
+ constructor(
+ actionData: IDeleteParagraphBulletAction,
+ commandUnit: CommandUnit,
+ observers: ActionObservers
+ ) {
+ super(actionData, commandUnit, observers);
+ this._doActionData = { ...actionData };
+ this.do();
+ this._oldActionData = { ...actionData };
+ }
+
+ redo(): void {
+ this.do();
+ }
+
+ do(): void {
+ const actionData = this.getDoActionData();
+ const document = this.getDocument();
+ // InsertTextApply(document, actionData.text);
+ }
+
+ undo(): void {
+ // TODO ...
+ // ...
+ }
+
+ validate(): boolean {
+ return false;
+ }
+}
diff --git a/packages/core/src/Docs/Action/DeleteSectionAction.ts b/packages/core/src/Docs/Action/DeleteSectionAction.ts
new file mode 100644
index 0000000000..fa120ef28c
--- /dev/null
+++ b/packages/core/src/Docs/Action/DeleteSectionAction.ts
@@ -0,0 +1,41 @@
+import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
+import { ActionObservers, CommandUnit } from '../../Command';
+
+export interface IDeleteSectionAction extends IDocActionData {
+ text: string;
+}
+
+export class DeleteSectionAction extends DocActionBase<
+ IDeleteSectionAction,
+ IDeleteSectionAction
+> {
+ constructor(
+ actionData: IDeleteSectionAction,
+ commandUnit: CommandUnit,
+ observers: ActionObservers
+ ) {
+ super(actionData, commandUnit, observers);
+ this._doActionData = { ...actionData };
+ this.do();
+ this._oldActionData = { ...actionData };
+ }
+
+ redo(): void {
+ this.do();
+ }
+
+ do(): void {
+ const actionData = this.getDoActionData();
+ const document = this.getDocument();
+ // InsertTextApply(document, actionData.text);
+ }
+
+ undo(): void {
+ // TODO ...
+ // ...
+ }
+
+ validate(): boolean {
+ return false;
+ }
+}
diff --git a/packages/core/src/Docs/Action/InsertBulletAction.ts b/packages/core/src/Docs/Action/InsertBulletAction.ts
new file mode 100644
index 0000000000..eaa69f94f3
--- /dev/null
+++ b/packages/core/src/Docs/Action/InsertBulletAction.ts
@@ -0,0 +1,41 @@
+import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
+import { ActionObservers, CommandUnit } from '../../Command';
+
+export interface IInsertBulletAction extends IDocActionData {
+ text: string;
+}
+
+export class InsertBulletAction extends DocActionBase<
+ IInsertBulletAction,
+ IInsertBulletAction
+> {
+ constructor(
+ actionData: IInsertBulletAction,
+ commandUnit: CommandUnit,
+ observers: ActionObservers
+ ) {
+ super(actionData, commandUnit, observers);
+ this._doActionData = { ...actionData };
+ this.do();
+ this._oldActionData = { ...actionData };
+ }
+
+ redo(): void {
+ this.do();
+ }
+
+ do(): void {
+ const actionData = this.getDoActionData();
+ const document = this.getDocument();
+ // InsertTextApply(document, actionData.text);
+ }
+
+ undo(): void {
+ // TODO ...
+ // ...
+ }
+
+ validate(): boolean {
+ return false;
+ }
+}
diff --git a/packages/core/src/Docs/Action/InsertParagraphAction.ts b/packages/core/src/Docs/Action/InsertParagraphAction.ts
new file mode 100644
index 0000000000..fc749c698b
--- /dev/null
+++ b/packages/core/src/Docs/Action/InsertParagraphAction.ts
@@ -0,0 +1,41 @@
+import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
+import { ActionObservers, CommandUnit } from '../../Command';
+
+export interface IInsertParagraphAction extends IDocActionData {
+ text: string;
+}
+
+export class InsertParagraphAction extends DocActionBase<
+ IInsertParagraphAction,
+ IInsertParagraphAction
+> {
+ constructor(
+ actionData: IInsertParagraphAction,
+ commandUnit: CommandUnit,
+ observers: ActionObservers
+ ) {
+ super(actionData, commandUnit, observers);
+ this._doActionData = { ...actionData };
+ this.do();
+ this._oldActionData = { ...actionData };
+ }
+
+ redo(): void {
+ this.do();
+ }
+
+ do(): void {
+ const actionData = this.getDoActionData();
+ const document = this.getDocument();
+ // InsertTextApply(document, actionData.text);
+ }
+
+ undo(): void {
+ // TODO ...
+ // ...
+ }
+
+ validate(): boolean {
+ return false;
+ }
+}
diff --git a/packages/core/src/Docs/Action/InsertParagraphBulletAction.ts b/packages/core/src/Docs/Action/InsertParagraphBulletAction.ts
new file mode 100644
index 0000000000..254789c55e
--- /dev/null
+++ b/packages/core/src/Docs/Action/InsertParagraphBulletAction.ts
@@ -0,0 +1,41 @@
+import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
+import { ActionObservers, CommandUnit } from '../../Command';
+
+export interface IInsertParagraphBulletAction extends IDocActionData {
+ text: string;
+}
+
+export class InsertParagraphBulletAction extends DocActionBase<
+ IInsertParagraphBulletAction,
+ IInsertParagraphBulletAction
+> {
+ constructor(
+ actionData: IInsertParagraphBulletAction,
+ commandUnit: CommandUnit,
+ observers: ActionObservers
+ ) {
+ super(actionData, commandUnit, observers);
+ this._doActionData = { ...actionData };
+ this.do();
+ this._oldActionData = { ...actionData };
+ }
+
+ redo(): void {
+ this.do();
+ }
+
+ do(): void {
+ const actionData = this.getDoActionData();
+ const document = this.getDocument();
+ // InsertTextApply(document, actionData.text);
+ }
+
+ undo(): void {
+ // TODO ...
+ // ...
+ }
+
+ validate(): boolean {
+ return false;
+ }
+}
diff --git a/packages/core/src/Docs/Action/InsertSectionAction.ts b/packages/core/src/Docs/Action/InsertSectionAction.ts
new file mode 100644
index 0000000000..5d807eeaac
--- /dev/null
+++ b/packages/core/src/Docs/Action/InsertSectionAction.ts
@@ -0,0 +1,41 @@
+import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
+import { ActionObservers, CommandUnit } from '../../Command';
+
+export interface IInsertSectionActionData extends IDocActionData {
+ text: string;
+}
+
+export class InsertSectionAction extends DocActionBase<
+ IInsertSectionActionData,
+ IInsertSectionActionData
+> {
+ constructor(
+ actionData: IInsertSectionActionData,
+ commandUnit: CommandUnit,
+ observers: ActionObservers
+ ) {
+ super(actionData, commandUnit, observers);
+ this._doActionData = { ...actionData };
+ this.do();
+ this._oldActionData = { ...actionData };
+ }
+
+ redo(): void {
+ this.do();
+ }
+
+ do(): void {
+ const actionData = this.getDoActionData();
+ const document = this.getDocument();
+ // InsertTextApply(document, actionData.text);
+ }
+
+ undo(): void {
+ // TODO ...
+ // ...
+ }
+
+ validate(): boolean {
+ return false;
+ }
+}
diff --git a/packages/core/src/Docs/Action/UpdateBulletAction.ts b/packages/core/src/Docs/Action/UpdateBulletAction.ts
new file mode 100644
index 0000000000..28afb89ab9
--- /dev/null
+++ b/packages/core/src/Docs/Action/UpdateBulletAction.ts
@@ -0,0 +1,41 @@
+import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
+import { ActionObservers, CommandUnit } from '../../Command';
+
+export interface IUpdateBulletAction extends IDocActionData {
+ text: string;
+}
+
+export class UpdateBulletAction extends DocActionBase<
+ IUpdateBulletAction,
+ IUpdateBulletAction
+> {
+ constructor(
+ actionData: IUpdateBulletAction,
+ commandUnit: CommandUnit,
+ observers: ActionObservers
+ ) {
+ super(actionData, commandUnit, observers);
+ this._doActionData = { ...actionData };
+ this.do();
+ this._oldActionData = { ...actionData };
+ }
+
+ redo(): void {
+ this.do();
+ }
+
+ do(): void {
+ const actionData = this.getDoActionData();
+ const document = this.getDocument();
+ // InsertTextApply(document, actionData.text);
+ }
+
+ undo(): void {
+ // TODO ...
+ // ...
+ }
+
+ validate(): boolean {
+ return false;
+ }
+}
diff --git a/packages/core/src/Docs/Action/UpdateParagraphBulletAction.ts b/packages/core/src/Docs/Action/UpdateParagraphBulletAction.ts
new file mode 100644
index 0000000000..2b64d249fd
--- /dev/null
+++ b/packages/core/src/Docs/Action/UpdateParagraphBulletAction.ts
@@ -0,0 +1,41 @@
+import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
+import { ActionObservers, CommandUnit } from '../../Command';
+
+export interface IUpdateParagraphBulletAction extends IDocActionData {
+ text: string;
+}
+
+export class UpdateParagraphBulletAction extends DocActionBase<
+ IUpdateParagraphBulletAction,
+ IUpdateParagraphBulletAction
+> {
+ constructor(
+ actionData: IUpdateParagraphBulletAction,
+ commandUnit: CommandUnit,
+ observers: ActionObservers
+ ) {
+ super(actionData, commandUnit, observers);
+ this._doActionData = { ...actionData };
+ this.do();
+ this._oldActionData = { ...actionData };
+ }
+
+ redo(): void {
+ this.do();
+ }
+
+ do(): void {
+ const actionData = this.getDoActionData();
+ const document = this.getDocument();
+ // InsertTextApply(document, actionData.text);
+ }
+
+ undo(): void {
+ // TODO ...
+ // ...
+ }
+
+ validate(): boolean {
+ return false;
+ }
+}
diff --git a/packages/core/src/Docs/Action/index.ts b/packages/core/src/Docs/Action/index.ts
index dbf5cdcf6d..3eaac86fff 100644
--- a/packages/core/src/Docs/Action/index.ts
+++ b/packages/core/src/Docs/Action/index.ts
@@ -9,3 +9,13 @@ export * from './SplitParagraphAction';
export * from './UpdateDocumentAction';
export * from './UpdateParagraphAction';
export * from './UpdateSectionAction';
+export * from './InsertParagraphAction';
+
+export * from './DeleteBulletAction';
+export * from './DeleteParagraphBulletAction';
+export * from './DeleteSectionAction';
+export * from './InsertBulletAction';
+export * from './InsertParagraphBulletAction';
+export * from './InsertSectionAction';
+export * from './UpdateBulletAction';
+export * from './UpdateParagraphBulletAction';
diff --git a/packages/core/src/Docs/Apply/Common.ts b/packages/core/src/Docs/Apply/Common.ts
index 4ff7521059..4bb1dcac35 100644
--- a/packages/core/src/Docs/Apply/Common.ts
+++ b/packages/core/src/Docs/Apply/Common.ts
@@ -1,4 +1,8 @@
-import { IBlockElement, IElement } from '../../Interfaces/IDocumentData';
+import {
+ IBlockElement,
+ IDocumentData,
+ IElement,
+} from '../../Interfaces/IDocumentData';
export function getTextStartByAnchor(start: number) {
let textStart = start - 1;
@@ -37,3 +41,18 @@ export function moveBlockCharIndex(
blockElement.st += moveIndex;
blockElement.ed += moveIndex;
}
+
+export function getDocsUpdateBody(model: IDocumentData, segmentId?: string) {
+ let body = model.body;
+
+ if (segmentId) {
+ const { headers, footers } = model;
+ if (headers?.[segmentId]) {
+ body = headers[segmentId].body;
+ } else if (footers?.[segmentId]) {
+ body = footers[segmentId].body;
+ }
+ }
+
+ return body;
+}
diff --git a/packages/core/src/Docs/Domain/DocumentModel.ts b/packages/core/src/Docs/Domain/DocumentModel.ts
index 2bcb35bccb..f1a27058a8 100644
--- a/packages/core/src/Docs/Domain/DocumentModel.ts
+++ b/packages/core/src/Docs/Domain/DocumentModel.ts
@@ -1,9 +1,10 @@
import { nanoid } from 'nanoid';
import { DocContext } from '../../Basics';
import { Command } from '../../Command';
-import { IDocumentData } from '../../Interfaces';
+import { BlockType, IDocumentData, ITextSelectionRange } from '../../Interfaces';
import { DEFAULT_DOC } from '../../Const';
import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';
+import { getDocsUpdateBody } from '../Apply/Common';
export class DocumentModel {
private _snapshot: IDocumentData;
@@ -26,13 +27,17 @@ export class DocumentModel {
return this._unitId;
}
- insertText(text: string, start: number, segmentId?: string): DocumentModel {
+ insertText(
+ text: string,
+ range: ITextSelectionRange,
+ segmentId?: string
+ ): DocumentModel {
const { _context } = this;
const _commandManager = _context.getCommandManager();
const insertTextAction = {
actionName: DOC_ACTION_NAMES.INSERT_TEXT_ACTION_NAME,
+ ...range,
text,
- start,
length: text.length,
segmentId,
};
@@ -46,14 +51,12 @@ export class DocumentModel {
return this;
}
- deleteText(text: string, start: number, segmentId?: string): DocumentModel {
+ deleteText(range: ITextSelectionRange, segmentId?: string): DocumentModel {
const { _context } = this;
const _commandManager = _context.getCommandManager();
const deleteTextAction = {
actionName: DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME,
- text,
- start,
- length: text.length,
+ ...range,
segmentId,
};
const command = new Command(
@@ -94,4 +97,74 @@ export class DocumentModel {
_commandManager.invoke(command);
return this;
}
+
+ private _getDeleteTextAction(range: ITextSelectionRange, segmentId?: string) {
+ const snapshot = this._snapshot;
+
+ const body = getDocsUpdateBody(snapshot, segmentId);
+
+ if (body == null) {
+ return [];
+ }
+
+ const { blockElements, blockElementOrder } = body;
+
+ const { cursorStart, cursorEnd, isCollapse, isEndBack, isStartBack } = range;
+
+ const actionList = [];
+
+ for (let blockId of blockElementOrder) {
+ const blockElement = blockElements[blockId];
+ if (blockElement == null) {
+ continue;
+ }
+
+ const { blockType, st, ed } = blockElement;
+
+ if (blockType === BlockType.PARAGRAPH) {
+ if (cursorStart > ed || cursorEnd < st) {
+ continue;
+ }
+
+ if (cursorStart === st && isCollapse && isStartBack) {
+ // bullet
+ actionList.push({
+ actionName: DOC_ACTION_NAMES.DELETE_BULLET_ACTION_NAME,
+ cursorStart: st,
+ cursorEnd: ed,
+ isCollapse: false,
+ isEndBack: false,
+ isStartBack: true,
+ blockId,
+ blockElement,
+ segmentId,
+ });
+ } else if (
+ (cursorStart < st || (cursorStart === st && isStartBack)) &&
+ (cursorEnd > ed || (cursorEnd === ed && !isEndBack)) &&
+ !isCollapse
+ ) {
+ // Paragraph
+ actionList.push({
+ actionName: DOC_ACTION_NAMES.DELETE_PARAGRAPH_ACTION_NAME,
+ cursorStart: st,
+ cursorEnd: ed,
+ isCollapse: false,
+ isEndBack: false,
+ isStartBack: true,
+ blockId,
+ blockElement,
+ segmentId,
+ });
+ } else {
+ // textRun
+ actionList.push({
+ actionName: DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME,
+ ...range,
+ segmentId,
+ });
+ }
+ }
+ }
+ }
}
diff --git a/packages/core/src/Interfaces/ISelectionData.ts b/packages/core/src/Interfaces/ISelectionData.ts
index d264dda3bd..20b3d24100 100644
--- a/packages/core/src/Interfaces/ISelectionData.ts
+++ b/packages/core/src/Interfaces/ISelectionData.ts
@@ -28,3 +28,11 @@ export interface ICellInfo extends IPosition {
}
export interface ISelection extends IPosition, IRangeData {}
+
+export interface ITextSelectionRange {
+ cursorStart: number;
+ cursorEnd: number;
+ isStartBack: boolean;
+ isEndBack: boolean;
+ isCollapse: boolean;
+}