Skip to content

Commit 5ff7964

Browse files
authored
fix(editor): prevent styling loss flash on custom page tabs when splitting/closing panes (Acode-Foundation#2449)
1 parent 88ab759 commit 5ff7964

2 files changed

Lines changed: 76 additions & 5 deletions

File tree

src/lib/editorFile.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ import run from "./run";
2929
import saveFile from "./saveFile";
3030
import appSettings from "./settings";
3131

32+
let mainCSSStyleSheet = null;
33+
34+
function getMainCSSStyleSheet() {
35+
if (mainCSSStyleSheet) return mainCSSStyleSheet;
36+
for (const sheet of document.styleSheets) {
37+
if (sheet.href && sheet.href.endsWith("main.css")) {
38+
return sheet;
39+
}
40+
}
41+
return null;
42+
}
43+
3244
function syncQuickToolsVisibility(file) {
3345
const { $toggler } = quickTools;
3446
const hideForFile = !!file?.hideQuickTools;
@@ -530,7 +542,43 @@ export default class EditorFile {
530542
shadow = container.attachShadow({ mode: "open" });
531543

532544
// Add base styles to shadow DOM first
533-
shadow.appendChild(<link rel="stylesheet" href="build/main.css" />);
545+
const sharedSheet = getMainCSSStyleSheet();
546+
let adopted = false;
547+
if (sharedSheet) {
548+
try {
549+
shadow.adoptedStyleSheets = [sharedSheet];
550+
adopted = true;
551+
} catch (e) {
552+
console.warn(
553+
"Failed to adopt document stylesheet, attempting constructed fallback",
554+
e,
555+
);
556+
if (
557+
typeof CSSStyleSheet !== "undefined" &&
558+
CSSStyleSheet.prototype.replaceSync
559+
) {
560+
try {
561+
const cssText = Array.from(sharedSheet.cssRules)
562+
.map((rule) => rule.cssText)
563+
.join("\n");
564+
const constructedSheet = new CSSStyleSheet();
565+
constructedSheet.replaceSync(cssText);
566+
shadow.adoptedStyleSheets = [constructedSheet];
567+
adopted = true;
568+
mainCSSStyleSheet = constructedSheet;
569+
} catch (innerError) {
570+
console.warn(
571+
"Failed constructed stylesheet fallback",
572+
innerError,
573+
);
574+
}
575+
}
576+
}
577+
}
578+
579+
if (!adopted) {
580+
shadow.appendChild(<link rel="stylesheet" href="build/main.css" />);
581+
}
534582

535583
// Handle custom stylesheets if provided
536584
if (options.stylesheets) {

src/lib/editorManager.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,36 @@ async function EditorManager($header, $body) {
408408
if (!node || node.type !== "split") return;
409409

410410
node.element.dataset.direction = node.direction;
411-
cleanupPaneSplitHandles(node.element);
412-
node.element.replaceChildren();
411+
412+
const targetElements = [];
413413
node.children.forEach((child, index) => {
414414
if (index > 0) {
415-
node.element.append(createPaneSplitHandle(node, index));
415+
targetElements.push(createPaneSplitHandle(node, index));
416+
}
417+
targetElements.push(child.element);
418+
});
419+
420+
cleanupPaneSplitHandles(node.element);
421+
422+
const currentChildren = Array.from(node.element.children);
423+
const targetSet = new Set(targetElements);
424+
425+
currentChildren.forEach((childEl) => {
426+
if (!targetSet.has(childEl)) {
427+
childEl.remove();
416428
}
417-
node.element.append(child.element);
429+
});
430+
431+
let currentEl = node.element.firstElementChild;
432+
for (const targetEl of targetElements) {
433+
if (currentEl === targetEl) {
434+
currentEl = currentEl.nextElementSibling;
435+
} else {
436+
node.element.insertBefore(targetEl, currentEl);
437+
}
438+
}
439+
440+
node.children.forEach((child) => {
418441
renderPaneLayout(child);
419442
});
420443
}

0 commit comments

Comments
 (0)