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