mirror of
https://github.com/penpot/penpot.git
synced 2026-11-27 05:10:32 +00:00
✨ Expose the file features and active renderer to plugins (#13169)
Plugins and the MCP could not tell whether the new render engine (WebGL) or the SVG engine draws the open file. Add file.features with the sorted features of the open file: its stored features plus the features active in the session. render-wasm/v1 comes from the session alone, so it is present only when WebGL draws the file, whether the renderer comes from the wasm query param, the profile setting or the workspace menu. The backend never stores that feature on the file, so the stored features alone cannot answer it. For any other file, such as a library or a File kept after opening another file, features is null. Add penpotUtils.isWasmRenderer() to the MCP plugin and list it in the MCP instructions. It throws a clear error when the Penpot version does not expose the file features. Regenerate the MCP API docs, which also picks up the missing penpot.location entry, and the plugin API test suite surface. Add plugin API test suite tests for the open file and for a file that is not open. Note in the plugins memory that every new or changed Plugin API member gets a suite test whenever the suite can reach the case. Closes #13167 AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
1edaf6baef
commit
70c37934af
@ -5,6 +5,7 @@
|
||||
- The Plugin API is a public facade over internal frontend/common data. Do not expect Plugin API property names, value shapes, or behavior boundaries to match internal CLJS attrs or helper APIs; inspect the relevant proxy and internal code path before using Plugin API observations in production internals or tests.
|
||||
- `plugins/libs/plugin-types/index.d.ts` contains TypeScript declarations only. Runtime objects are CLJS proxies built under `frontend/src/app/plugins/*.cljs` with `obj/reify`.
|
||||
- `shape.cljs` builds shape proxies with hidden ids and per-property CLJS implementations. `library.cljs` builds library proxies such as `LibraryComponentProxy`.
|
||||
- A new or changed proxy member needs its type in `plugins/libs/plugin-types/index.d.ts`, a `plugins/CHANGELOG.md` entry and a Plugin API test suite test: see `mem:plugins/core`.
|
||||
- `shape.cljs`, `library.cljs`, and related namespaces break circular dependencies with mutable nil vars patched from `app.plugins` at load time. If a proxy constructor appears nil, check the patching path in `frontend/src/app/plugins.cljs`.
|
||||
|
||||
## Key Domain Namespaces
|
||||
|
||||
@ -18,6 +18,7 @@
|
||||
|
||||
- From `plugins/`: install `pnpm -r install`; runtime dev server `pnpm run start` or `pnpm run start:app:runtime`; sample plugin `pnpm run start:plugin:<name>`; build runtime `pnpm run build:runtime`; build plugins `pnpm run build:plugins`; lint `pnpm run lint`; format `pnpm run format:check` / `pnpm run format`; tests `pnpm run test`; e2e `pnpm run test:e2e`.
|
||||
- If a change affects public Plugin API types or runtime, update `plugins/CHANGELOG.md`. Prefix type/signature entries with `**plugin-types:**`; runtime behavior entries with `**plugin-runtime:**`.
|
||||
- Every new or changed Plugin API member gets a test in `apps/plugin-api-test-suite/src/tests/` in the same change, whenever the suite can reach the case (skip only what its README lists as not reachable headless, such as needing a second user or closing the plugin). Cover each new behavior, including null and error cases. Read `apps/plugin-api-test-suite/README.md` before writing one: call the API through `ctx.penpot`/`ctx.penpotMgmt`, tag backend-dependent tests `skipIfMocked`, and rerun `gen:api` (then prettier on `src/generated/api-surface.json`, since the generator writes it unformatted) after changing `libs/plugin-types/index.d.ts`.
|
||||
- Cross-cutting testing principles and anti-patterns: `mem:testing`.
|
||||
- JS Plugin API behavior inside Penpot app: `mem:frontend/plugin-api-to-cljs-binding`; TS declarations are not runtime code; many API objects are CLJS proxies in `frontend/src/app/plugins/*.cljs`.
|
||||
|
||||
|
||||
@ -13,6 +13,7 @@
|
||||
[app.common.uuid :as uuid]
|
||||
[app.config :as cf]
|
||||
[app.main.data.exports.files :as exports.files]
|
||||
[app.main.data.helpers :as dsh]
|
||||
[app.main.data.plugins :as dp]
|
||||
[app.main.data.workspace :as dw]
|
||||
[app.main.data.workspace.versions :as dwv]
|
||||
@ -28,7 +29,8 @@
|
||||
[app.plugins.utils :as u]
|
||||
[app.util.http :as http]
|
||||
[app.util.object :as obj]
|
||||
[beicon.v2.core :as rx]))
|
||||
[beicon.v2.core :as rx]
|
||||
[clojure.set :as set]))
|
||||
|
||||
(defn file-version-proxy?
|
||||
[proxy]
|
||||
@ -129,6 +131,19 @@
|
||||
:deleted-at nil)))))
|
||||
reject))))))))))
|
||||
|
||||
(defn get-file-features
|
||||
"Sorted features of the open file `file-id`: its stored features plus
|
||||
the features active in the session. `render-wasm/v1` comes from the
|
||||
session alone, so it is present only when the WebGL renderer draws the
|
||||
file. Returns nil when `file-id` is not the open file."
|
||||
[state file-id]
|
||||
(when (= file-id (:current-file-id state))
|
||||
(let [file (dsh/lookup-file state file-id)]
|
||||
(-> (set (:features file))
|
||||
(disj "render-wasm/v1")
|
||||
(set/union (:features state))
|
||||
(sort)))))
|
||||
|
||||
(defn file-proxy? [p]
|
||||
(obj/type-of? p "FileProxy"))
|
||||
|
||||
@ -148,6 +163,9 @@
|
||||
:revn
|
||||
{:get #(-> (u/locate-file id) :revn)}
|
||||
|
||||
:features
|
||||
{:get #(some->> (get-file-features @st/state id) (apply array))}
|
||||
|
||||
:pages
|
||||
{:this true
|
||||
:get #(.getPages ^js %)}
|
||||
|
||||
@ -131,3 +131,30 @@
|
||||
(.-label pinned)))
|
||||
(t/is (false? (.-isAutosave pinned)))
|
||||
(t/is (identical? created-at (.-createdAt pinned))))))))
|
||||
|
||||
(t/deftest open-file-features-add-the-session-features
|
||||
(let [file-id (uuid/next)
|
||||
state {:current-file-id file-id
|
||||
:features #{"render-wasm/v1" "components/v2"}
|
||||
:files {file-id {:id file-id
|
||||
:features #{"variants/v1" "components/v2"}}}}]
|
||||
(t/is (= ["components/v2" "render-wasm/v1" "variants/v1"]
|
||||
(file/get-file-features state file-id)))))
|
||||
|
||||
(t/deftest open-file-features-take-the-renderer-from-the-session
|
||||
(let [file-id (uuid/next)
|
||||
state {:current-file-id file-id
|
||||
:features #{"components/v2"}
|
||||
:files {file-id {:id file-id
|
||||
:features #{"render-wasm/v1" "components/v2"}}}}]
|
||||
(t/is (= ["components/v2"]
|
||||
(file/get-file-features state file-id)))))
|
||||
|
||||
(t/deftest other-file-features-are-nil
|
||||
(let [file-id (uuid/next)
|
||||
other-id (uuid/next)
|
||||
state {:current-file-id file-id
|
||||
:features #{"render-wasm/v1"}
|
||||
:files {other-id {:id other-id
|
||||
:features #{"variants/v1" "components/v2"}}}}]
|
||||
(t/is (nil? (file/get-file-features state other-id)))))
|
||||
|
||||
@ -161,3 +161,23 @@ test("findShapes — excludes the root shape itself from the results", () => {
|
||||
|
||||
assert.ok(!result.includes(root as any));
|
||||
});
|
||||
|
||||
test("isWasmRenderer — true when the open file has render-wasm/v1", () => {
|
||||
(globalThis as any).penpot = { currentFile: { features: ["components/v2", "render-wasm/v1"] } };
|
||||
assert.equal(PenpotUtils.isWasmRenderer(), true);
|
||||
});
|
||||
|
||||
test("isWasmRenderer — false when the open file lacks render-wasm/v1", () => {
|
||||
(globalThis as any).penpot = { currentFile: { features: ["components/v2"] } };
|
||||
assert.equal(PenpotUtils.isWasmRenderer(), false);
|
||||
});
|
||||
|
||||
test("isWasmRenderer — throws when no file is open", () => {
|
||||
(globalThis as any).penpot = { currentFile: null };
|
||||
assert.throws(() => PenpotUtils.isWasmRenderer(), /No file is open/);
|
||||
});
|
||||
|
||||
test("isWasmRenderer — throws when the file features are not exposed", () => {
|
||||
(globalThis as any).penpot = { currentFile: {} };
|
||||
assert.throws(() => PenpotUtils.isWasmRenderer(), /does not expose the file features/);
|
||||
});
|
||||
|
||||
@ -186,6 +186,25 @@ export class PenpotUtils {
|
||||
return this.findPage((page) => page.name.toLowerCase() === name.toLowerCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the new render engine (WebGL, `render-wasm/v1`) draws the open file.
|
||||
* When it returns false, the file renders with the SVG engine.
|
||||
*
|
||||
* @returns true iff the open file has the `render-wasm/v1` feature active
|
||||
* @throws Error if no file is open or the Penpot version does not expose the file features
|
||||
*/
|
||||
public static isWasmRenderer(): boolean {
|
||||
const file = penpot.currentFile;
|
||||
if (!file) {
|
||||
throw new Error("No file is open");
|
||||
}
|
||||
const features = file.features;
|
||||
if (!Array.isArray(features)) {
|
||||
throw new Error("This Penpot version does not expose the file features");
|
||||
}
|
||||
return features.includes("render-wasm/v1");
|
||||
}
|
||||
|
||||
public static getPageForShape(shape: Shape): Page | null {
|
||||
for (const page of penpot.currentFile!.pages) {
|
||||
if (page.getShapeById(shape.id)) {
|
||||
|
||||
@ -38,6 +38,7 @@ Penpot:
|
||||
currentUser: User;
|
||||
activeUsers: ActiveUser[];
|
||||
theme: Theme;
|
||||
location: PenpotLocation;
|
||||
localStorage: LocalStorage;
|
||||
selection: Shape[];
|
||||
shapesColors(shapes: Shape[]): (Color & ColorShapeInfo)[];
|
||||
@ -309,6 +310,20 @@ Penpot:
|
||||
const currentTheme = context.theme;
|
||||
console.log(currentTheme);
|
||||
```
|
||||
location: |-
|
||||
```
|
||||
readonly location: PenpotLocation
|
||||
```
|
||||
|
||||
The Penpot instance URL and the current address in the app, in the
|
||||
dashboard as well as in the workspace. Requires `content:read` permission.
|
||||
|
||||
Example
|
||||
```
|
||||
const { baseUrl, href, params } = penpot.location;
|
||||
console.log(baseUrl); // "https://design.penpot.app/"
|
||||
console.log(params['file-id']);
|
||||
```
|
||||
localStorage: |-
|
||||
```
|
||||
readonly localStorage: LocalStorage
|
||||
@ -5917,6 +5932,7 @@ Context:
|
||||
currentUser: User;
|
||||
activeUsers: ActiveUser[];
|
||||
theme: Theme;
|
||||
location: PenpotLocation;
|
||||
localStorage: LocalStorage;
|
||||
selection: Shape[];
|
||||
shapesColors(shapes: Shape[]): (Color & ColorShapeInfo)[];
|
||||
@ -6120,6 +6136,20 @@ Context:
|
||||
const currentTheme = context.theme;
|
||||
console.log(currentTheme);
|
||||
```
|
||||
location: |-
|
||||
```
|
||||
readonly location: PenpotLocation
|
||||
```
|
||||
|
||||
The Penpot instance URL and the current address in the app, in the
|
||||
dashboard as well as in the workspace. Requires `content:read` permission.
|
||||
|
||||
Example
|
||||
```
|
||||
const { baseUrl, href, params } = penpot.location;
|
||||
console.log(baseUrl); // "https://design.penpot.app/"
|
||||
console.log(params['file-id']);
|
||||
```
|
||||
localStorage: |-
|
||||
```
|
||||
readonly localStorage: LocalStorage
|
||||
@ -8390,6 +8420,7 @@ File:
|
||||
id: string;
|
||||
name: string;
|
||||
revn: number;
|
||||
features: string[] | null;
|
||||
pages: Page[];
|
||||
export(
|
||||
exportType: "penpot" | "zip",
|
||||
@ -8437,6 +8468,24 @@ File:
|
||||
```
|
||||
|
||||
The `revn` will change for every document update
|
||||
features: |-
|
||||
```
|
||||
readonly features: string[] | null
|
||||
```
|
||||
|
||||
The features of the open file, sorted by name, e.g. `"components/v2"`
|
||||
or `"variants/v1"`. It also lists the features active in the session,
|
||||
such as `"render-wasm/v1"` when the new render engine (WebGL) draws the
|
||||
file. Each read returns a new array.
|
||||
|
||||
Only `penpot.currentFile` answers it: for any other file, such as a
|
||||
library, or a `File` kept after the user opens another file, it is
|
||||
`null`.
|
||||
|
||||
Example
|
||||
```
|
||||
const webgl = penpot.currentFile.features.includes('render-wasm/v1');
|
||||
```
|
||||
pages: |-
|
||||
```
|
||||
readonly pages: Page[]
|
||||
@ -14204,6 +14253,51 @@ LibraryTypography:
|
||||
const sharedKeys = shape.getSharedPluginDataKeys('exampleNamespace');
|
||||
console.log(sharedKeys);
|
||||
```
|
||||
PenpotLocation:
|
||||
overview: |-
|
||||
Interface PenpotLocation
|
||||
========================
|
||||
|
||||
The Penpot instance URL and the current address in the app.
|
||||
Each read of `penpot.location` returns a new snapshot.
|
||||
|
||||
Query params that carry secrets (`share-id`, any param ending in
|
||||
`token`, `secret` or `password`, and the OAuth params `request`,
|
||||
`code` and `state`) are left out of `params` and `href`.
|
||||
|
||||
```
|
||||
interface PenpotLocation {
|
||||
baseUrl: string;
|
||||
href: string;
|
||||
params: Record<string, string>;
|
||||
}
|
||||
```
|
||||
|
||||
Referenced by: Context, Penpot
|
||||
members:
|
||||
Properties:
|
||||
baseUrl: |-
|
||||
```
|
||||
readonly baseUrl: string
|
||||
```
|
||||
|
||||
The public URL of the Penpot instance, ending in `/`,
|
||||
e.g. `"https://design.penpot.app/"`.
|
||||
href: |-
|
||||
```
|
||||
readonly href: string
|
||||
```
|
||||
|
||||
The URL of the current screen, e.g. the open file and page:
|
||||
`"https://design.penpot.app/?screen=workspace&team-id=…&file-id=…&page-id=…"`.
|
||||
Use it to link to what the user is looking at.
|
||||
params: |-
|
||||
```
|
||||
readonly params: Record<string, string>
|
||||
```
|
||||
|
||||
The query params of the current URL, e.g. `screen`, `team-id`,
|
||||
`file-id` and `page-id`. A param repeated in the URL keeps its last value.
|
||||
LocalStorage:
|
||||
overview: |-
|
||||
Interface LocalStorage
|
||||
|
||||
@ -209,6 +209,8 @@ A key object for navigating the currently active Penpot file is the `penpot` obj
|
||||
* `penpot.location` gives the Penpot instance URL (`baseUrl`), the link to what the user is viewing (`href`, e.g. the open file and page)
|
||||
and the query params of that link (`params`, e.g. `params["file-id"]`). Use `href` when you need a link to the open file;
|
||||
never guess the instance domain or build the link by hand.
|
||||
* `penpot.currentFile.features` lists the features active for the open file (e.g. `"components/v2"`, `"render-wasm/v1"`);
|
||||
it is null on any other `File` object.
|
||||
Members of `penpot` (e.g. `penpot.currentFile` and `penpot.root`) will be null no Penpot file is currently opened.
|
||||
You can use `penpotMgmt` to navigate to a file if needed (see below).
|
||||
|
||||
@ -236,6 +238,8 @@ CRITICAL: The `penpotUtils` object provides essential utilities - USE THESE INST
|
||||
General-purpose utility for analyzing/validating descendants
|
||||
Calls evaluator on each descendant; collects non-null/undefined results
|
||||
Powerful pattern: evaluator can return corrector functions or diagnostic data
|
||||
* isWasmRenderer(): boolean
|
||||
Returns true iff the new render engine (WebGL) draws the open file; false means the SVG engine.
|
||||
* Further functions for specific tasks (described in the sections below)
|
||||
|
||||
General pointers for working with Penpot designs:
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
- **plugin-types, plugins-runtime:** `penpotMgmt` returns `Team`, `Project` and `ProjectFile` objects. Assigning `name` renames teams, projects and files, `project.pinned` pins a project and `file.shared` shares a file as a team library; `createProject`, `createFile`, `moveTo`, `duplicate`, `remove` and `restore` live on the objects. Added `penpotMgmt.listTeams`, `penpotMgmt.createTeam`, `penpotMgmt.getFile`, `listDeletedProjects` and `listDeletedFiles`, and the `manage:projects`, `manage:teams` and `manage:delete` permissions. Restoration uses the dashboard's Trash operation; projects need recoverable files and empty projects cannot be restored; the API exposes no permanent deletion method. Team deletion is not exposed.
|
||||
- **plugins-runtime, plugin-types**: Added global plugins (`scope: "global"`) that remain active across dashboard and workspace navigation until closed or logged out, with `penpotMgmt` for project and file discovery, file navigation and workspace events. Global scope adds the `allow:global` permission, which users must accept when installing the plugin.
|
||||
- **plugin-types, plugins-runtime:** Added `penpot.location` with the instance base URL (`baseUrl`), the URL of the current screen (`href`) and its query params (`params`), in the dashboard and the workspace. Params that carry secrets are left out. Requires `content:read`. Closes #13013.
|
||||
- **plugin-types:** Added `file.features`, the sorted features of the open file, including the features active in the session, such as `render-wasm/v1` when the new render engine (WebGL) draws the file. It is `null` for any other file. Closes #13167.
|
||||
- **plugin-types:** Added the `contentrollback` event and the `ContentRollback` type. The event fires after the backend rejected some changes and the workspace undid them, with the ids of the files, pages and shapes affected. It fires once the undone data is in the workspace, also when the workspace has to reload the file to undo the changes.
|
||||
|
||||
### 🩹 Fixes
|
||||
|
||||
@ -152,6 +152,7 @@
|
||||
"Export": ["scale", "skipChildren", "suffix", "type"],
|
||||
"File": [
|
||||
"export",
|
||||
"features",
|
||||
"findVersions",
|
||||
"id",
|
||||
"name",
|
||||
@ -3141,6 +3142,12 @@
|
||||
"type": null,
|
||||
"array": false
|
||||
},
|
||||
"features": {
|
||||
"decl": "File",
|
||||
"kind": "get",
|
||||
"type": null,
|
||||
"array": true
|
||||
},
|
||||
"pages": {
|
||||
"decl": "File",
|
||||
"kind": "get",
|
||||
|
||||
@ -22,6 +22,20 @@ describe('File', () => {
|
||||
}
|
||||
});
|
||||
|
||||
test('currentFile lists its features sorted', (ctx) => {
|
||||
const file = ctx.penpot.currentFile;
|
||||
expect(file).not.toBeNull();
|
||||
if (file) {
|
||||
const { features } = file;
|
||||
expect(features).not.toBeNull();
|
||||
if (!features) return;
|
||||
expect(features.every((f) => typeof f === 'string')).toBe(true);
|
||||
expect(features).toContain('components/v2');
|
||||
expect(features).toEqual([...features].sort());
|
||||
expect(file.features === features).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
test('file lists its pages', (ctx) => {
|
||||
const file = ctx.penpot.currentFile;
|
||||
if (file) {
|
||||
|
||||
@ -430,6 +430,19 @@ describe('Management', () => {
|
||||
expect(finished).toContain(other.id);
|
||||
});
|
||||
|
||||
test('features is null on a file that is not open', async (ctx) => {
|
||||
const home = ctx.penpot.currentFile;
|
||||
expect(home).not.toBeNull();
|
||||
if (!home) return;
|
||||
expect(home.features).not.toBeNull();
|
||||
const other = await temporaryFile(ctx, 'Features');
|
||||
|
||||
await inFile(ctx, other, () => {
|
||||
expect(home.features).toBeNull();
|
||||
expect(ctx.penpot.currentFile?.features).toContain('components/v2');
|
||||
});
|
||||
});
|
||||
|
||||
test('off stops workspacechange notifications', async (ctx) => {
|
||||
const other = await temporaryFile(ctx, 'Unsubscribed');
|
||||
let count = 0;
|
||||
|
||||
17
plugins/libs/plugin-types/index.d.ts
vendored
17
plugins/libs/plugin-types/index.d.ts
vendored
@ -1967,6 +1967,23 @@ export interface File extends PluginData {
|
||||
*/
|
||||
readonly revn: number;
|
||||
|
||||
/**
|
||||
* The features of the open file, sorted by name, e.g. `"components/v2"`
|
||||
* or `"variants/v1"`. It also lists the features active in the session,
|
||||
* such as `"render-wasm/v1"` when the new render engine (WebGL) draws the
|
||||
* file. Each read returns a new array.
|
||||
*
|
||||
* Only `penpot.currentFile` answers it: for any other file, such as a
|
||||
* library, or a `File` kept after the user opens another file, it is
|
||||
* `null`.
|
||||
*
|
||||
* @example
|
||||
* ```js
|
||||
* const webgl = penpot.currentFile.features.includes('render-wasm/v1');
|
||||
* ```
|
||||
*/
|
||||
readonly features: string[] | null;
|
||||
|
||||
/**
|
||||
* List all the pages for the current file
|
||||
*/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user