@foxglove/embed 0.8.0 → 0.10.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/dist/types.d.ts CHANGED
@@ -56,6 +56,84 @@ export type FoxgloveViewerEventMap = {
56
56
  ready: Event;
57
57
  error: CustomEvent<string>;
58
58
  };
59
+ /**
60
+ * A modifier key that must be held alongside the primary key.
61
+ *
62
+ * `"PrimaryModifier"` resolves to Command on macOS and Control on other platforms.
63
+ */
64
+ export type ShortcutModifier = "Meta" | "Control" | "Alt" | "Shift" | "PrimaryModifier";
65
+ /**
66
+ * Valid `event.key` values for key bindings matched by logical key.
67
+ *
68
+ * Only unmodified key values are allowed — these are the values produced
69
+ * without holding Shift or AltGr. This ensures the browser's Keyboard API
70
+ * (`navigator.keyboard.getLayoutMap()`) can resolve them across layouts.
71
+ *
72
+ * @see https://www.w3.org/TR/uievents-key/
73
+ */
74
+ export type EventKeyValue = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "-" | "=" | "[" | "]" | "\\" | ";" | "'" | "`" | "," | "." | "/" | "Enter" | "Escape" | "Backspace" | "Tab" | " " | "Delete" | "Insert" | "Home" | "End" | "PageUp" | "PageDown" | "ArrowUp" | "ArrowDown" | "ArrowLeft" | "ArrowRight" | "CapsLock" | "F1" | "F2" | "F3" | "F4" | "F5" | "F6" | "F7" | "F8" | "F9" | "F10" | "F11" | "F12" | "F13" | "F14" | "F15" | "F16" | "F17" | "F18" | "F19" | "F20" | "F21" | "F22" | "F23" | "F24";
75
+ /**
76
+ * Valid `event.code` values for key bindings matched by physical key code.
77
+ *
78
+ * @see https://www.w3.org/TR/uievents-code/
79
+ */
80
+ export type EventCodeValue = "KeyA" | "KeyB" | "KeyC" | "KeyD" | "KeyE" | "KeyF" | "KeyG" | "KeyH" | "KeyI" | "KeyJ" | "KeyK" | "KeyL" | "KeyM" | "KeyN" | "KeyO" | "KeyP" | "KeyQ" | "KeyR" | "KeyS" | "KeyT" | "KeyU" | "KeyV" | "KeyW" | "KeyX" | "KeyY" | "KeyZ" | "Digit0" | "Digit1" | "Digit2" | "Digit3" | "Digit4" | "Digit5" | "Digit6" | "Digit7" | "Digit8" | "Digit9" | "Minus" | "Equal" | "BracketLeft" | "BracketRight" | "Backslash" | "Semicolon" | "Quote" | "Backquote" | "Comma" | "Period" | "Slash" | "Enter" | "Escape" | "Backspace" | "Tab" | "Space" | "Delete" | "Insert" | "Home" | "End" | "PageUp" | "PageDown" | "ArrowUp" | "ArrowDown" | "ArrowLeft" | "ArrowRight" | "CapsLock" | "F1" | "F2" | "F3" | "F4" | "F5" | "F6" | "F7" | "F8" | "F9" | "F10" | "F11" | "F12" | "F13" | "F14" | "F15" | "F16" | "F17" | "F18" | "F19" | "F20" | "F21" | "F22" | "F23" | "F24";
81
+ export type FoxgloveCommandId = "player.togglePlayback" | "player.seekBackwardMedium" | "player.seekForwardMedium" | "player.seekBackwardSmall" | "player.seekForwardSmall" | "player.seekBackwardLarge" | "player.seekForwardLarge" | "player.seekStart" | "player.seekEnd" | "player.speedUp" | "player.speedDown" | "player.createEvent" | "panel.threeDee.togglePerspective" | "panel.threeDee.resetCamera" | "panel.threeDee.toggleInspector" | "panel.threeDee.moveForward" | "panel.threeDee.moveLeft" | "panel.threeDee.moveBackward" | "panel.threeDee.moveRight" | "panel.threeDee.rotateUp" | "panel.threeDee.rotateDown" | "panel.threeDee.rotateLeft" | "panel.threeDee.rotateRight" | "sidebar.left.toggleVisibility" | "sidebar.right.toggleVisibility";
82
+ /**
83
+ * A key that triggers a keybinding.
84
+ *
85
+ * Physical key codes like `"KeyW"` match by physical position (layout-independent).
86
+ *
87
+ * Character keys like `"p"` match by the character produced (layout-dependent).
88
+ *
89
+ * Named keys like `"Escape"` are unambiguous — identical in both modes.
90
+ */
91
+ export type ShortcutKey = EventKeyValue | EventCodeValue;
92
+ /**
93
+ * A key combination consisting of a {@link ShortcutKey} plus optional modifier keys.
94
+ *
95
+ * Physical key codes like `"KeyW"`, `"Space"` match by `event.code` (physical position).
96
+ *
97
+ * Character keys like `"p"`, `" "` match by `event.key` (logical key).
98
+ *
99
+ * Named keys like `"Escape"`, `"Enter"` are layout-independent and matched by `event.key`.
100
+ * `"PrimaryModifier"` resolves to Command on macOS and Control elsewhere.
101
+ *
102
+ * @example
103
+ * // Match by character `a` and modifier `PrimaryModifier` (Command on macOS, Control elsewhere)
104
+ * // The key will depend on the keyboard layout. On a QWERTY layout, this will match the key to the left of the S key.
105
+ * // On an AZERTY layout, this will match the key to the left of the Z key.
106
+ * { key: "a", modifiers: ["PrimaryModifier"] }
107
+ *
108
+ * // Match by physical key code `KeyA` and modifier `PrimaryModifier` (Command on macOS, Control elsewhere)
109
+ * // The key won't depend on the keyboard layout. On a QWERTY layout, this will match the key to the left of the A key.
110
+ * // On an AZERTY layout, this will match the key to the left of the S key.
111
+ * { key: "KeyA", modifiers: ["PrimaryModifier"] }
112
+ *
113
+ * // Match by named key `Escape`. This is layout-independent and will match the same key regardless of the keyboard layout.
114
+ * { key: "Escape" }
115
+ *
116
+ * @see ShortcutKey
117
+ */
118
+ export interface KeybindingBase {
119
+ /** The key that triggers this keybinding. */
120
+ key: ShortcutKey;
121
+ /** Modifier keys required alongside the primary key. Defaults to no modifiers if omitted. */
122
+ modifiers?: ShortcutModifier[];
123
+ }
124
+ export interface CommandKeybinding extends KeybindingBase {
125
+ /** A built-in viewer command to invoke, or `"noop"` to suppress the key without action. */
126
+ command: FoxgloveCommandId | "noop";
127
+ }
128
+ export interface HandlerKeybinding extends KeybindingBase {
129
+ /** A function to call when the key combination is pressed. */
130
+ handler: () => void;
131
+ }
132
+ /**
133
+ * A keybinding defines a key combination that triggers a built-in
134
+ * viewer command or a custom handler function.
135
+ */
136
+ export type Keybinding = CommandKeybinding | HandlerKeybinding;
59
137
  export type FoxgloveViewerEventType = keyof FoxgloveViewerEventMap;
