File size: 8,761 Bytes
a641ed5
dcce181
 
a641ed5
dcce181
a641ed5
 
 
dcce181
a641ed5
dcce181
a641ed5
 
 
 
 
 
dcce181
 
 
 
a641ed5
 
 
 
 
 
 
 
 
 
dcce181
a641ed5
 
 
 
 
 
 
 
 
 
 
 
 
dcce181
 
 
a641ed5
 
 
 
 
dcce181
a641ed5
 
dcce181
 
a641ed5
 
dcce181
 
 
 
 
 
 
 
a641ed5
 
 
dcce181
 
a641ed5
 
 
 
 
 
 
 
 
 
dcce181
 
 
 
a641ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcce181
a641ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcce181
a641ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcce181
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { useRef } from 'react'
import { LangBadge } from './primitives'

// Osnovna syntax tinting za čitljivost (nije full highlighter)
function SyntaxTinted({ code }) {
  const tokens = code.split(
    /(\b(?:def|return|if|else|elif|for|while|import|from|class|in|not|and|or|True|False|None|self|function|const|let|var|public|private|static|void|int|string|bool|unsigned|include|struct|typedef)\b|"[^"]*"|'[^']*'|`[^`]*`|\/\/[^\n]*|#[^\n]*|\b\d+\b)/g
  )
  return tokens.map((t, i) => {
    if (/^(def|return|if|else|elif|for|while|import|from|class|in|not|and|or|True|False|None|self|function|const|let|var|public|private|static|void|int|string|bool|unsigned|struct|typedef)$/.test(t))
      return <span key={i} style={{ color: '#22E0FF' }}>{t}</span>
    if (/^["'`].*["'`]$/.test(t))
      return <span key={i} style={{ color: '#10B981' }}>{t}</span>
    if (/^(#|\/\/)/.test(t))
      return <span key={i} style={{ color: '#64748B', fontStyle: 'italic' }}>{t}</span>
    if (/^\d+$/.test(t))
      return <span key={i} style={{ color: '#F59E0B' }}>{t}</span>
    return <span key={i}>{t}</span>
  })
}

export default function CodeEditor({
  code,
  onChange,
  lang = 'Python',
  readOnly = false,
  height = 360,
  annotations = [],
  filename,
}) {
  const scrollRef = useRef(null)
  const lines = (code || '').split('\n')
  const displayName = filename || ('submission.' + (
    lang === 'Python' ? 'py' :
    lang === 'Java'   ? 'java' :
    lang === 'JavaScript' ? 'js' :
    lang === 'C++'    ? 'cpp' :
    lang === 'C'      ? 'c' : 'txt'
  ))

  // Mapa linija na anotacije za brzo traženje
  const annotMap = {}
  for (const a of annotations) {
    annotMap[a.line] = a
  }

  return (
    <div style={{
      background: '#0F172A',
      border: '1px solid rgba(148,163,184,0.12)',
      borderRadius: 10,
      overflow: 'hidden',
      boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.03)',
    }}>

      {/* ── Header ── */}
      <div style={{
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        padding: '10px 14px',
        borderBottom: '1px solid rgba(148,163,184,0.12)',
        background: 'rgba(15,23,41,0.6)',
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <div style={{ display: 'flex', gap: 6 }}>
            <span style={{ width: 10, height: 10, borderRadius: '50%', background: '#EF4444', opacity: 0.6 }}/>
            <span style={{ width: 10, height: 10, borderRadius: '50%', background: '#F59E0B', opacity: 0.6 }}/>
            <span style={{ width: 10, height: 10, borderRadius: '50%', background: '#10B981', opacity: 0.6 }}/>
          </div>
          <span style={{ fontFamily: 'JetBrains Mono, monospace', fontSize: 12, color: '#94A3B8' }}>
            {displayName}
          </span>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          {annotations.length > 0 && (
            <span style={{
              fontSize: 10, color: '#F59E0B', letterSpacing: '0.04em',
              background: 'rgba(245,158,11,0.10)', border: '1px solid rgba(245,158,11,0.25)',
              padding: '2px 8px', borderRadius: 4,
            }}>
              {annotations.filter(a => a.tone === 'red').length} strong ·{' '}
              {annotations.filter(a => a.tone === 'amber').length} moderate
            </span>
          )}
          <LangBadge lang={lang}/>
        </div>
      </div>

      {/* ── Editor tijelo ──
           Jedan scroll container koji sadrži i broj linije i kod,
           tako da se uvijek pomiču zajedno.
      */}
      {readOnly ? (
        <div ref={scrollRef} style={{ height, overflow: 'auto' }}>
          <table style={{
            borderCollapse: 'collapse',
            width: '100%',
            fontFamily: 'JetBrains Mono, monospace',
            fontSize: 12,
            lineHeight: 1.7,
          }}>
            <tbody>
              {lines.map((line, idx) => {
                const lineNum = idx + 1
                const ann = annotMap[lineNum]
                const isRed   = ann?.tone === 'red'
                const isAmber = ann?.tone === 'amber'
                const bgColor = isRed
                  ? 'rgba(239,68,68,0.10)'
                  : isAmber
                  ? 'rgba(245,158,11,0.08)'
                  : 'transparent'
                const borderLeft = isRed
                  ? '2px solid #EF4444'
                  : isAmber
                  ? '2px solid #F59E0B'
                  : '2px solid transparent'

                return (
                  <tr key={idx} title={ann?.note || ''} style={{ background: bgColor }}>

                    {/* Broj linije */}
                    <td style={{
                      width: 44,
                      minWidth: 44,
                      padding: '0 10px 0 16px',
                      textAlign: 'right',
                      color: ann ? (isRed ? '#EF4444' : '#F59E0B') : '#475569',
                      userSelect: 'none',
                      borderRight: '1px solid rgba(148,163,184,0.08)',
                      borderLeft,
                      verticalAlign: 'top',
                      paddingTop: '1px',
                      whiteSpace: 'nowrap',
                    }}>
                      {lineNum}
                    </td>

                    {/* Ikona oznake (samo za označene linije) */}
                    <td style={{
                      width: 20,
                      minWidth: 20,
                      textAlign: 'center',
                      verticalAlign: 'top',
                      paddingTop: '2px',
                      color: isRed ? '#EF4444' : isAmber ? '#F59E0B' : 'transparent',
                      fontSize: 9,
                    }}>
                      {ann ? '⚠' : ''}
                    </td>

                    {/* Kod */}
                    <td style={{
                      padding: '0 16px',
                      color: '#CBD5E1',
                      whiteSpace: 'pre',
                      verticalAlign: 'top',
                    }}>
                      <SyntaxTinted code={line}/>
                    </td>

                    {/* Tooltip poruka (prikazuje se desno ako postoji anotacija) */}
                    {ann && (
                      <td style={{
                        padding: '0 12px',
                        fontSize: 10,
                        color: isRed ? '#EF4444' : '#F59E0B',
                        background: isRed ? 'rgba(239,68,68,0.08)' : 'rgba(245,158,11,0.08)',
                        whiteSpace: 'nowrap',
                        verticalAlign: 'top',
                        maxWidth: 280,
                        overflow: 'hidden',
                        textOverflow: 'ellipsis',
                        paddingTop: '2px',
                        fontStyle: 'italic',
                      }}>
                        {ann.note}
                      </td>
                    )}
                  </tr>
                )
              })}
            </tbody>
          </table>
        </div>
      ) : (
        // Edit mode — textarea s brojevima linija u sync
        <div style={{ display: 'flex', height, overflow: 'hidden' }}>
          {/* Brojevi linija za edit mode — sinkronizirani scrollom */}
          <div style={{
            padding: '12px 12px 12px 16px',
            background: 'rgba(10,15,28,0.4)',
            fontFamily: 'JetBrains Mono, monospace',
            fontSize: 12,
            lineHeight: 1.7,
            color: '#475569',
            textAlign: 'right',
            userSelect: 'none',
            borderRight: '1px solid rgba(148,163,184,0.08)',
            minWidth: 44,
            overflowY: 'hidden',
            pointerEvents: 'none',
          }}>
            {lines.map((_, i) => <div key={i}>{i + 1}</div>)}
          </div>
          <textarea
            value={code}
            onChange={e => onChange && onChange(e.target.value)}
            spellCheck="false"
            placeholder="Paste your code here…"
            onScroll={e => {
              // Sinkroniziramo scroll brojeva linija s textarea
              const prev = e.target.previousSibling
              if (prev) prev.scrollTop = e.target.scrollTop
            }}
            style={{
              flex: 1,
              border: 'none',
              outline: 'none',
              resize: 'none',
              background: 'transparent',
              color: '#CBD5E1',
              fontFamily: 'JetBrains Mono, monospace',
              fontSize: 12,
              lineHeight: 1.7,
              padding: '12px 16px',
              overflowY: 'auto',
            }}
          />
        </div>
      )}
    </div>
  )
}