Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(share): hide what an app view replaces until the view mounts
Elian Doran949d3d78279c
diff --git a/packages/share-theme/src/content/app_view.css b/packages/share-theme/src/content/app_view.cssindex 3eb404c32a..4ec411cd5d 100644--- a/packages/share-theme/src/content/app_view.css+++ b/packages/share-theme/src/content/app_view.css@@ -21,6 +21,36 @@ body:has(#content.app-view) {margin-top: 0;}+ #title-row {+ display: flex;+ flex: none;+ align-items: center;+ height: 50px;+ border-bottom: 1px solid var(--main-border-color);++ > .tn-icon {+ display: flex;+ flex-shrink: 0;+ align-items: center;+ justify-content: center;+ width: 50px;+ height: 50px;+ font-size: 30px;+ color: var(--muted-text-color);+ }++ > #title {+ min-width: 0;+ margin: 0;+ padding-inline: 12px;+ border-bottom: 0;+ line-height: 1;+ overflow: hidden;+ white-space: nowrap;+ text-overflow: ellipsis;+ }+ }+#content > :is(.share-collection, .share-note-view) {position: relative;display: flex;@@ -48,3 +78,43 @@ body:has(#content.app-view) {}}}++html.has-scripts:not(.app-view-shown) #content.app-view > :is(.share-collection, .share-note-view) {+ > :not(img) {+ visibility: hidden;+ overflow: hidden;+ animation: app-view-show 0s 10s forwards;+ }++ &:not(:has(> img))::after {+ content: "";+ position: absolute;+ inset: 0;+ width: 32px;+ height: 32px;+ margin: auto;+ border: 3px solid var(--background-highlight);+ border-top-color: var(--text-menu);+ border-radius: 50%;+ animation: app-view-spin 800ms linear infinite, app-view-hide 0s 10s forwards;+ }+}++@keyframes app-view-show {+ to {+ visibility: visible;+ overflow: auto;+ }+}++@keyframes app-view-hide {+ to {+ visibility: hidden;+ }+}++@keyframes app-view-spin {+ to {+ transform: rotate(360deg);+ }+}diff --git a/packages/share-theme/src/content/app_view.ts b/packages/share-theme/src/content/app_view.tsindex 0afb2dd3ed..dda853ada2 100644--- a/packages/share-theme/src/content/app_view.ts+++ b/packages/share-theme/src/content/app_view.ts@@ -8,13 +8,25 @@ export interface AppViewModule {}/**- * Mounts an app view on the page, if `#content` has its `container`, from the payload core embeds- * beside it. The view brings the app's context along, which expects jQuery as a global, so- * `app_globals.ts` loads first. `app_context.ts` then loads before the view's module, as in the- * app's entry: the modules it reaches depend on that order, such as `tree.ts`, which uses `ws.ts`- * as it loads.+ * Mounts an app view on the page, if `#content` has its `container`, then shows the container,+ * which `app_view.css` hides until then on a page that runs scripts. It shows it as well when the+ * view cannot mount, so the content core rendered stays readable.*/export default async function setupAppView(container: string, load: () => Promise<AppViewModule>) {+ try {+ await mountAppView(container, load);+ } finally {+ document.documentElement.classList.add("app-view-shown");+ }+}++/**+ * Mounts the app view from the payload core embeds beside `container`. The view brings the app's+ * context along, which expects jQuery as a global, so `app_globals.ts` loads first.+ * `app_context.ts` then loads before the view's module, as in the app's entry: the modules it+ * reaches depend on that order, such as `tree.ts`, which uses `ws.ts` as it loads.+ */+async function mountAppView(container: string, load: () => Promise<AppViewModule>) {const element = document.querySelector<HTMLElement>(`#content ${container}`);const data = document.querySelector("#content .share-froca");if (!element || !data?.textContent) {diff --git a/packages/share-theme/src/templates/boot_script.ejs b/packages/share-theme/src/templates/boot_script.ejsindex a463654835..ae94d76d4c 100644--- a/packages/share-theme/src/templates/boot_script.ejs+++ b/packages/share-theme/src/templates/boot_script.ejs@@ -1,6 +1,7 @@<script>- // Applies the theme before the first paint, to avoid color flashes. The- // function keeps its names out of the global scope, which `~shareHtml` snippets share.+ // Applies the theme before the first paint, to avoid color flashes, and marks the page as+ // running scripts, which hides what an app view replaces until it mounts. The function keeps+ // its names out of the global scope, which `~shareHtml` snippets share.(function () {function read(key) {try {@@ -15,7 +16,7 @@const stored = read("theme");const preference = stored === "light" || stored === "dark" ? stored : "system";const theme = preference === "system" ? (prefersDark ? "dark" : "light") : preference;- root.classList.add(`theme-${theme}`, `theme-preference-${preference}`);+ root.classList.add(`theme-${theme}`, `theme-preference-${preference}`, "has-scripts");window.glob = {isShare: true,diff --git a/packages/share-theme/src/templates/boot_script.spec.ts b/packages/share-theme/src/templates/boot_script.spec.tsindex 1e9fe7b667..25d901f3df 100644--- a/packages/share-theme/src/templates/boot_script.spec.ts+++ b/packages/share-theme/src/templates/boot_script.spec.ts@@ -10,18 +10,18 @@ describe("boot_script.ejs", () => {"theme": "dark"});- expect([ ...classes ]).toStrictEqual([ "theme-dark", "theme-preference-dark" ]);+ expect([ ...classes ]).toStrictEqual([ "theme-dark", "theme-preference-dark", "has-scripts" ]);expect(context.glob).toEqual({ isShare: true, isStatic: true, theme: "dark" });expect(() => runInContext(`const el = 1; let theme = 2; let root = 3;`, context)).not.toThrow();});it("follows the system theme when storage holds no theme or cannot be read", () => {expect([ ...runBootScript({}, true).classes ])- .toStrictEqual([ "theme-dark", "theme-preference-system" ]);+ .toStrictEqual([ "theme-dark", "theme-preference-system", "has-scripts" ]);expect([ ...runBootScript(null, false).classes ])- .toStrictEqual([ "theme-light", "theme-preference-system" ]);+ .toStrictEqual([ "theme-light", "theme-preference-system", "has-scripts" ]);expect([ ...runBootScript({ theme: "sepia" }, true).classes ])- .toStrictEqual([ "theme-dark", "theme-preference-system" ]);+ .toStrictEqual([ "theme-dark", "theme-preference-system", "has-scripts" ]);});it("leaves no inline script in the page template besides the boot script", () => {