Skip to content

Commit

Permalink
Try to avoid style recalculation when typing on the comment box
Browse files Browse the repository at this point in the history
I think this doesn't completely work.. It reduces the amount
of unnecessary DOM changes, but there are still some more.
  • Loading branch information
bhousel committed Apr 19, 2019
1 parent ca02f7e commit ae06e28
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
11 changes: 9 additions & 2 deletions modules/ui/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,26 @@ export function uiCommit(context) {
var prose = saveSection.selectAll('.commit-info')
.data([0]);

if (prose.enter().size()) { // first time, make sure to update user details in prose
_userDetails = null;
}

prose = prose.enter()
.append('p')
.attr('class', 'commit-info')
.text(t('commit.upload_explanation'))
.merge(prose);

// always check if this has changed, but only update prose.html()
// if needed, because it can trigger a style recalculation
osm.userDetails(function(err, user) {
if (err) return;

var userLink = d3_select(document.createElement('div'));

if (_userDetails === user) return; // no change
_userDetails = user;

var userLink = d3_select(document.createElement('div'));

if (user.image_url) {
userLink
.append('img')
Expand Down
30 changes: 18 additions & 12 deletions modules/ui/form_fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { utilGetSetValue, utilNoAuto } from '../util';
export function uiFormFields(context) {
var moreCombo = uiCombobox(context, 'more-fields').minItems(1);
var _fieldsArr = [];
var _lastPlaceholder = '';
var _state = '';
var _klass = '';

Expand Down Expand Up @@ -49,17 +50,22 @@ export function uiFormFields(context) {
});


notShown = notShown.map(function(field) {
var titles = [];
var moreFields = notShown.map(function(field) {
var label = field.label();
titles.push(label);
return {
title: field.label(),
value: field.label(),
title: label,
value: label,
field: field
};
});

var placeholder = titles.slice(0,3).join(', ') + ((titles.length > 3) ? '…' : '');


var more = selection.selectAll('.more-fields')
.data((_state === 'hover' || notShown.length === 0) ? [] : [0]);
.data((_state === 'hover' || moreFields.length === 0) ? [] : [0]);

more.exit()
.remove();
Expand All @@ -82,20 +88,14 @@ export function uiFormFields(context) {
.append('input')
.attr('class', 'value')
.attr('type', 'text')
.attr('placeholder', placeholder)
.call(utilNoAuto)
.merge(input);

input
.call(utilGetSetValue, '')
.attr('placeholder', function() {
var placeholder = [];
for (var field in notShown) {
placeholder.push(notShown[field].title);
}
return placeholder.slice(0,3).join(', ') + ((placeholder.length > 3) ? '…' : '');
})
.call(moreCombo
.data(notShown)
.data(moreFields)
.on('accept', function (d) {
if (!d) return; // user entered something that was not matched
var field = d.field;
Expand All @@ -106,6 +106,12 @@ export function uiFormFields(context) {
}
})
);

// avoid updating placeholder excessively (triggers style recalc)
if (_lastPlaceholder !== placeholder) {
input.attr('placeholder', placeholder);
_lastPlaceholder = placeholder;
}
}


Expand Down

0 comments on commit ae06e28

Please sign in to comment.