cabbage-react 1.0.46 → 1.0.48
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/README.md +2 -2
- package/dist/cabbage/cabbage.d.ts +30 -1
- package/dist/cabbage/cabbage.d.ts.map +1 -1
- package/dist/hooks/useCabbageMessage.d.ts +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +148 -113
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,14 +34,14 @@ Get properties for a widget from the backend. This hook:
|
|
|
34
34
|
Get messages from the backend. This hook:
|
|
35
35
|
|
|
36
36
|
- Automatically updates local state when a message is received
|
|
37
|
-
- Expects a serialized JSON object (sent as a string) with a `
|
|
37
|
+
- Expects a serialized JSON object (sent as a string) with a `type` property
|
|
38
38
|
|
|
39
39
|
#### Example: sending a message from Csound
|
|
40
40
|
|
|
41
41
|
```csd
|
|
42
42
|
jsonData:S = sprintf({{
|
|
43
43
|
{
|
|
44
|
-
"
|
|
44
|
+
"type":"NoteData",
|
|
45
45
|
"noteCount":%f,
|
|
46
46
|
"note":%f,
|
|
47
47
|
"noteLength":%f,
|
|
@@ -212,6 +212,35 @@ export class Cabbage {
|
|
|
212
212
|
* @param {Object|null} vscode - VS Code API object (null for plugin mode)
|
|
213
213
|
* @param {Object} additionalData - Additional data to include in the command
|
|
214
214
|
*/
|
|
215
|
-
|
|
215
|
+
/**
|
|
216
|
+
* Register a listener for incoming messages from the Cabbage backend.
|
|
217
|
+
*
|
|
218
|
+
* Abstracts the two environments so that the same UI code works everywhere:
|
|
219
|
+
* - **VS Code (iframe relay)**: messages arrive as window `message` events
|
|
220
|
+
* - **Native plugin (DAW)**: messages arrive via `window.hostMessageCallback`
|
|
221
|
+
*
|
|
222
|
+
* @param {function(Object): void} callback - Called with the message object.
|
|
223
|
+
* Common message shapes:
|
|
224
|
+
* - `{ command: "parameterChange", data: { paramIdx, value } }` (data may be top-level or nested)
|
|
225
|
+
* - `{ command: "widgetUpdate", id, value }` (from cabbageSetValue / cabbageSet)
|
|
226
|
+
* - `{ command: "widgetUpdate", id, widgetJson }` (full widget update)
|
|
227
|
+
* - `{ command: "channelDataUpdate", data: { channel, value } }`
|
|
228
|
+
* @returns {function(): void} Cleanup function — call it when the UI is torn down.
|
|
229
|
+
*
|
|
230
|
+
* @example <caption>Vanilla HTML</caption>
|
|
231
|
+
* import { Cabbage } from './cabbage/cabbage.js';
|
|
232
|
+
* Cabbage.addMessageListener((msg) => {
|
|
233
|
+
* const data = msg.data ?? msg;
|
|
234
|
+
* if (msg.command === 'parameterChange') { ... }
|
|
235
|
+
* });
|
|
236
|
+
*
|
|
237
|
+
* @example <caption>Svelte</caption>
|
|
238
|
+
* import { onMount, onDestroy } from 'svelte';
|
|
239
|
+
* let remove;
|
|
240
|
+
* onMount(() => { remove = Cabbage.addMessageListener((msg) => { ... }); });
|
|
241
|
+
* onDestroy(() => remove?.());
|
|
242
|
+
*/
|
|
243
|
+
static addMessageListener(callback: (arg0: Object) => void): () => void;
|
|
244
|
+
static sendCustomCommand(command: any, vscode?: null, additionalData?: {}): void;
|
|
216
245
|
}
|
|
217
246
|
//# sourceMappingURL=cabbage.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cabbage.d.ts","sourceRoot":"","sources":["../../src/cabbage/cabbage.js"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoFG;AAEH;IACC;;;;;;;;;;;;;;;;;;;OAmBG;IACH,oDALG;QAAqB,OAAO,EAApB,MAAM;QACc,KAAK,EAAzB,MAAM,GAAC,MAAM;QACC,OAAO;KAC7B,WAAQ,MAAM,GAAC,IAAI,
|
|
1
|
+
{"version":3,"file":"cabbage.d.ts","sourceRoot":"","sources":["../../src/cabbage/cabbage.js"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoFG;AAEH;IACC;;;;;;;;;;;;;;;;;;;OAmBG;IACH,oDALG;QAAqB,OAAO,EAApB,MAAM;QACc,KAAK,EAAzB,MAAM,GAAC,MAAM;QACC,OAAO;KAC7B,WAAQ,MAAM,GAAC,IAAI,QA2BrB;IAED;;;;;OAKG;IACH,8BAHW,MAAM,GAAC,IAAI,mBACX,MAAM,QAIhB;IACD;;;;;;;OAOG;IACH,yCALW,MAAM,aACN,MAAM,aACN,MAAM,WACN,MAAM,GAAC,IAAI,QAiCrB;IAED;;;;;;OAMG;IACH,uCAJW,MAAM,aACN,MAAM,aACN,MAAM,QAE8C;IAE/D;;;;;;;;;OASG;IACH,qCAPW,MAAM,GAAC,IAAI,WACX,MAAM,YAEd;QAAyB,SAAS;QACT,OAAO;QACN,uBAAuB;KACnD,QA8BA;IAED;;;;;;OAMG;IACH,uBAJW,MAAM,GAAC,IAAI,OACX,MAAM,QACN,MAAM,QA0BhB;IAED;;;;;;;;;;;OAWG;IACH,4BAPW,MAAM,UACN,MAAM,WACN,MAAM,QA8BhB;IAED;;;;;;;;;;;;;;;;;;OAkBG;IACH,kCANW,OAAO,QAUjB;IAED;;;;;;OAMG;IACH,gCAJW,MAAM,QACN,MAAM,GAAC,MAAM,WACb,MAAM,GAAC,IAAI,QAuCrB;IAED;;;;;;OAMG;IACH,gCAmBC;IAED;;;;;;OAMG;IACH;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,oCArBW,CAAS,IAAM,EAAN,MAAM,KAAG,IAAI,GAMpB,MAAY,IAAI,CAkC5B;IAED,iFAiCC;CACD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Custom hook to get
|
|
2
|
+
* Custom hook to get the latest message for a specific type from Cabbage backend.
|
|
3
3
|
* This hook listens to messages sent from the backend and updates the local state
|
|
4
4
|
* whenever new data is received.
|
|
5
5
|
* @param messageType
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react");class M{static sendControlData({channel:
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react");class M{static sendControlData({channel:n,value:s,gesture:a="complete"},o=null){const e={command:"controlData",channel:n,value:s,gesture:a};o!==null?o.postMessage(e):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(e):window.parent!==window?window.parent.postMessage(e,"*"):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",e)}static isReadyToLoad(n=null,s={}){this.sendCustomCommand("isReadyToLoad",n)}static sendMidiMessageFromUI(n,s,a,o=null){var e={statusByte:n,dataByte1:s,dataByte2:a};const i={command:"midiMessage",obj:JSON.stringify(e)};o!==null?o.postMessage(i):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(i):window.parent!==window?window.parent.postMessage(i,"*"):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",i)}static MidiMessageFromHost(n,s,a){}static triggerFileOpenDialog(n,s,a={}){var o={channel:s,directory:a.directory||"",filters:a.filters||"*",openAtLastKnownLocation:a.openAtLastKnownLocation!==void 0?a.openAtLastKnownLocation:!0};const e={command:"fileOpen",obj:JSON.stringify(o)};n!==null?n.postMessage(e):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(e):window.parent!==window?window.parent.postMessage(e,"*"):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",e)}static openUrl(n,s,a){var o={url:s,file:a};const e={command:"openUrl",obj:JSON.stringify(o)};n!==null?n.postMessage(e):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(e):window.parent!==window?window.parent.postMessage(e,"*"):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",e)}static requestResize(n,s,a=null){const o={command:"requestResize",width:n,height:s};if(a!==null){console.warn("Cabbage: requestResize is not supported in VS Code extension mode");return}else typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(o):window.parent!==window?window.parent.postMessage(o,"*"):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",o)}static consumeKeypresses(n){typeof window.consumeKeypresses=="function"&&window.consumeKeypresses(n)}static sendChannelData(n,s,a=null){var o={channel:n};if(typeof s=="string")o.stringData=s;else if(typeof s=="number")o.floatData=s;else{console.warn("Cabbage: sendChannelData received unsupported data type:",typeof s);return}const e={command:"channelData",obj:JSON.stringify(o)};a!==null?a.postMessage(e):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(e):window.parent!==window?window.parent.postMessage(e,"*"):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",e)}static sendWidgetUpdate(n,s=null){const a={command:"widgetStateUpdate",obj:JSON.stringify(CabbageUtils.sanitizeForEditor(n))};s!==null?s.postMessage(a):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(a):window.parent!==window?window.parent.postMessage(a,"*"):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",a)}static addMessageListener(n){const s=a=>{a.source!==window&&n(a.data)};return window.addEventListener("message",s),window.hostMessageCallback=n,()=>{window.removeEventListener("message",s),window.hostMessageCallback===n&&(window.hostMessageCallback=null)}}static sendCustomCommand(n,s=null,a={}){const o={command:n,text:JSON.stringify(a)};if(s!==null)s.postMessage(o);else if(typeof window.sendMessageFromUI=="function")try{const e=window.sendMessageFromUI(o)}catch(e){console.error("Cabbage: sendMessageFromUI threw error:",e),console.error("Cabbage: Error stack:",e.stack)}else window.parent!==window?window.parent.postMessage(o,"*"):(console.error("Cabbage: window.sendMessageFromUI is not available yet. Message:",o),console.error("Cabbage: typeof window.sendMessageFromUI:",typeof window.sendMessageFromUI),console.error("Cabbage: window.sendMessageFromUI value:",window.sendMessageFromUI))}}const C=r=>{const[n,s]=l.useState();return l.useEffect(()=>{const a=o=>{const{id:e,widgetJson:i,command:f}=o.data;if(e===r&&i&&f==="widgetUpdate"){const t=JSON.parse(i);console.log(`[Cabbage-React] Received properties for channelId: ${e}`,t),s(t)}};return window.addEventListener("message",a),()=>{window.removeEventListener("message",a)}},[]),{properties:n}},y=(r,n="complete")=>{const{properties:s}=C(r),[a,o]=l.useState(),[e,i]=l.useState(),f=t=>{o(t),M.sendControlData({channel:r,value:t,gesture:n})};return l.useEffect(()=>{var m;const t=s==null?void 0:s.channels.find(d=>d.id===r);if(!t)return;const g=t.parameterIndex;if(e===void 0&&g!==void 0&&(console.log(`[Cabbage-React] Received parameterIndex for channelId: ${t.id}`,g),i(g)),a!==void 0)return;const w=(m=t.range)==null?void 0:m.value;w!=null&&(console.log(`[Cabbage-React] Received initial value for channelId: ${t.id}`,w),o(w))},[s]),l.useEffect(()=>{const t=g=>{var m;const{command:w}=g.data;if(w==="parameterChange"){const{value:d,paramIdx:c}=g.data.data;if(c!==e||d===null)return;console.log(`[Cabbage-React] Received parameterChange for parameterIndex: ${c}`,d),o(d)}else if(w==="batchWidgetUpdate"){const d=g.data.widgets,c=d==null?void 0:d.find(p=>p.id===r);if(!c)return;const u=JSON.parse(c.widgetJson).channels.find(p=>p.id===r),b=(m=u==null?void 0:u.range)==null?void 0:m.value;console.log(`[Cabbage-React] Received batch widget update for channelId: ${c.id}`,b),o(b)}};return window.addEventListener("message",t),()=>{window.removeEventListener("message",t)}},[e]),{value:a,setValue:f}},I=r=>{const[n,s]=l.useState();return l.useEffect(()=>{const a=o=>{const{data:e,type:i}=o;if(e&&i==="message"){if(e.type!==r)return;console.log(`[Cabbage-React] Received data for messageType: ${e.type}`,e),s(e)}};return window.addEventListener("message",a),()=>{window.removeEventListener("message",a)}},[]),{data:n}};exports.Cabbage=M;exports.useCabbageMessage=I;exports.useCabbageProperties=C;exports.useCabbageState=y;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
class
|
|
1
|
+
import { useState as m, useEffect as f } from "react";
|
|
2
|
+
class C {
|
|
3
3
|
/**
|
|
4
4
|
* Send a widget value change to the Cabbage backend.
|
|
5
5
|
*
|
|
@@ -20,14 +20,14 @@ class y {
|
|
|
20
20
|
* @param {string} [data.gesture="complete"] - The gesture type: "begin" (start of interaction), "value" (during interaction), "end" (end of continuous interaction), or "complete" (discrete action e.g. button click).
|
|
21
21
|
* @param {Object|null} vscode - VS Code API object (null for plugin mode)
|
|
22
22
|
*/
|
|
23
|
-
static sendControlData({ channel:
|
|
23
|
+
static sendControlData({ channel: a, value: s, gesture: n = "complete" }, o = null) {
|
|
24
24
|
const e = {
|
|
25
25
|
command: "controlData",
|
|
26
|
-
channel:
|
|
26
|
+
channel: a,
|
|
27
27
|
value: s,
|
|
28
|
-
gesture:
|
|
28
|
+
gesture: n
|
|
29
29
|
};
|
|
30
|
-
|
|
30
|
+
o !== null ? o.postMessage(e) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(e) : window.parent !== window ? window.parent.postMessage(e, "*") : console.error(
|
|
31
31
|
"Cabbage: window.sendMessageFromUI is not available. Message:",
|
|
32
32
|
e
|
|
33
33
|
);
|
|
@@ -38,8 +38,8 @@ class y {
|
|
|
38
38
|
* @param {Object|null} vscode - VS Code API object (null for plugin mode)
|
|
39
39
|
* @param {Object} additionalData - Additional initialization data
|
|
40
40
|
*/
|
|
41
|
-
static isReadyToLoad(
|
|
42
|
-
this.sendCustomCommand("isReadyToLoad",
|
|
41
|
+
static isReadyToLoad(a = null, s = {}) {
|
|
42
|
+
this.sendCustomCommand("isReadyToLoad", a);
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
45
|
* Send a MIDI message from the UI to the Cabbage backend.
|
|
@@ -49,19 +49,19 @@ class y {
|
|
|
49
49
|
* @param {number} dataByte2 - Second MIDI data byte
|
|
50
50
|
* @param {Object|null} vscode - VS Code API object (null for plugin mode)
|
|
51
51
|
*/
|
|
52
|
-
static sendMidiMessageFromUI(
|
|
52
|
+
static sendMidiMessageFromUI(a, s, n, o = null) {
|
|
53
53
|
var e = {
|
|
54
|
-
statusByte:
|
|
54
|
+
statusByte: a,
|
|
55
55
|
dataByte1: s,
|
|
56
|
-
dataByte2:
|
|
56
|
+
dataByte2: n
|
|
57
57
|
};
|
|
58
|
-
const
|
|
58
|
+
const i = {
|
|
59
59
|
command: "midiMessage",
|
|
60
60
|
obj: JSON.stringify(e)
|
|
61
61
|
};
|
|
62
|
-
|
|
62
|
+
o !== null ? o.postMessage(i) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(i) : window.parent !== window ? window.parent.postMessage(i, "*") : console.error(
|
|
63
63
|
"Cabbage: window.sendMessageFromUI is not available. Message:",
|
|
64
|
-
|
|
64
|
+
i
|
|
65
65
|
);
|
|
66
66
|
}
|
|
67
67
|
/**
|
|
@@ -71,7 +71,7 @@ class y {
|
|
|
71
71
|
* @param {number} dataByte1 - First MIDI data byte
|
|
72
72
|
* @param {number} dataByte2 - Second MIDI data byte
|
|
73
73
|
*/
|
|
74
|
-
static MidiMessageFromHost(
|
|
74
|
+
static MidiMessageFromHost(a, s, n) {
|
|
75
75
|
}
|
|
76
76
|
/**
|
|
77
77
|
* Trigger a native file open dialog for file selection widgets.
|
|
@@ -83,18 +83,18 @@ class y {
|
|
|
83
83
|
* @param {string} [options.filters="*"] - File filters (e.g., "*.wav;*.aiff")
|
|
84
84
|
* @param {boolean} [options.openAtLastKnownLocation=true] - Whether to open at last known location
|
|
85
85
|
*/
|
|
86
|
-
static triggerFileOpenDialog(
|
|
87
|
-
var
|
|
86
|
+
static triggerFileOpenDialog(a, s, n = {}) {
|
|
87
|
+
var o = {
|
|
88
88
|
channel: s,
|
|
89
|
-
directory:
|
|
90
|
-
filters:
|
|
91
|
-
openAtLastKnownLocation:
|
|
89
|
+
directory: n.directory || "",
|
|
90
|
+
filters: n.filters || "*",
|
|
91
|
+
openAtLastKnownLocation: n.openAtLastKnownLocation !== void 0 ? n.openAtLastKnownLocation : !0
|
|
92
92
|
};
|
|
93
93
|
const e = {
|
|
94
94
|
command: "fileOpen",
|
|
95
|
-
obj: JSON.stringify(
|
|
95
|
+
obj: JSON.stringify(o)
|
|
96
96
|
};
|
|
97
|
-
|
|
97
|
+
a !== null ? a.postMessage(e) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(e) : window.parent !== window ? window.parent.postMessage(e, "*") : console.error(
|
|
98
98
|
"Cabbage: window.sendMessageFromUI is not available. Message:",
|
|
99
99
|
e
|
|
100
100
|
);
|
|
@@ -106,16 +106,16 @@ class y {
|
|
|
106
106
|
* @param {string} url - URL to open
|
|
107
107
|
* @param {string} file - File path to open
|
|
108
108
|
*/
|
|
109
|
-
static openUrl(
|
|
110
|
-
var
|
|
109
|
+
static openUrl(a, s, n) {
|
|
110
|
+
var o = {
|
|
111
111
|
url: s,
|
|
112
|
-
file:
|
|
112
|
+
file: n
|
|
113
113
|
};
|
|
114
114
|
const e = {
|
|
115
115
|
command: "openUrl",
|
|
116
|
-
obj: JSON.stringify(
|
|
116
|
+
obj: JSON.stringify(o)
|
|
117
117
|
};
|
|
118
|
-
|
|
118
|
+
a !== null ? a.postMessage(e) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(e) : window.parent !== window ? window.parent.postMessage(e, "*") : console.error(
|
|
119
119
|
"Cabbage: window.sendMessageFromUI is not available. Message:",
|
|
120
120
|
e
|
|
121
121
|
);
|
|
@@ -132,21 +132,21 @@ class y {
|
|
|
132
132
|
* The response will be sent via hostMessageCallback with:
|
|
133
133
|
* {command: "resizeResponse", accepted: boolean, width: number, height: number}
|
|
134
134
|
*/
|
|
135
|
-
static requestResize(
|
|
136
|
-
const
|
|
135
|
+
static requestResize(a, s, n = null) {
|
|
136
|
+
const o = {
|
|
137
137
|
command: "requestResize",
|
|
138
|
-
width:
|
|
138
|
+
width: a,
|
|
139
139
|
height: s
|
|
140
140
|
};
|
|
141
|
-
if (
|
|
141
|
+
if (n !== null) {
|
|
142
142
|
console.warn(
|
|
143
143
|
"Cabbage: requestResize is not supported in VS Code extension mode"
|
|
144
144
|
);
|
|
145
145
|
return;
|
|
146
146
|
} else
|
|
147
|
-
typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(
|
|
147
|
+
typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(o) : window.parent !== window ? window.parent.postMessage(o, "*") : console.error(
|
|
148
148
|
"Cabbage: window.sendMessageFromUI is not available. Message:",
|
|
149
|
-
|
|
149
|
+
o
|
|
150
150
|
);
|
|
151
151
|
}
|
|
152
152
|
/**
|
|
@@ -168,8 +168,8 @@ class y {
|
|
|
168
168
|
* myCustomEditor.addEventListener('focus', () => Cabbage.consumeKeypresses(true));
|
|
169
169
|
* myCustomEditor.addEventListener('blur', () => Cabbage.consumeKeypresses(false));
|
|
170
170
|
*/
|
|
171
|
-
static consumeKeypresses(
|
|
172
|
-
typeof window.consumeKeypresses == "function" && window.consumeKeypresses(
|
|
171
|
+
static consumeKeypresses(a) {
|
|
172
|
+
typeof window.consumeKeypresses == "function" && window.consumeKeypresses(a);
|
|
173
173
|
}
|
|
174
174
|
/**
|
|
175
175
|
* Send channel data directly to Csound without DAW automation involvement.
|
|
@@ -178,14 +178,14 @@ class y {
|
|
|
178
178
|
* @param {number|string} data - The data to send (number or string)
|
|
179
179
|
* @param {Object|null} vscode - VS Code API object (null for plugin mode)
|
|
180
180
|
*/
|
|
181
|
-
static sendChannelData(
|
|
182
|
-
var
|
|
183
|
-
channel:
|
|
181
|
+
static sendChannelData(a, s, n = null) {
|
|
182
|
+
var o = {
|
|
183
|
+
channel: a
|
|
184
184
|
};
|
|
185
185
|
if (typeof s == "string")
|
|
186
|
-
|
|
186
|
+
o.stringData = s;
|
|
187
187
|
else if (typeof s == "number")
|
|
188
|
-
|
|
188
|
+
o.floatData = s;
|
|
189
189
|
else {
|
|
190
190
|
console.warn(
|
|
191
191
|
"Cabbage: sendChannelData received unsupported data type:",
|
|
@@ -195,9 +195,9 @@ class y {
|
|
|
195
195
|
}
|
|
196
196
|
const e = {
|
|
197
197
|
command: "channelData",
|
|
198
|
-
obj: JSON.stringify(
|
|
198
|
+
obj: JSON.stringify(o)
|
|
199
199
|
};
|
|
200
|
-
|
|
200
|
+
n !== null ? n.postMessage(e) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(e) : window.parent !== window ? window.parent.postMessage(e, "*") : console.error(
|
|
201
201
|
"Cabbage: window.sendMessageFromUI is not available. Message:",
|
|
202
202
|
e
|
|
203
203
|
);
|
|
@@ -209,14 +209,14 @@ class y {
|
|
|
209
209
|
* @param {Object} widget - The widget configuration object to update
|
|
210
210
|
* @param {Object|null} vscode - VS Code API object (null for plugin mode)
|
|
211
211
|
*/
|
|
212
|
-
static sendWidgetUpdate(
|
|
213
|
-
const
|
|
212
|
+
static sendWidgetUpdate(a, s = null) {
|
|
213
|
+
const n = {
|
|
214
214
|
command: "widgetStateUpdate",
|
|
215
|
-
obj: JSON.stringify(CabbageUtils.sanitizeForEditor(
|
|
215
|
+
obj: JSON.stringify(CabbageUtils.sanitizeForEditor(a))
|
|
216
216
|
};
|
|
217
|
-
s !== null ? s.postMessage(
|
|
217
|
+
s !== null ? s.postMessage(n) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(n) : window.parent !== window ? window.parent.postMessage(n, "*") : console.error(
|
|
218
218
|
"Cabbage: window.sendMessageFromUI is not available. Message:",
|
|
219
|
-
|
|
219
|
+
n
|
|
220
220
|
);
|
|
221
221
|
}
|
|
222
222
|
/**
|
|
@@ -226,128 +226,163 @@ class y {
|
|
|
226
226
|
* @param {Object|null} vscode - VS Code API object (null for plugin mode)
|
|
227
227
|
* @param {Object} additionalData - Additional data to include in the command
|
|
228
228
|
*/
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
229
|
+
/**
|
|
230
|
+
* Register a listener for incoming messages from the Cabbage backend.
|
|
231
|
+
*
|
|
232
|
+
* Abstracts the two environments so that the same UI code works everywhere:
|
|
233
|
+
* - **VS Code (iframe relay)**: messages arrive as window `message` events
|
|
234
|
+
* - **Native plugin (DAW)**: messages arrive via `window.hostMessageCallback`
|
|
235
|
+
*
|
|
236
|
+
* @param {function(Object): void} callback - Called with the message object.
|
|
237
|
+
* Common message shapes:
|
|
238
|
+
* - `{ command: "parameterChange", data: { paramIdx, value } }` (data may be top-level or nested)
|
|
239
|
+
* - `{ command: "widgetUpdate", id, value }` (from cabbageSetValue / cabbageSet)
|
|
240
|
+
* - `{ command: "widgetUpdate", id, widgetJson }` (full widget update)
|
|
241
|
+
* - `{ command: "channelDataUpdate", data: { channel, value } }`
|
|
242
|
+
* @returns {function(): void} Cleanup function — call it when the UI is torn down.
|
|
243
|
+
*
|
|
244
|
+
* @example <caption>Vanilla HTML</caption>
|
|
245
|
+
* import { Cabbage } from './cabbage/cabbage.js';
|
|
246
|
+
* Cabbage.addMessageListener((msg) => {
|
|
247
|
+
* const data = msg.data ?? msg;
|
|
248
|
+
* if (msg.command === 'parameterChange') { ... }
|
|
249
|
+
* });
|
|
250
|
+
*
|
|
251
|
+
* @example <caption>Svelte</caption>
|
|
252
|
+
* import { onMount, onDestroy } from 'svelte';
|
|
253
|
+
* let remove;
|
|
254
|
+
* onMount(() => { remove = Cabbage.addMessageListener((msg) => { ... }); });
|
|
255
|
+
* onDestroy(() => remove?.());
|
|
256
|
+
*/
|
|
257
|
+
static addMessageListener(a) {
|
|
258
|
+
const s = (n) => {
|
|
259
|
+
n.source !== window && a(n.data);
|
|
260
|
+
};
|
|
261
|
+
return window.addEventListener("message", s), window.hostMessageCallback = a, () => {
|
|
262
|
+
window.removeEventListener("message", s), window.hostMessageCallback === a && (window.hostMessageCallback = null);
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
static sendCustomCommand(a, s = null, n = {}) {
|
|
266
|
+
const o = {
|
|
267
|
+
command: a,
|
|
268
|
+
text: JSON.stringify(n)
|
|
233
269
|
};
|
|
234
270
|
if (s !== null)
|
|
235
|
-
s.postMessage(
|
|
271
|
+
s.postMessage(o);
|
|
236
272
|
else if (typeof window.sendMessageFromUI == "function")
|
|
237
273
|
try {
|
|
238
|
-
const e = window.sendMessageFromUI(
|
|
274
|
+
const e = window.sendMessageFromUI(o);
|
|
239
275
|
} catch (e) {
|
|
240
276
|
console.error("Cabbage: sendMessageFromUI threw error:", e), console.error("Cabbage: Error stack:", e.stack);
|
|
241
277
|
}
|
|
242
|
-
else
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
);
|
|
278
|
+
else window.parent !== window ? window.parent.postMessage(o, "*") : (console.error(
|
|
279
|
+
"Cabbage: window.sendMessageFromUI is not available yet. Message:",
|
|
280
|
+
o
|
|
281
|
+
), console.error(
|
|
282
|
+
"Cabbage: typeof window.sendMessageFromUI:",
|
|
283
|
+
typeof window.sendMessageFromUI
|
|
284
|
+
), console.error(
|
|
285
|
+
"Cabbage: window.sendMessageFromUI value:",
|
|
286
|
+
window.sendMessageFromUI
|
|
287
|
+
));
|
|
253
288
|
}
|
|
254
289
|
}
|
|
255
|
-
const
|
|
256
|
-
const [
|
|
290
|
+
const y = (r) => {
|
|
291
|
+
const [a, s] = m();
|
|
257
292
|
return f(() => {
|
|
258
|
-
const
|
|
259
|
-
const { id: e, widgetJson:
|
|
260
|
-
if (e ===
|
|
261
|
-
const t = JSON.parse(
|
|
293
|
+
const n = (o) => {
|
|
294
|
+
const { id: e, widgetJson: i, command: p } = o.data;
|
|
295
|
+
if (e === r && i && p === "widgetUpdate") {
|
|
296
|
+
const t = JSON.parse(i);
|
|
262
297
|
console.log(
|
|
263
298
|
`[Cabbage-React] Received properties for channelId: ${e}`,
|
|
264
299
|
t
|
|
265
300
|
), s(t);
|
|
266
301
|
}
|
|
267
302
|
};
|
|
268
|
-
return window.addEventListener("message",
|
|
269
|
-
window.removeEventListener("message",
|
|
303
|
+
return window.addEventListener("message", n), () => {
|
|
304
|
+
window.removeEventListener("message", n);
|
|
270
305
|
};
|
|
271
306
|
}, []), {
|
|
272
|
-
properties:
|
|
307
|
+
properties: a
|
|
273
308
|
};
|
|
274
|
-
}, F = (
|
|
275
|
-
const { properties: s } =
|
|
276
|
-
|
|
277
|
-
channel:
|
|
309
|
+
}, F = (r, a = "complete") => {
|
|
310
|
+
const { properties: s } = y(r), [n, o] = m(), [e, i] = m(), p = (t) => {
|
|
311
|
+
o(t), C.sendControlData({
|
|
312
|
+
channel: r,
|
|
278
313
|
value: t,
|
|
279
|
-
gesture:
|
|
314
|
+
gesture: a
|
|
280
315
|
});
|
|
281
316
|
};
|
|
282
317
|
return f(() => {
|
|
283
|
-
var
|
|
318
|
+
var c;
|
|
284
319
|
const t = s == null ? void 0 : s.channels.find(
|
|
285
|
-
(d) => d.id ===
|
|
320
|
+
(d) => d.id === r
|
|
286
321
|
);
|
|
287
322
|
if (!t) return;
|
|
288
323
|
const l = t.parameterIndex;
|
|
289
324
|
if (e === void 0 && l !== void 0 && (console.log(
|
|
290
325
|
`[Cabbage-React] Received parameterIndex for channelId: ${t.id}`,
|
|
291
326
|
l
|
|
292
|
-
),
|
|
293
|
-
const
|
|
294
|
-
|
|
327
|
+
), i(l)), n !== void 0) return;
|
|
328
|
+
const w = (c = t.range) == null ? void 0 : c.value;
|
|
329
|
+
w != null && (console.log(
|
|
295
330
|
`[Cabbage-React] Received initial value for channelId: ${t.id}`,
|
|
296
|
-
|
|
297
|
-
),
|
|
331
|
+
w
|
|
332
|
+
), o(w));
|
|
298
333
|
}, [s]), f(() => {
|
|
299
334
|
const t = (l) => {
|
|
300
|
-
var
|
|
301
|
-
const { command:
|
|
302
|
-
if (
|
|
303
|
-
const { value: d, paramIdx:
|
|
304
|
-
if (
|
|
335
|
+
var c;
|
|
336
|
+
const { command: w } = l.data;
|
|
337
|
+
if (w === "parameterChange") {
|
|
338
|
+
const { value: d, paramIdx: g } = l.data.data;
|
|
339
|
+
if (g !== e || d === null) return;
|
|
305
340
|
console.log(
|
|
306
|
-
`[Cabbage-React] Received parameterChange for parameterIndex: ${
|
|
341
|
+
`[Cabbage-React] Received parameterChange for parameterIndex: ${g}`,
|
|
307
342
|
d
|
|
308
|
-
),
|
|
309
|
-
} else if (
|
|
310
|
-
const d = l.data.widgets,
|
|
311
|
-
if (!
|
|
312
|
-
const
|
|
313
|
-
(
|
|
314
|
-
), M = (
|
|
343
|
+
), o(d);
|
|
344
|
+
} else if (w === "batchWidgetUpdate") {
|
|
345
|
+
const d = l.data.widgets, g = d == null ? void 0 : d.find((b) => b.id === r);
|
|
346
|
+
if (!g) return;
|
|
347
|
+
const u = JSON.parse(g.widgetJson).channels.find(
|
|
348
|
+
(b) => b.id === r
|
|
349
|
+
), M = (c = u == null ? void 0 : u.range) == null ? void 0 : c.value;
|
|
315
350
|
console.log(
|
|
316
|
-
`[Cabbage-React] Received batch widget update for channelId: ${
|
|
351
|
+
`[Cabbage-React] Received batch widget update for channelId: ${g.id}`,
|
|
317
352
|
M
|
|
318
|
-
),
|
|
353
|
+
), o(M);
|
|
319
354
|
}
|
|
320
355
|
};
|
|
321
356
|
return window.addEventListener("message", t), () => {
|
|
322
357
|
window.removeEventListener("message", t);
|
|
323
358
|
};
|
|
324
359
|
}, [e]), {
|
|
325
|
-
value:
|
|
326
|
-
setValue:
|
|
360
|
+
value: n,
|
|
361
|
+
setValue: p
|
|
327
362
|
};
|
|
328
|
-
}, v = (
|
|
329
|
-
const [
|
|
363
|
+
}, v = (r) => {
|
|
364
|
+
const [a, s] = m();
|
|
330
365
|
return f(() => {
|
|
331
|
-
const
|
|
332
|
-
const { data: e, type:
|
|
333
|
-
if (e &&
|
|
334
|
-
if (e.type !==
|
|
366
|
+
const n = (o) => {
|
|
367
|
+
const { data: e, type: i } = o;
|
|
368
|
+
if (e && i === "message") {
|
|
369
|
+
if (e.type !== r) return;
|
|
335
370
|
console.log(
|
|
336
371
|
`[Cabbage-React] Received data for messageType: ${e.type}`,
|
|
337
372
|
e
|
|
338
373
|
), s(e);
|
|
339
374
|
}
|
|
340
375
|
};
|
|
341
|
-
return window.addEventListener("message",
|
|
342
|
-
window.removeEventListener("message",
|
|
376
|
+
return window.addEventListener("message", n), () => {
|
|
377
|
+
window.removeEventListener("message", n);
|
|
343
378
|
};
|
|
344
379
|
}, []), {
|
|
345
|
-
data:
|
|
380
|
+
data: a
|
|
346
381
|
};
|
|
347
382
|
};
|
|
348
383
|
export {
|
|
349
|
-
|
|
384
|
+
C as Cabbage,
|
|
350
385
|
v as useCabbageMessage,
|
|
351
|
-
|
|
386
|
+
y as useCabbageProperties,
|
|
352
387
|
F as useCabbageState
|
|
353
388
|
};
|