@kubohiroya/turbowarp-aframe 0.8.0 → 0.9.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/src/extension.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import {fixedInputRegistration} from './fixed-input-editors.js';
1
2
  import {registerSourceEditors} from './source-editors.js';
2
3
  import {validateTemplateNode, type TemplateNode} from './template-validation.js';
3
4
  import {extensionConfig} from './config';
@@ -199,13 +200,17 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
199
200
  if (runtime) runtime[runtimeCapabilityKey] = this.runtimeCapability;
200
201
  }
201
202
 
203
+ private readonly fixedEditors = fixedInputRegistration(Scratch);
204
+
202
205
  public getInfo(): Record<string, unknown> {
206
+ const blocks = blockDefinitions.map((block) => this.toScratchBlock(block));
207
+ this.fixedEditors.ensure(blocks.map(block => `${extensionConfig.id}_${block.opcode}`));
203
208
  return {
204
209
  id: extensionConfig.id,
205
210
  name: Scratch.translate(definitions.extensionName),
206
211
  docsURI: extensionConfig.docsURI,
207
212
  blockIconURI: extensionConfig.blockIconURI,
208
- blocks: blockDefinitions.map((block) => this.toScratchBlock(block))
213
+ blocks
209
214
  };
210
215
  }
211
216
 
@@ -633,6 +638,7 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
633
638
  }
634
639
 
