This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Adding JSLint passed inline color editor #2010
Merged
Merged
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 1d7e011
Merge branch 'master', remote-tracking branch 'origin' into rlim/colo…
RaymondLim ebc3e55
Code clean-up and other changes suggested in code review feedbacks.
RaymondLim 394cbc8
Some more clean up of "return" keyword.
RaymondLim 9ffeee6
Adding MIT license to one more file.
RaymondLim 4e134ec
Missed to add these two in previous commit.
RaymondLim 1af1631
Re-commiting these four.
RaymondLim f576ca3
One more "return" keyword removal that I missed.
RaymondLim 9eb433c
Another "return" keyword removal.
RaymondLim af8631e
Remove the parameter that I added into onClosed function. We can just…
RaymondLim b0c032c
Fix one JSLint error I introduced in last commit.
RaymondLim 1e546bd
Prevent clicks on some UI elements (color selection field, slider and…
RaymondLim 5560b96
Use bookmarks to update color values in main editor to fix issue #2048
RaymondLim 6ffd6e3
Limit existing color swatches to be shown as the number defined in MA…
RaymondLim f03b3c2
Need to get the color picker first before we remove the bookmark. Oth…
RaymondLim 8b547b8
Fix the console error that I introduced in previous commit. We should…
RaymondLim 38b0a7e
Another round of changes for review comments.
RaymondLim d01c931
Remove a "return" keyword I introduced with my new code for tab handl…
RaymondLim cd60e1f
Simplify the jQuery calls and selectors to find the last color swatch.
RaymondLim da5a685
Fix JSLint errors.
RaymondLim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
436 changes: 436 additions & 0 deletions
436
src/extensions/default/InlineColorEditor/ColorEditor.js
Large diffs are not rendered by default.
Oops, something went wrong.
157 changes: 157 additions & 0 deletions
157
src/extensions/default/InlineColorEditor/InlineColorEditor.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
|
|
||
| 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; | ||
| }); | ||
42 changes: 42 additions & 0 deletions
42
src/extensions/default/InlineColorEditor/InlineColorEditorTemplate.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).