<commit_before>import _debounce from 'lodash-es/debounce';

import { select as d3_select } from 'd3-selection';

import {
    modeAddArea,
    modeAddLine,
    modeAddPoint,
    modeBrowse
} from '../modes';

import { svgIcon } from '../svg';
import { t } from '../util/locale';
import { tooltip } from '../util/tooltip';
import { uiPresetIcon } from './preset_icon';
import { uiTooltipHtml } from './tooltipHtml';

export function uiModes(context) {


    function enabled() {
        return osmEditable();
    }

    function osmEditable() {
        var mode = context.mode();
        return context.editable() && mode && mode.id !== 'save';
    }


    return function(selection) {
        context
            .on('enter.editor', function(entered) {
                selection.selectAll('button.add-button')
                    .classed('active', function(mode) { return entered.button === mode.button; });
                context.container()
                    .classed('mode-' + entered.id, true);
            });

        context
            .on('exit.editor', function(exited) {
                context.container()
                    .classed('mode-' + exited.id, false);
            });
/*
        modes.forEach(function(mode) {
            context.keybinding().on(mode.key, function() {
                if (!enabled(mode)) return;

                if (mode.id === context.mode().id) {
                    context.enter(modeBrowse(context));
                } else {
                    context.enter(mode);
                }
            });
        });
*/

        var debouncedUpdate = _debounce(update, 500, { leading: true, trailing: true });

        context.map()
            .on('move.modes', debouncedUpdate)
            .on('drawn.modes', debouncedUpdate);

        context
            .on('enter.modes', update)
            .on('favoritePreset.modes', update);

        update();


        function update() {
            // add favorite presets to modes
            var favoritePresets = context.getFavoritePresets();
            var favoriteModes = favoritePresets.map(function(d) {
                var preset = context.presets().item(d.id);
                var markerClass = 'add-preset add-' + d.geom + ' add-preset-' + preset.name()
                    .replace(/\s+/g, '_')
                    + '-' + d.geom; //replace spaces with underscores to avoid css interpretation
                if (preset.isFallback()) {
                    markerClass += ' add-generic-preset';
                }
                var presetName = t('presets.presets.' + preset.id + '.name');
                var relevantMatchingGeometry = preset.geometry.filter(function(geometry) {
                    return ['vertex', 'point', 'line', 'area'].indexOf(geometry) !== -1;
                });
                var tooltipTitleID = 'modes.add_preset.title';
                if (relevantMatchingGeometry.length !== 1) {
                    tooltipTitleID = 'modes.add_preset.' + d.geom + '.title';
                }
                var favoriteMode = {
                    button: markerClass,
                    title: presetName,
                    description: t(tooltipTitleID, { feature: presetName }),
                    key: '',
                    preset: preset,
                    geometry: d.geom
                };
                switch (d.geom) {
                    case 'point':
                    case 'vertex':
                        return modeAddPoint(context, favoriteMode);
                    case 'line':
                        return modeAddLine(context, favoriteMode);
                    case 'area':
                        return modeAddArea(context, favoriteMode);
                }
            });

            var data = favoriteModes;

            var buttons = selection.selectAll('button.add-button')
                .data(data, function(d) { return d.id; });

            // exit
            buttons.exit()
                .remove();
<commit_msg>Fix stale favorite preset buttons<commit_after>import _debounce from 'lodash-es/debounce';

import { select as d3_select } from 'd3-selection';

import {
    modeAddArea,
    modeAddLine,
    modeAddPoint,
    modeBrowse
} from '../modes';

import { svgIcon } from '../svg';
import { t } from '../util/locale';
import { tooltip } from '../util/tooltip';
import { uiPresetIcon } from './preset_icon';
import { uiTooltipHtml } from './tooltipHtml';

export function uiModes(context) {


    function enabled() {
        return osmEditable();
    }

    function osmEditable() {
        var mode = context.mode();
        return context.editable() && mode && mode.id !== 'save';
    }


    return function(selection) {
        context
            .on('enter.editor', function(entered) {
                selection.selectAll('button.add-button')
                    .classed('active', function(mode) { return entered.button === mode.button; });
                context.container()
                    .classed('mode-' + entered.id, true);
            });

        context
            .on('exit.editor', function(exited) {
                context.container()
                    .classed('mode-' + exited.id, false);
            });
/*
        modes.forEach(function(mode) {
            context.keybinding().on(mode.key, function() {
                if (!enabled(mode)) return;

                if (mode.id === context.mode().id) {
                    context.enter(modeBrowse(context));
                } else {
                    context.enter(mode);
                }
            });
        });
*/

        var debouncedUpdate = _debounce(update, 500, { leading: true, trailing: true });

        context.map()
            .on('move.modes', debouncedUpdate)
            .on('drawn.modes', debouncedUpdate);

        context
            .on('enter.modes', update)
            .on('favoritePreset.modes', update);

        update();


        function update() {
            // add favorite presets to modes
            var favoritePresets = context.getFavoritePresets();
            var favoriteModes = favoritePresets.map(function(d) {
                var preset = context.presets().item(d.id);
                var markerClass = 'add-preset add-' + d.geom + ' add-preset-' + preset.name()
                    .replace(/\s+/g, '_')
                    + '-' + d.geom; //replace spaces with underscores to avoid css interpretation
                if (preset.isFallback()) {
                    markerClass += ' add-generic-preset';
                }
                var presetName = t('presets.presets.' + preset.id + '.name');
                var relevantMatchingGeometry = preset.geometry.filter(function(geometry) {
                    return ['vertex', 'point', 'line', 'area'].indexOf(geometry) !== -1;
                });
                var tooltipTitleID = 'modes.add_preset.title';
                if (relevantMatchingGeometry.length !== 1) {
                    tooltipTitleID = 'modes.add_preset.' + d.geom + '.title';
                }
                var favoriteMode = {
                    button: markerClass,
                    title: presetName,
                    description: t(tooltipTitleID, { feature: presetName }),
                    key: '',
                    preset: preset,
                    geometry: d.geom
                };
                switch (d.geom) {
                    case 'point':
                    case 'vertex':
                        return modeAddPoint(context, favoriteMode);
                    case 'line':
                        return modeAddLine(context, favoriteMode);
                    case 'area':
                        return modeAddArea(context, favoriteMode);
                }
            });

            var data = favoriteModes;

            var buttons = selection.selectAll('button.add-button')
                .data(data, function(d) { return d.button; });

            // exit
            buttons.exit()
                .remove();

{'fraction_non_alphanumeric': 0.12703175793948487, 'fraction_numerical': 0.0017504376094023505, 'mean_word_length': 1.4514250689549495, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6095123', 'n_tokens_mistral': 2167, 'n_tokens_neox': 1989, 'n_words': 434}