|
12 | 12 | <div class="card-body">
|
13 | 13 | <h2><%= file[:path] %></h2>
|
14 | 14 | <div class="code-pair" data-path="<%= file[:path] %>">
|
15 |
| - <div class="submission-code"><%= file[:submission_contents] %></div> |
16 |
| - <div class="model-code"><%= file[:model_contents] %></div> |
| 15 | + <pre class="submission-code"><%= file[:submission_contents] %></pre> |
| 16 | + <pre class="model-code"><%= file[:model_contents] %></pre> |
17 | 17 | </div>
|
18 | 18 | </div>
|
19 | 19 | </div>
|
20 | 20 | <br>
|
21 | 21 | <% end %>
|
22 | 22 |
|
23 |
| -<script src="https://cdnjs.cloudflare.com/ajax/libs/diff_match_patch/20121119/diff_match_patch.js" integrity="sha256-qsdlWiFdhEjZXZhx6NikCsPZqvONgasszEPBzSZ7j6M=" crossorigin="anonymous"></script> |
| 23 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.10.1/min/vs/loader.js" integrity="sha256-PwMxLuduSzsT9fefu+Nz/M8z0sYHV9Pp77VxsOxk1Pc=" crossorigin="anonymous"></script> |
| 24 | +<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.10.1/min/vs/editor/editor.main.css" integrity="sha256-2HiLV16hBsfTgFEvAitDCQkXtBIyBROeLVntVZrKBJg=" crossorigin="anonymous" /> |
24 | 25 |
|
25 | 26 | <script>
|
26 |
| - var dmp = new diff_match_patch(); |
27 |
| - document.querySelectorAll('.code-pair').forEach(function (pairElement) { |
28 |
| - var path = pairElement.dataset.path; |
29 |
| - var submissionCode = pairElement.querySelector('.submission-code').innerHTML; |
30 |
| - var modelCode = pairElement.querySelector('.model-code').innerHTML; |
31 |
| - var diff = dmp.diff_main(modelCode, submissionCode); |
32 |
| - var outputHtml = dmp.diff_prettyHtml(diff).replace(/¶/g, ''); |
33 |
| - pairElement.innerHTML = outputHtml; |
34 |
| - }); |
| 27 | + require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.10.1/min/vs/' }}); |
| 28 | + |
| 29 | + // Before loading vs/editor/editor.main, define a global MonacoEnvironment that overwrites |
| 30 | + // the default worker url location (used when creating WebWorkers). The problem here is that |
| 31 | + // HTML5 does not allow cross-domain web workers, so we need to proxy the instantiation of |
| 32 | + // a web worker through a same-domain script |
| 33 | + window.MonacoEnvironment = { |
| 34 | + getWorkerUrl: function(workerId, label) { |
| 35 | + return '/monaco-editor-worker-loader-proxy.js'; |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + require(["vs/editor/editor.main"], function () { |
| 40 | + document.querySelectorAll('.code-pair').forEach(function (pairElement) { |
| 41 | + |
| 42 | + var path = pairElement.dataset.path; |
| 43 | + var submissionElement = pairElement.querySelector('.submission-code'); |
| 44 | + var submissionCode = submissionElement.innerHTML; |
| 45 | + var modelElement = pairElement.querySelector('.model-code'); |
| 46 | + var modelCode = modelElement.innerHTML; |
| 47 | + |
| 48 | + var submissionHeight = submissionElement.clientHeight; |
| 49 | + var modelHeight = modelElement.clientHeight; |
| 50 | + |
| 51 | + var height; |
| 52 | + if (submissionHeight > modelHeight) { |
| 53 | + height = submissionHeight; |
| 54 | + } else { |
| 55 | + height = modelHeight; |
| 56 | + } |
| 57 | + pairElement.style.height = height + "px"; |
| 58 | + |
| 59 | + |
| 60 | + pairElement.innerHTML = ""; |
| 61 | + var diffEditor = monaco.editor.createDiffEditor(pairElement, { |
| 62 | + enableSplitViewResizing: true, |
| 63 | + scrollBeyondLastLine: false, |
| 64 | + readOnly: true, |
| 65 | + renderSideBySide: false, |
| 66 | + language: 'java', // TODO: detect from file extension |
| 67 | + }); |
| 68 | + diffEditor.setModel({ |
| 69 | + original: monaco.editor.createModel(modelCode, "text/plain"), |
| 70 | + modified: monaco.editor.createModel(submissionCode, "text/plain"), |
| 71 | + }); |
| 72 | + }); |
| 73 | + }); |
35 | 74 | </script>
|
36 | 75 |
|
37 |
| -<style> |
38 |
| - ins, .code-pair span { |
39 |
| - font-family: monospace; |
40 |
| - white-space: pre; |
41 |
| - text-decoration: none; |
42 |
| - } |
43 |
| -</style> |
| 76 | + |
0 commit comments