60
138
  export type SelectLayoutParams = {
61
139
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAEpD,cAAc,uBAAuB,CAAC;AAEtC;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,OAAO,CAAC;AAEvC,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,WAAW,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,iGAAiG;IACjG,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,YAAY,GACpB;IACE,IAAI,EAAE,QAAQ,CAAC;IACf,yCAAyC;IACzC,QAAQ,EAAE,MAAM,CAAC;IACjB,6EAA6E;IAC7E,KAAK,EAAE,MAAM,CAAC;IACd,6EAA6E;IAC7E,GAAG,EAAE,MAAM,CAAC;IACZ,iGAAiG;IACjG,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,GACD;IACE,IAAI,EAAE,QAAQ,CAAC;IACf,2CAA2C;IAC3C,UAAU,EAAE,MAAM,CAAC;IACnB,6EAA6E;IAC7E,KAAK,EAAE,MAAM,CAAC;IACd,6EAA6E;IAC7E,GAAG,EAAE,MAAM,CAAC;IACZ,iGAAiG;IACjG,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEN,MAAM,MAAM,kBAAkB,GAAG,oBAAoB,GAAG,qBAAqB,CAAC;AAE9E,MAAM,MAAM,UAAU,GAAG;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,kBAAkB,CAAC;IAC7B,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IA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AChC,4BAA4B,GAC5B,yBAAyB,GACzB,6BAA6B,GAC7B,0BAA0B,GAC1B,yBAAyB,GACzB,2BAA2B,GAC3B,2BAA2B,GAC3B,4BAA4B,GAC5B,+BAA+B,GAC/B,gCAAgC,CAAC;AAErC;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,GAAG,aAAa,GAAG,cAAc,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,WAAW,cAAc;IAC7B,6CAA6C;IAC7C,GAAG,EAAE,WAAW,CAAC;IACjB,6FAA6F;IAC7F,SAAS,CAAC,EAAE,gBAAgB,EAAE,CAAC;CAChC;AAED,MAAM,WAAW,iBAAkB,SAAQ,cAAc;IACvD,2FAA2F;IAC3F,OAAO,EAAE,iBAAiB,GAAG,MAAM,CAAC;CACrC;AAED,MAAM,WAAW,iBAAkB,SAAQ,cAAc;IACvD,8DAA8D;IAC9D,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG,iBAAiB,GAAG,iBAAiB,CAAC;AAE/D,MAAM,MAAM,uBAAuB,GAAG,MAAM,sBAAsB,CAAC;AAEnE,MAAM,MAAM,kBAAkB,GAC1B;IACE;;;SAGK;IACL,UAAU,EAAE,MAAM,CAAC;IACnB;;;;SAIK;IACL,YAAY,CAAC,EAAE,gBAAgB,CAAC;IAChC,4FAA4F;IAC5F,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,GACD;IACE;;;SAGK;IACL,UAAU,EAAE,MAAM,CAAC;IACnB;;;SAGK;IACL,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,sFAAsF;IACtF,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foxglove/embed",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "author": {
@@ -9,14 +9,18 @@ import type {
9
9
  IframeOriginAckCommand,
10
10
  IframeSelectLayoutCommand,
11
11
  IframeSetDatasourceCommand,
12
+ IframeSetKeybindingConfigCommand,
12
13
  IframeSetLayoutCommand,
14
+ SerializedKeybindingConfig,
13
15
  } from "./commands.ts";
14
16
  import type {
15
17
  DataSource,
16
18
  FoxgloveViewerEventMap,
17
19
  FoxgloveViewerEventType,
20
+ Keybinding,
18
21
  OpaqueLayoutData,
19
22
  SelectLayoutParams,
23
+ KeybindingBase,
20
24
  } from "./types.ts";
21
25
 
22
26
  interface FoxgloveEventTarget extends EventTarget {
@@ -96,9 +100,41 @@ export type FoxgloveViewerOptions = {
96
100
 
97
101
  /**
98
102
  * Identifies the embedding SDK client for analytics purposes. This should be set by SDK
99
- * libraries (e.g. Python SDK) to identify the embedding context.
103
+ * libraries (such as the Python SDK) to identify the embedding context.
100
104
  */
101
105
  embeddedViewer?: EmbeddedViewer;
106
+
107
+ /**
108
+ * Custom keybindings for the embedded viewer.
109
+ *
110
+ * Overrides default keybindings and registers custom handlers.
111
+ *
112
+ * Custom keybindings take priority over the viewer's default bindings. If multiple keybindings
113
+ * share the same key + modifiers, the first one in the list wins and the rest are ignored.
114
+ *
115
+ * @example
116
+ * [
117
+ * // Register a custom keybinding
118
+ * {
119
+ * key: "s",
120
+ * modifiers: ["Control", "Shift"],
121
+ * handler: () => {
122
+ * console.log("Custom keybinding triggered: take-snapshot");
123
+ * },
124
+ * },
125
+ * // Configure the "p" key to trigger the "player.togglePlayback" command
126
+ * {
127
+ * key: "p",
128
+ * command: "player.togglePlayback",
129
+ * },
130
+ * // Suppress a built-in keybinding
131
+ * {
132
+ * key: "[",
133
+ * command: "noop",
134
+ * },
135
+ * ]
136
+ */
137
+ keybindings?: Keybinding[];
102
138
  };
103
139
 
104
140
  /**
@@ -118,16 +154,20 @@ export class FoxgloveViewer extends TypedEventTarget {
118
154
  dataSource: IframeSetDatasourceCommand | undefined;
119
155
  selectLayout: IframeSelectLayoutCommand | undefined;
120
156
  extension: Array<IframeInstallExtensionCommand>;
157
+ keybindings: IframeSetKeybindingConfigCommand | undefined;
121
158
  } = {
122
159
  dataSource: undefined,
123
160
  selectLayout: undefined,
124
161
  extension: [],
162
+ keybindings: undefined,
125
163
  };
126
164
  /**
127
165
  * If a layout export request has been made, this holds the promise that will be resolved when a
128
166
  * response is received from the embed
129
167
  */
130
168
  #getLayoutPromise?: PromiseWithResolvers<OpaqueLayoutData>;
169
+ /** Maps auto-generated custom keybinding IDs to user-provided handlers. */
170
+ #customKeybindingHandlers = new Map<string, () => void>();
131
171
  /** Whether the frame has been destroyed. */
132
172
  #isDestroyed = false;
133
173
 
@@ -143,6 +183,7 @@ export class FoxgloveViewer extends TypedEventTarget {
143
183
  initialExtensions,
144
184
  colorScheme = "auto",
145
185
  embeddedViewer,
186
+ keybindings,
146
187
  } = options;
147
188
 
148
189
  this.#orgSlug = orgSlug;
@@ -181,11 +222,17 @@ export class FoxgloveViewer extends TypedEventTarget {
181
222
  this.installExtensions(initialExtensions);
182
223
  }
183
224
 
225
+ if (keybindings != undefined) {
226
+ this.setKeybindings(keybindings);
227
+ }
228
+
184
229
  // Create the iframe element.
185
230
  this.#iframe = document.createElement("iframe");
186
231
  this.#iframe.src = this.#server.href;
187
232
  this.#iframe.title = "Foxglove";
188
- this.#iframe.allow = "cross-origin-isolated";
233
+ // keyboard-map is required so the iframe can use navigator.keyboard.getLayoutMap()
234
+ // to detect the user's keyboard layout for correct shortcut handling.
235
+ this.#iframe.allow = "cross-origin-isolated; keyboard-map *";
189
236
  this.#iframe.style.width = "100%";
190
237
  this.#iframe.style.height = "100%";
191
238
  this.#iframe.style.border = "none";
@@ -304,6 +351,48 @@ export class FoxgloveViewer extends TypedEventTarget {
304
351
  });
305
352
  }
