Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit b2e1dc7

Browse files
committed
Merge pull request #2010 from adobe/rlim/color-picker
Adding inline color editor to core
2 parents 04a9096 + da5a685 commit b2e1dc7

14 files changed

+1063
-0
lines changed

src/extensions/default/InlineColorEditor/ColorEditor.js

Lines changed: 436 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
* DEALINGS IN THE SOFTWARE.
21+
*
22+
*/
23+
24+
/*jslint vars: true, plusplus: true, nomen: true, regexp: true, maxerr: 50 */
25+
/*global define, brackets, $, window */
26+
27+
define(function (require, exports, module) {
28+
"use strict";
29+
30+
var InlineWidget = brackets.getModule("editor/InlineWidget").InlineWidget,
31+
ColorEditor = require("ColorEditor").ColorEditor,
32+
InlineEditorTemplate = require("text!InlineColorEditorTemplate.html");
33+
34+
var MAX_USED_COLORS = 7;
35+
36+
function InlineColorEditor(color, startBookmark, endBookmark) {
37+
this.color = color;
38+
this.startBookmark = startBookmark;
39+
this.endBookmark = endBookmark;
40+
this.setColor = this.setColor.bind(this);
41+
42+
InlineWidget.call(this);
43+
}
44+
45+
InlineColorEditor.colorRegEx = /#[a-f0-9]{6}|#[a-f0-9]{3}|rgb\( ?\b([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b ?, ?\b([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b ?, ?\b([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b ?\)|rgba\( ?\b([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b ?, ?\b([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b ?, ?\b([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b ?, ?\b(1|0|0\.[0-9]{1,3}) ?\)|hsl\( ?\b([0-9]{1,2}|[12][0-9]{2}|3[0-5][0-9]|360)\b ?, ?\b([0-9]{1,2}|100)\b% ?, ?\b([0-9]{1,2}|100)\b% ?\)|hsla\( ?\b([0-9]{1,2}|[12][0-9]{2}|3[0-5][0-9]|360)\b ?, ?\b([0-9]{1,2}|100)\b% ?, ?\b([0-9]{1,2}|100)\b% ?, ?\b(1|0|0\.[0-9]{1,3}) ?\)/gi;
46+
47+
InlineColorEditor.prototype = new InlineWidget();
48+
InlineColorEditor.prototype.constructor = InlineColorEditor;
49+
InlineColorEditor.prototype.parentClass = InlineWidget.prototype;
50+
InlineColorEditor.prototype.$wrapperDiv = null;
51+
52+
InlineColorEditor.prototype.onClosed = function () {
53+
if (this.startBookmark) {
54+
this.startBookmark.clear();
55+
}
56+
if (this.endBookmark) {
57+
this.endBookmark.clear();
58+
}
59+
};
60+
61+
InlineColorEditor.prototype.setColor = function (colorLabel) {
62+
var start, end;
63+
if (!colorLabel) {
64+
return;
65+
}
66+
67+
if (colorLabel !== this.color) {
68+
start = this.startBookmark.find();
69+
if (!start) {
70+
return;
71+
}
72+
73+
end = this.endBookmark.find();
74+
if (!end || start.ch === end.ch) {
75+
end = { line: start.line,
76+
ch: start.ch + (this.color ? this.color.length : 0) };
77+
}
78+
79+
this.editor.document.replaceRange(colorLabel, start, end);
80+
this.editor.setSelection(start, {
81+
line: start.line,
82+
ch: start.ch + colorLabel.length
83+
});
84+
this.color = colorLabel;
85+
}
86+
};
87+
88+
InlineColorEditor.prototype.load = function (hostEditor) {
89+
var selectedColors = [];
90+
this.editor = hostEditor;
91+
this.parentClass.load.call(this, hostEditor);
92+
selectedColors = this.editor.document.getText().match(InlineColorEditor.colorRegEx);
93+
selectedColors = this.usedColors(selectedColors, MAX_USED_COLORS);
94+
this.$wrapperDiv = $(InlineEditorTemplate);
95+
this.colorEditor = new ColorEditor(this.$wrapperDiv, this.color, this.setColor, selectedColors);
96+
this.$htmlContent.append(this.$wrapperDiv);
97+
};
98+
99+
InlineColorEditor.prototype._editorHasFocus = function () {
100+
return true;
101+
};
102+
103+
InlineColorEditor.prototype.onAdded = function () {
104+
window.setTimeout(this._sizeEditorToContent.bind(this));
105+
this.colorEditor.focus();
106+
};
107+
108+
InlineColorEditor.prototype._sizeEditorToContent = function () {
109+
this.hostEditor.setInlineWidgetHeight(this, this.$wrapperDiv.outerHeight(), true);
110+
};
111+
112+
function _colorSort(a, b) {
113+
if (a.count === b.count) {
114+
return 0;
115+
}
116+
if (a.count > b.count) {
117+
return -1;
118+
}
119+
if (a.count < b.count) {
120+
return 1;
121+
}
122+
}
123+
124+
InlineColorEditor.prototype.usedColors = function (originalArray, maxLength) {
125+
var copyArray,
126+
compressed = [];
127+
128+
copyArray = $.map(originalArray, function (color) {
129+
return color.toLowerCase();
130+
});
131+
132+
compressed = $.map(originalArray, function (originalColor) {
133+
var a = {},
134+
lastIndex = 0,
135+
colorCount = 0,
136+
colorCopy = originalColor.toLowerCase();
137+
138+
do {
139+
lastIndex = copyArray.indexOf(colorCopy, lastIndex);
140+
if (lastIndex !== -1) {
141+
colorCount++;
142+
copyArray.splice(lastIndex, 1);
143+
}
144+
} while (lastIndex !== -1 && copyArray.length);
145+
146+
if (colorCount > 0) {
147+
a.value = originalColor;
148+
a.count = colorCount;
149+
return a;
150+
}
151+
}).sort(_colorSort);
152+
153+
return compressed.slice(0, maxLength);
154+
};
155+
156+
module.exports.InlineColorEditor = InlineColorEditor;
157+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
<div class="color_editor">
3+
<section>
4+
<div class="sliders">
5+
<div class="color_selection_field">
6+
<div class="saturation_gradient gradient_overlay"></div>
7+
<div class="luminosity_gradient gradient_overlay"></div>
8+
<div tabindex="0" class="selector_base">
9+
<div class="selector"></div>
10+
</div>
11+
</div>
12+
<div class="hue_slider slider">
13+
<div tabindex="0" class="selector_base">
14+
<div class="selector"></div>
15+
</div>
16+
</div>
17+
<div class="opacity_slider slider">
18+
<div class="opacity_gradient gradient_overlay"></div>
19+
<div tabindex="0" class="selector_base">
20+
<div class="selector"></div>
21+
</div>
22+
</div>
23+
</div>
24+
<footer>
25+
<input class="color_value" readonly="readonly" />
26+
<ul class="button-bar">
27+
<li class="selected"><a href="#" tabindex="0" class="rgba">RGBa</a></li>
28+
<li><a href="#" tabindex="0" class="hex">HEX</a></li>
29+
<li><a href="#" tabindex="0" class="hsla">HSLa</a></li>
30+
</ul>
31+
</footer>
32+
</section>
33+
<aside>
34+
<header>
35+
<div class="large_swatches">
36+
<div class="current_color large_swatch"></div>
37+
<div class="last_color large_swatch"></div>
38+
</div>
39+
</header>
40+
<ul class="swatches"></ul>
41+
</aside>
42+
</div>

0 commit comments

Comments
 (0)