From 99126f8f606944eabf4c632503f8db867caaca4f Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 2 Jul 2024 10:55:49 +0200 Subject: [PATCH] feat: change permissions names --- apps/contrast-plugin/src/assets/manifest.json | 2 +- .../public/assets/manifest.json | 6 +- apps/icons-plugin/src/assets/manifest.json | 5 +- .../src/assets/manifest.json | 5 +- .../poc-state-plugin/src/assets/manifest.json | 8 ++- .../src/assets/manifest.json | 5 +- apps/table-plugin/src/assets/manifest.json | 5 +- docs/create-angular-plugin.md | 8 ++- docs/create-plugin.md | 8 ++- libs/plugins-runtime/src/lib/api/index.ts | 68 +++++++++---------- .../src/lib/api/plugin-api.spec.ts | 8 ++- .../src/lib/models/manifest.schema.ts | 10 +-- 12 files changed, 85 insertions(+), 53 deletions(-) diff --git a/apps/contrast-plugin/src/assets/manifest.json b/apps/contrast-plugin/src/assets/manifest.json index 43d54a4..8e3664d 100644 --- a/apps/contrast-plugin/src/assets/manifest.json +++ b/apps/contrast-plugin/src/assets/manifest.json @@ -3,5 +3,5 @@ "description": "Measure contrast plugin", "code": "/assets/plugin.js", "icon": "/assets/icon.png", - "permissions": ["page:read", "file:read", "selection:read"] + "permissions": [ "content:read" ] } diff --git a/apps/create-palette-plugin/public/assets/manifest.json b/apps/create-palette-plugin/public/assets/manifest.json index 179f438..bfcef67 100644 --- a/apps/create-palette-plugin/public/assets/manifest.json +++ b/apps/create-palette-plugin/public/assets/manifest.json @@ -4,8 +4,8 @@ "code": "/plugin.js", "icon": "/assets/icon.png", "permissions": [ - "page:read", - "file:read", - "selection:read" + "content:read", + "content:write", + "library:read" ] } diff --git a/apps/icons-plugin/src/assets/manifest.json b/apps/icons-plugin/src/assets/manifest.json index b51f853..20135c3 100644 --- a/apps/icons-plugin/src/assets/manifest.json +++ b/apps/icons-plugin/src/assets/manifest.json @@ -3,5 +3,8 @@ "code": "/assets/plugin.js", "icon": "/assets/icon.png", "description": "Create icons from the Feather Icons set", - "permissions": ["page:read", "file:read", "selection:read"] + "permissions": [ + "content:read", + "content:write" + ] } diff --git a/apps/lorem-ipsum-plugin/src/assets/manifest.json b/apps/lorem-ipsum-plugin/src/assets/manifest.json index 8e9c59a..bb72dd1 100644 --- a/apps/lorem-ipsum-plugin/src/assets/manifest.json +++ b/apps/lorem-ipsum-plugin/src/assets/manifest.json @@ -3,5 +3,8 @@ "description": "Lorem ipsum text generator plugin", "code": "/assets/plugin.js", "icon": "/assets/icon.png", - "permissions": ["page:read", "file:read", "selection:read"] + "permissions": [ + "content:read", + "content:write" + ] } diff --git a/apps/poc-state-plugin/src/assets/manifest.json b/apps/poc-state-plugin/src/assets/manifest.json index 4c07e58..ce29e9c 100644 --- a/apps/poc-state-plugin/src/assets/manifest.json +++ b/apps/poc-state-plugin/src/assets/manifest.json @@ -3,8 +3,10 @@ "description": "Sandbox plugin for plugins development", "code": "/assets/plugin.js", "permissions": [ - "page:read", - "file:read", - "selection:read" + "content:read", + "content:write", + "library:read", + "library:write", + "user:read" ] } diff --git a/apps/rename-layers-plugin/src/assets/manifest.json b/apps/rename-layers-plugin/src/assets/manifest.json index e89a4f3..3b76863 100644 --- a/apps/rename-layers-plugin/src/assets/manifest.json +++ b/apps/rename-layers-plugin/src/assets/manifest.json @@ -3,5 +3,8 @@ "description": "Change the name of one or several layers", "code": "/assets/plugin.js", "icon": "/assets/icon.png", - "permissions": ["page:read", "file:read", "selection:read"] + "permissions": [ + "content:read", + "content:write" + ] } diff --git a/apps/table-plugin/src/assets/manifest.json b/apps/table-plugin/src/assets/manifest.json index 63b827b..e3bfcf7 100644 --- a/apps/table-plugin/src/assets/manifest.json +++ b/apps/table-plugin/src/assets/manifest.json @@ -3,5 +3,8 @@ "description": "Table plugin to import or create tables", "code": "/assets/plugin.js", "icon": "/assets/icon.png", - "permissions": ["page:read", "file:read", "selection:read"] + "permissions": [ + "content:read", + "content:write" + ] } diff --git a/docs/create-angular-plugin.md b/docs/create-angular-plugin.md index 961928b..e5377a9 100644 --- a/docs/create-angular-plugin.md +++ b/docs/create-angular-plugin.md @@ -26,7 +26,13 @@ Next, create a `manifest.json` file inside the `/src/assets` directory. This fil "host": "http://localhost:4200", "code": "/assets/plugin.js", "icon": "/assets/icon.png", - "permissions": ["page:read", "file:read", "selection:read"] + "permissions": [ + "content:read", + "content:write", + "library:read", + "library:write", + "user:read" + ] } ``` diff --git a/docs/create-plugin.md b/docs/create-plugin.md index 2d1ca08..7757f2f 100644 --- a/docs/create-plugin.md +++ b/docs/create-plugin.md @@ -26,7 +26,13 @@ Next, create a `manifest.json` file inside the `/public` directory. This file is "host": "http://localhost:4201", "code": "/plugin.js", "icon": "/icon.png", - "permissions": ["page:read", "file:read", "selection:read"] + "permissions": [ + "content:read", + "content:write", + "library:read", + "library:write", + "user:read" + ] } ``` diff --git a/libs/plugins-runtime/src/lib/api/index.ts b/libs/plugins-runtime/src/lib/api/index.ts index eae7ac9..000454a 100644 --- a/libs/plugins-runtime/src/lib/api/index.ts +++ b/libs/plugins-runtime/src/lib/api/index.ts @@ -48,8 +48,12 @@ let modals = new Set([]); const eventListeners: Map[]> = new Map(); window.addEventListener('message', (event) => { - for (const callback of uiMessagesCallbacks) { - callback(event.data); + try { + for (const callback of uiMessagesCallbacks) { + callback(event.data); + } + } catch (err) { + console.error(err); } }); @@ -168,13 +172,8 @@ export function createApi(context: PenpotContext, manifest: Manifest): Penpot { z.enum(validEvents).parse(type); z.function().parse(callback); - if (type === 'pagechange') { - checkPermission('page:read'); - } else if (type === 'filechange') { - checkPermission('file:read'); - } else if (type === 'selectionchange') { - checkPermission('selection:read'); - } + // To suscribe to events needs the read permission + checkPermission('content:read'); const listeners = eventListeners.get(type) || []; listeners.push(callback as Callback); @@ -199,69 +198,68 @@ export function createApi(context: PenpotContext, manifest: Manifest): Penpot { // Penpot State API get root(): PenpotShape { - checkPermission('page:read'); + checkPermission('content:read'); return context.root; }, get currentPage(): PenpotPage { - checkPermission('page:read'); + checkPermission('content:read'); return context.currentPage; }, get selection(): PenpotShape[] { - checkPermission('selection:read'); + checkPermission('content:read'); return context.selection; }, get viewport(): PenpotViewport { - // checkPermission('viewport:read'); return context.viewport; }, get library(): PenpotLibraryContext { - // checkPermission('library:read'); + checkPermission('library:read'); return context.library; }, get fonts(): PenpotFontsContext { - // checkPermission('viewport:read'); + checkPermission('content:read'); return context.fonts; }, get currentUser(): PenpotUser { - // checkPermission('user:read'); + checkPermission('user:read'); return context.currentUser; }, get activeUsers(): PenpotActiveUser { - // checkPermission('activeUsers:read'); + checkPermission('user:read'); return context.activeUsers; }, getFile(): PenpotFile | null { - checkPermission('file:read'); + checkPermission('content:read'); return context.getFile(); }, getPage(): PenpotPage | null { - checkPermission('page:read'); + checkPermission('content:read'); return context.getPage(); }, getSelected(): string[] { - checkPermission('selection:read'); + checkPermission('content:read'); return context.getSelected(); }, getSelectedShapes(): PenpotShape[] { - checkPermission('selection:read'); + checkPermission('content:read'); return context.getSelectedShapes(); }, shapesColors( shapes: PenpotShape[] ): (PenpotColor & PenpotColorShapeInfo)[] { - // checkPermission('selection:read'); + checkPermission('content:read'); return context.shapesColors(shapes); }, @@ -270,7 +268,7 @@ export function createApi(context: PenpotContext, manifest: Manifest): Penpot { oldColor: PenpotColor, newColor: PenpotColor ) { - // checkPermission('selection:read'); + checkPermission('content:write'); return context.replaceColor(shapes, oldColor, newColor); }, @@ -279,27 +277,27 @@ export function createApi(context: PenpotContext, manifest: Manifest): Penpot { }, createFrame(): PenpotFrame { - // checkPermission('page:write'); + checkPermission('content:write'); return context.createFrame(); }, createRectangle(): PenpotRectangle { - // checkPermission('page:write'); + checkPermission('content:write'); return context.createRectangle(); }, createEllipse(): PenpotEllipse { - // checkPermission('page:write'); + checkPermission('content:write'); return context.createEllipse(); }, createText(text: string): PenpotText | null { - // checkPermission('page:write'); + checkPermission('content:write'); return context.createText(text); }, createPath(): PenpotPath { - // checkPermission('page:write'); + checkPermission('content:write'); return context.createPath(); }, @@ -307,30 +305,32 @@ export function createApi(context: PenpotContext, manifest: Manifest): Penpot { boolType: PenpotBoolType, shapes: PenpotShape[] ): PenpotBool | null { - // checkPermission('page:write'); + checkPermission('content:write'); return context.createBoolean(boolType, shapes); }, createShapeFromSvg(svgString: string): PenpotGroup | null { - // checkPermission('page:write'); + checkPermission('content:write'); return context.createShapeFromSvg(svgString); }, group(shapes: PenpotShape[]): PenpotGroup | null { - // checkPermission('page:write'); + checkPermission('content:write'); return context.group(shapes); }, ungroup(group: PenpotGroup, ...other: PenpotGroup[]): void { - // checkPermission('page:write'); + checkPermission('content:write'); context.ungroup(group, ...other); }, uploadMediaUrl(name: string, url: string) { + checkPermission('content:write'); return context.uploadMediaUrl(name, url); }, uploadMediaData(name: string, data: Uint8Array, mimeType: string) { + checkPermission('content:write'); return context.uploadMediaData(name, data, mimeType); }, @@ -338,7 +338,7 @@ export function createApi(context: PenpotContext, manifest: Manifest): Penpot { shapes: PenpotShape[], options?: { type?: 'html' | 'svg' } ): string { - // checkPermission('page:write'); + checkPermission('content:read'); return context.generateMarkup(shapes, options); }, @@ -350,7 +350,7 @@ export function createApi(context: PenpotContext, manifest: Manifest): Penpot { includeChildren?: boolean; } ): string { - // checkPermission('page:write'); + checkPermission('content:read'); return context.generateStyle(shapes, options); }, }; diff --git a/libs/plugins-runtime/src/lib/api/plugin-api.spec.ts b/libs/plugins-runtime/src/lib/api/plugin-api.spec.ts index 9c07f3a..9edbe0b 100644 --- a/libs/plugins-runtime/src/lib/api/plugin-api.spec.ts +++ b/libs/plugins-runtime/src/lib/api/plugin-api.spec.ts @@ -35,7 +35,13 @@ describe('Plugin api', () => { name: 'test', code: '', host: 'http://fake.com', - permissions: ['page:read', 'file:read', 'selection:read'], + permissions: [ + 'content:read', + 'content:write', + 'library:read', + 'library:write', + 'user:read', + ], }); const addEventListenerMock = vi.mocked(window.addEventListener); diff --git a/libs/plugins-runtime/src/lib/models/manifest.schema.ts b/libs/plugins-runtime/src/lib/models/manifest.schema.ts index a58e1bd..373854e 100644 --- a/libs/plugins-runtime/src/lib/models/manifest.schema.ts +++ b/libs/plugins-runtime/src/lib/models/manifest.schema.ts @@ -9,11 +9,11 @@ export const manifestSchema = z.object({ description: z.string().max(200).optional(), permissions: z.array( z.enum([ - 'page:read', - 'page:write', - 'file:read', - 'file:write', - 'selection:read', + 'content:read', + 'content:write', + 'library:read', + 'library:write', + 'user:read', ]) ), });