Skip to content

Commit

Permalink
feat(plugin-types): removed old method and replaced with attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alotor committed Sep 30, 2024
1 parent 6adee11 commit 1866299
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 148 deletions.
12 changes: 6 additions & 6 deletions apps/contrast-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PluginMessageEvent, PluginUIEvent } from './model.js';

penpot.ui.open('CONTRAST PLUGIN', `?theme=${penpot.getTheme()}`, {
penpot.ui.open('CONTRAST PLUGIN', `?theme=${penpot.theme}`, {
width: 285,
height: 525,
});
Expand All @@ -10,8 +10,8 @@ penpot.ui.onMessage<PluginUIEvent>((message) => {
sendMessage({
type: 'init',
content: {
theme: penpot.getTheme(),
selection: penpot.getSelectedShapes(),
theme: penpot.theme,
selection: penpot.selection,
},
});

Expand All @@ -20,7 +20,7 @@ penpot.ui.onMessage<PluginUIEvent>((message) => {
});

penpot.on('selectionchange', () => {
const shapes = penpot.getSelectedShapes();
const shapes = penpot.selection;
sendMessage({ type: 'selection', content: shapes });

initEvents();
Expand All @@ -37,7 +37,7 @@ function initEvents() {
return penpot.on(
'shapechange',
() => {
const shapes = penpot.getSelectedShapes();
const shapes = penpot.selection;
sendMessage({ type: 'selection', content: shapes });
},
{ shapeId: shape.id }
Expand All @@ -46,7 +46,7 @@ function initEvents() {
}

penpot.on('themechange', () => {
const theme = penpot.getTheme();
const theme = penpot.theme;
sendMessage({ type: 'theme', content: theme });
});

Expand Down
2 changes: 1 addition & 1 deletion apps/e2e/src/plugins/component-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default function () {
rectangle.x = penpot.viewport.center.x;
rectangle.y = penpot.viewport.center.y;

const shape = penpot.getPage()?.getShapeById(rectangle.id);
const shape = penpot.currentPage?.getShapeById(rectangle.id);
if (shape) {
penpot.library.local.createComponent([shape]);
}
Expand Down
6 changes: 3 additions & 3 deletions apps/e2e/src/plugins/create-comments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default function () {
async function createComment() {
const page = penpot.getPage();
const page = penpot.currentPage;

if (page) {
await page.addCommentThread('Hello world!', {
Expand All @@ -11,7 +11,7 @@ export default function () {
}

async function replyComment() {
const page = penpot.getPage();
const page = penpot.currentPage;

if (page) {
const comments = await page.findCommentThreads({
Expand All @@ -23,7 +23,7 @@ export default function () {
}

async function deleteComment() {
const page = penpot.getPage();
const page = penpot.currentPage;

if (page) {
const commentThreads = await page.findCommentThreads({
Expand Down
4 changes: 2 additions & 2 deletions apps/e2e/src/plugins/create-ruler-guides.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default function () {
function createRulerGuides(): void {
const page = penpot.getPage();
const page = penpot.currentPage;

if (page) {
page.addRulerGuide('horizontal', penpot.viewport.center.x);
Expand All @@ -9,7 +9,7 @@ export default function () {
}

function removeRulerGuides(): void {
const page = penpot.getPage();
const page = penpot.currentPage;

if (page) {
page.removeRulerGuide(page.rulerGuides[0]);
Expand Down
2 changes: 1 addition & 1 deletion apps/icons-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PluginMessageEvent, PluginUIEvent } from './model.js';

penpot.ui.open('FEATHER ICONS PLUGIN', `?theme=${penpot.getTheme()}`, {
penpot.ui.open('FEATHER ICONS PLUGIN', `?theme=${penpot.theme}`, {
width: 292,
height: 540,
});
Expand Down
2 changes: 1 addition & 1 deletion apps/lorem-ipsum-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
generateCharacters,
} from './generator.js';

penpot.ui.open('LOREM IPSUM PLUGIN', `?theme=${penpot.getTheme()}`);
penpot.ui.open('LOREM IPSUM PLUGIN', `?theme=${penpot.theme}`);

penpot.on('themechange', (theme) => {
sendMessage({ type: 'theme', content: theme });
Expand Down
30 changes: 15 additions & 15 deletions apps/poc-state-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ penpot.ui.onMessage<{ content: string; data: unknown }>(async (message) => {
});

penpot.on('pagechange', () => {
const page = penpot.getPage();
const page = penpot.currentPage;
const shapes = page?.findShapes();

penpot.ui.sendMessage({
Expand All @@ -62,7 +62,7 @@ penpot.on('pagechange', () => {
});

penpot.on('filechange', () => {
const file = penpot.getFile();
const file = penpot.currentFile;

if (!file) {
return;
Expand All @@ -77,7 +77,7 @@ penpot.on('filechange', () => {
});

penpot.on('selectionchange', () => {
const selection = penpot.getSelectedShapes();
const selection = penpot.selection;
const data: string | null =
selection.length === 1 ? selection[0].getPluginData('counter') : null;
const counter = data ? parseInt(data, 10) : 0;
Expand All @@ -89,8 +89,8 @@ penpot.on('themechange', (theme) => {
});

function init() {
const page = penpot.getPage();
const file = penpot.getFile();
const page = penpot.currentPage;
const file = penpot.currentFile;

if (!page || !file) {
return;
Expand All @@ -108,15 +108,15 @@ function init() {
pageId: page.id,
fileId: file.id,
revn: file.revn,
theme: penpot.getTheme(),
theme: penpot.theme,
selection,
counter,
},
});
}

function changeName(data: { id: string; name: string }) {
const shape = penpot.getPage()?.getShapeById('' + data.id);
const shape = penpot.currentPage?.getShapeById('' + data.id);
if (shape) {
shape.name = data.name;
}
Expand All @@ -140,28 +140,28 @@ function createRect() {
}

function moveX(data: { id: string }) {
const shape = penpot.getPage()?.getShapeById('' + data.id);
const shape = penpot.currentPage?.getShapeById('' + data.id);
if (shape) {
shape.x += 100;
}
}

function moveY(data: { id: string }) {
const shape = penpot.getPage()?.getShapeById('' + data.id);
const shape = penpot.currentPage?.getShapeById('' + data.id);
if (shape) {
shape.y += 100;
}
}

function resizeW(data: { id: string }) {
const shape = penpot.getPage()?.getShapeById('' + data.id);
const shape = penpot.currentPage?.getShapeById('' + data.id);
if (shape) {
shape.resize(shape.width * 2, shape.height);
}
}

function resizeH(data: { id: string }) {
const shape = penpot.getPage()?.getShapeById('' + data.id);
const shape = penpot.currentPage?.getShapeById('' + data.id);
if (shape) {
shape.resize(shape.width, shape.height * 2);
}
Expand Down Expand Up @@ -449,7 +449,7 @@ function createMargins() {
selected.addRulerGuide('vertical', width - 10);
selected.addRulerGuide('horizontal', 10);
selected.addRulerGuide('horizontal', height - 10);
} else {
} else if (page) {
console.log('bound', penpot.viewport.bounds);
const { x, y, width, height } = penpot.viewport.bounds;
page.addRulerGuide('vertical', x + 100);
Expand All @@ -464,7 +464,7 @@ async function addComment() {

if (shape) {
const content = shape.name + ' - ' + Date.now();
const cthr = await penpot.currentPage.findCommentThreads();
const cthr = await penpot.currentPage?.findCommentThreads();
const th = cthr && cthr[0];

if (th) {
Expand All @@ -476,13 +476,13 @@ async function addComment() {
}
} else {
console.log('Create new thread', content);
await penpot.currentPage.addCommentThread(content, shape.center);
await penpot.currentPage?.addCommentThread(content, shape.center);
}
}
}

async function exportFile() {
const data = await penpot.getFile()?.export('penpot');
const data = await penpot.currentFile?.export('penpot');

if (data) {
penpot.ui.sendMessage({
Expand Down
8 changes: 4 additions & 4 deletions apps/rename-layers-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PluginMessageEvent } from './app/model';

penpot.ui.open('RENAME LAYER PLUGIN', `?theme=${penpot.getTheme()}`, {
penpot.ui.open('RENAME LAYER PLUGIN', `?theme=${penpot.theme}`, {
width: 290,
height: 550,
});
Expand Down Expand Up @@ -55,9 +55,9 @@ penpot.ui.onMessage<PluginMessageEvent>((message) => {
});

function getShapes() {
return penpot.getSelectedShapes().length
? penpot.getSelectedShapes()
: penpot.getPage()?.findShapes();
return penpot.selection.length
? penpot.selection
: penpot.currentPage?.findShapes();
}

function resetSelection() {
Expand Down
4 changes: 2 additions & 2 deletions apps/table-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GridLayout } from '@penpot/plugin-types';
import { PluginMessageEvent, TablePluginEvent } from './app/model';

penpot.ui.open('TABLE PLUGIN', `?theme=${penpot.getTheme()}`, {
penpot.ui.open('TABLE PLUGIN', `?theme=${penpot.theme}`, {
width: 280,
height: 610,
});
Expand Down Expand Up @@ -219,7 +219,7 @@ function createFlexCell(
function pluginData(message: PluginMessageEvent) {
if (message.type === 'tableconfig') {
const { type, options } = message.content;
const page = penpot.getPage();
const page = penpot.currentPage;

if (type === 'save') {
page?.setPluginData('table-plugin', JSON.stringify(options));
Expand Down
Loading

0 comments on commit 1866299

Please sign in to comment.