@dropsh/plugin-canvas 0.0.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 ADDED
@@ -0,0 +1,51 @@
1
+ drupal-cli is dual-licensed. You may use this software under the terms of
2
+ either the MIT License or the GNU General Public License version 2 or any
3
+ later version, at your option.
4
+
5
+ SPDX-License-Identifier: (MIT OR GPL-2.0-or-later)
6
+
7
+ ================================================================================
8
+ MIT License
9
+ ================================================================================
10
+
11
+ Copyright (c) 2026 Christian Wiedemann
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ of this software and associated documentation files (the "Software"), to deal
15
+ in the Software without restriction, including without limitation the rights
16
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ copies of the Software, and to permit persons to whom the Software is
18
+ furnished to do so, subject to the following conditions:
19
+
20
+ The above copyright notice and this permission notice shall be included in all
21
+ copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
+ SOFTWARE.
30
+
31
+ ================================================================================
32
+ GNU General Public License, version 2 or later
33
+ ================================================================================
34
+
35
+ The full text of the GNU GPL v2 is available at:
36
+ https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
37
+
38
+ The full text of any later GPL version is available at:
39
+ https://www.gnu.org/licenses/
40
+
41
+ Copyright (C) 2026 Christian Wiedemann
42
+
43
+ This program is free software; you can redistribute it and/or modify it under
44
+ the terms of the GNU General Public License as published by the Free Software
45
+ Foundation; either version 2 of the License, or (at your option) any later
46
+ version.
47
+
48
+ This program is distributed in the hope that it will be useful, but WITHOUT
49
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
50
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
51
+ details.
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # @dropsh/plugin-canvas
2
+
3
+ Canvas component schemas for [dropsh](https://github.com/pen-drop/dropsh), the
4
+ entity-agnostic CLI for Drupal 11 JSON:API.
5
+
6
+ Extends the `dropsh schema` output for Canvas pages with a typed
7
+ `attributes.components` array built from the site's Single Directory Components
8
+ (SDC), so `create` / `update` payloads for `canvas_page` can be validated against
9
+ the real component set.
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ pnpm add @dropsh/plugin-canvas
15
+ ```
16
+
17
+ `dropsh` is a peer dependency. The plugin depends on `@dropsh/sdc-client`
18
+ (installed automatically).
19
+
20
+ ## Usage
21
+
22
+ ```js
23
+ import { canvasPlugin } from "@dropsh/plugin-canvas";
24
+
25
+ export default {
26
+ site: { base_url: "https://my-drupal.example.com", jsonapi_prefix: "/jsonapi" },
27
+ plugins: [canvasPlugin()],
28
+ };
29
+ ```
30
+
31
+ ```bash
32
+ dropsh schema canvas_page/canvas_page --for=create
33
+ ```
34
+
35
+ The returned JSON Schema includes `x-dropsh-builder: "canvas"`,
36
+ `x-dropsh-components`, and a typed `attributes.components` array. If `jsonapi_sdc`
37
+ is missing or inaccessible, the Canvas schema **fails** (rather than returning
38
+ incomplete component information) so you never validate against a partial set.
39
+
40
+ ## Required Drupal modules
41
+
42
+ - `canvas`
43
+ - `jsonapi_sdc`
44
+
45
+ ## License
46
+
47
+ MIT OR GPL-2.0-or-later
@@ -0,0 +1,87 @@
1
+ import { toSdcComponentId } from "@dropsh/sdc-client";
2
+ function cloneValue(value) {
3
+ return JSON.parse(JSON.stringify(value));
4
+ }
5
+ function cloneSchema(schema) {
6
+ return cloneValue(schema);
7
+ }
8
+ function ensureObjectProperty(parent, key) {
9
+ parent.properties ??= {};
10
+ parent.properties[key] ??= { type: "object" };
11
+ const child = parent.properties[key];
12
+ child.type ??= "object";
13
+ child.properties ??= {};
14
+ return child;
15
+ }
16
+ function slotNames(components) {
17
+ return [...new Set(components.flatMap((component) => Object.keys(component.slots)))].sort();
18
+ }
19
+ function inputSchema(component) {
20
+ const props = Object.keys(component.props).length > 0
21
+ ? cloneValue(component.props)
22
+ : { properties: {}, additionalProperties: true };
23
+ return {
24
+ ...props,
25
+ type: "object",
26
+ description: `Inputs for ${toSdcComponentId(component.id)} (${component.name})`,
27
+ };
28
+ }
29
+ function slotSchema(knownSlots) {
30
+ return {
31
+ type: ["string", "null"],
32
+ enum: knownSlots.length > 0 ? [null, ...knownSlots] : [null],
33
+ description: knownSlots.length > 0
34
+ ? `Known SDC slot names: ${knownSlots.join(", ")}. Use null for root components.`
35
+ : "No SDC slots were reported by jsonapi_sdc. Use null for root components.",
36
+ };
37
+ }
38
+ function componentItemVariant(component, knownSlots) {
39
+ return {
40
+ type: "object",
41
+ additionalProperties: false,
42
+ properties: {
43
+ uuid: { type: "string", format: "uuid" },
44
+ component_id: { type: "string", const: toSdcComponentId(component.id) },
45
+ parent_uuid: { type: ["string", "null"], format: "uuid" },
46
+ slot: slotSchema(knownSlots),
47
+ inputs: inputSchema(component),
48
+ label: { type: ["string", "null"] },
49
+ },
50
+ required: ["uuid", "component_id", "inputs"],
51
+ };
52
+ }
53
+ function componentMetadata(component) {
54
+ return {
55
+ id: toSdcComponentId(component.id),
56
+ source_id: component.id,
57
+ name: component.name,
58
+ description: component.description,
59
+ provider: component.provider,
60
+ status: component.status,
61
+ props: cloneValue(component.props),
62
+ slots: cloneValue(component.slots),
63
+ variants: cloneValue(component.variants),
64
+ };
65
+ }
66
+ export function extendCanvasSchema(baseSchema, operation, components) {
67
+ const schema = cloneSchema(baseSchema);
68
+ const data = ensureObjectProperty(schema, "data");
69
+ if (operation === "update") {
70
+ data.required = ["type", "id"];
71
+ }
72
+ const attributes = ensureObjectProperty(data, "attributes");
73
+ const knownSlots = slotNames(components);
74
+ const componentItemVariants = [...components]
75
+ .sort((a, b) => toSdcComponentId(a.id).localeCompare(toSdcComponentId(b.id)))
76
+ .map((component) => componentItemVariant(component, knownSlots));
77
+ attributes.properties.components = {
78
+ type: "array",
79
+ description: "Canvas component tree. Root components use parent_uuid=null and slot=null. Child components set parent_uuid to another component uuid and slot to one of the parent's slot names.",
80
+ items: {
81
+ oneOf: componentItemVariants,
82
+ },
83
+ };
84
+ schema["x-dropsh-builder"] = "canvas";
85
+ schema["x-dropsh-components"] = components.map(componentMetadata);
86
+ return schema;
87
+ }
@@ -0,0 +1,28 @@
1
+ import { fetchSdcComponents } from "@dropsh/sdc-client";
2
+ import { HttpError } from "dropsh/plugin";
3
+ import { extendCanvasSchema } from "./canvas-schema.js";
4
+ export function canvasPlugin() {
5
+ return {
6
+ id: "canvas",
7
+ requiredModules: ["canvas", "jsonapi_sdc"],
8
+ async extendSchema(_entityType, _bundle, schema) {
9
+ return schema;
10
+ },
11
+ async extendOperationSchema(entityType, bundle, operation, schema, ctx) {
12
+ if (entityType !== "canvas_page" || bundle !== "canvas_page") {
13
+ return schema;
14
+ }
15
+ let components;
16
+ try {
17
+ components = await fetchSdcComponents(ctx);
18
+ }
19
+ catch (err) {
20
+ if (err instanceof HttpError && err.status === 404) {
21
+ throw new HttpError(404, "Canvas plugin requires Drupal module jsonapi_sdc to build component schemas.", err.body);
22
+ }
23
+ throw err;
24
+ }
25
+ return extendCanvasSchema(schema, operation, components);
26
+ },
27
+ };
28
+ }
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@dropsh/plugin-canvas",
3
+ "version": "0.0.2",
4
+ "type": "module",
5
+ "main": "./dist/plugins/canvas/src/index.js",
6
+ "exports": {
7
+ ".": "./dist/plugins/canvas/src/index.js"
8
+ },
9
+ "files": [
10
+ "dist/plugins/canvas/src",
11
+ "README.md"
12
+ ],
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "peerDependencies": {
17
+ "dropsh": "*"
18
+ },
19
+ "dependencies": {
20
+ "@dropsh/sdc-client": "0.0.2"
21
+ },
22
+ "devDependencies": {
23
+ "typescript": "^5.6.0",
24
+ "vitest": "^4.1.8",
25
+ "dropsh": "0.0.2"
26
+ },
27
+ "scripts": {
28
+ "build": "tsc",
29
+ "test": "vitest run --passWithNoTests",
30
+ "typecheck": "tsc --noEmit"
31
+ }
32
+ }