Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeMirror - what is addWidget for and how to use it?

I want to make some extensions to CodeMirror. The addWidget method seems like a promising starting point. The documentation states

addWidget(pos, node, scrollIntoView) Puts node, which should be an absolutely positioned DOM node, into the editor, positioned right below the given {line, ch} position. When scrollIntoView is true, the editor will ensure that the entire node is visible (if possible). To remove the widget again, simply use DOM methods (move it somewhere else, or call removeChild on its parent).

I don't really understand what that means or what I would use it for. I cannot find a usage of it in the CodeMirror codebase nor anywhere else in google.

like image 847
George Mauer Avatar asked Jan 16 '13 19:01

George Mauer


People also ask

How do I use CodeMirror in textarea?

This could be used to, for example, replace a textarea with a real editor: var myCodeMirror = CodeMirror(function(elt) { myTextArea. parentNode. replaceChild(elt, myTextArea); }, {value: myTextArea.

How do I setup a code mirror?

Download CodeMirror files. Download jQuery file. Inside the codemirror project folder create subfolders and name them js, css and plugin. The js folder will hold all the javascript files.


1 Answers

You need to pass an html node and a position and a Boolean value

// create a node
var htmlNode =document.createElement("h1");
var text =  document.createTextNode("Text or whatever");
htmlNode.appendChild(text)

// call this after you initialized the editor.
// the position must be like this {ch: YourCharecterNumber, line: YourLineNumber}
editor.addWidget({ch:30 , line: 1},htmlNode, true)
like image 170
aljordan82 Avatar answered Sep 20 '22 16:09

aljordan82