Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ext-static_highlight.js 4.7KB

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