Skip to content

Commit

Permalink
iD#10254 support 3rd-party icons for preset fields
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Jun 20, 2024
1 parent d05b347 commit d0d94dd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
13 changes: 13 additions & 0 deletions modules/svg/icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,16 @@ export function svgIcon(name, svgklass, useklass) {
.attr('class', useklass);
};
}

/** @param {string} url */
export function svgIconExternal(url) {
/** @param {import("d3-selection").Selection} selection */
return function drawIcon(selection) {
selection.selectAll('img.icon')
.data([0])
.enter()
.append('img')
.attr('class', 'icon')
.attr('src', url);
};
}
25 changes: 22 additions & 3 deletions modules/ui/fields/combo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { drag as d3_drag } from 'd3-drag';
import * as countryCoder from '@rapideditor/country-coder';

import { fileFetcher } from '../../core/file_fetcher';
import { prefs } from '../../core/preferences';
import { osmEntity } from '../../osm/entity';
import { t } from '../../core/localizer';
import { services } from '../../services';
import { uiCombobox } from '../combobox';
import { svgIcon } from '../../svg/icon';
import { svgIcon, svgIconExternal } from '../../svg/icon';

import { utilKeybinding } from '../../util/keybinding';
import { utilArrayUniq, utilGetSetValue, utilNoAuto, utilRebind, utilTotalExtent, utilUnicodeCharsCount } from '../../util';
Expand All @@ -22,6 +23,8 @@ export {
uiFieldCombo as uiFieldTypeCombo
};

const showThirdPartyIcons = prefs('preferences.privacy.thirdpartyicons') || 'true';

export function uiFieldCombo(field, context) {
var dispatch = d3_dispatch('change');
var _isMulti = (field.type === 'multiCombo' || field.type === 'manyCombo');
Expand Down Expand Up @@ -305,7 +308,15 @@ export function uiFieldCombo(field, context) {
.insert('span', ':first-child')
.attr('class', 'tag-value-icon');
if (iconsField.icons[value]) {
span.call(svgIcon(`#${iconsField.icons[value]}`));
const isExternal = (
iconsField.icons[value].startsWith('https://') &&
showThirdPartyIcons === 'true'
);
span.call(
isExternal
? svgIconExternal(iconsField.icons[value])
: svgIcon(`#${iconsField.icons[value]}`)
);
}
disp.call(this, selection);
};
Expand Down Expand Up @@ -613,12 +624,20 @@ export function uiFieldCombo(field, context) {
if (iconsField.icons) {
container.selectAll('.tag-value-icon').remove();
if (iconsField.icons[value]) {
const isExternal = (
iconsField.icons[value].startsWith('https://') &&
showThirdPartyIcons === 'true'
);
container.selectAll('.tag-value-icon')
.data([value])
.enter()
.insert('div', 'input')
.attr('class', 'tag-value-icon')
.call(svgIcon(`#${iconsField.icons[value]}`));
.call(
isExternal
? svgIconExternal(iconsField.icons[value])
: svgIcon(`#${iconsField.icons[value]}`)
);
}
}
}
Expand Down

0 comments on commit d0d94dd

Please sign in to comment.