1
Fork 0
mirror of https://github.com/glassflame/glassflame.github.io.git synced 2024-11-23 00:24:25 +00:00

Create CSS module-like structure

Doesn't currently work; @import rules are not yet valid in constructed stylesheets.
This commit is contained in:
Steffo 2023-10-29 19:19:29 +01:00
parent 8d3343fa05
commit 71c48fc14d
Signed by: steffo
GPG key ID: 2A24051445686895
25 changed files with 144 additions and 111 deletions

View file

@ -7,37 +7,16 @@
<!-- Interaction scripts --> <!-- Interaction scripts -->
<script type="module" src="src/index.mjs"></script> <script type="module" src="src/index.mjs"></script>
<!-- Global style --> <!-- Global style -->
<style> <link rel="stylesheet" type="text/css" href="style/base.css">
@import "style/base.css"; <link rel="stylesheet" type="text/css" href="style/light.css">
@import "style/light.css"; <link rel="stylesheet" type="text/css" href="style/dark.css">
@import "style/dark.css";
</style>
<!-- Templates --> <!-- Templates -->
<template id="template-vault"> <template id="template-vault">
<style>
.vault {
margin-left: 64px;
margin-right: 64px;
}
</style>
<div class="vault"> <div class="vault">
<slot name="vault-child"></slot> <slot name="vault-child"></slot>
</div> </div>
</template> </template>
<template id="template-canvas"> <template id="template-canvas">
<style>
.canvas {
position: relative;
display: flex;
width: max-content;
height: max-content;
margin-top: 64px;
margin-bottom: 64px;
}
</style>
<div class="canvas"> <div class="canvas">
<slot name="canvas-nodes">{Canvas nodes}</slot> <slot name="canvas-nodes">{Canvas nodes}</slot>
<slot name="canvas-edges">{Canvas edges}</slot> <slot name="canvas-edges">{Canvas edges}</slot>
@ -47,32 +26,6 @@
<slot name="display-container">{Displayed content}</slot> <slot name="display-container">{Displayed content}</slot>
</template> </template>
<template id="template-node-group"> <template id="template-node-group">
<style>
.node {
outline: var(--node-group-border-width) solid var(--color-node);
background-color: color-mix(in srgb, var(--color-node) 20%, var(--color-background));
border-radius: 0 8px 8px 8px;
padding: 12px;
}
.node-group-label {
position: relative;
bottom: 14px;
left: -12px;
transform: translateY(-100%);
display: inline-block;
outline: var(--node-group-border-width) solid var(--color-node);
background-color: color-mix(in srgb, var(--color-node) 20%, var(--color-background));
border-radius: 8px 8px 0 0;
padding: 12px;
}
.node-group-label-title {
margin: 0;
}
</style>
<div class="canvas-item node node-group"> <div class="canvas-item node node-group">
<aside class="node-group-label"> <aside class="node-group-label">
<h1 class="node-group-label-title"><slot name="node-group-label">{Group label}</slot></h1> <h1 class="node-group-label-title"><slot name="node-group-label">{Group label}</slot></h1>
@ -80,25 +33,6 @@
</div> </div>
</template> </template>
<template id="template-node-file"> <template id="template-node-file">
<style>
.node {
outline: var(--node-file-border-width) solid var(--color-node);
background-color: color-mix(in srgb, var(--color-node) 10%, var(--color-background));
border-radius: 8px;
padding: 12px;
}
.node-file {
overflow-x: clip;
overflow-y: scroll;
}
.node-file-label {
font-size: 2em;
margin-block-start: .67em;
margin-block-end: .67em;
}
</style>
<article class="canvas-item node node-file"> <article class="canvas-item node node-file">
<h1 class="node-file-label"> <h1 class="node-file-label">
<slot name="node-file-label">{Node title}</slot> <slot name="node-file-label">{Node title}</slot>
@ -107,70 +41,29 @@
</article> </article>
</template> </template>
<template id="template-node-text"> <template id="template-node-text">
<style>
.node {
outline: var(--node-file-border-width) solid var(--color-node);
background-color: color-mix(in srgb, var(--color-node) 10%, var(--color-background));
border-radius: 8px;
padding: 12px;
}
.node-text {
overflow-x: clip;
overflow-y: scroll;
}
</style>
<article class="canvas-item node node-text"> <article class="canvas-item node node-text">
<slot name="node-text-contents">{Node contents}</slot> <slot name="node-text-contents">{Node contents}</slot>
</article> </article>
</template> </template>
<template id="template-edge"> <template id="template-edge">
<style>
.edge {
}
</style>
<div class="canvas-item edge"> <div class="canvas-item edge">
<slot name="edge-svg">{Edge SVG}</slot> <slot name="edge-svg">{Edge SVG}</slot>
</div> </div>
</template> </template>
<template id="template-markdown"> <template id="template-markdown">
<style>
.markdown {
}
</style>
<div class="markdown"> <div class="markdown">
<slot name="markdown-document">{Markdown text}</slot> <slot name="markdown-document">{Markdown text}</slot>
</div> </div>
</template> </template>
<template id="template-frontmatter"> <template id="template-frontmatter">
<style>
.frontmatter {
opacity: 50%;
}
</style>
<div class="frontmatter"> <div class="frontmatter">
<slot name="frontmatter-contents">{Markdown text}</slot> <slot name="frontmatter-contents">{Markdown text}</slot>
</div> </div>
</template> </template>
<template id="template-hashtag"> <template id="template-hashtag">
<style>
.hashtag {
background-color: color-mix(in srgb, var(--color-accent) 20%, transparent);
color: var(--color-accent);
}
</style>
<span class="hashtag"><slot name="hashtag-tag">{#Hashtag}</slot></span> <span class="hashtag"><slot name="hashtag-tag">{#Hashtag}</slot></span>
</template> </template>
<template id="template-wikilink"> <template id="template-wikilink">
<style>
.wikilink {
color: var(--color-accent);
text-decoration: underline 1px solid currentColor;
cursor: pointer;
}
</style>
<a class="wikilink"></a> <a class="wikilink"></a>
</template> </template>
</head> </head>

View file

@ -22,6 +22,27 @@ export class CustomElement extends HTMLElement {
throw new NotImplementedError("template has not been overridden.") throw new NotImplementedError("template has not been overridden.")
} }
/**
* Create a stylesheet which imports the stylesheet located at this module's path with `.mjs` replaced by `.css`.
* @param importMetaURL The value of `import.meta.url` for the calling module.
* @returns {CSSStyleSheet} The created stylesheet.
*/
static makeModuleLikeStyleSheet(importMetaURL) {
const importURL = importMetaURL.replace(/[.]mjs$/, ".css")
const stylesheet = new CSSStyleSheet()
stylesheet.replaceSync(`@import "${importURL}";`)
return stylesheet
}
/**
* A way to use inheritance with custom stylesheets.
* @abstract Implementors should add their own stylesheets via this function.
* @returns {CSSStyleSheet[]} An array of stylesheets that should be added to the {@link shadowRoot}.
*/
static createStyleSheets() {
return []
}
/** /**
* The local cloned instance of the template node. * The local cloned instance of the template node.
* @type {Node} * @type {Node}
@ -41,8 +62,10 @@ export class CustomElement extends HTMLElement {
* Callback automatically called when this element is added to the DOM. * Callback automatically called when this element is added to the DOM.
*/ */
connectedCallback() { connectedCallback() {
// The shadow root, the inner contents of the element.. // The shadow root, the inner contents of the element.
const shadow = this.attachShadow({ mode: "open" }) const shadow = this.attachShadow({ mode: "open" })
// Attach stylesheets to the shadow root.
shadow.adoptedStyleSheets.push(...this.constructor.createStyleSheets())
// The element contained inside the shadow root.. // The element contained inside the shadow root..
this.#instance = this.constructor.template.content.cloneNode(true) this.#instance = this.constructor.template.content.cloneNode(true)
// Call the custom callback. // Call the custom callback.

View file

@ -0,0 +1,11 @@
.canvas {
position: relative;
display: flex;
width: max-content;
height: max-content;
margin-top: 64px;
margin-bottom: 64px;
}

View file

@ -10,6 +10,10 @@ export class CanvasElement extends CustomElement {
return document.getElementById("template-canvas") return document.getElementById("template-canvas")
} }
static createStyleSheets() {
return [...super.createStyleSheets(), this.makeModuleLikeStyleSheet(import.meta.url)]
}
/** /**
* The contents of the Canvas, as they were the last time they were updated. * The contents of the Canvas, as they were the last time they were updated.
* @type {string} * @type {string}

View file

@ -0,0 +1,3 @@
.canvas-item {
}

View file

@ -7,6 +7,10 @@ import {MalformedError} from "../../utils/errors.mjs";
* @abstract * @abstract
*/ */
export class CanvasItemElement extends CustomElement { export class CanvasItemElement extends CustomElement {
static createStyleSheets() {
return [...super.createStyleSheets(), this.makeModuleLikeStyleSheet(import.meta.url)]
}
/** /**
* @returns {number} The X coordinate of the top left vertex of this element. * @returns {number} The X coordinate of the top left vertex of this element.
*/ */

View file

@ -0,0 +1,3 @@
.edge {
}

View file

@ -11,6 +11,10 @@ export class EdgeElement extends CanvasItemElement {
return document.getElementById("template-edge") return document.getElementById("template-edge")
} }
static createStyleSheets() {
return [...super.createStyleSheets(), this.makeModuleLikeStyleSheet(import.meta.url)]
}
/** /**
* The canvas this element is contained in. * The canvas this element is contained in.
* Can be recalculated with {@link recalculateCanvas}. * Can be recalculated with {@link recalculateCanvas}.

View file

@ -0,0 +1,6 @@
.node {
outline: var(--node-group-border-width) solid var(--color-node);
background-color: color-mix(in srgb, var(--color-node) 20%, var(--color-background));
border-radius: 0 8px 8px 8px;
padding: 12px;
}

View file

@ -6,6 +6,10 @@ import { CanvasItemElement } from "../canvasitem.mjs";
* @abstract * @abstract
*/ */
export class NodeElement extends CanvasItemElement { export class NodeElement extends CanvasItemElement {
static createStyleSheets() {
return [...super.createStyleSheets(), this.makeModuleLikeStyleSheet(import.meta.url)]
}
/** /**
* Coordinates of the point where edges connected to the top of this node should attach to. * Coordinates of the point where edges connected to the top of this node should attach to.
* @type {[number, number]} * @type {[number, number]}

View file

@ -0,0 +1,10 @@
.node-file {
overflow-x: clip;
overflow-y: scroll;
}
.node-file-label {
font-size: 2em;
margin-block-start: .67em;
margin-block-end: .67em;
}

View file

@ -8,6 +8,10 @@ export class NodeFileElement extends NodeElement {
return document.getElementById("template-node-file") return document.getElementById("template-node-file")
} }
static createStyleSheets() {
return [...super.createStyleSheets(), this.makeModuleLikeStyleSheet(import.meta.url)]
}
/** /**
* Get the path of the file displayed by this node, relative to the root of the vault, from the `path` attribute. * Get the path of the file displayed by this node, relative to the root of the vault, from the `path` attribute.
* @returns {string} The path. * @returns {string} The path.

View file

@ -0,0 +1,17 @@
.node-group-label {
position: relative;
bottom: 14px;
left: -12px;
transform: translateY(-100%);
display: inline-block;
outline: var(--node-group-border-width) solid var(--color-node);
background-color: color-mix(in srgb, var(--color-node) 20%, var(--color-background));
border-radius: 8px 8px 0 0;
padding: 12px;
}
.node-group-label-title {
margin: 0;
}

View file

@ -10,6 +10,10 @@ export class NodeGroupElement extends NodeElement {
return document.getElementById("template-node-group") return document.getElementById("template-node-group")
} }
static createStyleSheets() {
return [...super.createStyleSheets(), this.makeModuleLikeStyleSheet(import.meta.url)]
}
/** /**
* The label text of the group. * The label text of the group.
* Obtained from the `label` attribute of the element. * Obtained from the `label` attribute of the element.

View file

@ -0,0 +1,4 @@
.node-text {
overflow-x: clip;
overflow-y: scroll;
}

View file

@ -9,6 +9,10 @@ export class NodeTextElement extends NodeElement {
return document.getElementById("template-node-text") return document.getElementById("template-node-text")
} }
static createStyleSheets() {
return [...super.createStyleSheets(), this.makeModuleLikeStyleSheet(import.meta.url)]
}
/** /**
* Get the Markdown source of this node from the `document` attribute. * Get the Markdown source of this node from the `document` attribute.
*/ */

View file

@ -0,0 +1,3 @@
.frontmatter {
opacity: 50%;
}

View file

@ -9,6 +9,10 @@ export class FrontMatterElement extends CustomElement {
return document.getElementById("template-frontmatter") return document.getElementById("template-frontmatter")
} }
static createStyleSheets() {
return [...super.createStyleSheets(), this.makeModuleLikeStyleSheet(import.meta.url)]
}
/** /**
* The programming language used to define this front matter, obtained from the `lang` attribute. * The programming language used to define this front matter, obtained from the `lang` attribute.
* @type {string} * @type {string}

View file

@ -0,0 +1,4 @@
.hashtag {
background-color: color-mix(in srgb, var(--color-accent) 20%, transparent);
color: var(--color-accent);
}

View file

@ -9,6 +9,10 @@ export class HashtagElement extends CustomElement {
return document.getElementById("template-hashtag") return document.getElementById("template-hashtag")
} }
static createStyleSheets() {
return [...super.createStyleSheets(), this.makeModuleLikeStyleSheet(import.meta.url)]
}
/** /**
* The name of the hashtag, with no leading hash, obtained from the `tag` attribute. * The name of the hashtag, with no leading hash, obtained from the `tag` attribute.
* @returns {string} * @returns {string}

View file

@ -0,0 +1,3 @@
.markdown {
}

View file

@ -10,6 +10,10 @@ export class MarkdownElement extends CustomElement {
return document.getElementById("template-markdown") return document.getElementById("template-markdown")
} }
static createStyleSheets() {
return [...super.createStyleSheets(), this.makeModuleLikeStyleSheet(import.meta.url)]
}
/** /**
* {@link Marked} Markdown renderer. * {@link Marked} Markdown renderer.
* @type {Marked} * @type {Marked}

View file

@ -0,0 +1,5 @@
.wikilink {
color: var(--color-accent);
text-decoration: underline 1px solid currentColor;
cursor: pointer;
}

View file

@ -14,6 +14,10 @@ export class WikilinkElement extends CustomElement {
return document.getElementById("template-wikilink") return document.getElementById("template-wikilink")
} }
static createStyleSheets() {
return [...super.createStyleSheets(), this.makeModuleLikeStyleSheet(import.meta.url)]
}
/** /**
* Element displaying the this one. * Element displaying the this one.
* Can be recalculated with {@link recalculateAncestors}. * Can be recalculated with {@link recalculateAncestors}.

4
src/elements/vault.css Normal file
View file

@ -0,0 +1,4 @@
.vault {
margin-left: 64px;
margin-right: 64px;
}