Skip to content

Commit

Permalink
fix(sheets-hyper-link-ui): link ref -range (#4131)
Browse files Browse the repository at this point in the history
  • Loading branch information
weird94 authored Nov 23, 2024
1 parent a58350b commit 924ad5e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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,
};
}
}
}
Expand All @@ -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()
));
}
}
});
Expand Down Expand Up @@ -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);
}
}
}
})
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -43,6 +43,7 @@ export class SheetsHyperLinkController extends Disposable {
UpdateHyperLinkMutation,
RemoveHyperLinkMutation,
UpdateHyperLinkRefMutation,
UpdateRichHyperLinkMutation,
].forEach((command) => {
this._commandService.registerCommand(command);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';
Expand Down

0 comments on commit 924ad5e

Please sign in to comment.