cabbage-react 1.0.31 → 1.0.33

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 CHANGED
@@ -20,24 +20,37 @@ npm install cabbage-react
20
20
 
21
21
  Synchronize a channel with the backend. This hook:
22
22
 
23
- - Identifies and sets the default value defined in .csd to a state.
24
- - Handles initialization when opening an existing session, or reopening the plugin window.
25
- - Listens for value updates from the host (DAW), or from Csound.
26
- - Sends changes to the backend using the provided value-setter.
23
+ - Automatically updates local state when value is received or changed
24
+ - Sends changes to the backend using the provided value-setter
27
25
 
28
26
  ### useCabbageProperties
29
27
 
30
28
  Get properties for a widget from the backend. This hook:
31
29
 
32
- - Listens for property updates from the backend.
33
- - Updates local state automatically when data changes.
30
+ - Automatically updates local state when properties are received or changed
34
31
 
35
32
  ### useCabbageMessage
36
33
 
37
- Get message for a channel from the backend. This hook:
34
+ Get messages from the backend. This hook:
38
35
 
39
- - Listens for messages from the backend.
40
- - Updates local state automatically when receiving a message.
36
+ - Automatically updates local state when a message is received
37
+ - Expects a serialized JSON object (sent as a string) with an `id` property
38
+
39
+ #### Example: sending a message from Csound
40
+
41
+ ```csd
42
+ jsonData:S = sprintf({{
43
+ {
44
+ "id":"NoteData",
45
+ "noteCount":%f,
46
+ "note":%f,
47
+ "noteLength":%f,
48
+ "noteVelocity":%f
49
+ }
50
+ }}, iNoteCount, iNote, iLength, iVelocity)
51
+
52
+ cabbageSendMessage(jsonData)
53
+ ```
41
54
 
42
55
  ## Usage
43
56
 
@@ -2,9 +2,9 @@
2
2
  * Custom hook to get a message 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
- * @param channelId
5
+ * @param messageId
6
6
  */
