Editor.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <div>
  3. <div class="codemirror">
  4. <codemirror
  5. ref="inputCm"
  6. v-model="formData.functionScript"
  7. :options="inputCmOptions"
  8. @ready="onCmReady"
  9. @focus="onCmFocus"
  10. @input="onCmCodeChange"
  11. />
  12. </div>
  13. <Button @click.native.prevent="commitCode" />
  14. <div class="codemirror">
  15. <codemirror
  16. ref="outputCm"
  17. :options="outputCmOptions"
  18. @ready="onCmReady"
  19. @focus="onCmFocus"
  20. @input="onCmCodeChange"
  21. />
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. /* eslint-disable @typescript-eslint/no-unused-vars */
  27. /* eslint-disable no-console */
  28. import Button from './Button.vue'
  29. export default {
  30. name: 'Editor',
  31. components: { Button },
  32. data() {
  33. return {
  34. formData: {
  35. functionScript: ''
  36. },
  37. inputCmOptions: {
  38. autoCloseBrackets: true,
  39. tabSize: 4, // tab字符的宽度,默认为4
  40. styleActiveLine: true, // 当前行背景高亮
  41. lineNumbers: true, // 显示行数
  42. line: true,
  43. mode: 'text/x-python',
  44. theme: 'lesser-dark'
  45. },
  46. outputCmOptions: {
  47. autoCloseBrackets: true,
  48. tabSize: 4, // tab字符的宽度,默认为4
  49. styleActiveLine: true, // 当前行背景高亮
  50. lineNumbers: true, // 显示行数
  51. line: true,
  52. readOnly: true,
  53. mode: 'text/x-python',
  54. theme: 'paraiso-light'
  55. },
  56. code: ''
  57. }
  58. },
  59. computed: {
  60. codemirror() {
  61. return this.$refs.inputCm.codemirror
  62. }
  63. },
  64. mounted() {
  65. this.initCode()
  66. },
  67. methods: {
  68. initCode() {
  69. this.$refs.inputCm.codemirror.setValue(
  70. 'class CurveFp(object):\n'+
  71. ' def __init__(self, p, a, b):\n'+
  72. ' """ y^2 = x^3 + a*x + b (mod p)."""\n'+
  73. ' self.p = p\n'+
  74. ' self.a = a\n'+
  75. ' self.b = b\n'+
  76. "class Point(object):\n"+
  77. " def __init__(self, curve, x, y, order=None):\n"+
  78. " self.curve = curve\n"+
  79. " self.x = x\n"+
  80. " self.y = y\n"+
  81. " self.order = order\n"+
  82. "def add(point1,point2):\n"+
  83. ' """to be done"""\n'+
  84. "def mul(n,point0):\n"+
  85. ' """to be done"""\n'
  86. )
  87. },
  88. onCmReady(cm) {
  89. // console.log('the editor is readied!', cm)
  90. // this.codemirror.setSize(('900px','full'))
  91. this.$refs.inputCm.codemirror.setSize('full', '500px')
  92. },
  93. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  94. onCmFocus(cm) {
  95. // console.log('the editor is focus!', cm)
  96. },
  97. onCmCodeChange(newCode) {
  98. /* console.log('this is new code', newCode)
  99. this.formData.functionScript = newCode
  100. this.$emit('codeChange', this.formData.functionScript) */
  101. this.code = newCode
  102. },
  103. /* commitCode(){
  104. console.log(this.code)
  105. }, */
  106. async commitCode() {
  107. const commitCodes = this.$refs.inputCm.codemirror.getValue()
  108. console.log(commitCodes)
  109. const _this = this
  110. await this.$axios
  111. .post('/api/commit', {
  112. code: commitCodes,
  113. level: 1
  114. })
  115. .then(function (res) {
  116. console.log(res)
  117. _this.code = res.data
  118. console.log(_this.code)
  119. })
  120. this.$refs.outputCm.codemirror.setValue(this.code.toString())
  121. }
  122. }
  123. }
  124. </script>
  125. <style></style>