summaryrefslogtreecommitdiff
path: root/templates/partials/code/index.html
blob: 9b39e5958f133579a7da2d28bcf11a7089065266 (plain)
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
<div class="flex flex-col w-full">
  <div class="flex flex-wrap p-2">
    <p class="w-full md:w-min text-nowrap m-auto">Python 3.10 (sandbox)</p>
      <a 
        href="https://cgit.yetsam.ru/yetsam.git" 
        class="text-neutral-400 underline hover:text-secondary-600 decoration-secondary-600 w-full md:w-min text-nowrap m-auto">
        go to source code (click me)
      </a>

  </div>
  
  {% include 'partials/code/textarea.html' %}

  <div class="flex w-full justify-between p-2">
    <p class="underline" id="output"></p>
    <button onclick="clearEditor()" class="bg-secondary-200 hover:bg-secondary-300 px-2 rounded-md ">Очистить</button>
    <button onclick="executeCode()" class="bg-primary-200 hover:bg-primary-300 px-2 rounded-md">
      Выполнить
    </button>
  </div>
  <div class="w-full bg-white min-h-20 border-2 p-2" id="code-result">
    <p class="text-gray-200">Нажмите "Выполнить"</p>
  </div>
</div>

<script>
  function executeCode(){
    fetch('/restricted_exec',{
      method: "POST",
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        code: document.codeEditor.getValue()
      })
    })
      .then(response => {
        response.json()
          .then(data => {
            const div = document.getElementById('code-result')
            div.replaceChildren([])
            data.forEach(line => {
              const p = document.createElement('p')
              p.innerHTML = line
              div.appendChild(p)
            })

          })
      })
  }
  function clearEditor(){
    document.codeEditor.getDoc().setValue('\n'.repeat(15))
  }
  let countdown = 4
  function countdownTimer(){
    if (countdown > 0){
      countdown--;
      document.getElementById('output').innerHTML = `Вывод: (${countdown})`
      setTimeout(countdownTimer, 1000)
    } else {
      document.getElementById('output').innerHTML = `Вывод:`
      executeCode()
    }
  }
  countdownTimer()

</script>