Skip to content

Commit b63e4e7

Browse files
committed
Update components
1 parent 769484c commit b63e4e7

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed
Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
import {
2-
keyCustomCode,
3-
commandNameCustomCode,
4-
typeCustomCode,
5-
} from './utils';
1+
import type grapesjs from 'grapesjs';
2+
import { PluginOptions } from '.';
3+
import { keyCustomCode, commandNameCustomCode, typeCustomCode } from './utils';
64

7-
export default (editor, opts = {}) => {
8-
const dc = editor.DomComponents;
9-
const defaultType = dc.getType('default');
10-
const defaultModel = defaultType.model;
5+
export default (editor: grapesjs.Editor, opts: PluginOptions = {}) => {
6+
const { Components } = editor;
117
const { toolbarBtnCustomCode } = opts;
12-
let timedInterval;
8+
let timedInterval: NodeJS.Timeout;
139

14-
dc.addType('script', {
10+
Components.addType('script', {
11+
// @ts-ignore
1512
view: {
1613
onRender() {
17-
const isCC = this.model.closestType(typeCustomCode);
18-
isCC && (this.el.innerHTML = '');
14+
// @ts-ignore
15+
const { model, el } = this;
16+
const isCC = model.closestType(typeCustomCode);
17+
isCC && (el.innerHTML = '');
1918
}
2019
},
2120
});
2221

23-
dc.addType(typeCustomCode, {
24-
25-
model: defaultModel.extend({
22+
Components.addType(typeCustomCode, {
23+
model: {
2624
defaults: {
27-
...defaultModel.prototype.defaults,
2825
name: 'Custom Code',
2926
editable: true,
3027
...opts.propsCustomCode,
@@ -34,14 +31,17 @@ export default (editor, opts = {}) => {
3431
* Initilize the component
3532
*/
3633
init() {
37-
this.listenTo(this, `change:${keyCustomCode}`, this.onCustomCodeChange);
34+
// @ts-ignore
35+
this.on(`change:${keyCustomCode}`, this.onCustomCodeChange);
3836
const initialCode = this.get(keyCustomCode) || opts.placeholderContent;
3937
!this.components().length && this.components(initialCode);
4038
const toolbar = this.get('toolbar');
4139
const id = 'custom-code';
4240

4341
// Add the custom code toolbar button if requested and it's not already in
42+
// @ts-ignore
4443
if (toolbarBtnCustomCode && !toolbar.filter(tlb => tlb.id === id ).length) {
44+
// @ts-ignore
4545
toolbar.unshift({
4646
id,
4747
command: commandNameCustomCode,
@@ -56,41 +56,40 @@ export default (editor, opts = {}) => {
5656
/**
5757
* Callback to launch on keyCustomCode change
5858
*/
59+
// @ts-ignore
5960
onCustomCodeChange() {
61+
// @ts-ignore
6062
this.components(this.get(keyCustomCode));
6163
},
62-
}, {
63-
/**
64-
* The component can be used only if requested explicitly via `type` property
65-
*/
66-
isComponent() {
67-
return false;
68-
}
69-
}),
64+
},
7065

71-
view: defaultType.view.extend({
66+
view: {
7267
events: {
7368
dblclick: 'onActive',
7469
},
7570

7671
init() {
72+
// @ts-ignore
7773
this.listenTo(this.model.components(), 'add remove reset', this.onComponentsChange);
74+
// @ts-ignore
7875
this.onComponentsChange();
7976
},
8077

8178
/**
8279
* Things to do once inner components of custom code are changed
8380
*/
81+
// @ts-ignore
8482
onComponentsChange() {
8583
timedInterval && clearInterval(timedInterval);
8684
timedInterval = setTimeout(() => {
87-
const { model } = this;
85+
// @ts-ignore
86+
const { model, el } = this;
8887
const content = model.get(keyCustomCode) || '';
8988
let droppable = 1;
9089

9190
// Avoid rendering codes with scripts
9291
if (content.indexOf('<script') >= 0) {
93-
this.el.innerHTML = opts.placeholderScript;
92+
el.innerHTML = opts.placeholderScript;
9493
droppable = 0;
9594
}
9695

@@ -99,9 +98,10 @@ export default (editor, opts = {}) => {
9998
},
10099

101100
onActive() {
102-
const target = this.model;
103-
this.em.get('Commands').run(commandNameCustomCode, { target });
101+
// @ts-ignore
102+
const { model, em } = this;
103+
em.get('Commands').run(commandNameCustomCode, { target: model });
104104
},
105-
})
105+
},
106106
});
107107
}

0 commit comments

Comments
 (0)