Skip to content

Commit 678e300

Browse files
committed
feat: add editor tab isolation
1 parent 77f05df commit 678e300

File tree

1 file changed

+79
-5
lines changed

1 file changed

+79
-5
lines changed

src/lib/editorFile.js

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import tile from "components/tile";
33
import confirm from "dialogs/confirm";
44
import fsOperation from "fileSystem";
55
import startDrag from "handlers/editorFileTab";
6+
import tag from "html-tag-js";
67
import mimeTypes from "mime-types";
78
import Path from "utils/Path";
89
import Url from "utils/Url";
@@ -194,12 +195,46 @@ export default class EditorFile {
194195
if (options?.type) {
195196
this.#type = options.type;
196197
if (this.#type !== "editor") {
197-
const container = (
198-
<div className="tab-page-container">
199-
<div className="tab-page-content">{options.content}</div>
200-
</div>
201-
);
198+
const container = tag("div", {
199+
className: "tab-page-container",
200+
});
201+
202+
// shadow dom
203+
const shadow = container.attachShadow({ mode: "open" });
204+
205+
// main app styles
206+
const mainStyle = tag("link", {
207+
rel: "stylesheet",
208+
href: "./css/build/main.css",
209+
});
210+
// icon styles
211+
const iconStyle = tag("link", {
212+
rel: "stylesheet",
213+
href: "./res/icons/style.css",
214+
});
215+
// file icon styles
216+
const fileIconStyle = tag("link", {
217+
rel: "stylesheet",
218+
href: "./res/file-icons/style.css",
219+
});
220+
221+
const content = tag("div", {
222+
className: "tab-page-content",
223+
});
224+
content.appendChild(options.content);
225+
226+
// Add stylesheet and content to shadow DOM
227+
shadow.appendChild(mainStyle);
228+
shadow.appendChild(iconStyle);
229+
shadow.appendChild(fileIconStyle);
230+
shadow.appendChild(content);
231+
202232
this.#content = container;
233+
234+
// Handle custom stylesheets if provided
235+
if (options.stylesheets) {
236+
this.#addCustomStyles(options.stylesheets, shadow);
237+
}
203238
} else {
204239
this.#content = options.content;
205240
}
@@ -838,6 +873,45 @@ export default class EditorFile {
838873
if (index > -1) events.splice(index, 1);
839874
}
840875

876+
/**
877+
* Add custom stylesheets to shadow DOM
878+
* @param {string|string[]} styles URLs or CSS strings
879+
* @param {ShadowRoot} shadow Shadow DOM root
880+
*/
881+
#addCustomStyles(styles, shadow) {
882+
if (typeof styles === "string") {
883+
styles = [styles];
884+
}
885+
886+
styles.forEach((style) => {
887+
if (style.startsWith("http") || style.startsWith("/")) {
888+
// External stylesheet
889+
const link = tag("link", {
890+
rel: "stylesheet",
891+
href: style,
892+
});
893+
shadow.appendChild(link);
894+
} else {
895+
// Inline CSS
896+
const styleElement = tag("style", {
897+
textContent: style,
898+
});
899+
shadow.appendChild(styleElement);
900+
}
901+
});
902+
}
903+
904+
/**
905+
* Add stylesheet to tab's shadow DOM
906+
* @param {string} style URL or CSS string
907+
*/
908+
addStyle(style) {
909+
if (this.#type === "editor" || !this.#content) return;
910+
911+
const shadow = this.#content.shadowRoot;
912+
this.#addCustomStyles(style, shadow);
913+
}
914+
841915
/**
842916
*
843917
* @param {FileAction} action

0 commit comments

Comments
 (0)