* Update popup style sort after toggle. Closes #619 * Add popup auto resort option * Switch autoResort to true by default * Refactor sorting * Fix: simplify sortStyles function * Change: autoResort=false
This commit is contained in:
parent
f5db0a5ab0
commit
1fbbeae9b9
|
@ -1056,6 +1056,10 @@
|
||||||
"message": "Styles before commands",
|
"message": "Styles before commands",
|
||||||
"description": "Label for the checkbox controlling section order in the popup."
|
"description": "Label for the checkbox controlling section order in the popup."
|
||||||
},
|
},
|
||||||
|
"popupAutoResort": {
|
||||||
|
"message": "Resort styles in popup after toggling",
|
||||||
|
"description": "Label for the checkbox controlling popup resorting."
|
||||||
|
},
|
||||||
"prefShowBadge": {
|
"prefShowBadge": {
|
||||||
"message": "Number of styles active for the current site",
|
"message": "Number of styles active for the current site",
|
||||||
"description": "Label for the checkbox controlling toolbar badge text."
|
"description": "Label for the checkbox controlling toolbar badge text."
|
||||||
|
|
|
@ -18,6 +18,7 @@ const prefs = (() => {
|
||||||
'popup.breadcrumbs.usePath': false, // use URL path for 'this URL'
|
'popup.breadcrumbs.usePath': false, // use URL path for 'this URL'
|
||||||
'popup.enabledFirst': true, // display enabled styles before disabled styles
|
'popup.enabledFirst': true, // display enabled styles before disabled styles
|
||||||
'popup.stylesFirst': true, // display enabled styles before disabled styles
|
'popup.stylesFirst': true, // display enabled styles before disabled styles
|
||||||
|
'popup.autoResort': false, // auto resort styles after toggling
|
||||||
'popup.borders': false, // add white borders on the sides
|
'popup.borders': false, // add white borders on the sides
|
||||||
'popup.findStylesInline': true, // use the inline style search
|
'popup.findStylesInline': true, // use the inline style search
|
||||||
|
|
||||||
|
|
|
@ -101,6 +101,13 @@
|
||||||
<span></span>
|
<span></span>
|
||||||
</span>
|
</span>
|
||||||
</label>
|
</label>
|
||||||
|
<label>
|
||||||
|
<span i18n-text="popupAutoResort"></span>
|
||||||
|
<span class="onoffswitch">
|
||||||
|
<input type="checkbox" id="popup.autoResort" class="slider">
|
||||||
|
<span></span>
|
||||||
|
</span>
|
||||||
|
</label>
|
||||||
<label class="chromium-only">
|
<label class="chromium-only">
|
||||||
<span i18n-text="popupBorders" i18n-title="popupBordersTooltip"></span>
|
<span i18n-text="popupBorders" i18n-title="popupBordersTooltip"></span>
|
||||||
<span class="onoffswitch">
|
<span class="onoffswitch">
|
||||||
|
|
|
@ -218,6 +218,15 @@ function initPopup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function sortStyles(entries) {
|
||||||
|
const enabledFirst = prefs.get('popup.enabledFirst');
|
||||||
|
entries.sort((a, b) =>
|
||||||
|
enabledFirst && a.styleMeta.enabled !== b.styleMeta.enabled ?
|
||||||
|
(a.styleMeta.enabled ? -1 : 1) :
|
||||||
|
a.styleMeta.name.localeCompare(b.styleMeta.name)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function showStyles(styles) {
|
function showStyles(styles) {
|
||||||
if (!styles) {
|
if (!styles) {
|
||||||
return;
|
return;
|
||||||
|
@ -227,25 +236,26 @@ function showStyles(styles) {
|
||||||
window.dispatchEvent(new Event('showStyles:done'));
|
window.dispatchEvent(new Event('showStyles:done'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const entries = styles.map(createStyleElement);
|
||||||
const enabledFirst = prefs.get('popup.enabledFirst');
|
sortStyles(entries);
|
||||||
styles.sort((a, b) => (
|
entries.forEach(e => installed.appendChild(e));
|
||||||
enabledFirst && a.enabled !== b.enabled
|
|
||||||
? !(a.enabled < b.enabled) ? -1 : 1
|
|
||||||
: a.name.localeCompare(b.name)
|
|
||||||
));
|
|
||||||
|
|
||||||
const container = document.createDocumentFragment();
|
|
||||||
styles.forEach(style => createStyleElement({style, container}));
|
|
||||||
installed.appendChild(container);
|
|
||||||
window.dispatchEvent(new Event('showStyles:done'));
|
window.dispatchEvent(new Event('showStyles:done'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sortStylesInPlace() {
|
||||||
|
if (!prefs.get('popup.autoResort')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const entries = $$('.entry', installed);
|
||||||
|
if (!entries.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sortStyles(entries);
|
||||||
|
entries.forEach(e => installed.appendChild(e));
|
||||||
|
}
|
||||||
|
|
||||||
function createStyleElement({
|
|
||||||
style,
|
function createStyleElement(style) {
|
||||||
container = installed,
|
|
||||||
}) {
|
|
||||||
let entry = $(ENTRY_ID_PREFIX + style.id);
|
let entry = $(ENTRY_ID_PREFIX + style.id);
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
entry = template.style.cloneNode(true);
|
entry = template.style.cloneNode(true);
|
||||||
|
@ -319,9 +329,7 @@ function createStyleElement({
|
||||||
entry.classList.toggle('not-applied', style.excluded || style.sloppy);
|
entry.classList.toggle('not-applied', style.excluded || style.sloppy);
|
||||||
entry.classList.toggle('regexp-partial', style.sloppy);
|
entry.classList.toggle('regexp-partial', style.sloppy);
|
||||||
|
|
||||||
if (entry.parentNode !== container) {
|
return entry;
|
||||||
container.appendChild(entry);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -343,7 +351,9 @@ Object.assign(handleEvent, {
|
||||||
toggle(event) {
|
toggle(event) {
|
||||||
// when fired on checkbox, prevent the parent label from seeing the event, see #501
|
// when fired on checkbox, prevent the parent label from seeing the event, see #501
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
API.toggleStyle(handleEvent.getClickedStyleId(event), this.checked);
|
API
|
||||||
|
.toggleStyle(handleEvent.getClickedStyleId(event), this.checked)
|
||||||
|
.then(sortStylesInPlace);
|
||||||
},
|
},
|
||||||
|
|
||||||
delete(event) {
|
delete(event) {
|
||||||
|
@ -475,12 +485,12 @@ function handleUpdate({style, reason}) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ($(ENTRY_ID_PREFIX + style.id)) {
|
if ($(ENTRY_ID_PREFIX + style.id)) {
|
||||||
createStyleElement({style});
|
createStyleElement(style);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
document.body.classList.remove('blocked');
|
document.body.classList.remove('blocked');
|
||||||
$$.remove('.blocked-info, #no-styles');
|
$$.remove('.blocked-info, #no-styles');
|
||||||
createStyleElement({style});
|
createStyleElement(style);
|
||||||
})
|
})
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user