Skip to content

Commit 9186e71

Browse files
committed
Improve the command
1 parent a176e4b commit 9186e71

File tree

4 files changed

+72
-20
lines changed

4 files changed

+72
-20
lines changed

src/commands.js

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,93 @@
11
import {
2-
commandCustomCode,
2+
commandNameCustomCode,
33
keyCustomCode,
44
} from './config';
55

66
export default (editor, opts = {}) => {
77
const cmd = editor.Commands;
8-
const { modalTitle, codeViewOptions } = opts;
8+
const { modalTitle, codeViewOptions, commandCustomCode } = opts;
9+
const appendToContent = (target, content) => {
10+
if (content instanceof HTMLElement) {
11+
target.appendChild(content);
12+
} else if (content) {
13+
target.insertAdjacentHTML('beforeend', content);
14+
}
15+
}
16+
17+
cmd.add(commandNameCustomCode, {
18+
keyCustomCode,
919

10-
cmd.add(commandCustomCode, {
1120
run(editor, sender, opts = {}) {
1221
this.editor = editor;
1322
this.options = opts;
1423
this.target = opts.target || editor.getSelected();
1524
const target = this.target;
1625

1726
if (target && target.get('editable')) {
18-
const title = opts.title || modalTitle;
19-
const content = this.getContent();
20-
const code = target.get(keyCustomCode) || '';
21-
editor.Modal.open({ title, content });
22-
this.getCodeViewer().setContent(code);
27+
this.showCustomCode(target);
2328
}
2429
},
2530

2631
stop(editor) {
2732
editor.Modal.close();
2833
},
2934

35+
/**
36+
* Method which tells how to show the custom code
37+
* @param {Component} target
38+
*/
39+
showCustomCode(target) {
40+
const { editor, options } = this;
41+
const title = options.title || modalTitle;
42+
const content = this.getContent();
43+
const code = target.get(keyCustomCode) || '';
44+
editor.Modal.open({ title, content });
45+
this.getCodeViewer().setContent(code);
46+
},
47+
48+
/**
49+
* Custom pre-content. Can be a simple string or an HTMLElement
50+
*/
51+
getPreContent() {},
52+
53+
/**
54+
* Custom post-content. Can be a simple string or an HTMLElement
55+
*/
56+
getPostContent() {},
57+
58+
/**
59+
* Get all the content for the custom code
60+
* @return {HTMLElement}
61+
*/
3062
getContent() {
31-
const { options, editor, target } = this;
63+
const { editor, options, target } = this;
3264
const content = document.createElement('div');
3365
const codeViewer = this.getCodeViewer();
66+
appendToContent(content, this.getPreContent());
3467
content.appendChild(codeViewer.getElement());
68+
appendToContent(content, this.getPostContent());
69+
appendToContent(content, this.getContentActions());
3570

71+
return content;
72+
},
73+
74+
/**
75+
* Get the actions content. Can be a simple string or an HTMLElement
76+
* @return {HTMLElement|String}
77+
*/
78+
getContentActions() {
79+
const { editor, options, target } = this;
3680
const btn = document.createElement('button');
37-
const pfx = options.pfx || editor.getConfig('stylePrefix');
38-
content.appendChild(btn);
39-
btn.innerHTML = options.labelBtn || 'Save';
81+
const pfx = editor.getConfig('stylePrefix');
82+
btn.innerHTML = opts.buttonLabel;
4083
btn.className = `${pfx}btn-prim ${pfx}btn-import__custom-code`;
4184
btn.onclick = () => {
42-
const code = codeViewer.getContent();
85+
const code = this.getCodeViewer().getContent();
4386
target.set(keyCustomCode, code);
4487
editor.Modal.close();
4588
};
4689

47-
return content;
90+
return btn;
4891
},
4992

5093
getCodeViewer() {
@@ -60,6 +103,8 @@ export default (editor, opts = {}) => {
60103
}
61104

62105
return this.codeViewer;
63-
}
106+
},
107+
108+
...commandCustomCode,
64109
});
65110
};

src/components.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
keyCustomCode,
3-
commandCustomCode,
3+
commandNameCustomCode,
44
typeCustomCode,
55
} from './config';
66

@@ -35,7 +35,7 @@ export default (editor, opts = {}) => {
3535
if (toolbarBtnCustomCode && !toolbar.filter(tlb => tlb.id === id ).length) {
3636
toolbar.unshift({
3737
id,
38-
command: commandCustomCode,
38+
command: commandNameCustomCode,
3939
label: `<svg viewBox="0 0 24 24">
4040
<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>
4141
</svg>`,
@@ -91,7 +91,7 @@ export default (editor, opts = {}) => {
9191

9292
onActive() {
9393
const target = this.model;
94-
this.em.get('Editor').runCommand(commandCustomCode, { target });
94+
this.em.get('Editor').runCommand(commandNameCustomCode, { target });
9595
},
9696
})
9797
});

src/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const keyCustomCode = 'custom-code-plugin__code';
22
const typeCustomCode = 'custom-code';
3-
const commandCustomCode = 'custom-code:open-modal';
3+
const commandNameCustomCode = 'custom-code:open-modal';
44

55
export {
66
keyCustomCode,
77
typeCustomCode,
8-
commandCustomCode,
8+
commandNameCustomCode,
99
}

src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ export default grapesjs.plugins.add('YOUR-PLUGIN-NAME', (editor, opts = {}) => {
3535

3636
// Additional options for the code viewer, eg. `{ theme: 'hopscotch', readOnly: 0 }`
3737
codeViewOptions: {},
38+
39+
// Label for the default save button
40+
buttonLabel: 'Insert your code',
41+
42+
// Object to extend the default custom code command.
43+
// Check the source to see all available methods
44+
commandCustomCode: {},
3845
}, ...opts };
3946

4047
// Add components

0 commit comments

Comments
 (0)