mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-07 08:33:19 +08:00
feat(settings): fix orphans/widows in code MIME types
This commit is contained in:
parent
978bb5eb0b
commit
0ef5cb843e
@ -7,10 +7,31 @@ const TPL = `
|
|||||||
<h4>${t('code_mime_types.title')}</h4>
|
<h4>${t('code_mime_types.title')}</h4>
|
||||||
|
|
||||||
<ul class="options-mime-types" style="list-style-type: none;"></ul>
|
<ul class="options-mime-types" style="list-style-type: none;"></ul>
|
||||||
</div>`;
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.options-mime-types section {
|
||||||
|
margin-bottom: 1em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
`;
|
||||||
|
|
||||||
let idCtr = 1; // global, since this can be shown in multiple dialogs
|
let idCtr = 1; // global, since this can be shown in multiple dialogs
|
||||||
|
|
||||||
|
function groupMimeTypesAlphabetically(ungroupedMimeTypes) {
|
||||||
|
const result = {};
|
||||||
|
ungroupedMimeTypes = ungroupedMimeTypes.toSorted((a, b) => a.title > b.title);
|
||||||
|
|
||||||
|
for (const mimeType of ungroupedMimeTypes) {
|
||||||
|
const initial = mimeType.title.charAt(0).toUpperCase();
|
||||||
|
if (!result[initial]) {
|
||||||
|
result[initial] = [];
|
||||||
|
}
|
||||||
|
result[initial].push(mimeType);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
export default class CodeMimeTypesOptions extends OptionsWidget {
|
export default class CodeMimeTypesOptions extends OptionsWidget {
|
||||||
doRender() {
|
doRender() {
|
||||||
this.$widget = $(TPL);
|
this.$widget = $(TPL);
|
||||||
@ -19,36 +40,29 @@ export default class CodeMimeTypesOptions extends OptionsWidget {
|
|||||||
|
|
||||||
async optionsLoaded(options) {
|
async optionsLoaded(options) {
|
||||||
this.$mimeTypes.empty();
|
this.$mimeTypes.empty();
|
||||||
let index = -1;
|
|
||||||
let prevInitial = "";
|
|
||||||
|
|
||||||
for (const mimeType of mimeTypesService.getMimeTypes()) {
|
const groupedMimeTypes = groupMimeTypesAlphabetically(mimeTypesService.getMimeTypes());
|
||||||
const id = "code-mime-type-" + (idCtr++);
|
|
||||||
index++;
|
for (const [ initial, mimeTypes ] of Object.entries(groupedMimeTypes)) {
|
||||||
|
const $section = $("<section>");
|
||||||
// Append a heading to group items by the first letter, excepting for the
|
$section.append($("<h5>").text(initial));
|
||||||
// first item ("Plain Text"). Note: this code assumes the items are already
|
|
||||||
// in alphabetical ordered.
|
for (const mimeType of mimeTypes) {
|
||||||
if (index > 0) {
|
const id = "code-mime-type-" + (idCtr++);
|
||||||
const initial = mimeType.title.charAt(0).toUpperCase();
|
$section.append($("<li>")
|
||||||
|
.append($('<input type="checkbox" class="form-check-input">')
|
||||||
if (initial !== prevInitial) {
|
.attr("id", id)
|
||||||
this.$mimeTypes.append($("<h5>").text(initial));
|
.attr("data-mime-type", mimeType.mime)
|
||||||
prevInitial = initial;
|
.prop("checked", mimeType.enabled))
|
||||||
}
|
.on('change', () => this.save())
|
||||||
|
.append(" ")
|
||||||
|
.append($('<label>')
|
||||||
|
.attr("for", id)
|
||||||
|
.text(mimeType.title))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.$mimeTypes.append($("<li>")
|
this.$mimeTypes.append($section);
|
||||||
.append($('<input type="checkbox" class="form-check-input">')
|
|
||||||
.attr("id", id)
|
|
||||||
.attr("data-mime-type", mimeType.mime)
|
|
||||||
.prop("checked", mimeType.enabled))
|
|
||||||
.on('change', () => this.save())
|
|
||||||
.append(" ")
|
|
||||||
.append($('<label>')
|
|
||||||
.attr("for", id)
|
|
||||||
.text(mimeType.title))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user