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' ;
6
4
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 ;
11
7
const { toolbarBtnCustomCode } = opts ;
12
- let timedInterval ;
8
+ let timedInterval : NodeJS . Timeout ;
13
9
14
- dc . addType ( 'script' , {
10
+ Components . addType ( 'script' , {
11
+ // @ts -ignore
15
12
view : {
16
13
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 = '' ) ;
19
18
}
20
19
} ,
21
20
} ) ;
22
21
23
- dc . addType ( typeCustomCode , {
24
-
25
- model : defaultModel . extend ( {
22
+ Components . addType ( typeCustomCode , {
23
+ model : {
26
24
defaults : {
27
- ...defaultModel . prototype . defaults ,
28
25
name : 'Custom Code' ,
29
26
editable : true ,
30
27
...opts . propsCustomCode ,
@@ -34,14 +31,17 @@ export default (editor, opts = {}) => {
34
31
* Initilize the component
35
32
*/
36
33
init ( ) {
37
- this . listenTo ( this , `change:${ keyCustomCode } ` , this . onCustomCodeChange ) ;
34
+ // @ts -ignore
35
+ this . on ( `change:${ keyCustomCode } ` , this . onCustomCodeChange ) ;
38
36
const initialCode = this . get ( keyCustomCode ) || opts . placeholderContent ;
39
37
! this . components ( ) . length && this . components ( initialCode ) ;
40
38
const toolbar = this . get ( 'toolbar' ) ;
41
39
const id = 'custom-code' ;
42
40
43
41
// Add the custom code toolbar button if requested and it's not already in
42
+ // @ts -ignore
44
43
if ( toolbarBtnCustomCode && ! toolbar . filter ( tlb => tlb . id === id ) . length ) {
44
+ // @ts -ignore
45
45
toolbar . unshift ( {
46
46
id,
47
47
command : commandNameCustomCode ,
@@ -56,41 +56,40 @@ export default (editor, opts = {}) => {
56
56
/**
57
57
* Callback to launch on keyCustomCode change
58
58
*/
59
+ // @ts -ignore
59
60
onCustomCodeChange ( ) {
61
+ // @ts -ignore
60
62
this . components ( this . get ( keyCustomCode ) ) ;
61
63
} ,
62
- } , {
63
- /**
64
- * The component can be used only if requested explicitly via `type` property
65
- */
66
- isComponent ( ) {
67
- return false ;
68
- }
69
- } ) ,
64
+ } ,
70
65
71
- view : defaultType . view . extend ( {
66
+ view : {
72
67
events : {
73
68
dblclick : 'onActive' ,
74
69
} ,
75
70
76
71
init ( ) {
72
+ // @ts -ignore
77
73
this . listenTo ( this . model . components ( ) , 'add remove reset' , this . onComponentsChange ) ;
74
+ // @ts -ignore
78
75
this . onComponentsChange ( ) ;
79
76
} ,
80
77
81
78
/**
82
79
* Things to do once inner components of custom code are changed
83
80
*/
81
+ // @ts -ignore
84
82
onComponentsChange ( ) {
85
83
timedInterval && clearInterval ( timedInterval ) ;
86
84
timedInterval = setTimeout ( ( ) => {
87
- const { model } = this ;
85
+ // @ts -ignore
86
+ const { model, el } = this ;
88
87
const content = model . get ( keyCustomCode ) || '' ;
89
88
let droppable = 1 ;
90
89
91
90
// Avoid rendering codes with scripts
92
91
if ( content . indexOf ( '<script' ) >= 0 ) {
93
- this . el . innerHTML = opts . placeholderScript ;
92
+ el . innerHTML = opts . placeholderScript ;
94
93
droppable = 0 ;
95
94
}
96
95
@@ -99,9 +98,10 @@ export default (editor, opts = {}) => {
99
98
} ,
100
99
101
100
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 } ) ;
104
104
} ,
105
- } )
105
+ } ,
106
106
} ) ;
107
107
}
0 commit comments