7
- export declare const useCabbageMessage: <T>(channelId: string) => {
7
+ export declare const useCabbageMessage: <T>(messageId: string) => {
8
8
  message: T | undefined;
9
9
  };
10
10
  //# sourceMappingURL=useCabbageMessage.d.ts.map
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("react");console.log("Cabbage: loading cabbage.js");class p{static sendControlData({channel:o,value:e,gesture:s="complete"},n=null){const a={command:"controlData",channel:o,value:e,gesture:s};n!==null?n.postMessage(a):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(a):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",a)}static isReadyToLoad(o=null,e={}){this.sendCustomCommand("isReadyToLoad",o)}static sendMidiMessageFromUI(o,e,s,n=null){var a={statusByte:o,dataByte1:e,dataByte2:s};const i={command:"midiMessage",obj:JSON.stringify(a)};n!==null?n.postMessage(i):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(i):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",i)}static MidiMessageFromHost(o,e,s){console.log("Cabbage: Got MIDI Message"+o+":"+e+":"+s)}static triggerFileOpenDialog(o,e,s={}){var n={channel:e,directory:s.directory||"",filters:s.filters||"*",openAtLastKnownLocation:s.openAtLastKnownLocation!==void 0?s.openAtLastKnownLocation:!0};const a={command:"fileOpen",obj:JSON.stringify(n)};o!==null?o.postMessage(a):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(a):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",a)}static openUrl(o,e,s){var n={url:e,file:s};const a={command:"openUrl",obj:JSON.stringify(n)};o!==null?o.postMessage(a):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(a):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",a)}static requestResize(o,e,s=null){const n={command:"requestResize",width:o,height:e};if(s!==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 sendChannelData(o,e,s=null){var n={channel:o};if(typeof e=="string")n.stringData=e;else if(typeof e=="number")n.floatData=e;else{console.warn("Cabbage: sendChannelData received unsupported data type:",typeof e);return}const a={command:"channelData",obj:JSON.stringify(n)};console.log("Cabbage: sending channel data from UI",n),s!==null?s.postMessage(a):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(a):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",a)}static sendWidgetUpdate(o,e=null){const s={command:"widgetStateUpdate",obj:JSON.stringify(CabbageUtils.sanitizeForEditor(o))};e!==null?e.postMessage(s):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(s):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",s)}static sendCustomCommand(o,e=null,s={}){const n={command:o,text:JSON.stringify(s)};if(e!==null)e.postMessage(n);else if(typeof window.sendMessageFromUI=="function"){console.log("Cabbage: Calling window.sendMessageFromUI with:",n);try{const a=window.sendMessageFromUI(n);console.log("Cabbage: sendMessageFromUI returned:",a)}catch(a){console.error("Cabbage: sendMessageFromUI threw error:",a),console.error("Cabbage: Error stack:",a.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=r=>{const[o,e]=g.useState();return g.useEffect(()=>{const s=n=>{const{id:a,widgetJson:i,command:b}=n.data;if(a===r&&i&&b==="widgetUpdate"){const t=JSON.parse(i);console.log(`[Cabbage-React] Received properties for channelId ${a}`,t),e(t)}};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[]),{properties:o}},I=(r,o="complete")=>{const{properties:e}=C(r),[s,n]=g.useState(),[a,i]=g.useState(),b=t=>{n(t),p.sendControlData({channel:r,value:t,gesture:o})};return g.useEffect(()=>{var u;const t=e==null?void 0:e.channels.find(d=>d.id===r);if(!t)return;const l=t==null?void 0:t.parameterIndex;a===void 0&&l!==void 0&&(console.log(`[Cabbage-React] Received parameterIndex for channelId "${t.id}"`,l),i(l));const c=(u=t.range)==null?void 0:u.defaultValue;s===void 0&&c!==void 0&&(console.log(`[Cabbage-React] Received default value for channelId "${t.id}"`,c),n(c))},[e]),g.useEffect(()=>{const t=l=>{var u;const{command:c}=l.data;if(c==="parameterChange"){const{value:d,paramIdx:m}=l.data.data;if(m!==a||d===null)return;console.log(`[Cabbage-React] Received parameterChange for parameterIndex ${m}`,d),n(d)}else if(c==="batchWidgetUpdate"){const d=l.data.widgets,m=d==null?void 0:d.find(f=>f.id===r);if(!m)return;const w=JSON.parse(m.widgetJson).channels.find(f=>f.id===r),M=(u=w==null?void 0:w.range)==null?void 0:u.value;console.log(`[Cabbage-React] Received batch widget update for channelId ${m.id}`,M),n(M)}};return window.addEventListener("message",t),()=>{window.removeEventListener("message",t)}},[a]),{value:s,setValue:b}},U=r=>{const[o,e]=g.useState();return g.useEffect(()=>{const s=n=>{const{id:a,widgetJson:i,command:b}=n.data;if(a===r&&i&&b==="widgetUpdate"){const t=JSON.parse(i);console.log(`[Cabbage-React] Received message for channelId ${a}`,t),e(t)}};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[]),{message:o}};exports.Cabbage=p;exports.useCabbageMessage=U;exports.useCabbageProperties=C;exports.useCabbageState=I;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react");console.log("Cabbage: loading cabbage.js");class p{static sendControlData({channel:o,value:e,gesture:s="complete"},n=null){const a={command:"controlData",channel:o,value:e,gesture:s};n!==null?n.postMessage(a):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(a):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",a)}static isReadyToLoad(o=null,e={}){this.sendCustomCommand("isReadyToLoad",o)}static sendMidiMessageFromUI(o,e,s,n=null){var a={statusByte:o,dataByte1:e,dataByte2:s};const r={command:"midiMessage",obj:JSON.stringify(a)};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,e,s){console.log("Cabbage: Got MIDI Message"+o+":"+e+":"+s)}static triggerFileOpenDialog(o,e,s={}){var n={channel:e,directory:s.directory||"",filters:s.filters||"*",openAtLastKnownLocation:s.openAtLastKnownLocation!==void 0?s.openAtLastKnownLocation:!0};const a={command:"fileOpen",obj:JSON.stringify(n)};o!==null?o.postMessage(a):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(a):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",a)}static openUrl(o,e,s){var n={url:e,file:s};const a={command:"openUrl",obj:JSON.stringify(n)};o!==null?o.postMessage(a):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(a):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",a)}static requestResize(o,e,s=null){const n={command:"requestResize",width:o,height:e};if(s!==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 sendChannelData(o,e,s=null){var n={channel:o};if(typeof e=="string")n.stringData=e;else if(typeof e=="number")n.floatData=e;else{console.warn("Cabbage: sendChannelData received unsupported data type:",typeof e);return}const a={command:"channelData",obj:JSON.stringify(n)};console.log("Cabbage: sending channel data from UI",n),s!==null?s.postMessage(a):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(a):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",a)}static sendWidgetUpdate(o,e=null){const s={command:"widgetStateUpdate",obj:JSON.stringify(CabbageUtils.sanitizeForEditor(o))};e!==null?e.postMessage(s):typeof window.sendMessageFromUI=="function"?window.sendMessageFromUI(s):console.error("Cabbage: window.sendMessageFromUI is not available. Message:",s)}static sendCustomCommand(o,e=null,s={}){const n={command:o,text:JSON.stringify(s)};if(e!==null)e.postMessage(n);else if(typeof window.sendMessageFromUI=="function"){console.log("Cabbage: Calling window.sendMessageFromUI with:",n);try{const a=window.sendMessageFromUI(n);console.log("Cabbage: sendMessageFromUI returned:",a)}catch(a){console.error("Cabbage: sendMessageFromUI threw error:",a),console.error("Cabbage: Error stack:",a.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,e]=l.useState();return l.useEffect(()=>{const s=n=>{const{id:a,widgetJson:r,command:g}=n.data;if(a===i&&r&&g==="widgetUpdate"){const t=JSON.parse(r);console.log(`[Cabbage-React] Received properties for channelId ${a}`,t),e(t)}};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[]),{properties:o}},I=(i,o="complete")=>{const{properties:e}=C(i),[s,n]=l.useState(),[a,r]=l.useState(),g=t=>{n(t),p.sendControlData({channel:i,value:t,gesture:o})};return l.useEffect(()=>{var u;const t=e==null?void 0:e.channels.find(d=>d.id===i);if(!t)return;const c=t==null?void 0:t.parameterIndex;a===void 0&&c!==void 0&&(console.log(`[Cabbage-React] Received parameterIndex for channelId "${t.id}"`,c),r(c));const m=(u=t.range)==null?void 0:u.defaultValue;s===void 0&&m!==void 0&&(console.log(`[Cabbage-React] Received default value for channelId "${t.id}"`,m),n(m))},[e]),l.useEffect(()=>{const t=c=>{var u;const{command:m}=c.data;if(m==="parameterChange"){const{value:d,paramIdx:b}=c.data.data;if(b!==a||d===null)return;console.log(`[Cabbage-React] Received parameterChange for parameterIndex ${b}`,d),n(d)}else if(m==="batchWidgetUpdate"){const d=c.data.widgets,b=d==null?void 0:d.find(f=>f.id===i);if(!b)return;const w=JSON.parse(b.widgetJson).channels.find(f=>f.id===i),M=(u=w==null?void 0:w.range)==null?void 0:u.value;console.log(`[Cabbage-React] Received batch widget update for channelId ${b.id}`,M),n(M)}};return window.addEventListener("message",t),()=>{window.removeEventListener("message",t)}},[a]),{value:s,setValue:g}},U=i=>{const[o,e]=l.useState();return l.useEffect(()=>{const s=n=>{const{widgetJson:a,command:r}=n.data;if(a&&r==="widgetUpdate"){const g=JSON.parse(a);if(g.id!==i)return;console.log(`[Cabbage-React] Received message for channelId ${g.id}`,g),e(g)}};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[]),{message:o}};exports.Cabbage=p;exports.useCabbageMessage=U;exports.useCabbageProperties=C;exports.useCabbageState=I;
package/dist/index.mjs CHANGED
@@ -56,13 +56,13 @@ class C {
56
56
  dataByte1: e,
57
57
  dataByte2: s
58
58
  };
59
- const i = {
59
+ const r = {
60
60
  command: "midiMessage",
61
61
  obj: JSON.stringify(a)
62
62
  };
63
- n !== null ? n.postMessage(i) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(i) : console.error(
63
+ n !== null ? n.postMessage(r) : typeof window.sendMessageFromUI == "function" ? window.sendMessageFromUI(r) : console.error(
64
64
  "Cabbage: window.sendMessageFromUI is not available. Message:",
65
- i
65
+ r
66
66
  );
67
67
  }
68
68
  /**
@@ -236,13 +236,13 @@ class C {
236
236
  );
237
237
  }
238
238
  }
239
- const I = (r) => {
239
+ const I = (i) => {
240
240
  const [o, e] = f();
241
241
  return b(() => {
242
242
  const s = (n) => {
243
- const { id: a, widgetJson: i, command: m } = n.data;
244
- if (a === r && i && m === "widgetUpdate") {
245
- const t = JSON.parse(i);
243
+ const { id: a, widgetJson: r, command: l } = n.data;
244
+ if (a === i && r && l === "widgetUpdate") {
245
+ const t = JSON.parse(r);
246
246
  console.log(
247
247
  `[Cabbage-React] Received properties for channelId ${a}`,
248
248
  t
@@ -255,10 +255,10 @@ const I = (r) => {
255
255
  }, []), {
256
256
  properties: o
257
257
  };
258
- }, h = (r, o = "complete") => {
259
- const { properties: e } = I(r), [s, n] = f(), [a, i] = f(), m = (t) => {
258
+ }, v = (i, o = "complete") => {
259
+ const { properties: e } = I(i), [s, n] = f(), [a, r] = f(), l = (t) => {
260
260
  n(t), C.sendControlData({
261
- channel: r,
261
+ channel: i,
262
262
  value: t,
263
263
  gesture: o
264
264
  });
@@ -266,38 +266,38 @@ const I = (r) => {
266
266
  return b(() => {
267
267
  var w;
268
268
  const t = e == null ? void 0 : e.channels.find(
269
- (d) => d.id === r
269
+ (d) => d.id === i
270
270
  );
271
271
  if (!t) return;
272
- const l = t == null ? void 0 : t.parameterIndex;
273
- a === void 0 && l !== void 0 && (console.log(
272
+ const g = t == null ? void 0 : t.parameterIndex;
273
+ a === void 0 && g !== void 0 && (console.log(
274
274
  `[Cabbage-React] Received parameterIndex for channelId "${t.id}"`,
275
- l
276
- ), i(l));
277
- const g = (w = t.range) == null ? void 0 : w.defaultValue;
278
- s === void 0 && g !== void 0 && (console.log(
279
- `[Cabbage-React] Received default value for channelId "${t.id}"`,
280
275
  g
281
- ), n(g));
276
+ ), r(g));
277
+ const c = (w = t.range) == null ? void 0 : w.defaultValue;
278
+ s === void 0 && c !== void 0 && (console.log(
279
+ `[Cabbage-React] Received default value for channelId "${t.id}"`,
280
+ c
281
+ ), n(c));
282
282
  }, [e]), b(() => {
283
- const t = (l) => {
283
+ const t = (g) => {
284
284
  var w;
285
- const { command: g } = l.data;
286
- if (g === "parameterChange") {
287
- const { value: d, paramIdx: c } = l.data.data;
288
- if (c !== a || d === null) return;
285
+ const { command: c } = g.data;
286
+ if (c === "parameterChange") {
287
+ const { value: d, paramIdx: m } = g.data.data;
288
+ if (m !== a || d === null) return;
289
289
  console.log(
290
- `[Cabbage-React] Received parameterChange for parameterIndex ${c}`,
290
+ `[Cabbage-React] Received parameterChange for parameterIndex ${m}`,
291
291
  d
292
292
  ), n(d);
293
- } else if (g === "batchWidgetUpdate") {
294
- const d = l.data.widgets, c = d == null ? void 0 : d.find((M) => M.id === r);
295
- if (!c) return;
296
- const u = JSON.parse(c.widgetJson).channels.find(
297
- (M) => M.id === r
293
+ } else if (c === "batchWidgetUpdate") {
294
+ const d = g.data.widgets, m = d == null ? void 0 : d.find((M) => M.id === i);
295
+ if (!m) return;
296
+ const u = JSON.parse(m.widgetJson).channels.find(
297
+ (M) => M.id === i
298
298
  ), p = (w = u == null ? void 0 : u.range) == null ? void 0 : w.value;
299
299
  console.log(
300
- `[Cabbage-React] Received batch widget update for channelId ${c.id}`,
300
+ `[Cabbage-React] Received batch widget update for channelId ${m.id}`,
301
301
  p
302
302
  ), n(p);
303
303
  }
@@ -307,19 +307,20 @@ const I = (r) => {
307
307
  };
308
308
  }, [a]), {
309
309
  value: s,
310
- setValue: m
310
+ setValue: l
311
311
  };
312
- }, v = (r) => {
312
+ }, y = (i) => {
313
313
  const [o, e] = f();
314
314
  return b(() => {
315
315
  const s = (n) => {
316
- const { id: a, widgetJson: i, command: m } = n.data;
317
- if (a === r && i && m === "widgetUpdate") {
318
- const t = JSON.parse(i);
316
+ const { widgetJson: a, command: r } = n.data;
317
+ if (a && r === "widgetUpdate") {
318
+ const l = JSON.parse(a);
319
+ if (l.id !== i) return;
319
320
  console.log(
320
- `[Cabbage-React] Received message for channelId ${a}`,
321
- t
322
- ), e(t);
321
+ `[Cabbage-React] Received message for channelId ${l.id}`,
322
+ l
323
+ ), e(l);
323
324
  }
324
325
  };
325
326
  return window.addEventListener("message", s), () => {
@@ -331,7 +332,7 @@ const I = (r) => {
331
332
  };
332
333
  export {
333
334
  C as Cabbage,
334
- v as useCabbageMessage,
335
+ y as useCabbageMessage,
335
336
  I as useCabbageProperties,
336
- h as useCabbageState
337
+ v as useCabbageState
337
338
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cabbage-react",
3
- "version": "1.0.31",
3
+ "version": "1.0.33",
4
4
  "type": "module",
5
5
  "keywords": [
6
6
  "cabbage",