v1.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. setTimeout(function()
  2. {
  3. $.getLib(config.webRoot + 'js/monaco-editor/min/vs/loader.js', {root: false}, function()
  4. {
  5. createMonaco(id, action, options, diffContent, onMouseDown, onMouseMove, vsPath, clientLang);
  6. resize(id);
  7. });
  8. }, 200);
  9. function createMonaco(id, action, options, diffContent, onMouseDown, onMouseMove, vsPath, clientLang)
  10. {
  11. if(!options.minimap) options.minimap = {enabled: false};
  12. require.config({
  13. paths: {vs: vsPath},
  14. 'vs/nls': {
  15. availableLanguages: {
  16. '*': clientLang
  17. }
  18. }
  19. });
  20. let decorations = [];
  21. let programmaticSelectionRange = null;
  22. require(['vs/editor/editor.main'], function ()
  23. {
  24. if(!lineMap && action == 'diff' && diffContent && diffContent.line) lineMap = diffContent.line.new;
  25. if(lineMap) options.lineNumbers = (line) => {return lineMap[line - 1];};
  26. $.extend(options, {
  27. // 关键配置项
  28. automaticLayout: true, // 自动调整布局
  29. scrollBeyondLastLine: false, // 禁用底部滚动区域
  30. minimap: {enabled: false}, // 可选:禁用缩略图以节省空间
  31. // 编辑器尺寸配置
  32. fontSize: 14,
  33. lineHeight: 24,
  34. lineNumbersMinChars: 2,
  35. glyphMargin: true, // 启用装饰器边距
  36. // 内边距设置
  37. padding: {top: 8, bottom: 8}
  38. });
  39. if(action == 'diff')
  40. {
  41. options.renderSideBySide = $.cookie.get('renderSideBySide') == 'true';
  42. modifiedEditor = monaco.editor.createDiffEditor(document.getElementById(id), options);
  43. window.modifiedEditor = modifiedEditor;
  44. modifiedEditor.setModel({
  45. original: monaco.editor.createModel(diffContent.code.old, options.lang),
  46. modified: monaco.editor.createModel(diffContent.code.new, options.lang),
  47. });
  48. editor = modifiedEditor.getModifiedEditor();
  49. const getOriginalEditor = modifiedEditor.getOriginalEditor();
  50. getOriginalEditor.updateOptions({
  51. lineNumbers: function(number)
  52. {
  53. var oldlc = diffContent.line.old;
  54. return oldlc[number - 1];
  55. }
  56. });
  57. if(onMouseDown) getOriginalEditor.onMouseDown(function(obj){eval(onMouseDown + '(obj)')})
  58. if(onMouseMove) getOriginalEditor.onMouseMove(function(obj){eval(onMouseMove + '(obj)')})
  59. }
  60. else
  61. {
  62. editor = monaco.editor.create(document.getElementById(id), options);
  63. }
  64. if(onMouseDown) editor.onMouseDown(function(obj){eval(onMouseDown + '(obj)')})
  65. if(onMouseMove) editor.onMouseMove(function(obj){eval(onMouseMove + '(obj)')})
  66. let range = null;
  67. if(selectedLines)
  68. {
  69. const lines = selectedLines.split(',');
  70. let startLine = parseInt(lines[0]);
  71. let endLine = parseInt(lines[1] || startLine + 1);
  72. let startCol = 1;
  73. let endCol = 0;
  74. if(lines.length == 4)
  75. {
  76. startCol = parseInt(lines[2]);
  77. endCol = parseInt(lines[3]);
  78. }
  79. if(endCol == 0) endLine += 1;
  80. if(lineMap)
  81. {
  82. lineMap.forEach((line, index) =>
  83. {
  84. if(line == startLine) startLine = index + 1;
  85. if(line == endLine) endLine = index + 1;
  86. });
  87. }
  88. range = new monaco.Range(startLine, startCol, endLine, endCol);
  89. programmaticSelectionRange = range;
  90. editor.setSelection(range);
  91. editor.revealRangeInCenter(range);
  92. updateDecorations();
  93. }
  94. editor.onMouseDown(function(e)
  95. {
  96. if(range)
  97. {
  98. programmaticSelectionRange = range;
  99. updateDecorations();
  100. }
  101. });
  102. function updateDecorations()
  103. {
  104. editor.deltaDecorations(decorations, []);
  105. decorations = [];
  106. if(programmaticSelectionRange)
  107. {
  108. decorations.push({
  109. range: programmaticSelectionRange,
  110. options: {
  111. className: selectedClass,
  112. isWholeLine: false
  113. }
  114. });
  115. }
  116. decorations = editor.deltaDecorations([], decorations);
  117. }
  118. editor.onDidChangeCursorSelection(function(e)
  119. {
  120. if(!programmaticSelectionRange) return; // 只有当没有程序化选择时才更新装饰器
  121. if(e.selection.startLineNumber == programmaticSelectionRange.startLineNumber || e.selection.endLineNumber == programmaticSelectionRange.endLineNumber)
  122. {
  123. programmaticSelectionRange = null;
  124. updateDecorations();
  125. }
  126. });
  127. });
  128. }
  129. function resize(id)
  130. {
  131. var $ = window.$ == undefined ? parent.$ : window.$;
  132. var windowHeight = $(window).height();
  133. var headerHeight = parseInt($('#header').height());
  134. var mainNavbar = parseInt($('#mainNavbar').height());
  135. var mainMenuHeight = parseInt($('#mainMenu').css('padding-top')) + parseInt($('#mainMenu').css('padding-bottom'));
  136. var appTabsHeight = parseInt($('#appTabs').height());
  137. var appsBarHeight = parseInt($('#appsBar').height());
  138. var tabsHeight = parseInt($('#fileTabs .tabs-navbar').height());
  139. headerHeight = headerHeight ? headerHeight : 0;
  140. appsBarHeight = appsBarHeight ? appsBarHeight : 0;
  141. tabsHeight = tabsHeight ? tabsHeight : 0;
  142. appTabsHeight = appTabsHeight ? appTabsHeight : 0;
  143. mainMenuHeight = mainMenuHeight ? mainMenuHeight : 0;
  144. mainNavbar = mainNavbar ? mainNavbar : 0;
  145. var codeHeight = windowHeight - headerHeight - appsBarHeight - tabsHeight - appTabsHeight - mainMenuHeight - mainNavbar;
  146. if(codeHeight > 0) $.cookie.set(id + 'Height', codeHeight);
  147. $('#' + id).css('height', $.cookie.get(id + 'Height'));
  148. }