Projekt

Obecné

Profil

Stáhnout (6.87 KB) Statistiky
| Větev: | Tag: | Revize:
1
ace.define("ace/ext/static_highlight",["require","exports","module","ace/edit_session","ace/layer/text","ace/config","ace/lib/dom","ace/lib/lang"], function(require, exports, module) {
2
"use strict";
3

    
4
var EditSession = require("../edit_session").EditSession;
5
var TextLayer = require("../layer/text").Text;
6
var baseStyles = ".ace_static_highlight {\
7
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', 'Droid Sans Mono', monospace;\
8
font-size: 12px;\
9
white-space: pre-wrap\
10
}\
11
.ace_static_highlight .ace_gutter {\
12
width: 2em;\
13
text-align: right;\
14
padding: 0 3px 0 0;\
15
margin-right: 3px;\
16
contain: none;\
17
}\
18
.ace_static_highlight.ace_show_gutter .ace_line {\
19
padding-left: 2.6em;\
20
}\
21
.ace_static_highlight .ace_line { position: relative; }\
22
.ace_static_highlight .ace_gutter-cell {\
23
-moz-user-select: -moz-none;\
24
-khtml-user-select: none;\
25
-webkit-user-select: none;\
26
user-select: none;\
27
top: 0;\
28
bottom: 0;\
29
left: 0;\
30
position: absolute;\
31
}\
32
.ace_static_highlight .ace_gutter-cell:before {\
33
content: counter(ace_line, decimal);\
34
counter-increment: ace_line;\
35
}\
36
.ace_static_highlight {\
37
counter-reset: ace_line;\
38
}\
39
";
40
var config = require("../config");
41
var dom = require("../lib/dom");
42
var escapeHTML = require("../lib/lang").escapeHTML;
43

    
44
function Element(type) {
45
    this.type = type;
46
    this.style = {};
47
    this.textContent = "";
48
}
49
Element.prototype.cloneNode = function() {
50
    return this;
51
};
52
Element.prototype.appendChild = function(child) {
53
    this.textContent += child.toString();
54
};
55
Element.prototype.toString = function() {
56
    var stringBuilder = [];
57
    if (this.type != "fragment") {
58
        stringBuilder.push("<", this.type);
59
        if (this.className)
60
            stringBuilder.push(" class='", this.className, "'");
61
        var styleStr = [];
62
        for (var key in this.style) {
63
            styleStr.push(key, ":", this.style[key]);
64
        }
65
        if (styleStr.length)
66
            stringBuilder.push(" style='", styleStr.join(""), "'");
67
        stringBuilder.push(">");
68
    }
69

    
70
    if (this.textContent) {
71
        stringBuilder.push(this.textContent);
72
    }
73

    
74
    if (this.type != "fragment") {
75
        stringBuilder.push("</", this.type, ">");
76
    }
77
    
78
    return stringBuilder.join("");
79
};
80

    
81

    
82
var simpleDom = {
83
    createTextNode: function(textContent, element) {
84
        return escapeHTML(textContent);
85
    },
86
    createElement: function(type) {
87
        return new Element(type);
88
    },
89
    createFragment: function() {
90
        return new Element("fragment");
91
    }
92
};
93

    
94
var SimpleTextLayer = function() {
95
    this.config = {};
96
    this.dom = simpleDom;
97
};
98
SimpleTextLayer.prototype = TextLayer.prototype;
99

    
100
var highlight = function(el, opts, callback) {
101
    var m = el.className.match(/lang-(\w+)/);
102
    var mode = opts.mode || m && ("ace/mode/" + m[1]);
103
    if (!mode)
104
        return false;
105
    var theme = opts.theme || "ace/theme/textmate";
106
    
107
    var data = "";
108
    var nodes = [];
109

    
110
    if (el.firstElementChild) {
111
        var textLen = 0;
112
        for (var i = 0; i < el.childNodes.length; i++) {
113
            var ch = el.childNodes[i];
114
            if (ch.nodeType == 3) {
115
                textLen += ch.data.length;
116
                data += ch.data;
117
            } else {
118
                nodes.push(textLen, ch);
119
            }
120
        }
121
    } else {
122
        data = el.textContent;
123
        if (opts.trim)
124
            data = data.trim();
125
    }
126
    
127
    highlight.render(data, mode, theme, opts.firstLineNumber, !opts.showGutter, function (highlighted) {
128
        dom.importCssString(highlighted.css, "ace_highlight");
129
        el.innerHTML = highlighted.html;
130
        var container = el.firstChild.firstChild;
131
        for (var i = 0; i < nodes.length; i += 2) {
132
            var pos = highlighted.session.doc.indexToPosition(nodes[i]);
133
            var node = nodes[i + 1];
134
            var lineEl = container.children[pos.row];
135
            lineEl && lineEl.appendChild(node);
136
        }
137
        callback && callback();
138
    });
139
};
140
highlight.render = function(input, mode, theme, lineStart, disableGutter, callback) {
141
    var waiting = 1;
142
    var modeCache = EditSession.prototype.$modes;
143
    if (typeof theme == "string") {
144
        waiting++;
145
        config.loadModule(['theme', theme], function(m) {
146
            theme = m;
147
            --waiting || done();
148
        });
149
    }
150
    var modeOptions;
151
    if (mode && typeof mode === "object" && !mode.getTokenizer) {
152
        modeOptions = mode;
153
        mode = modeOptions.path;
154
    }
155
    if (typeof mode == "string") {
156
        waiting++;
157
        config.loadModule(['mode', mode], function(m) {
158
            if (!modeCache[mode] || modeOptions)
159
                modeCache[mode] = new m.Mode(modeOptions);
160
            mode = modeCache[mode];
161
            --waiting || done();
162
        });
163
    }
164
    function done() {
165
        var result = highlight.renderSync(input, mode, theme, lineStart, disableGutter);
166
        return callback ? callback(result) : result;
167
    }
168
    return --waiting || done();
169
};
170
highlight.renderSync = function(input, mode, theme, lineStart, disableGutter) {
171
    lineStart = parseInt(lineStart || 1, 10);
172

    
173
    var session = new EditSession("");
174
    session.setUseWorker(false);
175
    session.setMode(mode);
176

    
177
    var textLayer = new SimpleTextLayer();
178
    textLayer.setSession(session);
179
    Object.keys(textLayer.$tabStrings).forEach(function(k) {
180
        if (typeof textLayer.$tabStrings[k] == "string") {
181
            var el = simpleDom.createFragment();
182
            el.textContent = textLayer.$tabStrings[k];
183
            textLayer.$tabStrings[k] = el;
184
        }
185
    });
186

    
187
    session.setValue(input);
188
    var length =  session.getLength();
189
    
190
    var outerEl = simpleDom.createElement("div");
191
    outerEl.className = theme.cssClass;
192
    
193
    var innerEl = simpleDom.createElement("div");
194
    innerEl.className = "ace_static_highlight" + (disableGutter ? "" : " ace_show_gutter");
195
    innerEl.style["counter-reset"] = "ace_line " + (lineStart - 1);
196

    
197
    for (var ix = 0; ix < length; ix++) {
198
        var lineEl = simpleDom.createElement("div");
199
        lineEl.className = "ace_line";
200
        
201
        if (!disableGutter) {
202
            var gutterEl = simpleDom.createElement("span");
203
            gutterEl.className ="ace_gutter ace_gutter-cell";
204
            gutterEl.textContent = "";
205
            lineEl.appendChild(gutterEl);
206
        }
207
        textLayer.$renderLine(lineEl, ix, false);
208
        lineEl.textContent += "\n";
209
        innerEl.appendChild(lineEl);
210
    }
211
    outerEl.appendChild(innerEl);
212

    
213
    return {
214
        css: baseStyles + theme.cssText,
215
        html: outerEl.toString(),
216
        session: session
217
    };
218
};
219

    
220
module.exports = highlight;
221
module.exports.highlight = highlight;
222
});                (function() {
223
                    ace.require(["ace/ext/static_highlight"], function(m) {
224
                        if (typeof module == "object" && typeof exports == "object" && module) {
225
                            module.exports = m;
226
                        }
227
                    });
228
                })();
229
            
(18-18/244)