@univerjs/docs-quick-insert-ui 0.25.0 → 1.0.0-alpha.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.
package/lib/es/index.js CHANGED
@@ -1,17 +1,17 @@
1
1
  import { CommandType, DeleteDirection, DependentOn, Direction, Disposable, DisposableCollection, ICommandService, IConfigService, IUniverInstanceService, Inject, Injector, LocaleService, Plugin, RANGE_DIRECTION, UniverInstanceType, generateRandomId, merge, toDisposable } from "@univerjs/core";
2
- import { DocSelectionManagerService, DocSkeletonManagerService, RichTextEditingMutation } from "@univerjs/docs";
3
- import { BulletListCommand, CutContentCommand, DeleteCommand, DeleteLeftCommand, DocCanvasPopManagerService, DocCreateTableOperation, DocEventManagerService, HorizontalLineCommand, IMEInputCommand, InsertCommand, MoveCursorOperation, NodePositionConvertToCursor, OrderListCommand, getAnchorBounding } from "@univerjs/docs-ui";
4
- import { ComponentManager, ILayoutService, IShortcutService, KeyCode, UniverUIPlugin, useDependency, useEvent, useObservable } from "@univerjs/ui";
2
+ import { DeleteTextCommand, DocSelectionManagerService, DocSkeletonManagerService, InsertTextCommand, RichTextEditingMutation } from "@univerjs/docs";
3
+ import { BulletListCommand, CutContentCommand, DeleteLeftCommand, DocCanvasPopManagerService, DocCreateTableOperation, DocEventManagerService, HorizontalLineCommand, IMEInputCommand, MoveCursorOperation, NodePositionConvertToCursor, OrderListCommand, getAnchorBounding } from "@univerjs/docs-ui";
4
+ import { ComponentManager, ILayoutService, IShortcutService, IconManager, KeyCode, UniverUIPlugin, useDependency, useEvent, useObservable } from "@univerjs/ui";
5
5
  import { IRenderManagerService, ptToPixel } from "@univerjs/engine-render";
6
6
  import { BehaviorSubject, combineLatest, distinctUntilChanged, map, of, tap } from "rxjs";
7
- import { useEffect, useMemo, useRef, useState } from "react";
7
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
8
8
  import { jsx, jsxs } from "react/jsx-runtime";
9
9
  import { Tooltip, borderBottomClassName, borderClassName, clsx, scrollbarClassName } from "@univerjs/design";
10
10
  import { InsertDocImageCommand, UniverDocsDrawingUIPlugin } from "@univerjs/docs-drawing-ui";
11
- import { DividerIcon, IncreaseIcon, TextIcon } from "@univerjs/icons";
12
11
  import { UniverDocsDrawingPlugin } from "@univerjs/docs-drawing";
13
12
  import { UniverDrawingPlugin } from "@univerjs/drawing";
14
13
  import { UniverDrawingUIPlugin } from "@univerjs/drawing-ui";
14
+ import { DividerIcon, IncreaseIcon, TextIcon } from "@univerjs/icons";
15
15
 
16
16
  //#region src/commands/commands/doc-quick-insert.command.ts
