From badcb3a7d7e803b004a065338617a33fe6ef1412 Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Thu, 26 Dec 2024 12:10:57 +0800 Subject: [PATCH] fix(component): fix lit portal not re-rendering in inline links case --- .../src/lit-react/lit-portal/lite-portal.tsx | 25 ++++++++++--------- .../bi-directional-link-panel.tsx | 2 +- .../block-suite-editor/lit-adaper.tsx | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/packages/frontend/component/src/lit-react/lit-portal/lite-portal.tsx b/packages/frontend/component/src/lit-react/lit-portal/lite-portal.tsx index 1f7249355c801..bcf68db34c4ad 100644 --- a/packages/frontend/component/src/lit-react/lit-portal/lite-portal.tsx +++ b/packages/frontend/component/src/lit-react/lit-portal/lite-portal.tsx @@ -13,10 +13,9 @@ type PortalEvent = { type PortalListener = (event: PortalEvent) => void; export function createLitPortalAnchor(callback: (event: PortalEvent) => void) { - const id = nanoid(); return html``; } @@ -119,24 +118,26 @@ export const useLitPortalFactory = () => { } const prevId = event.previousPortalId; - // Ignore first `willUpdate` - if (!prevId) { - return; - } - // No re-rendering allowed - // Used in `pdf embed view` scenario - if (!rerendering) { + // Ignores first `willUpdate` + if (!prevId) { return; } setPortals(portals => { const portal = portals.find(p => p.id === prevId); - if (!portal) return portals; + if (!portal) return [...portals]; + // Updates `ID` portal.id = id; - portal.portal.key = id; - portal.portal.children = element; + + // Re-rendering + // true: `inline link` + // false: `pdf embed view` + if (rerendering) { + portal.portal = ReactDOM.createPortal(element, target, id); + } + return [...portals]; }); }); diff --git a/packages/frontend/core/src/components/blocksuite/block-suite-editor/bi-directional-link-panel.tsx b/packages/frontend/core/src/components/blocksuite/block-suite-editor/bi-directional-link-panel.tsx index 9225fa6fac65f..e45e5b7875dd1 100644 --- a/packages/frontend/core/src/components/blocksuite/block-suite-editor/bi-directional-link-panel.tsx +++ b/packages/frontend/core/src/components/blocksuite/block-suite-editor/bi-directional-link-panel.tsx @@ -357,7 +357,7 @@ export const BiDirectionalLinkPanel = () => { { <> {portals.map(p => ( - {p.portal} + {p.portal} ))} } diff --git a/packages/frontend/core/src/components/blocksuite/block-suite-editor/lit-adaper.tsx b/packages/frontend/core/src/components/blocksuite/block-suite-editor/lit-adaper.tsx index 5857f08601b4b..56d971f5f1db4 100644 --- a/packages/frontend/core/src/components/blocksuite/block-suite-editor/lit-adaper.tsx +++ b/packages/frontend/core/src/components/blocksuite/block-suite-editor/lit-adaper.tsx @@ -184,7 +184,7 @@ const usePatchSpecs = (shared: boolean, mode: DocMode) => { () => ( <> {portals.map(p => ( - {p.portal} + {p.portal} ))} ),