ext-static_highlight.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ace.define("ace/ext/static_highlight",["require","exports","module","ace/edit_session","ace/layer/text","ace/config","ace/lib/dom"], function(require, exports, module) {
  2. "use strict";
  3. var EditSession = require("../edit_session").EditSession;
  4. var TextLayer = require("../layer/text").Text;
  5. var baseStyles = ".ace_static_highlight {\
  6. font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', 'Droid Sans Mono', monospace;\
  7. font-size: 12px;\
  8. }\
  9. .ace_static_highlight .ace_gutter {\
  10. width: 25px !important;\
  11. display: block;\
  12. float: left;\
  13. text-align: right;\
  14. padding: 0 3px 0 0;\
  15. margin-right: 3px;\
  16. position: static !important;\
  17. }\
  18. .ace_static_highlight .ace_line { clear: both; }\
  19. .ace_static_highlight .ace_gutter-cell {\
  20. -moz-user-select: -moz-none;\
  21. -khtml-user-select: none;\
  22. -webkit-user-select: none;\
  23. user-select: none;\
  24. }\
  25. .ace_static_highlight .ace_gutter-cell:before {\
  26. content: counter(ace_line, decimal);\
  27. counter-increment: ace_line;\
  28. }\
  29. .ace_static_highlight {\
  30. counter-reset: ace_line;\
  31. }\
  32. ";
  33. var config = require("../config");
  34. var dom = require("../lib/dom");
  35. var highlight = function(el, opts, callback) {
  36. var m = el.className.match(/lang-(\w+)/);
  37. var mode = opts.mode || m && ("ace/mode/" + m[1]);
  38. if (!mode)
  39. return false;
  40. var theme = opts.theme || "ace/theme/textmate";
  41. var data = "";
  42. var nodes = [];
  43. if (el.firstElementChild) {
  44. var textLen = 0;
  45. for (var i = 0; i < el.childNodes.length; i++) {
  46. var ch = el.childNodes[i];
  47. if (ch.nodeType == 3) {
  48. textLen += ch.data.length;
  49. data += ch.data;
  50. } else {
  51. nodes.push(textLen, ch);
  52. }
  53. }
  54. } else {
  55. data = dom.getInnerText(el);
  56. if (opts.trim)
  57. data = data.trim();
  58. }
  59. highlight.render(data, mode, theme, opts.firstLineNumber, !opts.showGutter, function (highlighted) {
  60. dom.importCssString(highlighted.css, "ace_highlight");
  61. el.innerHTML = highlighted.html;
  62. var container = el.firstChild.firstChild;
  63. for (var i = 0; i < nodes.length; i += 2) {
  64. var pos = highlighted.session.doc.indexToPosition(nodes[i]);
  65. var node = nodes[i + 1];
  66. var lineEl = container.children[pos.row];
  67. lineEl && lineEl.appendChild(node);
  68. }
  69. callback && callback();
  70. });
  71. };
  72. highlight.render = function(input, mode, theme, lineStart, disableGutter, callback) {
  73. var waiting = 1;
  74. var modeCache = EditSession.prototype.$modes;
  75. if (typeof theme == "string") {
  76. waiting++;
  77. config.loadModule(['theme', theme], function(m) {
  78. theme = m;
  79. --waiting || done();
  80. });
  81. }
  82. if (typeof mode == "string") {
  83. waiting++;
  84. config.loadModule(['mode', mode], function(m) {
  85. if (!modeCache[mode]) modeCache[mode] = new m.Mode();
  86. mode = modeCache[mode];
  87. --waiting || done();
  88. });
  89. }
  90. function done() {
  91. var result = highlight.renderSync(input, mode, theme, lineStart, disableGutter);
  92. return callback ? callback(result) : result;
  93. }
  94. return --waiting || done();
  95. };
  96. highlight.renderSync = function(input, mode, theme, lineStart, disableGutter) {
  97. lineStart = parseInt(lineStart || 1, 10);
  98. var session = new EditSession("");
  99. session.setUseWorker(false);
  100. session.setMode(mode);
  101. var textLayer = new TextLayer(document.createElement("div"));
  102. textLayer.setSession(session);
  103. textLayer.config = {
  104. characterWidth: 10,
  105. lineHeight: 20
  106. };
  107. session.setValue(input);
  108. var stringBuilder = [];
  109. var length = session.getLength();
  110. for(var ix = 0; ix < length; ix++) {
  111. stringBuilder.push("<div class='ace_line'>");
  112. if (!disableGutter)
  113. stringBuilder.push("<span class='ace_gutter ace_gutter-cell' unselectable='on'>" + /*(ix + lineStart) + */ "</span>");
  114. textLayer.$renderLine(stringBuilder, ix, true, false);
  115. stringBuilder.push("\n</div>");
  116. }
  117. var html = "<div class='" + theme.cssClass + "'>" +
  118. "<div class='ace_static_highlight' style='counter-reset:ace_line " + (lineStart - 1) + "'>" +
  119. stringBuilder.join("") +
  120. "</div>" +
  121. "</div>";
  122. textLayer.destroy();
  123. return {
  124. css: baseStyles + theme.cssText,
  125. html: html,
  126. session: session
  127. };
  128. };
  129. module.exports = highlight;
  130. module.exports.highlight =highlight;
  131. });
  132. ;
  133. (function() {
  134. ace.require(["ace/ext/static_highlight"], function() {});
  135. })();