0
Fork 0
mirror of https://github.com/penpot/penpot-plugins.git synced 2025-01-09 00:10:12 -05:00
penpot-plugins/docs/plugin-usage.md

79 lines
1.1 KiB
Markdown
Raw Normal View History

2024-03-12 01:48:02 -05:00
# Plugin usage
If you want your plugin to be opened in a modal, then use open UI:
2024-02-06 06:16:28 -05:00
```ts
2024-05-22 01:21:06 -05:00
penpot.ui.open('Plugin name', '/app.html', {
2024-02-06 06:16:28 -05:00
width: 500,
height: 600,
});
```
2024-02-20 06:18:59 -05:00
Get state:
```ts
// file file state
penpot.ui.getFileState();
// file page state
2024-02-28 06:54:37 -05:00
penpot.ui.getPageState();
2024-02-20 06:18:59 -05:00
// selection id
penpot.ui.getSelection();
2024-03-08 06:27:58 -05:00
// current theme (dark/light)
penpot.ui.getTheme();
2024-02-20 06:18:59 -05:00
```
2024-02-06 06:16:28 -05:00
### Messages
Receive message from iframe:
```ts
penpot.ui.onMessage((message) => {
2024-05-09 06:07:16 -05:00
console.log('Received message:', message);
2024-02-06 06:16:28 -05:00
});
```
Send message from iframe:
```ts
parent.postMessage({ content: 'text' }, '*');
```
Send message from plugin
```ts
penpot.ui.sendMessage({ type: 'hello' });
```
Send message from plugin:
```ts
window.addEventListener('message', function (event) {
console.log('Message received from plugin: ', event.data);
});
```
### Events
2024-03-08 06:27:58 -05:00
Current events `pagechange`, `filechange`,`selectionchange` and `themechange`.
2024-02-06 06:16:28 -05:00
```ts
const event = (page) => {
2024-05-09 06:07:16 -05:00
console.log(page.name);
2024-02-06 06:16:28 -05:00
};
penpot.on('pagechange', event);
penpot.off('pagechange', event);
```
2024-03-04 08:11:34 -05:00
### Requests
Same as the browser fetch API.
```ts
penpot.fetch('http://example.com/movies.json');
```