|
| 1 | +import type grapesjs from 'grapesjs'; |
| 2 | +import loadComponents from './components'; |
| 3 | +import loadBlocks from './blocks'; |
| 4 | +import loadCommands from './commands'; |
| 5 | + |
| 6 | +export type PluginOptions = { |
| 7 | + /** |
| 8 | + * Object to extend the default custom code block. Pass a falsy value to avoid adding the block |
| 9 | + * @example |
| 10 | + * { label: 'Custom Code', category: 'Extra', ... } |
| 11 | + */ |
| 12 | + blockCustomCode?: Partial<grapesjs.BlockOptions>; |
| 13 | + |
| 14 | + /** |
| 15 | + * Object to extend the default custom code properties. |
| 16 | + * @example |
| 17 | + * { name: 'Custom Code', droppable: false, ... } |
| 18 | + */ |
| 19 | + propsCustomCode?: grapesjs.ComponentDefinition; |
| 20 | + |
| 21 | + /** |
| 22 | + * Initial content of the custom code component |
| 23 | + * @default '<span>Insert here your custom code</span>' |
| 24 | + */ |
| 25 | + placeholderContent?: string; |
| 26 | + |
| 27 | + /** |
| 28 | + * Object to extend the default component's toolbar button for the code. Pass a falsy value to avoid adding the button |
| 29 | + * @example |
| 30 | + * { label: '</>', attributes: { title: 'Open custom code' } } |
| 31 | + */ |
| 32 | + toolbarBtnCustomCode?: Record<string, any>; |
| 33 | + |
| 34 | + /** |
| 35 | + * Content to show when the custom code contains `<script>` |
| 36 | + */ |
| 37 | + placeholderScript?: string; |
| 38 | + |
| 39 | + /** |
| 40 | + * Title for the custom code modal |
| 41 | + * @default 'Insert your code' |
| 42 | + */ |
| 43 | + modalTitle?: string; |
| 44 | + |
| 45 | + /** |
| 46 | + * Additional options for the code viewer. |
| 47 | + * @example |
| 48 | + * { theme: 'hopscotch', readOnly: 0 } |
| 49 | + */ |
| 50 | + codeViewOptions?: Record<string, any>; |
| 51 | + |
| 52 | + /** |
| 53 | + * Label for the default save button |
| 54 | + * @default 'Save' |
| 55 | + */ |
| 56 | + buttonLabel?: string; |
| 57 | + |
| 58 | + /** |
| 59 | + * Object to extend the default custom code command. |
| 60 | + */ |
| 61 | + commandCustomCode?: Record<string, any>; |
| 62 | +}; |
| 63 | + |
| 64 | +const plugin: grapesjs.Plugin<PluginOptions> = (editor, opts = {}) => { |
| 65 | + const options: PluginOptions = { |
| 66 | + blockCustomCode: {}, |
| 67 | + propsCustomCode: {}, |
| 68 | + placeholderContent: '<span>Insert here your custom code</span>', |
| 69 | + toolbarBtnCustomCode: {}, |
| 70 | + placeholderScript: `<div style="pointer-events: none; padding: 10px;"> |
| 71 | + <svg viewBox="0 0 24 24" style="height: 30px; vertical-align: middle;"> |
| 72 | + <path d="M13 14h-2v-4h2m0 8h-2v-2h2M1 21h22L12 2 1 21z"></path> |
| 73 | + </svg> |
| 74 | + Custom code with <i><script></i> can't be rendered on the canvas |
| 75 | + </div>`, |
| 76 | + modalTitle: 'Insert your code', |
| 77 | + codeViewOptions: {}, |
| 78 | + buttonLabel: 'Save', |
| 79 | + commandCustomCode: {}, |
| 80 | + ...opts |
| 81 | + }; |
| 82 | + |
| 83 | + // Add components |
| 84 | + loadComponents(editor, options); |
| 85 | + |
| 86 | + // Add blocks |
| 87 | + loadBlocks(editor, options); |
| 88 | + |
| 89 | + // Add commands |
| 90 | + loadCommands(editor, options); |
| 91 | +}; |
| 92 | + |
| 93 | +export default plugin; |
0 commit comments