Skip to content

Instantly share code, notes, and snippets.

@dmitrig01
Created August 19, 2011 19:32
Show Gist options
  • Save dmitrig01/1157771 to your computer and use it in GitHub Desktop.
Save dmitrig01/1157771 to your computer and use it in GitHub Desktop.
diff --git a/assets/js/codemirror.carto.js b/assets/js/codemirror.carto.js
index d45c74e..98f32ed 100644
--- a/assets/js/codemirror.carto.js
+++ b/assets/js/codemirror.carto.js
@@ -111,6 +111,9 @@ CodeMirror.defineMode('carto', function(config, parserConfig) {
var context = state.stack[state.stack.length - 1];
if (type == 'hash' && context == 'rule') {
style = 'carto-colorcode';
+ if (parserConfig.onColor) {
+ parserConfig.onColor(stream.current());
+ }
} else if (style == 'carto-identifier') {
if (context == 'rule') {
style = (valid_keywords[stream.current()] || valid_colors[stream.current()]) ?
diff --git a/views/Project.bones b/views/Project.bones
index 4fd31a8..f4f874c 100644
--- a/views/Project.bones
+++ b/views/Project.bones
@@ -46,6 +46,7 @@ view.prototype.initialize = function() {
'exportList',
'statusOpen',
'statusClose',
+ 'colors',
'colorOpen',
'colorSave',
'colorClose',
@@ -149,7 +150,8 @@ view.prototype.makeStylesheet = function(model) {
tabMode: 'shift',
mode: {
name: 'carto',
- reference: window.abilities.carto
+ reference: window.abilities.carto,
+ onColor: this.colors
},
onCursorActivity: function() {
model.set({'data': model.codemirror.getValue()});
@@ -209,17 +212,7 @@ view.prototype.attach = function() {
$(this.map.controls.legend.element()).remove();
}
- // Rescan stylesheets for colors, dedupe, sort by luminosity
- // and render swatches for each one.
- this.$('.colors').empty();
- _(this.model.get('Stylesheet').pluck('data').join('\n')
- .match(/\#[A-Fa-f0-9]{6}\b|\#[A-Fa-f0-9]{3}\b|\b(rgb\s*\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*\)|rgba\s*\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*,\s*(0?\.)?\d+\s*\))/g) || []
- ).chain()
- .uniq(true)
- .each(_(function(color) {
- var swatch = templates.ProjectSwatch({color:color});
- this.$('.colors').append(swatch);
- }).bind(this));
+ this.colors();
};
view.prototype.change = function() {
@@ -536,3 +529,22 @@ view.prototype.unload = function(ev) {
return false;
};
+view.prototype.colorList = [];
+view.prototype.colors = function(color) {
+ if (color) {
+ this.colorList[color] = true;
+ }
+ // Rescan stylesheets for colors, dedupe, sort by luminosity
+ // and render swatches for each one.
+ this.$('.colors').empty();
+ _(this.model.get('Stylesheet').pluck('data').join('\n')
+ .match(/\#[A-Fa-f0-9]{6}\b|\#[A-Fa-f0-9]{3}\b|\b(rgb\s*\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*\)|rgba\s*\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*,\s*(0?\.)?\d+\s*\))/g) || []
+ ).chain()
+ .uniq(true)
+ .each(_(function(color) {
+ if (color[0] != '#' || this.colorList[color]) {
+ var swatch = templates.ProjectSwatch({ color: color});
+ this.$('.colors').append(swatch);
+ }
+ }).bind(this));
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment