Skip to content

Commit 3a3d977

Browse files
committed
add preview for colour input
1 parent 3e7fd6e commit 3a3d977

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

css/80_app.css

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,15 @@ a.hide-toggle {
15051505
fill: #333;
15061506
opacity: .5;
15071507
}
1508+
.form-field-button.colour-preview {
1509+
background-color: #fff;
1510+
border-radius: 0 0 4px 0;
1511+
}
1512+
.form-field-button.colour-preview > div {
1513+
border: 3px solid #fff;
1514+
height: 100%;
1515+
border-radius: 8px;
1516+
}
15081517

15091518

15101519
/* round corners of first/last child elements */
@@ -1703,12 +1712,13 @@ a.hide-toggle {
17031712

17041713
/* Field - Text / Numeric
17051714
------------------------------------------------------- */
1706-
.form-field-input-text > input:only-of-type,
1715+
.form-field-input-text > input:only-child,
17071716
.form-field-input-tel > input:only-of-type,
17081717
.form-field-input-email > input:only-of-type,
17091718
.form-field-input-url > input:only-child {
17101719
border-radius: 0 0 4px 4px;
17111720
}
1721+
.form-field-input-text > input:not(:only-child),
17121722
.form-field-input-url > input:not(:only-child) {
17131723
border-radius: 0 0 0 4px;
17141724
}

modules/ui/fields/address.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ export function uiFieldAddress(field, context) {
288288
})
289289
.attr('title', function(subfield) {
290290
var val = tags[field.key + ':' + subfield.id];
291-
return val && Array.isArray(val) && val.filter(Boolean).join('\n');
291+
return (val && Array.isArray(val)) ? val.filter(Boolean).join('\n') : undefined;
292292
})
293293
.classed('mixed', function(subfield) {
294294
return Array.isArray(tags[field.key + ':' + subfield.id]);

modules/ui/fields/input.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function uiFieldText(field, context) {
2121
var dispatch = d3_dispatch('change');
2222
var input = d3_select(null);
2323
var outlinkButton = d3_select(null);
24+
var wrap = d3_select(null);
2425
var _entityIDs = [];
2526
var _tags;
2627
var _phoneFormats = {};
@@ -62,7 +63,7 @@ export function uiFieldText(field, context) {
6263
calcLocked();
6364
var isLocked = field.locked();
6465

65-
var wrap = selection.selectAll('.form-field-input-wrap')
66+
wrap = selection.selectAll('.form-field-input-wrap')
6667
.data([0]);
6768

6869
wrap = wrap.enter()
@@ -167,9 +168,37 @@ export function uiFieldText(field, context) {
167168
if (value) window.open(value, '_blank');
168169
})
169170
.merge(outlinkButton);
171+
} else if (field.key.includes('colour')) {
172+
input.attr('type', 'text');
173+
174+
updateColourPreview();
170175
}
171176
}
172177

178+
function updateColourPreview() {
179+
wrap.selectAll('.foreign-id-permalink')
180+
.remove();
181+
182+
const colour = utilGetSetValue(input);
183+
184+
// see https://github.com/openstreetmap/openstreetmap-website/blob/08e2a0/app/helpers/browse_tags_helper.rb#L173
185+
// we use the same logic to validate colours, except we don't need to check whether named colours
186+
// are valid, since the browser does this natively when we set the background-colour
187+
const isColourValid = !!colour.match(/^(#([0-9a-fA-F]{3}){1,2}|\w+)$/);
188+
if (!isColourValid) return;
189+
190+
outlinkButton = wrap.selectAll('.foreign-id-permalink')
191+
.data([colour], d => d);
192+
193+
outlinkButton
194+
.enter()
195+
.append('div')
196+
.attr('class', 'form-field-button foreign-id-permalink colour-preview')
197+
.append('div')
198+
.style('background-color', d => d)
199+
.merge(outlinkButton);
200+
}
201+
173202

174203
function updatePhonePlaceholder() {
175204
if (input.empty() || !Object.keys(_phoneFormats).length) return;
@@ -247,6 +276,8 @@ export function uiFieldText(field, context) {
247276
.attr('placeholder', isMixed ? t('inspector.multiple_values') : (field.placeholder() || t('inspector.unknown')))
248277
.classed('mixed', isMixed);
249278

279+
if (field.key.includes('colour')) updateColourPreview();
280+
250281
if (outlinkButton && !outlinkButton.empty()) {
251282
var disabled = !validIdentifierValueForLink();
252283
outlinkButton.classed('disabled', disabled);

modules/util/get_set_value.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Like selection.property('value', ...), but avoids no-op value sets,
22
// which can result in layout/repaint thrashing in some situations.
3+
/** @returns {string} */
34
export function utilGetSetValue(selection, value) {
45
function d3_selection_value(value) {
56
function valueNull() {

0 commit comments

Comments
 (0)