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: email link jump error (#2378)

zhangw950be43da554
diff --git a/packages/sheets-hyper-link-ui/src/common/util.ts b/packages/sheets-hyper-link-ui/src/common/util.ts
index ceade6f83d..8f6af8b184 100644
--- a/packages/sheets-hyper-link-ui/src/common/util.ts
+++ b/packages/sheets-hyper-link-ui/src/common/util.ts
@@ -28,3 +28,16 @@ export function hasProtocol(urlString: string) {
const pattern = /^[a-zA-Z]+:\/\//;
return pattern.test(urlString);
}
+
+export function isEmail(url: string) {
+ const pattern = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
+ return pattern.test(url);
+}
+
+export function serializeUrl(url: string) {
+ if (isLegalLink(url)) {
+ return hasProtocol(url) ? url : isEmail(url) ? `mailto://${url}` : `http://${url}`;
+ }
+
+ return url;
+}
diff --git a/packages/sheets-hyper-link-ui/src/controllers/copy-paste.controller.ts b/packages/sheets-hyper-link-ui/src/controllers/copy-paste.controller.ts
index aa44d58533..d8022e1f9e 100644
--- a/packages/sheets-hyper-link-ui/src/controllers/copy-paste.controller.ts
+++ b/packages/sheets-hyper-link-ui/src/controllers/copy-paste.controller.ts
@@ -22,7 +22,7 @@ import { COPY_TYPE, getRepeatRange, ISheetClipboardService, PREDEFINED_HOOK_NAME
import { Inject, Injector } from '@wendellhu/redi';
import { SPECIAL_PASTE_FORMULA } from '@univerjs/sheets-formula';
import { SHEETS_HYPER_LINK_UI_PLUGIN } from '../types/const';
-import { hasProtocol, isLegalLink } from '../common/util';
+import { isLegalLink, serializeUrl } from '../common/util';
import { SheetsHyperLinkResolverService } from '../services/resolver.service';
@OnLifecycle(LifecycleStages.Ready, SheetsHyperLinkCopyPasteController)
@@ -56,7 +56,7 @@ export class SheetsHyperLinkCopyPasteController extends Disposable {
},
onPastePlainText: (pasteTo: ISheetDiscreteRangeLocation, clipText: string) => {
if (isLegalLink(clipText)) {
- let text = hasProtocol(clipText) ? clipText : `http://${clipText}`;
+ let text = serializeUrl(clipText);
const url = new URL(text);
const name = clipText;
if (
diff --git a/packages/sheets-hyper-link-ui/src/controllers/set-range.controller.ts b/packages/sheets-hyper-link-ui/src/controllers/set-range.controller.ts
index bf0f74d62a..ca4ff51257 100644
--- a/packages/sheets-hyper-link-ui/src/controllers/set-range.controller.ts
+++ b/packages/sheets-hyper-link-ui/src/controllers/set-range.controller.ts
@@ -21,7 +21,7 @@ import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import { ClearSelectionAllCommand, ClearSelectionContentCommand, getSheetCommandTarget, SelectionManagerService, SetRangeValuesCommand, SetRangeValuesMutation, SetRangeValuesUndoMutationFactory, SheetInterceptorService } from '@univerjs/sheets';
import type { IAddHyperLinkCommandParams, IUpdateHyperLinkCommandParams, IUpdateHyperLinkMutationParams } from '@univerjs/sheets-hyper-link';
import { AddHyperLinkCommand, AddHyperLinkMutation, HyperLinkModel, RemoveHyperLinkMutation, UpdateHyperLinkCommand, UpdateHyperLinkMutation } from '@univerjs/sheets-hyper-link';
-import { hasProtocol, isLegalLink } from '../common/util';
+import { isLegalLink, serializeUrl } from '../common/util';
@OnLifecycle(LifecycleStages.Starting, SheetHyperLinkSetRangeController)
export class SheetHyperLinkSetRangeController extends Disposable {
@@ -169,7 +169,7 @@ export class SheetHyperLinkSetRangeController extends Disposable {
row,
column: col,
display: cellValue,
- payload: hasProtocol(cellValue) ? cellValue : `http://${cellValue}`,
+ payload: serializeUrl(cellValue),
},
},
});