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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(doc): inline format optimization in render (#486)
Ran Luoa6d88537b1c3
diff --git a/examples/univer-doc-ts/src/main.tsx b/examples/univer-doc-ts/src/main.tsxindex 4d7505be12..c2a3030671 100644--- a/examples/univer-doc-ts/src/main.tsx+++ b/examples/univer-doc-ts/src/main.tsx@@ -1,7 +1,7 @@import { DocPlugin } from '@univerjs/base-docs';import { RenderEngine } from '@univerjs/base-render';import { UIPlugin } from '@univerjs/base-ui';-import { DEFAULT_DOCUMENT_DATA_EN } from '@univerjs/common-plugin-data';+import { DEFAULT_DOCUMENT_DATA_CN } from '@univerjs/common-plugin-data';import { ImagePlugin } from '@univerjs/common-plugin-image';import { LocaleType, Univer } from '@univerjs/core';import { defaultTheme } from '@univerjs/design';@@ -37,7 +37,7 @@ univer.registerPlugin(DocUIPlugin, {univer.registerPlugin(ImagePlugin);-univer.createUniverDoc(DEFAULT_DOCUMENT_DATA_EN);+univer.createUniverDoc(DEFAULT_DOCUMENT_DATA_CN);// use for console testdeclare global {diff --git a/packages/base-docs/src/controllers/page-render.controller.ts b/packages/base-docs/src/controllers/page-render.controller.tsindex 49216774de..7f426b39a6 100644--- a/packages/base-docs/src/controllers/page-render.controller.ts+++ b/packages/base-docs/src/controllers/page-render.controller.ts@@ -58,9 +58,11 @@ export class PageRenderController extends Disposable {if ([DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(unitId)) {return;}+ // Draw page bordersconst { page, pageLeft, pageTop, ctx } = config;- const { width, height, marginBottom, marginLeft, marginRight, marginTop } = page;+ const { width, height } = page;ctx.save();+ // eslint-disable-next-line no-magic-numbersctx.translate(pageLeft - 0.5, pageTop - 0.5);Rect.drawWith(ctx, {width: pageSize?.width || width,diff --git a/packages/base-render/src/basics/font-cache.ts b/packages/base-render/src/basics/font-cache.tsindex 8c77063384..33c0098c37 100644--- a/packages/base-render/src/basics/font-cache.ts+++ b/packages/base-render/src/basics/font-cache.ts@@ -117,14 +117,18 @@ export class FontCache {if (this._fontDataMap.size === 0) {return getDefaultBaselineOffset(fontSize);}+const fontFamilyList = fontFamily.split(',');+for (let ff of fontFamilyList) {ff = ff.replace(/'/g, '');const fontData = this._fontDataMap.get(ff);if (!fontData) {continue;}+const { subscriptSizeRatio, subscriptOffset, superscriptSizeRatio, superscriptOffset } = fontData;+return {sbr: subscriptSizeRatio,sbo: subscriptOffset * fontSize,@@ -161,6 +165,7 @@ export class FontCache {const { fontString, fontSize, fontFamily } = fontStyle;let bBox = this._getBoundingBoxByFont(fontFamily, fontSize);+if (!bBox) {// if (content === DataStreamTreeTokenType.PARAGRAPH) {// content = '0';@@ -168,6 +173,7 @@ export class FontCache {const measureText = this.getMeasureText(content, fontString);bBox = this._calculateBoundingBoxByMeasureText(measureText, fontSize);}+return bBox;}@@ -309,6 +315,7 @@ export class FontCache {actualBoundingBoxAscent,actualBoundingBoxDescent,} = textCache;+return {width,ba: fontBoundingBoxAscent,diff --git a/packages/base-render/src/basics/tools.ts b/packages/base-render/src/basics/tools.tsindex f7cf82f7da..8be31e25bb 100644--- a/packages/base-render/src/basics/tools.ts+++ b/packages/base-render/src/basics/tools.ts@@ -488,6 +488,7 @@ export function pxToInch(px: number) {export function getScale(parentScale: IScale) {const { scaleX = 1, scaleY = 1 } = parentScale;+return Math.max(scaleX, scaleY);}diff --git a/packages/base-render/src/components/docs/block/paragraph/paragraph.ts b/packages/base-render/src/components/docs/block/paragraph/paragraph.tsindex 1e74cf0238..75e7106415 100644--- a/packages/base-render/src/components/docs/block/paragraph/paragraph.ts+++ b/packages/base-render/src/components/docs/block/paragraph/paragraph.ts@@ -213,7 +213,7 @@ export function dealWidthParagraph(i = newCharIndex;newSpanGroup = spanGroup;} else {- const span = createSkeletonWordSpan(char, fontCreateConfig); // measureText收敛到create中执行+ const span = createSkeletonWordSpan(char, fontCreateConfig); // measureText 收敛到 create 中执行newSpanGroup.push(span);}diff --git a/packages/base-render/src/components/docs/common/convert-cursor.ts b/packages/base-render/src/components/docs/common/convert-cursor.tsindex f8e90e82ea..fde54f1c48 100644--- a/packages/base-render/src/components/docs/common/convert-cursor.ts+++ b/packages/base-render/src/components/docs/common/convert-cursor.ts@@ -135,7 +135,7 @@ export function getOneTextSelectionRange(rangeList: ITextRange[]): Nullable<ITex}export class NodePositionConvertToCursor {- private _Liquid = new Liquid();+ private _liquid = new Liquid();private _documentOffsetConfig: Nullable<IDocumentOffsetConfig>;@@ -166,28 +166,30 @@ export class NodePositionConvertToCursor {}getRangePointData(startOrigin: Nullable<INodePosition>, endOrigin: Nullable<INodePosition>) {- const pointGroup: IPoint[][] = [];+ const borderBoxPointGroup: IPoint[][] = [];+ const contentBoxPointGroup: IPoint[][] = [];const cursorList: ITextRange[] = [];if (startOrigin == null || endOrigin == null) {return {- pointGroup,+ borderBoxPointGroup,+ contentBoxPointGroup,cursorList,};}const { start, end } = compareNodePosition(startOrigin, endOrigin);- // eslint-disable-next-line max-lines-per-functionthis._selectionIterator(start!, end!, (start_sp, end_sp, isFirst, isLast, divide, line) => {- const { lineHeight } = line;+ const { lineHeight, marginTop, paddingTop, contentHeight } = line;const { spanGroup, st } = divide;- const { x: startX, y: startY } = this._Liquid;+ const { x: startX, y: startY } = this._liquid;- let position: IPosition;+ let borderBoxPosition: IPosition;+ let contentBoxPosition: IPosition;const firstSpan = spanGroup[start_sp];const lastSpan = spanGroup[end_sp];@@ -215,12 +217,19 @@ export class NodePositionConvertToCursor {if (start_sp === 0 && end_sp === spanGroup.length - 1) {endOffset -= hasList ? 1 : 0;- position = {+ borderBoxPosition = {startX: startX + firstSpanLeft + (isCurrentList ? firstSpanWidth : 0),startY,endX: startX + lastSpanLeft + lastSpanWidth,endY: startY + lineHeight,};++ contentBoxPosition = {+ startX: startX + firstSpanLeft + (isCurrentList ? firstSpanWidth : 0),+ startY: startY + marginTop,+ endX: startX + lastSpanLeft + lastSpanWidth,+ endY: startY + marginTop + contentHeight,+ };} else {const isStartBackFin = isStartBack && !isCurrentList;@@ -234,20 +243,28 @@ export class NodePositionConvertToCursor {endOffset -= hasList && !isCurrentList ? 1 : 0;- position = {+ borderBoxPosition = {startX: startX + firstSpanLeft + (isStartBackFin ? 0 : firstSpanWidth),startY,endX: startX + lastSpanLeft + (isEndBack ? 0 : lastSpanWidth),endY: startY + lineHeight,};+ contentBoxPosition = {+ startX: startX + firstSpanLeft + (isStartBackFin ? 0 : firstSpanWidth),+ startY: startY + marginTop,+ endX: startX + lastSpanLeft + (isEndBack ? 0 : lastSpanWidth),+ endY: startY + marginTop + contentHeight,+ };+// for (let sp = start_sp; sp <= end_sp; sp++) {// const span = spanGroup[sp];// const { width: spanWidth, left: spanLeft } = span;// }}- pointGroup.push(this._pushToPoints(position));+ borderBoxPointGroup.push(this._pushToPoints(borderBoxPosition));+ contentBoxPointGroup.push(this._pushToPoints(contentBoxPosition));cursorList.push({startOffset: isStartBack ? startOffset : startOffset + 1,@@ -257,7 +274,8 @@ export class NodePositionConvertToCursor {});return {- pointGroup,+ borderBoxPointGroup,+ contentBoxPointGroup,cursorList,};}@@ -372,6 +390,7 @@ export class NodePositionConvertToCursor {private _pushToPoints(position: IPosition) {const { startX, startY, endX, endY } = position;const points: Array<{ x: number; y: number }> = [];+points.push({x: startX,y: startY,@@ -391,14 +410,15 @@ export class NodePositionConvertToCursor {x: startX,y: endY,});+points.push({x: startX,y: startY,});+return points;}- // eslint-disable-next-line max-lines-per-functionprivate _selectionIterator(startPosition: INodePosition,endPosition: INodePosition,@@ -419,7 +439,7 @@ export class NodePositionConvertToCursor {return [];}- this._Liquid.reset();+ this._liquid.reset();const skeletonData = skeleton.getSkeletonData();@@ -443,7 +463,7 @@ export class NodePositionConvertToCursor {for (let p = 0; p <= pageIndex - 1; p++) {const page = pages[p];- this._Liquid.translatePage(page, pageLayoutType, pageMarginLeft, pageMarginTop);+ this._liquid.translatePage(page, pageLayoutType, pageMarginLeft, pageMarginTop);}for (let p = pageIndex; p <= endPageIndex; p++) {@@ -457,8 +477,8 @@ export class NodePositionConvertToCursor {sections.length - 1,p);- this._Liquid.translateSave();- this._Liquid.translatePagePadding(page);+ this._liquid.translateSave();+ this._liquid.translatePagePadding(page);for (let s = start_s; s <= end_s; s++) {const section = sections[s];@@ -471,7 +491,7 @@ export class NodePositionConvertToCursor {s);- this._Liquid.translateSection(section);+ this._liquid.translateSection(section);for (let c = start_c; c <= end_c; c++) {const column = columns[c];@@ -484,7 +504,7 @@ export class NodePositionConvertToCursor {c);- this._Liquid.translateColumn(column);+ this._liquid.translateColumn(column);for (let l = start_l; l <= end_l; l++) {const line = lines[l];@@ -496,16 +516,16 @@ export class NodePositionConvertToCursor {divides.length - 1,l);- this._Liquid.translateSave();- this._Liquid.translateLine(line);+ this._liquid.translateSave();+ this._liquid.translateLine(line);for (let d = start_d; d <= end_d; d++) {const divide = divides[d];// console.log('div', divides, divide, startPosition, endPosition, start_d, end_d, d);- this._Liquid.translateSave();- this._Liquid.translateDivide(divide);+ this._liquid.translateSave();+ this._liquid.translateDivide(divide);const spanGroup = divide.spanGroup;@@ -531,16 +551,16 @@ export class NodePositionConvertToCursor {func && func(start_sp, end_sp, isFirst, isLast, divide, line, column, section, page);- this._Liquid.translateRestore();+ this._liquid.translateRestore();}- this._Liquid.translateRestore();+ this._liquid.translateRestore();}}}- this._Liquid.translateRestore();- this._Liquid.translatePage(page, pageLayoutType, pageMarginLeft, pageMarginTop);+ this._liquid.translateRestore();+ this._liquid.translatePage(page, pageLayoutType, pageMarginLeft, pageMarginTop);}}}diff --git a/packages/base-render/src/components/docs/common/line.ts b/packages/base-render/src/components/docs/common/line.tsindex b69c98b2ab..946525e065 100644--- a/packages/base-render/src/components/docs/common/line.ts+++ b/packages/base-render/src/components/docs/common/line.ts@@ -1,483 +1,483 @@-import { IDrawing, Nullable, PositionedObjectLayoutType, WrapTextType } from '@univerjs/core';--import {- IDocumentSkeletonDivide,- IDocumentSkeletonDrawing,- IDocumentSkeletonDrawingAnchor,- IDocumentSkeletonLine,- LineType,-} from '../../../basics/i-document-skeleton-cached';-import { Path2 } from '../../../basics/path2';-import { Transform } from '../../../basics/transform';-import { Vector2 } from '../../../basics/vector2';--interface IDrawingsSplit {- left: number;- width: number;-}--enum WrapTextRuler {- BOTH,- LEFT,- RIGHT,-}--enum AxisType {- X,- Y,-}--interface ILineBoundingBox {- lineHeight: number;- lineTop: number;- paddingLeft?: number;- paddingRight?: number;- paddingTop?: number;- paddingBottom?: number;- marginTop?: number;- spaceBelowApply?: number;- contentHeight: number;-}--// 处理divides, divideLen, lineIndex, 无序和有序列表标题, drawingTBIds 影响行的元素id集合-export function createSkeletonLine(- paragraphIndex: number,- lineType: LineType,- lineBoundingBox: ILineBoundingBox,- columnWidth: number,- lineIndex: number = 0,- isParagraphStart: boolean = false,- affectSkeDrawings?: Map<string, IDocumentSkeletonDrawing>,- headersDrawings?: Map<string, IDocumentSkeletonDrawing>,- footersDrawings?: Map<string, IDocumentSkeletonDrawing>-): IDocumentSkeletonLine {- const {- lineHeight = 15.6,- lineTop = 0,- contentHeight = 0,- paddingLeft = 0,- paddingRight = 0,- paddingTop = 0,- paddingBottom = 0,- marginTop = 0,- spaceBelowApply = 0,- } = lineBoundingBox;-- const lineSke = _getLineSke(lineType, paragraphIndex);- lineSke.lineIndex = lineIndex;- lineSke.paragraphStart = isParagraphStart; // 是否段落开始的第一行- lineSke.contentHeight = contentHeight;- lineSke.top = lineTop;- lineSke.lineHeight = lineHeight;- lineSke.paddingTop = paddingTop;- lineSke.paddingBottom = paddingBottom;- lineSke.marginTop = marginTop; // marginTop is initialized when it is created, and marginBottom is not calculated when it is created, it will be determined according to the situation of the next paragraph- lineSke.spaceBelowApply = spaceBelowApply;-- lineSke.divides = _calculateDividesByDrawings(- lineHeight,- lineTop,- columnWidth,- paddingLeft,- paddingRight,- affectSkeDrawings,- headersDrawings,- footersDrawings- );-- for (const divide of lineSke.divides) {- divide.parent = lineSke;- }-- return lineSke;-}--export function calculateLineTopByDrawings(- lineHeight: number = 15.6,- lineTop: number = 0,- pageSkeDrawings?: Map<string, IDocumentSkeletonDrawing>,- headersDrawings?: Map<string, IDocumentSkeletonDrawing>,- footersDrawings?: Map<string, IDocumentSkeletonDrawing>-) {- let maxTop = lineTop;- headersDrawings?.forEach((drawing) => {- const top = _getLineTopWidthWrapTopBottom(drawing, lineHeight, lineTop);- if (top) {- maxTop = Math.max(maxTop, top);- }- });-- footersDrawings?.forEach((drawing) => {- const top = _getLineTopWidthWrapTopBottom(drawing, lineHeight, lineTop);- if (top) {- maxTop = Math.max(maxTop, top);- }- });-- pageSkeDrawings?.forEach((drawing) => {- const top = _getLineTopWidthWrapTopBottom(drawing, lineHeight, lineTop);- if (top) {- maxTop = Math.max(maxTop, top);- }- });-- return maxTop;-}--function _getLineTopWidthWrapTopBottom(drawing: IDocumentSkeletonDrawing, lineHeight: number, lineTop: number) {- const { aTop, height, aLeft, width, angle = 0, drawingOrigin } = drawing;- const { layoutType, distT = 0, distB = 0 } = drawingOrigin;-- if (layoutType !== PositionedObjectLayoutType.WRAP_TOP_AND_BOTTOM) {- return;- }-- // if (elementIndex && showElementIndex < elementIndex) {- // // drawing出现在特定element之后,一般会是同一个段落跨页的场景- // // 在对drawing进行操作时,如果跨页则设置showElementIndex- // return;- // }-- if (angle === 0) {- const newAtop = aTop - distT;- const newHeight = height + distB;-- if (newAtop + newHeight < lineTop || newAtop > lineHeight + lineTop) {- return;- }- return newAtop + height;- }- // 旋转的情况,要考虑行首位与drawing旋转后得到的最大区域- let { top: sTop = 0, height: sHeight = 0 } = __getBoundingBox(angle, aLeft, width, aTop, height);-- sTop -= distT;- sHeight += distB;-- if (sTop + sHeight < lineTop || sTop > lineHeight + lineTop) {- return;- }- return sTop + sHeight;-}--function _calculateDividesByDrawings(- lineHeight: number,- lineTop: number,- columnWidth: number,- paddingLeft: number,- paddingRight: number,- paragraphAffectSkeDrawings?: Map<string, IDocumentSkeletonDrawing>,- headersDrawings?: Map<string, IDocumentSkeletonDrawing>,- footersDrawings?: Map<string, IDocumentSkeletonDrawing>-): IDocumentSkeletonDivide[] {- const drawingsMix: IDrawingsSplit[] = []; // 图文混排的情况- // 插入indent占位- drawingsMix.push(- {- left: 0,- width: paddingLeft,- },- {- left: columnWidth - paddingRight,- width: paddingRight,- }- );- headersDrawings?.forEach((drawing, drawingId) => {- const split = _calculateSplit(drawing, lineHeight, lineTop, columnWidth);- if (split) {- drawingsMix.push(split);- }- });-- footersDrawings?.forEach((drawing, drawingId) => {- const split = _calculateSplit(drawing, lineHeight, lineTop, columnWidth);- if (split) {- drawingsMix.push(split);- }- });- paragraphAffectSkeDrawings?.forEach((drawing, drawingId) => {- const split = _calculateSplit(drawing, lineHeight, lineTop, columnWidth);- if (split) {- drawingsMix.push(split);- }- });-- return _calculateDivideByDrawings(columnWidth, drawingsMix);-}--export function setDivideFullState(divide: IDocumentSkeletonDivide, state: boolean) {- divide.isFull = state;-}--export function setLineMarginBottom(line: IDocumentSkeletonLine, marginBottom: number) {- line.marginBottom = marginBottom;-}--// 获得文字内容后的行信息计算-export function updateDivideInLine(line: IDocumentSkeletonLine) {}--function _calculateSplit(- drawing: IDocumentSkeletonDrawing,- lineHeight: number,- lineTop: number,- columnWidth: number-): Nullable<IDrawingsSplit> {- const { aTop, height, aLeft, width, angle = 0, drawingOrigin } = drawing;- const { layoutType } = drawingOrigin;-- if (- layoutType === PositionedObjectLayoutType.WRAP_NONE ||- layoutType === PositionedObjectLayoutType.WRAP_TOP_AND_BOTTOM- ) {- return;- }-- if (layoutType === PositionedObjectLayoutType.WRAP_POLYGON) {- const { start = [0, 0], lineTo } = drawingOrigin;- if (!lineTo) {- return;- }-- const points: Vector2[] = [];- points.push(new Vector2(start[0], start[1]));- for (let i = 0; i < lineTo.length; i++) {- const point = lineTo[i];- points.push(new Vector2(point[0], point[1]));- }-- if (angle !== 0) {- const transform = new Transform().rotate(angle); // 建一个旋转后的变换类- for (let i = 0; i < points.length; i++) {- const point = points[i];- points[i] = transform.applyPoint(point);- }- }-- return __getCrossPoint(points, lineTop, lineHeight, columnWidth);- }-- if (angle === 0) {- // 无旋转的情况, wrapSquare | wrapThrough | wrapTight- return __getSplitWidthNoAngle(aTop, height, aLeft, width, lineTop, lineHeight, columnWidth, drawingOrigin);- }-- // 旋转的情况,要考虑行首位与drawing旋转后得到的最大区域- const boundingBox = __getBoundingBox(angle, aLeft, width, aTop, height);-- if (layoutType === PositionedObjectLayoutType.WRAP_SQUARE) {- // WRAP_SQUARE的情况下,旋转后的图形会重新有一个rect,用这个新rect来决定split- const { left: sLeft, width: sWidth, top: sTop, height: sHeight } = boundingBox;- return __getSplitWidthNoAngle(- sTop!,- sHeight!,- sLeft!,- sWidth!,- lineTop,- lineHeight,- columnWidth,- drawingOrigin- );- }-- // wrapThrough | wrapTight- return __getCrossPoint(boundingBox.points, lineTop, lineHeight, columnWidth);-}--function __getBoundingBox(angle: number, left: number, width: number, top: number, height: number) {- // 旋转的情况,要考虑行首位与drawing旋转后得到的最大区域- const transform = new Transform().rotate(angle); // 建一个旋转后的变换类- // 把drawing四个端点分别进行旋转- const lt = new Vector2(left, top);- const lb = new Vector2(left, top + height);- const rt = new Vector2(left + width, top);- const rb = new Vector2(left + width, top + height);- const boundingBox = transform.makeBoundingBoxFromPoints([lt, lb, rt, rb]); // 返回旋转后的点集合以及矩形选区- return boundingBox;-}--function __getCrossPoint(points: Vector2[], lineTop: number, lineHeight: number, columnWidth: number) {- const path = new Path2(points);- const crossPointTop = path.intersection([new Vector2(0, lineTop), new Vector2(columnWidth, lineTop)]);- const crossPointBottom = path.intersection([- new Vector2(0, lineTop + lineHeight),- new Vector2(columnWidth, lineTop + lineHeight),- ]);-- if (!crossPointTop && !crossPointBottom) {- return;- }-- const range = ___getMaxAndMinAxis([...points, ...(crossPointTop || []), ...(crossPointBottom || [])]);- return {- left: range.min,- width: range.max,- };-}--function ___getMaxAndMinAxis(points: Vector2[], axis = AxisType.X) {- const result = [];- for (let i = 0; i < points.length; i++) {- const point = points[i];- if (axis === AxisType.X) {- result.push(point.x);- } else {- result.push(point.y);- }- }- return {- max: Math.max(...result),- min: Math.min(...result),- };-}--function __getSplitWidthNoAngle(- top: number,- height: number,- left: number,- width: number,- lineTop: number,- lineHeight: number,- columnWidth: number,- drawingOrigin: IDrawing-) {- const {- layoutType,- wrapText = WrapTextType.BOTH_SIDES,- distL = 0,- distR = 0,- distT = 0,- distB = 0,- } = drawingOrigin;-- const newAtop = top - (layoutType === PositionedObjectLayoutType.WRAP_SQUARE ? distT : 0);- const newHeight = height + (layoutType === PositionedObjectLayoutType.WRAP_SQUARE ? distB : 0);-- if (newAtop + newHeight < lineTop || newAtop > lineHeight + lineTop) {- return;- }-- let resultLeft = left - distL;- let resultWidth = width + distR;- const ruler = ___getWrapTextRuler(wrapText, resultLeft, resultWidth, columnWidth);-- if (ruler === WrapTextRuler.LEFT) {- resultWidth = columnWidth - resultLeft;- } else if (ruler === WrapTextRuler.RIGHT) {- resultLeft = 0;- resultWidth = left + width + distR;- }-- return {- left: resultLeft,- width: resultWidth,- };-}--function ___getWrapTextRuler(wrapText: WrapTextType, resultLeft: number, resultWidth: number, columnWidth: number) {- let ruler = WrapTextRuler.BOTH;- if (wrapText === WrapTextType.LEFT) {- // 保留左侧,占满右侧,返回很大的width- ruler = WrapTextRuler.LEFT;- } else if (wrapText === WrapTextType.RIGHT) {- // 保留右侧,占满右侧,left从0开始- ruler = WrapTextRuler.RIGHT;- } else if (wrapText === WrapTextType.LARGEST) {- // 保留间隔最大的那一端- if (resultLeft > columnWidth - resultLeft - resultWidth) {- // 左侧留空比较大- ruler = WrapTextRuler.LEFT;- } else {- // 右侧留空比较大- ruler = WrapTextRuler.RIGHT;- }- }- return ruler;-}--function _calculateDivideByDrawings(columnWidth: number, drawingSplit: IDrawingsSplit[]): IDocumentSkeletonDivide[] {- drawingSplit.sort((pre, next) => {- if (pre.left > next.left) {- return 1;- }- return -1;- });-- const divideSkeleton: IDocumentSkeletonDivide[] = [];- let start = 0;- const splitLength = drawingSplit.length;- for (let i = 0; i < splitLength; i++) {- const split = drawingSplit[i];- const { left, width } = split;- if (left > start) {- // 插入start到left的间隔divide- let width = left - start;- width = width < columnWidth ? width : columnWidth - start;- const divide = __getDivideSKe(start, width);- divideSkeleton.push(divide);- }- start = Math.max(left + width, start);-- if (i === splitLength - 1 && left + width < columnWidth) {- // 最后一个split到右边界的divide- const divide = __getDivideSKe(left + width, columnWidth - left - width);- divideSkeleton.push(divide);- }- }-- return divideSkeleton;-}--function __getDivideSKe(left: number, width: number): IDocumentSkeletonDivide {- return {- // divide 分割,为了适配插入对象、图片、表格等,图文混排- spanGroup: [], // spanGroup- width, // width 被分割后的总宽度- left, // left 被对象分割后的偏移位置 | d1 | | d2 |- paddingLeft: 0, // paddingLeft 根据horizonAlign和width计算对齐偏移- isFull: false, // isFull, // 内容是否装满- st: 0, // startIndex- ed: 0, // endIndex- };-}--function _getLineSke(lineType: LineType, paragraphIndex: number): IDocumentSkeletonLine {- return {- paragraphIndex,- type: lineType,- divides: [], // /divides 受到对象影响,把行切分为N部分- lineHeight: 0, // lineHeight =max(span.fontBoundingBoxAscent + span.fontBoundingBoxDescent, span2.....) + space- contentHeight: 0, // contentHeight =max(span.fontBoundingBoxAscent + span.fontBoundingBoxDescent, span2.....)- top: 0, // top paragraph(spaceAbove, spaceBelow, lineSpacing*PreLineHeight)- asc: 0, // =max(span.textMetrics.asc) alphaBeta对齐,需要校准- paddingTop: 0, // paddingTop 内容到顶部的距离- paddingBottom: 0, // paddingBottom 内容到底部的距离- marginTop: 0, // marginTop 针对段落的spaceAbove- marginBottom: 0, // marginBottom 针对段落的spaceBlow- spaceBelowApply: 0, // lineSpacingApply- divideLen: 0, // divideLen 被对象分割为多少块- st: -1, // startIndex 文本开始索引- ed: -1, // endIndex 文本结束索引- lineIndex: 0, // lineIndex 行号- paragraphStart: false,- };-}--export function createAndUpdateBlockAnchor(- paragraphIndex: number,- line: IDocumentSkeletonLine,- top: number,- drawingAnchor?: Map<number, IDocumentSkeletonDrawingAnchor>-) {- if (!drawingAnchor) {- return;- }- if (drawingAnchor.has(paragraphIndex)) {- const anchor = drawingAnchor.get(paragraphIndex);- anchor?.elements.push(line);- } else {- drawingAnchor.set(paragraphIndex, {- elements: [line],- paragraphIndex,- top,- });- }-}+import { IDrawing, Nullable, PositionedObjectLayoutType, WrapTextType } from '@univerjs/core';++import {+ IDocumentSkeletonDivide,+ IDocumentSkeletonDrawing,+ IDocumentSkeletonDrawingAnchor,+ IDocumentSkeletonLine,+ LineType,+} from '../../../basics/i-document-skeleton-cached';+import { Path2 } from '../../../basics/path2';+import { Transform } from '../../../basics/transform';+import { Vector2 } from '../../../basics/vector2';++interface IDrawingsSplit {+ left: number;+ width: number;+}++enum WrapTextRuler {+ BOTH,+ LEFT,+ RIGHT,+}++enum AxisType {+ X,+ Y,+}++interface ILineBoundingBox {+ lineHeight: number;+ lineTop: number;+ paddingLeft?: number;+ paddingRight?: number;+ paddingTop?: number;+ paddingBottom?: number;+ marginTop?: number;+ spaceBelowApply?: number;+ contentHeight: number;+}++// 处理divides, divideLen, lineIndex, 无序和有序列表标题, drawingTBIds 影响行的元素id集合+export function createSkeletonLine(+ paragraphIndex: number,+ lineType: LineType,+ lineBoundingBox: ILineBoundingBox,+ columnWidth: number,+ lineIndex: number = 0,+ isParagraphStart: boolean = false,+ affectSkeDrawings?: Map<string, IDocumentSkeletonDrawing>,+ headersDrawings?: Map<string, IDocumentSkeletonDrawing>,+ footersDrawings?: Map<string, IDocumentSkeletonDrawing>+): IDocumentSkeletonLine {+ const {+ lineHeight = 15.6,+ lineTop = 0,+ contentHeight = 0,+ paddingLeft = 0,+ paddingRight = 0,+ paddingTop = 0,+ paddingBottom = 0,+ marginTop = 0,+ spaceBelowApply = 0,+ } = lineBoundingBox;++ const lineSke = _getLineSke(lineType, paragraphIndex);+ lineSke.lineIndex = lineIndex;+ lineSke.paragraphStart = isParagraphStart; // 是否段落开始的第一行+ lineSke.contentHeight = contentHeight;+ lineSke.top = lineTop;+ lineSke.lineHeight = lineHeight;+ lineSke.paddingTop = paddingTop;+ lineSke.paddingBottom = paddingBottom;+ lineSke.marginTop = marginTop; // marginTop is initialized when it is created, and marginBottom is not calculated when it is created, it will be determined according to the situation of the next paragraph+ lineSke.spaceBelowApply = spaceBelowApply;++ lineSke.divides = _calculateDividesByDrawings(+ lineHeight,+ lineTop,+ columnWidth,+ paddingLeft,+ paddingRight,+ affectSkeDrawings,+ headersDrawings,+ footersDrawings+ );++ for (const divide of lineSke.divides) {+ divide.parent = lineSke;+ }++ return lineSke;+}++export function calculateLineTopByDrawings(+ lineHeight: number = 15.6,+ lineTop: number = 0,+ pageSkeDrawings?: Map<string, IDocumentSkeletonDrawing>,+ headersDrawings?: Map<string, IDocumentSkeletonDrawing>,+ footersDrawings?: Map<string, IDocumentSkeletonDrawing>+) {+ let maxTop = lineTop;+ headersDrawings?.forEach((drawing) => {+ const top = _getLineTopWidthWrapTopBottom(drawing, lineHeight, lineTop);+ if (top) {+ maxTop = Math.max(maxTop, top);+ }+ });++ footersDrawings?.forEach((drawing) => {+ const top = _getLineTopWidthWrapTopBottom(drawing, lineHeight, lineTop);+ if (top) {+ maxTop = Math.max(maxTop, top);+ }+ });++ pageSkeDrawings?.forEach((drawing) => {+ const top = _getLineTopWidthWrapTopBottom(drawing, lineHeight, lineTop);+ if (top) {+ maxTop = Math.max(maxTop, top);+ }+ });++ return maxTop;+}++function _getLineTopWidthWrapTopBottom(drawing: IDocumentSkeletonDrawing, lineHeight: number, lineTop: number) {+ const { aTop, height, aLeft, width, angle = 0, drawingOrigin } = drawing;+ const { layoutType, distT = 0, distB = 0 } = drawingOrigin;++ if (layoutType !== PositionedObjectLayoutType.WRAP_TOP_AND_BOTTOM) {+ return;+ }++ // if (elementIndex && showElementIndex < elementIndex) {+ // // drawing出现在特定element之后,一般会是同一个段落跨页的场景+ // // 在对drawing进行操作时,如果跨页则设置showElementIndex+ // return;+ // }++ if (angle === 0) {+ const newAtop = aTop - distT;+ const newHeight = height + distB;++ if (newAtop + newHeight < lineTop || newAtop > lineHeight + lineTop) {+ return;+ }+ return newAtop + height;+ }+ // 旋转的情况,要考虑行首位与drawing旋转后得到的最大区域+ let { top: sTop = 0, height: sHeight = 0 } = __getBoundingBox(angle, aLeft, width, aTop, height);++ sTop -= distT;+ sHeight += distB;++ if (sTop + sHeight < lineTop || sTop > lineHeight + lineTop) {+ return;+ }+ return sTop + sHeight;+}++function _calculateDividesByDrawings(+ lineHeight: number,+ lineTop: number,+ columnWidth: number,+ paddingLeft: number,+ paddingRight: number,+ paragraphAffectSkeDrawings?: Map<string, IDocumentSkeletonDrawing>,+ headersDrawings?: Map<string, IDocumentSkeletonDrawing>,+ footersDrawings?: Map<string, IDocumentSkeletonDrawing>+): IDocumentSkeletonDivide[] {+ const drawingsMix: IDrawingsSplit[] = []; // 图文混排的情况+ // 插入indent占位+ drawingsMix.push(+ {+ left: 0,+ width: paddingLeft,+ },+ {+ left: columnWidth - paddingRight,+ width: paddingRight,+ }+ );+ headersDrawings?.forEach((drawing, drawingId) => {+ const split = _calculateSplit(drawing, lineHeight, lineTop, columnWidth);+ if (split) {+ drawingsMix.push(split);+ }+ });++ footersDrawings?.forEach((drawing, drawingId) => {+ const split = _calculateSplit(drawing, lineHeight, lineTop, columnWidth);+ if (split) {+ drawingsMix.push(split);+ }+ });+ paragraphAffectSkeDrawings?.forEach((drawing, drawingId) => {+ const split = _calculateSplit(drawing, lineHeight, lineTop, columnWidth);+ if (split) {+ drawingsMix.push(split);+ }+ });++ return _calculateDivideByDrawings(columnWidth, drawingsMix);+}++export function setDivideFullState(divide: IDocumentSkeletonDivide, state: boolean) {+ divide.isFull = state;+}++export function setLineMarginBottom(line: IDocumentSkeletonLine, marginBottom: number) {+ line.marginBottom = marginBottom;+}++// 获得文字内容后的行信息计算+export function updateDivideInLine(line: IDocumentSkeletonLine) {}++function _calculateSplit(+ drawing: IDocumentSkeletonDrawing,+ lineHeight: number,+ lineTop: number,+ columnWidth: number+): Nullable<IDrawingsSplit> {+ const { aTop, height, aLeft, width, angle = 0, drawingOrigin } = drawing;+ const { layoutType } = drawingOrigin;++ if (+ layoutType === PositionedObjectLayoutType.WRAP_NONE ||+ layoutType === PositionedObjectLayoutType.WRAP_TOP_AND_BOTTOM+ ) {+ return;+ }++ if (layoutType === PositionedObjectLayoutType.WRAP_POLYGON) {+ const { start = [0, 0], lineTo } = drawingOrigin;+ if (!lineTo) {+ return;+ }++ const points: Vector2[] = [];+ points.push(new Vector2(start[0], start[1]));+ for (let i = 0; i < lineTo.length; i++) {+ const point = lineTo[i];+ points.push(new Vector2(point[0], point[1]));+ }++ if (angle !== 0) {+ const transform = new Transform().rotate(angle); // 建一个旋转后的变换类+ for (let i = 0; i < points.length; i++) {+ const point = points[i];+ points[i] = transform.applyPoint(point);+ }+ }++ return __getCrossPoint(points, lineTop, lineHeight, columnWidth);+ }++ if (angle === 0) {+ // 无旋转的情况, wrapSquare | wrapThrough | wrapTight+ return __getSplitWidthNoAngle(aTop, height, aLeft, width, lineTop, lineHeight, columnWidth, drawingOrigin);+ }++ // 旋转的情况,要考虑行首位与drawing旋转后得到的最大区域+ const boundingBox = __getBoundingBox(angle, aLeft, width, aTop, height);++ if (layoutType === PositionedObjectLayoutType.WRAP_SQUARE) {+ // WRAP_SQUARE的情况下,旋转后的图形会重新有一个rect,用这个新rect来决定split+ const { left: sLeft, width: sWidth, top: sTop, height: sHeight } = boundingBox;+ return __getSplitWidthNoAngle(+ sTop!,+ sHeight!,+ sLeft!,+ sWidth!,+ lineTop,+ lineHeight,+ columnWidth,+ drawingOrigin+ );+ }++ // wrapThrough | wrapTight+ return __getCrossPoint(boundingBox.points, lineTop, lineHeight, columnWidth);+}++function __getBoundingBox(angle: number, left: number, width: number, top: number, height: number) {+ // 旋转的情况,要考虑行首位与drawing旋转后得到的最大区域+ const transform = new Transform().rotate(angle); // 建一个旋转后的变换类+ // 把drawing四个端点分别进行旋转+ const lt = new Vector2(left, top);+ const lb = new Vector2(left, top + height);+ const rt = new Vector2(left + width, top);+ const rb = new Vector2(left + width, top + height);+ const boundingBox = transform.makeBoundingBoxFromPoints([lt, lb, rt, rb]); // 返回旋转后的点集合以及矩形选区+ return boundingBox;+}++function __getCrossPoint(points: Vector2[], lineTop: number, lineHeight: number, columnWidth: number) {+ const path = new Path2(points);+ const crossPointTop = path.intersection([new Vector2(0, lineTop), new Vector2(columnWidth, lineTop)]);+ const crossPointBottom = path.intersection([+ new Vector2(0, lineTop + lineHeight),+ new Vector2(columnWidth, lineTop + lineHeight),+ ]);++ if (!crossPointTop && !crossPointBottom) {+ return;+ }++ const range = ___getMaxAndMinAxis([...points, ...(crossPointTop || []), ...(crossPointBottom || [])]);+ return {+ left: range.min,+ width: range.max,+ };+}++function ___getMaxAndMinAxis(points: Vector2[], axis = AxisType.X) {+ const result = [];+ for (let i = 0; i < points.length; i++) {+ const point = points[i];+ if (axis === AxisType.X) {+ result.push(point.x);+ } else {+ result.push(point.y);+ }+ }+ return {+ max: Math.max(...result),+ min: Math.min(...result),+ };+}++function __getSplitWidthNoAngle(+ top: number,+ height: number,+ left: number,+ width: number,+ lineTop: number,+ lineHeight: number,+ columnWidth: number,+ drawingOrigin: IDrawing+) {+ const {+ layoutType,+ wrapText = WrapTextType.BOTH_SIDES,+ distL = 0,+ distR = 0,+ distT = 0,+ distB = 0,+ } = drawingOrigin;++ const newAtop = top - (layoutType === PositionedObjectLayoutType.WRAP_SQUARE ? distT : 0);+ const newHeight = height + (layoutType === PositionedObjectLayoutType.WRAP_SQUARE ? distB : 0);++ if (newAtop + newHeight < lineTop || newAtop > lineHeight + lineTop) {+ return;+ }++ let resultLeft = left - distL;+ let resultWidth = width + distR;+ const ruler = ___getWrapTextRuler(wrapText, resultLeft, resultWidth, columnWidth);++ if (ruler === WrapTextRuler.LEFT) {+ resultWidth = columnWidth - resultLeft;+ } else if (ruler === WrapTextRuler.RIGHT) {+ resultLeft = 0;+ resultWidth = left + width + distR;+ }++ return {+ left: resultLeft,+ width: resultWidth,+ };+}++function ___getWrapTextRuler(wrapText: WrapTextType, resultLeft: number, resultWidth: number, columnWidth: number) {+ let ruler = WrapTextRuler.BOTH;+ if (wrapText === WrapTextType.LEFT) {+ // 保留左侧,占满右侧,返回很大的width+ ruler = WrapTextRuler.LEFT;+ } else if (wrapText === WrapTextType.RIGHT) {+ // 保留右侧,占满右侧,left从0开始+ ruler = WrapTextRuler.RIGHT;+ } else if (wrapText === WrapTextType.LARGEST) {+ // 保留间隔最大的那一端+ if (resultLeft > columnWidth - resultLeft - resultWidth) {+ // 左侧留空比较大+ ruler = WrapTextRuler.LEFT;+ } else {+ // 右侧留空比较大+ ruler = WrapTextRuler.RIGHT;+ }+ }+ return ruler;+}++function _calculateDivideByDrawings(columnWidth: number, drawingSplit: IDrawingsSplit[]): IDocumentSkeletonDivide[] {+ drawingSplit.sort((pre, next) => {+ if (pre.left > next.left) {+ return 1;+ }+ return -1;+ });++ const divideSkeleton: IDocumentSkeletonDivide[] = [];+ let start = 0;+ const splitLength = drawingSplit.length;+ for (let i = 0; i < splitLength; i++) {+ const split = drawingSplit[i];+ const { left, width } = split;+ if (left > start) {+ // 插入start到left的间隔divide+ let width = left - start;+ width = width < columnWidth ? width : columnWidth - start;+ const divide = __getDivideSKe(start, width);+ divideSkeleton.push(divide);+ }+ start = Math.max(left + width, start);++ if (i === splitLength - 1 && left + width < columnWidth) {+ // 最后一个split到右边界的divide+ const divide = __getDivideSKe(left + width, columnWidth - left - width);+ divideSkeleton.push(divide);+ }+ }++ return divideSkeleton;+}++function __getDivideSKe(left: number, width: number): IDocumentSkeletonDivide {+ return {+ // divide 分割,为了适配插入对象、图片、表格等,图文混排+ spanGroup: [], // spanGroup+ width, // width 被分割后的总宽度+ left, // left 被对象分割后的偏移位置 | d1 | | d2 |+ paddingLeft: 0, // paddingLeft 根据horizonAlign和width计算对齐偏移+ isFull: false, // isFull, // 内容是否装满+ st: 0, // startIndex+ ed: 0, // endIndex+ };+}++function _getLineSke(lineType: LineType, paragraphIndex: number): IDocumentSkeletonLine {+ return {+ paragraphIndex,+ type: lineType,+ divides: [], // /divides 受到对象影响,把行切分为 N 部分+ lineHeight: 0, // lineHeight =max(span.fontBoundingBoxAscent + span.fontBoundingBoxDescent, span2.....) + space+ contentHeight: 0, // contentHeight =max(span.fontBoundingBoxAscent + span.fontBoundingBoxDescent, span2.....)+ top: 0, // top paragraph(spaceAbove, spaceBelow, lineSpacing*PreLineHeight)+ asc: 0, // =max(span.textMetrics.asc) alphaBeta对齐,需要校准+ paddingTop: 0, // paddingTop 内容到顶部的距离+ paddingBottom: 0, // paddingBottom 内容到底部的距离+ marginTop: 0, // marginTop 针对段落的spaceAbove+ marginBottom: 0, // marginBottom 针对段落的spaceBelow+ spaceBelowApply: 0, // lineSpacingApply+ divideLen: 0, // divideLen 被对象分割为多少块+ st: -1, // startIndex 文本开始索引+ ed: -1, // endIndex 文本结束索引+ lineIndex: 0, // lineIndex 行号+ paragraphStart: false,+ };+}++export function createAndUpdateBlockAnchor(+ paragraphIndex: number,+ line: IDocumentSkeletonLine,+ top: number,+ drawingAnchor?: Map<number, IDocumentSkeletonDrawingAnchor>+) {+ if (!drawingAnchor) {+ return;+ }+ if (drawingAnchor.has(paragraphIndex)) {+ const anchor = drawingAnchor.get(paragraphIndex);+ anchor?.elements.push(line);+ } else {+ drawingAnchor.set(paragraphIndex, {+ elements: [line],+ paragraphIndex,+ top,+ });+ }+}diff --git a/packages/base-render/src/components/docs/common/range.ts b/packages/base-render/src/components/docs/common/range.tsindex d75a6b0eca..55b6fa1232 100644--- a/packages/base-render/src/components/docs/common/range.ts+++ b/packages/base-render/src/components/docs/common/range.ts@@ -197,19 +197,19 @@ export class TextRange {const convertor = new NodePositionConvertToCursor(_documentOffsetConfig, _docSkeleton);if (this._isCollapsed()) {- const { pointGroup, cursorList } = convertor.getRangePointData(anchor, anchor);+ const { contentBoxPointGroup, cursorList } = convertor.getRangePointData(anchor, anchor);this._setCursorList(cursorList);- pointGroup.length > 0 && this._createOrUpdateAnchor(pointGroup, docsLeft, docsTop);+ contentBoxPointGroup.length > 0 && this._createOrUpdateAnchor(contentBoxPointGroup, docsLeft, docsTop);return;}- const { pointGroup, cursorList } = convertor.getRangePointData(anchor, focus);+ const { borderBoxPointGroup, cursorList } = convertor.getRangePointData(anchor, focus);this._setCursorList(cursorList);- pointGroup.length > 0 && this._createOrUpdateRange(pointGroup, docsLeft, docsTop);+ borderBoxPointGroup.length > 0 && this._createOrUpdateRange(borderBoxPointGroup, docsLeft, docsTop);}private _isEmpty() {diff --git a/packages/base-render/src/components/docs/common/span.ts b/packages/base-render/src/components/docs/common/span.tsindex a08240b689..61832129f9 100644--- a/packages/base-render/src/components/docs/common/span.ts+++ b/packages/base-render/src/components/docs/common/span.ts@@ -54,6 +54,7 @@ export function _createSkeletonWordOrLetter(DT.CUSTOM_BLOCK,];let streamType = DT.LETTER;+if (skipWidthList.indexOf(content) > -1) {return {content: '',@@ -78,6 +79,7 @@ export function _createSkeletonWordOrLetter(streamType: content as DT,};}+if (content === DT.PARAGRAPH) {streamType = DT.PARAGRAPH;}@@ -86,6 +88,7 @@ export function _createSkeletonWordOrLetter(const { width: contentWidth = 0 } = bBox;let width = spanWidth ?? contentWidth;let paddingLeft = 0;+if (validationGrid(gridType, snapToGrid)) {// 当文字也需要对齐到网格式,进行处理// const multiple = Math.ceil(contentWidth / charSpace);@@ -94,6 +97,7 @@ export function _createSkeletonWordOrLetter(paddingLeft = (width - contentWidth) / 2;}}+return {content,ts: textStyle,diff --git a/packages/base-render/src/components/docs/doc-skeleton.ts b/packages/base-render/src/components/docs/doc-skeleton.tsindex c755d25ad4..bc7352c273 100644--- a/packages/base-render/src/components/docs/doc-skeleton.ts+++ b/packages/base-render/src/components/docs/doc-skeleton.ts@@ -74,9 +74,8 @@ export class DocumentSkeleton extends Skeleton {if (!this.dirty) {return;}- this._skeletonData = this._createSkeleton(bounds);- // this.onRecalculateChangeObservable.notifyObservers(this._skeletonData);+ this._skeletonData = this._createSkeleton(bounds);}getSkeletonData() {@@ -502,7 +501,7 @@ export class DocumentSkeleton extends Skeleton {},} = documentStyle;- const skeleton = this.__getNullSke();+ const skeleton = this._getNullSke();const docsConfig: IDocsConfig = {headerTreeMap,@@ -614,7 +613,7 @@ export class DocumentSkeleton extends Skeleton {if (sectionType === SectionType.CONTINUOUS) {updateBlockIndex(allSkeletonPages);- this.__addNewSectionByContinuous(curSkeletonPage, columnProperties, columnSeparatorType);+ this._addNewSectionByContinuous(curSkeletonPage, columnProperties, columnSeparatorType);isContinuous = true;} else {curSkeletonPage = createSkeletonPage(@@ -667,7 +666,7 @@ export class DocumentSkeleton extends Skeleton {}// 一页存在多个section的情况,仅在SectionType.CONTINUOUS的情况下出现- private __addNewSectionByContinuous(+ private _addNewSectionByContinuous(curSkeletonPage: IDocumentSkeletonPage,columnProperties: ISectionColumnProperties[],columnSeparatorType: ColumnSeparatorType@@ -697,7 +696,7 @@ export class DocumentSkeleton extends Skeleton {sections.push(newSection);}- private __getNullSke(): IDocumentSkeletonCached {+ private _getNullSke(): IDocumentSkeletonCached {return {pages: [],left: 0,diff --git a/packages/base-render/src/components/docs/extensions/background.ts b/packages/base-render/src/components/docs/extensions/background.tsindex 4c87bb05a0..4375128a62 100644--- a/packages/base-render/src/components/docs/extensions/background.ts+++ b/packages/base-render/src/components/docs/extensions/background.ts@@ -20,7 +20,7 @@ export class Background extends docExtension {return;}- const { lineHeight = 0 } = line;+ const { contentHeight = 0 } = line;const { ts: textStyle, width, content } = span;if (!textStyle) {return;@@ -40,7 +40,7 @@ export class Background extends docExtension {if (background) {ctx.fillStyle = background;- ctx.fillRect(spanStartPoint.x - 0.5, spanStartPoint.y, width + 1, lineHeight);+ ctx.fillRect(spanStartPoint.x - 0.5, spanStartPoint.y, width + 1, contentHeight + 1);}}diff --git a/packages/base-render/src/components/docs/extensions/font-and-base-line.ts b/packages/base-render/src/components/docs/extensions/font-and-base-line.tsindex a3584e8895..07909b51ae 100644--- a/packages/base-render/src/components/docs/extensions/font-and-base-line.ts+++ b/packages/base-render/src/components/docs/extensions/font-and-base-line.ts@@ -55,8 +55,8 @@ export class FontAndBaseLine extends docExtension {// const { renderConfig = {} } = this.extensionOffset;// const { centerAngle = 0 } = renderConfig;+ let offset = 0;- let offset = bBox.spo;if (baselineOffset === BaselineOffset.SUPERSCRIPT) {offset = -bBox.sbo;}diff --git a/packages/base-render/src/components/docs/extensions/line.ts b/packages/base-render/src/components/docs/extensions/line.tsindex 1aa97ba9e4..1ba4151590 100644--- a/packages/base-render/src/components/docs/extensions/line.ts+++ b/packages/base-render/src/components/docs/extensions/line.ts@@ -23,7 +23,7 @@ export class Line extends docExtension {return;}- const { asc: maxLineAsc = 0, lineHeight = 0, marginTop = 0 } = line;+ const { asc: maxLineAsc = 0, contentHeight = 0 } = line;const { ts: textStyle, left, width, bBox } = span;if (!textStyle) {return;@@ -53,7 +53,7 @@ export class Line extends docExtension {ctx.strokeStyle = color;this._setLineType(ctx, lineType || TextDecoration.SINGLE);- const startY = fixLineWidthByScale(lineHeight - marginTop + DEFAULT_OFFSET_SPACING - 0.5, scale);+ const startY = fixLineWidthByScale(contentHeight + DEFAULT_OFFSET_SPACING - 0.5, scale);const start = calculateRectRotate(originTranslate.addByPoint(left, startY),diff --git a/packages/base-render/src/components/docs/text-selection-render-manager.ts b/packages/base-render/src/components/docs/text-selection-render-manager.tsindex 64006b7d5f..cba46689f8 100644--- a/packages/base-render/src/components/docs/text-selection-render-manager.ts+++ b/packages/base-render/src/components/docs/text-selection-render-manager.ts@@ -164,8 +164,6 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSelreadonly textSelection$ = this._textSelection$.asObservable();- // onPaste$!: RxObservable<ClipboardEvent>;-private _container!: HTMLDivElement;private _inputParent!: HTMLDivElement;@@ -415,7 +413,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSelthis._viewportScrollX = scrollX;this._viewportScrollY = scrollY;- this._onSelectionStart$.next(this.getActiveRangeInstance()?.startNodePosition);+ this._onSelectionStart$.next(this._getActiveRangeInstance()?.startNodePosition);let preMoveOffsetX = evtOffsetX;@@ -468,7 +466,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSeltextRange.refresh();}- private getActiveRangeInstance() {+ private _getActiveRangeInstance() {return this._rangeList.find((range) => range.isActive());}@@ -696,7 +694,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel// Let the selection show on the current screen.private _scrollToSelection() {- const activeRangeInstance = this.getActiveRangeInstance();+ const activeRangeInstance = this._getActiveRangeInstance();const anchor = activeRangeInstance?.getAnchor();if (!anchor || (anchor && !anchor.visible) || this._activeViewport == null) {return;@@ -739,7 +737,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel}private _syncDomToSelection() {- const activeRangeInstance = this.getActiveRangeInstance();+ const activeRangeInstance = this._getActiveRangeInstance();const anchor = activeRangeInstance?.getAnchor();if (!anchor || (anchor && !anchor.visible) || this._activeViewport == null) {@@ -783,7 +781,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSelreturn;}- const activeRangeInstance = this.getActiveRangeInstance();+ const activeRangeInstance = this._getActiveRangeInstance();if (!activeRangeInstance) {return;@@ -813,7 +811,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSelreturn;}- const activeRangeInstance = this.getActiveRangeInstance();+ const activeRangeInstance = this._getActiveRangeInstance();activeRangeInstance?.activeStatic();@@ -828,7 +826,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSelconst bounds = viewport.getBounding();- const activeRangeInstance = this.getActiveRangeInstance();+ const activeRangeInstance = this._getActiveRangeInstance();const anchor = activeRangeInstance?.getAnchor();@@ -973,7 +971,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSelif (this._docSkeleton == null) {return;}- const activeRangeInstance = this.getActiveRangeInstance();+ const activeRangeInstance = this._getActiveRangeInstance();activeRangeInstance?.refresh();}diff --git a/packages/common-plugin-data/src/docs/default-document-data-cn.ts b/packages/common-plugin-data/src/docs/default-document-data-cn.tsnew file mode 100644index 0000000000..f52d92db48--- /dev/null+++ b/packages/common-plugin-data/src/docs/default-document-data-cn.ts@@ -0,0 +1,769 @@+import { BooleanNumber, IDocumentData, TextDecoration } from '@univerjs/core';++export const DEFAULT_DOCUMENT_DATA_CN: IDocumentData = {+ id: 'd',+ body: {+ dataStream:+ '零. 开篇\r上标和小标:HO2 5X 删除线:我是被删除的内容 下划线:我是下划线\r这篇文章旨在帮助新人快速熟悉开源项目univer的架构及代码,也是我过去一段时间参与到 univer 开发中的学习和总结,肯定有不够准确或者理解偏差,欢迎大家评论指正\r第壹章,会聊聊我对univer架构的理解,univer是如何拆分模块,以及模块之间的依赖关系。然后将univer放入MMV的架构模式中,分别分析下其模型层、视图层、控制器的边界和职责\r第贰章,我们先来看看 univer sheet 的模型层数据结构设计,如何区分 workbook、sheet、row、column、style 等,了解他们的包含关系,这对后面深入理解代码是有帮助的\r第叁、肆章,我将从两条控制链路来分析 univer 的代码,一条链路是 univer 启动和初始化渲染的过程。在这条链路中,是从模型层到视图层的过程。另外一条链路是 univer 响应用户事件,并且触发模型层数据变更,页面重新渲染,在这条链路中,是从视图层到模型层的过程。在这两部分,我们会涉及到大量的源码分析,在保留代码主逻辑的前提,删除了边界 case 的代码。同时在每个代码块第一行,表示该代码块所在的 TS 文件,这样便于直接阅读源码\r\n',+ textRuns: [+ {+ st: 0,+ ed: 2,+ ts: {+ fs: 24,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(255, 255, 255)',+ },+ bl: BooleanNumber.TRUE,+ bg: {+ rgb: 'rgb(255, 0, 0)',+ },+ },+ },+ {+ st: 2,+ ed: 4,+ ts: {+ fs: 9,+ ff: '"Times New Roman"',+ cl: {+ rgb: 'rgb(255, 255, 255)',+ },+ bl: BooleanNumber.TRUE,+ bg: {+ rgb: 'rgb(255, 0, 0)',+ },+ },+ },+ {+ st: 4,+ ed: 6,+ ts: {+ fs: 24,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(255, 255, 255)',+ },+ bl: BooleanNumber.TRUE,+ bg: {+ rgb: 'rgb(255, 0, 0)',+ },+ },+ },+ {+ st: 7,+ ed: 13,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 13,+ ed: 15,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 15,+ ed: 16,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ va: 1,+ },+ },+ {+ st: 16,+ ed: 18,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 18,+ ed: 19,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ va: 2,+ },+ },+ {+ st: 19,+ ed: 20,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 20,+ ed: 24,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 24,+ ed: 33,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ st: {+ s: BooleanNumber.TRUE,+ },+ },+ },+ {+ st: 33,+ ed: 37,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 37,+ ed: 42,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ ul: {+ s: BooleanNumber.TRUE,+ cl: {+ rgb: 'rgb(255, 0, 0)',+ },+ t: TextDecoration.DASH_DOT_DOT_HEAVY,+ },+ },+ },+ {+ st: 43,+ ed: 47,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(255, 255, 255)',+ },+ bg: {+ rgb: 'rgb(255, 0, 255)',+ },+ },+ },+ {+ st: 47,+ ed: 61,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 61,+ ed: 67,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(255, 192, 0)',+ },+ bl: BooleanNumber.TRUE,+ ul: {+ s: BooleanNumber.TRUE,+ },+ },+ },+ {+ st: 67,+ ed: 87,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 87,+ ed: 93,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(255, 192, 0)',+ },+ bl: BooleanNumber.TRUE,+ ul: {+ s: BooleanNumber.TRUE,+ },+ },+ },+ {+ st: 93,+ ed: 94,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 94,+ ed: 126,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 127,+ ed: 130,+ ts: {+ fs: 21,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ bl: BooleanNumber.TRUE,+ },+ },+ {+ st: 130,+ ed: 136,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 136,+ ed: 142,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 142,+ ed: 148,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 148,+ ed: 154,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 154,+ ed: 177,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 177,+ ed: 183,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ bl: BooleanNumber.TRUE,+ ul: {+ s: BooleanNumber.TRUE,+ },+ },+ },+ {+ st: 183,+ ed: 185,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 185,+ ed: 188,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 188,+ ed: 218,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 219,+ ed: 222,+ ts: {+ fs: 21,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ bl: BooleanNumber.TRUE,+ },+ },+ {+ st: 222,+ ed: 229,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 229,+ ed: 243,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 243,+ ed: 259,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 259,+ ed: 267,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ it: BooleanNumber.TRUE,+ ul: {+ s: BooleanNumber.TRUE,+ t: TextDecoration.DASHED_HEAVY,+ },+ },+ },+ {+ st: 267,+ ed: 268,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ it: BooleanNumber.TRUE,+ },+ },+ {+ st: 268,+ ed: 273,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ it: BooleanNumber.TRUE,+ ul: {+ s: BooleanNumber.TRUE,+ t: TextDecoration.DASH_DOT_DOT_HEAVY,+ },+ },+ },+ {+ st: 273,+ ed: 274,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ it: BooleanNumber.TRUE,+ },+ },+ {+ st: 274,+ ed: 277,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ it: BooleanNumber.TRUE,+ ul: {+ s: BooleanNumber.TRUE,+ t: TextDecoration.DASH_LONG,+ },+ },+ },+ {+ st: 277,+ ed: 278,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ it: BooleanNumber.TRUE,+ },+ },+ {+ st: 278,+ ed: 284,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ it: BooleanNumber.TRUE,+ ul: {+ s: BooleanNumber.TRUE,+ t: TextDecoration.DASH_LONG_HEAVY,+ },+ },+ },+ {+ st: 284,+ ed: 285,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ it: BooleanNumber.TRUE,+ },+ },+ {+ st: 285,+ ed: 290,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ it: BooleanNumber.TRUE,+ ul: {+ s: BooleanNumber.TRUE,+ t: TextDecoration.DOT_DOT_DASH,+ },+ },+ },+ {+ st: 290,+ ed: 291,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 291,+ ed: 318,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 319,+ ed: 324,+ ts: {+ fs: 21,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ bl: BooleanNumber.TRUE,+ },+ },+ {+ st: 324,+ ed: 337,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 337,+ ed: 345,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 345,+ ed: 354,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 354,+ ed: 362,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 362,+ ed: 401,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 401,+ ed: 409,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 409,+ ed: 492,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 492,+ ed: 498,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 498,+ ed: 523,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 523,+ ed: 527,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ {+ st: 527,+ ed: 540,+ ts: {+ fs: 14,+ ff: 'Microsoft YaHei"',+ cl: {+ rgb: 'rgb(47, 85, 151)',+ },+ },+ },+ ],+ paragraphs: [+ {+ startIndex: 6,+ paragraphStyle: {+ spaceAbove: 0,+ lineSpacing: 2,+ spaceBelow: 20,+ },+ },+ {+ startIndex: 42,+ paragraphStyle: {+ spaceAbove: 20,+ lineSpacing: 2,+ spaceBelow: 0,+ },+ },+ {+ startIndex: 126,+ paragraphStyle: {+ spaceAbove: 20,+ lineSpacing: 2,+ spaceBelow: 0,+ },+ },+ {+ startIndex: 218,+ paragraphStyle: {+ spaceAbove: 20,+ lineSpacing: 2,+ spaceBelow: 0,+ },+ },+ {+ startIndex: 318,+ paragraphStyle: {+ spaceAbove: 20,+ lineSpacing: 2,+ spaceBelow: 0,+ },+ },+ {+ startIndex: 540,+ paragraphStyle: {+ spaceAbove: 20,+ lineSpacing: 2,+ spaceBelow: 0,+ },+ },+ ],+ },+ documentStyle: {+ pageSize: {+ width: 595,+ height: 842,+ },+ marginTop: 50,+ marginBottom: 50,+ marginRight: 40,+ marginLeft: 40,+ },+};diff --git a/packages/common-plugin-data/src/docs/default-document-data-simple.ts b/packages/common-plugin-data/src/docs/default-document-data-simple.tsdeleted file mode 100644index 2c0f316b4c..0000000000--- a/packages/common-plugin-data/src/docs/default-document-data-simple.ts+++ /dev/null@@ -1,19 +0,0 @@-import { IDocumentData } from '@univerjs/core';--export const DEFAULT_DOCUMENT_DATA_SIMPLE: IDocumentData = {- id: 'd',- body: {- dataStream:- 'What’s New in the 2022 Gartner Hype Cycle for Emerging Technologies\rEmerging technologies for 2022 fit into three main themes: evolving/expanding immersive experiences\r\n',- },- documentStyle: {- pageSize: {- width: 594.3,- height: 840.51,- },- marginTop: 72,- marginBottom: 72,- marginRight: 90,- marginLeft: 90,- },-};diff --git a/packages/common-plugin-data/src/docs/index.ts b/packages/common-plugin-data/src/docs/index.tsindex 0a3a7214d1..06edde4e3a 100644--- a/packages/common-plugin-data/src/docs/index.ts+++ b/packages/common-plugin-data/src/docs/index.ts@@ -1,5 +1,5 @@// @index('./*.ts', f => `export * from '${f.path}'`)export * from './default-document-data';+export * from './default-document-data-cn';export * from './default-document-data-en';-export * from './default-document-data-simple';// @endindex