Skip to content

Commit

Permalink
fixed #13
Browse files Browse the repository at this point in the history
  • Loading branch information
easylogic committed Aug 23, 2017
1 parent 31c15e0 commit 42215e3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
26 changes: 25 additions & 1 deletion addon/colorpicker/colorview.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
var colorpicker_class = 'codemirror-colorview';
var colorpicker_background_class = 'codemirror-colorview-background';

// Excluded tokens do not show color views..
var excluded_token = ['comment'];

CodeMirror.defineOption("colorpicker", false, function (cm, val, old) {

if (old && old != CodeMirror.Init) {
Expand Down Expand Up @@ -113,6 +116,9 @@
this.opt = opt;
this.cm = cm;
this.markers = {};

// set excluded token
excluded_token = this.opt.excluded_token || excluded_token;

if (this.cm.colorpicker) {
this.colorpicker = this.cm.colorpicker();
Expand Down Expand Up @@ -295,7 +301,7 @@
}
}

codemirror_colorpicker.prototype.color_regexp = /(#(?:[\da-f]{3}){1,2}|rgb\((?:\s*\d{1,3},\s*){2}\d{1,3}\s*\)|rgba\((?:\s*\d{1,3},\s*){3}\d*\.?\d+\s*\)|hsl\(\s*\d{1,3}(?:,\s*\d{1,3}%){2}\s*\)|hsla\(\s*\d{1,3}(?:,\s*\d{1,3}%){2},\s*\d*\.?\d+\s*\)|(\w+))/gi;
codemirror_colorpicker.prototype.color_regexp = /(#(?:[\da-f]{3}){1,2}|rgb\((?:\s*\d{1,3},\s*){2}\d{1,3}\s*\)|rgba\((?:\s*\d{1,3},\s*){3}\d*\.?\d+\s*\)|hsl\(\s*\d{1,3}(?:,\s*\d{1,3}%){2}\s*\)|hsla\(\s*\d{1,3}(?:,\s*\d{1,3}%){2},\s*\d*\.?\d+\s*\)|([\w_\-]+))/gi;

codemirror_colorpicker.prototype.match_result = function (lineHandle) {
return lineHandle.text.match(this.color_regexp);
Expand Down Expand Up @@ -389,9 +395,27 @@

}

codemirror_colorpicker.prototype.is_excluded_token = function (line, ch) {
var token = this.cm.getTokenAt({line : line, ch : ch});
var count = 0;
for(var i = 0, len = excluded_token.length; i < len; i++) {
if (token.type === excluded_token[i]) {
count++;
break;
}
}

return count > 0; // true is that it has a excluded token
}

codemirror_colorpicker.prototype.render = function (cursor, lineNo, lineHandle, color, nameColor) {
var start = lineHandle.text.indexOf(color, cursor.next);

if (this.is_excluded_token(lineNo, start) === true) {
// excluded token do not show.
return;
}

cursor.next = start + color.length;

if (this.has_marker(lineNo, start))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codemirror-colorpicker",
"version": "1.0.8",
"version": "1.0.9",
"description": "colorpicker for codemirror",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 9 additions & 3 deletions sample/colorpicker.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/codemirror.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/codemirror.css" />

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/codemirror.js" ></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/mode/css/css.js" ></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/codemirror.js" ></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/mode/css/css.js" ></script>
<link rel="stylesheet" href="../addon/colorpicker/colorpicker.css" />
<script type="text/javascript" src="../addon/colorpicker/colorpicker.js" ></script>
<script type="text/javascript" src="../addon/colorpicker/colorview.js" ></script>
Expand All @@ -14,6 +14,12 @@
<body>

<textarea id="sample_text_area">
.sample-green {
background-color: green;
blue: blue;
}

/* background-color: yellow; */
var color_names = {
aliceblue: "rgb(240, 248, 255)",
antiquewhite: "rgb(250, 235, 215)",
Expand Down

0 comments on commit 42215e3

Please sign in to comment.