1
1
import {
2
- commandCustomCode ,
2
+ commandNameCustomCode ,
3
3
keyCustomCode ,
4
4
} from './config' ;
5
5
6
6
export default ( editor , opts = { } ) => {
7
7
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,
9
19
10
- cmd . add ( commandCustomCode , {
11
20
run ( editor , sender , opts = { } ) {
12
21
this . editor = editor ;
13
22
this . options = opts ;
14
23
this . target = opts . target || editor . getSelected ( ) ;
15
24
const target = this . target ;
16
25
17
26
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 ) ;
23
28
}
24
29
} ,
25
30
26
31
stop ( editor ) {
27
32
editor . Modal . close ( ) ;
28
33
} ,
29
34
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
+ */
30
62
getContent ( ) {
31
- const { options , editor , target } = this ;
63
+ const { editor , options , target } = this ;
32
64
const content = document . createElement ( 'div' ) ;
33
65
const codeViewer = this . getCodeViewer ( ) ;
66
+ appendToContent ( content , this . getPreContent ( ) ) ;
34
67
content . appendChild ( codeViewer . getElement ( ) ) ;
68
+ appendToContent ( content , this . getPostContent ( ) ) ;
69
+ appendToContent ( content , this . getContentActions ( ) ) ;
35
70
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 ;
36
80
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 ;
40
83
btn . className = `${ pfx } btn-prim ${ pfx } btn-import__custom-code` ;
41
84
btn . onclick = ( ) => {
42
- const code = codeViewer . getContent ( ) ;
85
+ const code = this . getCodeViewer ( ) . getContent ( ) ;
43
86
target . set ( keyCustomCode , code ) ;
44
87
editor . Modal . close ( ) ;
45
88
} ;
46
89
47
- return content ;
90
+ return btn ;
48
91
} ,
49
92
50
93
getCodeViewer ( ) {
@@ -60,6 +103,8 @@ export default (editor, opts = {}) => {
60
103
}
61
104
62
105
return this . codeViewer ;
63
- }
106
+ } ,
107
+
108
+ ...commandCustomCode ,
64
109
} ) ;
65
110
} ;
0 commit comments