306
353
 
354
+ /**
355
+ * Customize keybinding configuration for this viewer instance.
356
+ *
357
+ * Custom keybindings take priority over the viewer's default bindings. If multiple keybindings
358
+ * share the same key, the first one in the list wins and the rest are ignored. Each call fully
359
+ * replaces the previous configuration. Calling with an empty array restores the viewer's default
360
+ * keybindings and clears all custom keybindings.
361
+ *
362
+ * @example
363
+ * viewer.setKeybindings([
364
+ * // Register a keybinding to trigger a custom function
365
+ * {
366
+ * key: "s",
367
+ * modifiers: ["Control", "Shift"],
368
+ * handler: () => {
369
+ * console.log("Custom keybinding triggered: take-snapshot");
370
+ * },
371
+ * },
372
+ * // Configure the "p" key to trigger the "player.togglePlayback" command
373
+ * {
374
+ * key: "p",
375
+ * command: "player.togglePlayback",
376
+ * },
377
+ * // Suppress a built-in keybinding
378
+ * {
379
+ * key: "[",
380
+ * command: "noop",
381
+ * },
382
+ * ]);
383
+ *
384
+ * // Restore the viewer's default keybindings
385
+ * viewer.setKeybindings([]);
386
+ *
387
+ */
388
+ public setKeybindings(keybindings: Keybinding[]): void {
389
+ const serialized = this.#serializeKeybindings(keybindings);
390
+ this.#queueCommand({
391
+ type: "set-keybinding-config",
392
+ payload: serialized,
393
+ });
394
+ }
395
+
307
396
  /** Whether the frame is ready to receive commands. */
