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(docs-link): error display url link when add link (#3822)
zhangwf6cd524f66a8
diff --git a/packages/core/src/docs/data-model/text-x/build-utils/__test__/custom-range.spec.ts b/packages/core/src/docs/data-model/text-x/build-utils/__test__/custom-range.spec.tsindex a11cd82e38..00fc1327b9 100644--- a/packages/core/src/docs/data-model/text-x/build-utils/__test__/custom-range.spec.ts+++ b/packages/core/src/docs/data-model/text-x/build-utils/__test__/custom-range.spec.ts@@ -14,11 +14,11 @@* limitations under the License.*/-import { describe, expect, it } from 'vitest';-import { excludePointsFromRange, getCustomRangesInterestsWithRange, shouldDeleteCustomRange } from '../custom-range'; // 替换为你的文件路径+import type { ITextRange } from '../../../../../sheets/typedef';+import { describe, expect, it } from 'vitest'; // 替换为你的文件路径import { CustomRangeType, type ICustomRange } from '../../../../../types/interfaces';import { DataStreamTreeTokenType } from '../../../types';-import type { ITextRange } from '../../../../../sheets/typedef';+import { excludePointsFromRange, getCustomRangesInterestsWithSelection, shouldDeleteCustomRange } from '../custom-range';describe('excludePointsFromRange function', () => {it('should handle empty points array', () => {@@ -108,14 +108,14 @@ describe('excludePointsFromRange function', () => {});});-describe('getCustomRangesInterestsWithRange function', () => {+describe('getCustomRangesInterestsWithSelection function', () => {it('should return empty array if no custom ranges intersect', () => {const range: ITextRange = { startOffset: 5, endOffset: 10, collapsed: false };const customRanges: ICustomRange[] = [{ startIndex: 0, endIndex: 4, rangeId: '1', rangeType: CustomRangeType.HYPERLINK },{ startIndex: 11, endIndex: 15, rangeId: '2', rangeType: CustomRangeType.HYPERLINK },];- expect(getCustomRangesInterestsWithRange(range, customRanges)).toEqual([]);+ expect(getCustomRangesInterestsWithSelection(range, customRanges)).toEqual([]);});it('should return intersecting custom ranges', () => {@@ -124,7 +124,7 @@ describe('getCustomRangesInterestsWithRange function', () => {{ startIndex: 0, endIndex: 6, rangeId: '1', rangeType: CustomRangeType.HYPERLINK },{ startIndex: 8, endIndex: 12, rangeId: '2', rangeType: CustomRangeType.HYPERLINK },];- expect(getCustomRangesInterestsWithRange(range, customRanges)).toEqual([+ expect(getCustomRangesInterestsWithSelection(range, customRanges)).toEqual([{ startIndex: 0, endIndex: 6, rangeId: '1', rangeType: CustomRangeType.HYPERLINK },{ startIndex: 8, endIndex: 12, rangeId: '2', rangeType: CustomRangeType.HYPERLINK },]);@@ -136,7 +136,7 @@ describe('getCustomRangesInterestsWithRange function', () => {{ startIndex: 0, endIndex: 4, rangeId: '1', rangeType: CustomRangeType.HYPERLINK },{ startIndex: 5, endIndex: 10, rangeId: '2', rangeType: CustomRangeType.HYPERLINK },];- expect(getCustomRangesInterestsWithRange(range, customRanges)).toEqual([]);+ expect(getCustomRangesInterestsWithSelection(range, customRanges)).toEqual([]);});it('should handle multiple intersecting custom ranges', () => {@@ -147,7 +147,7 @@ describe('getCustomRangesInterestsWithRange function', () => {{ startIndex: 14, endIndex: 20, rangeId: '3', rangeType: CustomRangeType.HYPERLINK },];- expect(getCustomRangesInterestsWithRange(range, customRanges)).toEqual([+ expect(getCustomRangesInterestsWithSelection(range, customRanges)).toEqual([{ startIndex: 0, endIndex: 6, rangeId: '1', rangeType: CustomRangeType.HYPERLINK },{ startIndex: 8, endIndex: 12, rangeId: '2', rangeType: CustomRangeType.HYPERLINK },{ startIndex: 14, endIndex: 20, rangeId: '3', rangeType: CustomRangeType.HYPERLINK },diff --git a/packages/core/src/docs/data-model/text-x/build-utils/custom-range.ts b/packages/core/src/docs/data-model/text-x/build-utils/custom-range.tsindex 719f8ac6d2..0be5be0d0c 100644--- a/packages/core/src/docs/data-model/text-x/build-utils/custom-range.ts+++ b/packages/core/src/docs/data-model/text-x/build-utils/custom-range.ts@@ -64,7 +64,7 @@ export function shouldDeleteCustomRange(deleteStart: number, deleteLen: number,return true;}-export function getCustomRangesInterestsWithRange(range: ITextRange, customRanges: ICustomRange[]) {+export function getCustomRangesInterestsWithSelection(range: ITextRange, customRanges: ICustomRange[]) {const result: ICustomRange[] = [];for (let i = 0, len = customRanges.length; i < len; i++) {const customRange = customRanges[i];@@ -73,7 +73,7 @@ export function getCustomRangesInterestsWithRange(range: ITextRange, customRangeresult.push(customRange);}} else {- if (isIntersecting(range.startOffset, range.endOffset, customRange.startIndex, customRange.endIndex)) {+ if (isIntersecting(range.startOffset, range.endOffset - 1, customRange.startIndex, customRange.endIndex)) {result.push(customRange);}}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 0ed66b57fe..d7f3616fa1 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@@ -14,7 +14,7 @@* limitations under the License.*/-import { copyCustomRange, getCustomRangesInterestsWithRange, isIntersecting, shouldDeleteCustomRange } from './custom-range';+import { copyCustomRange, getCustomRangesInterestsWithSelection, isIntersecting, shouldDeleteCustomRange } from './custom-range';import { changeParagraphBulletNestLevel, setParagraphBullet, switchParagraphBullet, toggleChecklistParagraph } from './paragraph';import { fromPlainText, getPlainText, isEmptyDocument } from './parse';import { getDeleteSelection, getInsertSelection, getRetainAndDeleteFromReplace, isSegmentIntersects, makeSelection, normalizeSelection } from './selection';@@ -26,7 +26,7 @@ export class BuildTextUtils {delete: deleteCustomRangeTextX,copyCustomRange,- getCustomRangesInterestsWithRange,+ getCustomRangesInterestsWithSelection,shouldDeleteCustomRange,isIntersecting,diff --git a/packages/core/src/docs/data-model/text-x/build-utils/selection.ts b/packages/core/src/docs/data-model/text-x/build-utils/selection.tsindex 66f62fcee4..f288726a81 100644--- a/packages/core/src/docs/data-model/text-x/build-utils/selection.ts+++ b/packages/core/src/docs/data-model/text-x/build-utils/selection.ts@@ -53,10 +53,18 @@ export function getSelectionWithSymbolMax(selection: ITextRange, body: IDocumentstartOffset -= 1;}+ while (body.dataStream[startOffset] === DataStreamTreeTokenType.CUSTOM_RANGE_END) {+ startOffset += 1;+ }+while (body.dataStream[endOffset] === DataStreamTreeTokenType.CUSTOM_RANGE_END) {endOffset += 1;}+ while (body.dataStream[endOffset - 1] === DataStreamTreeTokenType.CUSTOM_RANGE_START) {+ endOffset -= 1;+ }+return {startOffset,endOffset,diff --git a/packages/docs-hyper-link-ui/src/commands/operations/popup.operation.ts b/packages/docs-hyper-link-ui/src/commands/operations/popup.operation.tsindex 71220ce032..317962903e 100644--- a/packages/docs-hyper-link-ui/src/commands/operations/popup.operation.ts+++ b/packages/docs-hyper-link-ui/src/commands/operations/popup.operation.ts@@ -14,9 +14,9 @@* limitations under the License.*/+import type { DocumentDataModel, IAccessor, ICommand, ITextRange } from '@univerjs/core';import { BuildTextUtils, CommandType, CustomRangeType, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';import { DocSelectionManagerService } from '@univerjs/docs';-import type { DocumentDataModel, IAccessor, ICommand, ITextRange } from '@univerjs/core';import { DocHyperLinkPopupService } from '../../services/hyper-link-popup.service';export const shouldDisableAddLink = (accessor: IAccessor) => {@@ -50,7 +50,7 @@ export const shouldDisableAddLink = (accessor: IAccessor) => {}}- const insertCustomRanges = BuildTextUtils.customRange.getCustomRangesInterestsWithRange(activeRange as ITextRange, body.customRanges ?? []);+ const insertCustomRanges = BuildTextUtils.customRange.getCustomRangesInterestsWithSelection(activeRange as ITextRange, body.customRanges ?? []);// can't insert hyperlink in range contains other custom rangesreturn !insertCustomRanges.every((range) => range.rangeType === CustomRangeType.HYPERLINK);};diff --git a/packages/docs-hyper-link-ui/src/views/hyper-link-edit/index.tsx b/packages/docs-hyper-link-ui/src/views/hyper-link-edit/index.tsxindex f16e1ce0b3..989280fb79 100644--- a/packages/docs-hyper-link-ui/src/views/hyper-link-edit/index.tsx+++ b/packages/docs-hyper-link-ui/src/views/hyper-link-edit/index.tsx@@ -71,7 +71,9 @@ export const DocHyperLinkEdit = () => {return;}- const matchedRange = doc?.getSelfOrHeaderFooterModel(activeRange.segmentId)?.getBody()?.customRanges?.find((i) => Math.max(activeRange.startOffset, i.startIndex) <= Math.min(activeRange.endOffset - 1, i.endIndex));+ const body = doc?.getSelfOrHeaderFooterModel(activeRange.segmentId)?.getBody();+ const selection = body ? BuildTextUtils.selection.getInsertSelection(activeRange, body) : null;+ const matchedRange = selection && BuildTextUtils.customRange.getCustomRangesInterestsWithSelection(selection, body?.customRanges ?? [])?.[0];if (doc && matchedRange) {setLink(matchedRange?.properties?.url ?? '');}diff --git a/packages/docs-ui/src/services/doc-auto-format.service.ts b/packages/docs-ui/src/services/doc-auto-format.service.tsindex d06fdb6b03..9a21fd541d 100644--- a/packages/docs-ui/src/services/doc-auto-format.service.ts+++ b/packages/docs-ui/src/services/doc-auto-format.service.ts@@ -14,10 +14,10 @@* limitations under the License.*/-import { BuildTextUtils, Disposable, Inject, IUniverInstanceService, toDisposable, UniverInstanceType } from '@univerjs/core';-import { DocSelectionManagerService } from '@univerjs/docs';import type { DocumentDataModel, ICommandInfo, ICustomRange, IDisposable, IParagraph, IParagraphRange, ITextRange, Nullable } from '@univerjs/core';import type { ITextRangeWithStyle } from '@univerjs/engine-render';+import { BuildTextUtils, Disposable, Inject, IUniverInstanceService, toDisposable, UniverInstanceType } from '@univerjs/core';+import { DocSelectionManagerService } from '@univerjs/docs';export interface IAutoFormatContext {unit: DocumentDataModel;@@ -121,7 +121,7 @@ export class DocAutoFormatService extends Disposable {selection,isBody: !selection.segmentId,paragraphs: getParagraphsInRange(selection, doc.getBody()?.paragraphs ?? []),- customRanges: BuildTextUtils.customRange.getCustomRangesInterestsWithRange(selection, doc.getBody()?.customRanges ?? []),+ customRanges: BuildTextUtils.customRange.getCustomRangesInterestsWithSelection(selection, doc.getBody()?.customRanges ?? []),commandId: id,commandParams: params,};diff --git a/packages/sheets-hyper-link-ui/src/services/popup.service.ts b/packages/sheets-hyper-link-ui/src/services/popup.service.tsindex 5ca508ca74..104dfe689d 100644--- a/packages/sheets-hyper-link-ui/src/services/popup.service.ts+++ b/packages/sheets-hyper-link-ui/src/services/popup.service.ts@@ -213,7 +213,7 @@ export class SheetsHyperLinkPopupService extends Disposable {label: BuildTextUtils.transform.getPlainText(body.dataStream),};}- const links = BuildTextUtils.customRange.getCustomRangesInterestsWithRange(textRange, body.customRanges?.filter((i) => i.rangeType === CustomRangeType.HYPERLINK) ?? []);+ const links = BuildTextUtils.customRange.getCustomRangesInterestsWithSelection(textRange, body.customRanges?.filter((i) => i.rangeType === CustomRangeType.HYPERLINK) ?? []);let start = textRange.startOffset;let end = textRange.endOffset;links.forEach((link) => {diff --git a/packages/sheets-hyper-link-ui/src/utils/index.ts b/packages/sheets-hyper-link-ui/src/utils/index.tsindex 29030e023b..e305a52dc4 100644--- a/packages/sheets-hyper-link-ui/src/utils/index.ts+++ b/packages/sheets-hyper-link-ui/src/utils/index.ts@@ -83,7 +83,7 @@ export const shouldDisableAddLink = (accessor: IAccessor) => {}}- const insertCustomRanges = BuildTextUtils.customRange.getCustomRangesInterestsWithRange(activeRange as ITextRange, body.customRanges ?? []);+ const insertCustomRanges = BuildTextUtils.customRange.getCustomRangesInterestsWithSelection(activeRange as ITextRange, body.customRanges ?? []);// can't insert hyperlink in range contains other custom rangesreturn !insertCustomRanges.every((range) => range.rangeType === CustomRangeType.HYPERLINK);};diff --git a/packages/sheets-hyper-link-ui/src/views/CellLinkEdit/index.tsx b/packages/sheets-hyper-link-ui/src/views/CellLinkEdit/index.tsxindex c645eff7e8..fc75672b73 100644--- a/packages/sheets-hyper-link-ui/src/views/CellLinkEdit/index.tsx+++ b/packages/sheets-hyper-link-ui/src/views/CellLinkEdit/index.tsx@@ -119,8 +119,10 @@ export const CellLinkEdit = () => {};} else {const doc = univerInstanceService.getCurrentUnitForType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC);- const selection = textSelectionService.getActiveTextRange();- const customRange = selection && BuildTextUtils.customRange.getCustomRangesInterestsWithRange(selection, doc?.getBody()?.customRanges ?? [])?.[0];+ const currentSelection = textSelectionService.getActiveTextRange();+ const body = doc?.getBody();+ const selection = currentSelection && body ? BuildTextUtils.selection.getInsertSelection(currentSelection, body) : null;+ const customRange = selection && BuildTextUtils.customRange.getCustomRangesInterestsWithSelection(selection, body?.customRanges ?? [])?.[0];setShowLabel(false);link = {