17
17
  const DeleteSearchKeyCommand = {
@@ -98,26 +98,33 @@ function getQuickInsertMenuLeafCount(menus) {
98
98
  return flattenMenuItems(menus).length;
99
99
  }
100
100
  function QuickInsertMenu(props) {
101
- const { menus, focusedMenuIndex, focusedMenuRef, menuNodeMapRef, componentManager, onFocusedMenuIndexChange, onSelect } = props;
101
+ const { menus, focusedMenuIndex, onFocusedMenuIndexChange, onFocusedMenuChange, onSelect } = props;
102
+ const iconManager = useDependency(IconManager);
102
103
  const flatMenus = useMemo(() => flattenMenuItems(menus), [menus]);
104
+ const menuNodeMapRef = useRef(/* @__PURE__ */ new Map());
103
105
  useEffect(() => {
104
106
  var _flatMenus$focusedMen, _menuNodeMapRef$curre;
105
107
  const focusedMenu = Number.isNaN(focusedMenuIndex) ? null : (_flatMenus$focusedMen = flatMenus[focusedMenuIndex]) !== null && _flatMenus$focusedMen !== void 0 ? _flatMenus$focusedMen : null;
106
- focusedMenuRef.current = focusedMenu;
108
+ onFocusedMenuChange(focusedMenu);
107
109
  if (!focusedMenu) return;
108
110
  (_menuNodeMapRef$curre = menuNodeMapRef.current.get(focusedMenu.id)) === null || _menuNodeMapRef$curre === void 0 || _menuNodeMapRef$curre.scrollIntoView({ block: "nearest" });
109
111
  }, [
110
112
  flatMenus,
111
113
  focusedMenuIndex,
112
- focusedMenuRef,
113
- menuNodeMapRef
114
+ onFocusedMenuChange
114
115
  ]);
116
+ useEffect(() => {
117
+ const menuNodeMap = menuNodeMapRef.current;
118
+ return () => {
119
+ menuNodeMap.clear();
120
+ };
121
+ }, []);
115
122
  const itemIndexRef = useRef(0);
116
123
  itemIndexRef.current = 0;
117
124
  function renderMenus(currentMenus) {
118
125
  return currentMenus.map((menu, index) => {
119
126
  const iconKey = menu.icon;
120
- const Icon = iconKey ? componentManager.get(iconKey) : null;
127
+ const Icon = iconKey ? iconManager.get(iconKey) : null;
121
128
  if (isMenuGroup(menu)) return /* @__PURE__ */ jsxs("div", {
122
129
  className: clsx("univer-grid univer-gap-1 univer-py-1", index !== currentMenus.length - 1 && borderBottomClassName),
123
130
  children: [/* @__PURE__ */ jsxs("div", {
@@ -178,21 +185,6 @@ function QuickInsertMenu(props) {
178
185
 
179
186
  //#endregion
180
187
  //#region src/views/QuickInsertPlaceholder.tsx
181
- /**
182
- * Copyright 2023-present DreamNum Co., Ltd.
183
- *
184
- * Licensed under the Apache License, Version 2.0 (the "License");
185
- * you may not use this file except in compliance with the License.
186
- * You may obtain a copy of the License at
187
- *
188
- * http://www.apache.org/licenses/LICENSE-2.0
189
- *
190
- * Unless required by applicable law or agreed to in writing, software
191
- * distributed under the License is distributed on an "AS IS" BASIS,
192
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
193
- * See the License for the specific language governing permissions and
194
- * limitations under the License.
195
- */
196
188
  const QuickInsertPlaceholderComponentKey = "docs.quick.insert.placeholder";
197
189
  const QuickInsertPlaceholder = () => {
198
190
  return /* @__PURE__ */ jsx("div", {
@@ -264,6 +256,9 @@ const QuickInsertPopup = () => {
264
256
  docQuickInsertPopupService.emitMenuSelected(menu);
265
257
  commandService.executeCommand(CloseQuickInsertPopupOperation.id);
266
258
  };
259
+ const handleFocusedMenuChange = useCallback((menu) => {
260
+ focusedMenuRef.current = menu;
261
+ }, []);
267
262
  useEffect(() => {
268
263
  /** Use up or down to navigate the focused menu instead of moving the cursor in documents. */
269
264
  const disposableCollection = new DisposableCollection();
@@ -338,12 +333,6 @@ const QuickInsertPopup = () => {
338
333
  useEffect(() => {
339
334
  setFocusedMenuIndex(0);
340
335
  }, [filteredMenus]);
341
- const menuNodeMapRef = useRef(/* @__PURE__ */ new Map());
342
- useEffect(() => {
343
- return () => {
344
- menuNodeMapRef.current.clear();
345
- };
346
- }, []);
347
336
  const hasMenus = filteredMenus.length > 0;
348
337
  const Placeholder = (currentPopup === null || currentPopup === void 0 ? void 0 : currentPopup.popup.Placeholder) || componentManager.get(QuickInsertPlaceholder.componentKey);
349
338
  return /* @__PURE__ */ jsx("div", {
@@ -351,10 +340,8 @@ const QuickInsertPopup = () => {
351
340
  children: hasMenus ? /* @__PURE__ */ jsx(QuickInsertMenu, {
352
341
  menus: filteredMenus,
353
342
  focusedMenuIndex,
354
- focusedMenuRef,
355
- menuNodeMapRef,
356
- componentManager,
357
343
  onFocusedMenuIndexChange: setFocusedMenuIndex,
344
+ onFocusedMenuChange: handleFocusedMenuChange,
358
345
  onSelect: handleMenuSelect
359
346
  }) : Placeholder && /* @__PURE__ */ jsx(Placeholder, {})
360
347
  });
@@ -362,7 +349,7 @@ const QuickInsertPopup = () => {
362
349
  QuickInsertPopup.componentKey = "docs.quick.insert.popup";
363
350
 
364
351
  //#endregion
365
- //#region \0@oxc-project+runtime@0.133.0/helpers/esm/typeof.js
352
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/typeof.js
366
353
  function _typeof(o) {
367
354
  "@babel/helpers - typeof";
368
355
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -373,7 +360,7 @@ function _typeof(o) {
373
360
  }
374
361
 
375
362
  //#endregion
376
- //#region \0@oxc-project+runtime@0.133.0/helpers/esm/toPrimitive.js
363
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPrimitive.js
377
364
  function toPrimitive(t, r) {
378
365
  if ("object" != _typeof(t) || !t) return t;
379
366
  var e = t[Symbol.toPrimitive];
@@ -386,14 +373,14 @@ function toPrimitive(t, r) {
386
373
  }
387
374
 
388
375
  //#endregion
389
- //#region \0@oxc-project+runtime@0.133.0/helpers/esm/toPropertyKey.js
376
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPropertyKey.js
390
377
  function toPropertyKey(t) {
391
378
  var i = toPrimitive(t, "string");
392
379
  return "symbol" == _typeof(i) ? i : i + "";
393
380
  }
394
381
 
395
382
  //#endregion
396
- //#region \0@oxc-project+runtime@0.133.0/helpers/esm/defineProperty.js
383
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/defineProperty.js
397
384
  function _defineProperty(e, r, t) {
398
385
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
399
386
  value: t,
@@ -404,7 +391,7 @@ function _defineProperty(e, r, t) {
404
391
  }
405
392
 
406
393
  //#endregion
407
- //#region \0@oxc-project+runtime@0.133.0/helpers/esm/decorateParam.js
394
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorateParam.js
408
395
  function __decorateParam(paramIndex, decorator) {
409
396
  return function(target, key) {
410
397
  decorator(target, key, paramIndex);
@@ -412,7 +399,7 @@ function __decorateParam(paramIndex, decorator) {
412
399
  }
413
400
 
414
401
  //#endregion
415
- //#region \0@oxc-project+runtime@0.133.0/helpers/esm/decorate.js
402
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorate.js
416
403
  function __decorate(decorators, target, key, desc) {
417
404
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
418
405
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -730,7 +717,7 @@ const builtInMenus = [{
730
717
  imageMenu
731
718
  ]
732
719
  }];
733
- const builtInMenuCommandIds = new Set([
720
+ const builtInMenuCommandIds = /* @__PURE__ */ new Set([
734
721
  numberedListMenu.id,
735
722
  bulletedListMenu.id,
736
723
  dividerMenu.id,
@@ -762,7 +749,7 @@ let DocQuickInsertTriggerController = class DocQuickInsertTriggerController exte
762
749
  const { _docQuickInsertPopupService, _textSelectionManagerService, _commandService } = this;
763
750
  const documentDataModel = this._univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC);
764
751
  if (documentDataModel === null || documentDataModel === void 0 ? void 0 : documentDataModel.getDisabled()) return;
765
- if (commandInfo.id === InsertCommand.id) {
752
+ if (commandInfo.id === InsertTextCommand.id) {
766
753
  const params = commandInfo.params;
767
754
  if (_docQuickInsertPopupService.editPopup) {
768
755
  _docQuickInsertPopupService.setInputOffset({
@@ -804,7 +791,7 @@ let DocQuickInsertTriggerController = class DocQuickInsertTriggerController exte
804
791
  });
805
792
  }
806
793
  }
807
- if (commandInfo.id === DeleteCommand.id) {
794
+ if (commandInfo.id === DeleteTextCommand.id) {
808
795
  const params = commandInfo.params;
809
796
  if (_docQuickInsertPopupService.editPopup && params.direction === DeleteDirection.LEFT) {
810
797
  var _params$len;
@@ -841,6 +828,53 @@ DocQuickInsertTriggerController = __decorate([
841
828
  __decorateParam(4, Inject(IUniverInstanceService))
842
829
  ], DocQuickInsertTriggerController);
843
830
 
831
+ //#endregion
832
+ //#region src/controllers/ui.controller.ts
833
+ let DocQuickInsertUIController = class DocQuickInsertUIController extends Disposable {
834
+ constructor(_commandService, _docQuickInsertPopupService) {
835
+ super();
836
+ this._commandService = _commandService;
837
+ this._docQuickInsertPopupService = _docQuickInsertPopupService;
838
+ this._initCommands();
839
+ this._initComponents();
840
+ this._initMenus();
841
+ }
842
+ _initCommands() {
843
+ [
844
+ DeleteSearchKeyCommand,
845
+ ShowQuickInsertPopupOperation,
846
+ CloseQuickInsertPopupOperation
847
+ ].forEach((operation) => {
848
+ this.disposeWithMe(this._commandService.registerCommand(operation));
849
+ });
850
+ }
851
+ _initComponents() {
852
+ [{
853
+ keyword: "/",
854
+ menus$: of(builtInMenus),
855
+ preconditions: (params) => {
856
+ var _startNodePosition;
857
+ return ((_startNodePosition = params.range.startNodePosition) === null || _startNodePosition === void 0 ? void 0 : _startNodePosition.glyph) === 0;
858
+ }
859
+ }].forEach((popup) => {
860
+ this.disposeWithMe(this._docQuickInsertPopupService.registerPopup(popup));
861
+ });
862
+ }
863
+ _initMenus() {}
864
+ };
865
+ DocQuickInsertUIController = __decorate([__decorateParam(0, ICommandService), __decorateParam(1, Inject(DocQuickInsertPopupService))], DocQuickInsertUIController);
866
+
867
+ //#endregion
868
+ //#region package.json
869
+ var name = "@univerjs/docs-quick-insert-ui";
870
+ var version = "1.0.0-alpha.0";
871
+
872
+ //#endregion
873
+ //#region src/config/config.ts
874
+ const DOCS_QUICK_INSERT_UI_PLUGIN_CONFIG_KEY = "docs-quick-insert-ui.config";
875
+ const configSymbol = Symbol(DOCS_QUICK_INSERT_UI_PLUGIN_CONFIG_KEY);
876
+ const defaultPluginConfig = {};
877
+
844
878
  //#endregion
845
879
  //#region src/menu/doc-quick-insert-menu.controller.ts
846
880
  let DocQuickInsertMenuController = class DocQuickInsertMenuController extends Disposable {
@@ -947,66 +981,48 @@ const QuickInsertButton = ({ className = "" }) => {
947
981
  QuickInsertButton.componentKey = QuickInsertButtonComponentKey;
948
982
 
949
983
  //#endregion
950
- //#region src/controllers/doc-quick-insert-ui.controller.ts
951
- let DocQuickInsertUIController = class DocQuickInsertUIController extends Disposable {
952
- constructor(_commandService, _docQuickInsertPopupService, _componentManager) {
984
+ //#region src/controllers/components.controller.ts
985
+ /**
986
+ * Copyright 2023-present DreamNum Co., Ltd.
987
+ *
988
+ * Licensed under the Apache License, Version 2.0 (the "License");
989
+ * you may not use this file except in compliance with the License.
990
+ * You may obtain a copy of the License at
991
+ *
992
+ * http://www.apache.org/licenses/LICENSE-2.0
993
+ *
994
+ * Unless required by applicable law or agreed to in writing, software
995
+ * distributed under the License is distributed on an "AS IS" BASIS,
996
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
997
+ * See the License for the specific language governing permissions and
998
+ * limitations under the License.
999
+ */
1000
+ let ComponentsController = class ComponentsController extends Disposable {
1001
+ constructor(_componentManager, _iconManager) {
953
1002
  super();
954
- this._commandService = _commandService;
955
- this._docQuickInsertPopupService = _docQuickInsertPopupService;
956
1003
  this._componentManager = _componentManager;
957
- this._initCommands();
958
- this._initComponents();
959
- this._initMenus();
1004
+ this._iconManager = _iconManager;
1005
+ this._registerIcons();
1006
+ this._registerComponents();
960
1007
  }
961
- _initCommands() {
962
- [
963
- DeleteSearchKeyCommand,
964
- ShowQuickInsertPopupOperation,
965
- CloseQuickInsertPopupOperation
966
- ].forEach((operation) => {
967
- this.disposeWithMe(this._commandService.registerCommand(operation));
968
- });
1008
+ _registerIcons() {
1009
+ this.disposeWithMe(this._iconManager.register({
1010
+ DividerIcon,
1011
+ TextIcon
1012
+ }));
969
1013
  }
970
- _initComponents() {
1014
+ _registerComponents() {
971
1015
  [
972
1016
  [QuickInsertPopup.componentKey, QuickInsertPopup],
973
1017
  [KeywordInputPlaceholder.componentKey, KeywordInputPlaceholder],
974
1018
  [QuickInsertPlaceholder.componentKey, QuickInsertPlaceholder],
975
- [DividerIcon.displayName, DividerIcon],
976
- [TextIcon.displayName, TextIcon],
977
1019
  [QuickInsertButton.componentKey, QuickInsertButton]
978
1020
  ].forEach(([key, comp]) => {
979
1021
  if (key) this.disposeWithMe(this._componentManager.register(key, comp));
980
1022
  });
981
- [{
982
- keyword: "/",
983
- menus$: of(builtInMenus),
984
- preconditions: (params) => {
985
- var _startNodePosition;
986
- return ((_startNodePosition = params.range.startNodePosition) === null || _startNodePosition === void 0 ? void 0 : _startNodePosition.glyph) === 0;
987
- }
988
- }].forEach((popup) => {
989
- this.disposeWithMe(this._docQuickInsertPopupService.registerPopup(popup));
990
- });
991
1023
  }
992
- _initMenus() {}
993
1024
  };
994
- DocQuickInsertUIController = __decorate([
995
- __decorateParam(0, ICommandService),
996
- __decorateParam(1, Inject(DocQuickInsertPopupService)),
997
- __decorateParam(2, Inject(ComponentManager))
998
- ], DocQuickInsertUIController);
999
-
1000
- //#endregion
1001
- //#region package.json
1002
- var name = "@univerjs/docs-quick-insert-ui";
1003
- var version = "0.25.0";
1004
-
1005
- //#endregion
1006
- //#region src/config/config.ts
1007
- const DOCS_QUICK_INSERT_UI_PLUGIN_CONFIG_KEY = "docs-quick-insert-ui.config";
1008
- const configSymbol = Symbol(DOCS_QUICK_INSERT_UI_PLUGIN_CONFIG_KEY);
1009
- const defaultPluginConfig = {};
1025
+ ComponentsController = __decorate([__decorateParam(0, Inject(ComponentManager)), __decorateParam(1, Inject(IconManager))], ComponentsController);
1010
1026
 
1011
1027
  //#endregion
1012
1028
  //#region src/plugin.ts
@@ -1022,6 +1038,8 @@ let UniverDocsQuickInsertUIPlugin = class UniverDocsQuickInsertUIPlugin extends
1022
1038
  this._configService.setConfig(DOCS_QUICK_INSERT_UI_PLUGIN_CONFIG_KEY, rest);
1023
1039
  }
1024
1040
  onStarting() {
1041
+ this._injector.add([ComponentsController]);
1042
+ this._injector.get(ComponentsController);
1025
1043
  [
1026
1044
  [DocQuickInsertUIController],
1027
1045
  [DocQuickInsertTriggerController],