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
feat(doc): handle custom range transform (#3836)
Ran Luo89d644165e73
diff --git a/.vscode/settings.json b/.vscode/settings.jsonindex fb581086e2..374990ab5d 100644--- a/.vscode/settings.json+++ b/.vscode/settings.json@@ -8,9 +8,12 @@"commentTranslate.hover.enabled": true,"commentTranslate.source": "Google","cSpell.words": [+ "ABJAD","adjustability",+ "AIUEO","Bandings","Catmull",+ "CHOSUNG","clazz","clsx","codelf",@@ -38,7 +41,9 @@"EXTRALIGHT","FONTFACE","fract",+ "FULLSTOP","gainsboro",+ "GANADA","GANTT","Gggpound","Govett",@@ -51,6 +56,7 @@"hoverable","IHTTP","ilvl",+ "IROHA","IRPC","Jamo","jikkai",diff --git a/packages/core/src/docs/data-model/text-x/__tests__/transform.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/transform.spec.tsindex 299e90b355..813d8ba60d 100644--- a/packages/core/src/docs/data-model/text-x/__tests__/transform.spec.ts+++ b/packages/core/src/docs/data-model/text-x/__tests__/transform.spec.ts@@ -18,6 +18,7 @@ import type { TextXAction } from '../action-types';import { describe, expect, it } from 'vitest';import { UpdateDocsAttributeType } from '../../../../shared';import { BooleanNumber } from '../../../../types/enum';+import { CustomRangeType } from '../../../../types/interfaces';import { TextXActionType } from '../action-types';import { TextX } from '../text-x';@@ -716,7 +717,7 @@ describe('transform()', () => {},];- const expectedActionsWithPriorityTrue: TextXAction[] = [+ const expectedActionsWithPriorityFalse: TextXAction[] = [{t: TextXActionType.RETAIN,len: 1,@@ -736,7 +737,7 @@ describe('transform()', () => {},];- const expectedActionsWithPriorityFalse: TextXAction[] = [+ const expectedActionsWithPriorityTrue: TextXAction[] = [{t: TextXActionType.RETAIN,len: 1,@@ -757,8 +758,107 @@ describe('transform()', () => {},];- expect(TextX._transform(actionsA, actionsB, 'right')).toEqual(expectedActionsWithPriorityFalse);- expect(TextX._transform(actionsA, actionsB, 'left')).toEqual(expectedActionsWithPriorityTrue);+ expect(TextX._transform(actionsA, actionsB, 'right')).toEqual(expectedActionsWithPriorityTrue);+ expect(TextX._transform(actionsA, actionsB, 'left')).toEqual(expectedActionsWithPriorityFalse);+ });++ it('retain + retain with custom range', () => {+ const actionsA: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ segmentId: '',+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: -1,+ endIndex: 0,+ rangeId: 'rangeId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.example.com',+ },+ },+ ],+ },+ },+ ];++ const actionsB: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ segmentId: '',+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: -1,+ endIndex: 0,+ rangeId: 'rangeId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.baidu.com',+ },+ },+ ],+ },+ },+ ];++ const expectedActionsWithPriorityTrue: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ segmentId: '',+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: -1,+ endIndex: 0,+ rangeId: 'rangeId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.baidu.com',+ },+ },+ ],+ },+ },+ ];++ const expectedActionsWithPriorityFalse: TextXAction[] = [+ {+ t: TextXActionType.RETAIN,+ len: 1,+ segmentId: '',+ coverType: UpdateDocsAttributeType.REPLACE,+ body: {+ dataStream: '',+ customRanges: [+ {+ startIndex: -1,+ endIndex: 0,+ rangeId: 'rangeId',+ rangeType: CustomRangeType.HYPERLINK,+ properties: {+ url: 'https://www.example.com',+ },+ },+ ],+ },+ },+ ];++ // console.log(JSON.stringify(TextX._transform(actionsA, actionsB, 'right'), null, 2));++ expect(TextX._transform(actionsA, actionsB, 'right')).toEqual(expectedActionsWithPriorityTrue);+ expect(TextX._transform(actionsA, actionsB, 'left')).toEqual(expectedActionsWithPriorityFalse);});it('insert after the retain attributes', () => {diff --git a/packages/core/src/docs/data-model/text-x/action-iterator.ts b/packages/core/src/docs/data-model/text-x/action-iterator.tsindex 6f78df7480..9c0f867bee 100644--- a/packages/core/src/docs/data-model/text-x/action-iterator.ts+++ b/packages/core/src/docs/data-model/text-x/action-iterator.ts@@ -14,8 +14,10 @@* limitations under the License.*/-import { Tools } from '../../../shared/tools';+/* eslint-disable no-param-reassign */+import type { TextXAction } from './action-types';+import { Tools } from '../../../shared/tools';import { TextXActionType } from './action-types';import { getBodySlice } from './utils';@@ -114,6 +116,7 @@ export class ActionIterator {const index = this._index;const next = this.next();const restActions = this._actions.slice(this._index);+this._offset = offset;this._index = index;diff --git a/packages/core/src/docs/data-model/text-x/build-utils/index.ts b/packages/core/src/docs/data-model/text-x/build-utils/index.tsindex d7f3616fa1..67adbffed4 100644--- a/packages/core/src/docs/data-model/text-x/build-utils/index.ts+++ b/packages/core/src/docs/data-model/text-x/build-utils/index.ts@@ -41,7 +41,7 @@ export class BuildTextUtils {getInsertSelection,getDeleteActions: getRetainAndDeleteFromReplace,- getDeleteExculdeLastLineBreakActions: getRetainAndDeleteAndExcludeLineBreak,+ getDeleteExcludeLastLineBreakActions: getRetainAndDeleteAndExcludeLineBreak,};static range = {diff --git a/packages/core/src/docs/data-model/text-x/transform-utils.ts b/packages/core/src/docs/data-model/text-x/transform-utils.tsindex 239e632ec6..8344aefe8d 100644--- a/packages/core/src/docs/data-model/text-x/transform-utils.ts+++ b/packages/core/src/docs/data-model/text-x/transform-utils.ts@@ -14,8 +14,10 @@* limitations under the License.*/+/* eslint-disable no-param-reassign */+import type { Nullable } from '../../../shared';-import type { IDocumentBody, IParagraph, IParagraphStyle, ITextRun, ITextStyle } from '../../../types/interfaces';+import type { ICustomRange, IDocumentBody, IParagraph, IParagraphStyle, ITextRun, ITextStyle } from '../../../types/interfaces';import type { IRetainAction } from './action-types';import { Tools, UpdateDocsAttributeType } from '../../../shared';import { normalizeTextRuns } from './apply-utils/common';@@ -53,6 +55,7 @@ function transformTextRuns(originTextRuns: ITextRun[], targetTextRuns: ITextRun[return false;}+ // TODO: @JOCS, I think no need to iterate over the text runs and compare them! because the textRuns length is always 1.while (updateIndex < updateLength && removeIndex < removeLength) {const { st: updateSt, ed: updateEd, ts: targetStyle } = targetTextRuns[updateIndex];const { st: removeSt, ed: removeEd, ts: originStyle, sId } = originTextRuns[removeIndex];@@ -147,6 +150,25 @@ function transformTextRuns(originTextRuns: ITextRun[], targetTextRuns: ITextRun[return normalizeTextRuns(newUpdateTextRuns);}+function transformCustomRanges(+ originCustomRanges: ICustomRange[],+ targetCustomRanges: ICustomRange[],+ transformType: TextXTransformType,+ coverType: UpdateDocsAttributeType = UpdateDocsAttributeType.COVER+): ICustomRange[] {+ if (originCustomRanges.length === 0) {+ return Tools.deepClone(targetCustomRanges);+ }++ if (coverType === UpdateDocsAttributeType.REPLACE) {+ return transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED+ ? Tools.deepClone(originCustomRanges)+ : Tools.deepClone(targetCustomRanges);+ } else {+ throw new Error('CustomRanges is only supported in replace mode.');+ }+}+// At present, only the two properties of paragraphStyle and bullet are handled,// paragraphStyle is treated separately, while bullet is treated as a whole because// the properties in bullet are related@@ -207,7 +229,7 @@ function transformParagraph(return paragraph;}-// eslint-disable-next-line max-lines-per-function+// eslint-disable-next-line max-lines-per-function, complexityexport function transformBody(thisAction: IRetainAction,otherAction: IRetainAction,@@ -215,8 +237,9 @@ export function transformBody(): IDocumentBody {const { body: thisBody, coverType: thisCoverType } = thisAction;const { body: otherBody, coverType: otherCoverType } = otherAction;+if (thisBody == null || thisBody.dataStream !== '' || otherBody == null || otherBody.dataStream !== '') {- throw new Error('Data stream is not supported in transform.');+ throw new Error('Data stream is not supported in retain transform.');}if (thisCoverType !== otherCoverType) {@@ -227,8 +250,8 @@ export function transformBody(dataStream: '',};- const { textRuns: thisTextRuns = [], paragraphs: thisParagraphs = [] } = thisBody;- const { textRuns: otherTextRuns = [], paragraphs: otherParagraphs = [] } = otherBody;+ const { textRuns: thisTextRuns = [], paragraphs: thisParagraphs = [], customRanges: thisCustomRanges = [] } = thisBody;+ const { textRuns: otherTextRuns = [], paragraphs: otherParagraphs = [], customRanges: otherCustomRanges = [] } = otherBody;let textRuns: ITextRun[] = [];if (priority) {@@ -240,6 +263,17 @@ export function transformBody(retBody.textRuns = textRuns;}+ let customRanges: ICustomRange[] = [];+ if (priority) {+ customRanges = transformCustomRanges(thisCustomRanges, otherCustomRanges, TextXTransformType.COVER_ONLY_NOT_EXISTED, thisCoverType);+ } else {+ customRanges = transformCustomRanges(thisCustomRanges, otherCustomRanges, TextXTransformType.COVER, thisCoverType);+ }++ if (customRanges.length) {+ retBody.customRanges = customRanges;+ }+const paragraphs: IParagraph[] = [];let thisIndex = 0;diff --git a/packages/core/src/docs/data-model/text-x/utils.ts b/packages/core/src/docs/data-model/text-x/utils.tsindex af934a1977..1a9e7eddd0 100644--- a/packages/core/src/docs/data-model/text-x/utils.ts+++ b/packages/core/src/docs/data-model/text-x/utils.ts@@ -355,9 +355,9 @@ export function isUselessRetainAction(action: IRetainAction): boolean {return true;}- const { textRuns = [], paragraphs = [] } = body;+ const { textRuns = [], paragraphs = [], customRanges = [], customBlocks = [], customDecorations = [] } = body;- if (textRuns.length === 0 && paragraphs.length === 0) {+ if (textRuns.length === 0 && paragraphs.length === 0 && customRanges.length === 0 && customBlocks.length === 0 && customDecorations.length === 0) {return true;}diff --git a/packages/core/src/docs/data-model/types.ts b/packages/core/src/docs/data-model/types.tsindex 6795e4056e..c2b32c5b05 100644--- a/packages/core/src/docs/data-model/types.ts+++ b/packages/core/src/docs/data-model/types.ts@@ -37,7 +37,7 @@ export enum DataStreamTreeTokenType {TABLE_CELL_END = '\x1D', // 表格单元格结束TABLE_ROW_END = '\x0E', // 表格行结束TABLE_END = '\x0F', // 表格结束-+ // custom range.CUSTOM_RANGE_START = '\x1F', // 自定义范围开始CUSTOM_RANGE_END = '\x1E', // 自定义范围结束@@ -45,6 +45,7 @@ export enum DataStreamTreeTokenType {PAGE_BREAK = '\f', // 换页DOCS_END = '\0', // 文档结尾TAB = '\t', // 制表符+ // custom block.CUSTOM_BLOCK = '\b', // 图片 mention 等不参与文档流的场景LETTER = '',diff --git a/packages/core/src/types/interfaces/i-document-data.ts b/packages/core/src/types/interfaces/i-document-data.tsindex 37bbff5439..665d769863 100644--- a/packages/core/src/types/interfaces/i-document-data.ts+++ b/packages/core/src/types/interfaces/i-document-data.ts@@ -134,6 +134,7 @@ export interface IDocumentBody {// links?: { [index: number]: IHyperlink }; // linkscustomRanges?: ICustomRange[]; // plugin register,implement special logic for streams, hyperlink, field,structured document tags, bookmark,comment+customDecorations?: ICustomDecoration[];/**diff --git a/packages/docs-ui/src/commands/commands/break-line.command.ts b/packages/docs-ui/src/commands/commands/break-line.command.tsindex c4617fd53d..2df0da2d4a 100644--- a/packages/docs-ui/src/commands/commands/break-line.command.ts+++ b/packages/docs-ui/src/commands/commands/break-line.command.ts@@ -14,8 +14,8 @@* limitations under the License.*/-import type { ICommand, IParagraph } from '@univerjs/core';-import { BuildTextUtils, CommandType, DataStreamTreeTokenType, getBodySlice, ICommandService, IUniverInstanceService, normalizeBody, PresetListType, Tools, updateAttributeByInsert } from '@univerjs/core';+import type { DocumentDataModel, ICommand, IParagraph } from '@univerjs/core';+import { BuildTextUtils, CommandType, DataStreamTreeTokenType, getBodySlice, ICommandService, IUniverInstanceService, normalizeBody, PresetListType, Tools, UniverInstanceType, updateAttributeByInsert } from '@univerjs/core';import { DocSelectionManagerService } from '@univerjs/docs';import { InsertCommand } from './core-editing.command';@@ -51,6 +51,7 @@ export function generateParagraphs(dataStream: string, prevParagraph?: IParagrapexport const BreakLineCommand: ICommand = {id: 'doc.command.break-line',+type: CommandType.COMMAND,handler: async (accessor) => {@@ -68,21 +69,23 @@ export const BreakLineCommand: ICommand = {if (rectRanges && rectRanges.length) {const { startOffset } = activeTextRange;- docSelectionManagerService.replaceTextRanges([{+ docSelectionManagerService.replaceDocRanges([{startOffset,endOffset: startOffset,}]);+return true;}const { segmentId } = activeTextRange;- const docDataModel = univerInstanceService.getCurrentUniverDocInstance();- const body = docDataModel?.getSelfOrHeaderFooterModel(segmentId).getBody();- if (!docDataModel || !body) {+ const docDataModel = univerInstanceService.getCurrentUnitForType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC);+ const body = docDataModel?.getSelfOrHeaderFooterModel(segmentId ?? '').getBody();+ if (docDataModel == null || body == null) {return false;}const unitId = docDataModel.getUnitId();+const { startOffset, endOffset } = BuildTextUtils.selection.getInsertSelection(activeTextRange, body);const paragraphs = body.paragraphs ?? [];diff --git a/packages/docs-ui/src/commands/commands/clipboard.inner.command.ts b/packages/docs-ui/src/commands/commands/clipboard.inner.command.tsindex 19366ca2d5..7c04c8270c 100644--- a/packages/docs-ui/src/commands/commands/clipboard.inner.command.ts+++ b/packages/docs-ui/src/commands/commands/clipboard.inner.command.ts@@ -259,7 +259,7 @@ function getCutActionsFromTextRanges(segmentId,});} else {- textX.push(...BuildTextUtils.selection.getDeleteExculdeLastLineBreakActions(selection, originBody, segmentId, memoryCursor.cursor, false));+ textX.push(...BuildTextUtils.selection.getDeleteExcludeLastLineBreakActions(selection, originBody, segmentId, memoryCursor.cursor, false));}memoryCursor.reset();diff --git a/packages/docs-ui/src/commands/commands/core-editing.command.ts b/packages/docs-ui/src/commands/commands/core-editing.command.tsindex 9a24a83ade..951932ed50 100644--- a/packages/docs-ui/src/commands/commands/core-editing.command.ts+++ b/packages/docs-ui/src/commands/commands/core-editing.command.ts@@ -46,6 +46,7 @@ export const EditorInsertTextCommandId = 'doc.command.insert-text';*/export const InsertCommand: ICommand<IInsertCommandParams> = {id: EditorInsertTextCommandId,+type: CommandType.COMMAND,// eslint-disable-next-line max-lines-per-function@@ -106,6 +107,7 @@ export const InsertCommand: ICommand<IInsertCommandParams> = {const { dos, retain } = BuildTextUtils.selection.getDeleteActions(actualRange, segmentId, 0, originBody);textX.push(...dos);+doMutation.params.textRanges = [{startOffset: startOffset + cursorMove + retain,endOffset: startOffset + cursorMove + retain,diff --git a/packages/docs-ui/src/commands/commands/replace-content.command.ts b/packages/docs-ui/src/commands/commands/replace-content.command.tsindex f36b6693f1..07df1025fd 100644--- a/packages/docs-ui/src/commands/commands/replace-content.command.ts+++ b/packages/docs-ui/src/commands/commands/replace-content.command.ts@@ -338,7 +338,7 @@ export const ReplaceSelectionCommand: ICommand<IReplaceSelectionCommandParams> =const textX = new TextX();const jsonX = JSONX.getInstance();// delete- textX.push(...BuildTextUtils.selection.getDeleteExculdeLastLineBreakActions(selection, body, '', 0, false));+ textX.push(...BuildTextUtils.selection.getDeleteExcludeLastLineBreakActions(selection, body, '', 0, false));// inserttextX.push({t: TextXActionType.INSERT,