@wazootech/worlds-kit 0.0.0-stage → 0.1.2
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 +115 -2
- package/dist/index.d.ts +86 -0
- package/dist/index.js +397 -0
- package/package.json +67 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ethan Davidson
|
|
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
CHANGED
|
@@ -1,3 +1,116 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://docs.wazoo.dev">
|
|
3
|
+
<img src="https://wazoo.dev/assets/wazoo.svg" alt="Wazoo Worlds" width="120" />
|
|
4
|
+
</a>
|
|
5
|
+
<br /><br />
|
|
6
|
+
<em>React composition library for building item-oriented interfaces on Worlds.</em>
|
|
7
|
+
<br /><br />
|
|
8
|
+
<a href="https://www.npmjs.com/package/@wazootech/worlds-kit"><img src="https://img.shields.io/npm/v/%40wazootech%2Fworlds-kit" alt="npm" /></a>
|
|
9
|
+
<a href="https://github.com/wazootech/worlds-kit"><img src="https://img.shields.io/badge/GitHub-black?logo=github" alt="GitHub" /></a>
|
|
10
|
+
<a href="https://deepwiki.com/wazootech/worlds-kit"><img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki" /></a>
|
|
11
|
+
</p>
|
|
2
12
|
|
|
3
|
-
|
|
13
|
+
A React composition library for building item-oriented interfaces on top of [Worlds](https://wazoo.dev/worlds). WorldsKit keeps the interaction model demonstrated in Alexander Obenauer's Lab Note 001—implicit item binding, datasets, sortable datasets, add actions, and `Source`/`Detail` composition—while using Worlds as the RDF-native persistence and query layer.
|
|
14
|
+
|
|
15
|
+
> WorldsKit is an independent reimplementation inspired by the ideas and interaction demonstrated in [Lab Note 001: “Composing application interfaces”](https://alexanderobenauer.com/labnotes/001/). It is not Alexander Obenauer's original framework and does not claim source-level equivalence. The original demonstration video is [available on YouTube](https://www.youtube.com/watch?v=PkFBGKkUBD4).
|
|
16
|
+
|
|
17
|
+
## What is implemented
|
|
18
|
+
|
|
19
|
+
- `WorldsKitApp`: provides a WorldsKit data source, world ID, and implicit selection context.
|
|
20
|
+
- `Dataset`: queries an ordered RDF collection and renders each result through a supplied React template.
|
|
21
|
+
- `DatasetSortable`: adds drag-and-drop ordering and persists the resulting order as RDF mutations.
|
|
22
|
+
- `Source` and `Detail`: provide master-detail composition without manually threading selected IDs through the tree.
|
|
23
|
+
- `TextField`, `Checkbox`, and `Title`: item-bound primitives backed by SPARQL query/mutation operations.
|
|
24
|
+
- `Todo` and `SimpleRow`: small templates used by the reference demo.
|
|
25
|
+
- `createWorldsKitDataSource`: a minimal HTTP data-source adapter for Worlds query and update operations. Inject a richer WorldsKit data source when you need subscriptions or application-specific transport.
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
WorldsKit needs React 18 or newer.
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
npm install @wazootech/worlds-kit react react-dom # Node.js 22 or newer
|
|
33
|
+
bun add @wazootech/worlds-kit react react-dom # Bun
|
|
34
|
+
deno add npm:@wazootech/worlds-kit npm:react npm:react-dom # Deno
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Deno waits a day before it installs a new release. To get one sooner, add `--minimum-dependency-age=0` to the command.
|
|
38
|
+
|
|
39
|
+
Versions up to 0.1.1 were published on JSR as `@worlds/kit`. Only the name and the registry changed: every export is the same. The npm package is compiled, so it also loads in Node.js and Bun without a bundler.
|
|
40
|
+
|
|
41
|
+
## Quick start
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
import { createWorldsKitDataSource, DatasetSortable, Detail, SimpleRow, Source, Title, Todo, WorldsKitApp } from "@wazootech/worlds-kit";
|
|
45
|
+
|
|
46
|
+
const dataSource = createWorldsKitDataSource("https://your-worlds-endpoint.example");
|
|
47
|
+
|
|
48
|
+
export default function App() {
|
|
49
|
+
return (
|
|
50
|
+
<WorldsKitApp worldId="my-world" dataSource={dataSource}>
|
|
51
|
+
<Source>
|
|
52
|
+
<DatasetSortable itemId="lists" template={<SimpleRow editable />} addButton={<button>Add a list</button>} />
|
|
53
|
+
</Source>
|
|
54
|
+
<Detail>
|
|
55
|
+
<Title />
|
|
56
|
+
<DatasetSortable template={<Todo />} addButton={<button>Add a todo...</button>} />
|
|
57
|
+
</Detail>
|
|
58
|
+
</WorldsKitApp>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The adapter uses RDF identifiers and predicates under the `https://wazoo.dev/worlds-kit/` namespace. The list/detail demo models list records as members of the `lists` dataset under `root`; todo records use the selected list item as their parent. The exact Worlds endpoint and authentication mechanism are intentionally injected through `WorldsKitDataSource` so the UI library does not own deployment or credentials.
|
|
64
|
+
|
|
65
|
+
## Demo
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
bun install
|
|
69
|
+
bun run build
|
|
70
|
+
cd demo
|
|
71
|
+
bun run --bun dev
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The demo expects a Worlds-compatible HTTP endpoint in `VITE_WORLDS_ENDPOINT` and an optional `VITE_WORLDS_TOKEN`. It is deliberately a small composition example, not a hosted production app.
|
|
75
|
+
|
|
76
|
+
## Worlds adapter contract
|
|
77
|
+
|
|
78
|
+
`WorldsKitDataSource` has three operations:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
export type WorldsKitDataSource = {
|
|
82
|
+
query: (sparql: string, options?: { signal?: AbortSignal }) => Promise<unknown>;
|
|
83
|
+
mutate: (update: string, options?: { signal?: AbortSignal }) => Promise<unknown>;
|
|
84
|
+
subscribe?: (sparql: string, onData: (data: unknown) => void, onError: (error: Error) => void) => () => void;
|
|
85
|
+
};
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`subscribe` is optional. Without it, WorldsKit performs an initial query; with it, datasets and bound items can receive live result updates. This keeps the React library independent of a particular Worlds transport while making SPARQL the durable source of truth.
|
|
89
|
+
|
|
90
|
+
## Development
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
bun install --frozen-lockfile --ignore-scripts
|
|
94
|
+
bun run ci
|
|
95
|
+
bun run check:package
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
`bun run ci` checks formatting, lints, type-checks, tests and builds. `bun run check:package` installs the built package in an empty project, then renders components with React 18 and 19 on Node.js, Bun and Deno, which must all be on your PATH.
|
|
99
|
+
|
|
100
|
+
**Releases:** bump `version` in `package.json` in a pull request (a patch, unless the change breaks callers). Merging it to `main` publishes that version to npm. Merging without a new version publishes nothing. A version with a suffix, such as `0.2.0-rc.0`, goes to the `next` tag instead of `latest`.
|
|
101
|
+
|
|
102
|
+
npm accepts the publish because this repository's `publish.yml` is the package's trusted publisher, so there is no npm token to store or rotate. After publishing, a second job installs the new version from npm and checks it. If only that job fails, re-run it on its own.
|
|
103
|
+
|
|
104
|
+
## Scope and limitations
|
|
105
|
+
|
|
106
|
+
This first Worlds-backed implementation mirrors the demonstrated interaction model, not undisclosed internals. It does not yet provide authentication, authorization policy, schema validation, deletion UI, offline conflict resolution, or mutation history. Production applications must supply a properly scoped WorldsKit data source and enforce authorization in the Worlds service.
|
|
107
|
+
|
|
108
|
+
The later item graph, references, views, actions, services, mutations, modules, and fluid interaction roadmap remains intentionally separate from this compatibility foundation. See `ROADMAP.md` for the proposed next steps and the corresponding Lab Note references.
|
|
109
|
+
|
|
110
|
+
## Sources
|
|
111
|
+
|
|
112
|
+
- Alexander Obenauer, [LN 001: Composing application interfaces](https://alexanderobenauer.com/labnotes/001/), January 10, 2021.
|
|
113
|
+
- [Worlds documentation](https://docs.wazoo.dev/llms-full.txt).
|
|
114
|
+
- [Worlds TypeScript SDK documentation](https://docs.wazoo.dev/platform/typescript-sdk).
|
|
115
|
+
- [SPARQL 1.1 Query Language](https://www.w3.org/TR/sparql11-query/).
|
|
116
|
+
- [SPARQL 1.1 Update](https://www.w3.org/TR/sparql11-update/).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ReactElement, ReactNode } from "react";
|
|
2
|
+
type WorldsKitItem = {
|
|
3
|
+
id: string;
|
|
4
|
+
title?: string;
|
|
5
|
+
completed?: boolean;
|
|
6
|
+
orderIndex?: number;
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
};
|
|
9
|
+
type ItemUpdates = Record<string, unknown>;
|
|
10
|
+
type ItemTemplateProps = {
|
|
11
|
+
data?: WorldsKitItem;
|
|
12
|
+
onChange?: (updates: ItemUpdates) => void;
|
|
13
|
+
};
|
|
14
|
+
type DatasetProps = {
|
|
15
|
+
itemId?: string;
|
|
16
|
+
template: ReactElement<ItemTemplateProps>;
|
|
17
|
+
addButton?: ReactElement<{
|
|
18
|
+
onClick?: () => void;
|
|
19
|
+
}>;
|
|
20
|
+
isSource?: boolean;
|
|
21
|
+
onSelect?: (id: string) => void;
|
|
22
|
+
selectedId?: string | null;
|
|
23
|
+
className?: string;
|
|
24
|
+
emptyState?: ReactElement | string | null;
|
|
25
|
+
};
|
|
26
|
+
type WorldsKitSubscription = () => void;
|
|
27
|
+
type WorldsKitBinding = Record<string, unknown>;
|
|
28
|
+
type WorldsKitQueryResult<T = WorldsKitBinding> = {
|
|
29
|
+
results?: T[];
|
|
30
|
+
bindings?: T[];
|
|
31
|
+
};
|
|
32
|
+
type WorldsKitDataSourceOptions = {
|
|
33
|
+
signal?: AbortSignal;
|
|
34
|
+
worldId?: string;
|
|
35
|
+
};
|
|
36
|
+
type WorldsKitDataSource = {
|
|
37
|
+
query(sparql: string, options?: WorldsKitDataSourceOptions): Promise<unknown>;
|
|
38
|
+
mutate(update: string, options?: WorldsKitDataSourceOptions): Promise<unknown>;
|
|
39
|
+
subscribe?(sparql: string, onData: (result: unknown) => void, onError: (error: Error) => void, options?: WorldsKitDataSourceOptions): WorldsKitSubscription;
|
|
40
|
+
};
|
|
41
|
+
type WorldsKitState = {
|
|
42
|
+
worldId: string;
|
|
43
|
+
dataSource: WorldsKitDataSource;
|
|
44
|
+
activeSourceId: string | null;
|
|
45
|
+
setActiveSourceId: (id: string | null) => void;
|
|
46
|
+
};
|
|
47
|
+
export declare function WorldsKitApp({ worldId, dataSource, children }: {
|
|
48
|
+
worldId: string;
|
|
49
|
+
dataSource: WorldsKitDataSource;
|
|
50
|
+
children: ReactNode;
|
|
51
|
+
}): ReactNode;
|
|
52
|
+
export declare function useWorldsKit(): WorldsKitState;
|
|
53
|
+
export declare function useSelectedItem(): string | null;
|
|
54
|
+
export declare function useParentItem(): string | null;
|
|
55
|
+
export declare const ItemProvider: import("react").Provider<string | null>;
|
|
56
|
+
export declare function Checkbox({ itemId, datasetId, parentId }: {
|
|
57
|
+
itemId?: string;
|
|
58
|
+
datasetId?: string;
|
|
59
|
+
parentId?: string;
|
|
60
|
+
}): ReactNode;
|
|
61
|
+
export declare function Dataset(props: DatasetProps): ReactNode;
|
|
62
|
+
export declare function DatasetSortable(props: DatasetProps): ReactNode;
|
|
63
|
+
export declare function Detail({ children }: {
|
|
64
|
+
children: ReactNode;
|
|
65
|
+
}): ReactNode;
|
|
66
|
+
export declare function SimpleRow({ data, onChange, editable }: ItemTemplateProps & {
|
|
67
|
+
editable?: boolean;
|
|
68
|
+
}): ReactNode;
|
|
69
|
+
export declare function Source({ children }: {
|
|
70
|
+
children: ReactElement<DatasetProps>;
|
|
71
|
+
}): ReactNode;
|
|
72
|
+
export declare function TextField({ itemId, datasetId, parentId, placeholder, property }: {
|
|
73
|
+
itemId?: string;
|
|
74
|
+
datasetId?: string;
|
|
75
|
+
parentId?: string;
|
|
76
|
+
placeholder?: string;
|
|
77
|
+
property?: "title" | "value";
|
|
78
|
+
}): ReactNode;
|
|
79
|
+
export declare function Title(): ReactNode;
|
|
80
|
+
export declare function Todo({ data }: ItemTemplateProps): ReactNode;
|
|
81
|
+
type CreateDataSourceOptions = {
|
|
82
|
+
token?: string;
|
|
83
|
+
worldId?: string;
|
|
84
|
+
};
|
|
85
|
+
export declare function createWorldsKitDataSource(endpoint: string, tokenOrOptions?: string | CreateDataSourceOptions, options?: CreateDataSourceOptions): WorldsKitDataSource;
|
|
86
|
+
export type { DatasetProps, ItemTemplateProps, ItemUpdates, WorldsKitBinding, WorldsKitDataSource, WorldsKitItem, WorldsKitQueryResult, WorldsKitSubscription };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import { cloneElement, createContext, useContext, useEffect, useState } from "react";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
// src/context.tsx
|
|
4
|
+
const WorldsKitContext = createContext(null);
|
|
5
|
+
const ItemContext = createContext(null);
|
|
6
|
+
function WorldsKitApp({ worldId, dataSource, children }) {
|
|
7
|
+
const [activeSourceId, setActiveSourceId] = useState(null);
|
|
8
|
+
return /* @__PURE__ */ jsx(WorldsKitContext.Provider, {
|
|
9
|
+
value: {
|
|
10
|
+
worldId,
|
|
11
|
+
dataSource,
|
|
12
|
+
activeSourceId,
|
|
13
|
+
setActiveSourceId
|
|
14
|
+
},
|
|
15
|
+
children
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function useWorldsKit() {
|
|
19
|
+
const value = useContext(WorldsKitContext);
|
|
20
|
+
if (!value) throw new Error("WorldsKit components must be rendered inside WorldsKitApp.");
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
function useSelectedItem() {
|
|
24
|
+
return useContext(ItemContext);
|
|
25
|
+
}
|
|
26
|
+
function useParentItem() {
|
|
27
|
+
return useContext(ItemContext);
|
|
28
|
+
}
|
|
29
|
+
const ItemProvider = ItemContext.Provider;
|
|
30
|
+
// src/worlds.ts
|
|
31
|
+
const namespace = "https://kit.wazoo.dev/";
|
|
32
|
+
const xsdNamespace = "http://www.w3.org/2001/XMLSchema#";
|
|
33
|
+
function createWorldsKitDataSource(endpoint, tokenOrOptions, options) {
|
|
34
|
+
const token = typeof tokenOrOptions === "string" ? tokenOrOptions : tokenOrOptions?.token;
|
|
35
|
+
const defaultWorldId = typeof tokenOrOptions === "object" ? tokenOrOptions?.worldId : options?.worldId;
|
|
36
|
+
const requestHeaders = {
|
|
37
|
+
Accept: "application/sparql-results+json, application/json",
|
|
38
|
+
...token ? { Authorization: `Bearer ${token}` } : {}
|
|
39
|
+
};
|
|
40
|
+
const resolveUrl = (targetWorldId) => {
|
|
41
|
+
let url = endpoint.replace(/\/+$/, "");
|
|
42
|
+
if (!url.includes("/sparql")) {
|
|
43
|
+
const worldId = targetWorldId || defaultWorldId;
|
|
44
|
+
if (worldId) url = `${url}/worlds/${encodeURIComponent(worldId)}/sparql`;
|
|
45
|
+
}
|
|
46
|
+
return url;
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
async query(sparql, options) {
|
|
50
|
+
const url = resolveUrl(options?.worldId);
|
|
51
|
+
const isWorldsApi = url.includes("/worlds/");
|
|
52
|
+
const response = await fetch(url, {
|
|
53
|
+
method: "POST",
|
|
54
|
+
headers: {
|
|
55
|
+
...requestHeaders,
|
|
56
|
+
"Content-Type": isWorldsApi ? "application/json" : "application/sparql-query"
|
|
57
|
+
},
|
|
58
|
+
body: isWorldsApi ? JSON.stringify({ query: sparql }) : sparql,
|
|
59
|
+
signal: options?.signal
|
|
60
|
+
});
|
|
61
|
+
if (!response.ok) throw new Error(`Worlds query failed (${response.status})`);
|
|
62
|
+
return await response.json();
|
|
63
|
+
},
|
|
64
|
+
async mutate(update, options) {
|
|
65
|
+
const url = resolveUrl(options?.worldId);
|
|
66
|
+
const isWorldsApi = url.includes("/worlds/");
|
|
67
|
+
const response = await fetch(url, {
|
|
68
|
+
method: "POST",
|
|
69
|
+
headers: {
|
|
70
|
+
...requestHeaders,
|
|
71
|
+
Accept: "application/json",
|
|
72
|
+
"Content-Type": isWorldsApi ? "application/json" : "application/sparql-query"
|
|
73
|
+
},
|
|
74
|
+
body: isWorldsApi ? JSON.stringify({ query: update }) : update,
|
|
75
|
+
signal: options?.signal
|
|
76
|
+
});
|
|
77
|
+
if (!response.ok) throw new Error(`Worlds mutation failed (${response.status})`);
|
|
78
|
+
const responseText = await response.text();
|
|
79
|
+
return responseText ? JSON.parse(responseText) : void 0;
|
|
80
|
+
},
|
|
81
|
+
subscribe(sparql, onData, onError, options) {
|
|
82
|
+
let active = true;
|
|
83
|
+
const pollInterval = options?.pollIntervalMs ?? 3e3;
|
|
84
|
+
const run = async () => {
|
|
85
|
+
try {
|
|
86
|
+
const data = await this.query(sparql, options);
|
|
87
|
+
if (active) onData(data);
|
|
88
|
+
} catch (err) {
|
|
89
|
+
if (active && err.name !== "AbortError") onError(err);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
run();
|
|
93
|
+
const timer = setInterval(() => {
|
|
94
|
+
run();
|
|
95
|
+
}, pollInterval);
|
|
96
|
+
return () => {
|
|
97
|
+
active = false;
|
|
98
|
+
clearInterval(timer);
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function worldIri(worldId) {
|
|
104
|
+
return `${namespace}world/${encodeURIComponent(worldId)}`;
|
|
105
|
+
}
|
|
106
|
+
function itemIri(worldId, id) {
|
|
107
|
+
return `${worldIri(worldId)}/item/${encodeURIComponent(id)}`;
|
|
108
|
+
}
|
|
109
|
+
function datasetIri(worldId, id) {
|
|
110
|
+
return `${worldIri(worldId)}/dataset/${encodeURIComponent(id)}`;
|
|
111
|
+
}
|
|
112
|
+
function predicateIri(name) {
|
|
113
|
+
return `${namespace}${encodeURIComponent(name)}`;
|
|
114
|
+
}
|
|
115
|
+
function encodeIri(iri) {
|
|
116
|
+
return `<${iri.replace(/[<>\s]/g, "")}>`;
|
|
117
|
+
}
|
|
118
|
+
function literal(value) {
|
|
119
|
+
if (typeof value === "boolean") return `"${value}"^^<${xsdNamespace}boolean>`;
|
|
120
|
+
if (typeof value === "number" && Number.isFinite(value)) return `"${value}"^^<${xsdNamespace}${Number.isInteger(value) ? "integer" : "double"}>`;
|
|
121
|
+
if (value instanceof Date) return `"${value.toISOString()}"^^<${xsdNamespace}dateTime>`;
|
|
122
|
+
return JSON.stringify(String(value ?? ""));
|
|
123
|
+
}
|
|
124
|
+
function sparqlValue(value) {
|
|
125
|
+
if (typeof value === "string" && /^https?:\/\//.test(value)) return encodeIri(value);
|
|
126
|
+
return literal(value);
|
|
127
|
+
}
|
|
128
|
+
function queryBindings(result) {
|
|
129
|
+
if (!result) return [];
|
|
130
|
+
if (Array.isArray(result)) return result;
|
|
131
|
+
if (typeof result !== "object") return [];
|
|
132
|
+
const { results, bindings } = result;
|
|
133
|
+
if (typeof results === "object" && results !== null && Array.isArray(results.bindings)) return results.bindings;
|
|
134
|
+
if (Array.isArray(results)) return results;
|
|
135
|
+
if (Array.isArray(bindings)) return bindings;
|
|
136
|
+
return [];
|
|
137
|
+
}
|
|
138
|
+
function bindingValue(value) {
|
|
139
|
+
if (typeof value === "object" && value !== null && "value" in value) return value.value;
|
|
140
|
+
return value;
|
|
141
|
+
}
|
|
142
|
+
// src/store.ts
|
|
143
|
+
function itemQuery(worldId, parentId, datasetId) {
|
|
144
|
+
const parent = encodeIri(itemIri(worldId, parentId));
|
|
145
|
+
const dataset = encodeIri(datasetIri(worldId, datasetId));
|
|
146
|
+
return `SELECT ?id ?title ?completed ?orderIndex ?value WHERE { ?id <${predicateIri("inDataset")}> ${dataset} ; <${predicateIri("parent")}> ${parent} . OPTIONAL { ?id <${predicateIri("title")}> ?title } OPTIONAL { ?id <${predicateIri("completed")}> ?completed } OPTIONAL { ?id <${predicateIri("orderIndex")}> ?orderIndex } OPTIONAL { ?id <${predicateIri("value")}> ?value } } ORDER BY COALESCE(?orderIndex, 0)`;
|
|
147
|
+
}
|
|
148
|
+
function normalizeTerm(value) {
|
|
149
|
+
return String(bindingValue(value) ?? "");
|
|
150
|
+
}
|
|
151
|
+
function itemIdFromIri(value, _worldId) {
|
|
152
|
+
const rawId = normalizeTerm(value).replace(/^<|>$/g, "");
|
|
153
|
+
const markerIndex = rawId.indexOf("/item/");
|
|
154
|
+
const id = markerIndex >= 0 ? rawId.slice(markerIndex + 6) : rawId.split("/").pop() ?? rawId;
|
|
155
|
+
return decodeURIComponent(id);
|
|
156
|
+
}
|
|
157
|
+
function normalizeItem(row, worldId) {
|
|
158
|
+
const itemId = itemIdFromIri(row.id ?? row.item ?? row.subject, worldId);
|
|
159
|
+
const title = row.title === void 0 ? void 0 : normalizeTerm(row.title);
|
|
160
|
+
const completed = row.completed === void 0 ? void 0 : normalizeTerm(row.completed) === "true";
|
|
161
|
+
const orderIndex = row.orderIndex === void 0 ? void 0 : Number(normalizeTerm(row.orderIndex));
|
|
162
|
+
const value = row.value === void 0 ? void 0 : bindingValue(row.value);
|
|
163
|
+
return {
|
|
164
|
+
id: decodeURIComponent(itemId),
|
|
165
|
+
title,
|
|
166
|
+
completed,
|
|
167
|
+
orderIndex,
|
|
168
|
+
value
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function subscribeToDataset(dataSource, worldId, parentId, datasetId, onItems, onError) {
|
|
172
|
+
const sparql = itemQuery(worldId, parentId, datasetId);
|
|
173
|
+
const processResult = (result) => {
|
|
174
|
+
const items = queryBindings(result).map((row) => normalizeItem(row, worldId));
|
|
175
|
+
items.sort((a, b) => (a.orderIndex ?? 0) - (b.orderIndex ?? 0));
|
|
176
|
+
onItems(items);
|
|
177
|
+
};
|
|
178
|
+
if (dataSource.subscribe) return dataSource.subscribe(sparql, processResult, onError, { worldId });
|
|
179
|
+
let active = true;
|
|
180
|
+
const controller = new AbortController();
|
|
181
|
+
dataSource.query(sparql, {
|
|
182
|
+
worldId,
|
|
183
|
+
signal: controller.signal
|
|
184
|
+
}).then((result) => {
|
|
185
|
+
if (active) processResult(result);
|
|
186
|
+
}).catch((error) => {
|
|
187
|
+
if (active && error.name !== "AbortError") onError(error);
|
|
188
|
+
});
|
|
189
|
+
return () => {
|
|
190
|
+
active = false;
|
|
191
|
+
controller.abort();
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function addDatasetItem(dataSource, worldId, parentId, datasetId, orderIndex) {
|
|
195
|
+
const id = crypto.randomUUID();
|
|
196
|
+
const subject = encodeIri(itemIri(worldId, id));
|
|
197
|
+
const parent = encodeIri(itemIri(worldId, parentId));
|
|
198
|
+
const dataset = encodeIri(datasetIri(worldId, datasetId));
|
|
199
|
+
const insert = `INSERT DATA { ${subject} <${predicateIri("inDataset")}> ${dataset} ; <${predicateIri("parent")}> ${parent} ; <${predicateIri("title")}> "" ; <${predicateIri("completed")}> ${literal(false)} ; <${predicateIri("orderIndex")}> ${literal(orderIndex)} ; <${predicateIri("createdAt")}> ${literal(/* @__PURE__ */ new Date())} }`;
|
|
200
|
+
return dataSource.mutate(insert, { worldId }).then(() => ({ id }));
|
|
201
|
+
}
|
|
202
|
+
function updateDatasetItem(dataSource, worldId, _parentId, _datasetId, id, updates) {
|
|
203
|
+
return updateItem(dataSource, worldId, id, updates);
|
|
204
|
+
}
|
|
205
|
+
function updateItem(dataSource, worldId, id, updates) {
|
|
206
|
+
const subject = encodeIri(itemIri(worldId, id));
|
|
207
|
+
const entries = Object.entries(updates);
|
|
208
|
+
if (entries.length === 0) return Promise.resolve(void 0);
|
|
209
|
+
const deletePatterns = entries.map(([key], index) => `${subject} <${predicateIri(key)}> ?old${index} .`).join(" ");
|
|
210
|
+
const insertPatterns = entries.map(([key, value]) => `${subject} <${predicateIri(key)}> ${sparqlValue(value)} .`).join(" ");
|
|
211
|
+
const optionalPatterns = entries.map(([key], index) => `OPTIONAL { ${subject} <${predicateIri(key)}> ?old${index} }`).join(" ");
|
|
212
|
+
return dataSource.mutate(`DELETE { ${deletePatterns} } INSERT { ${insertPatterns} } WHERE { ${optionalPatterns} }`, { worldId });
|
|
213
|
+
}
|
|
214
|
+
function updateDatasetOrder(dataSource, worldId, _parentId, _datasetId, ids) {
|
|
215
|
+
return Promise.all(ids.map((id, index) => updateItem(dataSource, worldId, id, { orderIndex: index })));
|
|
216
|
+
}
|
|
217
|
+
function subscribeToDatasetItem(dataSource, worldId, parentId, datasetId, id, onItem, onError) {
|
|
218
|
+
return subscribeToDataset(dataSource, worldId, parentId, datasetId, (items) => onItem(items.find((item) => item.id === id) ?? null), onError);
|
|
219
|
+
}
|
|
220
|
+
// src/components/useBoundItem.ts
|
|
221
|
+
function useBoundItem(itemId, datasetId, parentId) {
|
|
222
|
+
const { dataSource, worldId } = useWorldsKit();
|
|
223
|
+
const [item, setItem] = useState({ id: itemId });
|
|
224
|
+
useEffect(() => {
|
|
225
|
+
return subscribeToDatasetItem(dataSource, worldId, parentId, datasetId, itemId, (value) => value && setItem(value), () => {});
|
|
226
|
+
}, [
|
|
227
|
+
dataSource,
|
|
228
|
+
datasetId,
|
|
229
|
+
itemId,
|
|
230
|
+
parentId,
|
|
231
|
+
worldId
|
|
232
|
+
]);
|
|
233
|
+
return {
|
|
234
|
+
item,
|
|
235
|
+
update: (updates) => updateDatasetItem(dataSource, worldId, parentId, datasetId, itemId, updates)
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
// src/components/Checkbox.tsx
|
|
239
|
+
function Checkbox({ itemId, datasetId = "lists", parentId = "root" }) {
|
|
240
|
+
const contextItemId = useParentItem();
|
|
241
|
+
const targetId = itemId ?? contextItemId;
|
|
242
|
+
if (!targetId) return null;
|
|
243
|
+
const { item, update } = useBoundItem(targetId, datasetId, parentId);
|
|
244
|
+
const isChecked = Boolean(item.completed ?? item.value);
|
|
245
|
+
return /* @__PURE__ */ jsx("input", {
|
|
246
|
+
type: "checkbox",
|
|
247
|
+
checked: isChecked,
|
|
248
|
+
onChange: (event) => void update({
|
|
249
|
+
completed: event.target.checked,
|
|
250
|
+
value: event.target.checked
|
|
251
|
+
})
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
// src/components/useItems.ts
|
|
255
|
+
function useItems(datasetId, parentId) {
|
|
256
|
+
const { worldId, dataSource } = useWorldsKit();
|
|
257
|
+
const resolvedParentId = parentId ?? "root";
|
|
258
|
+
const [items, setItems] = useState([]);
|
|
259
|
+
const [error, setError] = useState(null);
|
|
260
|
+
useEffect(() => {
|
|
261
|
+
setError(null);
|
|
262
|
+
return subscribeToDataset(dataSource, worldId, resolvedParentId, datasetId, setItems, setError);
|
|
263
|
+
}, [
|
|
264
|
+
dataSource,
|
|
265
|
+
datasetId,
|
|
266
|
+
resolvedParentId,
|
|
267
|
+
worldId
|
|
268
|
+
]);
|
|
269
|
+
return {
|
|
270
|
+
items,
|
|
271
|
+
error
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
// src/components/Dataset.tsx
|
|
275
|
+
function DatasetView({ template, addButton, itemId = "default", isSource, onSelect, selectedId, sortable = false, className = "", emptyState = null }) {
|
|
276
|
+
const parentId = useParentItem();
|
|
277
|
+
const { worldId, dataSource } = useWorldsKit();
|
|
278
|
+
const { items, error } = useItems(itemId, parentId);
|
|
279
|
+
const [draggedId, setDraggedId] = useState(null);
|
|
280
|
+
const add = async () => {
|
|
281
|
+
await addDatasetItem(dataSource, worldId, parentId ?? "root", itemId, items.length);
|
|
282
|
+
};
|
|
283
|
+
const update = async (id, updates) => {
|
|
284
|
+
await updateDatasetItem(dataSource, worldId, parentId ?? "root", itemId, id, updates);
|
|
285
|
+
};
|
|
286
|
+
const reorder = async (fromId, toId) => {
|
|
287
|
+
if (fromId === toId) return;
|
|
288
|
+
const fromIndex = items.findIndex((item) => item.id === fromId);
|
|
289
|
+
const toIndex = items.findIndex((item) => item.id === toId);
|
|
290
|
+
if (fromIndex < 0 || toIndex < 0) return;
|
|
291
|
+
const next = [...items];
|
|
292
|
+
const [moved] = next.splice(fromIndex, 1);
|
|
293
|
+
if (!moved) return;
|
|
294
|
+
next.splice(toIndex, 0, moved);
|
|
295
|
+
await updateDatasetOrder(dataSource, worldId, parentId ?? "root", itemId, next.map((item) => item.id));
|
|
296
|
+
};
|
|
297
|
+
if (error) return /* @__PURE__ */ jsxs("div", {
|
|
298
|
+
role: "alert",
|
|
299
|
+
children: ["Unable to load this dataset: ", error.message]
|
|
300
|
+
});
|
|
301
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
302
|
+
className: `worlds-kit-dataset ${className}`,
|
|
303
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
304
|
+
className: "worlds-kit-dataset-items",
|
|
305
|
+
children: [items.length === 0 && emptyState, items.map((item) => /* @__PURE__ */ jsx("div", {
|
|
306
|
+
className: "worlds-kit-item",
|
|
307
|
+
"data-selected": selectedId === item.id,
|
|
308
|
+
draggable: sortable,
|
|
309
|
+
onDragStart: () => setDraggedId(item.id),
|
|
310
|
+
onDragOver: (event) => sortable && event.preventDefault(),
|
|
311
|
+
onDrop: () => draggedId && void reorder(draggedId, item.id),
|
|
312
|
+
onClick: () => isSource && onSelect?.(item.id),
|
|
313
|
+
children: cloneElement(template, {
|
|
314
|
+
data: item,
|
|
315
|
+
onChange: (updates) => void update(item.id, updates)
|
|
316
|
+
})
|
|
317
|
+
}, item.id))]
|
|
318
|
+
}), addButton && cloneElement(addButton, { onClick: add })]
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
function Dataset(props) {
|
|
322
|
+
return /* @__PURE__ */ jsx(DatasetView, { ...props });
|
|
323
|
+
}
|
|
324
|
+
function DatasetSortable(props) {
|
|
325
|
+
return /* @__PURE__ */ jsx(DatasetView, {
|
|
326
|
+
...props,
|
|
327
|
+
sortable: true
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
// src/components/Detail.tsx
|
|
331
|
+
function Detail({ children }) {
|
|
332
|
+
const { activeSourceId } = useWorldsKit();
|
|
333
|
+
if (!activeSourceId) return /* @__PURE__ */ jsx("div", {
|
|
334
|
+
className: "worlds-kit-empty-state",
|
|
335
|
+
children: "Select an item..."
|
|
336
|
+
});
|
|
337
|
+
return /* @__PURE__ */ jsx(ItemProvider, {
|
|
338
|
+
value: activeSourceId,
|
|
339
|
+
children
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
// src/components/SimpleRow.tsx
|
|
343
|
+
function SimpleRow({ data, onChange, editable = false }) {
|
|
344
|
+
if (!data) return null;
|
|
345
|
+
return /* @__PURE__ */ jsx("div", {
|
|
346
|
+
className: "simple-row",
|
|
347
|
+
children: editable ? /* @__PURE__ */ jsx("input", {
|
|
348
|
+
value: data.title ?? "",
|
|
349
|
+
onChange: (event) => void onChange?.({ title: event.target.value })
|
|
350
|
+
}) : /* @__PURE__ */ jsx("span", { children: data.title ?? "" })
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
// src/components/Source.tsx
|
|
354
|
+
function Source({ children }) {
|
|
355
|
+
const { activeSourceId, setActiveSourceId } = useWorldsKit();
|
|
356
|
+
return cloneElement(children, {
|
|
357
|
+
isSource: true,
|
|
358
|
+
selectedId: activeSourceId,
|
|
359
|
+
onSelect: setActiveSourceId
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
// src/components/TextField.tsx
|
|
363
|
+
function TextField({ itemId, datasetId = "lists", parentId = "root", placeholder = "", property = "title" }) {
|
|
364
|
+
const contextItemId = useParentItem();
|
|
365
|
+
const targetId = itemId ?? contextItemId;
|
|
366
|
+
if (!targetId) return null;
|
|
367
|
+
const { item, update } = useBoundItem(targetId, datasetId, parentId);
|
|
368
|
+
const currentValue = property === "title" ? item.title ?? "" : String(item.value ?? "");
|
|
369
|
+
return /* @__PURE__ */ jsx("input", {
|
|
370
|
+
value: currentValue,
|
|
371
|
+
placeholder,
|
|
372
|
+
onChange: (event) => void update(property === "title" ? { title: event.target.value } : { value: event.target.value })
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
// src/components/Title.tsx
|
|
376
|
+
function Title() {
|
|
377
|
+
const id = useParentItem();
|
|
378
|
+
if (!id) return null;
|
|
379
|
+
const { item } = useBoundItem(id, "lists", "root");
|
|
380
|
+
return /* @__PURE__ */ jsx("h1", { children: item.title ?? "" });
|
|
381
|
+
}
|
|
382
|
+
// src/components/Todo.tsx
|
|
383
|
+
function Todo({ data }) {
|
|
384
|
+
if (!data) return null;
|
|
385
|
+
return /* @__PURE__ */ jsx(ItemProvider, {
|
|
386
|
+
value: data.id,
|
|
387
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
388
|
+
className: "todo-row",
|
|
389
|
+
"data-completed": Boolean(data.completed),
|
|
390
|
+
children: [/* @__PURE__ */ jsx(Checkbox, { itemId: data.id }), /* @__PURE__ */ jsx(TextField, {
|
|
391
|
+
itemId: data.id,
|
|
392
|
+
property: "title"
|
|
393
|
+
})]
|
|
394
|
+
})
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
export { Checkbox, Dataset, DatasetSortable, Detail, ItemProvider, SimpleRow, Source, TextField, Title, Todo, WorldsKitApp, createWorldsKitDataSource, useParentItem, useSelectedItem, useWorldsKit };
|
package/package.json
CHANGED
|
@@ -1,6 +1,69 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wazootech/worlds-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "React components for apps on Wazoo Worlds: lists, detail views and editable fields that load and save their own data.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"wazoo",
|
|
7
|
+
"worlds",
|
|
8
|
+
"react",
|
|
9
|
+
"components",
|
|
10
|
+
"knowledge-graph",
|
|
11
|
+
"rdf",
|
|
12
|
+
"sparql"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/wazootech/worlds-kit#readme",
|
|
15
|
+
"bugs": "https://github.com/wazootech/worlds-kit/issues",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/wazootech/worlds-kit.git"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"type": "module",
|
|
22
|
+
"packageManager": "bun@1.4.4",
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=22"
|
|
25
|
+
},
|
|
26
|
+
"workspaces": [
|
|
27
|
+
"demo"
|
|
28
|
+
],
|
|
29
|
+
"files": [
|
|
30
|
+
"dist/*.js",
|
|
31
|
+
"dist/*.d.ts"
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"default": "./dist/index.js"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"check": "tsc --noEmit -p tsconfig.json",
|
|
44
|
+
"format": "prettier \"src/**/*.{ts,tsx}\" \"tests/**/*.{ts,tsx}\" \"scripts/*\" tsdown.config.ts --write",
|
|
45
|
+
"format:check": "prettier \"src/**/*.{ts,tsx}\" \"tests/**/*.{ts,tsx}\" \"scripts/*\" tsdown.config.ts --check",
|
|
46
|
+
"lint": "eslint . --max-warnings 0",
|
|
47
|
+
"build": "tsdown",
|
|
48
|
+
"test": "bun test tests/*.test.ts",
|
|
49
|
+
"check:package": "bun scripts/check-package.ts",
|
|
50
|
+
"ci": "bun run format:check && bun run lint && bun run check && bun run test && bun run build"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"react": ">=18",
|
|
54
|
+
"react-dom": ">=18"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@eslint/js": "^9.20.0",
|
|
58
|
+
"@types/node": "^24.0.0",
|
|
59
|
+
"@types/react": "^19.2.18",
|
|
60
|
+
"@types/react-dom": "^19.2.4",
|
|
61
|
+
"eslint": "^9.20.0",
|
|
62
|
+
"prettier": "^3.9.5",
|
|
63
|
+
"react": "^19.1.0",
|
|
64
|
+
"react-dom": "^19.1.0",
|
|
65
|
+
"tsdown": "0.23.7",
|
|
66
|
+
"typescript": "^5.8.3",
|
|
67
|
+
"typescript-eslint": "^8.24.0"
|
|
68
|
+
}
|
|
69
|
+
}
|