index.html 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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
  9. id="container"
  10. style="width: 800px; height: 600px; border: 1px solid grey"
  11. ></div>
  12. <script src="../node_modules/monaco-editor/min/vs/loader.js"></script>
  13. <script>
  14. require.config({ paths: { vs: '../node_modules/monaco-editor/min/vs' } });
  15. require(['vs/editor/editor.main'], function () {
  16. var diffEditor = monaco.editor.createDiffEditor(
  17. document.getElementById('container')
  18. );
  19. Promise.all([xhr('original.txt'), xhr('modified.txt')]).then(function (
  20. r
  21. ) {
  22. var originalTxt = r[0].responseText;
  23. var modifiedTxt = r[1].responseText;
  24. diffEditor.setModel({
  25. original: monaco.editor.createModel(originalTxt, 'javascript'),
  26. modified: monaco.editor.createModel(modifiedTxt, 'javascript')
  27. });
  28. });
  29. });
  30. </script>
  31. <script>
  32. function xhr(url) {
  33. var req = null;
  34. return new Promise(
  35. function (c, e) {
  36. req = new XMLHttpRequest();
  37. req.onreadystatechange = function () {
  38. if (req._canceled) {
  39. return;
  40. }
  41. if (req.readyState === 4) {
  42. if (
  43. (req.status >= 200 && req.status < 300) ||
  44. req.status === 1223
  45. ) {
  46. c(req);
  47. } else {
  48. e(req);
  49. }
  50. req.onreadystatechange = function () {};
  51. }
  52. };
  53. req.open('GET', url, true);
  54. req.responseType = '';
  55. req.send(null);
  56. },
  57. function () {
  58. req._canceled = true;
  59. req.abort();
  60. }
  61. );
  62. }
  63. </script>
  64. </body>
  65. </html>