635
640
  public dispose(): void {
641
+ this.fixedEditors.dispose();
636
642
  if (this.disposed) return;
637
643
  this.disposed = true;
638
644
  this.handleSourceEditorDispose();
@@ -0,0 +1,142 @@
1
+ import {parseFixedNumberInput} from '@kubohiroya/turbowarp-extension-data-editor';
2
+ import type {FixedInputBinding, FixedInputEditorOptions, JsonValue, LiteralValue} from '@kubohiroya/turbowarp-extension-data-editor';
3
+ import {fixedInputBundle} from './fixed-input-schema.js';
4
+
5
+ const existingNumber = (value: LiteralValue): number => {
6
+ if ((typeof value !== 'number' && typeof value !== 'string') ||
7
+ (typeof value === 'string' && value.trim() === '')) throw new TypeError('Expected a numeric literal.');
8
+ const number = Number(value);
9
+ if (!Number.isFinite(number)) throw new TypeError('Expected a finite number.');
10
+ return number;
11
+ };
12
+ const newNumber = (value: JsonValue): string => {
13
+ const parsed = parseFixedNumberInput(value);
14
+ if (parsed.kind !== 'valid') throw new TypeError('Invalid new numeric input.');
15
+ return parsed.token;
16
+ };
17
+
18
+ function binding(opcode: string, argument: string, values?: readonly string[]): FixedInputBinding {
19
+ return {argument, schemaBundle: fixedInputBundle(opcode, argument),
20
+ control: values ? {kind: 'enum', choices: values.map(value => ({value}))} : {kind: 'number'},
21
+ read(value) {
22
+ if (!values) return existingNumber(value);
23
+ if (typeof value !== 'string') throw new TypeError('Expected a string.');
24
+ return projectEnum(opcode, argument, value);
25
+ },
26
+ encode(value) {
27
+ if (!values) return newNumber(value);
28
+ if (typeof value !== 'string' || !values.includes(value)) throw new TypeError('Invalid selection.');
29
+ return value;
30
+ },
31
+ validateCandidate(value) {
32
+ try {
33
+ if (!values) existingNumber(value);
34
+ else if (typeof value !== 'string' || !values.includes(value)) throw new TypeError('Invalid selection.');
35
+ return [];
36
+ } catch {return [{message: 'Invalid fixed input.'}];}
37
+ }};
38
+ }
39
+
40
+ function projectEnum(_opcode: string, argument: string, value: string): string {
41
+ return argument === 'UNIT' ? value.trim().toLowerCase() : value.trim();
42
+ }
43
+
44
+ export const fixedInputEditors: readonly FixedInputEditorOptions[] = [
45
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-createScene', opcodes: ['turbowarpaframe_createScene'],
46
+ contractId: 'turbowarp-aframe/createScene', contractRevision: '1',
47
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
48
+ bindings: [binding('createScene', 'LAYER', ["above-stage", "below-stage"])]},
49
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-setPosition', opcodes: ['turbowarpaframe_setPosition'],
50
+ contractId: 'turbowarp-aframe/setPosition', contractRevision: '1',
51
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
52
+ bindings: [binding('setPosition', 'X'), binding('setPosition', 'Y'), binding('setPosition', 'Z')]},
53
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-moveBy', opcodes: ['turbowarpaframe_moveBy'],
54
+ contractId: 'turbowarp-aframe/moveBy', contractRevision: '1',
55
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
56
+ bindings: [binding('moveBy', 'X'), binding('moveBy', 'Y'), binding('moveBy', 'Z')]},
57
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-setRotation', opcodes: ['turbowarpaframe_setRotation'],
58
+ contractId: 'turbowarp-aframe/setRotation', contractRevision: '1',
59
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
60
+ bindings: [binding('setRotation', 'X'), binding('setRotation', 'Y'), binding('setRotation', 'Z')]},
61
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-createAnimationClip', opcodes: ['turbowarpaframe_createAnimationClip'],
62
+ contractId: 'turbowarp-aframe/createAnimationClip', contractRevision: '1',
63
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
64
+ bindings: [binding('createAnimationClip', 'DURATION')]},
65
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-addVectorKeyframeTrack', opcodes: ['turbowarpaframe_addVectorKeyframeTrack'],
66
+ contractId: 'turbowarp-aframe/addVectorKeyframeTrack', contractRevision: '1',
67
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
68
+ bindings: [binding('addVectorKeyframeTrack', 'PATH', [".position", ".scale"])]},
69
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-addQuaternionKeyframeTrack', opcodes: ['turbowarpaframe_addQuaternionKeyframeTrack'],
70
+ contractId: 'turbowarp-aframe/addQuaternionKeyframeTrack', contractRevision: '1',
71
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
72
+ bindings: [binding('addQuaternionKeyframeTrack', 'PATH', [".quaternion"])]},
73
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-addEulerRotationKeyframeTrack', opcodes: ['turbowarpaframe_addEulerRotationKeyframeTrack'],
74
+ contractId: 'turbowarp-aframe/addEulerRotationKeyframeTrack', contractRevision: '1',
75
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
76
+ bindings: [binding('addEulerRotationKeyframeTrack', 'PATH', [".quaternion"]), binding('addEulerRotationKeyframeTrack', 'UNIT', ["degrees", "radians"])]},
77
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-addPositionKeyframe', opcodes: ['turbowarpaframe_addPositionKeyframe'],
78
+ contractId: 'turbowarp-aframe/addPositionKeyframe', contractRevision: '1',
79
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
80
+ bindings: [binding('addPositionKeyframe', 'TIME'), binding('addPositionKeyframe', 'X'), binding('addPositionKeyframe', 'Y'), binding('addPositionKeyframe', 'Z')]},
81
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-addScaleKeyframe', opcodes: ['turbowarpaframe_addScaleKeyframe'],
82
+ contractId: 'turbowarp-aframe/addScaleKeyframe', contractRevision: '1',
83
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
84
+ bindings: [binding('addScaleKeyframe', 'TIME'), binding('addScaleKeyframe', 'X'), binding('addScaleKeyframe', 'Y'), binding('addScaleKeyframe', 'Z')]},
85
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-addEulerRotationKeyframe', opcodes: ['turbowarpaframe_addEulerRotationKeyframe'],
86
+ contractId: 'turbowarp-aframe/addEulerRotationKeyframe', contractRevision: '1',
87
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
88
+ bindings: [binding('addEulerRotationKeyframe', 'TIME'), binding('addEulerRotationKeyframe', 'X'), binding('addEulerRotationKeyframe', 'Y'), binding('addEulerRotationKeyframe', 'Z'), binding('addEulerRotationKeyframe', 'UNIT', ["degrees", "radians"])]},
89
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-setAnimationTimeScale', opcodes: ['turbowarpaframe_setAnimationTimeScale'],
90
+ contractId: 'turbowarp-aframe/setAnimationTimeScale', contractRevision: '1',
91
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
92
+ bindings: [binding('setAnimationTimeScale', 'SCALE')]},
93
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-setVrmBoneRotation', opcodes: ['turbowarpaframe_setVrmBoneRotation'],
94
+ contractId: 'turbowarp-aframe/setVrmBoneRotation', contractRevision: '1',
95
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
96
+ bindings: [binding('setVrmBoneRotation', 'X'), binding('setVrmBoneRotation', 'Y'), binding('setVrmBoneRotation', 'Z')]},
97
+ {apiVersion: 1, extensionId: 'turbowarpaframe', editorId: 'fixed-setVrmExpression', opcodes: ['turbowarpaframe_setVrmExpression'],
98
+ contractId: 'turbowarp-aframe/setVrmExpression', contractRevision: '1',
99
+ validationContext: () => ({consumerId: 'turbowarp-aframe', nowMs: 0, settings: null}),
100
+ bindings: [binding('setVrmExpression', 'WEIGHT')]},
101
+ ];
102
+
103
+ interface OptionalHost {
104
+ gui?: {blockEditors?: {apiVersion?: number; registerFixedInputEditor?: (options: FixedInputEditorOptions) => () => void}};
105
+ vm?: {runtime?: {on?: (event: string, listener: () => void) => void; off?: (event: string, listener: () => void) => void}};
106
+ }
107
+
108
+ /** Register only currently declared opcodes; release obsolete feature-flag scopes. */
109
+ export function fixedInputRegistration(scratch: unknown) {
110
+ const host = (typeof scratch === 'object' && scratch !== null ? scratch : {}) as OptionalHost;
111
+ let active: unknown;
112
+ let activeOpcodes = '';
113
+ const removers: (() => void)[] = [];
114
+ let disposed = false;
115
+ const dispose = () => {
116
+ host.vm?.runtime?.off?.('RUNTIME_DISPOSED', shutdown);
117
+ for (const remove of removers.splice(0).reverse()) remove();
118
+ active = undefined; activeOpcodes = '';
119
+ };
120
+ const shutdown = () => {disposed = true; dispose();};
121
+ return {
122
+ ensure(opcodes: readonly string[]) {
123
+ const api = host.gui?.blockEditors;
124
+ const signature = [...new Set(opcodes)].sort().join('\0');
125
+ if (disposed || (api === active && signature === activeOpcodes)) return;
126
+ dispose();
127
+ if (api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== 'function') return;
128
+ try {
129
+ for (const definition of fixedInputEditors) {
130
+ if (definition.opcodes.every(opcode => opcodes.includes(opcode))) {
131
+ const remove = api.registerFixedInputEditor.call(api, definition);
132
+ if (typeof remove !== 'function') throw new TypeError('Invalid fixed registration cleanup.');
133
+ removers.push(remove);
134
+ }
135
+ }
136
+ active = api; activeOpcodes = signature;
137
+ host.vm?.runtime?.on?.('RUNTIME_DISPOSED', shutdown);
138
+ } catch (error) {dispose(); throw error;}
139
+ },
140
+ dispose: shutdown
141
+ };
142
+ }
@@ -0,0 +1,14 @@
1
+ import document from '../schemas/block-editor-fixed-inputs.schema.json' with {type: 'json'};
2
+ import type {SchemaBundle, SchemaDocument} from '@kubohiroya/turbowarp-extension-data-editor/schema';
3
+
4
+ /** Owner canonical fragments, distinct from older SOURCE/editor schemas. */
5
+ export function fixedInputBundle(opcode: string, argument: string): SchemaBundle {
6
+ const source = {repository: 'turbowarp-aframe', path: 'schemas/block-editor-fixed-inputs.schema.json',
7
+ symbol: `#/$defs/${opcode}/properties/${argument}`};
8
+ const fragment = (document.$defs as Record<string, {properties: Record<string, SchemaDocument>}>)[opcode]!.properties[argument]!;
9
+ return {identity: {ownerId: 'turbowarp-aframe', contractId: 'fixed-inputs', revision: '1', variantId: `${opcode}.${argument}`},
10
+ dialect: '2020-12', root: {retrievalUri: document.$id, document: fragment, artifact: {role: 'canonical-schema', source}}, resources: [],
11
+ provenance: {canonicalSources: [source, {repository: 'turbowarp-aframe', path: 'src/extension.ts', symbol: opcode}],
12
+ generatedFiles: [], referenceSchemas: [], generationSources: []}};
13
+ }
14
+ export const fixedSchemas = document.$defs;