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

[BS-2565](https://linear.app/affine-design/issue/BS-2565/关闭-chat-block-center-peek-后新生成的-chat-block-最后一个-message-样式不正确)
This commit is contained in:
donteatfriedrice
2025-02-12 10:38:07 +00:00
parent db8557eafb
commit bd041cbfcf
@@ -12,7 +12,7 @@ export class ShadowlessElement extends LitElement {
static onDisconnectedMap = new WeakMap< static onDisconnectedMap = new WeakMap<
Constructor, // class Constructor, // class
(() => void) | null WeakMap<Node, (() => void) | null>
>(); >();
// styles registered in ShadowlessElement will be available globally // styles registered in ShadowlessElement will be available globally
@@ -67,7 +67,10 @@ export class ShadowlessElement extends LitElement {
injectedStyles.push(style); 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()); injectedStyles.forEach(style => style.remove());
}); });
} }
@@ -79,6 +82,7 @@ export class ShadowlessElement extends LitElement {
} }
override disconnectedCallback(): void { override disconnectedCallback(): void {
const parentRoot = this.getRootNode();
super.disconnectedCallback(); super.disconnectedCallback();
const SE = this.constructor as typeof ShadowlessElement; const SE = this.constructor as typeof ShadowlessElement;
let styleInjectedCount = this.getConnectedCount(); let styleInjectedCount = this.getConnectedCount();
@@ -86,7 +90,8 @@ export class ShadowlessElement extends LitElement {
this.setConnectedCount(styleInjectedCount); this.setConnectedCount(styleInjectedCount);
if (styleInjectedCount === 0) { 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)?.();
} }
} }
} }