|
| 1 | +# Payload Dynamic Value Rich Text Plugin |
| 2 | + |
| 3 | +A powerful [Payload CMS](https://payloadcms.com) plugin for the Lexical editor that allows you to designate field values as dynamic variables. These variables can be reused throughout your rich text content and are visually represented as interactive badges. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Inline Autocomplete**: Trigger a searchable list of available dynamic fields using `@` (default) or a custom character (like `#`). |
| 8 | +- **Visual Badges**: Variables appear in the editor as clean, non-editable badges with a "VAR" prefix, making them distinct from regular text. |
| 9 | +- **Automatic Field Resolution**: Pull fields automatically from specified Collections and Globals, or provide manually defined fields. |
| 10 | +- **Unit Tokenization**: Variables are treated as single units—selecting or deleting them happens in a single action. |
| 11 | +- **Clipboard Support**: Copy and paste dynamic values between editors or documents while maintaining their data integrity. |
| 12 | +- **Light & Dark Mode**: Seamlessly integrates with Payload's admin UI theme. |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +pnpm add payloadcms-dynamic-value-richtext |
| 18 | +# or |
| 19 | +npm install payloadcms-dynamic-value-richtext |
| 20 | +``` |
| 21 | + |
| 22 | +## Basic Setup |
| 23 | + |
| 24 | +Add the plugin to your `payload.config.ts`: |
| 25 | + |
| 26 | +```typescript |
| 27 | +import { buildConfig } from 'payload' |
| 28 | +import { dynamicValuePlugin } from 'payloadcms-dynamic-value-richtext' |
| 29 | + |
| 30 | +export default buildConfig({ |
| 31 | + plugins: [ |
| 32 | + dynamicValuePlugin({ |
| 33 | + collections: ['company-settings', 'sites'], |
| 34 | + globals: ['contact-info'], |
| 35 | + trigger: '@', |
| 36 | + }), |
| 37 | + ], |
| 38 | + // ... rest of config |
| 39 | +}) |
| 40 | +``` |
| 41 | + |
| 42 | +## Configuration |
| 43 | + |
| 44 | +In your Lexical editor configuration, add the `DynamicValueFeature` to your list of features: |
| 45 | + |
| 46 | +```typescript |
| 47 | +import { DynamicValueFeature } from 'payloadcms-dynamic-value-richtext' |
| 48 | + |
| 49 | +// In your collection/global field definition: |
| 50 | +{ |
| 51 | + name: 'content', |
| 52 | + type: 'richText', |
| 53 | + editor: lexicalEditor({ |
| 54 | + features: ({ defaultFeatures }) => [ |
| 55 | + ...defaultFeatures, |
| 56 | + DynamicValueFeature(), |
| 57 | + ], |
| 58 | + }), |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### Plugin Options |
| 63 | + |
| 64 | +| Option | Type | Description | |
| 65 | +| :------------ | :----------------- | :---------------------------------------------------------------------- | |
| 66 | +| `collections` | `CollectionSlug[]` | Automatically extract all text-based fields from these collections. | |
| 67 | +| `globals` | `GlobalSlug[]` | Automatically extract all text-based fields from these globals. | |
| 68 | +| `fields` | `Field[]` | Manually provide a list of Payload fields to be used as dynamic values. | |
| 69 | +| `trigger` | `string` | The character that triggers the autocomplete popup. Defaults to `@`. | |
| 70 | + |
| 71 | +## How it Works |
| 72 | + |
| 73 | +1. **Resolution**: The plugin recursively flattens the fields of your chosen collections and globals. For example, if you have a Global `contact-info` with a field `email`, it becomes available as `contact-info.email`. |
| 74 | +2. **Injection**: When you type the trigger character in the editor, a popup appears showing all available fields. |
| 75 | +3. **Storage**: The variable is stored as a custom `DecoratorNode` in Lexical, which holds a reference to the field path (`value`) and the display label. |
| 76 | + |
| 77 | +## Development |
| 78 | + |
| 79 | +To run the development environment: |
| 80 | + |
| 81 | +1. Clone the repository |
| 82 | +2. Install dependencies: `pnpm install` |
| 83 | +3. Create a `.env` in the `dev` folder (see `.env.example`) |
| 84 | +4. Run development: `pnpm dev` |
| 85 | + |
| 86 | +## Ownership |
| 87 | + |
| 88 | +Created & Owned by **OD LABS**. This plugin is open and free for use to all under the MIT License. |
| 89 | + |
| 90 | +## Contributions |
| 91 | + |
| 92 | +Contributions are welcome! If you have ideas for improvements or find any issues, feel free to open a Pull Request or create an Issue. |
| 93 | + |
| 94 | +## License |
| 95 | + |
| 96 | +MIT |
0 commit comments