|
| 1 | +# TextEditorPluginSlot |
| 2 | + |
| 3 | +### Slot ID: `org.openedx.frontend.authoring.text_editor_plugin.v1` |
| 4 | + |
| 5 | +### Slot ID Aliases |
| 6 | +* `text_editor_plugin_slot` |
| 7 | + |
| 8 | +### Plugin Props: |
| 9 | + |
| 10 | +* `blockType` - String. The type of block being edited (e.g., `html`). |
| 11 | + |
| 12 | +## Description |
| 13 | + |
| 14 | +The `TextEditorPluginSlot` is rendered inside the Text Editor modal window for HTML XBlocks. |
| 15 | +By default, the slot is **empty**. |
| 16 | +It is intended as a generic extension point that can host **any React component** – for example: |
| 17 | + |
| 18 | +- **Contextual helpers** (tips, validation messages, writing guides) |
| 19 | +- **Content utilities** (templates, reusable snippets, glossary insert tools) |
| 20 | +- **Integrations** (linking to external systems, analytics, metadata editors) |
| 21 | + |
| 22 | +Your component is responsible for interacting with the editor (if needed) using Redux state, |
| 23 | +DOM APIs, or other utilities provided by `frontend-app-authoring`. |
| 24 | + |
| 25 | +#### Interacting with Editor State |
| 26 | + |
| 27 | +```jsx |
| 28 | +import { useSelector } from 'react-redux'; |
| 29 | +import { selectors } from 'CourseAuthoring/editors/data/redux'; |
| 30 | + |
| 31 | +const MyComponent = ({ blockType }) => { |
| 32 | + // Read editor state |
| 33 | + const showRawEditor = useSelector(selectors.app.showRawEditor); |
| 34 | + const blockValue = useSelector(selectors.app.blockValue); |
| 35 | + |
| 36 | + // Update CodeMirror (raw editor) |
| 37 | + const updateRawContent = (content) => { |
| 38 | + const cm = document.querySelector('.CodeMirror')?.CodeMirror; |
| 39 | + if (cm?.dispatch) { |
| 40 | + cm.dispatch(cm.state.update({ |
| 41 | + changes: { from: 0, to: cm.state.doc.length, insert: content } |
| 42 | + })); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + return <button onClick={() => showRawEditor ? updateRawContent('<p>New content</p>') : updateContent('<p>New content</p>')}> |
| 47 | + Update Editor |
| 48 | + </button>; |
| 49 | +}; |
| 50 | +``` |
| 51 | +
|
| 52 | +## Examples |
| 53 | +
|
| 54 | +### Default content |
| 55 | +
|
| 56 | + |
| 57 | +
|
| 58 | +### Replaced with custom component |
| 59 | +
|
| 60 | +The following `env.config.tsx` will add a centered `h1` tag im HTML editor. |
| 61 | +
|
| 62 | + |
| 63 | +
|
| 64 | +```tsx |
| 65 | +import { DIRECT_PLUGIN, PLUGIN_OPERATIONS } from '@openedx/frontend-plugin-framework'; |
| 66 | + |
| 67 | +const config = { |
| 68 | + pluginSlots: { |
| 69 | + 'org.openedx.frontend.authoring.text_editor_plugin.v1': { |
| 70 | + plugins: [ |
| 71 | + { |
| 72 | + op: PLUGIN_OPERATIONS.Insert, |
| 73 | + widget: { |
| 74 | + id: 'my-html-editor-helper', |
| 75 | + type: DIRECT_PLUGIN, |
| 76 | + RenderWidget: () => ( |
| 77 | + <h1 style={{ textAlign: 'center' }}>🦶</h1> |
| 78 | + ), |
| 79 | + }, |
| 80 | + }, |
| 81 | + ] |
| 82 | + } |
| 83 | + }, |
| 84 | +} |
| 85 | + |
| 86 | +export default config; |
| 87 | +``` |
| 88 | +
|
| 89 | +### Custom component with plugin props |
| 90 | +
|
| 91 | + |
| 92 | +
|
| 93 | +The following `env.config.tsx` example demonstrates how to add a custom component to the HTML Editor plugin slot that receives the plugin props. The example shows a Paragon Alert component that renders the current `blockType` provided by the slot: |
| 94 | +
|
| 95 | +```jsx |
| 96 | +import { DIRECT_PLUGIN, PLUGIN_OPERATIONS } from '@openedx/frontend-plugin-framework'; |
| 97 | +import { Alert } from '@openedx/paragon'; |
| 98 | + |
| 99 | +const config = { |
| 100 | + pluginSlots: { |
| 101 | + 'org.openedx.frontend.authoring.text_editor_plugin.v1': { |
| 102 | + plugins: [ |
| 103 | + { |
| 104 | + op: PLUGIN_OPERATIONS.Insert, |
| 105 | + widget: { |
| 106 | + id: 'custom-html-editor-assistant', |
| 107 | + priority: 1, |
| 108 | + type: DIRECT_PLUGIN, |
| 109 | + RenderWidget: ({ blockType }) => { |
| 110 | + return ( |
| 111 | + <Alert variant="success"> |
| 112 | + <Alert.Heading>Custom component for {blockType} HTML editor 🤗🤗🤗</Alert.Heading> |
| 113 | + </Alert> |
| 114 | + ); |
| 115 | + }, |
| 116 | + }, |
| 117 | + op: PLUGIN_OPERATIONS.Insert, |
| 118 | + }, |
| 119 | + ] |
| 120 | + } |
| 121 | + }, |
| 122 | +} |
| 123 | + |
| 124 | +export default config; |
| 125 | +``` |
0 commit comments