From 4b506d24c26411b38319063aece2952470270dc1 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Fri, 7 Jun 2024 11:30:51 +0200 Subject: [PATCH] feat(poc-state-plugin): add example for plugin data --- .../poc-state-plugin/src/app/app.component.ts | 20 +++++++++++++++++ apps/poc-state-plugin/src/plugin.ts | 22 +++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/apps/poc-state-plugin/src/app/app.component.ts b/apps/poc-state-plugin/src/app/app.component.ts index eb8d82c..65e6e00 100644 --- a/apps/poc-state-plugin/src/app/app.component.ts +++ b/apps/poc-state-plugin/src/app/app.component.ts @@ -13,6 +13,9 @@ import type { PenpotShape } from '@penpot/plugin-types';

Current project name: {{ projectName() }}

+

+ Counter: {{ counter() }} +

@@ -66,6 +69,13 @@ import type { PenpotShape } from '@penpot/plugin-types'; > Create color palette board +

@@ -88,11 +98,13 @@ export class AppComponent { #fileId = null; #revn = 0; #selection = signal([]); + form = new FormGroup({ name: new FormControl(''), }); theme = signal(''); projectName = signal('Unknown'); + counter = signal(0); constructor() { window.addEventListener('message', (event) => { @@ -106,14 +118,18 @@ export class AppComponent { ); } else if (event.data.type === 'selection') { this.#refreshSelection(event.data.content.selection); + this.counter.set(event.data.content.counter); } else if (event.data.type === 'init') { this.#fileId = event.data.content.fileId; this.#revn = event.data.content.revn; this.#refreshPage(event.data.content.pageId, event.data.content.name); this.#refreshSelection(event.data.content.selection); this.theme.set(event.data.content.theme); + this.counter.set(event.data.content.counter); } else if (event.data.type === 'theme') { 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' }); } + increaseCounter() { + this.#sendMessage({ content: 'increase-counter' }); + } + #sendMessage(message: unknown) { parent.postMessage(message, '*'); } diff --git a/apps/poc-state-plugin/src/plugin.ts b/apps/poc-state-plugin/src/plugin.ts index 3c18b13..ee8bd4f 100644 --- a/apps/poc-state-plugin/src/plugin.ts +++ b/apps/poc-state-plugin/src/plugin.ts @@ -16,6 +16,11 @@ penpot.ui.onMessage<{ content: string; data: unknown }>((message) => { 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({ type: 'init', content: { @@ -24,7 +29,8 @@ penpot.ui.onMessage<{ content: string; data: unknown }>((message) => { fileId: file.id, revn: file.revn, theme: penpot.getTheme(), - selection: penpot.getSelectedShapes(), + selection, + counter, }, }); } 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); } } + } 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', () => { 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) => {