Skip to content

Commit

Permalink
Moving CSS embedding to the top of the view container (#2261)
Browse files Browse the repository at this point in the history
fixing #2260
  • Loading branch information
GottZ authored Mar 17, 2024
1 parent a47ac38 commit 242a7d0
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/api/inline-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export class DataviewInlineApi {
const simpleViewPath = `${viewName}.js`;
const complexViewPath = `${viewName}/view.js`;
let checkForCss = false;
let cssElement = undefined;
let viewFile = this.app.metadataCache.getFirstLinkpathDest(simpleViewPath, this.currentFilePath);
if (!viewFile) {
viewFile = this.app.metadataCache.getFirstLinkpathDest(complexViewPath, this.currentFilePath);
Expand All @@ -333,6 +334,16 @@ export class DataviewInlineApi {
return;
}

if (checkForCss) {
// Check for optional CSS.
let cssFile = this.app.metadataCache.getFirstLinkpathDest(`${viewName}/view.css`, this.currentFilePath);
if (cssFile) {
let cssContents = await this.app.vault.read(cssFile);
cssContents += `\n/*# sourceURL=${location.origin}/${cssFile.path} */`;
cssElement = this.container.createEl("style", { text: cssContents, attr: { scope: " " } });
}
}

let contents = await this.app.vault.read(viewFile);
if (contents.contains("await")) contents = "(async () => { " + contents + " })()";
contents += `\n//# sourceURL=${viewFile.path}`;
Expand All @@ -352,20 +363,9 @@ export class DataviewInlineApi {
true
);
} catch (ex) {
if (cssElement) this.container.removeChild(cssElement);
renderErrorPre(this.container, `Dataview: Failed to execute view '${viewFile.path}'.\n\n${ex}`);
}

if (!checkForCss) {
return;
}

// Check for optional CSS.
let cssFile = this.app.metadataCache.getFirstLinkpathDest(`${viewName}/view.css`, this.currentFilePath);
if (!cssFile) return;

let cssContents = await this.app.vault.read(cssFile);
cssContents += `\n/*# sourceURL=${location.origin}/${cssFile.path} */`;
this.container.createEl("style", { text: cssContents, attr: { scope: " " } });
}

/** Render a dataview list of the given values. */
Expand Down

0 comments on commit 242a7d0

Please sign in to comment.