This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
code_editor.go
103 lines (88 loc) · 2.54 KB
/
code_editor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package forms
import (
"github.com/iwind/TeaGo/utils/string"
"net/http"
)
type CodeEditor struct {
Element
Lang string
Readonly bool
}
func NewCodeEditor(title string, subTitle string) *CodeEditor {
return &CodeEditor{
Element: Element{
Title: title,
Subtitle: subTitle,
},
}
}
func (this *CodeEditor) Super() *Element {
return &this.Element
}
func (this *CodeEditor) Compose() string {
code := this.Value
this.Javascript = `
var codeEditor` + this.Code + `Code = ` + stringutil.JSONEncode(code) + `;
if (codeEditor` + this.Code + `Code == null) {
codeEditor` + this.Code + `Code = "SELECT 1";
}
var codeEditor` + this.Code + `Lang = ` + stringutil.JSONEncode(this.Lang) + `;
var codeEditor` + this.Code + ` = null;
this.$delay(function () {
Tea.Vue.$watch("selectedSource", function (v) {
Tea.delay(function () {
this.loadCodeEditor` + this.Code + `();
}, 1000);
});
});
this.loadCodeEditor` + this.Code + ` = function () {
//if (codeEditor` + this.Code + ` == null) {
codeEditor` + this.Code + ` = CodeMirror.fromTextArea(document.getElementById("code-editor-` + this.Code + `"), {
//theme: "idea",
lineNumbers: true,
value: "",
readOnly: ` + stringutil.JSONEncode(this.Readonly) + `,
showCursorWhenSelecting: true,
height: "auto",
//scrollbarStyle: null,
viewportMargin: Infinity,
lineWrapping: true,
highlightFormatting: false,
indentUnit: 4,
indentWithTabs: true
});
//}
var that = this;
codeEditor` + this.Code + `.setValue(codeEditor` + this.Code + `Code);
codeEditor` + this.Code + `.save();
var info = CodeMirror.findModeByMIME(codeEditor` + this.Code + `Lang);
if (info != null) {
codeEditor` + this.Code + `.setOption("mode", info.mode);
CodeMirror.modeURL = "/codemirror/mode/%N/%N.js";
CodeMirror.autoLoadMode(codeEditor` + this.Code + `, info.mode);
}
codeEditor` + this.Code + `.on("change", function () {
codeEditor` + this.Code + `.save();
});
};`
this.CSS = `/** codemirror **/
/** codemirror **/
.CodeMirror {
border: 1px solid #eee;
height: auto!important;
}
.CodeMirror-vscrollbar {
width: 6px;
border-radius: 3px!important;
}
.CodeMirror-vscrollbar::-webkit-scrollbar-thumb {
border-radius: 2px;
}
`
return `<textarea name="` + this.Code + `_code" id="code-editor-` + this.Code + `" rows="1"></textarea>`
}
func (this *CodeEditor) ApplyRequest(req *http.Request) (value interface{}, skip bool, err error) {
return map[string]interface{}{
"code": req.Form.Get(this.Namespace + "_code"),
}, false, nil
}