
* Holding for a moment...
* web: replace rollup with esbuild
This commit replaces rollup with esbuild.
The biggest fix was to alter the way CSS is imported into our system;
esbuild delivers it to the browser as text, rather than as a bundle
with metadata that, frankly, we never use. ESBuild will bundle the
CSS for us just fine, and interpreting those strings *as* CSS turned
out to be a small hurdle. Code has been added to AKElement and
Interface to ensure that all CSS referenced by an element has been
converted to a Browser CSSStyleSheet before being presented to the
browser.
A similar fix has been provided for the markdown imports. The
biggest headache there was that the re-arrangement of our documentation
broke Jen's existing parser for fixing relative links. I've provided
a corresponding hack that provides the necessary detail, but since
the Markdown is being presented to the browser as text, we have to
provide a hint in the markdown component for where any relative
links should go, and we're importing and processing the markdown
at runtime. This doesn't seem to be a big performance hit.
The entire build process is driven by the new build script, `build.mjs`,
which starts the esbuild process as a service connected to the build
script and then runs the commands sent to it as fast as possible.
The biggest "hack" in it is actually the replacement for rollup's
`rollup-copy-plugin`, which is clever enough I'm surprised it doesn't
exist as a standalone file-copy package in its own right.
I've also used a filesystem watch library to encode a "watcher"
mechanism into the build script. `node build.mjs --watch` will
work on MacOS; I haven't tested it elsewhere, at least not yet.
`node build.mjs --proxy` does what the old rollup.proxy.js script
did.
The savings are substantial. It takes less than two seconds to build
the whole UI, a huge savings off the older ~45-50 seconds I routinely
saw on my old Mac. It's also about 9% smaller.
The trade-offs appear to be small: processing the CSS as StyleSheets,
and the Markdown as HTML, at run-time is a small performance hit,
but I didn't notice it in amongst everything else the UI does as
it starts up.
Manual chunking is gone; esbuild's support for that is quite difficult
to get right compared to Rollup's, although there's been a bit of
yelling at ESbuild over it. Codemirror is built into its own chunk;
it's just not _named_ distinctly anymore.
The one thing I haven't been able to test yet is whether or not the
polyfills and runtim shims work as expected on older browsers.
* web: continue with performance and build fixes
This commit introduces a couple of fixes enabled by esbuild and other
features.
1. build-locales
`build-locales` is a new NodeJS script in the `./scripts` folder
that does pretty much what it says in the name: it translates Xliff
files into `.ts` files. It has two DevExp advantages over the old
build system.
First, it will check the build times of the xlf files and
their ts equivalents, and will only run the actual build-locales
command if the XLF files are newer than their TS equivalents.
Second, it captures the stderr output from the build-locales command
and summarizes it. Instead of the thousands of lines of "this
string has no translation equivalent," now it just reports the
number of missed translations per locale.
2. check-spelling
This is a simple wrapper around the `codespell` command, mostly
just to reduce the visual clutter of `package.json`, but also to
permit it to run just about anywhere without needed hard-coded
paths to the dictionaries, using a fairly classic trick with git.
3. pseudolocalize and import-maps
These scripts were in TypeScript, but for our purposes I've
saved their constructed equivalents instead. This saves on
visual clutter in the `package.json` script, and reduced the
time they have to run during full builds. They're small enough
I feel confident they won't need too much looking over.
Also, two lint bugs in Markdown.ts have been fixed.
* Removed a few lines that weren't in use.
* build-locales was sufficiently complex it needed some comments.
* web: formalize that horrible unixy git status checker into a proper function.
* Added types for , the Markdown processor for in-line documentation.
* web: upgrade to Lit3
This commit replaces our Lit2 implementation with a Lit3 implementation.
This upgrade required two major shifts within our code, both of them consequential.
First, the restructuring of the way the get/set decorators for properties and states meant that a
lot of the code we were using needed to be refactored. More than that, a lot of those custom
accessors were implemented to trigger side-effects, such as when a providerID is set or changed
triggering the ProviderView to fetch the requsted Provider. The Lit2 and Lit3 documentation both say
[there is a better way to handle
this](https://lit.dev/docs/v2/components/properties/#:~:text=In%20most%20cases%2C%20you%20do%20not%20need%20to%20create%20custom%20property%20accessors)
by detecting the change in the `willUpdate()` point of an elements Lifecycle and triggering the side
effect there instead. I've done this in several places with a pattern of detecting the change, and
then naming the corresponding change as `fetchRequestedThing()`. The resulting code is cleaner and
uses fewer controversial features.
The other is that the type signature for `LitElement.createRenderRoot()` has changed to be either an
HTMLElement or a DocumentFragment. This required some serious refactoring of type changes through
Base and Interface codes. Noteably, the custom `AdoptedStyleSheetsElement` interface has been
superseded by the supplied and standardized
[DocumentOrShadowRoot](aa2b2352e1/src/lib/dom.generated.d.ts (L4715)
)
interface. Unfortunately, that interface is a mixin, and casting or instance checking are still in
place to make sure the objects being manipulated are typed "correctly."
Three files I touched during the course of this triggered SonarJS, so there are some minor fixes,
replacing some awkward syntax with more idiomatic code. These are very minor, such as replacing:
```
const result = someFunction();
return result;
/* with */
return someFunction();
```
and
```
const result = x();
if (!result) { return true } else { return false }
/* with */
return !x();
```
* fix package lock
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
* don't use hardcoded magic values
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
---------
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
Co-authored-by: Jens Langhammer <jens@goauthentik.io>
208 lines
7.0 KiB
TypeScript
208 lines
7.0 KiB
TypeScript
import { EVENT_REFRESH } from "@goauthentik/common/constants";
|
|
import { AKElement } from "@goauthentik/elements/Base";
|
|
import { setURLParams } from "@goauthentik/elements/router/RouteMatch";
|
|
|
|
import { msg } from "@lit/localize";
|
|
import { CSSResult, TemplateResult, html } from "lit";
|
|
import { customElement, property, state } from "lit/decorators.js";
|
|
|
|
import PFTreeView from "@patternfly/patternfly/components/TreeView/tree-view.css";
|
|
import PFBase from "@patternfly/patternfly/patternfly-base.css";
|
|
|
|
export interface TreeViewItem {
|
|
id?: string;
|
|
label: string;
|
|
childItems: TreeViewItem[];
|
|
parent?: TreeViewItem;
|
|
level: number;
|
|
}
|
|
|
|
@customElement("ak-treeview-node")
|
|
export class TreeViewNode extends AKElement {
|
|
@property({ attribute: false })
|
|
item?: TreeViewItem;
|
|
|
|
@property({ type: Boolean })
|
|
open = false;
|
|
|
|
@property({ attribute: false })
|
|
host?: TreeView;
|
|
|
|
@property()
|
|
activePath = "";
|
|
|
|
@property()
|
|
separator = "";
|
|
|
|
get openable(): boolean {
|
|
return (this.item?.childItems || []).length > 0;
|
|
}
|
|
|
|
get fullPath(): string {
|
|
const pathItems = [];
|
|
let item = this.item;
|
|
while (item) {
|
|
if (item.id) {
|
|
pathItems.push(item.id);
|
|
}
|
|
item = item.parent;
|
|
}
|
|
return pathItems.reverse().join(this.separator);
|
|
}
|
|
|
|
protected createRenderRoot() {
|
|
return this;
|
|
}
|
|
|
|
firstUpdated(): void {
|
|
const pathSegments = this.activePath.split(this.separator);
|
|
const level = this.item?.level || 0;
|
|
// Ignore the last item as that shouldn't be expanded
|
|
pathSegments.pop();
|
|
if (pathSegments[level] == this.item?.id) {
|
|
this.open = true;
|
|
}
|
|
if (this.activePath === this.fullPath && this.host !== undefined) {
|
|
this.host.activeNode = this;
|
|
}
|
|
}
|
|
|
|
render(): TemplateResult {
|
|
const shouldRenderChildren = (this.item?.childItems || []).length > 0 && this.open;
|
|
return html`
|
|
<li
|
|
class="pf-c-tree-view__list-item ${this.open ? "pf-m-expanded" : ""}"
|
|
role="treeitem"
|
|
tabindex="0"
|
|
>
|
|
<div class="pf-c-tree-view__content">
|
|
<button
|
|
class="pf-c-tree-view__node ${this.host?.activeNode === this
|
|
? "pf-m-current"
|
|
: ""}"
|
|
@click=${() => {
|
|
if (this.host) {
|
|
this.host.activeNode = this;
|
|
}
|
|
setURLParams({ path: this.fullPath });
|
|
this.dispatchEvent(
|
|
new CustomEvent(EVENT_REFRESH, {
|
|
bubbles: true,
|
|
composed: true,
|
|
}),
|
|
);
|
|
}}
|
|
>
|
|
<div class="pf-c-tree-view__node-container">
|
|
${this.openable
|
|
? html` <button
|
|
class="pf-c-tree-view__node-toggle"
|
|
@click=${(e: Event) => {
|
|
if (this.openable) {
|
|
this.open = !this.open;
|
|
e.stopPropagation();
|
|
}
|
|
}}
|
|
>
|
|
<span class="pf-c-tree-view__node-toggle-icon">
|
|
<i class="fas fa-angle-right" aria-hidden="true"></i>
|
|
</span>
|
|
</button>`
|
|
: html``}
|
|
<span class="pf-c-tree-view__node-icon">
|
|
<i
|
|
class="fas ${this.open ? "fa-folder-open" : "fa-folder"}"
|
|
aria-hidden="true"
|
|
></i>
|
|
</span>
|
|
<span class="pf-c-tree-view__node-text">${this.item?.label}</span>
|
|
</div>
|
|
</button>
|
|
</div>
|
|
<ul class="pf-c-tree-view__list" role="group" ?hidden=${!shouldRenderChildren}>
|
|
${this.item?.childItems.map((item) => {
|
|
return html`<ak-treeview-node
|
|
.item=${item}
|
|
activePath=${this.activePath}
|
|
separator=${this.separator}
|
|
.host=${this.host}
|
|
></ak-treeview-node>`;
|
|
})}
|
|
</ul>
|
|
</li>
|
|
`;
|
|
}
|
|
}
|
|
|
|
@customElement("ak-treeview")
|
|
export class TreeView extends AKElement {
|
|
static get styles(): CSSResult[] {
|
|
return [PFBase, PFTreeView];
|
|
}
|
|
|
|
@property({ type: Array })
|
|
items: string[] = [];
|
|
|
|
@property()
|
|
activePath = "";
|
|
|
|
@state()
|
|
activeNode?: TreeViewNode;
|
|
|
|
separator = "/";
|
|
|
|
createNode(path: string[], parentItem: TreeViewItem, level: number): TreeViewItem {
|
|
const id = path.shift();
|
|
const idx = parentItem.childItems.findIndex((e: TreeViewItem) => {
|
|
return e.id == id;
|
|
});
|
|
if (idx < 0) {
|
|
const item: TreeViewItem = {
|
|
id: id,
|
|
label: id || "",
|
|
childItems: [],
|
|
level: level,
|
|
parent: parentItem,
|
|
};
|
|
parentItem.childItems.push(item);
|
|
if (path.length !== 0) {
|
|
const child = this.createNode(path, item, level + 1);
|
|
child.parent = item;
|
|
}
|
|
return item;
|
|
} else {
|
|
return this.createNode(path, parentItem.childItems[idx], level + 1);
|
|
}
|
|
}
|
|
|
|
parse(data: string[]): TreeViewItem {
|
|
const rootItem: TreeViewItem = {
|
|
id: undefined,
|
|
label: msg("Root"),
|
|
childItems: [],
|
|
level: -1,
|
|
};
|
|
for (let i = 0; i < data.length; i++) {
|
|
const path: string = data[i];
|
|
const split: string[] = path.split(this.separator);
|
|
this.createNode(split, rootItem, 0);
|
|
}
|
|
return rootItem;
|
|
}
|
|
|
|
render(): TemplateResult {
|
|
const rootItem = this.parse(this.items);
|
|
return html`<div class="pf-c-tree-view pf-m-guides">
|
|
<ul class="pf-c-tree-view__list" role="tree">
|
|
<ak-treeview-node
|
|
.item=${rootItem}
|
|
activePath=${this.activePath}
|
|
?open=${true}
|
|
separator=${this.separator}
|
|
.host=${this}
|
|
></ak-treeview-node>
|
|
</ul>
|
|
</div>`;
|
|
}
|
|
}
|