Skip to content

Commit 76688c1

Browse files
author
Philipp
committed
#1 Closed, Ready for 1.2.0
1 parent 7b427b0 commit 76688c1

File tree

4 files changed

+96
-17
lines changed

4 files changed

+96
-17
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ In order to make CodeMirror work correctly, do not forget to start your HTML pag
1212

1313
##### Initialize
1414

15-
`options` can be `null` or contain original CodeMirror settings
15+
`codemirrorOptions` can be `null` or contain original CodeMirror settings
16+
17+
`jqueryCodemirroOptions` can be `null` (ignored)
1618

1719
```
18-
$(...).codemirrorInit(options);
20+
$(...).codemirrorInit(codemirrorOptions, jqueryCodemirrorOptions);
1921
```
2022

21-
`defaults` are:
23+
`codemirrorDefaults` are:
2224

2325
```
2426
mode: "text/html",
@@ -28,10 +30,29 @@ lineWrapping: true
2830

2931
`NB!` Do not forget to add required mode `mode/xml/xml.js`
3032

31-
You can set configs in DOM element attribute `codemirror-config`
33+
`jqueryCodemirrorDefaults` are:
34+
35+
```
36+
height: "auto"; (Set in CSS)
37+
```
38+
39+
Height options are `auto` and `inherit`
40+
41+
When `auto`, CodeMirror container is auto resized. (Container's CSS parameter `height` value should be `auto`! May have `min-height`, should not ave `max-height`)
42+
43+
When `inherit`, CodeMirror container is not resized, scroll-bar appears. (Container's CSS `height` value should not be `auto`! )
44+
45+
46+
You can set CodeMirror configs in DOM element attribute `codemirror-config` or `codemirror`. Latest is preferred.
47+
48+
```
49+
<div codemirror='{"autofocus": true, "value": "Hello world"}'></div>
50+
```
51+
52+
You can set plugin configs in DOM element attribute `jquery-codemirror-config` or `jquery-codemirror`. Latest is preferred.
3253

3354
```
34-
<div codemirror-config='{"autofocus": true, "value": "Hello world"}'></div>
55+
<div jquery-codemirror='{"height": "inherit"}'></div>
3556
```
3657

3758
##### CodeMirror methods

index.html

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@
2222
.code-snippet-group-2 {
2323
margin: 10px;
2424
width: 802px;
25+
height: 150px;
26+
border: 1px solid black;
27+
}
28+
29+
.code-snippet-group-2 {
30+
margin: 10px;
31+
width: 802px;
32+
height: 150px;
33+
border: 1px solid black;
34+
}
35+
36+
.code-snippet-group-3 {
37+
margin: 10px;
38+
width: 802px;
39+
height: 150px;
2540
border: 1px solid black;
2641
}
2742

@@ -35,14 +50,18 @@
3550

3651
<h3>'philsweb.jquery-codemirror' Demo</h3>
3752

38-
<div class="code-snippet-group-1" first style="min-height:50px;"></div>
53+
<div class="code-snippet-group-1" first style="min-height:100px;"></div>
3954

4055
<div class="code-snippet-group-1" second
4156
codemirror-config='{"cursorHeight": ".5", "autofocus": true, "value": " Hello world"}'></div>
4257

43-
<div class="code-snippet-group-1" third></div>
58+
<div class="code-snippet-group-1" third jquery-codemirror-config='{"height":"inherit"}' style="height:150px;"></div>
59+
4460

45-
<div class="code-snippet-group-2" codemirror-config='{"firstLineNumber": -5}'></div>
61+
<div class="code-snippet-group-2" jquery-codemirror='{"height":"inherit"}' codemirror='{"firstLineNumber": -5}'></div>
62+
63+
64+
<div class="code-snippet-group-3"></div>
4665

4766
</body>
4867

@@ -72,5 +91,7 @@ <h3>'philsweb.jquery-codemirror' Demo</h3>
7291

7392

7493
$('.code-snippet-group-2').codemirrorInit({value: "<div> Hello world </div>\n\n\n"});
94+
95+
$('.code-snippet-group-3').codemirrorInit({value: "<div> Hello world </div>\n\n\n"}, {height: "inherit"});
7596
});
7697
</script>

philsweb.codemirror.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
.codemirror-buttons-wrapper {
66
position: relative;
7-
z-index: 9999; /* @todo Should be configured */
7+
z-index: 9999;
88
display: none;
99
}
1010

philsweb.jquery-codemirror.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@license Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
88
*/
99
(function ($) {
10-
$.fn.codemirrorInit = function (options) {
10+
$.fn.codemirrorInit = function (codemirrorOptions, jqueryCodemirrorOptions) {
1111

1212
$(this).init.prototype = $.extend({}, $(this).init.prototype, {
1313

@@ -41,15 +41,30 @@
4141
}
4242
});
4343

44-
var defaults = {
44+
// Defaults
45+
46+
var codemirrorDefaults = {
4547
mode: "text/html",
4648
lineNumbers: true,
4749
lineWrapping: true
4850
};
4951

50-
function getLocalConfigs(element) {
51-
var configJson = $(element).attr('codemirror-config');
5252

53+
// Private methods
54+
55+
function getConfigAttrTitle(element, preferredTitle) {
56+
if ($(element).is("[" + preferredTitle + "]")) {
57+
return preferredTitle;
58+
}
59+
60+
if ($(element).is("[" + preferredTitle + "-config" + "]")) {
61+
return preferredTitle + "-config";
62+
}
63+
64+
return null;
65+
}
66+
67+
function parseJsonConfig(configJson) {
5368
if (configJson === undefined) {
5469
return {};
5570
}
@@ -67,7 +82,24 @@
6782
}
6883

6984
return {};
85+
}
86+
87+
function getCodemirrorElementConfigs(element) {
7088

89+
var codemirrorConfigAttrTitle = getConfigAttrTitle(element, 'codemirror');
90+
91+
var configJson = (codemirrorConfigAttrTitle === null) ? undefined : $(element).attr(codemirrorConfigAttrTitle);
92+
93+
return parseJsonConfig(configJson);
94+
}
95+
96+
function getJqueryCodemirrorElementConfigs(element) {
97+
98+
var codemirrorConfigAttrTitle = getConfigAttrTitle(element, 'jquery-codemirror');
99+
100+
var configJson = (codemirrorConfigAttrTitle === null) ? undefined : $(element).attr(codemirrorConfigAttrTitle);
101+
102+
return parseJsonConfig(configJson);
71103
}
72104

73105
function copyToClipboard(value) {
@@ -79,13 +111,18 @@
79111
$temp.remove();
80112
}
81113

114+
// Element iteration
115+
82116
return this.each(function () {
83117

84-
var basicConfigs = $.extend({}, defaults, options);
85-
var localConfigs = getLocalConfigs(this);
86-
var configs = $.extend({}, basicConfigs, localConfigs);
118+
var basicCodemirrorConfigs = $.extend({}, codemirrorDefaults, codemirrorOptions);
119+
var elementCodemirrorConfigs = getCodemirrorElementConfigs(this);
120+
var codemirrorConfigs = $.extend({}, basicCodemirrorConfigs, elementCodemirrorConfigs);
121+
122+
$.data(this, 'codemirror', CodeMirror(this, codemirrorConfigs));
87123

88-
$.data(this, 'codemirror', CodeMirror(this, configs));
124+
var jqueryCodemirrorConfigs = $.extend({}, jqueryCodemirrorOptions, getJqueryCodemirrorElementConfigs(this));
125+
$(this).children('.CodeMirror').css(jqueryCodemirrorConfigs);
89126

90127
// Buttons
91128
var buttonsWrapper = $('<div>');

0 commit comments

Comments
 (0)