index.html 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  5. </head>
  6. <body>
  7. <h2>Monaco Diff Editor Sample</h2>
  8. <div id="container" style="width: 800px; height: 600px; border: 1px solid grey"></div>
  9. <script src="../node_modules/monaco-editor/min/vs/loader.js"></script>
  10. <script>
  11. require.config({ paths: { vs: '../node_modules/monaco-editor/min/vs' } });
  12. require(['vs/editor/editor.main'], function () {
  13. var diffEditor = monaco.editor.createDiffEditor(document.getElementById('container'));
  14. Promise.all([xhr('original.txt'), xhr('modified.txt')]).then(function (r) {
  15. var originalTxt = r[0].responseText;
  16. var modifiedTxt = r[1].responseText;
  17. diffEditor.setModel({
  18. original: monaco.editor.createModel(originalTxt, 'javascript'),
  19. modified: monaco.editor.createModel(modifiedTxt, 'javascript')
  20. });
  21. });
  22. });
  23. </script>
  24. <script>
  25. function xhr(url) {
  26. var req = null;
  27. return new Promise(
  28. function (c, e) {
  29. req = new XMLHttpRequest();
  30. req.onreadystatechange = function () {
  31. if (req._canceled) {
  32. return;
  33. }
  34. if (req.readyState === 4) {
  35. if ((req.status >= 200 && req.status < 300) || req.status === 1223) {
  36. c(req);
  37. } else {
  38. e(req);
  39. }
  40. req.onreadystatechange = function () {};
  41. }
  42. };
  43. req.open('GET', url, true);
  44. req.responseType = '';
  45. req.send(null);
  46. },
  47. function () {
  48. req._canceled = true;
  49. req.abort();
  50. }
  51. );
  52. }
  53. </script>
  54. </body>
  55. </html>