Export template source from 4c88728f144add3457b4273d4b9a04ee53c53be3:quixos-instance/packages/web-studio-canvas-runtime
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { objectRef, type QxObjectRef } from "@quixos/camino-package-runtime";
|
||||
import type { Contexts } from "./gen/qx.js";
|
||||
|
||||
export type PlacementInput = {
|
||||
canvas_id: QxObjectRef;
|
||||
subject_id: QxObjectRef;
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
z: number;
|
||||
};
|
||||
|
||||
const finiteNumber = (value: unknown, fallback: number) =>
|
||||
typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
||||
|
||||
const objectId = (value: unknown, field: string) => {
|
||||
try {
|
||||
return objectRef(value as QxObjectRef);
|
||||
} catch {
|
||||
throw new Error(`${field} must be a managed object reference`);
|
||||
}
|
||||
};
|
||||
|
||||
export const normalizePlacementInput = (input: Record<string, unknown>): PlacementInput => ({
|
||||
canvas_id: objectId(input.canvas_id, "canvas_id"),
|
||||
subject_id: objectId(input.subject_id, "subject_id"),
|
||||
x: finiteNumber(input.x, 0),
|
||||
y: finiteNumber(input.y, 0),
|
||||
width: Math.max(220, finiteNumber(input.width, 420)),
|
||||
height: Math.max(150, finiteNumber(input.height, 320)),
|
||||
z: Math.trunc(finiteNumber(input.z, 0)),
|
||||
});
|
||||
|
||||
export const createCanvas = ({ objectId }: Contexts["createCanvas"]) => objectId;
|
||||
|
||||
export const createPlacement = async (context: Contexts["createPlacement"]) => {
|
||||
const input = normalizePlacementInput(context.input);
|
||||
await Promise.all([
|
||||
context.ports.placement["position.set"]({ x: input.x, y: input.y }),
|
||||
context.ports.placement["size.set"]({ width: input.width, height: input.height }),
|
||||
context.ports.placement["z.set"](input.z),
|
||||
]);
|
||||
await context.ports.placement["subject.connect"](context.input.subject_id);
|
||||
await context.ports.placement["canvas.connect"](context.input.canvas_id);
|
||||
return context.objectId;
|
||||
};
|
||||
+246
File diff suppressed because one or more lines are too long
@@ -0,0 +1,2 @@
|
||||
// This adapter keeps the implementation file hand-authored.
|
||||
export { createCanvas as handler } from "../canvas.impl.js";
|
||||
@@ -0,0 +1,2 @@
|
||||
// This adapter keeps the implementation file hand-authored.
|
||||
export { createPlacement as handler } from "../canvas.impl.js";
|
||||
@@ -0,0 +1,18 @@
|
||||
import { encodeQxValue, decodeQxValue, type MessageBinding, type QxValueType } from "@quixos/camino-package-runtime";
|
||||
|
||||
const position: QxValueType = {
|
||||
kind: "record",
|
||||
fields: { x: { kind: "scalar", name: "double" }, y: { kind: "scalar", name: "double" } },
|
||||
};
|
||||
const size: QxValueType = {
|
||||
kind: "record",
|
||||
fields: { width: { kind: "scalar", name: "double" }, height: { kind: "scalar", name: "double" } },
|
||||
};
|
||||
export const positionBinding: MessageBinding<{ x: number; y: number }> = {
|
||||
encode: (value) => encodeQxValue(position, value, {}),
|
||||
decode: (value) => decodeQxValue(position, value, {}),
|
||||
};
|
||||
export const sizeBinding: MessageBinding<{ width: number; height: number }> = {
|
||||
encode: (value) => encodeQxValue(size, value, {}),
|
||||
decode: (value) => decodeQxValue(size, value, {}),
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
// Generated by qx-scaffold-v1
|
||||
import { serveMigration } from "@quixos/camino-package-runtime";
|
||||
await serveMigration({});
|
||||
@@ -0,0 +1,11 @@
|
||||
// Generated by qx-scaffold-v1
|
||||
import { servePackageRuntime } from "@quixos/camino-package-runtime";
|
||||
import { createRuntime } from "./gen/qx.js";
|
||||
import { handler as impl0 } from "./impl/createCanvas.js";
|
||||
import { handler as impl1 } from "./impl/createPlacement.js";
|
||||
servePackageRuntime(
|
||||
createRuntime({
|
||||
createCanvas: impl0,
|
||||
createPlacement: impl1,
|
||||
}),
|
||||
);
|
||||
Reference in New Issue
Block a user