0
Fork 0
mirror of https://github.com/penpot/penpot-plugins.git synced 2025-01-22 14:49:27 -05:00

feat(poc-state-plugin): add example for plugin data

This commit is contained in:
alonso.torres 2024-06-07 11:30:51 +02:00 committed by Alonso Torres
parent fbee228760
commit 4b506d24c2
2 changed files with 40 additions and 2 deletions

View file

@ -13,6 +13,9 @@ import type { PenpotShape } from '@penpot/plugin-types';
<p> <p>
Current project name: <span>{{ projectName() }}</span> Current project name: <span>{{ projectName() }}</span>
</p> </p>
<p>
Counter: <span>{{ counter() }}</span>
</p>
<form [formGroup]="form" (ngSubmit)="updateName()"> <form [formGroup]="form" (ngSubmit)="updateName()">
<div class="name-wrap"> <div class="name-wrap">
@ -66,6 +69,13 @@ import type { PenpotShape } from '@penpot/plugin-types';
> >
Create color palette board Create color palette board
</button> </button>
<button
type="button"
data-appearance="secondary"
(click)="increaseCounter()"
>
+COUNTER
</button>
</div> </div>
<p> <p>
@ -88,11 +98,13 @@ export class AppComponent {
#fileId = null; #fileId = null;
#revn = 0; #revn = 0;
#selection = signal<PenpotShape[]>([]); #selection = signal<PenpotShape[]>([]);
form = new FormGroup({ form = new FormGroup({
name: new FormControl(''), name: new FormControl(''),
}); });
theme = signal(''); theme = signal('');
projectName = signal('Unknown'); projectName = signal('Unknown');
counter = signal(0);
constructor() { constructor() {
window.addEventListener('message', (event) => { window.addEventListener('message', (event) => {
@ -106,14 +118,18 @@ export class AppComponent {
); );
} else if (event.data.type === 'selection') { } else if (event.data.type === 'selection') {
this.#refreshSelection(event.data.content.selection); this.#refreshSelection(event.data.content.selection);
this.counter.set(event.data.content.counter);
} else if (event.data.type === 'init') { } else if (event.data.type === 'init') {
this.#fileId = event.data.content.fileId; this.#fileId = event.data.content.fileId;
this.#revn = event.data.content.revn; this.#revn = event.data.content.revn;
this.#refreshPage(event.data.content.pageId, event.data.content.name); this.#refreshPage(event.data.content.pageId, event.data.content.name);
this.#refreshSelection(event.data.content.selection); this.#refreshSelection(event.data.content.selection);
this.theme.set(event.data.content.theme); this.theme.set(event.data.content.theme);
this.counter.set(event.data.content.counter);
} else if (event.data.type === 'theme') { } else if (event.data.type === 'theme') {
this.theme.set(event.data.content); this.theme.set(event.data.content);
} else if (event.data.type === 'update-counter') {
this.counter.set(event.data.content.counter);
} }
}); });
@ -174,6 +190,10 @@ export class AppComponent {
this.#sendMessage({ content: 'create-colors' }); this.#sendMessage({ content: 'create-colors' });
} }
increaseCounter() {
this.#sendMessage({ content: 'increase-counter' });
}
#sendMessage(message: unknown) { #sendMessage(message: unknown) {
parent.postMessage(message, '*'); parent.postMessage(message, '*');
} }

View file

@ -16,6 +16,11 @@ penpot.ui.onMessage<{ content: string; data: unknown }>((message) => {
return; return;
} }
const selection = penpot.selection;
const data: string | null =
selection.length === 1 ? selection[0].getPluginData('counter') : null;
const counter = data ? parseInt(data, 10) : 0;
penpot.ui.sendMessage({ penpot.ui.sendMessage({
type: 'init', type: 'init',
content: { content: {
@ -24,7 +29,8 @@ penpot.ui.onMessage<{ content: string; data: unknown }>((message) => {
fileId: file.id, fileId: file.id,
revn: file.revn, revn: file.revn,
theme: penpot.getTheme(), theme: penpot.getTheme(),
selection: penpot.getSelectedShapes(), selection,
counter,
}, },
}); });
} else if (message.content === 'change-name') { } else if (message.content === 'change-name') {
@ -254,6 +260,15 @@ Phasellus fringilla tortor elit, ac dictum tellus posuere sodales. Ut eget imper
board.appendChild(text); board.appendChild(text);
} }
} }
} else if (message.content === 'increase-counter') {
const selection = penpot.selection;
const data: string | null =
selection.length === 1 ? selection[0].getPluginData('counter') : null;
let counter = data ? parseInt(data, 10) : 0;
counter++;
selection[0].setPluginData('counter', '' + counter);
penpot.ui.sendMessage({ type: 'update-counter', content: { counter } });
} }
}); });
@ -284,7 +299,10 @@ penpot.on('filechange', () => {
penpot.on('selectionchange', () => { penpot.on('selectionchange', () => {
const selection = penpot.getSelectedShapes(); const selection = penpot.getSelectedShapes();
penpot.ui.sendMessage({ type: 'selection', content: { selection } }); const data: string | null =
selection.length === 1 ? selection[0].getPluginData('counter') : null;
const counter = data ? parseInt(data, 10) : 0;
penpot.ui.sendMessage({ type: 'selection', content: { selection, counter } });
}); });
penpot.on('themechange', (theme) => { penpot.on('themechange', (theme) => {