I am using codemirror to allow the user to type anycode like css/html/js.
I need to enable if the user type something like in css mode
div {
padding-
}
It should hint the user to chose the available options from the list like
div {
padding-top
padding-left
padding-right
padding-bottom
}
Something like sublime editor using codemirror. Please see the attached image for the demo of sublime auto hinting
Here is my code:
<script src="codemirror-5.4/mode/javascript/javascript.js"></script>
<script src="codemirror-5.4/mode/css/css.js"></script>
<script src="codemirror-5.4/mode/htmlmixed/htmlmixed.js"></script>
<script src="codemirror-5.4/addon/display/fullscreen.js"></script>
<script src="codemirror-5.4/keymap/sublime.js"></script>
<script src="codemirror-5.4/addon/hint/show-hint.js"></script>
<script src="codemirror-5.4/addon/hint/css-hint.js"></script>
<script src="codemirror-5.4/addon/hint/javascript.js"></script>
<h3>Editor</h3>
<div class="control-group">
<label class="control-label" for="textarea2">HTML</label>
<div class="controls">
<textarea class="code" name="code" id="codert" cols="40" rows="5" placeholder="Enter code here ..." style="width: 810px; height: 200px">
</textarea>
</div>
</div>
<div class="control-group">
<label class="control-label" for="textarea3">CSS</label>
<div class="controls">
<textarea id="code" class="code" name="codeCSS" cols="40" rows="5" placeholder="Enter code here ..." style="width: 810px; height: 200px">
</textarea>
</div>
</div>
<div class="control-group">
<label class="control-label" for="textarea3">javascript</label>
<div class="controls">
<textarea id="codeJS" class="code" name="codeJS" cols="40" rows="5" placeholder="Enter code here ..." style="width: 0px; height: 0px">
</textarea>
</div>
</div>
JavaScript code for codemirror
<script>
function loadCSS() {
var $head = $("#preview").contents().find("head");
$head.html("<style>" + editor.getValue() + "</style>");
};
function loadJS() {
var scriptTag = "<script>"+editorJS.getValue()+"<";
scriptTag += "/script>";
var previewFrame2 = document.getElementById('preview');
var preview2 = previewFrame2.contentDocument || previewFrame2.contentWindow.document;
preview2.open();
preview2.write(editor2.getValue()+scriptTag);
preview2.close();
loadCSS();
};
var delay;
// Initialize CodeMirror editor with a nice html5 canvas demo.
// css editor
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode: "text/x-scss",
keyMap: "sublime",
theme: 'monokai',
autoCloseTags: true,
lineWrapping: true,
extraKeys: {"Ctrl-Space": "autocomplete"}
});
editor.on("change", function() {
clearTimeout(delay);
delay = setTimeout(updatePreview, 0);
});
function updatePreview() {
loadCSS();
}
setTimeout(updatePreview, 0);
var delay2;
// Initialize CodeMirror editor with a nice html5 canvas demo.
var editor2 = CodeMirror.fromTextArea(document.getElementById('codert'), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode: "text/html",
keyMap: "sublime",
theme: 'monokai',
autoCloseTags: true,
lineWrapping: true,
extraKeys: {"Ctrl-Space": "autocomplete"}
});
editor2.on("change", function() {
clearTimeout(delay2);
delay2 = setTimeout(updatePreview2, 0);
});
function updatePreview2() {
var scriptTag = "<script>"+editorJS.getValue()+"<";
scriptTag += "/script>";
var previewFrame2 = document.getElementById('preview');
var preview2 = previewFrame2.contentDocument || previewFrame2.contentWindow.document;
preview2.open();
preview2.write(editor2.getValue()+scriptTag);
preview2.close();
loadCSS();
}
setTimeout(updatePreview2, 0);
var delayJS;
// Initialize CodeMirror editor with a nice html5 canvas demo.
var editorJS = CodeMirror.fromTextArea(document.getElementById('codeJS'), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode: 'javascript',
keyMap: "sublime",
theme: 'monokai',
autoCloseTags: true,
lineWrapping: true,
extraKeys: {"Ctrl-Space": "autocomplete"}
});
editorJS.on("change", function() {
clearTimeout(delayJS);
delayJS = setTimeout(updatePreviewJS, 0);
});
function updatePreviewJS() {
loadJS();
}
setTimeout(updatePreviewJS, 0);
</script>
Press ctrl-space to activate autocompletion. Built on top of the show-hint and javascript-hint addons. hinting function can also return one.
// create an instance var editor = CodeMirror. fromTextArea('code'); // store it $('#code'). data('CodeMirrorInstance', editor); // get it var myInstance = $('code'). data('CodeMirrorInstance'); // from here on the API functions are available to 'myInstance' again.
CodeMirror 6 is a complete rewrite with focus in accessibility and mobile support. Its API is not backwards compatible so we can't use any of the previous libraries.
This is how I enabled custom code hinting in my application's UI using CodeMirror 5
(it's basically a hodgepodge made out of several solutions I found online and from the source code at https://codemirror.net/demo/complete.html)
First of all I added CodeMirror and the code hint plugin
<script src="libs/codemirror-5/codemirror.min.js"></script>
<script src="libs/codemirror-5/plugins/show-hint.js"></script>
I overloaded (is that the right term?) the showHint
command, placed it in a function (note that because CM calls this function, it will inject the instance context as a usable argument).
function getSnippets(codemirror) {
let snippets = [
{text: 'SELECT', displayName: 'select'},
{text: 'UPDATE', displayName: 'update'},
{text: 'CREATE', displayName: 'create'},
// other snippets for hinting
]
CodeMirror.showHint(codemirror, function () {
let cursor = codemirror.getCursor();
let line = codemirror.getLine(cursor.line);
let start = cursor.ch, end = cursor.ch;
// corrects ignoring trailing whitespaces removal
while (start && /\w/.test(line.charAt(start - 1)))
--start;
while (end < line.length && /\w/.test(line.charAt(end)))
++end;
const token = codemirror.getTokenAt(cursor);
const currentWord = token.string;
// reduce hint options if user has already entered something
const list = snippets.filter(function (item) {
return item.displayText.indexOf(currentWord) >= 0;
});
return {
list: list.length ? list : snippets,
from: CodeMirror.Pos(cursor.line, start),
to: CodeMirror.Pos(cursor.line, end)
};
}, {completeSingle: true});
}
Then, inside the codemirror instance options I defined hint calling by pressing Ctrl+Space, this will call the getSnippets
function I defined earlier
let codemirrorOptions = {
mode: 'sql',
extraKeys: {
"Ctrl-Space": getSnippets
},
lineNumbers: false,
lineWrapping: true
};
You didn't post all the code so I could be wrong, but make sure to add the show-hint.css
stylesheet to the page header.
<link rel="stylesheet" href="../addon/hint/show-hint.css">
Otherwise, the hints just don't show up and I for one assumed the autocomplete function was not working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With