MCPcopy
hub / github.com/hokein/electron-sample-apps / CodeMirror

Function CodeMirror

mini-code-editor/cm/lib/codemirror.js:11–1913  ·  view source on GitHub ↗
(place, givenOptions)

Source from the content-addressed store, hash-verified

9 // This is the function that produces an editor instance. Its
10 // closure is used to store the editor state.
11 function CodeMirror(place, givenOptions) {
12 // Determine effective options based on given values and defaults.
13 var options = {}, defaults = CodeMirror.defaults;
14 for (var opt in defaults)
15 if (defaults.hasOwnProperty(opt))
16 options[opt] = (givenOptions && givenOptions.hasOwnProperty(opt) ? givenOptions : defaults)[opt];
17
18 // The element in which the editor lives.
19 var wrapper = document.createElement("div");
20 wrapper.className = "CodeMirror" + (options.lineWrapping ? " CodeMirror-wrap" : "");
21 // This mess creates the base DOM structure for the editor.
22 wrapper.innerHTML =
23 '<div style="overflow: hidden; position: relative; width: 3px; height: 0px;">' + // Wraps and hides input textarea
24 '<textarea style="position: absolute; padding: 0; width: 1px; height: 1em" wrap="off" ' +
25 'autocorrect="off" autocapitalize="off"></textarea></div>' +
26 '<div class="CodeMirror-scroll" tabindex="-1">' +
27 '<div style="position: relative">' + // Set to the height of the text, causes scrolling
28 '<div style="position: relative">' + // Moved around its parent to cover visible view
29 '<div class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div>' +
30 // Provides positioning relative to (visible) text origin
31 '<div class="CodeMirror-lines"><div style="position: relative; z-index: 0">' +
32 '<div style="position: absolute; width: 100%; height: 0; overflow: hidden; visibility: hidden;"></div>' +
33 '<pre class="CodeMirror-cursor">&#160;</pre>' + // Absolutely positioned blinky cursor
34 '<div style="position: relative; z-index: -1"></div><div></div>' + // DIVs containing the selection and the actual code
35 '</div></div></div></div></div>';
36 if (place.appendChild) place.appendChild(wrapper); else place(wrapper);
37 // I've never seen more elegant code in my life.
38 var inputDiv = wrapper.firstChild, input = inputDiv.firstChild,
39 scroller = wrapper.lastChild, code = scroller.firstChild,
40 mover = code.firstChild, gutter = mover.firstChild, gutterText = gutter.firstChild,
41 lineSpace = gutter.nextSibling.firstChild, measure = lineSpace.firstChild,
42 cursor = measure.nextSibling, selectionDiv = cursor.nextSibling,
43 lineDiv = selectionDiv.nextSibling;
44 themeChanged(); keyMapChanged();
45 // Needed to hide big blue blinking cursor on Mobile Safari
46 if (ios) input.style.width = "0px";
47 if (!webkit) lineSpace.draggable = true;
48 lineSpace.style.outline = "none";
49 if (options.tabindex != null) input.tabIndex = options.tabindex;
50 if (options.autofocus) focusInput();
51 if (!options.gutter && !options.lineNumbers) gutter.style.display = "none";
52 // Needed to handle Tab key in KHTML
53 if (khtml) inputDiv.style.height = "1px", inputDiv.style.position = "absolute";
54
55 // Check for problem with IE innerHTML not working when we have a
56 // P (or similar) parent node.
57 try { stringWidth("x"); }
58 catch (e) {
59 if (e.message.match(/runtime/i))
60 e = new Error("A CodeMirror inside a P-style element does not work in Internet Explorer. (innerHTML bug)");
61 throw e;
62 }
63
64 // Delayed object wrap timeouts, making sure only one is active. blinker holds an interval.
65 var poll = new Delayed(), highlight = new Delayed(), blinker;
66
67 // mode holds a mode API object. doc is the tree of Line objects,
68 // work an array of lines that should be parsed, and history the

Callers 4

editor.jsFile · 0.85
codemirror.jsFile · 0.85
test.jsFile · 0.85
testCMFunction · 0.85

Calls 15

themeChangedFunction · 0.85
keyMapChangedFunction · 0.85
focusInputFunction · 0.85
stringWidthFunction · 0.85
loadModeFunction · 0.85
operationFunction · 0.85
setValueFunction · 0.85
connectFunction · 0.85
updateDisplayFunction · 0.85
fastPollFunction · 0.85
replaceSelectionFunction · 0.85
onBlurFunction · 0.85

Tested by

no test coverage detected