Skip to content

Commit 44793a5

Browse files
committed
Add handleSave in commands
1 parent 9186e71 commit 44793a5

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/commands.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default (editor, opts = {}) => {
1414
}
1515
}
1616

17+
// Add the custom code command
1718
cmd.add(commandNameCustomCode, {
1819
keyCustomCode,
1920

@@ -76,20 +77,30 @@ export default (editor, opts = {}) => {
7677
* @return {HTMLElement|String}
7778
*/
7879
getContentActions() {
79-
const { editor, options, target } = this;
80+
const { editor } = this;
8081
const btn = document.createElement('button');
8182
const pfx = editor.getConfig('stylePrefix');
8283
btn.innerHTML = opts.buttonLabel;
8384
btn.className = `${pfx}btn-prim ${pfx}btn-import__custom-code`;
84-
btn.onclick = () => {
85-
const code = this.getCodeViewer().getContent();
86-
target.set(keyCustomCode, code);
87-
editor.Modal.close();
88-
};
85+
btn.onclick = () => this.handleSave();
8986

9087
return btn;
9188
},
9289

90+
/**
91+
* Handle the main save task
92+
*/
93+
handleSave() {
94+
const { editor, target } = this;
95+
const code = this.getCodeViewer().getContent();
96+
target.set(keyCustomCode, code);
97+
editor.Modal.close();
98+
},
99+
100+
/**
101+
* Return the code viewer instance
102+
* @return {CodeViewer}
103+
*/
93104
getCodeViewer() {
94105
const { editor } = this;
95106

src/components.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default (editor, opts = {}) => {
9191

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

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default grapesjs.plugins.add('YOUR-PLUGIN-NAME', (editor, opts = {}) => {
3737
codeViewOptions: {},
3838

3939
// Label for the default save button
40-
buttonLabel: 'Insert your code',
40+
buttonLabel: 'Save',
4141

4242
// Object to extend the default custom code command.
4343
// Check the source to see all available methods

0 commit comments

Comments
 (0)