0
Fork 0
mirror of https://github.com/penpot/penpot-plugins.git synced 2025-01-08 07:50:44 -05:00

feat: new api functionality

This commit is contained in:
alonso.torres 2024-04-29 12:24:38 +02:00 committed by Alonso Torres
parent 811f55ec31
commit 9a501d3abe
4 changed files with 52 additions and 33 deletions

View file

@ -69,11 +69,23 @@ penpot.ui.onMessage<{ content: string; data: unknown }>((message) => {
const selection = penpot.selection; const selection = penpot.selection;
for (const shape of selection) { for (const shape of selection) {
( if (penpot.utils.types.isText(shape)) {
shape as PenpotText shape.characters = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam id mauris ut felis finibus congue. Ut odio ipsum, condimentum id tellus sit amet, dapibus sagittis ligula. Pellentesque hendrerit, nulla sit amet aliquet scelerisque, orci nunc commodo tellus, quis hendrerit nisl massa non tellus.
).characters = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam id mauris ut felis finibus congue. Ut odio ipsum, condimentum id tellus sit amet, dapibus sagittis ligula. Pellentesque hendrerit, nulla sit amet aliquet scelerisque, orci nunc commodo tellus, quis hendrerit nisl massa non tellus.
Phasellus fringilla tortor elit, ac dictum tellus posuere sodales. Ut eget imperdiet ante. Nunc eros magna, tincidunt non finibus in, tempor elementum nunc. Sed commodo magna in arcu aliquam efficitur.`; Phasellus fringilla tortor elit, ac dictum tellus posuere sodales. Ut eget imperdiet ante. Nunc eros magna, tincidunt non finibus in, tempor elementum nunc. Sed commodo magna in arcu aliquam efficitur.`;
} else if (penpot.utils.types.isRectangle(shape)) {
const width = Math.ceil(shape.width);
const height = Math.ceil(shape.height);
penpot
.uploadMediaUrl(
'placeholder',
`https://picsum.photos/${width}/${height}`
)
.then((data) => {
shape.fills = [{ fillOpacity: 1, fillImage: data }];
})
.catch((err) => console.error(err));
}
} }
} }
}); });

View file

@ -166,6 +166,11 @@ export interface PenpotContext {
getSelectedShapes(): PenpotShape[]; getSelectedShapes(): PenpotShape[];
getTheme(): PenpotTheme; getTheme(): PenpotTheme;
uploadMediaUrl(name: string, url: string): Promise<PenpotImageData>;
group(first: PenpotShape, ...rest: PenpotShape): PenpotGroup;
ungroup(first: PenpotShape, ...rest: PenpotShape);
createRectangle(): PenpotRectangle; createRectangle(): PenpotRectangle;
createFrame(): PepotFrame; createFrame(): PepotFrame;
} }
@ -183,6 +188,8 @@ export interface Penpot extends PenpotContext {
utils: { utils: {
types: { types: {
isText(shape: PenpotShape): shape is PenpotText; isText(shape: PenpotShape): shape is PenpotText;
isRectangle(shape: PenpotShape): shape is PenpotRectangle;
isFrame(shape: PenpotShape): shape is PenpotFrame;
}; };
}; };
log: (...data: unknown[]) => void; log: (...data: unknown[]) => void;
@ -198,19 +205,6 @@ export interface Penpot extends PenpotContext {
) => void; ) => void;
fetch: typeof fetch; fetch: typeof fetch;
// Exposes Penpot Context
root: PenpotShape;
currentPage: PenpotPage;
selection: PenpotShape[];
getFile(): PenpotFile | null;
getPage(): PenpotPage | null;
getSelected(): string[];
getSelectedShapes(): PenpotShape[];
getTheme(): PenpotTheme;
createRectangle(): PenpotShape;
} }
declare global { declare global {

View file

@ -4,6 +4,8 @@ import type {
EventsMap, EventsMap,
PenpotPage, PenpotPage,
PenpotShape, PenpotShape,
PenpotRectangle,
PenpotFrame,
} from '@penpot/plugin-types'; } from '@penpot/plugin-types';
import { Manifest, Permissions } from '../models/manifest.model'; import { Manifest, Permissions } from '../models/manifest.model';
@ -44,7 +46,7 @@ export function triggerEvent(
listeners.forEach((listener) => listener(message)); listeners.forEach((listener) => listener(message));
} }
export function createApi(context: PenpotContext, manifest: Manifest) { export function createApi(context: PenpotContext, manifest: Manifest): Penpot {
const closePlugin = () => { const closePlugin = () => {
modal?.removeEventListener('close', closePlugin); modal?.removeEventListener('close', closePlugin);
if (modal) { if (modal) {
@ -91,6 +93,12 @@ export function createApi(context: PenpotContext, manifest: Manifest) {
isText(shape: PenpotShape): shape is PenpotText { isText(shape: PenpotShape): shape is PenpotText {
return shape.type === 'text'; return shape.type === 'text';
}, },
isRectangle(shape: PenpotShape): shape is PenpotRectangle {
return shape.type === 'rect';
},
isFrame(shape: PenpotShape): shape is PenpotFrame {
return shape.type === 'frame';
},
}, },
}, },
@ -187,6 +195,10 @@ export function createApi(context: PenpotContext, manifest: Manifest) {
return context.createRectangle(); return context.createRectangle();
}, },
uploadMediaUrl(name: string, url: string) {
return context.uploadMediaUrl(name, url);
},
fetch, fetch,
}; };

View file

@ -1,4 +1,5 @@
html, body { html,
body {
font-family: 'Work Sans', sans-serif; font-family: 'Work Sans', sans-serif;
font-optical-sizing: auto; font-optical-sizing: auto;
font-style: normal; font-style: normal;
@ -31,12 +32,12 @@ ul {
padding: 0; padding: 0;
} }
[data-theme="dark"] { [data-theme='dark'] {
background-color: var(--db-primary); background-color: var(--db-quaternary);
color: var(--lb-primary); color: var(--lb-primary);
} }
[data-theme="light"] { [data-theme='light'] {
background-color: var(--lb-primary); background-color: var(--lb-primary);
color: var(--db-primary); color: var(--db-primary);
} }