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

DR-Univere9914d0c9025
diff --git a/packages/base-docs/src/Basic/DemoData.ts b/packages/base-docs/src/Basic/DemoData.ts
index 9fcfd56a70..eb4057c69a 100644
--- a/packages/base-docs/src/Basic/DemoData.ts
+++ b/packages/base-docs/src/Basic/DemoData.ts
@@ -304,8 +304,8 @@ export const docsDemoData: IDocumentData = {
},
fourParagraph: {
blockId: 'fourParagraph',
- st: 230,
- ed: 321,
+ st: 231,
+ ed: 322,
blockType: BlockType.PARAGRAPH,
paragraph: {
bullet: {
@@ -318,8 +318,8 @@ export const docsDemoData: IDocumentData = {
elements: {
oneElement: {
eId: 'oneElement',
- st: 230,
- ed: 285,
+ st: 231,
+ ed: 286,
et: ParagraphElementType.TEXT_RUN,
tr: {
ct: '国家主席习近平以视频方式出席在上海举行的第五届中国国际进口博览会开幕式并发表题为《共创开放繁荣的美好未来》的致辞',
@@ -327,8 +327,8 @@ export const docsDemoData: IDocumentData = {
},
twoElement: {
eId: 'twoElement',
- st: 286,
- ed: 321,
+ st: 287,
+ ed: 322,
et: ParagraphElementType.TEXT_RUN,
tr: {
ct: '中国将推动各国各方共享深化国际合作机遇,全面深入参与世界贸易组织改革谈判',
@@ -349,8 +349,8 @@ export const docsDemoData: IDocumentData = {
},
fiveParagraph: {
blockId: 'fiveParagraph',
- st: 322,
- ed: 399,
+ st: 323,
+ ed: 400,
blockType: BlockType.PARAGRAPH,
paragraph: {
bullet: {
@@ -363,8 +363,8 @@ export const docsDemoData: IDocumentData = {
elements: {
oneElement: {
eId: 'oneElement',
- st: 322,
- ed: 359,
+ st: 323,
+ ed: 360,
et: ParagraphElementType.TEXT_RUN,
tr: {
ct: '中国男排两名现役国手彭世坤和张秉龙分别效力的三得利太阳鸟和东京大熊本轮遭遇。',
@@ -372,8 +372,8 @@ export const docsDemoData: IDocumentData = {
},
twoElement: {
eId: 'twoElement',
- st: 360,
- ed: 399,
+ st: 361,
+ ed: 400,
et: ParagraphElementType.TEXT_RUN,
tr: {
ct: '双方经过激战,主场作战的卫冕冠军三得利技高一筹3-2逆转击败对手,力夺第3场胜利',
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 412ffd9a08..d52dfcabcc 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
@@ -110,6 +110,7 @@ function _divideOperator(
// 一个字符超列宽
setColumnFullState(column, true);
if (isBlankColumn(column)) {
+ console.log(spanGroup);
divide.spanGroup.push(...spanGroup);
} else {
_columnOperator(spanGroup, pages, sectionBreakConfig, paragraphConfig, elementIndex, isFirstSpan, defaultSpanLineHeight);
@@ -157,6 +158,7 @@ function _divideOperator(
for (let span of spanGroup) {
span.parent = divide;
}
+ // console.log('spanGroup', spanGroup, spanGroup.length, spanGroup[0].content);
divide.spanGroup.push(...spanGroup);
}
} else {
diff --git a/packages/base-render/src/Component/Docs/Common/Page.ts b/packages/base-render/src/Component/Docs/Common/Page.ts
index a04ca12eba..36343c8edf 100644
--- a/packages/base-render/src/Component/Docs/Common/Page.ts
+++ b/packages/base-render/src/Component/Docs/Common/Page.ts
@@ -176,7 +176,7 @@ function _createSkeletonHeader(
});
const areaPage = createSkeletonPage(headerConfig, skeletonResourceReference);
- const page = dealWithBlocks(blockElementArray, areaPage, headerConfig, skeletonResourceReference)[0];
+ const page = dealWithBlocks(blockElementArray, areaPage, headerConfig, skeletonResourceReference).pages[0];
const column = page.sections[0].columns[0];
const height = column.height || 0;
const { skeDrawings, st, ed } = page;
diff --git a/packages/base-render/src/Component/Docs/Common/TextSelection.ts b/packages/base-render/src/Component/Docs/Common/TextSelection.ts
index 29b0d7302b..e4bf842c4c 100644
--- a/packages/base-render/src/Component/Docs/Common/TextSelection.ts
+++ b/packages/base-render/src/Component/Docs/Common/TextSelection.ts
@@ -55,6 +55,12 @@ 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;
@@ -74,6 +80,8 @@ export class TextSelection {
private _anchorShape: Nullable<Rect>;
+ private _cursor: number = 0;
+
private _currentStartState: ICurrentNodePositionState = {
page: NodePositionStateType.NORMAL,
section: NodePositionStateType.NORMAL,
@@ -377,8 +385,13 @@ export class TextSelection {
private _getRangePointData(startOrigin: INodePosition, endOrigin: INodePosition, skeleton: DocumentSkeleton) {
const pointGroup: IPoint[][] = [];
+ const cursorList: ISelectionCursor[] = [];
+
if (startOrigin == null || endOrigin == null) {
- return;
+ return {
+ pointGroup,
+ cursorList,
+ };
}
const { start, end } = this._compareNodePosition(startOrigin, endOrigin);
@@ -389,7 +402,8 @@ export class TextSelection {
this._selectionIterator(start!, end!, skeleton, (start_sp, end_sp, divide, line) => {
const { lineHeight } = line;
- const spanGroup = divide.spanGroup;
+
+ const { spanGroup, st } = divide;
const { x: startX, y: startY } = this._Liquid;
@@ -404,11 +418,19 @@ export class TextSelection {
const lastSpanLeft = lastSpan?.left || 0;
const lastSpanWidth = lastSpan?.width || 0;
- let isList = firstSpan?.spanType === SpanType.LIST;
+ let isCurrentList = firstSpan?.spanType === SpanType.LIST;
+
+ let hasList = spanGroup[0]?.spanType === SpanType.LIST;
+
+ let cursorStart = start_sp + st;
+
+ let cursorEnd = end_sp + st;
+
+ const isCollapse = start === end;
if (start_sp === 0 && end_sp === spanGroup.length - 1) {
position = {
- startX: startX + firstSpanLeft + (isList ? firstSpanWidth : 0),
+ startX: startX + firstSpanLeft + (isCurrentList ? firstSpanWidth : 0),
startY,
endX: startX + lastSpanLeft + lastSpanWidth,
endY: startY + lineHeight,
@@ -416,10 +438,22 @@ export class TextSelection {
} else {
// console.log('span', firstSpan, lastSpan, start_sp, end_sp);
+ const isStartBackFin = (isStartBack || start_sp === 0) && !isCurrentList;
+
+ const isEndBackFin = isEndBack && end_sp !== spanGroup.length - 1;
+
+ cursorStart += isStartBackFin ? 0 : 1;
+
+ cursorStart -= hasList ? 1 : 0;
+
+ cursorEnd += isEndBackFin ? 0 : 1;
+
+ cursorEnd -= hasList ? 1 : 0;
+
position = {
- startX: startX + firstSpanLeft + ((isStartBack || start_sp === 0) && !isList ? 0 : firstSpanWidth),
+ startX: startX + firstSpanLeft + (isStartBackFin ? 0 : firstSpanWidth),
startY,
- endX: startX + lastSpanLeft + (!isEndBack || end_sp === spanGroup.length - 1 ? lastSpanWidth : 0),
+ endX: startX + lastSpanLeft + (isEndBackFin ? 0 : lastSpanWidth),
endY: startY + lineHeight,
};
@@ -430,8 +464,17 @@ export class TextSelection {
}
pointGroup.push(this._pushToPoints(position));
+ cursorList.push({
+ cursorStart,
+ cursorEnd,
+ isCollapse,
+ });
});
- return pointGroup;
+
+ return {
+ pointGroup,
+ cursorList,
+ };
}
private _selectionIterator(
@@ -517,6 +560,22 @@ export class TextSelection {
}
}
+ private _setCursor(cursorList: ISelectionCursor[]) {
+ if (cursorList.length === 0) {
+ return;
+ }
+
+ const firstCursor = cursorList[0];
+
+ this._cursor = firstCursor.cursorStart;
+
+ console.log('this._cursor', this._cursor);
+ }
+
+ get cursor() {
+ return this._cursor;
+ }
+
getAnchor() {
return this._anchorShape;
}
@@ -607,14 +666,17 @@ export class TextSelection {
}
if (this.isCollapsed()) {
- const pointGroup = this._getRangePointData(start!, start!, skeleton);
- pointGroup && this._createAndUpdateAnchor(pointGroup, documents.left, documents.top);
+ const data = this._getRangePointData(start!, start!, skeleton);
+ const { pointGroup, cursorList } = data;
+ this._setCursor(cursorList);
+ pointGroup.length > 0 && this._createAndUpdateAnchor(pointGroup, documents.left, documents.top);
return;
}
- const pointGroup = this._getRangePointData(start!, end!, skeleton);
-
- pointGroup && this._createAndUpdateRange(pointGroup, documents.left, documents.top);
+ const data = this._getRangePointData(start!, end!, skeleton);
+ const { pointGroup, cursorList } = data;
+ this._setCursor(cursorList);
+ pointGroup.length > 0 && this._createAndUpdateRange(pointGroup, documents.left, documents.top);
}
getStart() {
diff --git a/packages/base-render/src/Component/Docs/Common/Tools.ts b/packages/base-render/src/Component/Docs/Common/Tools.ts
index 202c789c6e..048e8a03f2 100644
--- a/packages/base-render/src/Component/Docs/Common/Tools.ts
+++ b/packages/base-render/src/Component/Docs/Common/Tools.ts
@@ -264,25 +264,25 @@ export function updateBlockIndex(pages: IDocumentSkeletonPage[]) {
// return;
// }
- let prePageStartIndex = firstPageStartIndex;
+ let prePageStartIndex = -1;
for (let page of pages) {
const { sections } = page;
let pageStartIndex = prePageStartIndex;
let pageEndIndex = pageStartIndex;
- let preSectionStartIndex = 0;
+ let preSectionStartIndex = pageStartIndex;
let maxPageWidth = -Infinity;
let pageHeight = 0;
for (let section of sections) {
const { columns } = section;
let sectionStartIndex = preSectionStartIndex;
let sectionEndIndex = pageStartIndex;
- let preColumnStartIndex = 0;
+ let preColumnStartIndex = sectionStartIndex;
let maxSectionHeight = -Infinity;
for (let column of columns) {
const { lines } = column;
let columStartIndex = preColumnStartIndex;
let columnEndIndex = columStartIndex;
- let preLineStartIndex = 0;
+ let preLineStartIndex = columStartIndex;
let columnHeight = 0;
let maxColumnWidth = -Infinity;
let preLine: Nullable<IDocumentSkeletonLine> = null;
@@ -290,7 +290,7 @@ export function updateBlockIndex(pages: IDocumentSkeletonPage[]) {
const { divides, lineHeight } = line;
let lineStartIndex = preLineStartIndex;
let lineEndIndex = lineStartIndex;
- let preDivideStartIndex = 0;
+ let preDivideStartIndex = lineStartIndex;
let actualWidth = 0;
let maxLineAsc = 0;
columnHeight += lineHeight;
@@ -298,16 +298,23 @@ export function updateBlockIndex(pages: IDocumentSkeletonPage[]) {
for (let i = 0; i < divideLength; i++) {
const divide = divides[i];
const { spanGroup } = divide;
+
+ if (spanGroup.length === 0) {
+ continue;
+ }
+
let divStartIndex = preDivideStartIndex;
let divEndIndex = divStartIndex;
for (let span of spanGroup) {
const increaseValue = span.spanType === SpanType.LIST ? 0 : span.count || 1;
- pageEndIndex += increaseValue;
- sectionEndIndex += increaseValue;
- columnEndIndex += increaseValue;
- lineEndIndex += increaseValue;
+ // pageEndIndex += increaseValue;
+ // sectionEndIndex += increaseValue;
+ // columnEndIndex += increaseValue;
+ // lineEndIndex += increaseValue;
divEndIndex += increaseValue;
+ // console.log('span', span, increaseValue, divEndIndex);
+
const bBox = span.bBox;
const { ba } = bBox;
@@ -328,39 +335,46 @@ export function updateBlockIndex(pages: IDocumentSkeletonPage[]) {
actualWidth += divide.left;
}
- divide.st = divStartIndex;
- divide.ed = divEndIndex;
+ divide.st = divStartIndex === 0 ? 0 : divStartIndex + 1;
+ divide.ed = divEndIndex >= divide.st ? divEndIndex : divide.st;
+ preDivideStartIndex = divide.ed;
+ // console.log('divide', divide, divide.st, divide.ed);
}
- line.st = lineStartIndex;
- line.ed = lineEndIndex;
+ line.st = lineStartIndex === 0 ? 0 : lineStartIndex + 1;
+ line.ed = preDivideStartIndex >= line.st ? preDivideStartIndex : line.st;
line.width = actualWidth;
line.asc = maxLineAsc;
maxColumnWidth = Math.max(maxColumnWidth, actualWidth);
line.top = (preLine?.top || 0) + (preLine?.lineHeight || 0);
preLine = line;
+ preLineStartIndex = line.ed;
}
- column.st = columStartIndex;
- column.ed = columnEndIndex;
+ column.st = columStartIndex === 0 ? 0 : columStartIndex + 1;
+ column.ed = preLineStartIndex >= column.st ? preLineStartIndex : column.st;
column.height = columnHeight;
if (column.width === Infinity) {
column.width = maxColumnWidth;
}
maxPageWidth = Math.max(maxPageWidth, maxColumnWidth);
maxSectionHeight = Math.max(maxSectionHeight, column.height);
+
+ preColumnStartIndex = column.ed;
}
- section.st = sectionStartIndex;
- section.ed = sectionEndIndex;
+ section.st = sectionStartIndex === 0 ? 0 : sectionStartIndex + 1;
+ section.ed = preColumnStartIndex >= section.st ? preColumnStartIndex : section.st;
section.height = maxSectionHeight;
pageHeight += maxSectionHeight;
+
+ preSectionStartIndex = preColumnStartIndex;
}
- page.st = pageStartIndex;
- page.ed = pageEndIndex;
+ page.st = pageStartIndex === 0 ? 0 : pageStartIndex + 1;
+ page.ed = preSectionStartIndex >= page.st ? preSectionStartIndex : page.st;
page.height = pageHeight;
page.width = maxPageWidth;
- prePageStartIndex = pageEndIndex;
+ prePageStartIndex = page.ed;
}
}