Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
ee383e9
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jun 9, 2023
1 parent 8728d83 commit c0b05bd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 30 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog for Weavy

## v17.0.4

<time>2023-06-09</time>

* Fixed some issues for css styling of apps in uikit-js via *custom elements*.

## v17.0.3

<time>2023-06-02</time>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@weavy/dropin-js",
"version": "17.0.3",
"version": "17.0.4",
"author": "Weavy",
"description": "Javascript library for the Weavy drop-in UI",
"homepage": "https://github.com/weavy/weavy-dropin-js",
Expand Down
35 changes: 18 additions & 17 deletions src/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import WeavyPanel from "./panel";
import WeavyRoot from "./dom-root";
import WeavyOverlays from "./overlays";
import Weavy from "../weavy";
import WeavyStyles, { applyStyleSheet } from "./styles";
import WeavyStyles, { applyStyleSheet, moveImportsToTop } from "./styles";
import FileBrowser from "./filebrowser";
import { classNamesConcat } from "../utils/dom";

Expand All @@ -27,7 +27,7 @@ export default class WeavyApp extends MixinWeavyEvents(HTMLElement) {
};

static get observedAttributes() {
return ["class", "load", "uid"];
return ["class", "load", "uid", "css"];
}

attributeChangedCallback(name, oldValue, newValue) {
Expand All @@ -38,6 +38,13 @@ export default class WeavyApp extends MixinWeavyEvents(HTMLElement) {
this.overlays.className = newValue;
});
}

if (name === "css") {
this.css = newValue;
this.whenBuilt().then(() => {
this.panel.postStyles()
})
}
}
/**
* The uid of the app, defined in options.
Expand Down Expand Up @@ -302,7 +309,9 @@ export default class WeavyApp extends MixinWeavyEvents(HTMLElement) {
this.#styles = new WeavyStyles(this);
this.#styles.eventParent = this;

if (this.options.styles !== undefined) {
if (this.hasAttribute("css")) {
this.#styles.css = this.getAttribute("css");
} else if (this.options.styles !== undefined) {
this.#styles.css = this.options.css;
}

Expand Down Expand Up @@ -362,6 +371,10 @@ export default class WeavyApp extends MixinWeavyEvents(HTMLElement) {
this.append(this.root.root);
document.documentElement.append(this.overlayRoot.root);

if (this.hasAttribute("css")) {
this.css = this.getAttribute("css");
}

// CONFIGURE
Weavy.whenReady().then(() => {
this.#styles.updateStyles();
Expand Down Expand Up @@ -494,9 +507,9 @@ export default class WeavyApp extends MixinWeavyEvents(HTMLElement) {
this.overlays.className = this.className;

this.on("panel-css", (eventCss) => {
eventCss.css = [eventCss.css, this.#styles.getAllCSS()]
eventCss.css = moveImportsToTop([eventCss.css, this.#styles.getAllCSS()]
.filter((s) => s)
.join("\n");
.join("\n"));
});

this.on("overlay-open", (overlayOptions) => {
Expand Down Expand Up @@ -773,18 +786,6 @@ export default class WeavyApp extends MixinWeavyEvents(HTMLElement) {
await this.panel.postMessage(message, transfer);
}

/**
* Adds CSS styles to the app using inline css.
*
* @category methods
* @function WeavyApp#addCSS
* @param {string} css - CSS string
*/
async addCSS(css) {
await this.whenBuilt();
this.root.addStyles({ css });
}

/**
* Check if another app or an object is matching this app. It checks for a match of the uid property or matching the url property against the base url.
*
Expand Down
28 changes: 18 additions & 10 deletions src/components/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const console = new WeavyConsole("Styles")
export function applyStyleSheet(root, stylesheet) {
// Prefer modern CSS registration
if (WeavyStyles.supportsConstructableStyleSheets) {
root.adoptedStyleSheets = Array.prototype.concat.call(root.adoptedStyleSheets, [stylesheet]);
try {
root.adoptedStyleSheets = Array.prototype.concat.call(root.adoptedStyleSheets, [stylesheet]);
} catch(e) { /* No worries */ }
} else {
// Fallback CSS registration
(root === document ? document.head : root).appendChild(stylesheet);
Expand All @@ -33,15 +35,27 @@ export function createStyleSheet(css) {
}

export function updateStyleSheet(styleSheet, css) {
css = moveImportsToTop(css)
// Prefer modern CSS registration
if (WeavyStyles.supportsConstructableStyleSheets) {
styleSheet.replaceSync(css);
try {
styleSheet.replaceSync(css);
} catch(e) { /* No worries */ }

} else {
// Fallback CSS registration
styleSheet.replaceChildren(document.createTextNode(css));
}
}

export function moveImportsToTop(css) {
// Move @imports to the top
let foundImports = []
css = css.replace(/@import\s+\S*;?/gm, (match) => { foundImports.push(match); return ""; });
css = foundImports.join("\n") + css;
return css;
}

/**
* @class WeavyStyles
Expand Down Expand Up @@ -124,13 +138,10 @@ export default class WeavyStyles extends WeavyEvents {
set css(css) {
this.#css = css

// Check if we have a new color?
this.#updateExternalColor();

this.styleSheets.custom ??= createStyleSheet();
updateStyleSheet(this.styleSheets.custom, css);

this.#showAnyErrors(this.#element.id, "Some stylesheets could not be processed");
this.updateStyles();
}

/**
Expand Down Expand Up @@ -420,10 +431,7 @@ export default class WeavyStyles extends WeavyEvents {

let combinedCSS = [rootFont, rootColors, this.#externalCss, rootVars, this.#css].filter((s) => s).join("\n");

// Move @imports to the top
let foundImports = []
combinedCSS = combinedCSS.replace(/@import\s+\S*;?/gm, (match) => { foundImports.push(match); return ""; });
combinedCSS = foundImports.join("\n") + combinedCSS;
combinedCSS = moveImportsToTop(combinedCSS);

return combinedCSS;
}
Expand Down

0 comments on commit c0b05bd

Please sign in to comment.