Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix(editor): shadowless element should remove style element correctly (#10128)

donteatfriedricebd041cbfcf2d
diff --git a/blocksuite/framework/block-std/src/view/element/shadowless-element.ts b/blocksuite/framework/block-std/src/view/element/shadowless-element.ts
index a13446b677..7ad5fe1026 100644
--- a/blocksuite/framework/block-std/src/view/element/shadowless-element.ts
+++ b/blocksuite/framework/block-std/src/view/element/shadowless-element.ts
@@ -12,7 +12,7 @@ export class ShadowlessElement extends LitElement {
static onDisconnectedMap = new WeakMap<
Constructor, // class
- (() => void) | null
+ WeakMap<Node, (() => void) | null>
>();
// styles registered in ShadowlessElement will be available globally
@@ -67,7 +67,10 @@ export class ShadowlessElement extends LitElement {
injectedStyles.push(style);
}
});
- SE.onDisconnectedMap.set(SE, () => {
+ if (!SE.onDisconnectedMap.has(SE)) {
+ SE.onDisconnectedMap.set(SE, new WeakMap());
+ }
+ SE.onDisconnectedMap.get(SE)?.set(parentRoot, () => {
injectedStyles.forEach(style => style.remove());
});
}
@@ -79,6 +82,7 @@ export class ShadowlessElement extends LitElement {
}
override disconnectedCallback(): void {
+ const parentRoot = this.getRootNode();
super.disconnectedCallback();
const SE = this.constructor as typeof ShadowlessElement;
let styleInjectedCount = this.getConnectedCount();
@@ -86,7 +90,8 @@ export class ShadowlessElement extends LitElement {
this.setConnectedCount(styleInjectedCount);
if (styleInjectedCount === 0) {
- SE.onDisconnectedMap.get(SE)?.();
+ // remove the style element when the last shadowless element is disconnected in the parent root
+ SE.onDisconnectedMap.get(SE)?.get(parentRoot)?.();
}
}
}