@univerjs/docs-hyper-link-ui 1.0.0-alpha.1 → 1.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/index.js +263 -260
- package/lib/es/index.js +263 -261
- package/lib/index.js +263 -261
- package/lib/types/index.d.ts +1 -0
- package/lib/umd/index.js +1 -1
- package/package.json +12 -12
package/lib/es/index.js
CHANGED
|
@@ -1,26 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { UniverDocsHyperLinkPlugin } from "@univerjs/docs-hyper-link";
|
|
3
|
-
import { IRenderManagerService } from "@univerjs/engine-render";
|
|
4
|
-
import { CopyIcon, LinkIcon, UnlinkIcon, WriteIcon } from "@univerjs/icons";
|
|
1
|
+
import { DocCanvasPopManagerService, DocEventManagerService, DocRenderController, EMPTY_PARAGRAPH_MENU_ID, FLOAT_TOOLBAR_MENU_POSITION, INSERT_BELLOW_MENU_ID, whenDocAndEditorFocused } from "@univerjs/docs-ui";
|
|
5
2
|
import { ComponentManager, ContextMenuGroup, ContextMenuPosition, IMenuManagerService, IMessageService, IShortcutService, IconManager, KeyCode, MenuItemType, MetaKeys, RibbonInsertGroup, getMenuHiddenObservable, useDependency, useObservable } from "@univerjs/ui";
|
|
6
|
-
import {
|
|
3
|
+
import { BuildTextUtils, CommandType, CustomRangeType, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DependentOn, Disposable, ICommandService, IConfigService, IUniverInstanceService, Inject, Injector, LocaleService, Plugin, Tools, UniverInstanceType, generateRandomId, getBodySlice, isSafeUrl, merge } from "@univerjs/core";
|
|
7
4
|
import { DOC_INTERCEPTOR_POINT, DocInterceptorService, DocSelectionManagerService, DocSkeletonManagerService, SetTextSelectionsOperation, addCustomRangeBySelectionFactory, deleteCustomRangeFactory, replaceSelectionFactory } from "@univerjs/docs";
|
|
8
|
-
import { useEffect, useState } from "react";
|
|
9
|
-
import { DocCanvasPopManagerService, DocEventManagerService, DocRenderController, EMPTY_PARAGRAPH_MENU_ID, FLOAT_TOOLBAR_MENU_POSITION, INSERT_BELLOW_MENU_ID, whenDocAndEditorFocused } from "@univerjs/docs-ui";
|
|
10
5
|
import { BehaviorSubject, Observable, debounceTime, distinctUntilChanged, pairwise } from "rxjs";
|
|
6
|
+
import { Button, FormLayout, Input, MessageType, Tooltip, borderClassName, clsx } from "@univerjs/design";
|
|
7
|
+
import { useEffect, useState } from "react";
|
|
11
8
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
import { CopyIcon, LinkIcon, UnlinkIcon, WriteIcon } from "@univerjs/icons";
|
|
10
|
+
import { UniverDocsHyperLinkPlugin } from "@univerjs/docs-hyper-link";
|
|
11
|
+
import { IRenderManagerService } from "@univerjs/engine-render";
|
|
12
12
|
|
|
13
|
-
//#region package.json
|
|
14
|
-
var name = "@univerjs/docs-hyper-link-ui";
|
|
15
|
-
var version = "1.0.0-alpha.1";
|
|
16
|
-
|
|
17
|
-
//#endregion
|
|
18
|
-
//#region src/config/config.ts
|
|
19
|
-
const DOCS_HYPER_LINK_UI_PLUGIN_CONFIG_KEY = "docs-hyper-link-ui.config";
|
|
20
|
-
const configSymbol = Symbol(DOCS_HYPER_LINK_UI_PLUGIN_CONFIG_KEY);
|
|
21
|
-
const defaultPluginConfig = {};
|
|
22
|
-
|
|
23
|
-
//#endregion
|
|
24
13
|
//#region src/commands/commands/add-link.command.ts
|
|
25
14
|
const AddDocHyperLinkCommand = {
|
|
26
15
|
type: CommandType.COMMAND,
|
|
@@ -47,7 +36,7 @@ const UpdateDocHyperLinkCommand = {
|
|
|
47
36
|
id: "docs.command.update-hyper-link",
|
|
48
37
|
type: CommandType.COMMAND,
|
|
49
38
|
handler(accessor, params) {
|
|
50
|
-
var
|
|
39
|
+
var _doc$getSelfOrHeaderF, _getBodySlice$textRun;
|
|
51
40
|
if (!params) return false;
|
|
52
41
|
const { unitId, payload, segmentId, linkId } = params;
|
|
53
42
|
const commandService = accessor.get(ICommandService);
|
|
@@ -55,7 +44,9 @@ const UpdateDocHyperLinkCommand = {
|
|
|
55
44
|
const currentSelection = accessor.get(DocSelectionManagerService).getActiveTextRange();
|
|
56
45
|
const doc = univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_DOC);
|
|
57
46
|
if (!currentSelection || !doc) return false;
|
|
58
|
-
const
|
|
47
|
+
const oldBody = (_doc$getSelfOrHeaderF = doc.getSelfOrHeaderFooterModel(segmentId)) === null || _doc$getSelfOrHeaderF === void 0 ? void 0 : _doc$getSelfOrHeaderF.getBody();
|
|
48
|
+
if (!oldBody) return false;
|
|
49
|
+
const textRun = (_getBodySlice$textRun = getBodySlice(oldBody, currentSelection.startOffset, currentSelection.endOffset).textRuns) === null || _getBodySlice$textRun === void 0 ? void 0 : _getBodySlice$textRun[0];
|
|
59
50
|
if (textRun) textRun.ed = params.label.length + 1;
|
|
60
51
|
const replaceSelection = replaceSelectionFactory(accessor, {
|
|
61
52
|
unitId,
|
|
@@ -82,6 +73,142 @@ const UpdateDocHyperLinkCommand = {
|
|
|
82
73
|
}
|
|
83
74
|
};
|
|
84
75
|
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/views/hyper-link-edit/utils.ts
|
|
78
|
+
/**
|
|
79
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
80
|
+
*
|
|
81
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
82
|
+
* you may not use this file except in compliance with the License.
|
|
83
|
+
* You may obtain a copy of the License at
|
|
84
|
+
*
|
|
85
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
86
|
+
*
|
|
87
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
88
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
89
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
90
|
+
* See the License for the specific language governing permissions and
|
|
91
|
+
* limitations under the License.
|
|
92
|
+
*/
|
|
93
|
+
function isBlankInput(value) {
|
|
94
|
+
return value.trim().length === 0;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/views/DocHyperLinkEdit.tsx
|
|
99
|
+
function hasProtocol(urlString) {
|
|
100
|
+
return /^[a-zA-Z]+:\/\//.test(urlString);
|
|
101
|
+
}
|
|
102
|
+
function isEmail(url) {
|
|
103
|
+
return /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(url);
|
|
104
|
+
}
|
|
105
|
+
function transformUrl(urlStr) {
|
|
106
|
+
return hasProtocol(urlStr) ? urlStr : isEmail(urlStr) ? `mailto://${urlStr}` : `https://${urlStr}`;
|
|
107
|
+
}
|
|
108
|
+
const DocHyperLinkEdit = () => {
|
|
109
|
+
const hyperLinkService = useDependency(DocHyperLinkPopupService);
|
|
110
|
+
const localeService = useDependency(LocaleService);
|
|
111
|
+
const editing = useObservable(hyperLinkService.editingLink$);
|
|
112
|
+
const commandService = useDependency(ICommandService);
|
|
113
|
+
const univerInstanceService = useDependency(IUniverInstanceService);
|
|
114
|
+
const docSelectionManagerService = useDependency(DocSelectionManagerService);
|
|
115
|
+
const [link, setLink] = useState("");
|
|
116
|
+
const [label, setLabel] = useState("");
|
|
117
|
+
const [showError, setShowError] = useState(false);
|
|
118
|
+
const isLegal = Tools.isLegalUrl(link);
|
|
119
|
+
const doc = editing ? univerInstanceService.getUnit(editing.unitId, UniverInstanceType.UNIVER_DOC) : univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC);
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
var _doc$getSelfOrHeaderF2, _BuildTextUtils$custo, _body$customRanges2;
|
|
122
|
+
const activeRange = docSelectionManagerService.getActiveTextRange();
|
|
123
|
+
if (!activeRange) return;
|
|
124
|
+
if (editing) {
|
|
125
|
+
var _doc$getSelfOrHeaderF, _body$customRanges;
|
|
126
|
+
const body = doc === null || doc === void 0 || (_doc$getSelfOrHeaderF = doc.getSelfOrHeaderFooterModel(editing.segmentId)) === null || _doc$getSelfOrHeaderF === void 0 ? void 0 : _doc$getSelfOrHeaderF.getBody();
|
|
127
|
+
const matchedRange = body === null || body === void 0 || (_body$customRanges = body.customRanges) === null || _body$customRanges === void 0 ? void 0 : _body$customRanges.find((i) => (editing === null || editing === void 0 ? void 0 : editing.linkId) === i.rangeId && i.startIndex === editing.startIndex && i.endIndex === editing.endIndex);
|
|
128
|
+
if (doc && matchedRange) {
|
|
129
|
+
var _matchedRange$propert, _matchedRange$propert2;
|
|
130
|
+
setLink((_matchedRange$propert = (_matchedRange$propert2 = matchedRange.properties) === null || _matchedRange$propert2 === void 0 ? void 0 : _matchedRange$propert2.url) !== null && _matchedRange$propert !== void 0 ? _matchedRange$propert : "");
|
|
131
|
+
setLabel(BuildTextUtils.transform.getPlainText(getBodySlice(body, matchedRange.startIndex, matchedRange.endIndex + 1).dataStream));
|
|
132
|
+
}
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const body = doc === null || doc === void 0 || (_doc$getSelfOrHeaderF2 = doc.getSelfOrHeaderFooterModel(activeRange.segmentId)) === null || _doc$getSelfOrHeaderF2 === void 0 ? void 0 : _doc$getSelfOrHeaderF2.getBody();
|
|
136
|
+
const selection = body ? activeRange : null;
|
|
137
|
+
const matchedRange = selection && ((_BuildTextUtils$custo = BuildTextUtils.customRange.getCustomRangesInterestsWithSelection(selection, (_body$customRanges2 = body === null || body === void 0 ? void 0 : body.customRanges) !== null && _body$customRanges2 !== void 0 ? _body$customRanges2 : [])) === null || _BuildTextUtils$custo === void 0 ? void 0 : _BuildTextUtils$custo[0]);
|
|
138
|
+
if (doc && matchedRange) {
|
|
139
|
+
var _matchedRange$propert3, _matchedRange$propert4;
|
|
140
|
+
setLink((_matchedRange$propert3 = matchedRange === null || matchedRange === void 0 || (_matchedRange$propert4 = matchedRange.properties) === null || _matchedRange$propert4 === void 0 ? void 0 : _matchedRange$propert4.url) !== null && _matchedRange$propert3 !== void 0 ? _matchedRange$propert3 : "");
|
|
141
|
+
}
|
|
142
|
+
}, [
|
|
143
|
+
doc,
|
|
144
|
+
editing,
|
|
145
|
+
docSelectionManagerService,
|
|
146
|
+
univerInstanceService
|
|
147
|
+
]);
|
|
148
|
+
const handleCancel = () => {
|
|
149
|
+
hyperLinkService.hideEditPopup();
|
|
150
|
+
};
|
|
151
|
+
const handleConfirm = () => {
|
|
152
|
+
setShowError(true);
|
|
153
|
+
if (!isLegal || !doc) return;
|
|
154
|
+
const linkFinal = transformUrl(link);
|
|
155
|
+
if (!editing) commandService.executeCommand(AddDocHyperLinkCommand.id, {
|
|
156
|
+
unitId: doc.getUnitId(),
|
|
157
|
+
payload: linkFinal
|
|
158
|
+
});
|
|
159
|
+
else {
|
|
160
|
+
if (isBlankInput(label)) return;
|
|
161
|
+
commandService.executeCommand(UpdateDocHyperLinkCommand.id, {
|
|
162
|
+
unitId: doc.getUnitId(),
|
|
163
|
+
payload: linkFinal,
|
|
164
|
+
linkId: editing.linkId,
|
|
165
|
+
label,
|
|
166
|
+
segmentId: editing.segmentId
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
hyperLinkService.hideEditPopup();
|
|
170
|
+
};
|
|
171
|
+
if (!doc) return;
|
|
172
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
173
|
+
className: clsx("univer-box-border univer-w-[328px] univer-rounded-xl univer-bg-white univer-px-6 univer-py-5 univer-shadow dark:!univer-bg-gray-900", borderClassName),
|
|
174
|
+
children: [/* @__PURE__ */ jsxs("div", { children: [editing ? /* @__PURE__ */ jsx(FormLayout, {
|
|
175
|
+
label: localeService.t("docs-hyper-link-ui.edit.label"),
|
|
176
|
+
error: showError && isBlankInput(label) ? localeService.t("docs-hyper-link-ui.edit.labelError") : "",
|
|
177
|
+
children: /* @__PURE__ */ jsx(Input, {
|
|
178
|
+
value: label,
|
|
179
|
+
onChange: setLabel,
|
|
180
|
+
autoFocus: true,
|
|
181
|
+
onKeyDown: (evt) => {
|
|
182
|
+
if (evt.keyCode === KeyCode.ENTER) handleConfirm();
|
|
183
|
+
}
|
|
184
|
+
})
|
|
185
|
+
}) : null, /* @__PURE__ */ jsx(FormLayout, {
|
|
186
|
+
label: localeService.t("docs-hyper-link-ui.edit.address"),
|
|
187
|
+
error: showError && !isLegal ? localeService.t("docs-hyper-link-ui.edit.addressError") : "",
|
|
188
|
+
children: /* @__PURE__ */ jsx(Input, {
|
|
189
|
+
value: link,
|
|
190
|
+
onChange: setLink,
|
|
191
|
+
autoFocus: true,
|
|
192
|
+
onKeyDown: (evt) => {
|
|
193
|
+
if (evt.keyCode === KeyCode.ENTER) handleConfirm();
|
|
194
|
+
}
|
|
195
|
+
})
|
|
196
|
+
})] }), /* @__PURE__ */ jsxs("div", {
|
|
197
|
+
className: "univer-flex univer-justify-end univer-gap-3",
|
|
198
|
+
children: [/* @__PURE__ */ jsx(Button, {
|
|
199
|
+
onClick: handleCancel,
|
|
200
|
+
children: localeService.t("docs-hyper-link-ui.edit.cancel")
|
|
201
|
+
}), /* @__PURE__ */ jsx(Button, {
|
|
202
|
+
variant: "primary",
|
|
203
|
+
disabled: isBlankInput(link),
|
|
204
|
+
onClick: handleConfirm,
|
|
205
|
+
children: localeService.t("docs-hyper-link-ui.edit.confirm")
|
|
206
|
+
})]
|
|
207
|
+
})]
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
DocHyperLinkEdit.componentKey = "docs-hyper-link-edit";
|
|
211
|
+
|
|
85
212
|
//#endregion
|
|
86
213
|
//#region src/commands/commands/delete-link.command.ts
|
|
87
214
|
const DeleteDocHyperLinkCommand = {
|
|
@@ -101,65 +228,10 @@ const DeleteDocHyperLinkCommand = {
|
|
|
101
228
|
}
|
|
102
229
|
};
|
|
103
230
|
|
|
104
|
-
//#endregion
|
|
105
|
-
//#region src/commands/operations/popup.operation.ts
|
|
106
|
-
const shouldDisableAddLink = (accessor) => {
|
|
107
|
-
const textSelectionService = accessor.get(DocSelectionManagerService);
|
|
108
|
-
const univerInstanceService = accessor.get(IUniverInstanceService);
|
|
109
|
-
const textRanges = textSelectionService.getTextRanges();
|
|
110
|
-
if (!(textRanges === null || textRanges === void 0 ? void 0 : textRanges.length)) return true;
|
|
111
|
-
const activeRange = textRanges[0];
|
|
112
|
-
if (!univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC) || !activeRange || activeRange.collapsed) return true;
|
|
113
|
-
return false;
|
|
114
|
-
};
|
|
115
|
-
const ShowDocHyperLinkEditPopupOperation = {
|
|
116
|
-
type: CommandType.OPERATION,
|
|
117
|
-
id: "doc.operation.show-hyper-link-edit-popup",
|
|
118
|
-
handler(accessor, params) {
|
|
119
|
-
var _univerInstanceServic;
|
|
120
|
-
const linkInfo = params === null || params === void 0 ? void 0 : params.link;
|
|
121
|
-
const univerInstanceService = accessor.get(IUniverInstanceService);
|
|
122
|
-
if (shouldDisableAddLink(accessor) && !linkInfo) return false;
|
|
123
|
-
const hyperLinkService = accessor.get(DocHyperLinkPopupService);
|
|
124
|
-
const unitId = (linkInfo === null || linkInfo === void 0 ? void 0 : linkInfo.unitId) || ((_univerInstanceServic = univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC)) === null || _univerInstanceServic === void 0 ? void 0 : _univerInstanceServic.getUnitId());
|
|
125
|
-
if (!unitId) return false;
|
|
126
|
-
hyperLinkService.showEditPopup(unitId, linkInfo);
|
|
127
|
-
return true;
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
const ToggleDocHyperLinkInfoPopupOperation = {
|
|
131
|
-
type: CommandType.OPERATION,
|
|
132
|
-
id: "doc.operation.toggle-hyper-link-info-popup",
|
|
133
|
-
handler(accessor, params) {
|
|
134
|
-
const hyperLinkService = accessor.get(DocHyperLinkPopupService);
|
|
135
|
-
if (!params) {
|
|
136
|
-
hyperLinkService.hideInfoPopup();
|
|
137
|
-
return true;
|
|
138
|
-
}
|
|
139
|
-
hyperLinkService.showInfoPopup(params);
|
|
140
|
-
return true;
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
const ClickDocHyperLinkOperation = {
|
|
144
|
-
type: CommandType.OPERATION,
|
|
145
|
-
id: "doc.operation.click-hyper-link",
|
|
146
|
-
handler(accessor, params) {
|
|
147
|
-
var _body$customRanges;
|
|
148
|
-
if (!params) return false;
|
|
149
|
-
const { unitId, linkId, segmentId } = params;
|
|
150
|
-
const doc = accessor.get(IUniverInstanceService).getUnit(unitId, UniverInstanceType.UNIVER_DOC);
|
|
151
|
-
const body = doc === null || doc === void 0 ? void 0 : doc.getSelfOrHeaderFooterModel(segmentId).getBody();
|
|
152
|
-
const link = body === null || body === void 0 || (_body$customRanges = body.customRanges) === null || _body$customRanges === void 0 || (_body$customRanges = _body$customRanges.find((range) => range.rangeId === linkId && range.rangeType === CustomRangeType.HYPERLINK)) === null || _body$customRanges === void 0 || (_body$customRanges = _body$customRanges.properties) === null || _body$customRanges === void 0 ? void 0 : _body$customRanges.url;
|
|
153
|
-
if (!isSafeUrl(link)) return false;
|
|
154
|
-
window.open(link, "_blank", "noopener noreferrer");
|
|
155
|
-
return true;
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
|
|
159
231
|
//#endregion
|
|
160
232
|
//#region src/views/DocLinkPopup.tsx
|
|
161
233
|
const DocLinkPopup = () => {
|
|
162
|
-
var _body$customRanges, _link$properties;
|
|
234
|
+
var _doc$getSelfOrHeaderF, _body$customRanges, _link$properties;
|
|
163
235
|
const hyperLinkService = useDependency(DocHyperLinkPopupService);
|
|
164
236
|
const commandService = useDependency(ICommandService);
|
|
165
237
|
const messageService = useDependency(IMessageService);
|
|
@@ -169,7 +241,7 @@ const DocLinkPopup = () => {
|
|
|
169
241
|
if (!currentPopup) return null;
|
|
170
242
|
const { unitId, linkId, segmentId, startIndex, endIndex } = currentPopup;
|
|
171
243
|
const doc = univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_DOC);
|
|
172
|
-
const body = doc === null || doc === void 0
|
|
244
|
+
const body = doc === null || doc === void 0 || (_doc$getSelfOrHeaderF = doc.getSelfOrHeaderFooterModel(segmentId)) === null || _doc$getSelfOrHeaderF === void 0 ? void 0 : _doc$getSelfOrHeaderF.getBody();
|
|
173
245
|
const link = body === null || body === void 0 || (_body$customRanges = body.customRanges) === null || _body$customRanges === void 0 ? void 0 : _body$customRanges.find((range) => range.rangeId === linkId && range.rangeType === CustomRangeType.HYPERLINK && range.startIndex === startIndex && range.endIndex === endIndex);
|
|
174
246
|
if (!link) return null;
|
|
175
247
|
const url = (_link$properties = link.properties) === null || _link$properties === void 0 ? void 0 : _link$properties.url;
|
|
@@ -405,140 +477,124 @@ DocHyperLinkPopupService = __decorate([
|
|
|
405
477
|
], DocHyperLinkPopupService);
|
|
406
478
|
|
|
407
479
|
//#endregion
|
|
408
|
-
//#region src/
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
480
|
+
//#region src/commands/operations/popup.operation.ts
|
|
481
|
+
const shouldDisableAddLink = (accessor) => {
|
|
482
|
+
const textSelectionService = accessor.get(DocSelectionManagerService);
|
|
483
|
+
const univerInstanceService = accessor.get(IUniverInstanceService);
|
|
484
|
+
const textRanges = textSelectionService.getTextRanges();
|
|
485
|
+
if (!(textRanges === null || textRanges === void 0 ? void 0 : textRanges.length)) return true;
|
|
486
|
+
const activeRange = textRanges[0];
|
|
487
|
+
if (!univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC) || !activeRange || activeRange.collapsed) return true;
|
|
488
|
+
return false;
|
|
489
|
+
};
|
|
490
|
+
const ShowDocHyperLinkEditPopupOperation = {
|
|
491
|
+
type: CommandType.OPERATION,
|
|
492
|
+
id: "doc.operation.show-hyper-link-edit-popup",
|
|
493
|
+
handler(accessor, params) {
|
|
494
|
+
var _univerInstanceServic;
|
|
495
|
+
const linkInfo = params === null || params === void 0 ? void 0 : params.link;
|
|
496
|
+
const univerInstanceService = accessor.get(IUniverInstanceService);
|
|
497
|
+
if (shouldDisableAddLink(accessor) && !linkInfo) return false;
|
|
498
|
+
const hyperLinkService = accessor.get(DocHyperLinkPopupService);
|
|
499
|
+
const unitId = (linkInfo === null || linkInfo === void 0 ? void 0 : linkInfo.unitId) || ((_univerInstanceServic = univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC)) === null || _univerInstanceServic === void 0 ? void 0 : _univerInstanceServic.getUnitId());
|
|
500
|
+
if (!unitId) return false;
|
|
501
|
+
hyperLinkService.showEditPopup(unitId, linkInfo);
|
|
502
|
+
return true;
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
const ToggleDocHyperLinkInfoPopupOperation = {
|
|
506
|
+
type: CommandType.OPERATION,
|
|
507
|
+
id: "doc.operation.toggle-hyper-link-info-popup",
|
|
508
|
+
handler(accessor, params) {
|
|
509
|
+
const hyperLinkService = accessor.get(DocHyperLinkPopupService);
|
|
510
|
+
if (!params) {
|
|
511
|
+
hyperLinkService.hideInfoPopup();
|
|
512
|
+
return true;
|
|
513
|
+
}
|
|
514
|
+
hyperLinkService.showInfoPopup(params);
|
|
515
|
+
return true;
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
const ClickDocHyperLinkOperation = {
|
|
519
|
+
type: CommandType.OPERATION,
|
|
520
|
+
id: "doc.operation.click-hyper-link",
|
|
521
|
+
handler(accessor, params) {
|
|
522
|
+
var _doc$getSelfOrHeaderF, _body$customRanges;
|
|
523
|
+
if (!params) return false;
|
|
524
|
+
const { unitId, linkId, segmentId } = params;
|
|
525
|
+
const doc = accessor.get(IUniverInstanceService).getUnit(unitId, UniverInstanceType.UNIVER_DOC);
|
|
526
|
+
const body = doc === null || doc === void 0 || (_doc$getSelfOrHeaderF = doc.getSelfOrHeaderFooterModel(segmentId)) === null || _doc$getSelfOrHeaderF === void 0 ? void 0 : _doc$getSelfOrHeaderF.getBody();
|
|
527
|
+
const link = body === null || body === void 0 || (_body$customRanges = body.customRanges) === null || _body$customRanges === void 0 || (_body$customRanges = _body$customRanges.find((range) => range.rangeId === linkId && range.rangeType === CustomRangeType.HYPERLINK)) === null || _body$customRanges === void 0 || (_body$customRanges = _body$customRanges.properties) === null || _body$customRanges === void 0 ? void 0 : _body$customRanges.url;
|
|
528
|
+
if (!isSafeUrl(link)) return false;
|
|
529
|
+
window.open(link, "_blank", "noopener noreferrer");
|
|
530
|
+
return true;
|
|
531
|
+
}
|
|
532
|
+
};
|
|
427
533
|
|
|
428
534
|
//#endregion
|
|
429
|
-
//#region src/
|
|
430
|
-
function
|
|
431
|
-
return
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
const
|
|
440
|
-
|
|
441
|
-
const localeService = useDependency(LocaleService);
|
|
442
|
-
const editing = useObservable(hyperLinkService.editingLink$);
|
|
443
|
-
const commandService = useDependency(ICommandService);
|
|
444
|
-
const univerInstanceService = useDependency(IUniverInstanceService);
|
|
445
|
-
const docSelectionManagerService = useDependency(DocSelectionManagerService);
|
|
446
|
-
const [link, setLink] = useState("");
|
|
447
|
-
const [label, setLabel] = useState("");
|
|
448
|
-
const [showError, setShowError] = useState(false);
|
|
449
|
-
const isLegal = Tools.isLegalUrl(link);
|
|
450
|
-
const doc = editing ? univerInstanceService.getUnit(editing.unitId, UniverInstanceType.UNIVER_DOC) : univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC);
|
|
451
|
-
useEffect(() => {
|
|
452
|
-
var _doc$getSelfOrHeaderF2, _BuildTextUtils$custo, _body$customRanges2;
|
|
453
|
-
const activeRange = docSelectionManagerService.getActiveTextRange();
|
|
454
|
-
if (!activeRange) return;
|
|
455
|
-
if (editing) {
|
|
456
|
-
var _doc$getSelfOrHeaderF, _body$customRanges;
|
|
457
|
-
const body = doc === null || doc === void 0 || (_doc$getSelfOrHeaderF = doc.getSelfOrHeaderFooterModel(editing.segmentId)) === null || _doc$getSelfOrHeaderF === void 0 ? void 0 : _doc$getSelfOrHeaderF.getBody();
|
|
458
|
-
const matchedRange = body === null || body === void 0 || (_body$customRanges = body.customRanges) === null || _body$customRanges === void 0 ? void 0 : _body$customRanges.find((i) => (editing === null || editing === void 0 ? void 0 : editing.linkId) === i.rangeId && i.startIndex === editing.startIndex && i.endIndex === editing.endIndex);
|
|
459
|
-
if (doc && matchedRange) {
|
|
460
|
-
var _matchedRange$propert, _matchedRange$propert2;
|
|
461
|
-
setLink((_matchedRange$propert = (_matchedRange$propert2 = matchedRange.properties) === null || _matchedRange$propert2 === void 0 ? void 0 : _matchedRange$propert2.url) !== null && _matchedRange$propert !== void 0 ? _matchedRange$propert : "");
|
|
462
|
-
setLabel(BuildTextUtils.transform.getPlainText(getBodySlice(body, matchedRange.startIndex, matchedRange.endIndex + 1).dataStream));
|
|
463
|
-
}
|
|
464
|
-
return;
|
|
465
|
-
}
|
|
466
|
-
const body = doc === null || doc === void 0 || (_doc$getSelfOrHeaderF2 = doc.getSelfOrHeaderFooterModel(activeRange.segmentId)) === null || _doc$getSelfOrHeaderF2 === void 0 ? void 0 : _doc$getSelfOrHeaderF2.getBody();
|
|
467
|
-
const selection = body ? activeRange : null;
|
|
468
|
-
const matchedRange = selection && ((_BuildTextUtils$custo = BuildTextUtils.customRange.getCustomRangesInterestsWithSelection(selection, (_body$customRanges2 = body === null || body === void 0 ? void 0 : body.customRanges) !== null && _body$customRanges2 !== void 0 ? _body$customRanges2 : [])) === null || _BuildTextUtils$custo === void 0 ? void 0 : _BuildTextUtils$custo[0]);
|
|
469
|
-
if (doc && matchedRange) {
|
|
470
|
-
var _matchedRange$propert3, _matchedRange$propert4;
|
|
471
|
-
setLink((_matchedRange$propert3 = matchedRange === null || matchedRange === void 0 || (_matchedRange$propert4 = matchedRange.properties) === null || _matchedRange$propert4 === void 0 ? void 0 : _matchedRange$propert4.url) !== null && _matchedRange$propert3 !== void 0 ? _matchedRange$propert3 : "");
|
|
472
|
-
}
|
|
473
|
-
}, [
|
|
474
|
-
doc,
|
|
475
|
-
editing,
|
|
476
|
-
docSelectionManagerService,
|
|
477
|
-
univerInstanceService
|
|
478
|
-
]);
|
|
479
|
-
const handleCancel = () => {
|
|
480
|
-
hyperLinkService.hideEditPopup();
|
|
481
|
-
};
|
|
482
|
-
const handleConfirm = () => {
|
|
483
|
-
setShowError(true);
|
|
484
|
-
if (!isLegal || !doc) return;
|
|
485
|
-
const linkFinal = transformUrl(link);
|
|
486
|
-
if (!editing) commandService.executeCommand(AddDocHyperLinkCommand.id, {
|
|
487
|
-
unitId: doc.getUnitId(),
|
|
488
|
-
payload: linkFinal
|
|
489
|
-
});
|
|
490
|
-
else {
|
|
491
|
-
if (isBlankInput(label)) return;
|
|
492
|
-
commandService.executeCommand(UpdateDocHyperLinkCommand.id, {
|
|
493
|
-
unitId: doc.getUnitId(),
|
|
494
|
-
payload: linkFinal,
|
|
495
|
-
linkId: editing.linkId,
|
|
496
|
-
label,
|
|
497
|
-
segmentId: editing.segmentId
|
|
535
|
+
//#region src/menu/menu.ts
|
|
536
|
+
function AddHyperLinkMenuItemFactory(accessor) {
|
|
537
|
+
return {
|
|
538
|
+
id: ShowDocHyperLinkEditPopupOperation.id,
|
|
539
|
+
type: MenuItemType.BUTTON,
|
|
540
|
+
icon: "LinkIcon",
|
|
541
|
+
title: "docs-hyper-link-ui.menu.tooltip",
|
|
542
|
+
tooltip: "docs-hyper-link-ui.menu.tooltip",
|
|
543
|
+
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC),
|
|
544
|
+
disabled$: new Observable(function(subscribe) {
|
|
545
|
+
const observer = accessor.get(DocSelectionManagerService).textSelection$.pipe(debounceTime(16)).subscribe(() => {
|
|
546
|
+
subscribe.next(shouldDisableAddLink(accessor));
|
|
498
547
|
});
|
|
499
|
-
|
|
500
|
-
|
|
548
|
+
return () => {
|
|
549
|
+
observer.unsubscribe();
|
|
550
|
+
};
|
|
551
|
+
})
|
|
501
552
|
};
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
children: /* @__PURE__ */ jsx(Input, {
|
|
509
|
-
value: label,
|
|
510
|
-
onChange: setLabel,
|
|
511
|
-
autoFocus: true,
|
|
512
|
-
onKeyDown: (evt) => {
|
|
513
|
-
if (evt.keyCode === KeyCode.ENTER) handleConfirm();
|
|
514
|
-
}
|
|
515
|
-
})
|
|
516
|
-
}) : null, /* @__PURE__ */ jsx(FormLayout, {
|
|
517
|
-
label: localeService.t("docs-hyper-link-ui.edit.address"),
|
|
518
|
-
error: showError && !isLegal ? localeService.t("docs-hyper-link-ui.edit.addressError") : "",
|
|
519
|
-
children: /* @__PURE__ */ jsx(Input, {
|
|
520
|
-
value: link,
|
|
521
|
-
onChange: setLink,
|
|
522
|
-
autoFocus: true,
|
|
523
|
-
onKeyDown: (evt) => {
|
|
524
|
-
if (evt.keyCode === KeyCode.ENTER) handleConfirm();
|
|
525
|
-
}
|
|
526
|
-
})
|
|
527
|
-
})] }), /* @__PURE__ */ jsxs("div", {
|
|
528
|
-
className: "univer-flex univer-justify-end univer-gap-3",
|
|
529
|
-
children: [/* @__PURE__ */ jsx(Button, {
|
|
530
|
-
onClick: handleCancel,
|
|
531
|
-
children: localeService.t("docs-hyper-link-ui.edit.cancel")
|
|
532
|
-
}), /* @__PURE__ */ jsx(Button, {
|
|
533
|
-
variant: "primary",
|
|
534
|
-
disabled: isBlankInput(link),
|
|
535
|
-
onClick: handleConfirm,
|
|
536
|
-
children: localeService.t("docs-hyper-link-ui.edit.confirm")
|
|
537
|
-
})]
|
|
538
|
-
})]
|
|
539
|
-
});
|
|
553
|
+
}
|
|
554
|
+
const addLinkShortcut = {
|
|
555
|
+
id: ShowDocHyperLinkEditPopupOperation.id,
|
|
556
|
+
binding: MetaKeys.CTRL_COMMAND | KeyCode.K,
|
|
557
|
+
description: "docs-hyper-link-ui.menu.tooltip",
|
|
558
|
+
preconditions: whenDocAndEditorFocused
|
|
540
559
|
};
|
|
541
|
-
|
|
560
|
+
|
|
561
|
+
//#endregion
|
|
562
|
+
//#region src/menu/schema.ts
|
|
563
|
+
const menuSchema = {
|
|
564
|
+
[RibbonInsertGroup.MEDIA]: { [ShowDocHyperLinkEditPopupOperation.id]: {
|
|
565
|
+
order: 1,
|
|
566
|
+
menuItemFactory: AddHyperLinkMenuItemFactory
|
|
567
|
+
} },
|
|
568
|
+
[FLOAT_TOOLBAR_MENU_POSITION]: { [ShowDocHyperLinkEditPopupOperation.id]: {
|
|
569
|
+
order: 20,
|
|
570
|
+
menuItemFactory: AddHyperLinkMenuItemFactory
|
|
571
|
+
} },
|
|
572
|
+
[ContextMenuPosition.MAIN_AREA]: { [ContextMenuGroup.DATA]: { [ShowDocHyperLinkEditPopupOperation.id]: {
|
|
573
|
+
order: 0,
|
|
574
|
+
menuItemFactory: AddHyperLinkMenuItemFactory
|
|
575
|
+
} } },
|
|
576
|
+
[ContextMenuPosition.PARAGRAPH]: {
|
|
577
|
+
[ContextMenuGroup.LAYOUT]: { [INSERT_BELLOW_MENU_ID]: { [ShowDocHyperLinkEditPopupOperation.id]: {
|
|
578
|
+
order: 6,
|
|
579
|
+
menuItemFactory: AddHyperLinkMenuItemFactory
|
|
580
|
+
} } },
|
|
581
|
+
[EMPTY_PARAGRAPH_MENU_ID]: { [ContextMenuGroup.LAYOUT]: { [ShowDocHyperLinkEditPopupOperation.id]: {
|
|
582
|
+
order: 6,
|
|
583
|
+
menuItemFactory: AddHyperLinkMenuItemFactory
|
|
584
|
+
} } }
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
//#endregion
|
|
589
|
+
//#region package.json
|
|
590
|
+
var name = "@univerjs/docs-hyper-link-ui";
|
|
591
|
+
var version = "1.0.0-alpha.3";
|
|
592
|
+
|
|
593
|
+
//#endregion
|
|
594
|
+
//#region src/config/config.ts
|
|
595
|
+
const DOCS_HYPER_LINK_UI_PLUGIN_CONFIG_KEY = "docs-hyper-link-ui.config";
|
|
596
|
+
const configSymbol = Symbol(DOCS_HYPER_LINK_UI_PLUGIN_CONFIG_KEY);
|
|
597
|
+
const defaultPluginConfig = {};
|
|
542
598
|
|
|
543
599
|
//#endregion
|
|
544
600
|
//#region src/controllers/components.controller.ts
|
|
@@ -730,60 +786,6 @@ DocHyperLinkRenderController = __decorate([
|
|
|
730
786
|
__decorateParam(3, Inject(DocRenderController))
|
|
731
787
|
], DocHyperLinkRenderController);
|
|
732
788
|
|
|
733
|
-
//#endregion
|
|
734
|
-
//#region src/menu/menu.ts
|
|
735
|
-
function AddHyperLinkMenuItemFactory(accessor) {
|
|
736
|
-
return {
|
|
737
|
-
id: ShowDocHyperLinkEditPopupOperation.id,
|
|
738
|
-
type: MenuItemType.BUTTON,
|
|
739
|
-
icon: "LinkIcon",
|
|
740
|
-
title: "docs-hyper-link-ui.menu.tooltip",
|
|
741
|
-
tooltip: "docs-hyper-link-ui.menu.tooltip",
|
|
742
|
-
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC),
|
|
743
|
-
disabled$: new Observable(function(subscribe) {
|
|
744
|
-
const observer = accessor.get(DocSelectionManagerService).textSelection$.pipe(debounceTime(16)).subscribe(() => {
|
|
745
|
-
subscribe.next(shouldDisableAddLink(accessor));
|
|
746
|
-
});
|
|
747
|
-
return () => {
|
|
748
|
-
observer.unsubscribe();
|
|
749
|
-
};
|
|
750
|
-
})
|
|
751
|
-
};
|
|
752
|
-
}
|
|
753
|
-
const addLinkShortcut = {
|
|
754
|
-
id: ShowDocHyperLinkEditPopupOperation.id,
|
|
755
|
-
binding: MetaKeys.CTRL_COMMAND | KeyCode.K,
|
|
756
|
-
description: "docs-hyper-link-ui.menu.tooltip",
|
|
757
|
-
preconditions: whenDocAndEditorFocused
|
|
758
|
-
};
|
|
759
|
-
|
|
760
|
-
//#endregion
|
|
761
|
-
//#region src/menu/schema.ts
|
|
762
|
-
const menuSchema = {
|
|
763
|
-
[RibbonInsertGroup.MEDIA]: { [ShowDocHyperLinkEditPopupOperation.id]: {
|
|
764
|
-
order: 1,
|
|
765
|
-
menuItemFactory: AddHyperLinkMenuItemFactory
|
|
766
|
-
} },
|
|
767
|
-
[FLOAT_TOOLBAR_MENU_POSITION]: { [ShowDocHyperLinkEditPopupOperation.id]: {
|
|
768
|
-
order: 20,
|
|
769
|
-
menuItemFactory: AddHyperLinkMenuItemFactory
|
|
770
|
-
} },
|
|
771
|
-
[ContextMenuPosition.MAIN_AREA]: { [ContextMenuGroup.DATA]: { [ShowDocHyperLinkEditPopupOperation.id]: {
|
|
772
|
-
order: 0,
|
|
773
|
-
menuItemFactory: AddHyperLinkMenuItemFactory
|
|
774
|
-
} } },
|
|
775
|
-
[ContextMenuPosition.PARAGRAPH]: {
|
|
776
|
-
[ContextMenuGroup.LAYOUT]: { [INSERT_BELLOW_MENU_ID]: { [ShowDocHyperLinkEditPopupOperation.id]: {
|
|
777
|
-
order: 6,
|
|
778
|
-
menuItemFactory: AddHyperLinkMenuItemFactory
|
|
779
|
-
} } },
|
|
780
|
-
[EMPTY_PARAGRAPH_MENU_ID]: { [ContextMenuGroup.LAYOUT]: { [ShowDocHyperLinkEditPopupOperation.id]: {
|
|
781
|
-
order: 6,
|
|
782
|
-
menuItemFactory: AddHyperLinkMenuItemFactory
|
|
783
|
-
} } }
|
|
784
|
-
}
|
|
785
|
-
};
|
|
786
|
-
|
|
787
789
|
//#endregion
|
|
788
790
|
//#region src/controllers/ui.controller.ts
|
|
789
791
|
let DocHyperLinkUIController = class DocHyperLinkUIController extends Disposable {
|
|
@@ -892,4 +894,4 @@ UniverDocsHyperLinkUIPlugin = __decorate([
|
|
|
892
894
|
], UniverDocsHyperLinkUIPlugin);
|
|
893
895
|
|
|
894
896
|
//#endregion
|
|
895
|
-
export { UniverDocsHyperLinkUIPlugin };
|
|
897
|
+
export { menuSchema as DocsHyperLinkUIMenuSchema, UniverDocsHyperLinkUIPlugin };
|