var app = getApp()
Page({
data: {
height: 20,
focus: false,
pythonCode: "",
hideTabName: "关键字",
showTabName: "案例",
showTab: "demoTab",
hideTab: "keywordTab",
codes: [
{ key: 'hello1', display: 'Hellow World!', code:'print("Hellow World!")'},
{ key: 'hello2', display: 'Hellow World!', code: 'print("Hellow World!")' },
{ key: 'hello3', display: 'Hellow World!', code: 'print("Hellow World!")' },
{ key: 'hello4', display: 'Hellow World!', code: 'print("Hellow World!")' },
{ key: 'hello5', display: 'Hellow World!', code: 'print("Hellow World!")' }
],
keywords: [
{ key: 'if', display: 'if', code: 'if' },
{ key: 'while', display: 'while', code: 'while' },
{ key: 'try', display: 'try', code: 'try' },
{ key: 'str', display: 'str', code: 'str' },
{ key: 'pint', display: 'pint', code: 'print' },
{ key: 'for', display: 'for', code: 'for' }
]
},
onLoad: function (options) {
var page = this;
if (!(app.editor.codes) && !(app.editor.keywords)) {
var url = app.domain + "/codes"
wx.request({
url: url,
data: {
code: this.data.pythonCode,
},
method: 'POST',
dataType: 'json',
header: {
'content-type': 'application/json'
},
success: function (res) {
app.editor.codes = res.data.codes,
app.editor.keywords = res.data.keywords
page.setData({
codes: res.data.codes,
keywords:res.data.keywords
})
}
})
} else {
page.setData({
codes: app.editor.codes,
keywords: app.editor.keywords
})
}
},
onReady: function () {
},
bindTextAreaFocus: function (e) {
},
bindTextAreaBlur: function (e) {
},
bindinputHandler: function (e) {
console.log(e.detail.value)
this.setData({
pythonCode: e.detail.value,
})
},
execute: function (e) {
var page = this;
app.editor.sourceCode = "";
app.editor.result = "";
app.editor.notify = "";
var url = app.domain + "/run"
console.log(page.data.pythonCode)
wx.request({
url: url,
data: {
code: page.data.pythonCode,
},
method: 'POST',
dataType: 'json',
header: {
'content-type': 'application/json'
},
success: function (res) {
app.editor.sourceCode = page.data.pythonCode,
app.editor.result = res.data.output,
app.editor.notify = res.data.notify,
wx.navigateTo({
url: 'result/result',
})
}
})
},
changeTab: function (e) {
var currentShowTab = this.data.showTab;
var currentHideTab = this.data.hideTab;
var currentShowTabName = this.data.showTabName;
var currentHideTabName = this.data.hideTabName;
this.setData({
showTab: currentHideTab,
hideTab: currentShowTab,
showTabName: currentHideTabName,
hideTabName: currentShowTabName,
})
},
insertKeyWord: function (e) {
var page = this;
var key = e.currentTarget.dataset.word;
var value;
for (var i = 0; i < page.data.keywords.length ; i++) {
if (page.data.keywords[i].key == key) {
value = page.data.keywords[i].code;
break;
}
}
console.log(page.data.pythonCode + " " + value)
page.setData({
pythonCode: page.data.pythonCode + " " + value,
})
console.log(e)
},
insertCode: function (e) {
var page = this;
var key = e.currentTarget.dataset.code;
var value;
for (var i = 0; i < page.data.keywords.length; i++) {
if (page.data.codes[i].key == key) {
value = page.data.codes[i].code;
break;
}
}
console.log(value)
page.setData({
pythonCode: value,
})
console.log(e)
}
})
|