Skip to content

Commit e01c716

Browse files
committed
Declare plugin options
1 parent 449dfdb commit e01c716

File tree

6 files changed

+106
-61
lines changed

6 files changed

+106
-61
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ This plugin adds the possibility to embed custom code
2424

2525
|Option|Description|Default|
2626
|-|-|-
27-
| `blockLabel` | Label of the custom code block | `Custom Code` |
2827
| `blockCustomCode` | Object to extend the default custom code block, eg. `{ label: 'Custom Code', category: 'Extra', ... }`. Pass a falsy value to avoid adding the block | `{}` |
2928
| `propsCustomCode` | Object to extend the default custom code properties, eg. `{ name: 'Custom Code', droppable: false, ... }` | `{}` |
3029
| `placeholderContent` | Initial content of the custom code component | `<span>Insert here your custom code</span>` |

src/blocks.js renamed to src/blocks.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
import type grapesjs from 'grapesjs';
12
import { typeCustomCode } from './utils';
23

3-
export default (editor, opts = {}) => {
4-
const bm = editor.BlockManager;
5-
const { blockCustomCode, blockLabel } = opts;
4+
export default (editor: grapesjs.Editor, opts = {}) => {
5+
const { Blocks } = editor;
6+
const { blockCustomCode } = opts;
67

7-
blockCustomCode && bm.add(typeCustomCode, {
8-
label: `<svg viewBox="0 0 24 24">
8+
blockCustomCode && Blocks.add(typeCustomCode, {
9+
label: 'Custom Code',
10+
media: `
11+
<svg viewBox="0 0 24 24">
912
<path d="M14.6 16.6l4.6-4.6-4.6-4.6L16 6l6 6-6 6-1.4-1.4m-5.2 0L4.8 12l4.6-4.6L8 6l-6 6 6 6 1.4-1.4z"></path>
1013
</svg>
11-
<div>${blockLabel}</div>`,
14+
`,
1215
category: 'Extra',
1316
activate: true,
1417
select: true,

src/index.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/index.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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>&lt;script&gt;</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;
File renamed without changes.

tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./node_modules/grapesjs-cli/src/template/tsconfig.json",
3+
"include": ["src"]
4+
}

0 commit comments

Comments
 (0)