308
397
  public isReady(): boolean {
309
398
  return this.#isReady;
@@ -329,6 +418,48 @@ export class FoxgloveViewer extends TypedEventTarget {
329
418
  this.#iframe.style.colorScheme = colorScheme === "auto" ? "inherit" : colorScheme;
330
419
  }
331
420
 
421
+ /**
422
+ * Converts the public flat `Keybinding[]` into the internal wire format
423
+ * (`SerializedKeybindingConfig`) for postMessage, decomposing keybindings
424
+ * into overrides and custom shortcuts.
425
+ *
426
+ * - `CommandKeybinding` (non-noop) becomes a `ShortcutOverride`
427
+ * - `HandlerKeybinding` becomes a `SerializedCustomShortcut` with a stored handler
428
+ * - `CommandKeybinding` with `"noop"` becomes a `SerializedCustomShortcut` with
429
+ * no stored handler — it claims the key slot to suppress defaults
430
+ *
431
+ * Handler keybindings are serialized before noop keybindings so that handlers
432
+ * win conflicts over noop suppressions.
433
+ */
434
+ #serializeKeybindings(keybindings: Keybinding[]): SerializedKeybindingConfig {
435
+ this.#customKeybindingHandlers.clear();
436
+
437
+ const overrides: Required<SerializedKeybindingConfig>["overrides"] = [];
438
+ const handlerCustom: Required<SerializedKeybindingConfig>["custom"] = [];
439
+ const noopCustom: Required<SerializedKeybindingConfig>["custom"] = [];
440
+
441
+ for (const kb of keybindings) {
442
+ const binding: KeybindingBase = { key: kb.key, modifiers: kb.modifiers };
443
+
444
+ const prefix = [...(kb.modifiers ?? []), kb.key].join("+");
445
+ if ("handler" in kb) {
446
+ const id = `${prefix}-${crypto.randomUUID()}`;
447
+ this.#customKeybindingHandlers.set(id, kb.handler);
448
+ handlerCustom.push({ id, binding });
449
+ } else if (kb.command === "noop") {
450
+ const id = `noop-${prefix}-${crypto.randomUUID()}`;
451
+ noopCustom.push({ id, binding });
452
+ } else {
453
+ overrides.push({ id: kb.command, binding });
454
+ }
455
+ }
456
+
457
+ return {
458
+ overrides,
459
+ custom: [...handlerCustom, ...noopCustom],
460
+ };
461
+ }
462
+
332
463
  /**
333
464
  * Queue a command to be executed when the frame is ready.
334
465
  *
@@ -356,6 +487,9 @@ export class FoxgloveViewer extends TypedEventTarget {
356
487
  case "select-layout":
357
488
  this.#latestCommands.selectLayout = command;
358
489
  break;
490
+ case "set-keybinding-config":
491
+ this.#latestCommands.keybindings = command;
492
+ break;
359
493
  }
360
494
 
361
495
  if (this.#isReady) {
@@ -404,6 +538,7 @@ export class FoxgloveViewer extends TypedEventTarget {
404
538
  initialDataSource: this.#latestCommands.dataSource?.payload,
405
539
  initialLayoutParams: this.#latestCommands.selectLayout?.payload,
406
540
  initialExtensions: this.#latestCommands.extension.flatMap((command) => command.payload),
541
+ initialKeybindingsConfig: this.#latestCommands.keybindings?.payload,
407
542
  },
408
543
  });
409
544
 
@@ -430,6 +565,12 @@ export class FoxgloveViewer extends TypedEventTarget {
430
565
  }
431
566
  break;
432
567
 
568
+ case "foxglove-keybinding-triggered": {
569
+ const handler = this.#customKeybindingHandlers.get(message.data.payload.keybindingId);
570
+ handler?.();
571
+ break;
572
+ }
573
+
433
574
  default:
434
575
  console.warn("[FoxgloveViewer] Unhandled message type:", message.data);
435
576
  break;
package/src/commands.ts CHANGED
@@ -1,4 +1,51 @@
1
- import type { DataSource, OpaqueLayoutData, SelectLayoutParams } from "./types.ts";
1
+ /**
2
+ * This file contains type definitions to define the communication protocol between the parent page and the embedded iframe.
3
+ *
4
+ * These type are only meant for internal usage and are bound to change without notice.
5
+ */
6
+
7
+ import type {
8
+ DataSource,
9
+ FoxgloveCommandId,
10
+ OpaqueLayoutData,
11
+ SelectLayoutParams,
12
+ KeybindingBase,
13
+ } from "./types.ts";
14
+
15
+ /**
16
+ * Override or disable a built-in viewer shortcut.
17
+ *
18
+ * Provide a `binding` to remap the shortcut, or `null` to disable it.
19
+ *
20
+ * When multiple overrides share the same binding, the first one in the list wins
21
+ * and the rest are cleared.
22
+ */
23
+ type ShortcutOverride = {
24
+ /** The built-in shortcut to override. */
25
+ id: FoxgloveCommandId;
26
+ /** The new key combination, or `null` to disable the shortcut. */
27
+ // eslint-disable-next-line no-restricted-syntax -- Use null to clear a shortcut
28
+ binding: KeybindingBase | null;
29
+ };
30
+
31
+ /**
32
+ * Serializable representation of a custom shortcut used for postMessage communication
33
+ * between the parent page and the embedded iframe.
34
+ */
35
+ type SerializedCustomShortcut = {
36
+ /** Auto-generated identifier used to correlate iframe events back to handlers. */
37
+ id: string;
38
+ /** The key combination to listen for. */
39
+ binding: KeybindingBase;
40
+ };
41
+
42
+ /**
43
+ * Serializable keybinding configuration sent over postMessage to the iframe.
44
+ */
45
+ export type SerializedKeybindingConfig = {
46
+ overrides?: ShortcutOverride[];
47
+ custom?: SerializedCustomShortcut[];
48
+ };
2
49
 
