@univerjs/docs-toc-ui 1.0.0

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.
Files changed (116) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +43 -0
  3. package/lib/cjs/index.js +735 -0
  4. package/lib/cjs/locale/ar-SA.js +32 -0
  5. package/lib/cjs/locale/ca-ES.js +32 -0
  6. package/lib/cjs/locale/de-DE.js +32 -0
  7. package/lib/cjs/locale/en-US.js +47 -0
  8. package/lib/cjs/locale/es-ES.js +32 -0
  9. package/lib/cjs/locale/fa-IR.js +32 -0
  10. package/lib/cjs/locale/fr-FR.js +32 -0
  11. package/lib/cjs/locale/id-ID.js +32 -0
  12. package/lib/cjs/locale/it-IT.js +32 -0
  13. package/lib/cjs/locale/ja-JP.js +32 -0
  14. package/lib/cjs/locale/ko-KR.js +32 -0
  15. package/lib/cjs/locale/pl-PL.js +32 -0
  16. package/lib/cjs/locale/pt-BR.js +32 -0
  17. package/lib/cjs/locale/ru-RU.js +32 -0
  18. package/lib/cjs/locale/sk-SK.js +32 -0
  19. package/lib/cjs/locale/vi-VN.js +32 -0
  20. package/lib/cjs/locale/zh-CN.js +32 -0
  21. package/lib/cjs/locale/zh-HK.js +32 -0
  22. package/lib/cjs/locale/zh-TW.js +32 -0
  23. package/lib/es/index.js +728 -0
  24. package/lib/es/locale/ar-SA.js +32 -0
  25. package/lib/es/locale/ca-ES.js +32 -0
  26. package/lib/es/locale/de-DE.js +32 -0
  27. package/lib/es/locale/en-US.js +47 -0
  28. package/lib/es/locale/es-ES.js +32 -0
  29. package/lib/es/locale/fa-IR.js +32 -0
  30. package/lib/es/locale/fr-FR.js +32 -0
  31. package/lib/es/locale/id-ID.js +32 -0
  32. package/lib/es/locale/it-IT.js +32 -0
  33. package/lib/es/locale/ja-JP.js +32 -0
  34. package/lib/es/locale/ko-KR.js +32 -0
  35. package/lib/es/locale/pl-PL.js +32 -0
  36. package/lib/es/locale/pt-BR.js +32 -0
  37. package/lib/es/locale/ru-RU.js +32 -0
  38. package/lib/es/locale/sk-SK.js +32 -0
  39. package/lib/es/locale/vi-VN.js +32 -0
  40. package/lib/es/locale/zh-CN.js +32 -0
  41. package/lib/es/locale/zh-HK.js +32 -0
  42. package/lib/es/locale/zh-TW.js +32 -0
  43. package/lib/index.css +156 -0
  44. package/lib/index.js +728 -0
  45. package/lib/locale/ar-SA.js +32 -0
  46. package/lib/locale/ca-ES.js +32 -0
  47. package/lib/locale/de-DE.js +32 -0
  48. package/lib/locale/en-US.js +47 -0
  49. package/lib/locale/es-ES.js +32 -0
  50. package/lib/locale/fa-IR.js +32 -0
  51. package/lib/locale/fr-FR.js +32 -0
  52. package/lib/locale/id-ID.js +32 -0
  53. package/lib/locale/it-IT.js +32 -0
  54. package/lib/locale/ja-JP.js +32 -0
  55. package/lib/locale/ko-KR.js +32 -0
  56. package/lib/locale/pl-PL.js +32 -0
  57. package/lib/locale/pt-BR.js +32 -0
  58. package/lib/locale/ru-RU.js +32 -0
  59. package/lib/locale/sk-SK.js +32 -0
  60. package/lib/locale/vi-VN.js +32 -0
  61. package/lib/locale/zh-CN.js +32 -0
  62. package/lib/locale/zh-HK.js +32 -0
  63. package/lib/locale/zh-TW.js +32 -0
  64. package/lib/types/commands/table-of-contents-dialog.operation.d.ts +21 -0
  65. package/lib/types/config/config.d.ts +22 -0
  66. package/lib/types/controllers/components.controller.d.ts +20 -0
  67. package/lib/types/controllers/table-of-contents-highlight.render-controller.d.ts +33 -0
  68. package/lib/types/controllers/table-of-contents-ribbon.controller.d.ts +27 -0
  69. package/lib/types/controllers/ui.controller.d.ts +20 -0
  70. package/lib/types/index.d.ts +18 -0
  71. package/lib/types/locale/ar-SA.d.ts +18 -0
  72. package/lib/types/locale/ca-ES.d.ts +18 -0
  73. package/lib/types/locale/de-DE.d.ts +18 -0
  74. package/lib/types/locale/en-US.d.ts +48 -0
  75. package/lib/types/locale/es-ES.d.ts +18 -0
  76. package/lib/types/locale/fa-IR.d.ts +18 -0
  77. package/lib/types/locale/fr-FR.d.ts +18 -0
  78. package/lib/types/locale/id-ID.d.ts +18 -0
  79. package/lib/types/locale/it-IT.d.ts +18 -0
  80. package/lib/types/locale/ja-JP.d.ts +18 -0
  81. package/lib/types/locale/ko-KR.d.ts +18 -0
  82. package/lib/types/locale/pl-PL.d.ts +18 -0
  83. package/lib/types/locale/pt-BR.d.ts +18 -0
  84. package/lib/types/locale/ru-RU.d.ts +18 -0
  85. package/lib/types/locale/sk-SK.d.ts +18 -0
  86. package/lib/types/locale/types.d.ts +18 -0
  87. package/lib/types/locale/vi-VN.d.ts +18 -0
  88. package/lib/types/locale/zh-CN.d.ts +18 -0
  89. package/lib/types/locale/zh-HK.d.ts +18 -0
  90. package/lib/types/locale/zh-TW.d.ts +18 -0
  91. package/lib/types/menu/menu.d.ts +22 -0
  92. package/lib/types/menu/schema.d.ts +19 -0
  93. package/lib/types/plugin.d.ts +32 -0
  94. package/lib/types/views/InsertTableOfContentsDialog.d.ts +29 -0
  95. package/lib/types/views/TableOfContentsDialog.d.ts +21 -0
  96. package/lib/umd/index.js +4 -0
  97. package/lib/umd/locale/ar-SA.js +1 -0
  98. package/lib/umd/locale/ca-ES.js +1 -0
  99. package/lib/umd/locale/de-DE.js +1 -0
  100. package/lib/umd/locale/en-US.js +1 -0
  101. package/lib/umd/locale/es-ES.js +1 -0
  102. package/lib/umd/locale/fa-IR.js +1 -0
  103. package/lib/umd/locale/fr-FR.js +1 -0
  104. package/lib/umd/locale/id-ID.js +1 -0
  105. package/lib/umd/locale/it-IT.js +1 -0
  106. package/lib/umd/locale/ja-JP.js +1 -0
  107. package/lib/umd/locale/ko-KR.js +1 -0
  108. package/lib/umd/locale/pl-PL.js +1 -0
  109. package/lib/umd/locale/pt-BR.js +1 -0
  110. package/lib/umd/locale/ru-RU.js +1 -0
  111. package/lib/umd/locale/sk-SK.js +1 -0
  112. package/lib/umd/locale/vi-VN.js +1 -0
  113. package/lib/umd/locale/zh-CN.js +1 -0
  114. package/lib/umd/locale/zh-HK.js +1 -0
  115. package/lib/umd/locale/zh-TW.js +1 -0
  116. package/package.json +90 -0
