Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a7c2d19
Adding JSLint passed inline color editor
RaymondLim Oct 31, 2012
1d7e011
Merge branch 'master', remote-tracking branch 'origin' into rlim/colo…
RaymondLim Oct 31, 2012
ebc3e55
Code clean-up and other changes suggested in code review feedbacks.
RaymondLim Nov 3, 2012
394cbc8
Some more clean up of "return" keyword.
RaymondLim Nov 3, 2012
9ffeee6
Adding MIT license to one more file.
RaymondLim Nov 3, 2012
4e134ec
Missed to add these two in previous commit.
RaymondLim Nov 5, 2012
1af1631
Re-commiting these four.
RaymondLim Nov 5, 2012
f576ca3
One more "return" keyword removal that I missed.
RaymondLim Nov 5, 2012
9eb433c
Another "return" keyword removal.
RaymondLim Nov 5, 2012
af8631e
Remove the parameter that I added into onClosed function. We can just…
RaymondLim Nov 5, 2012
b0c032c
Fix one JSLint error I introduced in last commit.
RaymondLim Nov 5, 2012
1e546bd
Prevent clicks on some UI elements (color selection field, slider and…
RaymondLim Nov 6, 2012
5560b96
Use bookmarks to update color values in main editor to fix issue #2048
RaymondLim Nov 8, 2012
6ffd6e3
Limit existing color swatches to be shown as the number defined in MA…
RaymondLim Nov 12, 2012
f03b3c2
Need to get the color picker first before we remove the bookmark. Oth…
RaymondLim Nov 12, 2012
8b547b8
Fix the console error that I introduced in previous commit. We should…
RaymondLim Nov 12, 2012
38b0a7e
Another round of changes for review comments.
RaymondLim Nov 14, 2012
d01c931
Remove a "return" keyword I introduced with my new code for tab handl…
RaymondLim Nov 14, 2012
cd60e1f
Simplify the jQuery calls and selectors to find the last color swatch.
RaymondLim Nov 14, 2012
da5a685
Fix JSLint errors.
RaymondLim Nov 15, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
436 changes: 436 additions & 0 deletions src/extensions/default/InlineColorEditor/ColorEditor.js

Large diffs are not rendered by default.

157 changes: 157 additions & 0 deletions src/extensions/default/InlineColorEditor/InlineColorEditor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

/*jslint vars: true, plusplus: true, nomen: true, regexp: true, maxerr: 50 */
/*global define, brackets, $, window */

define(function (require, exports, module) {
"use strict";

var InlineWidget = brackets.getModule("editor/InlineWidget").InlineWidget,
ColorEditor = require("ColorEditor").ColorEditor,
InlineEditorTemplate = require("text!InlineColorEditorTemplate.html");

var MAX_USED_COLORS = 7;

function InlineColorEditor(color, startBookmark, endBookmark) {
this.color = color;
this.startBookmark = startBookmark;
this.endBookmark = endBookmark;
this.setColor = this.setColor.bind(this);

InlineWidget.call(this);
}

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;

InlineColorEditor.prototype = new InlineWidget();
InlineColorEditor.prototype.constructor = InlineColorEditor;
InlineColorEditor.prototype.parentClass = InlineWidget.prototype;
InlineColorEditor.prototype.$wrapperDiv = null;

InlineColorEditor.prototype.onClosed = function () {
if (this.startBookmark) {
this.startBookmark.clear();
}
if (this.endBookmark) {
this.endBookmark.clear();
}
};

InlineColorEditor.prototype.setColor = function (colorLabel) {
var start, end;
if (!colorLabel) {
return;
}

if (colorLabel !== this.color) {
start = this.startBookmark.find();
if (!start) {
return;
}

end = this.endBookmark.find();
if (!end || start.ch === end.ch) {
end = { line: start.line,
ch: start.ch + (this.color ? this.color.length : 0) };
}

this.editor.document.replaceRange(colorLabel, start, end);
this.editor.setSelection(start, {
line: start.line,
ch: start.ch + colorLabel.length
});
this.color = colorLabel;
}
};

InlineColorEditor.prototype.load = function (hostEditor) {
var selectedColors = [];
this.editor = hostEditor;
this.parentClass.load.call(this, hostEditor);
selectedColors = this.editor.document.getText().match(InlineColorEditor.colorRegEx);
selectedColors = this.usedColors(selectedColors, MAX_USED_COLORS);
this.$wrapperDiv = $(InlineEditorTemplate);
this.colorEditor = new ColorEditor(this.$wrapperDiv, this.color, this.setColor, selectedColors);
this.$htmlContent.append(this.$wrapperDiv);
};

InlineColorEditor.prototype._editorHasFocus = function () {
return true;
};

InlineColorEditor.prototype.onAdded = function () {
window.setTimeout(this._sizeEditorToContent.bind(this));
this.colorEditor.focus();
};

InlineColorEditor.prototype._sizeEditorToContent = function () {
this.hostEditor.setInlineWidgetHeight(this, this.$wrapperDiv.outerHeight(), true);
};

function _colorSort(a, b) {
if (a.count === b.count) {
return 0;
}
if (a.count > b.count) {
return -1;
}
if (a.count < b.count) {
return 1;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the code above is hardcoded to not pass undefined, we can remove this length check (or alternatively, move the arg and hardcode a reference to MAX_USED_COLORS down here).

}

InlineColorEditor.prototype.usedColors = function (originalArray, maxLength) {
var copyArray,
compressed = [];

copyArray = $.map(originalArray, function (color) {
return color.toLowerCase();
});

compressed = $.map(originalArray, function (originalColor) {
var a = {},
lastIndex = 0,
colorCount = 0,
colorCopy = originalColor.toLowerCase();

do {
lastIndex = copyArray.indexOf(colorCopy, lastIndex);
if (lastIndex !== -1) {
colorCount++;
copyArray.splice(lastIndex, 1);
}
} while (lastIndex !== -1 && copyArray.length);

if (colorCount > 0) {
a.value = originalColor;
a.count = colorCount;
return a;
}
}).sort(_colorSort);

return compressed.slice(0, maxLength);
};

module.exports.InlineColorEditor = InlineColorEditor;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

<div class="color_editor">
<section>
<div class="sliders">
<div class="color_selection_field">
<div class="saturation_gradient gradient_overlay"></div>
<div class="luminosity_gradient gradient_overlay"></div>
<div tabindex="0" class="selector_base">
<div class="selector"></div>
</div>
</div>
<div class="hue_slider slider">
<div tabindex="0" class="selector_base">
<div class="selector"></div>
</div>
</div>
<div class="opacity_slider slider">
<div class="opacity_gradient gradient_overlay"></div>
<div tabindex="0" class="selector_base">
<div class="selector"></div>
</div>
</div>
</div>
<footer>
<input class="color_value" readonly="readonly" />
<ul class="button-bar">
<li class="selected"><a href="#" tabindex="0" class="rgba">RGBa</a></li>
<li><a href="#" tabindex="0" class="hex">HEX</a></li>
<li><a href="#" tabindex="0" class="hsla">HSLa</a></li>
</ul>
</footer>
</section>
<aside>
<header>
<div class="large_swatches">
<div class="current_color large_swatch"></div>
<div class="last_color large_swatch"></div>
</div>
</header>
<ul class="swatches"></ul>
</aside>
</div>
Loading