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.

fix(sheets-hyper-link-ui): link ref -range (#4131)

zhangw924ad5eb92cb
diff --git a/packages/sheets-hyper-link/src/commands/mutations/update-hyper-link.mutation.ts b/packages/sheets-hyper-link/src/commands/mutations/update-hyper-link.mutation.ts
index ec9d85aed9..7ec256ea0c 100644
--- a/packages/sheets-hyper-link/src/commands/mutations/update-hyper-link.mutation.ts
+++ b/packages/sheets-hyper-link/src/commands/mutations/update-hyper-link.mutation.ts
@@ -15,9 +15,10 @@
*/
import type { ICommand } from '@univerjs/core';
-import { CommandType } from '@univerjs/core';
-import { HyperLinkModel } from '../../models/hyper-link.model';
import type { ICellLinkContent } from '../../types/interfaces/i-hyper-link';
+import { CommandType, CustomRangeType, IUniverInstanceService } from '@univerjs/core';
+import { getSheetCommandTarget } from '@univerjs/sheets';
+import { HyperLinkModel } from '../../models/hyper-link.model';
export interface IUpdateHyperLinkMutationParams {
unitId: string;
@@ -62,3 +63,39 @@ export const UpdateHyperLinkRefMutation: ICommand<IUpdateHyperLinkRefMutationPar
return model.updateHyperLinkRef(unitId, subUnitId, id, { row, column }, silent);
},
};
+
+export interface IUpdateRichHyperLinkMutationParams {
+ unitId: string;
+ subUnitId: string;
+ row: number;
+ col: number;
+ id: string;
+ url: string;
+}
+
+export const UpdateRichHyperLinkMutation: ICommand<IUpdateRichHyperLinkMutationParams> = {
+ type: CommandType.MUTATION,
+ id: 'sheets.mutation.update-rich-hyper-link',
+ handler(accessor, params) {
+ if (!params) {
+ return false;
+ }
+
+ const { unitId, subUnitId, row, col, id, url } = params;
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const sheetTarget = getSheetCommandTarget(univerInstanceService, { unitId, subUnitId });
+ if (!sheetTarget) {
+ return false;
+ }
+ const { worksheet } = sheetTarget;
+ const cell = worksheet.getCellRaw(row, col);
+
+ const link = cell?.p?.body?.customRanges?.find((range) => range.rangeType === CustomRangeType.HYPERLINK && range.rangeId === id);
+ if (!link) {
+ return true;
+ }
+
+ link.properties!.url = url;
+ return true;
+ },
+};
diff --git a/packages/sheets-hyper-link/src/controllers/rich-text-ref-range.controller.ts b/packages/sheets-hyper-link/src/controllers/rich-text-ref-range.controller.ts
index 4aaa475cc2..610997ecca 100644
--- a/packages/sheets-hyper-link/src/controllers/rich-text-ref-range.controller.ts
+++ b/packages/sheets-hyper-link/src/controllers/rich-text-ref-range.controller.ts
@@ -14,11 +14,13 @@
* limitations under the License.
*/
-import type { IDisposable, IDocumentData, IRange, Nullable, Workbook } from '@univerjs/core';
+import type { IDisposable, IDocumentData, Workbook } from '@univerjs/core';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
-import { CustomRangeType, Disposable, DisposableCollection, ICommandService, Inject, isValidRange, IUniverInstanceService, ObjectMatrix, UniverInstanceType } from '@univerjs/core';
+import type { IUpdateRichHyperLinkMutationParams } from '../commands/mutations/update-hyper-link.mutation';
+import { CustomRangeType, Disposable, DisposableCollection, ICommandService, Inject, isValidRange, IUniverInstanceService, ObjectMatrix, Rectangle, UniverInstanceType } from '@univerjs/core';
import { deserializeRangeWithSheet, serializeRange } from '@univerjs/engine-formula';
-import { RefRangeService, SetRangeValuesMutation } from '@univerjs/sheets';
+import { getSheetCommandTarget, handleDefaultRangeChangeWithEffectRefCommands, RefRangeService, SetRangeValuesMutation } from '@univerjs/sheets';
+import { UpdateRichHyperLinkMutation } from '../commands/mutations/update-hyper-link.mutation';
import { ERROR_RANGE } from '../types/const';
export class SheetsHyperLinkRichTextRefRangeController extends Disposable {
@@ -48,7 +50,7 @@ export class SheetsHyperLinkRichTextRefRangeController extends Disposable {
return subUnitMap;
}
- private _isLegalRangeUrl(unitId: string, payload: string): Nullable<IRange> {
+ private _isLegalRangeUrl(unitId: string, payload: string) {
const workbook = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET);
if (!workbook) {
return null;
@@ -71,7 +73,10 @@ export class SheetsHyperLinkRichTextRefRangeController extends Disposable {
}
const range = deserializeRangeWithSheet(searchObj.range).range;
if (isValidRange(range, worksheet) && searchObj.range !== ERROR_RANGE) {
- return range;
+ return {
+ range,
+ worksheet,
+ };
}
}
}
@@ -88,12 +93,53 @@ export class SheetsHyperLinkRichTextRefRangeController extends Disposable {
p.body?.customRanges?.forEach((customRange) => {
if (customRange.rangeType === CustomRangeType.HYPERLINK) {
const payload = customRange.properties?.url;
- const range = this._isLegalRangeUrl(unitId, payload);
- if (range) {
+ const rangeInfo = this._isLegalRangeUrl(unitId, payload);
+
+ if (rangeInfo) {
+ const { range, worksheet } = rangeInfo;
hasWatch = true;
- disposableCollection.add(this._refRangeService.watchRange(unitId, subUnitId, range, (before, after) => {
- customRange.properties!.url = `#gid=${subUnitId}&range=${after ? serializeRange(after) : ERROR_RANGE}`;
- }));
+ disposableCollection.add(
+ this._refRangeService.registerRefRange(
+ range,
+ (commandInfo) => {
+ const newRange = handleDefaultRangeChangeWithEffectRefCommands(range, commandInfo);
+ if (newRange && Rectangle.equals(newRange, range)) {
+ return {
+ preRedos: [],
+ preUndos: [],
+ redos: [],
+ undos: [],
+ };
+ }
+ return {
+ preRedos: [{
+ id: UpdateRichHyperLinkMutation.id,
+ params: {
+ unitId,
+ subUnitId,
+ row,
+ col,
+ id: customRange.rangeId,
+ url: `#gid=${subUnitId}&range=${newRange ? serializeRange(newRange) : ERROR_RANGE}`,
+ },
+ }],
+ undos: [{
+ id: UpdateRichHyperLinkMutation.id,
+ params: {
+ unitId,
+ subUnitId,
+ row,
+ col,
+ id: customRange.rangeId,
+ url: payload,
+ },
+ }],
+ redos: [],
+ };
+ },
+ worksheet.getUnitId(),
+ worksheet.getSheetId()
+ ));
}
}
});
@@ -176,5 +222,27 @@ export class SheetsHyperLinkRichTextRefRangeController extends Disposable {
}
})
);
+
+ this.disposeWithMe(
+ this._commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === UpdateRichHyperLinkMutation.id) {
+ const params = commandInfo.params as IUpdateRichHyperLinkMutationParams;
+ const { unitId, subUnitId, row, col } = params;
+ const sheetTarget = getSheetCommandTarget(this._univerInstanceService, { unitId, subUnitId });
+ const map = this._enusreMap(unitId, subUnitId);
+ const dispose = map.getValue(row, col);
+ if (dispose) {
+ dispose.dispose();
+ }
+ if (sheetTarget) {
+ const { worksheet } = sheetTarget;
+ const cell = worksheet.getCellRaw(row, col);
+ if (cell && cell.p) {
+ this._registerRange(unitId, subUnitId, row, col, cell.p);
+ }
+ }
+ }
+ })
+ );
}
}
diff --git a/packages/sheets-hyper-link/src/controllers/sheet-hyper-link.controller.ts b/packages/sheets-hyper-link/src/controllers/sheet-hyper-link.controller.ts
index 8cfeda355c..a1df0ee860 100644
--- a/packages/sheets-hyper-link/src/controllers/sheet-hyper-link.controller.ts
+++ b/packages/sheets-hyper-link/src/controllers/sheet-hyper-link.controller.ts
@@ -20,7 +20,7 @@ import { CancelHyperLinkCommand, CancelRichHyperLinkCommand } from '../commands/
import { UpdateHyperLinkCommand, UpdateRichHyperLinkCommand } from '../commands/commands/update-hyper-link.command';
import { AddHyperLinkMutation } from '../commands/mutations/add-hyper-link.mutation';
import { RemoveHyperLinkMutation } from '../commands/mutations/remove-hyper-link.mutation';
-import { UpdateHyperLinkMutation, UpdateHyperLinkRefMutation } from '../commands/mutations/update-hyper-link.mutation';
+import { UpdateHyperLinkMutation, UpdateHyperLinkRefMutation, UpdateRichHyperLinkMutation } from '../commands/mutations/update-hyper-link.mutation';
export class SheetsHyperLinkController extends Disposable {
constructor(
@@ -43,6 +43,7 @@ export class SheetsHyperLinkController extends Disposable {
UpdateHyperLinkMutation,
RemoveHyperLinkMutation,
UpdateHyperLinkRefMutation,
+ UpdateRichHyperLinkMutation,
].forEach((command) => {
this._commandService.registerCommand(command);
});
diff --git a/packages/sheets/src/commands/commands/insert-range-move-down.command.ts b/packages/sheets/src/commands/commands/insert-range-move-down.command.ts
index 7413bcd171..4b1afd0b5d 100644
--- a/packages/sheets/src/commands/commands/insert-range-move-down.command.ts
+++ b/packages/sheets/src/commands/commands/insert-range-move-down.command.ts
@@ -15,6 +15,12 @@
*/
import type { IAccessor, ICellData, ICommand, IMutationInfo, IObjectMatrixPrimitiveType, IRange } from '@univerjs/core';
+import type {
+ IInsertRangeMutationParams,
+ IInsertRowMutationParams,
+ IRemoveRowsMutationParams,
+} from '../../basics/interfaces/mutation-interface';
+
import {
BooleanNumber,
CommandType,
@@ -27,12 +33,6 @@ import {
Range,
sequenceExecute,
} from '@univerjs/core';
-
-import type {
- IInsertRangeMutationParams,
- IInsertRowMutationParams,
- IRemoveRowsMutationParams,
-} from '../../basics/interfaces/mutation-interface';
import { SheetsSelectionsService } from '../../services/selections/selection-manager.service';
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import { InsertRowMutation, InsertRowMutationUndoFactory } from '../mutations/insert-row-col.mutation';