cabbage-react 1.0.47 → 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.
@@ -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
- static sendCustomCommand(command: string, vscode?: Object | null, additionalData?: Object): void;
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,QAyBrB;IAED;;;;;OAKG;IACH,8BAHW,MAAM,GAAC,IAAI,mBACX,MAAM,QAIhB;IACD;;;;;;;OAOG;IACH,yCALW,MAAM,aACN,MAAM,aACN,MAAM,WACN,MAAM,GAAC,IAAI,QA+BrB;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,QA4BA;IAED;;;;;;OAMG;IACH,uBAJW,MAAM,GAAC,IAAI,OACX,MAAM,QACN,MAAM,QAwBhB;IAED;;;;;;;;;;;OAWG;IACH,4BAPW,MAAM,UACN,MAAM,WACN,MAAM,QA4BhB;IAED;;;;;;;;;;;;;;;;;;OAkBG;IACH,kCANW,OAAO,QAUjB;IAED;;;;;;OAMG;IACH,gCAJW,MAAM,QACN,MAAM,GAAC,MAAM,WACb,MAAM,GAAC,IAAI,QAqCrB;IAED;;;;;;OAMG;IACH,gCAiBC;IAED;;;;;;OAMG;IACH,kCAJW,MAAM,WACN,MAAM,GAAC,IAAI,mBACX,MAAM,QAiChB;CACD"}
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 a message from Cabbage backend.
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:o,value:s,gesture:a="complete"},n=null){const e={command:"controlData",channel:o,value:s,gesture:a};n!==null?n.postMessage(e):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(e):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",e)}static isReadyToLoad(o=null,s={}){this.sendCustomCommand("isReadyToLoad",o)}static sendMidiMessageFromUI(o,s,a,n=null){var e={statusByte:o,dataByte1:s,dataByte2:a};const r={command:"midiMessage",obj:JSON.stringify(e)};n!==null?n.postMessage(r):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(r):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",r)}static MidiMessageFromHost(o,s,a){}static triggerFileOpenDialog(o,s,a={}){var n={channel:s,directory:a.directory||"",filters:a.filters||"*",openAtLastKnownLocation:a.openAtLastKnownLocation!==void 0?a.openAtLastKnownLocation:!0};const e={command:"fileOpen",obj:JSON.stringify(n)};o!==null?o.postMessage(e):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(e):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",e)}static openUrl(o,s,a){var n={url:s,file:a};const e={command:"openUrl",obj:JSON.stringify(n)};o!==null?o.postMessage(e):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(e):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",e)}static requestResize(o,s,a=null){const n={command:"requestResize",width:o,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(n):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",n)}static consumeKeypresses(o){typeof window.consumeKeypresses=="function"&&window.consumeKeypresses(o)}static sendChannelData(o,s,a=null){var n={channel:o};if(typeof s=="string")n.stringData=s;else if(typeof s=="number")n.floatData=s;else{console.warn("Cabbage: sendChannelData received unsupported data type:",typeof s);return}const e={command:"channelData",obj:JSON.stringify(n)};a!==null?a.postMessage(e):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(e):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",e)}static sendWidgetUpdate(o,s=null){const a={command:"widgetStateUpdate",obj:JSON.stringify(CabbageUtils.sanitizeForEditor(o))};s!==null?s.postMessage(a):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(a):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",a)}static sendCustomCommand(o,s=null,a={}){const n={command:o,text:JSON.stringify(a)};if(s!==null)s.postMessage(n);else if(typeof window.sendMessageFromUI=="function")try{const e=window.sendMessageFromUI(n)}catch(e){console.error("Cabbage: sendMessageFromUI threw error:",e),console.error("Cabbage: Error stack:",e.stack)}else console.error("Cabbage: window.sendMessageFromUI is not available yet. Message:",n),console.error("Cabbage: typeof window.sendMessageFromUI:",typeof window.sendMessageFromUI),console.error("Cabbage: window.sendMessageFromUI value:",window.sendMessageFromUI)}}const C=i=>{const[o,s]=l.useState();return l.useEffect(()=>{const a=n=>{const{id:e,widgetJson:r,command:w}=n.data;if(e===i&&r&&w==="widgetUpdate"){const t=JSON.parse(r);console.log(`[Cabbage-React] Received properties for channelId: ${e}`,t),s(t)}};return window.addEventListener("message",a),()=>{window.removeEventListener("message",a)}},[]),{properties:o}},y=(i,o="complete")=>{const{properties:s}=C(i),[a,n]=l.useState(),[e,r]=l.useState(),w=t=>{n(t),M.sendControlData({channel:i,value:t,gesture:o})};return l.useEffect(()=>{var u;const t=s==null?void 0:s.channels.find(d=>d.id===i);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),r(g)),a!==void 0)return;const c=(u=t.range)==null?void 0:u.value;c!=null&&(console.log(`[Cabbage-React] Received initial value for channelId: ${t.id}`,c),n(c))},[s]),l.useEffect(()=>{const t=g=>{var u;const{command:c}=g.data;if(c==="parameterChange"){const{value:d,paramIdx:m}=g.data.data;if(m!==e||d===null)return;console.log(`[Cabbage-React] Received parameterChange for parameterIndex: ${m}`,d),n(d)}else if(c==="batchWidgetUpdate"){const d=g.data.widgets,m=d==null?void 0:d.find(b=>b.id===i);if(!m)return;const f=JSON.parse(m.widgetJson).channels.find(b=>b.id===i),p=(u=f==null?void 0:f.range)==null?void 0:u.value;console.log(`[Cabbage-React] Received batch widget update for channelId: ${m.id}`,p),n(p)}};return window.addEventListener("message",t),()=>{window.removeEventListener("message",t)}},[e]),{value:a,setValue:w}},I=i=>{const[o,s]=l.useState();return l.useEffect(()=>{const a=n=>{const{data:e,type:r}=n;if(e&&r==="message"){if(e.type!==i)return;console.log(`[Cabbage-React] Received data for messageType: ${e.type}`,e),s(e)}};return window.addEventListener("message",a),()=>{window.removeEventListener("message",a)}},[]),{data:o}};exports.Cabbage=M;exports.useCabbageMessage=I;exports.useCabbageProperties=C;exports.useCabbageState=y;
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 w, useEffect as f } from "react";
2
- class y {
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: o, value: s, gesture: a = "complete" }, n = null) {
23
+ static sendControlData({ channel: a, value: s, gesture: n = "complete" }, o = null) {
24
24
  const e = {
25
25
  command: "controlData",
26
- channel: o,
26
+ channel: a,
27
27
  value: s,
28
- gesture: a
28
+ gesture: n
29
29
  };
30
- n !== null ? n.postMessage(e) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(e) : console.error(
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(o = null, s = {}) {
42
- this.sendCustomCommand("isReadyToLoad", o);
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(o, s, a, n = null) {
52
+ static sendMidiMessageFromUI(a, s, n, o = null) {
53
53
  var e = {
54
- statusByte: o,
54
+ statusByte: a,
55
55
  dataByte1: s,
56
- dataByte2: a
56
+ dataByte2: n
57
57
  };
58
- const r = {
58
+ const i = {
59
59
  command: "midiMessage",
60
60
  obj: JSON.stringify(e)
61
61
  };
62
- n !== null ? n.postMessage(r) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(r) : console.error(
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
- r
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(o, s, a) {
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(o, s, a = {}) {
87
- var n = {
86
+ static triggerFileOpenDialog(a, s, n = {}) {
87
+ var o = {
88
88
  channel: s,
89
- directory: a.directory || "",
90
- filters: a.filters || "*",
91
- openAtLastKnownLocation: a.openAtLastKnownLocation !== void 0 ? a.openAtLastKnownLocation : !0
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(n)
95
+ obj: JSON.stringify(o)
96
96
  };
97
- o !== null ? o.postMessage(e) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(e) : console.error(
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(o, s, a) {
110
- var n = {
109
+ static openUrl(a, s, n) {
110
+ var o = {
111
111
  url: s,
112
- file: a
112
+ file: n
113
113
  };
114
114
  const e = {
115
115
  command: "openUrl",
116
- obj: JSON.stringify(n)
116
+ obj: JSON.stringify(o)
117
117
  };
118
- o !== null ? o.postMessage(e) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(e) : console.error(
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(o, s, a = null) {
136
- const n = {
135
+ static requestResize(a, s, n = null) {
136
+ const o = {
137
137
  command: "requestResize",
138
- width: o,
138
+ width: a,
139
139
  height: s
140
140
  };
141
- if (a !== null) {
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(n) : console.error(
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
- n
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(o) {
172
- typeof window.consumeKeypresses == "function" && window.consumeKeypresses(o);
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(o, s, a = null) {
182
- var n = {
183
- channel: o
181
+ static sendChannelData(a, s, n = null) {
182
+ var o = {
183
+ channel: a
184
184
  };
185
185
  if (typeof s == "string")
186
- n.stringData = s;
186
+ o.stringData = s;
187
187
  else if (typeof s == "number")
188
- n.floatData = s;
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(n)
198
+ obj: JSON.stringify(o)
199
199
  };
200
- a !== null ? a.postMessage(e) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(e) : console.error(
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(o, s = null) {
213
- const a = {
212
+ static sendWidgetUpdate(a, s = null) {
213
+ const n = {
214
214
  command: "widgetStateUpdate",
215
- obj: JSON.stringify(CabbageUtils.sanitizeForEditor(o))
215
+ obj: JSON.stringify(CabbageUtils.sanitizeForEditor(a))
216
216
  };
217
- s !== null ? s.postMessage(a) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(a) : console.error(
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
- a
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
- static sendCustomCommand(o, s = null, a = {}) {
230
- const n = {
231
- command: o,
232
- text: JSON.stringify(a)
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(n);
271
+ s.postMessage(o);
236
272
  else if (typeof window.sendMessageFromUI == "function")
237
273
  try {
238
- const e = window.sendMessageFromUI(n);
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
- console.error(
244
- "Cabbage: window.sendMessageFromUI is not available yet. Message:",
245
- n
246
- ), console.error(
247
- "Cabbage: typeof window.sendMessageFromUI:",
248
- typeof window.sendMessageFromUI
249
- ), console.error(
250
- "Cabbage: window.sendMessageFromUI value:",
251
- window.sendMessageFromUI
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 C = (i) => {
256
- const [o, s] = w();
290
+ const y = (r) => {
291
+ const [a, s] = m();
257
292
  return f(() => {
258
- const a = (n) => {
259
- const { id: e, widgetJson: r, command: u } = n.data;
260
- if (e === i && r && u === "widgetUpdate") {
261
- const t = JSON.parse(r);
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", a), () => {
269
- window.removeEventListener("message", a);
303
+ return window.addEventListener("message", n), () => {
304
+ window.removeEventListener("message", n);
270
305
  };
271
306
  }, []), {
272
- properties: o
307
+ properties: a
273
308
  };
274
- }, F = (i, o = "complete") => {
275
- const { properties: s } = C(i), [a, n] = w(), [e, r] = w(), u = (t) => {
276
- n(t), y.sendControlData({
277
- channel: i,
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: o
314
+ gesture: a
280
315
  });
281
316
  };
282
317
  return f(() => {
283
- var m;
318
+ var c;
284
319
  const t = s == null ? void 0 : s.channels.find(
285
- (d) => d.id === i
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
- ), r(l)), a !== void 0) return;
293
- const g = (m = t.range) == null ? void 0 : m.value;
294
- g != null && (console.log(
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
- g
297
- ), n(g));
331
+ w
332
+ ), o(w));
298
333
  }, [s]), f(() => {
299
334
  const t = (l) => {
300
- var m;
301
- const { command: g } = l.data;
302
- if (g === "parameterChange") {
303
- const { value: d, paramIdx: c } = l.data.data;
304
- if (c !== e || d === null) return;
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: ${c}`,
341
+ `[Cabbage-React] Received parameterChange for parameterIndex: ${g}`,
307
342
  d
308
- ), n(d);
309
- } else if (g === "batchWidgetUpdate") {
310
- const d = l.data.widgets, c = d == null ? void 0 : d.find((p) => p.id === i);
311
- if (!c) return;
312
- const b = JSON.parse(c.widgetJson).channels.find(
313
- (p) => p.id === i
314
- ), M = (m = b == null ? void 0 : b.range) == null ? void 0 : m.value;
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: ${c.id}`,
351
+ `[Cabbage-React] Received batch widget update for channelId: ${g.id}`,
317
352
  M
318
- ), n(M);
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: a,
326
- setValue: u
360
+ value: n,
361
+ setValue: p
327
362
  };
328
- }, v = (i) => {
329
- const [o, s] = w();
363
+ }, v = (r) => {
364
+ const [a, s] = m();
330
365
  return f(() => {
331
- const a = (n) => {
332
- const { data: e, type: r } = n;
333
- if (e && r === "message") {
334
- if (e.type !== i) return;
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", a), () => {
342
- window.removeEventListener("message", a);
376
+ return window.addEventListener("message", n), () => {
377
+ window.removeEventListener("message", n);
343
378
  };
344
379
  }, []), {
345
- data: o
380
+ data: a
346
381
  };
347
382
  };
348
383
  export {
349
- y as Cabbage,
384
+ C as Cabbage,
350
385
  v as useCabbageMessage,
351
- C as useCabbageProperties,
386
+ y as useCabbageProperties,
352
387
  F as useCabbageState
353
388
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cabbage-react",
3
- "version": "1.0.47",
3
+ "version": "1.0.48",
4
4
  "type": "module",
5
5
  "keywords": [
6
6
  "cabbage",