@@ -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 - 9 a - f A - 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 ) ;
0 commit comments