6
6
* Note : Logic > comment
7
7
* License : GNU GPLv3 with Visual Python special exception
8
8
* Date : 2021. 11. 18
9
- * Change Date :
9
+ * Change Date : 2022. 08. 02
10
10
*/
11
11
12
12
//============================================================================
@@ -18,6 +18,67 @@ define([
18
18
] , function ( com_String , PopupComponent ) {
19
19
20
20
const COMMENT_DEFAULT_CODE = '# Write down comments'
21
+ // Templates from NumPy Style Python Docstrings.
22
+ const COMMENT_CLASS_TEMPLATE =
23
+ `
24
+ """Summarize the class in one line.
25
+
26
+ Several sentences ...
27
+ providing an extended description.
28
+
29
+ Note
30
+ ----------
31
+ Note about this class.
32
+
33
+ Parameters
34
+ ----------
35
+ param1 : param_type
36
+ Parameter description.
37
+ param2 : param_type
38
+ Parameter description.
39
+
40
+ Attributes
41
+ ----------
42
+ attr1 : attr_type
43
+ Attibute description.
44
+ attr2 : attr_type
45
+ Attibute description.
46
+
47
+ Examples
48
+ ----------
49
+
50
+ References
51
+ ----------
52
+
53
+ """
54
+ `
55
+ const COMMENT_METHOD_TEMPLATE =
56
+ `
57
+ """Summarize the function in one line.
58
+
59
+ Several sentences ...
60
+ providing an extended description.
61
+
62
+ Parameters
63
+ ----------
64
+ param1 : param_type
65
+ Parameter description.
66
+ param2 : param_type
67
+ Parameter description.
68
+
69
+ Returns
70
+ -------
71
+ return_type
72
+ Return description.
73
+
74
+ Note
75
+ ----------
76
+
77
+ Examples
78
+ ----------
79
+
80
+ """
81
+ `
21
82
22
83
/**
23
84
* Comment
@@ -29,45 +90,87 @@ define([
29
90
this . config . dataview = false ;
30
91
this . config . codeview = false ;
31
92
this . config . saveOnly = true ;
93
+
94
+ this . cmKey = 'code' ; // Code Mirror Key
95
+ this . selectBoxClassName = 'vp-ct-option' ; // Select Box ClassName
32
96
33
97
this . state = {
34
98
code : COMMENT_DEFAULT_CODE ,
35
99
...this . state
100
+ } ;
101
+
102
+ this . cmTemplates = { // a kind of templates
103
+ Template : COMMENT_DEFAULT_CODE ,
104
+ Class : COMMENT_CLASS_TEMPLATE ,
105
+ Method : COMMENT_METHOD_TEMPLATE
36
106
}
37
107
38
- this . _addCodemirror ( 'code' , this . wrapSelector ( '#code' ) ) ;
108
+ this . _addCodemirror ( this . cmKey , this . wrapSelector ( '#code' ) ) ;
39
109
}
40
110
41
111
_bindEvent ( ) {
42
112
super . _bindEvent ( ) ;
43
- /** Implement binding events */
113
+
114
+ var commentTemplates = this . cmTemplates ;
115
+ var cm_key = this . cmKey ;
116
+ let cmCodeListTemp = this . cmCodeList ;
117
+
118
+ // Select box change Event
119
+ $ ( '.' + this . selectBoxClassName ) . on ( 'change' , function ( ) {
120
+ // get code mirror object
121
+ let targetCmObj = cmCodeListTemp . filter ( obj => obj . key == cm_key ) ;
122
+ var templateOption = $ ( this ) . val ( ) ;
123
+ let cm = targetCmObj [ 0 ] . cm ;
124
+
125
+ // Change Code Mirror Text
126
+ if ( templateOption == 'vp_template_class' ) {
127
+ cm . setValue ( commentTemplates . Class ) ;
128
+ } else if ( templateOption == 'vp_template_method' ) {
129
+ cm . setValue ( commentTemplates . Method ) ;
130
+ } else if ( templateOption == 'vp_template_template' ) {
131
+ cm . setValue ( commentTemplates . Template ) ;
132
+ }
133
+
134
+ cm . save ( ) ;
135
+ } ) ;
136
+
44
137
}
45
138
139
+
46
140
templateForBody ( ) {
47
141
/** Implement generating template */
48
142
var page = new com_String ( ) ;
49
143
page . appendFormatLine ( '<textarea name="code" class="code vp-state" id="code">{0}</textarea>'
50
144
, this . state . code ) ;
145
+ // add select box
146
+ page . appendFormatLine ( '<select class="vp-select w100 {0}" >' , this . selectBoxClassName ) ;
147
+ // add options
148
+ Object . entries ( this . cmTemplates ) . forEach ( ( [ opt , t_code ] ) => {
149
+ page . appendFormatLine ( '<option value="{0}">{1}</option>' ,
150
+ 'vp_template_' + opt . toLowerCase ( ) , opt ) ;
151
+ } ) ;
152
+ page . appendFormatLine ( '</select>' ) ;
153
+
51
154
return page . toString ( ) ;
52
155
}
53
156
54
157
open ( ) {
55
158
super . open ( ) ;
56
159
57
160
if ( this . state . code === COMMENT_DEFAULT_CODE ) {
58
- // set default selection
59
- let cmObj = this . getCodemirror ( 'code' ) ;
161
+
162
+ let cmObj = this . getCodemirror ( this . cmKey ) ;
60
163
if ( cmObj && cmObj . cm ) {
61
164
cmObj . cm . setSelection ( { line : 0 , ch : 2 } , { line : 0 } ) ;
62
165
cmObj . cm . focus ( ) ;
63
166
}
64
167
}
65
168
}
66
-
169
+
67
170
generateCode ( ) {
68
171
return this . state . code ;
69
172
}
70
-
173
+
71
174
}
72
175
73
176
return Comment ;
0 commit comments