mirror of
https://github.com/penpot/penpot.git
synced 2026-05-14 20:43:55 +00:00
✨ Add optional parameter throwOnError to penpot.ui.sendMessage
This provides more flexibility to callers, who may need to react to a failure appropriately.
This commit is contained in:
parent
2a326ba23e
commit
94a5c6c4fd
@ -1,6 +1,7 @@
|
||||
## 1.5.0 (Unreleased)
|
||||
|
||||
- **plugins-runtime**: Added `version` field that returns the current version
|
||||
- **plugins-runtime**: Added optional parameter `throwOnError` to `penpot.ui.sendMessage` (default false, backwards-compatible)
|
||||
- **plugin-types**: Added a flags subcontexts with the flag `naturalChildrenOrdering`
|
||||
- **plugin-types**: Fix penpot.openPage() to navigate in same tab by default
|
||||
- **plugin-types:** Change `LibraryComponent.isVariant()` return type to type guard `this is LibraryVariantComponent`
|
||||
|
||||
3
plugins/libs/plugin-types/index.d.ts
vendored
3
plugins/libs/plugin-types/index.d.ts
vendored
@ -54,13 +54,14 @@ export interface Penpot extends Omit<
|
||||
* Sends a message to the plugin UI.
|
||||
*
|
||||
* @param message content usually is an object
|
||||
* @param throwOnError if true, throws an error when the message cannot be cloned instead of logging to console (default: false)
|
||||
*
|
||||
* @example
|
||||
* ```js
|
||||
* this.sendMessage({ type: 'example-type', content: 'data we want to share' });
|
||||
* ```
|
||||
*/
|
||||
sendMessage: (message: unknown) => void;
|
||||
sendMessage: (message: unknown, throwOnError?: boolean) => void;
|
||||
/**
|
||||
* This is usually used in the `plugin.ts` file in order to handle the data sent by our plugin
|
||||
*
|
||||
|
||||
@ -67,17 +67,24 @@ export function createApi(
|
||||
return plugin.resizeModal(width, height);
|
||||
},
|
||||
|
||||
sendMessage(message: unknown) {
|
||||
sendMessage(message: unknown, throwOnError = false) {
|
||||
let cloneableMessage: unknown;
|
||||
|
||||
try {
|
||||
cloneableMessage = structuredClone(message);
|
||||
} catch (err) {
|
||||
console.error(
|
||||
const msg =
|
||||
'plugin sendMessage: the message could not be cloned. ' +
|
||||
'Ensure the message does not contain functions, DOM nodes, or other non-serializable values.',
|
||||
err,
|
||||
);
|
||||
'Ensure the message does not contain functions, DOM nodes, or other non-serializable values.';
|
||||
if (throwOnError) {
|
||||
throw new Error(
|
||||
msg +
|
||||
' Original error: ' +
|
||||
(err instanceof Error ? err.message : String(err)),
|
||||
);
|
||||
} else {
|
||||
console.error(msg, err);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user