0
Fork 0
mirror of https://github.com/penpot/penpot-plugins.git synced 2025-01-06 14:50:21 -05:00
penpot-plugins/docs/create-angular-plugin.md

154 lines
4 KiB
Markdown
Raw Permalink Normal View History

2024-04-23 05:33:38 -05:00
# Creating a Plugin
2024-06-19 07:15:23 -05:00
This guide walks you through the steps to create an Angular plugin for our platform. You'll start by setting up the basic structure, configuring necessary files, and then running a local server to preview your plugin.
If you prefer to create the plugin without a specific framework there's also [Creating a Plugin](./create-plugin.md).
2024-10-25 05:05:02 -05:00
Keep in mind that this guide is for creating a plugin **inside `penpot-plugins` monorepo**. If you want to create a plugin outside our environment you can check the [Penpot Plugin Starter Template](https://github.com/penpot/penpot-plugin-starter-template) or the documentation at [Create a Plugin](https://help.penpot.app/plugins/create-a-plugin/).
2024-06-19 07:15:23 -05:00
Let's dive in.
2024-04-23 05:33:38 -05:00
### Step 1: Initialize the Plugin
First, you need to create the scaffolding for your plugin. Use the following command, replacing `example-plugin` with the name of your plugin:
```sh
2024-05-08 08:32:42 -05:00
npx nx g @nx/angular:app example-plugin --directory=apps/example-plugin --bundler=esbuild
2024-04-23 05:33:38 -05:00
```
### Step 2: Configure the Manifest
Next, create a `manifest.json` file inside the `/src/assets` directory. This file is crucial as it defines key properties of your plugin, including permissions and the entry point script.
```json
{
"name": "Example plugin",
2024-05-30 02:33:05 -05:00
"host": "http://localhost:4200",
"code": "/assets/plugin.js",
"icon": "/assets/icon.png",
2024-07-02 03:55:49 -05:00
"permissions": [
"content:write",
"library:write",
"user:read",
"comment:read",
"allow:downloads"
2024-07-02 03:55:49 -05:00
]
2024-04-23 05:33:38 -05:00
}
```
### Step 3: Update Project Configuration
2024-06-06 08:42:52 -05:00
Now, add the plugin tag.
2024-04-23 05:33:38 -05:00
```typescript
2024-05-08 08:32:42 -05:00
"tags": ["type:plugin"],
2024-04-23 05:33:38 -05:00
```
2024-05-30 08:02:56 -05:00
Also, update `targets.build` with the following code to allow the use of Penpot styles and build the plugin code.
2024-04-23 05:33:38 -05:00
```json
2024-05-30 08:02:56 -05:00
"options": {
"styles": [
"libs/plugins-styles/src/lib/styles.css",
"apps/example-plugin/src/styles.css"
],
"optimization": {
"scripts": true,
"styles": true,
"fonts": false
}
},
"dependsOn": ["buildPlugin"]
```
Add the default port to the `serve.configurations.development` task:
```json
"development": {
// ...
"port": 4302,
2024-04-23 05:33:38 -05:00
}
```
2024-06-19 07:15:23 -05:00
For choosing the port go check the Sample Plugins table at the [README](../README.md) so your plugin doesn't use a duplicate port. We're using the range 4300-4399.
2024-04-23 05:33:38 -05:00
### Step 4: Modify TypeScript Configuration
Create ``tsconfig.plugin.json` next to the `tsconfig.json`:
```json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"types": []
},
"files": ["src/plugin.ts"],
"include": ["../../libs/plugin-types/index.d.ts"]
}
```
Add the reference to the main tsconfig.json:
```json
"references": [
{
"path": "./tsconfig.plugin.json"
}
],
```
2024-06-12 07:30:32 -05:00
### Step 5: Add TS parser to eslint
Add these options to the end of the `eslint.config.js` file to allow linting with type information:
```js
{
languageOptions: {
parserOptions: {
project: './tsconfig.*?.json',
tsconfigRootDir: import.meta.dirname,
},
},
},
```
### Strep 6: Hello world plugin code
2024-05-08 08:32:42 -05:00
Create the file `apps/example-plugin/src/plugin.ts` with the following code:
```ts
console.log('Hello Plugin');
```
2024-06-12 07:30:32 -05:00
### Step 7: Run the plugin
2024-04-23 05:33:38 -05:00
Run this command:
```sh
2024-05-30 08:02:56 -05:00
npx nx run example-plugin:init
2024-04-23 05:33:38 -05:00
```
This will run two tasks: `serve`, the usual Angular server, and `buildPlugin`, which will compile the `plugin.ts` file.
2024-06-12 07:30:32 -05:00
### Step 8: Load the Plugin in Penpot
2024-04-23 05:33:38 -05:00
2024-06-19 07:15:23 -05:00
To load your plugin into Penpot you can use the shortcut `Ctrl + Alt + P` to directly open the Plugin manager modal. There you need to provide the plugin's manifest URL (example: `http://plugin.example/manifest.json`) for the installation. If there's no issues the plugin will be installed and then you would be able to open it whenever you like.
2024-04-23 05:33:38 -05:00
2024-06-19 07:15:23 -05:00
You can also open the Plugin manager modal via:
- Menu
![Penpot's menu image](./images/plugin-menu.png)
2024-04-23 05:33:38 -05:00
2024-06-12 07:30:32 -05:00
### Step 9: Build plugin
2024-05-30 08:02:56 -05:00
```
npx nx run example-plugin:build
```
2024-04-23 05:33:38 -05:00
### Learn More About Plugin Development
For more detailed information on plugin development, check out our guides:
2024-06-19 07:15:23 -05:00
- [Plugin Usage Documentation](,/plugin-usage.md)