Skip to content

Commit dcc6885

Browse files
committed
fix(embed): keep sheet tabs switchable from child blocks
1 parent 4794064 commit dcc6885

4 files changed

Lines changed: 44 additions & 4 deletions

File tree

packages/embed-ui/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export {
5151
EmbedMountService,
5252
} from './services/embed-mount.service';
5353
export { EmbedOverlayRootService, type EmbedOverlayRootRegistration } from './services/embed-overlay-root.service';
54-
export { disposeEmbedReactRoot } from './services/react-root-disposal';
54+
export { createEmbedReactRoot, disposeEmbedReactRoot } from './services/react-root-disposal';
5555
export {
5656
createEmbedRenderChildViewContribution,
5757
createEmbedChildRender,

packages/embed-ui/src/services/embed-product-menu-mounting.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import type { EmbedProductMenuMountContext } from '../types/embed-ui';
55
import { COMMAND_EXECUTION_INJECTOR_KEY, ICommandService, IConfigService, Injector, IUniverInstanceService, LocaleService, toDisposable } from '@univerjs/core';
66
import { DesktopRibbonService, IMenuManagerService, IRibbonService, MenuManagerPosition, MenuManagerService, RediProvider, Ribbon } from '@univerjs/ui';
77
import { createElement } from 'react';
8-
import { createRoot } from 'react-dom/client';
98
import { map, merge, of } from 'rxjs';
10-
import { disposeEmbedReactRoot } from './react-root-disposal';
9+
import { createEmbedReactRoot, disposeEmbedReactRoot } from './react-root-disposal';
1110

1211
export function mountEmbedProductRibbonMenu(context: EmbedProductMenuMountContext): IDisposable | undefined {
1312
const { container, injector, childType, childUnitId, menuSchema, menuTitlePrefix, activeRibbonTab, toolbarOnly } = context;
@@ -22,7 +21,7 @@ export function mountEmbedProductRibbonMenu(context: EmbedProductMenuMountContex
2221
menuTitlePrefix,
2322
activeRibbonTab,
2423
});
25-
const root = createRoot(container);
24+
const root = createEmbedReactRoot(container);
2625
root.render(createElement(
2726
RediProvider,
2827
{ value: { injector: scoped.injector as Injector } },

packages/embed-ui/src/services/react-root-disposal.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
11
import type { Root } from 'react-dom/client';
2+
import { createRoot } from 'react-dom/client';
3+
4+
const rootsByContainer = new WeakMap<Element, Root>();
5+
const containersByRoot = new WeakMap<Root, Element>();
6+
const rootVersions = new WeakMap<Root, number>();
7+
8+
export function createEmbedReactRoot(container: Element): Root {
9+
const existing = rootsByContainer.get(container);
10+
if (existing) {
11+
rootVersions.set(existing, (rootVersions.get(existing) ?? 0) + 1);
12+
return existing;
13+
}
14+
15+
const root = createRoot(container);
16+
rootsByContainer.set(container, root);
17+
containersByRoot.set(root, container);
18+
rootVersions.set(root, 0);
19+
return root;
20+
}
221

322
export function disposeEmbedReactRoot(root: Root): void {
23+
const container = containersByRoot.get(root);
24+
const version = rootVersions.get(root) ?? 0;
425
const unmount = () => {
26+
if (container && rootsByContainer.get(container) !== root) {
27+
return;
28+
}
29+
if ((rootVersions.get(root) ?? 0) !== version) {
30+
return;
31+
}
32+
if (container) {
33+
rootsByContainer.delete(container);
34+
containersByRoot.delete(root);
35+
}
36+
rootVersions.delete(root);
537
root.unmount();
638
};
739
if (typeof queueMicrotask === 'function') {

packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,15 @@ export function SheetBarTabs() {
304304
void commandService.executeCommand(SetWorksheetActiveOperation.id, {
305305
subUnitId,
306306
unitId: workbookRef.current.getUnitId(),
307+
}).then((result) => {
308+
if (result !== false) {
309+
return;
310+
}
311+
312+
const worksheet = workbookRef.current.getSheetBySheetId(subUnitId);
313+
if (worksheet) {
314+
workbookRef.current.setActiveSheet(worksheet);
315+
}
307316
});
308317
}, [commandService]);
309318

0 commit comments

Comments
 (0)