astro-integration-pocketbase 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +87 -0
- package/index.ts +3 -0
- package/package.json +39 -0
- package/src/middleware/index.ts +20 -0
- package/src/middleware/is-pocketbase-entry.ts +35 -0
- package/src/pocketbase-integration.ts +67 -0
- package/src/toolbar/dom/create-entity.ts +42 -0
- package/src/toolbar/dom/create-header.ts +43 -0
- package/src/toolbar/dom/create-placeholder.ts +23 -0
- package/src/toolbar/dom/index.ts +3 -0
- package/src/toolbar/index.ts +6 -0
- package/src/toolbar/init-toolbar.ts +141 -0
- package/src/toolbar/page-navigation-listener.ts +14 -0
- package/src/toolbar/types/entity.ts +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 pawcode Development
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# astro-integration-pocketbase
|
|
2
|
+
|
|
3
|
+
<!--  -->
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/astro-integration-pocketbase)
|
|
6
|
+
[](https://www.npmjs.com/package/astro-integration-pocketbase)
|
|
7
|
+
[](https://github.com/pawcoding/astro-integration-pocketbase/blob/master/LICENSE)
|
|
8
|
+
[](https://discord.gg/GzgTh4hxrx)
|
|
9
|
+
|
|
10
|
+
This package provides an Astro toolbar for users of [astro-loader-pocketbase](https://github.com/pawcoding/astro-loader-pocketbase) to view PocketBase data directly in the Astro dev server.
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
> [!WARNING]
|
|
15
|
+
> This package is still under development.
|
|
16
|
+
> Until the first stable 1.0 release **breaking changes can occur at any time**.
|
|
17
|
+
|
|
18
|
+
## Basic usage
|
|
19
|
+
|
|
20
|
+
_For the toolbar to work, you need to have the [`astro-loader-pocketbase`](https://www.npmjs.com/package/astro-loader-pocketbase) package installed and configured in your project._
|
|
21
|
+
|
|
22
|
+
To use the toolbar, you need to import the `pocketbaseIntegration` function and add it to the `integrations` array in your Astro config file.
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { pocketbaseIntegration } from "astro-integration-pocketbase";
|
|
26
|
+
import { defineConfig } from "astro/config";
|
|
27
|
+
|
|
28
|
+
export default defineConfig({
|
|
29
|
+
integrations: [
|
|
30
|
+
pocketbaseIntegration({
|
|
31
|
+
// Make sure to use the same URL as in your pocketbaseLoader configuration
|
|
32
|
+
url: "https://<your-pocketbase-url>"
|
|
33
|
+
})
|
|
34
|
+
]
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
After adding the integration to your Astro config, you can start the dev server and see the PocketBase icon in the toolbar.
|
|
39
|
+
If you click on the icon, you can see the PocketBase entity viewer.
|
|
40
|
+
|
|
41
|
+
If a loader is found, the viewer will show a refresh button to reload all entries from the loaders.
|
|
42
|
+
|
|
43
|
+
## Entity viewer
|
|
44
|
+
|
|
45
|
+
To view the PocketBase entries inside the entity viewer, you need to use `Astro.props` to pass the entries to your page (and thus to the toolbar).
|
|
46
|
+
|
|
47
|
+
<details>
|
|
48
|
+
<summary>
|
|
49
|
+
Example page with PocketBase entries from a collection
|
|
50
|
+
</summary>
|
|
51
|
+
|
|
52
|
+
```astro
|
|
53
|
+
---
|
|
54
|
+
import { render, getCollection } from "astro:content";
|
|
55
|
+
import type { CollectionEntry } from "astro:content";
|
|
56
|
+
|
|
57
|
+
interface Props {
|
|
58
|
+
entry: CollectionEntry<"<your-collection">
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function getStaticPaths() {
|
|
62
|
+
const entries = await getCollection("<your-collection>");
|
|
63
|
+
return entries.map((entry) => ({
|
|
64
|
+
params: { id: entry.id },
|
|
65
|
+
props: { entry },
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const { entry } = Astro.props;
|
|
70
|
+
const { Content } = await render(entry);
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
<article>
|
|
74
|
+
<h1>{entry.data.title}</h1>
|
|
75
|
+
<Content />
|
|
76
|
+
</article>
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
</details>
|
|
80
|
+
|
|
81
|
+
The integration will automatically detect PocketBase entries in the props and display them in the entity viewer.
|
|
82
|
+
|
|
83
|
+
## All options
|
|
84
|
+
|
|
85
|
+
| Option | Type | Required | Description |
|
|
86
|
+
| ------ | -------- | -------- | ------------------------------------ |
|
|
87
|
+
| `url` | `string` | x | The URL of your PocketBase instance. |
|
package/index.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "astro-integration-pocketbase",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Luis Wolf <development@pawcode.de> (https://pawcode.de)",
|
|
6
|
+
"homepage": "https://github.com/pawcoding/astro-integration-pocketbase",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./index.ts"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src",
|
|
13
|
+
"index.ts"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"lint": "npx eslint",
|
|
17
|
+
"prepare": "husky"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"astro": "^4.0.0 || ^5.0.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@eslint/js": "^9.12.0",
|
|
24
|
+
"@stylistic/eslint-plugin": "^2.9.0",
|
|
25
|
+
"@types/node": "^22.7.4",
|
|
26
|
+
"astro": "^5.0.0-beta.3",
|
|
27
|
+
"eslint": "^9.12.0",
|
|
28
|
+
"globals": "^15.10.0",
|
|
29
|
+
"husky": "^9.1.6",
|
|
30
|
+
"typescript": "^5.6.2",
|
|
31
|
+
"typescript-eslint": "^8.8.0"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"astro",
|
|
35
|
+
"astro-integration",
|
|
36
|
+
"pocketbase",
|
|
37
|
+
"withastro"
|
|
38
|
+
]
|
|
39
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { defineMiddleware } from "astro/middleware";
|
|
2
|
+
import { isPocketbaseEntry } from "./is-pocketbase-entry";
|
|
3
|
+
|
|
4
|
+
export const onRequest = defineMiddleware(async (context, next) => {
|
|
5
|
+
// Look for entities given as props to the page
|
|
6
|
+
const props = Object.values(context.props);
|
|
7
|
+
const entities = props.filter(isPocketbaseEntry).map((prop) => prop.data);
|
|
8
|
+
|
|
9
|
+
const response = await next();
|
|
10
|
+
const body = await response.text();
|
|
11
|
+
|
|
12
|
+
// Append the entities to the <head>
|
|
13
|
+
const entitiesJson = JSON.stringify(entities);
|
|
14
|
+
const newBody = body.replace(
|
|
15
|
+
"</head>",
|
|
16
|
+
`<script>window.__astro_entities__ = ${entitiesJson}</script></head>`
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
return new Response(newBody, response);
|
|
20
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { z } from "astro/zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Schema for a PocketBase entry created with [astro-loader-pocketbase](https://github.com/pawcoding/astro-loader-pocketbase)
|
|
5
|
+
*/
|
|
6
|
+
const pocketbaseEntrySchema = z.object({
|
|
7
|
+
id: z.string().length(15),
|
|
8
|
+
data: z.object({
|
|
9
|
+
id: z.string().length(15),
|
|
10
|
+
collectionId: z.string().length(15),
|
|
11
|
+
collectionName: z.string(),
|
|
12
|
+
updated: z.optional(z.date()),
|
|
13
|
+
created: z.optional(z.date())
|
|
14
|
+
}),
|
|
15
|
+
digest: z.string().length(16),
|
|
16
|
+
collection: z.string()
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Type for a PocketBase entry created with [astro-loader-pocketbase](https://github.com/pawcoding/astro-loader-pocketbase)
|
|
21
|
+
*/
|
|
22
|
+
export type PocketBaseEntry = z.infer<typeof pocketbaseEntrySchema>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Checks if the given data is a PocketBase entry.
|
|
26
|
+
*/
|
|
27
|
+
export function isPocketbaseEntry(data: unknown): data is PocketBaseEntry {
|
|
28
|
+
try {
|
|
29
|
+
// Try to parse the data with the PocketBase entry schema
|
|
30
|
+
pocketbaseEntrySchema.parse(data);
|
|
31
|
+
return true;
|
|
32
|
+
} catch {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { AstroIntegration } from "astro";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
|
|
4
|
+
export function pocketbaseIntegration({
|
|
5
|
+
url
|
|
6
|
+
}: {
|
|
7
|
+
url: string;
|
|
8
|
+
}): AstroIntegration {
|
|
9
|
+
return {
|
|
10
|
+
name: "pocketbase-integration",
|
|
11
|
+
hooks: {
|
|
12
|
+
"astro:config:setup": ({ addDevToolbarApp, addMiddleware, command }) => {
|
|
13
|
+
// This integration is only available in dev mode
|
|
14
|
+
if (command !== "dev") {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Setup Toolbar
|
|
19
|
+
addDevToolbarApp({
|
|
20
|
+
name: "PocketBase",
|
|
21
|
+
id: `pocketbase-entry`,
|
|
22
|
+
icon: `<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>PocketBase</title><path fill="currentColor" d="M5.684 12a.632.632 0 0 1-.631-.632V4.421c0-.349.282-.632.631-.632h2.37c.46 0 .889.047 1.287.139.407.084.758.23 1.053.44.303.202.541.475.715.82.173.335.26.75.26 1.246 0 .479-.092.894-.273 1.247a2.373 2.373 0 0 1-.715.869 3.11 3.11 0 0 1-1.053.503c-.398.11-.823.164-1.273.164h-.46a.632.632 0 0 0-.632.632v1.52a.632.632 0 0 1-.632.631Zm1.279-4.888c0 .349.283.632.632.632h.343c1.04 0 1.56-.437 1.56-1.31 0-.428-.135-.73-.404-.907-.26-.176-.645-.264-1.156-.264h-.343a.632.632 0 0 0-.632.631Zm6.3 13.098a.632.632 0 0 1-.631-.631v-6.947a.63.63 0 0 1 .631-.632h2.203c.44 0 .845.034 1.216.1.38.06.708.169.984.328.276.16.492.37.647.63.164.26.246.587.246.982 0 .185-.03.37-.09.554a1.537 1.537 0 0 1-.26.516 1.857 1.857 0 0 1-1.076.7.031.031 0 0 0-.023.03c0 .015.01.028.025.03.591.111 1.04.32 1.346.626.311.31.466.743.466 1.297 0 .42-.082.78-.246 1.083a2.153 2.153 0 0 1-.685.755 3.4 3.4 0 0 1-1.036.441 5.477 5.477 0 0 1-1.268.139zm1.271-5.542c0 .349.283.631.632.631h.21c.465 0 .802-.088 1.009-.264.207-.176.31-.424.31-.743 0-.302-.107-.516-.323-.642-.207-.135-.535-.202-.984-.202h-.222a.632.632 0 0 0-.632.632Zm0 3.463c0 .349.283.631.632.631h.39c1.019 0 1.528-.369 1.528-1.108 0-.36-.125-.621-.376-.78-.241-.16-.625-.24-1.152-.24h-.39a.632.632 0 0 0-.632.632zM1.389 0C.629 0 0 .629 0 1.389V15.03a1.4 1.4 0 0 0 1.389 1.39H8.21a.632.632 0 0 0 .63-.632.632.632 0 0 0-.63-.63H1.389c-.078 0-.125-.05-.125-.128V1.39c0-.078.047-.125.125-.125H15.03c.078 0 .127.047.127.125v6.82a.632.632 0 0 0 .631.63.632.632 0 0 0 .633-.63V1.389A1.4 1.4 0 0 0 15.032 0ZM15.79 7.578a.632.632 0 0 0-.632.633.632.632 0 0 0 .631.63h6.822c.078 0 .125.05.125.128V22.61c0 .078-.047.125-.125.125H8.97c-.077 0-.127-.047-.127-.125v-6.82a.632.632 0 0 0-.631-.63.632.632 0 0 0-.633.63v6.822A1.4 1.4 0 0 0 8.968 24h13.643c.76 0 1.389-.629 1.389-1.389V8.97a1.4 1.4 0 0 0-1.389-1.39Z"/></svg>`,
|
|
23
|
+
entrypoint: fileURLToPath(new URL("./toolbar", import.meta.url))
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Setup middleware
|
|
27
|
+
addMiddleware({
|
|
28
|
+
order: "post",
|
|
29
|
+
entrypoint: fileURLToPath(new URL("./middleware", import.meta.url))
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
"astro:server:setup": ({ toolbar, logger, refreshContent }) => {
|
|
33
|
+
// Setup the listener for the refresh event if a loader is available
|
|
34
|
+
if (refreshContent) {
|
|
35
|
+
logger.info("Setting up refresh listener for PocketBase integration");
|
|
36
|
+
// Listen for the refresh event of the toolbar
|
|
37
|
+
toolbar.on("astro-integration-pocketbase:refresh", async () => {
|
|
38
|
+
// Send a loading state to the toolbar
|
|
39
|
+
toolbar.send("astro-integration-pocketbase:refresh", {
|
|
40
|
+
loading: true
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Refresh content loaded by the PocketBase loader
|
|
44
|
+
await refreshContent({
|
|
45
|
+
loaders: ["pocketbase-loader"],
|
|
46
|
+
// TODO: add context to refresh one or all collections
|
|
47
|
+
context: {}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Reset the loading state in the toolbar
|
|
51
|
+
toolbar.send("astro-integration-pocketbase:refresh", {
|
|
52
|
+
loading: false
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Send settings to the toolbar on initialization
|
|
58
|
+
toolbar.onAppInitialized("pocketbase-entry", () => {
|
|
59
|
+
toolbar.send("astro-integration-pocketbase:settings", {
|
|
60
|
+
enabled: !!refreshContent,
|
|
61
|
+
baseUrl: url
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { DevToolbarCard } from "astro/runtime/client/dev-toolbar/ui-library/card.js";
|
|
2
|
+
import type { Entity } from "../types/entity";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a card for an entity.
|
|
6
|
+
*/
|
|
7
|
+
export function createEntity(data: Entity, baseUrl?: string): DevToolbarCard {
|
|
8
|
+
// Create the main card
|
|
9
|
+
const main = document.createElement("astro-dev-toolbar-card");
|
|
10
|
+
|
|
11
|
+
// Create the main content container
|
|
12
|
+
const content = document.createElement("div");
|
|
13
|
+
content.style.position = "relative";
|
|
14
|
+
main.appendChild(content);
|
|
15
|
+
|
|
16
|
+
// Add the "View in PocketBase" button
|
|
17
|
+
if (baseUrl) {
|
|
18
|
+
const url = `${baseUrl}/_/#/collections?collectionId=${data.collectionId}&recordId=${data.id}`;
|
|
19
|
+
|
|
20
|
+
const viewInPocketbase = document.createElement("astro-dev-toolbar-button");
|
|
21
|
+
viewInPocketbase.size = "small";
|
|
22
|
+
viewInPocketbase.buttonStyle = "purple";
|
|
23
|
+
viewInPocketbase.textContent = "View in PocketBase";
|
|
24
|
+
viewInPocketbase.style.position = "absolute";
|
|
25
|
+
viewInPocketbase.style.top = "0";
|
|
26
|
+
viewInPocketbase.style.right = "0";
|
|
27
|
+
viewInPocketbase.addEventListener("click", () => {
|
|
28
|
+
window.open(url, "_blank");
|
|
29
|
+
});
|
|
30
|
+
content.appendChild(viewInPocketbase);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Add the entity data
|
|
34
|
+
const entity = document.createElement("pre");
|
|
35
|
+
entity.style.margin = "0";
|
|
36
|
+
entity.style.overflow = "auto";
|
|
37
|
+
entity.style.height = "300px";
|
|
38
|
+
entity.textContent = JSON.stringify(data, null, 2);
|
|
39
|
+
content.appendChild(entity);
|
|
40
|
+
|
|
41
|
+
return main;
|
|
42
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ToolbarServerHelpers } from "astro";
|
|
2
|
+
import type { DevToolbarButton } from "astro/runtime/client/dev-toolbar/ui-library/button.js";
|
|
3
|
+
import { default as packageJson } from "../../../package.json";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates the header for the PocketBase toolbar.
|
|
7
|
+
*/
|
|
8
|
+
export function createHeader(
|
|
9
|
+
server: ToolbarServerHelpers
|
|
10
|
+
): [HTMLElement, DevToolbarButton] {
|
|
11
|
+
// Create the header
|
|
12
|
+
const header = document.createElement("header");
|
|
13
|
+
header.style.display = "grid";
|
|
14
|
+
header.style.gap = "0.25rem";
|
|
15
|
+
header.style.gridTemplateColumns = "auto auto 1fr";
|
|
16
|
+
|
|
17
|
+
// Create the title
|
|
18
|
+
const title = document.createElement("h3");
|
|
19
|
+
title.style.marginTop = "0.25rem";
|
|
20
|
+
title.textContent = "PocketBase";
|
|
21
|
+
header.appendChild(title);
|
|
22
|
+
|
|
23
|
+
// Create the version badge
|
|
24
|
+
const version = document.createElement("astro-dev-toolbar-badge");
|
|
25
|
+
version.textContent = packageJson.version;
|
|
26
|
+
version.badgeStyle = "yellow";
|
|
27
|
+
header.appendChild(version);
|
|
28
|
+
|
|
29
|
+
// Create the refresh button
|
|
30
|
+
const refresh = document.createElement("astro-dev-toolbar-button");
|
|
31
|
+
refresh.size = "small";
|
|
32
|
+
refresh.buttonStyle = "green";
|
|
33
|
+
refresh.style.marginLeft = "auto";
|
|
34
|
+
refresh.textContent = "Refresh content";
|
|
35
|
+
// The refresh button is hidden by default
|
|
36
|
+
refresh.style.display = "none";
|
|
37
|
+
refresh.addEventListener("click", () => {
|
|
38
|
+
server.send("astro-integration-pocketbase:refresh", true);
|
|
39
|
+
});
|
|
40
|
+
header.appendChild(refresh);
|
|
41
|
+
|
|
42
|
+
return [header, refresh];
|
|
43
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { DevToolbarCard } from "astro/runtime/client/dev-toolbar/ui-library/card.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a placeholder card.
|
|
5
|
+
*/
|
|
6
|
+
export function createPlaceholder(): DevToolbarCard {
|
|
7
|
+
// Create the main card
|
|
8
|
+
const main = document.createElement("astro-dev-toolbar-card");
|
|
9
|
+
|
|
10
|
+
// Create the main content container
|
|
11
|
+
const content = document.createElement("div");
|
|
12
|
+
content.style.display = "flex";
|
|
13
|
+
content.style.alignItems = "center";
|
|
14
|
+
content.style.justifyContent = "center";
|
|
15
|
+
main.appendChild(content);
|
|
16
|
+
|
|
17
|
+
// Add the placeholder text
|
|
18
|
+
const placeholder = document.createElement("span");
|
|
19
|
+
placeholder.textContent = "Here you will see the raw content of an entity";
|
|
20
|
+
content.appendChild(placeholder);
|
|
21
|
+
|
|
22
|
+
return main;
|
|
23
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ToolbarAppEventTarget,
|
|
3
|
+
ToolbarServerHelpers
|
|
4
|
+
} from "astro/runtime/client/dev-toolbar/helpers.js";
|
|
5
|
+
import { createEntity, createHeader, createPlaceholder } from "./dom/";
|
|
6
|
+
import { listenToNavigation } from "./page-navigation-listener";
|
|
7
|
+
import type { Entity } from "./types/entity";
|
|
8
|
+
|
|
9
|
+
declare global {
|
|
10
|
+
interface Window {
|
|
11
|
+
__astro_entities__?: Array<Entity>;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Initializes the PocketBase toolbar.
|
|
17
|
+
*/
|
|
18
|
+
export async function initToolbar(
|
|
19
|
+
canvas: ShadowRoot,
|
|
20
|
+
app: ToolbarAppEventTarget,
|
|
21
|
+
server: ToolbarServerHelpers
|
|
22
|
+
): Promise<void> {
|
|
23
|
+
// Base url of the PocketBase instance
|
|
24
|
+
let pbUrl: string | undefined = undefined;
|
|
25
|
+
|
|
26
|
+
const container = document.createElement("astro-dev-toolbar-window");
|
|
27
|
+
|
|
28
|
+
const [header, refresh] = createHeader(server);
|
|
29
|
+
container.appendChild(header);
|
|
30
|
+
|
|
31
|
+
// Container for the main content
|
|
32
|
+
const contentContainer = document.createElement("div");
|
|
33
|
+
container.appendChild(contentContainer);
|
|
34
|
+
|
|
35
|
+
const placeholder = createPlaceholder();
|
|
36
|
+
contentContainer.appendChild(placeholder);
|
|
37
|
+
|
|
38
|
+
app.onToggled(({ state }) => {
|
|
39
|
+
// Clear the container
|
|
40
|
+
contentContainer.innerHTML = "";
|
|
41
|
+
|
|
42
|
+
if (!state) {
|
|
43
|
+
// Clear the dev-window
|
|
44
|
+
canvas.innerHTML = "";
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Append the dev-window
|
|
49
|
+
canvas.appendChild(container);
|
|
50
|
+
|
|
51
|
+
// Check if entities are present
|
|
52
|
+
const entities = window.__astro_entities__;
|
|
53
|
+
if (!entities || entities.length === 0) {
|
|
54
|
+
// No entities to display, show a placeholder
|
|
55
|
+
contentContainer.appendChild(createPlaceholder());
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Display the information about the entities
|
|
60
|
+
for (const entity of entities) {
|
|
61
|
+
contentContainer.appendChild(createEntity(entity, pbUrl));
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Update the toolbar placement based on the user's preference
|
|
66
|
+
function updateToolbarPlacement(
|
|
67
|
+
placement: "bottom-left" | "bottom-right" | "bottom-center"
|
|
68
|
+
) {
|
|
69
|
+
if (placement === "bottom-left") {
|
|
70
|
+
container.style.left = "16px";
|
|
71
|
+
container.style.right = "unset";
|
|
72
|
+
container.style.transform = "translateX(0)";
|
|
73
|
+
} else if (placement === "bottom-right") {
|
|
74
|
+
container.style.left = "unset";
|
|
75
|
+
container.style.right = "16px";
|
|
76
|
+
container.style.transform = "translateX(0)";
|
|
77
|
+
} else {
|
|
78
|
+
container.style.left = "50%";
|
|
79
|
+
container.style.right = "unset";
|
|
80
|
+
container.style.transform = "translateX(-50%)";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Read the initial toolbar placement from local storage
|
|
85
|
+
// This is a workaround since the initial toolbar placement is not available via any API
|
|
86
|
+
const settings = localStorage.getItem("astro:dev-toolbar:settings");
|
|
87
|
+
if (settings) {
|
|
88
|
+
const { placement } = JSON.parse(settings);
|
|
89
|
+
updateToolbarPlacement(placement);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Listen for toolbar placement updates
|
|
93
|
+
app.onToolbarPlacementUpdated(({ placement }) => {
|
|
94
|
+
updateToolbarPlacement(placement);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
server.on(
|
|
98
|
+
"astro-integration-pocketbase:settings",
|
|
99
|
+
({ enabled, baseUrl }: { enabled: boolean; baseUrl: string }) => {
|
|
100
|
+
// Show the refresh button if a loader is available
|
|
101
|
+
if (enabled) {
|
|
102
|
+
refresh.style.display = "unset";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Store the base URL for later use
|
|
106
|
+
pbUrl = baseUrl;
|
|
107
|
+
}
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
server.on(
|
|
111
|
+
"astro-integration-pocketbase:refresh",
|
|
112
|
+
({ loading }: { loading?: boolean }) => {
|
|
113
|
+
// Show loading state while refreshing content
|
|
114
|
+
if (loading) {
|
|
115
|
+
refresh.textContent = "Refreshing content...";
|
|
116
|
+
refresh.buttonStyle = "gray";
|
|
117
|
+
refresh.style.pointerEvents = "none";
|
|
118
|
+
} else {
|
|
119
|
+
refresh.textContent = "Refresh content";
|
|
120
|
+
refresh.buttonStyle = "green";
|
|
121
|
+
refresh.style.pointerEvents = "unset";
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
// Toggle the notification based on the presence of entities
|
|
127
|
+
listenToNavigation(() => {
|
|
128
|
+
// Check if entities are present
|
|
129
|
+
const entities = window.__astro_entities__;
|
|
130
|
+
if (!entities || entities.length === 0) {
|
|
131
|
+
app.toggleNotification({ state: false });
|
|
132
|
+
|
|
133
|
+
// Hide the toolbar if no entities are present
|
|
134
|
+
app.toggleState({ state: false });
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Show the notification
|
|
139
|
+
app.toggleNotification({ state: true, level: "info" });
|
|
140
|
+
});
|
|
141
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Execute a callback whenever the user navigates to a new page.
|
|
3
|
+
*/
|
|
4
|
+
export function listenToNavigation(callback: () => void): void {
|
|
5
|
+
// We trigger the callback immediately to ensure the toolbar is in sync with the current page.
|
|
6
|
+
// If no client-side navigation is done, this is the only time the callback will be triggered.
|
|
7
|
+
// Fortunately, this scrip will be re-executed on every page load.
|
|
8
|
+
callback();
|
|
9
|
+
|
|
10
|
+
// If client-side navigation is done (ViewTransitions / ClientRouter), we listen for page-load events.
|
|
11
|
+
document.addEventListener("astro:page-load", () => {
|
|
12
|
+
callback();
|
|
13
|
+
});
|
|
14
|
+
}
|