@@ -0,0 +1,728 @@
1
+ import { DeleteTableOfContentsCommand, InsertTableOfContentsCommand, UniverDocsTocPlugin, UpdateTableOfContentsCommand, countTableOfContentsHeadings, findTableOfContentsAtOffset } from "@univerjs/docs-toc";
2
+ import { ComponentManager, ContextMenuGroup, ContextMenuPosition, IMenuManagerService, IMessageService, IRibbonService, MenuItemType, MenuManagerPosition, RibbonInsertGroup, getMenuHiddenObservable, useDependency } from "@univerjs/ui";
3
+ import { BooleanNumber, ColorKit, CommandType, DependentOn, Disposable, ICommandService, IConfigService, IConfirmService, IUniverInstanceService, Inject, Injector, LocaleService, Plugin, RxDisposable, ThemeService, UniverInstanceType, generateRandomId, merge } from "@univerjs/core";
4
+ import { MessageType } from "@univerjs/design";
5
+ import { DocSelectionManagerService, DocSkeletonManagerService, DocStateChangeManagerService, UniverDocsPlugin } from "@univerjs/docs";
6
+ import { useState } from "react";
7
+ import { jsx, jsxs } from "react/jsx-runtime";
8
+ import { NodePositionConvertToCursor, TEXT_RANGE_LAYER_INDEX, UniverDocsUIPlugin, disableMenuWithoutDocumentUnitPermission } from "@univerjs/docs-ui";
9
+ import { UnitAction } from "@univerjs/protocol";
10
+ import { Observable, combineLatest, map, takeUntil } from "rxjs";
11
+ import { IRenderManagerService, Rect, RegularPolygon, UniverRenderEnginePlugin } from "@univerjs/engine-render";
12
+
13
+ //#region src/views/InsertTableOfContentsDialog.tsx
14
+ const INSERT_TABLE_OF_CONTENTS_DIALOG_COMPONENT = "doc.insert-table-of-contents-dialog";
15
+ const FORMAT_OPTIONS = [
16
+ ["fromTemplate", "docs-toc-ui.tableOfContents.formatFromTemplate"],
17
+ ["classic", "docs-toc-ui.tableOfContents.formatClassic"],
18
+ ["modern", "docs-toc-ui.tableOfContents.formatModern"],
19
+ ["simple", "docs-toc-ui.tableOfContents.formatSimple"]
20
+ ];
21
+ const LEADER_OPTIONS = [
22
+ ["none", "docs-toc-ui.tableOfContents.leaderNone"],
23
+ ["dots", "docs-toc-ui.tableOfContents.leaderDots"],
24
+ ["dashes", "docs-toc-ui.tableOfContents.leaderDashes"],
25
+ ["underline", "docs-toc-ui.tableOfContents.leaderUnderline"]
26
+ ];
27
+ function InsertTableOfContentsDialog(props) {
28
+ const localeService = useDependency(LocaleService);
29
+ const [value, setValue] = useState(props.options);
30
+ const update = (patch) => {
31
+ const next = {
32
+ ...value,
33
+ ...patch
34
+ };
35
+ setValue(next);
36
+ props.onChange(next);
37
+ };
38
+ const leader = value.tabLeader === "dots" ? "··········" : value.tabLeader === "dashes" ? "----------" : value.tabLeader === "underline" ? "__________" : "";
39
+ return /* @__PURE__ */ jsxs("div", {
40
+ className: "univer-grid univer-gap-4 univer-py-2",
41
+ "data-testid": "table-of-contents-insert-dialog",
42
+ children: [
43
+ /* @__PURE__ */ jsxs("div", {
44
+ className: "univer-grid univer-grid-cols-2 univer-gap-3",
45
+ children: [/* @__PURE__ */ jsxs("label", {
46
+ className: "univer-grid univer-gap-1 univer-text-sm",
47
+ children: [/* @__PURE__ */ jsx("span", { children: localeService.t("docs-toc-ui.tableOfContents.levels") }), /* @__PURE__ */ jsx("select", {
48
+ className: "univer-h-8 univer-rounded-md univer-border univer-bg-transparent univer-px-2",
49
+ value: value.levels,
50
+ onChange: (event) => update({ levels: Number(event.target.value) }),
51
+ children: Array.from({ length: 9 }, (_, index) => index + 1).map((level) => /* @__PURE__ */ jsx("option", {
52
+ value: level,
53
+ children: level
54
+ }, level))
55
+ })]
56
+ }), /* @__PURE__ */ jsxs("label", {
57
+ className: "univer-grid univer-gap-1 univer-text-sm",
58
+ children: [/* @__PURE__ */ jsx("span", { children: localeService.t("docs-toc-ui.tableOfContents.format") }), /* @__PURE__ */ jsx("select", {
59
+ className: "univer-h-8 univer-rounded-md univer-border univer-bg-transparent univer-px-2",
60
+ value: value.format,
61
+ onChange: (event) => update({ format: event.target.value }),
62
+ children: FORMAT_OPTIONS.map(([format, label]) => /* @__PURE__ */ jsx("option", {
63
+ value: format,
64
+ children: localeService.t(label)
65
+ }, format))
66
+ })]
67
+ })]
68
+ }),
69
+ /* @__PURE__ */ jsxs("div", {
70
+ className: "univer-grid univer-gap-2 univer-text-sm",
71
+ children: [
72
+ /* @__PURE__ */ jsxs("label", {
73
+ className: "univer-flex univer-items-center univer-gap-2",
74
+ children: [/* @__PURE__ */ jsx("input", {
75
+ type: "checkbox",
76
+ checked: value.showPageNumbers,
77
+ onChange: (event) => update({
78
+ showPageNumbers: event.target.checked,
79
+ rightAlignPageNumbers: event.target.checked && value.rightAlignPageNumbers
80
+ })
81
+ }), /* @__PURE__ */ jsx("span", { children: localeService.t("docs-toc-ui.tableOfContents.showPageNumbers") })]
82
+ }),
83
+ /* @__PURE__ */ jsxs("label", {
84
+ className: "univer-flex univer-items-center univer-gap-2",
85
+ children: [/* @__PURE__ */ jsx("input", {
86
+ type: "checkbox",
87
+ checked: value.rightAlignPageNumbers,
88
+ disabled: !value.showPageNumbers,
89
+ onChange: (event) => update({ rightAlignPageNumbers: event.target.checked })
90
+ }), /* @__PURE__ */ jsx("span", { children: localeService.t("docs-toc-ui.tableOfContents.rightAlignPageNumbers") })]
91
+ }),
92
+ /* @__PURE__ */ jsxs("label", {
93
+ className: "univer-flex univer-items-center univer-justify-between univer-gap-3",
94
+ children: [/* @__PURE__ */ jsx("span", { children: localeService.t("docs-toc-ui.tableOfContents.tabLeader") }), /* @__PURE__ */ jsx("select", {
95
+ className: "univer-h-8 univer-min-w-40 univer-rounded-md univer-border univer-bg-transparent univer-px-2",
96
+ value: value.tabLeader,
97
+ disabled: !value.showPageNumbers || !value.rightAlignPageNumbers,
98
+ onChange: (event) => update({ tabLeader: event.target.value }),
99
+ children: LEADER_OPTIONS.map(([tabLeader, label]) => /* @__PURE__ */ jsx("option", {
100
+ value: tabLeader,
101
+ children: localeService.t(label)
102
+ }, tabLeader))
103
+ })]
104
+ })
105
+ ]
106
+ }),
107
+ /* @__PURE__ */ jsxs("section", {
108
+ className: "univer-rounded-lg univer-border univer-bg-gray-50 univer-p-4 dark:univer-bg-gray-900",
109
+ children: [/* @__PURE__ */ jsx("div", {
110
+ className: "univer-mb-3 univer-text-xs univer-font-medium univer-text-gray-500",
111
+ children: localeService.t("docs-toc-ui.tableOfContents.preview")
112
+ }), Array.from({ length: Math.min(value.levels, 3) }, (_, index) => index).map((level) => /* @__PURE__ */ jsxs("div", {
113
+ className: `
114
+ univer-flex univer-items-baseline univer-text-sm
115
+ ${value.format === "modern" ? "univer-mb-2" : "univer-mb-1"}
116
+ `,
117
+ style: { marginInlineStart: `${level * (value.format === "simple" ? 12 : value.format === "classic" ? 14 : value.format === "modern" ? 20 : 18)}px` },
118
+ children: [
119
+ /* @__PURE__ */ jsxs("span", { children: [
120
+ localeService.t("docs-toc-ui.tableOfContents.previewHeading"),
121
+ " ",
122
+ level + 1
123
+ ] }),
124
+ value.showPageNumbers && value.rightAlignPageNumbers && /* @__PURE__ */ jsx("span", {
125
+ className: "univer-mx-2 univer-flex-1 univer-overflow-hidden univer-whitespace-nowrap univer-text-gray-400",
126
+ children: leader
127
+ }),
128
+ value.showPageNumbers && /* @__PURE__ */ jsx("span", {
129
+ className: value.rightAlignPageNumbers ? "" : "univer-ml-2",
130
+ children: level + 1
131
+ })
132
+ ]
133
+ }, level))]
134
+ })
135
+ ]
136
+ });
137
+ }
138
+
139
+ //#endregion
140
+ //#region src/views/TableOfContentsDialog.tsx
141
+ const TABLE_OF_CONTENTS_DIALOG_COMPONENT = "doc.table-of-contents-dialog";
142
+ function TableOfContentsDialog(props) {
143
+ const localeService = useDependency(LocaleService);
144
+ const [mode, setMode] = useState(props.mode);
145
+ return /* @__PURE__ */ jsxs("div", {
146
+ className: "univer-grid univer-gap-4 univer-py-2",
147
+ "data-testid": "table-of-contents-update-dialog",
148
+ children: [/* @__PURE__ */ jsx("p", {
149
+ className: "univer-m-0 univer-text-sm",
150
+ children: localeService.t("docs-toc-ui.tableOfContents.updateHint")
151
+ }), /* @__PURE__ */ jsx("fieldset", {
152
+ className: "univer-grid univer-gap-3 univer-border-0 univer-p-0",
153
+ children: [["pageNumbersOnly", "docs-toc-ui.tableOfContents.updatePageNumbersOnly"], ["entireTable", "docs-toc-ui.tableOfContents.updateEntireTable"]].map(([value, label]) => /* @__PURE__ */ jsxs("label", {
154
+ className: "univer-flex univer-gap-3 univer-rounded-lg univer-border univer-p-3",
155
+ children: [/* @__PURE__ */ jsx("input", {
156
+ type: "radio",
157
+ name: "toc-update-mode",
158
+ value,
159
+ checked: mode === value,
160
+ onChange: () => {
161
+ setMode(value);
162
+ props.onModeChange(value);
163
+ }
164
+ }), /* @__PURE__ */ jsx("span", {
165
+ className: "univer-text-sm",
166
+ children: localeService.t(label)
167
+ })]
168
+ }, value))
169
+ })]
170
+ });
171
+ }
172
+
173
+ //#endregion
174
+ //#region src/commands/table-of-contents-dialog.operation.ts
175
+ const TABLE_OF_CONTENTS_DIALOG_ID = "doc.dialog.update-table-of-contents";
176
+ const INSERT_TABLE_OF_CONTENTS_DIALOG_ID = "doc.dialog.insert-table-of-contents";
177
+ const DEFAULT_INSERT_VALUE = {
178
+ levels: 3,
179
+ showPageNumbers: true,
180
+ rightAlignPageNumbers: true,
181
+ tabLeader: "dots",
182
+ format: "fromTemplate"
183
+ };
184
+ function showNoHeadingsMessage(accessor) {
185
+ accessor.get(IMessageService).show({
186
+ type: MessageType.Warning,
187
+ content: accessor.get(LocaleService).t("docs-toc-ui.tableOfContents.noHeadings")
188
+ });
189
+ }
190
+ async function insertTableOfContents(accessor, value, title) {
191
+ const doc = accessor.get(IUniverInstanceService).getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC);
192
+ if (!doc || countTableOfContentsHeadings(doc.getBody(), value.levels) === 0) {
193
+ showNoHeadingsMessage(accessor);
194
+ return false;
195
+ }
196
+ return Boolean(await accessor.get(ICommandService).executeCommand(InsertTableOfContentsCommand.id, {
197
+ unitId: doc.getUnitId(),
198
+ ...value,
199
+ title
200
+ }));
201
+ }
202
+ const InsertTableOfContentsOperation = {
203
+ id: "doc.operation.insert-table-of-contents",
204
+ type: CommandType.OPERATION,
205
+ async handler(accessor, params) {
206
+ if ((params === null || params === void 0 ? void 0 : params.mode) === "automatic") {
207
+ const title = accessor.get(LocaleService).t("docs-toc-ui.tableOfContents.contentsTitle");
208
+ return insertTableOfContents(accessor, DEFAULT_INSERT_VALUE, title);
209
+ }
210
+ if ((params === null || params === void 0 ? void 0 : params.mode) !== "custom") return false;
211
+ if (!accessor.get(IUniverInstanceService).getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC)) return false;
212
+ const confirmService = accessor.get(IConfirmService);
213
+ const localeService = accessor.get(LocaleService);
214
+ const state = { options: { ...DEFAULT_INSERT_VALUE } };
215
+ await confirmService.open({
216
+ id: INSERT_TABLE_OF_CONTENTS_DIALOG_ID,
217
+ width: 520,
218
+ title: { title: localeService.t("docs-toc-ui.tableOfContents.customTitle") },
219
+ children: { label: {
220
+ name: INSERT_TABLE_OF_CONTENTS_DIALOG_COMPONENT,
221
+ props: {
222
+ options: state.options,
223
+ onChange: (value) => {
224
+ state.options = value;
225
+ }
226
+ }
227
+ } },
228
+ onConfirm: async () => {
229
+ if (await insertTableOfContents(accessor, state.options)) confirmService.close(INSERT_TABLE_OF_CONTENTS_DIALOG_ID);
230
+ },
231
+ onClose: () => confirmService.close(INSERT_TABLE_OF_CONTENTS_DIALOG_ID)
232
+ });
233
+ return true;
234
+ }
235
+ };
236
+ const OpenTableOfContentsDialogOperation = {
237
+ id: "doc.operation.open-table-of-contents-dialog",
238
+ type: CommandType.OPERATION,
239
+ async handler(accessor) {
240
+ var _toc$properties;
241
+ const doc = accessor.get(IUniverInstanceService).getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC);
242
+ const selection = accessor.get(DocSelectionManagerService).getActiveTextRange();
243
+ const toc = findTableOfContentsAtOffset(doc === null || doc === void 0 ? void 0 : doc.getBody(), selection === null || selection === void 0 ? void 0 : selection.startOffset);
244
+ if (!doc || !toc || ((_toc$properties = toc.properties) === null || _toc$properties === void 0 ? void 0 : _toc$properties.locked) === BooleanNumber.TRUE) return false;
245
+ const confirmService = accessor.get(IConfirmService);
246
+ const commandService = accessor.get(ICommandService);
247
+ const localeService = accessor.get(LocaleService);
248
+ const state = { mode: "pageNumbersOnly" };
249
+ await confirmService.open({
250
+ id: TABLE_OF_CONTENTS_DIALOG_ID,
251
+ width: 420,
252
+ title: { title: localeService.t("docs-toc-ui.tableOfContents.updateTitle") },
253
+ children: { label: {
254
+ name: TABLE_OF_CONTENTS_DIALOG_COMPONENT,
255
+ props: {
256
+ mode: state.mode,
257
+ onModeChange: (mode) => {
258
+ state.mode = mode;
259
+ }
260
+ }
261
+ } },
262
+ onConfirm: async () => {
263
+ try {
264
+ await commandService.executeCommand(UpdateTableOfContentsCommand.id, {
265
+ unitId: doc.getUnitId(),
266
+ rangeId: toc.rangeId,
267
+ mode: state.mode
268
+ });
269
+ } finally {
270
+ confirmService.close(TABLE_OF_CONTENTS_DIALOG_ID);
271
+ }
272
+ },
273
+ onClose: () => confirmService.close(TABLE_OF_CONTENTS_DIALOG_ID)
274
+ });
275
+ return true;
276
+ }
277
+ };
278
+
279
+ //#endregion
280
+ //#region src/menu/menu.ts
281
+ const INSERT_TABLE_OF_CONTENTS_MENU_ID = "doc.menu.insert-table-of-contents";
282
+ function getActiveTableOfContentsObservable(accessor) {
283
+ const selectionManager = accessor.get(DocSelectionManagerService);
284
+ const instanceService = accessor.get(IUniverInstanceService);
285
+ return new Observable((subscriber) => {
286
+ const emit = () => {
287
+ const doc = instanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC);
288
+ const selection = selectionManager.getActiveTextRange();
289
+ subscriber.next((selection === null || selection === void 0 ? void 0 : selection.segmentId) ? void 0 : findTableOfContentsAtOffset(doc === null || doc === void 0 ? void 0 : doc.getBody(), selection === null || selection === void 0 ? void 0 : selection.startOffset));
290
+ };
291
+ emit();
292
+ const selectionSubscription = selectionManager.textSelection$.subscribe(emit);
293
+ const unitSubscription = instanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_DOC).subscribe(emit);
294
+ const documentSubscription = accessor.get(DocStateChangeManagerService).docStateChange$.subscribe(emit);
295
+ return () => {
296
+ selectionSubscription.unsubscribe();
297
+ unitSubscription.unsubscribe();
298
+ documentSubscription.unsubscribe();
299
+ };
300
+ });
301
+ }
302
+ function getInsertTableOfContentsDisabledObservable(accessor) {
303
+ const selectionManager = accessor.get(DocSelectionManagerService);
304
+ const instanceService = accessor.get(IUniverInstanceService);
305
+ return new Observable((subscriber) => {
306
+ const emit = () => {
307
+ const doc = instanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC);
308
+ const selection = selectionManager.getActiveTextRange();
309
+ subscriber.next(!selection || selection.startOffset !== selection.endOffset || Boolean(selection.segmentId) || Boolean(findTableOfContentsAtOffset(doc === null || doc === void 0 ? void 0 : doc.getBody(), selection.startOffset)));
310
+ };
311
+ emit();
312
+ const selectionSubscription = selectionManager.textSelection$.subscribe(emit);
313
+ const unitSubscription = instanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_DOC).subscribe(emit);
314
+ return () => {
315
+ selectionSubscription.unsubscribe();
316
+ unitSubscription.unsubscribe();
317
+ };
318
+ });
319
+ }
320
+ function InsertTableOfContentsMenuFactory(accessor) {
321
+ const permissionDisabled$ = disableMenuWithoutDocumentUnitPermission(accessor, UnitAction.Edit);
322
+ return {
323
+ id: INSERT_TABLE_OF_CONTENTS_MENU_ID,
324
+ type: MenuItemType.SUBITEMS,
325
+ icon: "UnorderIcon",
326
+ title: "docs-toc-ui.tableOfContents.insertTitle",
327
+ selectionsCommandId: InsertTableOfContentsOperation.id,
328
+ selections: [{
329
+ value: "automatic",
330
+ label: "docs-toc-ui.tableOfContents.automaticTitle",
331
+ icon: "UnorderIcon",
332
+ params: { mode: "automatic" }
333
+ }, {
334
+ value: "custom",
335
+ label: "docs-toc-ui.tableOfContents.customTitle",
336
+ icon: "DocSettingIcon",
337
+ params: { mode: "custom" }
338
+ }],
339
+ disabled$: combineLatest([permissionDisabled$, getInsertTableOfContentsDisabledObservable(accessor)]).pipe(map(([permissionDisabled, unavailable]) => permissionDisabled || unavailable)),
340
+ hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC)
341
+ };
342
+ }
343
+ function UpdateTableOfContentsMenuFactory(accessor) {
344
+ return {
345
+ id: OpenTableOfContentsDialogOperation.id,
346
+ commandId: OpenTableOfContentsDialogOperation.id,
347
+ type: MenuItemType.BUTTON,
348
+ icon: "UnorderIcon",
349
+ title: "docs-toc-ui.tableOfContents.updateTitle",
350
+ disabled$: combineLatest([disableMenuWithoutDocumentUnitPermission(accessor, UnitAction.Edit), getActiveTableOfContentsObservable(accessor)]).pipe(map(([permissionDisabled, toc]) => {
351
+ var _toc$properties;
352
+ return permissionDisabled || !toc || ((_toc$properties = toc.properties) === null || _toc$properties === void 0 ? void 0 : _toc$properties.locked) === BooleanNumber.TRUE;
353
+ })),
354
+ hidden$: combineLatest([getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC), getActiveTableOfContentsObservable(accessor)]).pipe(map(([hidden, toc]) => hidden || !toc))
355
+ };
356
+ }
357
+ function DeleteTableOfContentsMenuFactory(accessor) {
358
+ return {
359
+ id: DeleteTableOfContentsCommand.id,
360
+ commandId: DeleteTableOfContentsCommand.id,
361
+ type: MenuItemType.BUTTON,
362
+ icon: "DeleteIcon",
363
+ title: "docs-toc-ui.tableOfContents.removeTitle",
364
+ disabled$: disableMenuWithoutDocumentUnitPermission(accessor, UnitAction.Edit),
365
+ hidden$: combineLatest([getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC), getActiveTableOfContentsObservable(accessor)]).pipe(map(([hidden, toc]) => hidden || !toc))
366
+ };
367
+ }
368
+
369
+ //#endregion
370
+ //#region src/menu/schema.ts
371
+ const DOC_TABLE_OF_CONTENTS_RIBBON_TAB = "ribbon.table-of-contents";
372
+ const DOC_TABLE_OF_CONTENTS_RIBBON_GROUP = "ribbon.table-of-contents.manage";
373
+ const tableOfContentsRibbonMenuSchema = { [MenuManagerPosition.RIBBON]: { [DOC_TABLE_OF_CONTENTS_RIBBON_TAB]: {
374
+ order: 100,
375
+ title: "docs-toc-ui.tableOfContents.title",
376
+ contextual: true,
377
+ [DOC_TABLE_OF_CONTENTS_RIBBON_GROUP]: {
378
+ order: 0,
379
+ [OpenTableOfContentsDialogOperation.id]: {
380
+ order: 0,
381
+ gridLayout: {
382
+ row: 1,
383
+ column: 1,
384
+ rowSpan: 2,
385
+ showLabel: true
386
+ },
387
+ menuItemFactory: UpdateTableOfContentsMenuFactory
388
+ },
389
+ [DeleteTableOfContentsCommand.id]: {
390
+ order: 1,
391
+ gridLayout: {
392
+ row: 1,
393
+ column: 2,
394
+ rowSpan: 2,
395
+ showLabel: true
396
+ },
397
+ menuItemFactory: DeleteTableOfContentsMenuFactory
398
+ }
399
+ }
400
+ } } };
401
+ const DocsTocUIMenuSchema = {
402
+ [RibbonInsertGroup.CELL]: { [INSERT_TABLE_OF_CONTENTS_MENU_ID]: {
403
+ order: 7,
404
+ gridLayout: {
405
+ row: 1,
406
+ column: 1,
407
+ showLabel: true
408
+ },
409
+ menuItemFactory: InsertTableOfContentsMenuFactory
410
+ } },
411
+ [ContextMenuPosition.MAIN_AREA]: { [ContextMenuGroup.LAYOUT]: {
412
+ [OpenTableOfContentsDialogOperation.id]: {
413
+ order: -1,
414
+ menuItemFactory: UpdateTableOfContentsMenuFactory
415
+ },
416
+ [DeleteTableOfContentsCommand.id]: {
417
+ order: -.5,
418
+ menuItemFactory: DeleteTableOfContentsMenuFactory
419
+ }
420
+ } }
421
+ };
422
+
423
+ //#endregion
424
+ //#region package.json
425
+ var name = "@univerjs/docs-toc-ui";
426
+ var version = "1.0.0";
427
+
428
+ //#endregion
429
+ //#region src/config/config.ts
430
+ const DOCS_TOC_UI_PLUGIN_CONFIG_KEY = "docs-toc-ui.config";
431
+ const configSymbol = Symbol(DOCS_TOC_UI_PLUGIN_CONFIG_KEY);
432
+ const defaultPluginConfig = {};
433
+
434
+ //#endregion
435
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorateParam.js
436
+ function __decorateParam(paramIndex, decorator) {
437
+ return function(target, key) {
438
+ decorator(target, key, paramIndex);
439
+ };
440
+ }
441
+
442
+ //#endregion
443
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorate.js
444
+ function __decorate(decorators, target, key, desc) {
445
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
446
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
447
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
448
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
449
+ }
450
+
451
+ //#endregion
452
+ //#region src/controllers/components.controller.ts
453
+ /**
454
+ * Copyright 2023-present DreamNum Co., Ltd.
455
+ *
456
+ * Licensed under the Apache License, Version 2.0 (the "License");
457
+ * you may not use this file except in compliance with the License.
458
+ * You may obtain a copy of the License at
459
+ *
460
+ * http://www.apache.org/licenses/LICENSE-2.0
461
+ *
462
+ * Unless required by applicable law or agreed to in writing, software
463
+ * distributed under the License is distributed on an "AS IS" BASIS,
464
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
465
+ * See the License for the specific language governing permissions and
466
+ * limitations under the License.
467
+ */
468
+ let ComponentsController = class ComponentsController extends Disposable {
469
+ constructor(componentManager) {
470
+ super();
471
+ this.disposeWithMe(componentManager.register(TABLE_OF_CONTENTS_DIALOG_COMPONENT, TableOfContentsDialog));
472
+ this.disposeWithMe(componentManager.register(INSERT_TABLE_OF_CONTENTS_DIALOG_COMPONENT, InsertTableOfContentsDialog));
473
+ }
474
+ };
475
+ ComponentsController = __decorate([__decorateParam(0, Inject(ComponentManager))], ComponentsController);
476
+
477
+ //#endregion
478
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/typeof.js
479
+ function _typeof(o) {
480
+ "@babel/helpers - typeof";
481
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
482
+ return typeof o;
483
+ } : function(o) {
484
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
485
+ }, _typeof(o);
486
+ }
487
+
488
+ //#endregion
489
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPrimitive.js
490
+ function toPrimitive(t, r) {
491
+ if ("object" != _typeof(t) || !t) return t;
492
+ var e = t[Symbol.toPrimitive];
493
+ if (void 0 !== e) {
494
+ var i = e.call(t, r || "default");
495
+ if ("object" != _typeof(i)) return i;
496
+ throw new TypeError("@@toPrimitive must return a primitive value.");
497
+ }
498
+ return ("string" === r ? String : Number)(t);
499
+ }
500
+
501
+ //#endregion
502
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPropertyKey.js
503
+ function toPropertyKey(t) {
504
+ var i = toPrimitive(t, "string");
505
+ return "symbol" == _typeof(i) ? i : i + "";
506
+ }
507
+
508
+ //#endregion
509
+ //#region \0@oxc-project+runtime@0.149.0/helpers/esm/defineProperty.js
510
+ function _defineProperty(e, r, t) {
511
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
512
+ value: t,
513
+ enumerable: !0,
514
+ configurable: !0,
515
+ writable: !0
516
+ }) : e[r] = t, e;
517
+ }
518
+
519
+ //#endregion
520
+ //#region src/controllers/table-of-contents-highlight.render-controller.ts
521
+ const RANGE_PADDING = 4;
522
+ let TableOfContentsHighlightRenderController = class TableOfContentsHighlightRenderController extends RxDisposable {
523
+ constructor(_context, _instanceService, _selectionManager, _skeletonManager, _themeService) {
524
+ super();
525
+ this._context = _context;
526
+ this._instanceService = _instanceService;
527
+ this._selectionManager = _selectionManager;
528
+ this._skeletonManager = _skeletonManager;
529
+ this._themeService = _themeService;
530
+ _defineProperty(this, "_range", null);
531
+ _defineProperty(this, "_outline", null);
532
+ _defineProperty(this, "_rangeId", null);
533
+ this._selectionManager.textSelection$.pipe(takeUntil(this.dispose$)).subscribe(() => this._update());
534
+ this._skeletonManager.currentSkeleton$.pipe(takeUntil(this.dispose$)).subscribe(() => this._update(true));
535
+ this._instanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_DOC).pipe(takeUntil(this.dispose$)).subscribe(() => this._update());
536
+ this._update();
537
+ }
538
+ _update(force = false) {
539
+ const currentDoc = this._instanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC);
540
+ const selection = this._selectionManager.getActiveTextRange();
541
+ const toc = (currentDoc === null || currentDoc === void 0 ? void 0 : currentDoc.getUnitId()) === this._context.unitId && !(selection === null || selection === void 0 ? void 0 : selection.segmentId) ? findTableOfContentsAtOffset(this._context.unit.getBody(), selection === null || selection === void 0 ? void 0 : selection.startOffset) : void 0;
542
+ if (!toc) {
543
+ this._clear();
544
+ return;
545
+ }
546
+ if (!force && toc.rangeId === this._rangeId) return;
547
+ this._clear();
548
+ const document = this._context.mainComponent;
549
+ const endOffset = Math.max(toc.startIndex + 1, toc.endIndex - 1);
550
+ if (!document) return;
551
+ const primary = this._themeService.getColorFromTheme("primary.600");
552
+ const skeleton = this._skeletonManager.getSkeleton();
553
+ const startPosition = skeleton.findNodePositionByCharIndex(toc.startIndex + 1, true, "", -1);
554
+ const endPosition = skeleton.findNodePositionByCharIndex(endOffset, true, "", -1);
555
+ if (!startPosition || !endPosition) return;
556
+ const offsetConfig = document.getOffsetConfig();
557
+ const { borderBoxPointGroup } = new NodePositionConvertToCursor(offsetConfig, skeleton).getRangePointData(startPosition, endPosition);
558
+ const bounds = getPointGroupBounds(borderBoxPointGroup);
559
+ if (!bounds) return;
560
+ const range = new RegularPolygon(`__DocTocFill__${generateRandomId()}`, {
561
+ pointsGroup: borderBoxPointGroup,
562
+ left: offsetConfig.docsLeft,
563
+ top: offsetConfig.docsTop,
564
+ fill: new ColorKit(primary).setAlpha(.08).toRgbString(),
565
+ evented: false,
566
+ debounceParentDirty: false
567
+ });
568
+ const outline = new Rect(`__DocTocRange__${toc.rangeId}`, {
569
+ left: bounds.left + offsetConfig.docsLeft - RANGE_PADDING,
570
+ top: bounds.top + offsetConfig.docsTop - RANGE_PADDING,
571
+ width: bounds.width + 8,
572
+ height: bounds.height + 8,
573
+ radius: 2,
574
+ fill: "rgba(0, 0, 0, 0)",
575
+ stroke: new ColorKit(primary).setAlpha(.8).toRgbString(),
576
+ strokeWidth: 1,
577
+ evented: false,
578
+ debounceParentDirty: false
579
+ });
580
+ this._context.scene.addObject(range, TEXT_RANGE_LAYER_INDEX);
581
+ this._context.scene.addObject(outline, TEXT_RANGE_LAYER_INDEX);
582
+ this._range = range;
583
+ this._outline = outline;
584
+ this._rangeId = toc.rangeId;
585
+ this._context.scene.makeDirty();
586
+ }
587
+ _clear() {
588
+ var _this$_range, _this$_outline;
589
+ if (!this._range && !this._outline) {
590
+ this._rangeId = null;
591
+ return;
592
+ }
593
+ (_this$_range = this._range) === null || _this$_range === void 0 || _this$_range.dispose();
594
+ (_this$_outline = this._outline) === null || _this$_outline === void 0 || _this$_outline.dispose();
595
+ this._range = null;
596
+ this._outline = null;
597
+ this._rangeId = null;
598
+ this._context.scene.makeDirty();
599
+ }
600
+ dispose() {
601
+ this._clear();
602
+ super.dispose();
603
+ }
604
+ };
605
+ TableOfContentsHighlightRenderController = __decorate([
606
+ __decorateParam(1, IUniverInstanceService),
607
+ __decorateParam(2, Inject(DocSelectionManagerService)),
608
+ __decorateParam(3, Inject(DocSkeletonManagerService)),
609
+ __decorateParam(4, Inject(ThemeService))
610
+ ], TableOfContentsHighlightRenderController);
611
+ function getPointGroupBounds(pointsGroup) {
612
+ const points = pointsGroup.flat();
613
+ if (!points.length) return null;
614
+ const left = Math.min(...points.map((point) => point.x));
615
+ const top = Math.min(...points.map((point) => point.y));
616
+ const right = Math.max(...points.map((point) => point.x));
617
+ const bottom = Math.max(...points.map((point) => point.y));
618
+ return {
619
+ left,
620
+ top,
621
+ width: right - left,
622
+ height: bottom - top
623
+ };
624
+ }
625
+
626
+ //#endregion
627
+ //#region src/controllers/table-of-contents-ribbon.controller.ts
628
+ let TableOfContentsRibbonController = class TableOfContentsRibbonController extends Disposable {
629
+ constructor(_instanceService, _selectionManager, _ribbonService) {
630
+ super();
631
+ this._instanceService = _instanceService;
632
+ this._selectionManager = _selectionManager;
633
+ this._ribbonService = _ribbonService;
634
+ _defineProperty(this, "_visible", false);
635
+ this.disposeWithMe(this._selectionManager.textSelection$.subscribe(() => this._update()));
636
+ this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_DOC).subscribe(() => this._update()));
637
+ this._update();
638
+ }
639
+ _update() {
640
+ const doc = this._instanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC);
641
+ const selection = this._selectionManager.getActiveTextRange();
642
+ const visible = Boolean(doc && selection && !selection.segmentId && findTableOfContentsAtOffset(doc.getBody(), selection.startOffset));
643
+ if (visible === this._visible) return;
644
+ this._visible = visible;
645
+ if (visible) this._ribbonService.showContextualTab(DOC_TABLE_OF_CONTENTS_RIBBON_TAB, { activate: true });
646
+ else this._ribbonService.hideContextualTab(DOC_TABLE_OF_CONTENTS_RIBBON_TAB);
647
+ }
648
+ dispose() {
649
+ if (this._visible) this._ribbonService.hideContextualTab(DOC_TABLE_OF_CONTENTS_RIBBON_TAB);
650
+ super.dispose();
651
+ }
652
+ };
653
+ TableOfContentsRibbonController = __decorate([
654
+ __decorateParam(0, IUniverInstanceService),
655
+ __decorateParam(1, Inject(DocSelectionManagerService)),
656
+ __decorateParam(2, IRibbonService)
657
+ ], TableOfContentsRibbonController);
658
+
659
+ //#endregion
660
+ //#region src/controllers/ui.controller.ts
661
+ /**
662
+ * Copyright 2023-present DreamNum Co., Ltd.
663
+ *
664
+ * Licensed under the Apache License, Version 2.0 (the "License");
665
+ * you may not use this file except in compliance with the License.
666
+ * You may obtain a copy of the License at
667
+ *
668
+ * http://www.apache.org/licenses/LICENSE-2.0
669
+ *
670
+ * Unless required by applicable law or agreed to in writing, software
671
+ * distributed under the License is distributed on an "AS IS" BASIS,
672
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
673
+ * See the License for the specific language governing permissions and
674
+ * limitations under the License.
675
+ */
676
+ let DocsTocUIController = class DocsTocUIController extends Disposable {
677
+ constructor(commandService, menuManagerService) {
678
+ super();
679
+ this.disposeWithMe(commandService.registerCommand(OpenTableOfContentsDialogOperation));
680
+ this.disposeWithMe(commandService.registerCommand(InsertTableOfContentsOperation));
681
+ menuManagerService.appendRootMenu(tableOfContentsRibbonMenuSchema);
682
+ menuManagerService.mergeMenu(DocsTocUIMenuSchema);
683
+ }
684
+ };
685
+ DocsTocUIController = __decorate([__decorateParam(0, ICommandService), __decorateParam(1, IMenuManagerService)], DocsTocUIController);
686
+
687
+ //#endregion
688
+ //#region src/plugin.ts
689
+ let UniverDocsTocUIPlugin = class UniverDocsTocUIPlugin extends Plugin {
690
+ constructor(_config = defaultPluginConfig, _injector, _configService, _renderManagerService) {
691
+ super();
692
+ this._config = _config;
693
+ this._injector = _injector;
694
+ this._configService = _configService;
695
+ this._renderManagerService = _renderManagerService;
696
+ const { menu, ...rest } = merge({}, defaultPluginConfig, this._config);
697
+ if (menu) this._configService.setConfig("menu", menu, { merge: true });
698
+ this._configService.setConfig(DOCS_TOC_UI_PLUGIN_CONFIG_KEY, rest);
699
+ }
700
+ onStarting() {
701
+ [
702
+ [ComponentsController],
703
+ [DocsTocUIController],
704
+ [TableOfContentsRibbonController]
705
+ ].forEach((dependency) => this._injector.add(dependency));
706
+ this._injector.get(ComponentsController);
707
+ this._injector.get(DocsTocUIController);
708
+ }
709
+ onReady() {
710
+ this._injector.get(TableOfContentsRibbonController);
711
+ }
712
+ onRendered() {
713
+ this.disposeWithMe(this._renderManagerService.registerRenderModule(UniverInstanceType.UNIVER_DOC, [TableOfContentsHighlightRenderController]));
714
+ }
715
+ };
716
+ _defineProperty(UniverDocsTocUIPlugin, "pluginName", "DOCS_TOC_UI_PLUGIN");
717
+ _defineProperty(UniverDocsTocUIPlugin, "packageName", name);
718
+ _defineProperty(UniverDocsTocUIPlugin, "version", version);
719
+ _defineProperty(UniverDocsTocUIPlugin, "type", UniverInstanceType.UNIVER_DOC);
720
+ UniverDocsTocUIPlugin = __decorate([
721
+ DependentOn(UniverDocsPlugin, UniverRenderEnginePlugin, UniverDocsTocPlugin, UniverDocsUIPlugin),
722
+ __decorateParam(1, Inject(Injector)),
723
+ __decorateParam(2, IConfigService),
724
+ __decorateParam(3, IRenderManagerService)
725
+ ], UniverDocsTocUIPlugin);
726
+
727
+ //#endregion
728
+ export { DocsTocUIMenuSchema, UniverDocsTocUIPlugin };