Skip to content

Commit 156aa8f

Browse files
author
Philipp
committed
Basic implementation
1 parent e70e8ca commit 156aa8f

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
.idea/*
2-
bower_components/*
1+
.idea
2+
bower_components

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"tests"
2222
],
2323
"dependencies": {
24-
"jquery": "^3.3.1"
24+
"jquery": "^3.3.1",
25+
"codemirror": "^5.35.0"
2526
}
2627
}

index.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<script src="bower_components/jquery/dist/jquery.min.js"></script>
2+
3+
<script src="bower_components/codemirror/lib/codemirror.js"></script>
4+
<link rel="stylesheet" href="bower_components/codemirror/lib/codemirror.css">
5+
<script src="bower_components/codemirror/mode/xml/xml.js"></script>
6+
7+
<script src="philsweb.jquery.codemirror.js"></script>
8+
9+
<div class="margin10 code-snippet" data-test style="width:250px;height:300px;border:1px solid black;"></div>
10+
11+
<div class="margin10 code-snippet" style="width:350px;height:150px;border:1px solid black;"></div>
12+
13+
<div class="margin10 code-snippet" data-no-lines style="width:500px;height:100px;border:1px solid black;"></div>
14+
15+
<style>
16+
.margin10 {
17+
margin: 10px;
18+
}
19+
20+
.CodeMirror {
21+
height: inherit;
22+
}
23+
</style>
24+
25+
<script>
26+
$(function () {
27+
28+
$('.code-snippet').codemirror();
29+
30+
$('.code-snippet[data-test]').setValue('Any string');
31+
32+
console.log($('.code-snippet[data-test]').getValue());
33+
34+
var elements = $('.code-snippet');
35+
36+
var increment = 1;
37+
38+
elements.each(function (key, element) {
39+
40+
$.data(element, 'codemirror').setValue('Any' + increment++);
41+
$(element).setValue('New' + increment++);
42+
43+
});
44+
45+
$('.code-snippet[data-no-lines]').setOption('lineNumbers', false);
46+
47+
$('.code-snippet[data-no-lines]').setOptions({lineNumbers: true, readOnly: true});
48+
49+
$.data($('.code-snippet[data-no-lines]')[0], 'codemirror').focus();
50+
});
51+
</script>

philsweb.jquery.codemirror.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
(function ($) {
2-
$.fn.codemirror = function (options) {
2+
$.fn.codemirror = function () {
33

4+
$(this).init.prototype = $.extend({}, $(this).init.prototype, {
5+
6+
setValue: function (value) {
7+
for (var i = 0; i < this.length; i++) {
8+
$.data(this[i], 'codemirror').setValue(value);
9+
}
10+
},
11+
getValue: function () {
12+
if (this.length > 1) {
13+
throw new Error("Only one DOM element can be selected");
14+
}
15+
return $.data(this[0], 'codemirror').getValue();
16+
},
17+
setOption: function (key, value) {
18+
for (var i = 0; i < this.length; i++) {
19+
$.data(this[i], 'codemirror').setOption(key, value);
20+
}
21+
},
22+
setOptions: function (optionsObject) {
23+
for (var i = 0; i < this.length; i++) {
24+
var $this = this[i];
25+
$.each(optionsObject, function (key, value) {
26+
$.data($this, 'codemirror').setOption(key, value);
27+
});
28+
}
29+
}
30+
31+
});
32+
33+
return this.each(function () {
34+
35+
$.data(this, 'codemirror', CodeMirror(this, {
36+
//value: '<div> Hello world! </div>',
37+
mode: "text/html",
38+
lineNumbers: true,
39+
lineWrapping: true
40+
}));
41+
});
442
};
43+
544
})(jQuery);

0 commit comments

Comments
 (0)