3
50
  export type IframeOriginAckCommand = {
4
51
  type: "origin-ack";
@@ -28,6 +75,11 @@ export type IframeInstallExtensionCommand = {
28
75
  payload: Array<string | File>;
29
76
  };
30
77
 
78
+ export type IframeSetKeybindingConfigCommand = {
79
+ type: "set-keybinding-config";
80
+ payload: SerializedKeybindingConfig;
81
+ };
82
+
31
83
  export type IframeHandshakeAckCommand = {
32
84
  type: "handshake-ack";
33
85
  payload: {
@@ -36,6 +88,7 @@ export type IframeHandshakeAckCommand = {
36
88
  initialLayout?: OpaqueLayoutData;
37
89
  initialLayoutParams?: SelectLayoutParams;
38
90
  initialExtensions: Array<string | File>;
91
+ initialKeybindingsConfig?: SerializedKeybindingConfig;
39
92
  };
40
93
  };
41
94
 
@@ -46,6 +99,7 @@ export type IframeCommand =
46
99
  | IframeSelectLayoutCommand
47
100
  | IframeGetLayoutCommand
48
101
  | IframeInstallExtensionCommand
102
+ | IframeSetKeybindingConfigCommand
49
103
  | IframeHandshakeAckCommand;
50
104
 
51
105
  export type IframeEvent =
@@ -53,4 +107,5 @@ export type IframeEvent =
53
107
  | { type: "foxglove-handshake-request" }
54
108
  | { type: "foxglove-handshake-complete" }
55
109
  | { type: "foxglove-error"; payload: string }
56
- | { type: "foxglove-layout-data"; payload: OpaqueLayoutData };
110
+ | { type: "foxglove-layout-data"; payload: OpaqueLayoutData }
111
+ | { type: "foxglove-keybinding-triggered"; payload: { keybindingId: string } };
@@ -523,6 +523,14 @@ export type BaseRendererVoxelGridTopicSettings = BaseRendererBaseGridSettings &
523
523
  * via the `definition` "BaseRendererUrdfBaseSettings".
524
524
  */
525
525
  export type BaseRendererUrdfBaseSettings = BaseRendererBaseSettings & {
526
+ /**
527
+ * How the robot joints are controlled.
528
+ */
529
+ controlMode?: "transforms" | "jointStates";
530
+ /**
531
+ * Topic containing joint state messages to control robot joints. Only used when controlMode is 'jointStates'.
532
+ */
533
+ jointStatesTopic?: string;
526
534
  /**
527
535
  * Robot link geometries to display.
528
536
  *
@@ -774,6 +782,14 @@ export type BaseRendererUrdfLayerSettings = BaseRendererBaseLayerSettings & {
774
782
  * Prefix for robot's transforms. Also commonly known as a TF prefix.
775
783
  */
776
784
  framePrefix?: string;
785
+ /**
786
+ * How the robot joints are controlled.
787
+ */
788
+ controlMode?: "transforms" | "jointStates";
789
+ /**
790
+ * Topic containing joint state messages to control robot joints. Only used when controlMode is 'jointStates'.
791
+ */
792
+ jointStatesTopic?: string;
777
793
  /**
778
794
  * Robot link geometries to display.
779
795
  *