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.

Developer doc editor third (#98)

Univer088390d275b2
diff --git a/packages/base-docs/src/Controller/DocumentController.ts b/packages/base-docs/src/Controller/DocumentController.ts
index 1cc5c9b29a..6b29a22ff0 100644
--- a/packages/base-docs/src/Controller/DocumentController.ts
+++ b/packages/base-docs/src/Controller/DocumentController.ts
@@ -1,10 +1,10 @@
-import { Injector } from '@wendellhu/redi';
+import { Injector, Inject } from '@wendellhu/redi';
import { InputController } from './InputController';
export class DocumentController {
private readonly _inputController: InputController;
- constructor(injector: Injector) {
- this._inputController = injector.createInstance(InputController) as InputController;
+ constructor(@Inject(Injector) private readonly _injector: Injector) {
+ this._inputController = this._injector.createInstance(InputController);
}
}
diff --git a/packages/base-docs/src/Controller/InputController.ts b/packages/base-docs/src/Controller/InputController.ts
index ca345fc990..e9ea9d19ab 100644
--- a/packages/base-docs/src/Controller/InputController.ts
+++ b/packages/base-docs/src/Controller/InputController.ts
@@ -1,5 +1,19 @@
-import { Documents, IDocumentSkeletonLine, IDocumentSkeletonSpan, IEditorInputConfig, SpanType, TextSelection } from '@univerjs/base-render';
-import { DataStreamTreeTokenType, DocumentModel, ICurrentUniverService, IParagraph, Nullable, Observable } from '@univerjs/core';
+import {
+ Documents,
+ IDocumentSkeletonLine,
+ IDocumentSkeletonSpan,
+ IEditorInputConfig,
+ INodePosition,
+ INodeSearch,
+ TextSelection,
+ getParagraphBySpan,
+ hasListSpan,
+ isFirstSpan,
+ isIndentBySpan,
+ isPlaceholderOrSpace,
+ isSameLine,
+} from '@univerjs/base-render';
+import { DataStreamTreeTokenType, DocumentModel, ICurrentUniverService, IParagraph, Nullable, Observable, UpdateDocsAttributeType } from '@univerjs/core';
import { Inject } from '@wendellhu/redi';
import { CanvasView } from '../View/Render/CanvasView';
import { DocsView } from '../View/Render/Views';
@@ -21,16 +35,14 @@ export class InputController {
private _previousIMEStart: number;
- private _currentSpan: Nullable<IDocumentSkeletonSpan>;
+ private _currentNodePosition: Nullable<INodePosition>;
- private _currentSpanPosBack: boolean = false;
-
- constructor(@Inject(CanvasView) private readonly _CanvasView: CanvasView, @ICurrentUniverService private readonly _currentUniverService: ICurrentUniverService) {
+ constructor(@Inject(CanvasView) private readonly _canvasView: CanvasView, @ICurrentUniverService private readonly _currentUniverService: ICurrentUniverService) {
this._initialize();
}
private _initialize() {
- const docsView = this._CanvasView.getDocsView() as DocsView;
+ const docsView = this._canvasView.getDocsView() as DocsView;
const events = docsView.getDocs().getEditorInputEvent();
const docsModel = this._currentUniverService.getCurrentUniverDocInstance().getDocument();
@@ -47,12 +59,8 @@ export class InputController {
this._initialComposition(onCompositionstartObservable, onCompositionupdateObservable, onCompositionendObservable, docsModel);
- onSelectionStartObservable.add((nodeInfo) => {
- if (nodeInfo === false) {
- this._currentSpan = null;
- } else {
- this._currentSpan = nodeInfo.node;
- }
+ onSelectionStartObservable.add((nodePosition) => {
+ this._currentNodePosition = nodePosition;
});
}
@@ -67,6 +75,8 @@ export class InputController {
const activeRange = activeSelection?.getRange();
+ const startNodePosition = activeSelection?.getStart();
+
const segmentId = activeSelection?.segmentId;
const skeleton = document.getSkeleton();
@@ -78,6 +88,12 @@ export class InputController {
const { cursorStart, cursorEnd, isCollapse, isEndBack, isStartBack } = activeRange;
if (e.key === KeyboardKeyType.Backspace) {
+ const preSpan = document.findSpanByPosition(startNodePosition);
+
+ const preIsBullet = hasListSpan(preSpan);
+
+ const preIsIndent = isIndentBySpan(preSpan, docsModel.body);
+
let cursor = cursorStart;
if (isStartBack === true) {
@@ -88,17 +104,79 @@ export class InputController {
cursor += 1;
}
+ let span = document.findNodeByCharIndex(cursor);
+
const selectionRemain = document.remainActiveSelection() as TextSelection | undefined;
// const content = document.findNodeByCharIndex(cursor - 1)?.content || '';
-
- docsModel.delete(activeRange, segmentId);
+ const isUpdateParagraph = isFirstSpan(preSpan) && span !== preSpan && (preIsBullet === true || preIsIndent === true);
+ if (isUpdateParagraph) {
+ const paragraph = getParagraphBySpan(preSpan, docsModel.body);
+
+ if (paragraph == null) {
+ return;
+ }
+
+ const paragraphIndex = paragraph?.startIndex;
+
+ const updateParagraph: IParagraph = { startIndex: 0 };
+
+ const paragraphStyle = paragraph.paragraphStyle;
+ if (preIsBullet === true) {
+ const paragraphStyle = paragraph.paragraphStyle;
+ if (paragraphStyle) {
+ updateParagraph.paragraphStyle = paragraphStyle;
+ }
+ } else if (preIsIndent === true) {
+ const bullet = paragraph.bullet;
+ if (bullet) {
+ updateParagraph.bullet = bullet;
+ }
+ if (paragraphStyle != null) {
+ updateParagraph.paragraphStyle = { ...paragraphStyle };
+ delete updateParagraph.paragraphStyle.hanging;
+ delete updateParagraph.paragraphStyle.indentStart;
+ }
+ }
+
+ docsModel.update(
+ { dataStream: '', paragraphs: [{ ...updateParagraph }] },
+ {
+ cursorStart: paragraphIndex,
+ cursorEnd: paragraphIndex,
+ isEndBack: false,
+ isStartBack: false,
+ isCollapse: true,
+ },
+ UpdateDocsAttributeType.REPLACE,
+ segmentId
+ );
+ } else {
+ const endNodePosition = activeSelection?.getEnd();
+ if (endNodePosition != null) {
+ const endSpan = document.findSpanByPosition(endNodePosition);
+ if (hasListSpan(endSpan) && !isSameLine(preSpan, endSpan)) {
+ activeRange.cursorEnd -= 1;
+ }
+ }
+ docsModel.delete(activeRange, segmentId);
+ }
skeleton.calculate();
- const span = document.findNodeByCharIndex(cursor - 1);
+ let isBack = false;
- this._adjustSelection(document as Documents, selectionRemain, span);
+ if (isUpdateParagraph) {
+ isBack = true;
+ cursor++;
+ } else if (isFirstSpan(span)) {
+ isBack = true;
+ } else {
+ cursor--;
+ }
+ span = document.findNodeByCharIndex(cursor);
+
+ this._adjustSelection(document as Documents, selectionRemain, span, isBack);
this._resetIME();
} else if (e.key === KeyboardKeyType.Enter) {
@@ -122,58 +200,102 @@ export class InputController {
skeleton.calculate();
- const span = document.findNodeByCharIndex(cursor + 1);
+ const span = document.findNodeByCharIndex(++cursor);
this._adjustSelection(document as Documents, selectionRemain, span, true);
} else if (e.key === KeyboardKeyType.ArrowUp) {
- let cursor = cursorStart;
- if (isStartBack === true) {
- cursor -= 1;
- }
- const span = document.findNodeByCharIndex(cursor);
- const newSpan = this._getTopOrBottomSpan(span, false);
- if (newSpan == null) {
+ const preSpan = document.findSpanByPosition(startNodePosition);
+ // let cursor = cursorStart;
+ // if (isStartBack === true) {
+ // cursor -= 1;
+ // }
+ // const span = document.findNodeByCharIndex(cursor);
+ const newPos = this._getTopOrBottomPosition(document as Documents, preSpan, false);
+ if (newPos == null) {
return;
}
const selectionRemain = document.remainActiveSelection() as TextSelection | undefined;
- this._adjustSelection(document as Documents, selectionRemain, newSpan, false, true);
- } else if (e.key === KeyboardKeyType.ArrowDown) {
- let cursor = cursorStart;
- if (isStartBack === true) {
- cursor -= 1;
+
+ if (selectionRemain == null) {
+ return;
}
- const span = document.findNodeByCharIndex(cursor);
- const newSpan = this._getTopOrBottomSpan(span, true);
- if (newSpan == null) {
+
+ this._syncSelection(document as Documents, selectionRemain, newPos, true);
+ // this._adjustSelection(document as Documents, selectionRemain, newSpan, false, true);
+ } else if (e.key === KeyboardKeyType.ArrowDown) {
+ const preSpan = document.findSpanByPosition(startNodePosition);
+ // let cursor = cursorStart;
+ // if (isStartBack === true) {
+ // cursor -= 1;
+ // }
+
+ // const span = document.findNodeByCharIndex(cursor);
+ const newPos = this._getTopOrBottomPosition(document as Documents, preSpan, true);
+ if (newPos == null) {
return;
}
const selectionRemain = document.remainActiveSelection() as TextSelection | undefined;
- this._adjustSelection(document as Documents, selectionRemain, newSpan, false, true);
+ if (selectionRemain == null) {
+ return;
+ }
+ this._syncSelection(document as Documents, selectionRemain, newPos, true);
+ // this._adjustSelection(document as Documents, selectionRemain, newSpan, false, true);
} else if (e.key === KeyboardKeyType.ArrowLeft) {
let cursor = cursorStart;
if (isStartBack === true) {
cursor -= 1;
}
- const span = document.findNodeByCharIndex(cursor - 1);
- this._currentSpan = span;
+
+ const preSpan = document.findSpanByPosition(startNodePosition);
+ let span: Nullable<IDocumentSkeletonSpan>;
+ let isBack = false;
+ if (isFirstSpan(preSpan)) {
+ span = document.findNodeByCharIndex(cursor);
+
+ if (preSpan === span) {
+ isBack = true;
+ }
+
+ while (isPlaceholderOrSpace(span)) {
+ span = document.findNodeByCharIndex(--cursor);
+ }
+ } else {
+ span = document.findNodeByCharIndex(--cursor);
+ }
+
if (span == null) {
return;
}
const selectionRemain = document.remainActiveSelection() as TextSelection | undefined;
- this._adjustSelection(document as Documents, selectionRemain, span, false, true);
+ this._adjustSelection(document as Documents, selectionRemain, span, isBack, true);
} else if (e.key === KeyboardKeyType.ArrowRight) {
- // copy handler
+ const preSpan = document.findSpanByPosition(startNodePosition);
+
let cursor = cursorStart;
if (isStartBack === true) {
cursor -= 1;
}
- const span = document.findNodeByCharIndex(cursor + 1);
- this._currentSpan = span;
+
+ let span = document.findNodeByCharIndex(++cursor);
+
+ const originCursor = cursor;
+
+ while (isPlaceholderOrSpace(span)) {
+ span = document.findNodeByCharIndex(++cursor);
+ }
+
+ let isBack = false;
+ if (isFirstSpan(span) && preSpan !== span) {
+ isBack = true;
+ } else {
+ span = document.findNodeByCharIndex(originCursor);
+ }
+
if (span == null) {
return;
}
const selectionRemain = document.remainActiveSelection() as TextSelection | undefined;
- this._adjustSelection(document as Documents, selectionRemain, span, false, true);
+ this._adjustSelection(document as Documents, selectionRemain, span, isBack, true);
} else if (e.key === KeyboardKeyType.Copy && isCtrl) {
// copy handler
} else if (e.key === KeyboardKeyType.Paste && isCtrl) {
@@ -216,7 +338,9 @@ export class InputController {
skeleton.calculate();
- const span = document.findNodeByCharIndex(cursor + content.length - 1);
+ cursor += content.length - 1;
+
+ const span = document.findNodeByCharIndex(cursor);
this._adjustSelection(document as Documents, selectionRemain, span);
});
@@ -250,7 +374,7 @@ export class InputController {
onCompositionupdateObservable.add((config) => {
const { event, document, activeSelection } = config;
- const cursor = this._previousIMEStart;
+ let cursor = this._previousIMEStart;
const skeleton = document.getSkeleton();
@@ -274,7 +398,9 @@ export class InputController {
skeleton.calculate();
- const span = document.findNodeByCharIndex(cursor + content.length);
+ cursor += content.length;
+
+ const span = document.findNodeByCharIndex(cursor);
this._adjustSelection(document as Documents, selectionRemain, span);
@@ -286,8 +412,8 @@ export class InputController {
});
}
- private _getTopOrBottomSpan(span: Nullable<IDocumentSkeletonSpan>, direction: boolean) {
- const referenceSpan = this._currentSpan;
+ private _getTopOrBottomPosition(document: Documents, span: Nullable<IDocumentSkeletonSpan>, direction: boolean): Nullable<INodePosition> {
+ const referenceSpan = document.findSpanByPosition(this._currentNodePosition);
if (referenceSpan == null || span == null) {
return;
}
@@ -300,9 +426,17 @@ export class InputController {
return;
}
- const newSpan = this._matchSpanByLeftOffset(line, offsetLeft);
+ const position: Nullable<INodeSearch> = this._matchPositionByLeftOffset(document, line, offsetLeft);
- return newSpan;
+ let isBack = this._currentNodePosition?.isBack;
+
+ isBack = isBack == null ? false : isBack;
+
+ if (position == null) {
+ return;
+ }
+
+ return { ...position, isBack };
}
private _getSpanLeftOffsetInLine(span: IDocumentSkeletonSpan) {
@@ -316,10 +450,16 @@ export class InputController {
const { left, width } = span;
- return divideLeft + left + width;
+ const start = divideLeft + left;
+
+ if (this._currentNodePosition?.isBack === true) {
+ return start;
+ }
+
+ return start + width;
}
- private _matchSpanByLeftOffset(line: IDocumentSkeletonLine, offsetLeft: number) {
+ private _matchPositionByLeftOffset(document: Documents, line: IDocumentSkeletonLine, offsetLeft: number) {
const nearestNode: {
span?: IDocumentSkeletonSpan;
distance: number;
@@ -333,7 +473,7 @@ export class InputController {
const leftSide = divideLeft + left;
const rightSide = leftSide + width;
if (offsetLeft >= leftSide && offsetLeft <= rightSide) {
- return span;
+ return document.findPositionBySpan(span);
}
const distance = Math.abs(offsetLeft - (leftSide + rightSide) / 2);
@@ -344,7 +484,11 @@ export class InputController {
}
}
- return nearestNode.span;
+ if (nearestNode.span == null) {
+ return;
+ }
+
+ return document.findPositionBySpan(nearestNode.span);
}
// eslint-disable-next-line max-lines-per-function
@@ -497,12 +641,30 @@ export class InputController {
return;
}
- if (this._hasListSpan(span)) {
- position.span += 1;
- }
+ const newPos: INodePosition = { ...position, isBack };
- const newPos = { ...position, isBack };
+ this._syncSelection(document, selectionRemain, newPos, isSyncScroll);
+ this._currentNodePosition = selectionRemain.getStart();
+
+ // document.addSelection(selectionRemain);
+
+ // selectionRemain.startNodePosition = newPos;
+
+ // selectionRemain.endNodePosition = undefined;
+
+ // selectionRemain.refresh(document);
+
+ // this._currentNodePosition = selectionRemain.getStart();
+
+ // if (isSyncScroll) {
+ // document.scrollBySelection();
+ // }
+
+ // document.syncSelection();
+ }
+
+ private _syncSelection(document: Documents, selectionRemain: TextSelection, newPos: INodePosition, isSyncScroll = false) {
document.addSelection(selectionRemain);
selectionRemain.startNodePosition = newPos;
@@ -517,16 +679,4 @@ export class InputController {
document.syncSelection();
}
-
- private _hasListSpan(span: Nullable<IDocumentSkeletonSpan>) {
- const divide = span?.parent;
-
- if (divide == null) {
- return false;
- }
-
- const spanGroup = divide.spanGroup;
-
- return spanGroup[0]?.spanType === SpanType.LIST;
- }
}
diff --git a/packages/base-render/src/Basics/DocumentNodeTools.ts b/packages/base-render/src/Basics/DocumentNodeTools.ts
new file mode 100644
index 0000000000..0e3796c81f
--- /dev/null
+++ b/packages/base-render/src/Basics/DocumentNodeTools.ts
@@ -0,0 +1,128 @@
+import { checkParagraphHasIndentByStyle, DataStreamTreeTokenType, IDocumentBody, Nullable } from '@univerjs/core';
+
+import { IDocumentSkeletonSpan, SpanType } from './IDocumentSkeletonCached';
+
+export function hasListSpan(span: Nullable<IDocumentSkeletonSpan>) {
+ const divide = span?.parent;
+
+ if (divide == null) {
+ return false;
+ }
+
+ const spanGroup = divide.spanGroup;
+
+ return spanGroup[0]?.spanType === SpanType.LIST;
+}
+
+export function isIndentBySpan(span: Nullable<IDocumentSkeletonSpan>, body?: IDocumentBody) {
+ const paragraph = getParagraphBySpan(span, body);
+ if (paragraph == null) {
+ return false;
+ }
+ const paragraphStyle = paragraph.paragraphStyle;
+
+ if (paragraphStyle == null) {
+ return false;
+ }
+
+ return checkParagraphHasIndentByStyle(paragraphStyle);
+}
+
+export function isLastSpan(span: Nullable<IDocumentSkeletonSpan>) {
+ const divide = span?.parent;
+
+ const line = divide?.parent;
+
+ const spanGroup = divide?.spanGroup;
+
+ const divides = line?.divides;
+
+ if (spanGroup && span && divides && divide) {
+ const spanIndex = spanGroup.indexOf(span);
+ const divideIndex = divides.indexOf(divide);
+
+ if (divideIndex === divides.length - 1 && spanIndex === spanGroup.length - 1) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+export function isFirstSpan(span: Nullable<IDocumentSkeletonSpan>) {
+ const divide = span?.parent;
+
+ const line = divide?.parent;
+
+ const spanGroup = divide?.spanGroup;
+
+ const divides = line?.divides;
+
+ if (spanGroup && span && divides && divide) {
+ const spanIndex = spanGroup.indexOf(span);
+ const divideIndex = divides.indexOf(divide);
+
+ if (divideIndex === 0 && spanIndex === 0) {
+ return true;
+ }
+
+ if (divideIndex === 0 && spanIndex === 1 && spanGroup[0].spanType === SpanType.LIST) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+export function getParagraphBySpan(span: Nullable<IDocumentSkeletonSpan>, body?: IDocumentBody) {
+ const line = span?.parent?.parent;
+ if (line == null || body == null) {
+ return;
+ }
+ const paragraphs = body.paragraphs;
+
+ if (paragraphs == null) {
+ return;
+ }
+
+ for (const paragraph of paragraphs) {
+ if (paragraph.startIndex === line.paragraphIndex) {
+ return paragraph;
+ }
+ }
+}
+
+export function isPlaceholderOrSpace(span: Nullable<IDocumentSkeletonSpan>) {
+ if (span == null) {
+ return false;
+ }
+
+ if (
+ [DataStreamTreeTokenType.PARAGRAPH, DataStreamTreeTokenType.TAB, DataStreamTreeTokenType.SECTION_BREAK].indexOf(span.streamType) !== -1 ||
+ span.content === DataStreamTreeTokenType.SPACE
+ ) {
+ return true;
+ }
+
+ return false;
+}
+
+export function isSameLine(span1: Nullable<IDocumentSkeletonSpan>, span2: Nullable<IDocumentSkeletonSpan>) {
+ if (span1 == null) {
+ return false;
+ }
+
+ if (span2 == null) {
+ return false;
+ }
+
+ if (span1.parent == null) {
+ return false;
+ }
+
+ if (span2.parent == null) {
+ return false;
+ }
+
+ return span1.parent.parent === span2.parent.parent;
+}
diff --git a/packages/base-render/src/Basics/FontCache.ts b/packages/base-render/src/Basics/FontCache.ts
index b1c12b7312..dcb7b1e04f 100644
--- a/packages/base-render/src/Basics/FontCache.ts
+++ b/packages/base-render/src/Basics/FontCache.ts
@@ -81,7 +81,7 @@ export class FontCache {
let isDelete = false;
let i = 0;
- for (let item of this._globalFontMeasureCache) {
+ for (const item of this._globalFontMeasureCache) {
const [, values] = item;
allSize += values.size;
if (allSize > cacheLimit) {
@@ -93,7 +93,7 @@ export class FontCache {
if (isDelete) {
let deleteAllSize = 0;
- for (let item of this._globalFontMeasureCache) {
+ for (const item of this._globalFontMeasureCache) {
const [key, values] = item;
deleteAllSize += values.size;
if (deleteAllSize > cacheLimit / 2) {
@@ -200,7 +200,7 @@ export class FontCache {
// 兼容不支持textMetrics的情况
if (fontBoundingBoxAscent == null || fontBoundingBoxDescent == null || Number.isNaN(fontBoundingBoxAscent) || Number.isNaN(fontBoundingBoxDescent)) {
- let oneLineTextHeight = this.getTextSizeByDom(DEFAULT_MEASURE_TEXT, fontString).height;
+ const oneLineTextHeight = this.getTextSizeByDom(DEFAULT_MEASURE_TEXT, fontString).height;
if (ctx.textBaseline === 'top') {
cache.fontBoundingBoxAscent = cache.actualBoundingBoxAscent = oneLineTextHeight;
@@ -220,7 +220,7 @@ export class FontCache {
private static _clearMeasureCache(limit: number, values: Map<string, IMeasureTextCache>) {
let valueIndex = 0;
- for (let txtItem of values) {
+ for (const txtItem of values) {
const [txtKey] = txtItem;
if (valueIndex > limit) {
break;
diff --git a/packages/base-render/src/Basics/IDocumentSkeletonCached.ts b/packages/base-render/src/Basics/IDocumentSkeletonCached.ts
index fc611007f0..e0395adc23 100644
--- a/packages/base-render/src/Basics/IDocumentSkeletonCached.ts
+++ b/packages/base-render/src/Basics/IDocumentSkeletonCached.ts
@@ -3,7 +3,6 @@ import {
ColumnSeparatorType,
IDrawing,
INestingLevel,
- IParagraphStyle,
ITextStyle,
IDocumentRenderConfig,
IIndentStart,
@@ -126,7 +125,6 @@ export interface IDocumentSkeletonLine {
lineIndex: number; // lineIndex 行号
bullet?: IDocumentSkeletonBullet; // 无序和有序列表标题
paragraphStart: boolean; // Paragraph start 默认false
- paragraphStyleOrigin?: IParagraphStyle; // ParagraphStyleOrigin 段落样式配置
// dtId: string[]; // drawingTBIds 影响行的元素id集合,会切割divide,影响上下
// bmt: number; // benchmarkTop, drawing的位置是根据paragraph的位置进行相对定位的,段落跨页后,需要一个校准
diff --git a/packages/base-render/src/Basics/Interfaces.ts b/packages/base-render/src/Basics/Interfaces.ts
index 33ce6ce90f..1d569f35a5 100644
--- a/packages/base-render/src/Basics/Interfaces.ts
+++ b/packages/base-render/src/Basics/Interfaces.ts
@@ -139,3 +139,7 @@ export interface INodeSearch {
section: number;
page: number;
}
+
+export interface INodePosition extends INodeSearch {
+ isBack: boolean;
+}
diff --git a/packages/base-render/src/Basics/ScrollXY.ts b/packages/base-render/src/Basics/ScrollXY.ts
index 8afbe618fa..ae474df53a 100644
--- a/packages/base-render/src/Basics/ScrollXY.ts
+++ b/packages/base-render/src/Basics/ScrollXY.ts
@@ -1,8 +1,8 @@
export function getCurrentScrollXY(scrollTimer: any) {
const scene = scrollTimer.getScene();
const viewport = scrollTimer.getViewportByCoord(scene);
- let scrollX = 0;
- let scrollY = 0;
+ const scrollX = 0;
+ const scrollY = 0;
if (!viewport) {
return {
scrollX,
diff --git a/packages/base-render/src/Basics/index.ts b/packages/base-render/src/Basics/index.ts
index e6e1719692..fa4f1c2218 100644
--- a/packages/base-render/src/Basics/index.ts
+++ b/packages/base-render/src/Basics/index.ts
@@ -10,3 +10,4 @@ export * from './Vector2';
export * from './Path2';
export * from './Tools';
export * from './ScrollXY';
+export * from './DocumentNodeTools';
diff --git a/packages/base-render/src/Component/Docs/Block/Paragraph/Language.Ruler.ts b/packages/base-render/src/Component/Docs/Block/Paragraph/Language.Ruler.ts
index 33e5b3934a..a967c4536c 100644
--- a/packages/base-render/src/Component/Docs/Block/Paragraph/Language.Ruler.ts
+++ b/packages/base-render/src/Component/Docs/Block/Paragraph/Language.Ruler.ts
@@ -1,4 +1,4 @@
-import { DataStreamTreeNode, DocumentBodyModel, IParagraphStyle, Nullable } from '@univerjs/core';
+import { DataStreamTreeNode, DataStreamTreeTokenType, DocumentBodyModel, IParagraphStyle, Nullable } from '@univerjs/core';
import { createSkeletonLetterSpan, createSkeletonWordSpan } from '../../Common/Span';
import { hasArabic, hasCJK, hasSpaceAndTab, hasTibetan } from '../../../../Basics/Tools';
import { IDocumentSkeletonSpan } from '../../../../Basics/IDocumentSkeletonCached';
@@ -19,6 +19,9 @@ export function composeCharForLanguage(
sectionBreakConfig: ISectionBreakConfig,
paragraphStyle: IParagraphStyle
): Nullable<LanguageResult> {
+ if (char === DataStreamTreeTokenType.SPACE) {
+ return;
+ }
if (hasArabic(char)) {
return ArabicHandler(char, index, charArray, bodyModel, paragraphNode, sectionBreakConfig, paragraphStyle);
}
diff --git a/packages/base-render/src/Component/Docs/Block/Paragraph/Layout.Ruler.ts b/packages/base-render/src/Component/Docs/Block/Paragraph/Layout.Ruler.ts
index 29c2901158..b65031eefd 100644
--- a/packages/base-render/src/Component/Docs/Block/Paragraph/Layout.Ruler.ts
+++ b/packages/base-render/src/Component/Docs/Block/Paragraph/Layout.Ruler.ts
@@ -1,4 +1,4 @@
-import { BooleanNumber, GridType, HorizontalAlign, INumberUnit, IParagraphStyle, NamedStyleType, SpacingRule } from '@univerjs/core';
+import { BooleanNumber, DataStreamTreeTokenType, GridType, HorizontalAlign, INumberUnit, IParagraphStyle, NamedStyleType, SpacingRule } from '@univerjs/core';
import {
getCharSpaceApply,
getCharSpaceConfig,
@@ -19,8 +19,8 @@ import {
} from '../../Common/Tools';
// eslint-disable-next-line import/no-cycle
import { createSkeletonPage } from '../../Common/Page';
-import { addSpanToDivide, calculateLineTopByDrawings, createAndUpdateBlockAnchor, createSkeletonLine, setDivideFullState } from '../../Common/Line';
-import { createSkeletonBulletSpan, setSpanGroupLeft } from '../../Common/Span';
+import { calculateLineTopByDrawings, createAndUpdateBlockAnchor, createSkeletonLine, setDivideFullState } from '../../Common/Line';
+import { createSkeletonBulletSpan, addSpanToDivide } from '../../Common/Span';
import { setColumnFullState } from '../../Common/Section';
import {
IDocumentSkeletonBullet,
@@ -87,7 +87,9 @@ function _divideOperator(
// const { width: pageWidth, marginLeft: pageMarginLeft, marginRight: pageMarginRight } = lastPage;
const pageContentWidth = getPageContentWidth(lastPage);
- if (preWidth + preLeft + width > divide.width) {
+ const preOffsetLeft = preWidth + preLeft;
+
+ if (preOffsetLeft + width > divide.width) {
// w超过div宽度
setDivideFullState(divide, true);
const column = getColumnByDivide(divide);
@@ -95,7 +97,7 @@ function _divideOperator(
if (width > pageContentWidth) {
// 一个字符超页内容宽
if (isBlankPage(lastPage)) {
- addSpanToDivide(divide, spanGroup);
+ addSpanToDivide(divide, spanGroup, preOffsetLeft);
// divide.spanGroup.push(...spanGroup);
__makeColumnsFull(column?.parent?.columns);
} else {
@@ -105,15 +107,18 @@ function _divideOperator(
// 一个字符超列宽
setColumnFullState(column, true);
if (isBlankColumn(column)) {
- console.log(spanGroup);
- addSpanToDivide(divide, spanGroup);
+ addSpanToDivide(divide, spanGroup, preOffsetLeft);
// divide.spanGroup.push(...spanGroup);
} else {
_columnOperator(spanGroup, pages, sectionBreakConfig, paragraphConfig, paragraphStart, defaultSpanLineHeight);
}
} else if (divideInfo.isLast) {
// 最后一个divide
- _lineOperator(spanGroup, pages, sectionBreakConfig, paragraphConfig, paragraphStart, defaultSpanLineHeight);
+ if (spanGroup.length === 1 && spanGroup[0].content === DataStreamTreeTokenType.SPACE) {
+ addSpanToDivide(divide, spanGroup, preOffsetLeft);
+ } else {
+ _lineOperator(spanGroup, pages, sectionBreakConfig, paragraphConfig, paragraphStart, defaultSpanLineHeight);
+ }
} else {
// 不是最后一个divide
_divideOperator(spanGroup, pages, sectionBreakConfig, paragraphConfig, paragraphStart, defaultSpanLineHeight);
@@ -150,12 +155,9 @@ function _divideOperator(
return;
}
}
- setSpanGroupLeft(spanGroup, preWidth + preLeft);
- for (let span of spanGroup) {
- span.parent = divide;
- }
+
// console.log('spanGroup', spanGroup, spanGroup.length, spanGroup[0].content);
- addSpanToDivide(divide, spanGroup);
+ addSpanToDivide(divide, spanGroup, preOffsetLeft);
// divide.spanGroup.push(...spanGroup);
}
} else {
@@ -338,14 +340,14 @@ function __getIndentPadding(
charSpaceApply: number
) {
const { spanType = SpanType.LETTER, bBox } = span;
- let indentFirstLineNumber = getNumberUnitValue(indentFirstLine, charSpaceApply);
- let hangingNumber = getNumberUnitValue(hanging, charSpaceApply);
- let indentStartNumber = getNumberUnitValue(indentStart, charSpaceApply);
- let indentEndNumber = getNumberUnitValue(indentEnd, charSpaceApply);
+ const indentFirstLineNumber = getNumberUnitValue(indentFirstLine, charSpaceApply);
+ const hangingNumber = getNumberUnitValue(hanging, charSpaceApply);
+ const indentStartNumber = getNumberUnitValue(indentStart, charSpaceApply);
+ const indentEndNumber = getNumberUnitValue(indentEnd, charSpaceApply);
let paddingLeft = indentStartNumber;
- let paddingRight = indentEndNumber;
- let changeBulletWidth = {
+ const paddingRight = indentEndNumber;
+ const changeBulletWidth = {
state: false,
hangingNumber: 0,
};
diff --git a/packages/base-render/src/Component/Docs/Block/Paragraph/Paragraph.ts b/packages/base-render/src/Component/Docs/Block/Paragraph/Paragraph.ts
index 534e723f24..eeafc68f1d 100644
--- a/packages/base-render/src/Component/Docs/Block/Paragraph/Paragraph.ts
+++ b/packages/base-render/src/Component/Docs/Block/Paragraph/Paragraph.ts
@@ -41,7 +41,7 @@ export function dealWidthParagraph(
paragraphNode: DataStreamTreeNode,
curPage: IDocumentSkeletonPage,
sectionBreakConfig: ISectionBreakConfig,
- skeletonResourceReference: ISkeletonResourceReference,
+ skeletonResourceReference: ISkeletonResourceReference
): IDocumentSkeletonPage[] {
const {
gridType = GridType.LINES,
@@ -314,4 +314,4 @@ function horizontalAlignHandler(line: IDocumentSkeletonLine, horizontalAlign: Ho
/**todo */
}
}
-}
\ No newline at end of file
+}
diff --git a/packages/base-render/src/Component/Docs/Common/Line.ts b/packages/base-render/src/Component/Docs/Common/Line.ts
index 3ef2ac860b..eb1b060e6d 100644
--- a/packages/base-render/src/Component/Docs/Common/Line.ts
+++ b/packages/base-render/src/Component/Docs/Common/Line.ts
@@ -1,12 +1,5 @@
import { IDrawing, Nullable, PositionedObjectLayoutType, WrapTextType } from '@univerjs/core';
-import {
- IDocumentSkeletonDrawingAnchor,
- IDocumentSkeletonDivide,
- IDocumentSkeletonDrawing,
- IDocumentSkeletonLine,
- IDocumentSkeletonSpan,
- LineType,
-} from '../../../Basics/IDocumentSkeletonCached';
+import { IDocumentSkeletonDrawingAnchor, IDocumentSkeletonDivide, IDocumentSkeletonDrawing, IDocumentSkeletonLine, LineType } from '../../../Basics/IDocumentSkeletonCached';
import { Vector2 } from '../../../Basics/Vector2';
@@ -79,7 +72,7 @@ export function createSkeletonLine(
lineSke.divides = _calculateDividesByDrawings(lineHeight, lineTop, columnWidth, paddingLeft, paddingRight, affectSkeDrawings, headersDrawings, footersDrawings);
- for (let divide of lineSke.divides) {
+ for (const divide of lineSke.divides) {
divide.parent = lineSke;
}
@@ -223,7 +216,7 @@ function _calculateSplit(drawing: IDocumentSkeletonDrawing, lineHeight: number,
return;
}
- let points: Vector2[] = [];
+ const points: Vector2[] = [];
points.push(new Vector2(start[0], start[1]));
for (let i = 0; i < lineTo.length; i++) {
const point = lineTo[i];
@@ -315,7 +308,7 @@ function __getSplitWidthNoAngle(top: number, height: number, left: number, width
let resultLeft = left - distL;
let resultWidth = width + distR;
- let ruler = ___getWrapTextRuler(wrapText, resultLeft, resultWidth, columnWidth);
+ const ruler = ___getWrapTextRuler(wrapText, resultLeft, resultWidth, columnWidth);
if (ruler === WrapTextRuler.LEFT) {
resultWidth = columnWidth - resultLeft;
@@ -434,25 +427,3 @@ export function createAndUpdateBlockAnchor(paragraphIndex: number, line: IDocume
});
}
}
-
-export function addSpanToDivide(divide: IDocumentSkeletonDivide, spanGroup: IDocumentSkeletonSpan[]) {
- const line = divide.parent;
- if (line != null) {
- const isFirstLine = line.divides[0].spanGroup[0] == null;
- const firstSpan = spanGroup[0];
- const firstSpanContent = firstSpan.content || ' ';
- if (isFirstLine && firstSpanContent === ' ') {
- const width = firstSpan.width;
- firstSpan.width = 0;
- for (let span of spanGroup) {
- if (span === firstSpan) {
- continue;
- }
-
- span.left -= width;
- }
- }
- }
-
- divide.spanGroup.push(...spanGroup);
-}
diff --git a/packages/base-render/src/Component/Docs/Common/Span.ts b/packages/base-render/src/Component/Docs/Common/Span.ts
index 2dbebbd64a..ba9a284f54 100644
--- a/packages/base-render/src/Component/Docs/Common/Span.ts
+++ b/packages/base-render/src/Component/Docs/Common/Span.ts
@@ -1,8 +1,10 @@
import { BooleanNumber, BulletAlignment, DataStreamTreeTokenType as DT, GridType } from '@univerjs/core';
-import { validationGrid } from './Tools';
-import { IDocumentSkeletonBullet, IDocumentSkeletonSpan, SpanType } from '../../../Basics/IDocumentSkeletonCached';
+
import { FontCache } from '../../../Basics/FontCache';
+import { IDocumentSkeletonBullet, IDocumentSkeletonDivide, IDocumentSkeletonSpan, SpanType } from '../../../Basics/IDocumentSkeletonCached';
import { IFontCreateConfig } from '../../../Basics/Interfaces';
+import { hasCJK } from '../../../Basics/Tools';
+import { validationGrid } from './Tools';
export function createSkeletonWordSpan(content: string, config: IFontCreateConfig, spanWidth?: number): IDocumentSkeletonSpan {
return _createSkeletonWordOrLetter(SpanType.WORD, content, config, spanWidth);
@@ -68,9 +70,9 @@ export function _createSkeletonWordOrLetter(spanType: SpanType, content: string,
let paddingLeft = 0;
if (validationGrid(gridType, snapToGrid)) {
// 当文字也需要对齐到网格式,进行处理
- const multiple = Math.ceil(contentWidth / charSpace);
- width = multiple * charSpace;
- if (gridType === GridType.LINES_AND_CHARS) {
+ // const multiple = Math.ceil(contentWidth / charSpace);
+ width = contentWidth + (hasCJK(content) ? charSpace : charSpace / 2);
+ if (gridType === GridType.SNAP_TO_CHARS) {
paddingLeft = (width - contentWidth) / 2;
}
}
@@ -137,6 +139,33 @@ export function setSpanLeft(span: IDocumentSkeletonSpan, left: number = 0) {
span.left = left;
}
+export function addSpanToDivide(divide: IDocumentSkeletonDivide, spanGroup: IDocumentSkeletonSpan[], offsetLeft: number = 0) {
+ // const line = divide.parent;
+ // if (line != null) {
+ // const isFirstLine = line.divides[0].spanGroup[0] == null;
+ // const firstSpan = spanGroup[0];
+ // const firstSpanContent = firstSpan.content || ' ';
+ // if (isFirstLine && firstSpanContent === ' ') {
+ // const width = firstSpan.width;
+ // firstSpan.width = 0;
+ // for (const span of spanGroup) {
+ // if (span === firstSpan) {
+ // continue;
+ // }
+
+ // span.left -= width;
+ // }
+ // }
+ // }
+
+ setSpanGroupLeft(spanGroup, offsetLeft);
+ for (const span of spanGroup) {
+ span.parent = divide;
+ }
+
+ divide.spanGroup.push(...spanGroup);
+}
+
function _getMaxBoundingBox(span: IDocumentSkeletonSpan, bulletSkeleton: IDocumentSkeletonBullet) {
const { ba: spanAscent, bd: spanDescent } = span.bBox;
const { ba: bulletAscent, bd: bulletDescent } = bulletSkeleton.bBox;
diff --git a/packages/base-render/src/Component/Docs/Common/TextSelection.ts b/packages/base-render/src/Component/Docs/Common/TextSelection.ts
index f3be36d1e4..f578aad474 100644
--- a/packages/base-render/src/Component/Docs/Common/TextSelection.ts
+++ b/packages/base-render/src/Component/Docs/Common/TextSelection.ts
@@ -10,12 +10,14 @@ import {
} from '../../../Basics/IDocumentSkeletonCached';
import { getColor } from '../../../Basics/Tools';
import { IPoint } from '../../../Basics/Vector2';
-import { Scene } from '../../../Scene';
+
import { Rect } from '../../../Shape/Rect';
import { RegularPolygon } from '../../../Shape/RegularPolygon';
import { Liquid } from './Liquid';
import { DocComponent } from '../DocComponent';
+import { INodePosition } from '../../../Basics/Interfaces';
+import { ThinScene } from '../../../ThinScene';
enum NodePositionStateType {
NORMAL,
@@ -53,15 +55,6 @@ interface ICurrentNodePositionState {
const TEXT_RANGE_KEY_PREFIX = '__TestSelectionRange__';
const TEXT_ANCHOR_KEY_PREFIX = '__TestSelectionAnchor__';
-export interface INodePosition {
- page: number;
- section: number;
- column: number;
- line: number;
- divide: number;
- span: number;
- isBack: boolean;
-}
export class TextSelection {
private _current = false;
@@ -92,7 +85,7 @@ export class TextSelection {
span: NodePositionStateType.NORMAL,
};
- constructor(private _scene: Scene, public startNodePosition?: Nullable<INodePosition>, public endNodePosition?: Nullable<INodePosition>, public segmentId?: string) {}
+ constructor(private _scene: ThinScene, public startNodePosition?: Nullable<INodePosition>, public endNodePosition?: Nullable<INodePosition>, public segmentId?: string) {}
getRange() {
const cursorList = this._rangeList;
@@ -219,7 +212,6 @@ export class TextSelection {
const data = this._getRangePointData(start!, start!, documents);
const { pointGroup, cursorList } = data;
this._setRangeList(cursorList);
- console.log(pointGroup, cursorList);
pointGroup.length > 0 && this._createAndUpdateAnchor(pointGroup, documents.left, documents.top);
return;
}
@@ -227,13 +219,12 @@ export class TextSelection {
const data = this._getRangePointData(start!, end!, documents);
const { pointGroup, cursorList } = data;
this._setRangeList(cursorList);
- console.log(pointGroup, cursorList);
pointGroup.length > 0 && this._createAndUpdateRange(pointGroup, documents.left, documents.top);
}
getStart() {
if (this.startNodePosition == null) {
- return this.endNodePosition;
+ return null;
}
if (this.endNodePosition == null) {
@@ -245,6 +236,20 @@ export class TextSelection {
return start;
}
+ getEnd() {
+ if (this.startNodePosition == null) {
+ return this.endNodePosition;
+ }
+
+ if (this.endNodePosition == null) {
+ return null;
+ }
+
+ const { end } = this._compareNodePosition(this.startNodePosition, this.endNodePosition);
+
+ return end;
+ }
+
private _resetCurrentNodePositionState() {
this._currentStartState = {
page: NodePositionStateType.NORMAL,
diff --git a/packages/base-render/src/Component/Docs/DocComponent.ts b/packages/base-render/src/Component/Docs/DocComponent.ts
index 369eedf924..240e9a53b6 100644
--- a/packages/base-render/src/Component/Docs/DocComponent.ts
+++ b/packages/base-render/src/Component/Docs/DocComponent.ts
@@ -1,12 +1,12 @@
import { Nullable } from '@univerjs/core';
import { IBoundRect } from '../../Basics/Vector2';
-import { INodeInfo, INodeSearch, ITransformChangeState } from '../../Basics/Interfaces';
+import { INodeInfo, INodePosition, INodeSearch, ITransformChangeState } from '../../Basics/Interfaces';
import { Canvas } from '../../Canvas';
import { RenderComponent } from '../Component';
import { DocumentSkeleton } from './DocSkeleton';
import { Scene } from '../../Scene';
import { DOCS_EXTENSION_TYPE } from './DocExtension';
-import { IDocumentSkeletonLine, IDocumentSkeletonSpan, PageLayoutType } from '../../Basics/IDocumentSkeletonCached';
+import { IDocumentSkeletonLine, IDocumentSkeletonSpan, PageLayoutType, SpanType } from '../../Basics/IDocumentSkeletonCached';
import { RENDER_CLASS_TYPE } from '../../Basics/Const';
export class DocComponent extends RenderComponent<IDocumentSkeletonSpan | IDocumentSkeletonLine, DOCS_EXTENSION_TYPE> {
@@ -90,6 +90,34 @@ export class DocComponent extends RenderComponent<IDocumentSkeletonSpan | IDocum
remainActiveSelection() {}
+ findSpanByPosition(position: Nullable<INodePosition>) {
+ const skeleton = this.getSkeleton();
+
+ if (!skeleton || position == null) {
+ return;
+ }
+
+ const skeletonData = skeleton.getSkeletonData();
+
+ const { divide, line, column, section, page, isBack } = position;
+
+ let { span } = position;
+
+ if (isBack === true) {
+ span -= 1;
+ }
+
+ span = span < 0 ? 0 : span;
+
+ const spanGroup = skeletonData.pages[page].sections[section].columns[column].lines[line].divides[divide].spanGroup;
+
+ if (spanGroup[span].spanType === SpanType.LIST) {
+ return spanGroup[span + 1];
+ }
+
+ return spanGroup[span];
+ }
+
findPositionBySpan(span: IDocumentSkeletonSpan): Nullable<INodeSearch> {
const divide = span.parent;
@@ -129,9 +157,7 @@ export class DocComponent extends RenderComponent<IDocumentSkeletonSpan | IDocum
};
}
- findNodeByCoord(offsetX: number, offsetY: number): false | INodeInfo {
- return false;
- }
+ findNodeByCoord(offsetX: number, offsetY: number): Nullable<INodeInfo> {}
findCoordByNode(span: IDocumentSkeletonSpan) {}
@@ -183,6 +209,10 @@ export class DocComponent extends RenderComponent<IDocumentSkeletonSpan | IDocum
continue;
}
+ if (spanGroup[0].spanType === SpanType.LIST) {
+ charIndex++;
+ }
+
const span = spanGroup[charIndex - st];
if (span) {
diff --git a/packages/base-render/src/Component/Docs/Document.Editor.ts b/packages/base-render/src/Component/Docs/Document.Editor.ts
index 7a951537a0..f797dcb687 100644
--- a/packages/base-render/src/Component/Docs/Document.Editor.ts
+++ b/packages/base-render/src/Component/Docs/Document.Editor.ts
@@ -6,13 +6,13 @@ import { getOffsetRectForDom, transformBoundingCoord } from '../../Basics/Positi
import { ScrollTimer } from '../../ScrollTimer';
import { DocComponent } from './DocComponent';
-import { INodePosition, TextSelection } from './Common/TextSelection';
+import { TextSelection } from './Common/TextSelection';
import { IScrollObserverParam, Viewport } from '../../Viewport';
import { checkStyle, injectStyle } from '../../Basics/Tools';
import { Vector2 } from '../../Basics/Vector2';
import { getCurrentScrollXY } from '../../Basics/ScrollXY';
-import { INodeInfo } from '../../Basics/Interfaces';
+import { INodeInfo, INodePosition } from '../../Basics/Interfaces';
export interface IEditorInputConfig {
event: Event | CompositionEvent | KeyboardEvent;
@@ -33,7 +33,7 @@ export class DocsEditor {
onCompositionendObservable = new Observable<IEditorInputConfig>();
- onSelectionStartObservable = new Observable<INodeInfo | false>();
+ onSelectionStartObservable = new Observable<Nullable<INodePosition>>();
private _container: HTMLDivElement;
@@ -299,8 +299,8 @@ export class DocsEditor {
return this._documents?.getSkeleton()?.getSkeletonData();
}
- private _getNodePosition(node: INodeInfo | false, isStart: Boolean = true) {
- if (node === false) {
+ private _getNodePosition(node: Nullable<INodeInfo>) {
+ if (node == null) {
return;
}
@@ -313,7 +313,8 @@ export class DocsEditor {
}
let isBack = false;
- if (ratioX < 0.5 || (span.streamType === DataStreamTreeTokenType.PARAGRAPH && isStart)) {
+ //|| (span.streamType === DataStreamTreeTokenType.PARAGRAPH && isStart)
+ if (ratioX < 0.5) {
isBack = true;
}
@@ -469,7 +470,7 @@ export class DocsEditor {
const endNode = this._documents.findNodeByCoord(moveOffsetX, moveOffsetY);
- const endPosition = this._getNodePosition(endNode, false);
+ const endPosition = this._getNodePosition(endNode);
// console.log('endNode', endNode, endPosition, { moveOffsetX, moveOffsetY, _viewportScrollY: this._viewportScrollY, scrollX });
@@ -661,6 +662,7 @@ export class DocsEditor {
});
}
+ // eslint-disable-next-line max-lines-per-function
private _attachSelectionEvent(documents: DocComponent) {
this._moveInObserver = documents.onPointerEnterObserver.add(() => {
documents.cursor = CURSOR_TYPE.TEXT;
@@ -687,11 +689,15 @@ export class DocsEditor {
console.log('startNode', startNode, position, evtOffsetX, evtOffsetY);
- if (!position) {
+ if (position == null) {
this._deleteAllTextSelection();
return;
}
+ if (startNode?.node.streamType === DataStreamTreeTokenType.PARAGRAPH) {
+ position.isBack = true;
+ }
+
if (evt.ctrlKey || this._isEmptyTextSelection()) {
const newTextSelection = new TextSelection(this._documents.getScene(), position);
this._addTextSelection(newTextSelection);
@@ -713,15 +719,24 @@ export class DocsEditor {
this._viewportScrollX = scrollX;
this._viewportScrollY = scrollY;
- this.onSelectionStartObservable.notifyObservers(startNode);
+ this.onSelectionStartObservable.notifyObservers(this.getActiveTextSelection()?.getStart());
+
+ let preMoveOffsetX = evtOffsetX;
+
+ let preMoveOffsetY = evtOffsetY;
this._moveObserver = scene.onPointerMoveObserver.add((moveEvt: IPointerEvent | IMouseEvent) => {
const { offsetX: moveOffsetX, offsetY: moveOffsetY } = moveEvt;
+ scene.setCursor(CURSOR_TYPE.TEXT);
+ if (Math.sqrt((moveOffsetX - preMoveOffsetX) ** 2 + (moveOffsetY - preMoveOffsetY) ** 2) < 3) {
+ return;
+ }
this._moving(moveOffsetX, moveOffsetY, scrollTimer);
scrollTimer.scrolling(moveOffsetX, moveOffsetY, () => {
this._moving(moveOffsetX, moveOffsetY, scrollTimer);
});
- scene.setCursor(CURSOR_TYPE.TEXT);
+ preMoveOffsetX = moveOffsetX;
+ preMoveOffsetY = moveOffsetY;
});
this._upObserver = scene.onPointerUpObserver.add((upEvt: IPointerEvent | IMouseEvent) => {
diff --git a/packages/base-render/src/Component/Docs/Document.ts b/packages/base-render/src/Component/Docs/Document.ts
index eac489685c..7c9a4763a9 100644
--- a/packages/base-render/src/Component/Docs/Document.ts
+++ b/packages/base-render/src/Component/Docs/Document.ts
@@ -12,6 +12,7 @@ import { DocsEditor } from './Document.Editor';
import { Liquid } from './Common/Liquid';
import { Scene } from '../../Scene';
import { TextSelection } from './Common/TextSelection';
+import { INodeInfo } from '../../Basics/Interfaces';
export interface IDocumentsConfig {
pageMarginLeft?: number;
@@ -353,12 +354,12 @@ export class Documents extends DocComponent {
continue;
}
- const { width: spanWidth, left: spanLeft } = span;
+ const { width: spanWidth, left: spanLeft, paddingLeft } = span;
const { x: translateX, y: translateY } = this._drawLiquid;
const originTranslate = Vector2.create(fixLineWidthByScale(translateX, scale), fixLineWidthByScale(translateY, scale));
const centerPoint = Vector2.create(fixLineWidthByScale(spanWidth / 2, scale), fixLineWidthByScale(lineHeight / 2, scale));
const spanStartPoint = calculateRectRotate(
- originTranslate.addByPoint(fixLineWidthByScale(spanLeft, scale), 0),
+ originTranslate.addByPoint(fixLineWidthByScale(spanLeft + paddingLeft, scale), 0),
centerPoint,
centerAngle,
vertexAngle,
@@ -366,7 +367,7 @@ export class Documents extends DocComponent {
);
const spanPointWithFont = calculateRectRotate(
- originTranslate.addByPoint(fixLineWidthByScale(spanLeft + maxLineAscSin, scale), fixLineWidthByScale(maxLineAscCos, scale)),
+ originTranslate.addByPoint(fixLineWidthByScale(spanLeft + maxLineAscSin + paddingLeft, scale), fixLineWidthByScale(maxLineAscCos, scale)),
centerPoint,
centerAngle,
vertexAngle,
@@ -413,12 +414,12 @@ export class Documents extends DocComponent {
}
// eslint-disable-next-line max-lines-per-function
- override findNodeByCoord(offsetX: number, offsetY: number) {
+ override findNodeByCoord(offsetX: number, offsetY: number): Nullable<INodeInfo> {
const scene = this.getScene() as Scene;
const originCoord = scene.transformToSceneCoord(Vector2.FromArray([offsetX, offsetY]));
if (!originCoord) {
- return false;
+ return;
}
const coord = this._getInverseCoord(originCoord);
@@ -426,7 +427,7 @@ export class Documents extends DocComponent {
const skeleton = this.getSkeleton();
if (!skeleton) {
- return false;
+ return;
}
this._findLiquid.reset();
@@ -435,6 +436,12 @@ export class Documents extends DocComponent {
const pages = skeletonData.pages;
+ let nearestNodeList: INodeInfo[] = [];
+
+ let nearestNodeDistanceList: number[] = [];
+
+ let nearestNodeDistanceY = Infinity;
+
for (let i = 0, len = pages.length; i < len; i++) {
const page = pages[i];
@@ -448,6 +455,7 @@ export class Documents extends DocComponent {
this._findLiquid.translatePagePadding(page);
const sections = page.sections;
+
for (const section of sections) {
const { columns, height } = section;
@@ -455,9 +463,9 @@ export class Documents extends DocComponent {
const { y: startY } = this._findLiquid;
- if (!(y >= startY && y <= startY + height)) {
- continue;
- }
+ // if (!(y >= startY && y <= startY + height)) {
+ // continue;
+ // }
for (const column of columns) {
const { lines, width: columnWidth } = column;
@@ -466,9 +474,9 @@ export class Documents extends DocComponent {
const { x: startX } = this._findLiquid;
- if (!(x >= startX && x <= startX + columnWidth)) {
- continue;
- }
+ // if (!(x >= startX && x <= startX + columnWidth)) {
+ // continue;
+ // }
const linesCount = lines.length;
@@ -488,10 +496,12 @@ export class Documents extends DocComponent {
const endY_fin = startY + lineHeight;
- if (!(y >= startY_fin && y <= endY_fin)) {
- this._findLiquid.translateRestore();
- continue;
- }
+ const distanceY = Math.abs(y - endY_fin);
+
+ // if (!(y >= startY_fin && y <= endY_fin)) {
+ // this._findLiquid.translateRestore();
+ // continue;
+ // }
const divideLength = divides.length;
for (let i = 0; i < divideLength; i++) {
@@ -503,10 +513,10 @@ export class Documents extends DocComponent {
const { x: startX } = this._findLiquid;
- if (!(x >= startX && x <= startX + divideWidth)) {
- this._findLiquid.translateRestore();
- continue;
- }
+ // if (!(x >= startX && x <= startX + divideWidth)) {
+ // this._findLiquid.translateRestore();
+ // continue;
+ // }
for (const span of spanGroup) {
if (!span.content || span.content.length === 0) {
@@ -519,15 +529,48 @@ export class Documents extends DocComponent {
const endX_fin = startX + spanLeft + spanWidth;
- if (!(x >= startX_fin && x <= endX_fin)) {
+ const distanceX = Math.abs(x - endX_fin);
+
+ if (y >= startY_fin && y <= endY_fin) {
+ if (x >= startX_fin && x <= endX_fin) {
+ return {
+ node: span,
+ ratioX: x / (startX_fin + endX_fin),
+ ratioY: y / (startY_fin + endY_fin),
+ };
+ }
+
+ if (nearestNodeDistanceY !== -Infinity) {
+ nearestNodeList = [];
+ nearestNodeDistanceList = [];
+ }
+ nearestNodeList.push({
+ node: span,
+ ratioX: x / (startX_fin + endX_fin),
+ ratioY: y / (startY_fin + endY_fin),
+ });
+
+ nearestNodeDistanceList.push(distanceX);
+
+ nearestNodeDistanceY = -Infinity;
continue;
}
- return {
- node: span,
- ratioX: x / (startX_fin + endX_fin),
- ratioY: y / (startY_fin + endY_fin),
- };
+ if (distanceY < nearestNodeDistanceY) {
+ nearestNodeDistanceY = distanceY;
+ nearestNodeList = [];
+ nearestNodeDistanceList = [];
+ }
+
+ if (distanceY === nearestNodeDistanceY) {
+ nearestNodeList.push({
+ node: span,
+ ratioX: x / (startX_fin + endX_fin),
+ ratioY: y / (startY_fin + endY_fin),
+ });
+
+ nearestNodeDistanceList.push(distanceX);
+ }
}
this._findLiquid.translateRestore();
}
@@ -540,48 +583,54 @@ export class Documents extends DocComponent {
this._translatePage(page);
}
- return false;
+ return this._getNearestNode(nearestNodeList, nearestNodeDistanceList);
}
- override findCoordByNode(span: IDocumentSkeletonSpan) {
- const divide = span.parent;
- const defaultCoordinate = {
- x: -Infinity,
- y: -Infinity,
- };
- if (divide == null) {
- return defaultCoordinate;
- }
+ // override findCoordByNode(span: IDocumentSkeletonSpan) {
+ // const divide = span.parent;
+ // const defaultCoordinate = {
+ // x: -Infinity,
+ // y: -Infinity,
+ // };
+ // if (divide == null) {
+ // return defaultCoordinate;
+ // }
- const line = divide.parent;
+ // const line = divide.parent;
- if (line == null) {
- return defaultCoordinate;
- }
+ // if (line == null) {
+ // return defaultCoordinate;
+ // }
- const column = line.parent;
+ // const column = line.parent;
- if (column == null) {
- return defaultCoordinate;
- }
+ // if (column == null) {
+ // return defaultCoordinate;
+ // }
- const section = column.parent;
+ // const section = column.parent;
- if (section == null) {
- return defaultCoordinate;
- }
+ // if (section == null) {
+ // return defaultCoordinate;
+ // }
- const page = section.parent;
+ // const page = section.parent;
- if (page == null) {
- return defaultCoordinate;
- }
- }
+ // if (page == null) {
+ // return defaultCoordinate;
+ // }
+ // }
protected override _draw(ctx: CanvasRenderingContext2D, bounds?: IBoundRect) {
this.draw(ctx, bounds);
}
+ private _getNearestNode(nearestNodeList: INodeInfo[], nearestNodeDistanceList: number[]) {
+ const miniValue = Math.min(...nearestNodeDistanceList);
+ const miniValueIndex = nearestNodeDistanceList.indexOf(miniValue);
+ return nearestNodeList[miniValueIndex];
+ }
+
private _horizontalHandler(pageWidth: number, pagePaddingLeft: number, pagePaddingRight: number, horizontalAlign: HorizontalAlign) {
let offsetLeft = 0;
if (horizontalAlign === HorizontalAlign.CENTER) {
diff --git a/packages/base-render/src/Component/Docs/Extensions/FontAndBaseLine.ts b/packages/base-render/src/Component/Docs/Extensions/FontAndBaseLine.ts
index 0a0971d0ba..fd52d3697a 100644
--- a/packages/base-render/src/Component/Docs/Extensions/FontAndBaseLine.ts
+++ b/packages/base-render/src/Component/Docs/Extensions/FontAndBaseLine.ts
@@ -23,9 +23,9 @@ export class FontAndBaseLine extends docExtension {
}
const { asc = 0, marginTop: lineMarginTop = 0, paddingTop: linePaddingTop = 0 } = line;
- let maxLineAsc = asc + lineMarginTop + linePaddingTop;
+ const maxLineAsc = asc + lineMarginTop + linePaddingTop;
const { ts: textStyle, content, fontStyle, bBox } = span;
- let { spanPointWithFont = Vector2.create(0, 0) } = this.extensionOffset;
+ const { spanPointWithFont = Vector2.create(0, 0) } = this.extensionOffset;
if (!textStyle) {
if (content != null) {
ctx.fillText(content, spanPointWithFont.x, spanPointWithFont.y);
diff --git a/packages/base-render/src/Component/Sheets/SheetSkeleton.ts b/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
index c67bbad642..553d3061f9 100644
--- a/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
+++ b/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
@@ -241,7 +241,7 @@ export class SpreadsheetSkeleton extends Skeleton {
};
}
let isSearching = true;
- let searchedMarge = new ObjectMatrix<boolean>();
+ const searchedMarge = new ObjectMatrix<boolean>();
while (isSearching) {
isSearching = false;
dataMergeCache.forEach((row: number, rowArray: ObjectArray<IRangeData>) => {
@@ -720,7 +720,7 @@ export class SpreadsheetSkeleton extends Skeleton {
if (!cache.font![fontString]) {
cache.font![fontString] = new ObjectMatrix();
}
- let { a: angle = 0, v: isVertical = BooleanNumber.FALSE } = textRotation;
+ const { a: angle = 0, v: isVertical = BooleanNumber.FALSE } = textRotation;
let centerAngle = 0;
let vertexAngle = angle;
if (isVertical === BooleanNumber.TRUE) {
@@ -746,7 +746,8 @@ export class SpreadsheetSkeleton extends Skeleton {
const fontCache = cache.font![fontString];
- let { a: angle, v: isVertical = BooleanNumber.FALSE } = textRotation as ITextRotation;
+ let { a: angle } = textRotation as ITextRotation;
+ const { v: isVertical = BooleanNumber.FALSE } = textRotation as ITextRotation;
if (isVertical === BooleanNumber.TRUE) {
angle = 90;
}
@@ -783,7 +784,7 @@ export class SpreadsheetSkeleton extends Skeleton {
const paragraphs = document.body.paragraphs || [];
- for (let paragraph of paragraphs) {
+ for (const paragraph of paragraphs) {
if (!paragraph.paragraphStyle) {
paragraph.paragraphStyle = {};
}
diff --git a/packages/base-sheets/src/Commands/Commands/set-style.command.ts b/packages/base-sheets/src/Commands/Commands/set-style.command.ts
index 647910a9a3..74383b69c0 100644
--- a/packages/base-sheets/src/Commands/Commands/set-style.command.ts
+++ b/packages/base-sheets/src/Commands/Commands/set-style.command.ts
@@ -43,7 +43,7 @@ export interface ISetStyleParams<T> {
* The command to set cell style.
* Set style to a bunch of ranges.
*/
-export const SetStyleCommand: ICommand = {
+export const SetStyleCommand: ICommand<ISetStyleParams> = {
type: CommandType.COMMAND,
id: 'sheet.command.set-style',
@@ -415,3 +415,69 @@ export const SetTextRotationCommand: ICommand<ISetTextRotationCommandParams> = {
return commandService.executeCommand(SetStyleCommand.id, setStyleParams);
},
};
+
+export const SetSpanCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-span',
+ handler: async (accessor, params) => true,
+};
+
+export const SetFontFamilyCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-font-family',
+ handler: async (accessor) => true,
+};
+
+export const SetFontSizeCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-font-family',
+ handler: async (accessor) => true,
+};
+
+export const SetTextColorCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-font-family',
+ handler: async (accessor) => true,
+};
+
+export const SetBackgroundColorCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-background-color',
+ handler: async (accessor) => true,
+};
+
+export const SetCellBorderCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-cell-border',
+ handler: async (accessor) => true,
+};
+
+export const SetSpanCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-span',
+ handler: async (params) => true,
+};
+
+export const SetVerticalTextAlignCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-vertical-text-align',
+ handler: async (params) => true,
+};
+
+export const SetHorizontalTextAlignCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-horizontal-text-align',
+ handler: async (params) => true,
+};
+
+export const SetTextWrapCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-text-wrap',
+ handler: async (params) => true,
+};
+
+export const SetTextRotationCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-text-rotation',
+ handler: async (params) => true,
+};
diff --git a/packages/common-plugin-data/src/Docs/DEFAULT_DOCUMENT_DATA_EN.ts b/packages/common-plugin-data/src/Docs/DEFAULT_DOCUMENT_DATA_EN.ts
index 54da56c7c8..4f56d81836 100644
--- a/packages/common-plugin-data/src/Docs/DEFAULT_DOCUMENT_DATA_EN.ts
+++ b/packages/common-plugin-data/src/Docs/DEFAULT_DOCUMENT_DATA_EN.ts
@@ -398,5 +398,8 @@ export const DEFAULT_DOCUMENT_DATA_EN: IDocumentData = {
marginBottom: 72,
marginRight: 90,
marginLeft: 90,
+ // gridType: GridType.LINES_AND_CHARS,
+ // linePitch: 24,
+ // charSpace: 12,
},
};
diff --git a/packages/core/src/Docs/Apply/Common.ts b/packages/core/src/Docs/Apply/Common.ts
index 5c58fbbc09..0c4682b662 100644
--- a/packages/core/src/Docs/Apply/Common.ts
+++ b/packages/core/src/Docs/Apply/Common.ts
@@ -1,6 +1,6 @@
import { ICustomBlock, ICustomRange, IDocumentBody, IParagraph, ISectionBreak, ITable, ITextRun } from '../../Types/Interfaces/IDocumentData';
import { Nullable } from '../../Shared/Types';
-import { checkParagraphHasBullet, horizontalLineSegmentsSubtraction } from '../../Shared/DocTool';
+import { horizontalLineSegmentsSubtraction } from '../../Shared/DocTool';
import { isSameStyleTextRun } from '../../Shared/Compare';
import { sortRulesFactory } from '../../Shared/SortRules';
import { DataStreamTreeTokenType } from '../Domain/Types';
@@ -297,7 +297,7 @@ export function deleteTextRuns(body: IDocumentBody, textLength: number, currentI
return removeTextRuns;
}
-export function deleteParagraphs(body: IDocumentBody, textLength: number, currentIndex: number) {
+export function deleteParagraphs(body: IDocumentBody, textLength: number, currentIndex: number, closeRemoveAfterFirstNew = false) {
const { paragraphs } = body;
const startIndex = currentIndex;
@@ -311,15 +311,18 @@ export function deleteParagraphs(body: IDocumentBody, textLength: number, curren
for (let i = 0, len = paragraphs.length; i < len; i++) {
const paragraph = paragraphs[i];
const { startIndex: index } = paragraph;
- if (startIndex === endIndex && endIndex === index) {
- const nextParagraph = paragraphs[i + 1];
- const isBullet = checkParagraphHasBullet(nextParagraph);
- if (isBullet && nextParagraph != null) {
- delete nextParagraph.bullet;
- delete nextParagraph.paragraphStyle?.hanging;
- delete nextParagraph.paragraphStyle?.indentStart;
- }
- } else if (index >= startIndex && index <= endIndex) {
+ // if (startIndex === endIndex && endIndex === index) {
+ // const nextParagraph = paragraphs[i + 1];
+ // const isBullet = checkParagraphHasBullet(nextParagraph);
+ // const isIndent = checkParagraphHasIndent(nextParagraph);
+ // if (isBullet && nextParagraph != null) {
+ // delete nextParagraph.bullet;
+ // } else if (isIndent && nextParagraph != null) {
+ // delete nextParagraph.paragraphStyle?.hanging;
+ // delete nextParagraph.paragraphStyle?.indentStart;
+ // }
+ // } else
+ if (index >= startIndex && index <= endIndex) {
removeParagraphs.push({
...paragraph,
startIndex: index - currentIndex,
@@ -336,7 +339,7 @@ export function deleteParagraphs(body: IDocumentBody, textLength: number, curren
removeAfterFirstNew = paragraph;
}
}
- if (removeAfterFirstNew != null) {
+ if (removeAfterFirstNew != null && closeRemoveAfterFirstNew === false) {
// When deleting a paragraph, the configuration of the paragraph
// in the beginning range should be retained. Due to the label design,
// the paragraph mark is located after the content, so when deleting,
diff --git a/packages/core/src/Docs/Apply/DeleteApply.ts b/packages/core/src/Docs/Apply/DeleteApply.ts
index 500e620eff..1f062f8d91 100644
--- a/packages/core/src/Docs/Apply/DeleteApply.ts
+++ b/packages/core/src/Docs/Apply/DeleteApply.ts
@@ -4,7 +4,7 @@ import { deleteContent } from '../../Shared/DocTool';
import { IDocumentBody } from '../../Types/Interfaces/IDocumentData';
import { deleteCustomBlocks, deleteCustomRanges, deleteParagraphs, deleteSectionBreaks, deleteTables, deleteTextRuns } from './Common';
-export function DeleteApply(document: DocumentModel, textLength: number, currentIndex: number, segmentId?: string) {
+export function DeleteApply(document: DocumentModel, textLength: number, currentIndex: number, segmentId?: string): IDocumentBody {
const doc = document.snapshot;
const bodyModel = document.getBodyModel(segmentId);
@@ -15,6 +15,10 @@ export function DeleteApply(document: DocumentModel, textLength: number, current
throw new Error('no body has changed');
}
+ if (textLength <= 0) {
+ return { dataStream: '' };
+ }
+
bodyModel.delete(currentIndex, textLength);
const deleBody = updateAttributeByDelete(body, textLength, currentIndex);
diff --git a/packages/core/src/Docs/Apply/UpdateAttributeApply.ts b/packages/core/src/Docs/Apply/UpdateAttributeApply.ts
index 860c4b2b22..97cdc9b51a 100644
--- a/packages/core/src/Docs/Apply/UpdateAttributeApply.ts
+++ b/packages/core/src/Docs/Apply/UpdateAttributeApply.ts
@@ -14,7 +14,7 @@ import {
insertTables,
insertTextRuns,
} from './Common';
-import { ICustomBlock, ICustomRange, IDocumentBody, IParagraph, ISectionBreak, ITable, ITextRun } from '../../Interfaces/IDocumentData';
+import { ICustomBlock, ICustomRange, IDocumentBody, IParagraph, ISectionBreak, ITable, ITextRun } from '../../Types/Interfaces/IDocumentData';
export function UpdateAttributeApply(
document: DocumentModel,
@@ -66,7 +66,7 @@ function updateAttribute(body: IDocumentBody, updateBody: IDocumentBody, textLen
function updateTextRuns(body: IDocumentBody, updateBody: IDocumentBody, textLength: number, currentIndex: number, coverType: UpdateDocsAttributeType) {
const { textRuns } = body;
- let { textRuns: updateDataTextRuns } = updateBody;
+ const { textRuns: updateDataTextRuns } = updateBody;
if (textRuns == null || updateDataTextRuns == null) {
return;
@@ -84,10 +84,11 @@ function updateTextRuns(body: IDocumentBody, updateBody: IDocumentBody, textLeng
function coverTextRun(updateDataTextRuns: ITextRun[], removeTextRuns: ITextRun[], coverType: UpdateDocsAttributeType) {
const newUpdateTextRun: ITextRun[] = [];
- for (let updateTextRun of updateDataTextRuns) {
- let { st: updateSt, ed: updateEd, ts: updateStyle } = updateTextRun;
+ for (const updateTextRun of updateDataTextRuns) {
+ let { st: updateSt } = updateTextRun;
+ const { ed: updateEd, ts: updateStyle } = updateTextRun;
let splitUpdateTextRuns: ITextRun[] = [];
- for (let removeTextRun of removeTextRuns) {
+ for (const removeTextRun of removeTextRuns) {
const { st: removeSt, ed: removeEd, ts: removeStyle, sId } = removeTextRun;
let newTs;
if (coverType === UpdateDocsAttributeType.COVER) {
@@ -158,19 +159,19 @@ function coverTextRun(updateDataTextRuns: ITextRun[], removeTextRuns: ITextRun[]
function updateParagraphs(body: IDocumentBody, updateBody: IDocumentBody, textLength: number, currentIndex: number, coverType: UpdateDocsAttributeType) {
const { paragraphs } = body;
- let { paragraphs: updateDataParagraphs } = updateBody;
+ const { paragraphs: updateDataParagraphs } = updateBody;
if (paragraphs == null || updateDataParagraphs == null) {
return;
}
- const removeParagraphs = deleteParagraphs(body, textLength, currentIndex);
+ const removeParagraphs = deleteParagraphs(body, textLength, currentIndex, true);
if (coverType !== UpdateDocsAttributeType.REPLACE) {
const newUpdateParagraphs: IParagraph[] = [];
- for (let updateParagraph of updateDataParagraphs) {
- let { startIndex: updateStartIndex, paragraphStyle: updateParagraphStyle, bullet: updateBullet } = updateParagraph;
+ for (const updateParagraph of updateDataParagraphs) {
+ const { startIndex: updateStartIndex, paragraphStyle: updateParagraphStyle, bullet: updateBullet } = updateParagraph;
let splitUpdateParagraphs: IParagraph[] = [];
- for (let removeParagraph of removeParagraphs) {
+ for (const removeParagraph of removeParagraphs) {
const { startIndex: removeStartIndex, paragraphStyle: removeParagraphStyle, bullet: removeBullet } = removeParagraph;
let newParagraphStyle;
let newBullet;
@@ -205,7 +206,7 @@ function updateParagraphs(body: IDocumentBody, updateBody: IDocumentBody, textLe
function updateSectionBreaks(body: IDocumentBody, updateBody: IDocumentBody, textLength: number, currentIndex: number, coverType: UpdateDocsAttributeType) {
const { sectionBreaks } = body;
- let { sectionBreaks: updateDataSectionBreaks } = updateBody;
+ const { sectionBreaks: updateDataSectionBreaks } = updateBody;
if (sectionBreaks == null || updateDataSectionBreaks == null) {
return;
@@ -214,10 +215,10 @@ function updateSectionBreaks(body: IDocumentBody, updateBody: IDocumentBody, tex
const removeSectionBreaks = deleteSectionBreaks(body, textLength, currentIndex);
if (coverType !== UpdateDocsAttributeType.REPLACE) {
const newUpdateSectionBreaks: ISectionBreak[] = [];
- for (let updateSectionBreak of updateDataSectionBreaks) {
- let { startIndex: updateStartIndex } = updateSectionBreak;
+ for (const updateSectionBreak of updateDataSectionBreaks) {
+ const { startIndex: updateStartIndex } = updateSectionBreak;
let splitUpdateSectionBreaks: ISectionBreak[] = [];
- for (let removeSectionBreak of removeSectionBreaks) {
+ for (const removeSectionBreak of removeSectionBreaks) {
const { startIndex: removeStartIndex } = removeSectionBreak;
if (updateStartIndex === removeStartIndex) {
if (coverType === UpdateDocsAttributeType.COVER) {
@@ -247,7 +248,7 @@ function updateSectionBreaks(body: IDocumentBody, updateBody: IDocumentBody, tex
function updateCustomBlocks(body: IDocumentBody, updateBody: IDocumentBody, textLength: number, currentIndex: number, coverType: UpdateDocsAttributeType) {
const { customBlocks } = body;
- let { customBlocks: updateDataCustomBlocks } = updateBody;
+ const { customBlocks: updateDataCustomBlocks } = updateBody;
if (customBlocks == null || updateDataCustomBlocks == null) {
return;
@@ -256,10 +257,10 @@ function updateCustomBlocks(body: IDocumentBody, updateBody: IDocumentBody, text
const removeCustomBlocks = deleteCustomBlocks(body, textLength, currentIndex);
if (coverType !== UpdateDocsAttributeType.REPLACE) {
const newUpdateCustomBlocks: ICustomBlock[] = [];
- for (let updateCustomBlock of updateDataCustomBlocks) {
- let { startIndex: updateStartIndex } = updateCustomBlock;
+ for (const updateCustomBlock of updateDataCustomBlocks) {
+ const { startIndex: updateStartIndex } = updateCustomBlock;
let splitUpdateCustomBlocks: ICustomBlock[] = [];
- for (let removeCustomBlock of removeCustomBlocks) {
+ for (const removeCustomBlock of removeCustomBlocks) {
const { startIndex: removeStartIndex } = removeCustomBlock;
if (updateStartIndex === removeStartIndex) {
if (coverType === UpdateDocsAttributeType.COVER) {
@@ -289,7 +290,7 @@ function updateCustomBlocks(body: IDocumentBody, updateBody: IDocumentBody, text
function updateTables(body: IDocumentBody, updateBody: IDocumentBody, textLength: number, currentIndex: number, coverType: UpdateDocsAttributeType) {
const { tables } = body;
- let { tables: updateDataTables } = updateBody;
+ const { tables: updateDataTables } = updateBody;
if (tables == null || updateDataTables == null) {
return;
@@ -298,10 +299,10 @@ function updateTables(body: IDocumentBody, updateBody: IDocumentBody, textLength
const removeTables = deleteTables(body, textLength, currentIndex);
if (coverType !== UpdateDocsAttributeType.REPLACE) {
const newUpdateTables: ITable[] = [];
- for (let updateTable of updateDataTables) {
- let { startIndex: updateStartIndex, endIndex: updateEndIndex } = updateTable;
+ for (const updateTable of updateDataTables) {
+ const { startIndex: updateStartIndex, endIndex: updateEndIndex } = updateTable;
let splitUpdateTables: ITable[] = [];
- for (let removeTable of removeTables) {
+ for (const removeTable of removeTables) {
const { startIndex: removeStartIndex, endIndex: removeEndIndex } = removeTable;
if (removeStartIndex >= updateStartIndex && removeEndIndex <= updateEndIndex) {
if (coverType === UpdateDocsAttributeType.COVER) {
@@ -331,7 +332,7 @@ function updateTables(body: IDocumentBody, updateBody: IDocumentBody, textLength
function updateCustomRanges(body: IDocumentBody, updateBody: IDocumentBody, textLength: number, currentIndex: number, coverType: UpdateDocsAttributeType) {
const { customRanges } = body;
- let { tables: updateDataCustomRanges } = updateBody;
+ const { tables: updateDataCustomRanges } = updateBody;
if (customRanges == null || updateDataCustomRanges == null) {
return;
@@ -340,10 +341,10 @@ function updateCustomRanges(body: IDocumentBody, updateBody: IDocumentBody, text
const removeCustomRanges = deleteCustomRanges(body, textLength, currentIndex);
if (coverType !== UpdateDocsAttributeType.REPLACE) {
const newUpdateCustomRanges: ICustomRange[] = [];
- for (let updateCustomRange of updateDataCustomRanges) {
- let { startIndex: updateStartIndex, endIndex: updateEndIndex } = updateCustomRange;
+ for (const updateCustomRange of updateDataCustomRanges) {
+ const { startIndex: updateStartIndex, endIndex: updateEndIndex } = updateCustomRange;
let splitUpdateCustomRanges: ICustomRange[] = [];
- for (let removeCustomRange of removeCustomRanges) {
+ for (const removeCustomRange of removeCustomRanges) {
const { startIndex: removeStartIndex, endIndex: removeEndIndex } = removeCustomRange;
if (removeStartIndex >= updateStartIndex && removeEndIndex <= updateEndIndex) {
if (coverType === UpdateDocsAttributeType.COVER) {
diff --git a/packages/core/src/Docs/Domain/DataStreamTreeNode.ts b/packages/core/src/Docs/Domain/DataStreamTreeNode.ts
index 1763e67d7d..b817881784 100644
--- a/packages/core/src/Docs/Domain/DataStreamTreeNode.ts
+++ b/packages/core/src/Docs/Domain/DataStreamTreeNode.ts
@@ -11,7 +11,9 @@ export class DataStreamTreeNode {
endIndex: number;
- isBullet: boolean = false;
+ // isBullet: boolean = false;
+
+ // isIndent: boolean = false;
blocks: number[] = [];
diff --git a/packages/core/src/Docs/Domain/DocumentBodyModel.ts b/packages/core/src/Docs/Domain/DocumentBodyModel.ts
index b6586bf432..de89b8354a 100644
--- a/packages/core/src/Docs/Domain/DocumentBodyModel.ts
+++ b/packages/core/src/Docs/Domain/DocumentBodyModel.ts
@@ -1,7 +1,5 @@
import { IDocumentBody, ITextRun } from '../../Types/Interfaces/IDocumentData';
-import { checkParagraphHasBullet } from '../../Shared/DocTool';
-
import { Nullable } from '../../Shared/Types';
import { DataStreamTreeNode } from './DataStreamTreeNode';
import { DataStreamTreeNodeType, DataStreamTreeTokenType } from './Types';
@@ -56,7 +54,7 @@ export class DocumentBodyModelSimple {
const sectionList: DataStreamTreeNode[] = [];
let nodeList: DataStreamTreeNode[] = [];
let currentBlocks: number[] = [];
- let paragraphIndex = 0;
+ // let paragraphIndex = 0;
for (let i = 0; i < dataStreamLen; i++) {
const char = dataStream[i];
if (char === DataStreamTreeTokenType.PARAGRAPH) {
@@ -66,10 +64,13 @@ export class DocumentBodyModelSimple {
content
);
- const isBullet = this._checkParagraphBullet(paragraphIndex++);
+ // const isBullet = this._checkParagraphBullet(paragraphIndex);
+ // const isIndent = isBullet === true ? true : this._checkParagraphIndent(paragraphIndex);
+ // paragraphIndex++;
node.setIndexRange(i - content.length + 1, i);
node.addBlocks(currentBlocks);
- node.isBullet = isBullet;
+ // node.isBullet = isBullet;
+ // node.isIndent = isIndent;
nodeList.push(node);
content = '';
currentBlocks = [];
@@ -159,14 +160,23 @@ export class DocumentBodyModelSimple {
parent.setIndexRange(children[0].startIndex - startOffset, children[children.length - 1].endIndex + 1);
}
- private _checkParagraphBullet(index: number) {
- const { paragraphs } = this.body;
- if (paragraphs == null) {
- return false;
- }
+ // private _checkParagraphBullet(index: number) {
+ // const { paragraphs } = this.body;
+ // if (paragraphs == null) {
+ // return false;
+ // }
- return checkParagraphHasBullet(paragraphs[index]);
- }
+ // return checkParagraphHasBullet(paragraphs[index]);
+ // }
+
+ // private _checkParagraphIndent(index: number) {
+ // const { paragraphs } = this.body;
+ // if (paragraphs == null) {
+ // return false;
+ // }
+
+ // return checkParagraphHasIndent(paragraphs[index]);
+ // }
}
export class DocumentBodyModel extends DocumentBodyModelSimple {
@@ -293,14 +303,14 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {
continue;
}
- if (nextNode.isBullet) {
- i++;
- continue;
- } else {
- node.minus(startIndex, endIndex);
- node.merge(nextNode);
- nodeCount--;
- }
+ // if (nextNode.isBullet || nextNode.isIndent) {
+ // i++;
+ // continue;
+ // } else {
+ node.minus(startIndex, endIndex);
+ node.merge(nextNode);
+ nodeCount--;
+ // }
}
// else if (node.nodeType === DataStreamTreeNodeType.SECTION_BREAK) {
// } else if (node.nodeType === DataStreamTreeNodeType.TABLE) {
@@ -321,12 +331,12 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {
// The third case.
// The text selection left contains the current node
node.minus(st, endIndex);
- if (mergeNode) {
+ if (mergeNode != null) {
mergeNode.merge(node);
mergeNode = null;
+ nodeCount--;
+ continue;
}
- nodeCount--;
- continue;
} else if (startIndex > st && startIndex < ed) {
// The fourth case.
// The text selection right contains the current node
diff --git a/packages/core/src/Docs/Domain/DocumentModel.ts b/packages/core/src/Docs/Domain/DocumentModel.ts
index 49f0d3d552..da6da0c68e 100644
--- a/packages/core/src/Docs/Domain/DocumentModel.ts
+++ b/packages/core/src/Docs/Domain/DocumentModel.ts
@@ -47,6 +47,10 @@ export class DocumentModelSimple {
return this.snapshot.lists;
}
+ get body() {
+ return this.snapshot.body;
+ }
+
getBodyModel(segmentId?: string) {
if (segmentId == null) {
return this.bodyModel;
@@ -172,7 +176,7 @@ export class DocumentModel extends DocumentModelSimple {
return this;
}
- update(body: IDocumentBody, range: ITextSelectionRange, coverType = UpdateDocsAttributeType.COVER, segmentId?: string) {
+ update(updateBody: IDocumentBody, range: ITextSelectionRange, coverType = UpdateDocsAttributeType.COVER, segmentId?: string) {
const { cursorStart, cursorEnd, isEndBack, isStartBack } = range;
const actionList = [];
@@ -190,7 +194,7 @@ export class DocumentModel extends DocumentModelSimple {
actionName: DOC_ACTION_NAMES.RETAIN_ACTION_NAME,
len: textEnd - textStart + 1,
coverType,
- body,
+ body: updateBody,
segmentId,
});
diff --git a/packages/core/src/Docs/Domain/Types.ts b/packages/core/src/Docs/Domain/Types.ts
index ad493d4bf0..0d39c7bc79 100644
--- a/packages/core/src/Docs/Domain/Types.ts
+++ b/packages/core/src/Docs/Domain/Types.ts
@@ -38,4 +38,6 @@ export enum DataStreamTreeTokenType {
CUSTOM_BLOCK = '\b', // 图片 mention等不参与文档流的场景
LETTER = '',
+
+ SPACE = ' ',
}
diff --git a/packages/core/src/Shared/DocTool.ts b/packages/core/src/Shared/DocTool.ts
index 819fcebc57..8829d8753c 100644
--- a/packages/core/src/Shared/DocTool.ts
+++ b/packages/core/src/Shared/DocTool.ts
@@ -1,4 +1,4 @@
-import { IParagraph } from '../Types/Interfaces/IDocumentData';
+import { IParagraph, IParagraphStyle } from '../Types/Interfaces/IDocumentData';
export function horizontalLineSegmentsSubtraction(A1: number, A2: number, B1: number, B2: number) {
// 确保A1 < A2, B1 < B2
@@ -52,7 +52,15 @@ export function checkParagraphHasIndent(paragraph: IParagraph) {
}
const paragraphStyle = paragraph.paragraphStyle;
- if (paragraphStyle?.indentStart == null || paragraphStyle?.indentStart === 0) {
+ return checkParagraphHasIndentByStyle(paragraphStyle);
+}
+
+export function checkParagraphHasIndentByStyle(paragraphStyle?: IParagraphStyle) {
+ if (paragraphStyle == null) {
+ return false;
+ }
+
+ if (((paragraphStyle?.indentStart == null || paragraphStyle?.indentStart === 0) && paragraphStyle?.hanging == null) || paragraphStyle?.hanging === 0) {
return false;
}
diff --git a/packages/core/src/Shared/index.ts b/packages/core/src/Shared/index.ts
index 17b46b707a..2a96aae714 100644
--- a/packages/core/src/Shared/index.ts
+++ b/packages/core/src/Shared/index.ts
@@ -28,4 +28,5 @@ export * from './TypeStore';
export * from './HashAlgorithm';
export * from './Compare';
export * from './CommandEnum';
+export * from './DocTool';
// @endindex