Skip to content

Commit

Permalink
feat(plugins-runtime): adds new permissions comment:read, `comment:…
Browse files Browse the repository at this point in the history
…write` and `allow:downloads`
  • Loading branch information
Alotor committed Sep 24, 2024
1 parent 86f98eb commit 1c1f6f3
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 6 deletions.
7 changes: 4 additions & 3 deletions libs/plugins-runtime/src/lib/api/openUI.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export const openUIApi = z
z.string(),
z.string(),
z.enum(['dark', 'light']),
openUISchema.optional()
openUISchema.optional(),
z.boolean().optional()
)
.implement((title, url, theme, options) => {
return createModal(title, url, theme, options);
.implement((title, url, theme, options, allowDownloads) => {
return createModal(title, url, theme, options, allowDownloads);
});
5 changes: 4 additions & 1 deletion libs/plugins-runtime/src/lib/api/plugin-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ describe('Plugin api', () => {
'library:read',
'library:write',
'user:read',
'comment:read',
'comment:write',
'allow:downloads',
],
},
openModal: vi.fn(),
Expand Down Expand Up @@ -107,7 +110,7 @@ describe('Plugin api', () => {
name: 'test',
id: '123',
revn: 0,
} as File;
} as File;

pluginManager.context.getFile.mockImplementation(() => exampleFile);

Expand Down
7 changes: 6 additions & 1 deletion libs/plugins-runtime/src/lib/create-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export function createModal(
name: string,
url: string,
theme: Theme,
options?: OpenUIOptions
options?: OpenUIOptions,
allowDownloads?: boolean
) {
const modal = document.createElement('plugin-modal') as PluginModalElement;

Expand Down Expand Up @@ -44,6 +45,10 @@ export function createModal(
modal.setAttribute('width', String(width));
modal.setAttribute('height', String(height));

if (allowDownloads) {
modal.setAttribute('allow-downloads', 'true');
}

document.body.appendChild(modal);

return modal;
Expand Down
3 changes: 3 additions & 0 deletions libs/plugins-runtime/src/lib/create-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ describe('createPlugin', () => {
'library:read',
'library:write',
'user:read',
'comment:read',
'comment:write',
'allow:downloads',
],
};

Expand Down
1 change: 1 addition & 0 deletions libs/plugins-runtime/src/lib/create-sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function createSandbox(
penpot: proxyApi,
fetch: ses.harden(safeFetch),
console: ses.harden(window.console),
Date: ses.harden(Date),
Math: ses.harden(Math),
setTimeout: ses.harden(
(...[handler, timeout]: Parameters<typeof setTimeout>) => {
Expand Down
3 changes: 3 additions & 0 deletions libs/plugins-runtime/src/lib/load-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ describe('plugin-loader', () => {
'library:read',
'library:write',
'user:read',
'comment:read',
'comment:write',
'allow:downloads',
],
};

Expand Down
5 changes: 5 additions & 0 deletions libs/plugins-runtime/src/lib/modal/plugin-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class PluginModalElement extends HTMLElement {
const iframeSrc = this.getAttribute('iframe-src');
const width = Number(this.getAttribute('width') || '300');
const height = Number(this.getAttribute('height') || '400');
const allowDownloads = this.getAttribute('allow-downloads') || false;

if (!title || !iframeSrc) {
throw new Error('title and iframe-src attributes are required');
Expand Down Expand Up @@ -100,6 +101,10 @@ export class PluginModalElement extends HTMLElement {
'allow-storage-access-by-user-activation'
);

if (allowDownloads) {
iframe.sandbox.add('allow-downloads');
}

iframe.addEventListener('load', () => {
this.shadowRoot?.dispatchEvent(
new CustomEvent('load', {
Expand Down
3 changes: 3 additions & 0 deletions libs/plugins-runtime/src/lib/models/manifest.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export const manifestSchema = z.object({
'library:read',
'library:write',
'user:read',
'comment:read',
'comment:write',
'allow:downloads',
])
),
});
3 changes: 3 additions & 0 deletions libs/plugins-runtime/src/lib/plugin-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ describe('createPluginManager', () => {
'library:read',
'library:write',
'user:read',
'comment:read',
'comment:write',
'allow:downloads',
],
};

Expand Down
6 changes: 5 additions & 1 deletion libs/plugins-runtime/src/lib/plugin-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export async function createPluginManager(
let uiMessagesCallbacks: ((message: unknown) => void)[] = [];
const timeouts = new Set<ReturnType<typeof setTimeout>>();

const allowDownloads = !!manifest.permissions.find(
(s) => s === 'allow:downloads'
);

const themeChangeId = context.addListener('themechange', (theme: Theme) => {
modal?.setTheme(theme);
});
Expand Down Expand Up @@ -83,7 +87,7 @@ export async function createPluginManager(
return;
}

modal = openUIApi(name, modalUrl, theme, options);
modal = openUIApi(name, modalUrl, theme, options, allowDownloads);

modal.setTheme(theme);

Expand Down

0 comments on commit 1c1f6f3

Please sign in to comment.