Skip to content

Commit

Permalink
refactor: ♻️ Use Svelte for checkbox component
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Dec 16, 2021
1 parent 18f3abe commit 389b558
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 178 deletions.
315 changes: 231 additions & 84 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20319,6 +20319,17 @@ function attr(node, attribute, value) {
else if (node.getAttribute(attribute) !== value)
node.setAttribute(attribute, value);
}
function get_binding_group_value(group, __value, checked) {
const value = new Set();
for (let i = 0; i < group.length; i += 1) {
if (group[i].checked)
value.add(group[i].__value);
}
if (!checked) {
value.delete(__value);
}
return Array.from(value);
}
function children(element) {
return Array.from(element.childNodes);
}
Expand Down Expand Up @@ -20624,6 +20635,184 @@ class SvelteComponent {
}
}

/* src\Components\Checkboxes.svelte generated by Svelte v3.35.0 */

function get_each_context$a(ctx, list, i) {
const child_ctx = ctx.slice();
child_ctx[7] = list[i];
return child_ctx;
}

// (9:0) {#each options as option}
function create_each_block$a(ctx) {
let div;
let label;
let input;
let input_value_value;
let t0;
let t1_value = /*option*/ ctx[7] + "";
let t1;
let t2;
let mounted;
let dispose;

return {
c() {
div = element("div");
label = element("label");
input = element("input");
t0 = space();
t1 = text(t1_value);
t2 = space();
attr(input, "type", "checkbox");
input.__value = input_value_value = /*option*/ ctx[7];
input.value = input.__value;
/*$$binding_groups*/ ctx[5][0].push(input);
},
m(target, anchor) {
insert(target, div, anchor);
append(div, label);
append(label, input);
input.checked = ~/*selected*/ ctx[3].indexOf(input.__value);
append(label, t0);
append(label, t1);
append(div, t2);

if (!mounted) {
dispose = [
listen(input, "change", /*input_change_handler*/ ctx[4]),
listen(input, "change", /*change_handler*/ ctx[6])
];

mounted = true;
}
},
p(ctx, dirty) {
if (dirty & /*options*/ 4 && input_value_value !== (input_value_value = /*option*/ ctx[7])) {
input.__value = input_value_value;
input.value = input.__value;
}

if (dirty & /*selected*/ 8) {
input.checked = ~/*selected*/ ctx[3].indexOf(input.__value);
}

if (dirty & /*options*/ 4 && t1_value !== (t1_value = /*option*/ ctx[7] + "")) set_data(t1, t1_value);
},
d(detaching) {
if (detaching) detach(div);
/*$$binding_groups*/ ctx[5][0].splice(/*$$binding_groups*/ ctx[5][0].indexOf(input), 1);
mounted = false;
run_all(dispose);
}
};
}

function create_fragment$j(ctx) {
let each_1_anchor;
let each_value = /*options*/ ctx[2];
let each_blocks = [];

for (let i = 0; i < each_value.length; i += 1) {
each_blocks[i] = create_each_block$a(get_each_context$a(ctx, each_value, i));
}

return {
c() {
for (let i = 0; i < each_blocks.length; i += 1) {
each_blocks[i].c();
}

each_1_anchor = empty();
},
m(target, anchor) {
for (let i = 0; i < each_blocks.length; i += 1) {
each_blocks[i].m(target, anchor);
}

insert(target, each_1_anchor, anchor);
},
p(ctx, [dirty]) {
if (dirty & /*options, selected, plugin, settingName, undefined, console*/ 15) {
each_value = /*options*/ ctx[2];
let i;

for (i = 0; i < each_value.length; i += 1) {
const child_ctx = get_each_context$a(ctx, each_value, i);

if (each_blocks[i]) {
each_blocks[i].p(child_ctx, dirty);
} else {
each_blocks[i] = create_each_block$a(child_ctx);
each_blocks[i].c();
each_blocks[i].m(each_1_anchor.parentNode, each_1_anchor);
}
}

for (; i < each_blocks.length; i += 1) {
each_blocks[i].d(1);
}

each_blocks.length = each_value.length;
}
},
i: noop,
o: noop,
d(detaching) {
destroy_each(each_blocks, detaching);
if (detaching) detach(each_1_anchor);
}
};
}

function instance$j($$self, $$props, $$invalidate) {

let { plugin } = $$props;
let { settingName } = $$props;
let { options } = $$props;
let selected = plugin.settings[settingName];
console.log({ options, selected });
const $$binding_groups = [[]];

function input_change_handler() {
selected = get_binding_group_value($$binding_groups[0], this.__value, this.checked);
$$invalidate(3, selected);
}

const change_handler = async () => {
if (plugin.settings[settingName] === undefined) {
return console.log(settingName + " not found in BC settings");
}

$$invalidate(0, plugin.settings[settingName] = selected, plugin);
await plugin.saveSettings();
await plugin.refreshIndex();
};

$$self.$$set = $$props => {
if ("plugin" in $$props) $$invalidate(0, plugin = $$props.plugin);
if ("settingName" in $$props) $$invalidate(1, settingName = $$props.settingName);
if ("options" in $$props) $$invalidate(2, options = $$props.options);
};

return [
plugin,
settingName,
options,
selected,
input_change_handler,
$$binding_groups,
change_handler
];
}

class Checkboxes extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance$j, create_fragment$j, safe_not_equal, { plugin: 0, settingName: 1, options: 2 });
}
}

/* src\Components\KoFi.svelte generated by Svelte v3.35.0 */

function create_fragment$i(ctx) {
Expand Down Expand Up @@ -20806,7 +20995,8 @@ const DEFAULT_SETTINGS = {
enableAlphaSort: true,
fieldSuggestor: true,
filterImpliedSiblingsOfDifferentTypes: false,
limitWriteBCCheckboxStates: {},
limitWriteBCCheckboxes: [],
CHECKBOX_STATES_OVERWRITTEN: false,
gridDots: false,
gridHeatmap: false,
heatmapColour: getComputedStyle(document.body).getPropertyValue("--text-accent"),
Expand All @@ -20830,7 +21020,7 @@ const DEFAULT_SETTINGS = {
showTrail: true,
showGrid: true,
showPrevNext: true,
limitTrailCheckboxStates: {},
limitTrailCheckboxes: [],
showAll: false,
noPathMessage: `This note has no real or implied parents`,
trailSeperator: "→",
Expand Down Expand Up @@ -25067,39 +25257,14 @@ class BCSettingTab extends require$$0.PluginSettingTab {
limitTrailFieldsDiv.createEl("strong", {
text: "Limit Trail View to only show certain fields",
});
const checkboxDiv = limitTrailFieldsDiv.createDiv({ cls: "checkboxes" });
function drawLimitTrailCheckboxes(div) {
checkboxDiv.empty();
const checkboxStates = settings.limitTrailCheckboxStates;
settings.userHiers.forEach((userHier) => {
userHier.up.forEach(async (field) => {
if (field === "")
return;
// First sort out limitTrailCheckboxStates
if (checkboxStates[field] === undefined) {
checkboxStates[field] = true;
await plugin.saveSettings();
}
const cbDiv = div.createDiv();
const checkedQ = checkboxStates[field];
const cb = cbDiv.createEl("input", {
type: "checkbox",
attr: { id: field },
});
cb.checked = checkedQ;
cbDiv.createEl("label", {
text: field,
attr: { for: field },
});
cb.addEventListener("change", async () => {
checkboxStates[field] = cb.checked;
await plugin.saveSettings();
console.log(settings.limitTrailCheckboxStates);
});
});
});
}
drawLimitTrailCheckboxes(checkboxDiv);
new Checkboxes({
target: trailDetails,
props: {
plugin: this.plugin,
settingName: "limitTrailCheckboxes",
options: getFields(settings.userHiers, "up"),
},
});
// new Setting(trailDetails)
// .setName("Field name to hide trail")
// .setDesc(
Expand Down Expand Up @@ -25331,44 +25496,14 @@ class BCSettingTab extends require$$0.PluginSettingTab {
limitWriteBCDiv.createEl("strong", {
text: "Limit to only write certain fields to files",
});
const limitWriteBCCheckboxDiv = limitWriteBCDiv.createDiv({
cls: "checkboxes",
new Checkboxes({
target: writeBCsToFileDetails,
props: {
plugin,
options: getFields(settings.userHiers),
settingName: "limitWriteBCCheckboxes",
},
});
function drawLimitWriteBCCheckboxes(div) {
limitWriteBCCheckboxDiv.empty();
const checkboxStates = settings.limitWriteBCCheckboxStates;
settings.userHiers.forEach((userHier) => {
DIRECTIONS$1.forEach((dir) => {
var _a;
(_a = userHier[dir]) === null || _a === void 0 ? void 0 : _a.forEach(async (field) => {
if (field === "")
return;
// First sort out limitWriteBCCheckboxStates
if (checkboxStates[field] === undefined) {
checkboxStates[field] = true;
await plugin.saveSettings();
}
const cbDiv = div.createDiv();
const checkedQ = checkboxStates[field];
const cb = cbDiv.createEl("input", {
type: "checkbox",
attr: { id: field },
});
cb.checked = checkedQ;
cbDiv.createEl("label", {
text: field,
attr: { for: field },
});
cb.addEventListener("change", async (event) => {
checkboxStates[field] = cb.checked;
await plugin.saveSettings();
console.log(settings.limitWriteBCCheckboxStates);
});
});
});
});
}
drawLimitWriteBCCheckboxes(limitWriteBCCheckboxDiv);
new require$$0.Setting(writeBCsToFileDetails)
.setName("Write BCs to file Inline")
.setDesc("When writing BCs to file, should they be written inline (using Dataview syntax), or into the YAML of the note?")
Expand Down Expand Up @@ -51357,7 +51492,7 @@ class BCPlugin extends require$$0.Plugin {
this.writeBCToFile = async (file) => {
var _a;
const { app, settings, mainG } = this;
const { limitWriteBCCheckboxStates, writeBCsInline, userHiers } = settings;
const { limitWriteBCCheckboxes, writeBCsInline, userHiers } = settings;
const { frontmatter } = app.metadataCache.getFileCache(file);
const api = (_a = app.plugins.plugins.metaedit) === null || _a === void 0 ? void 0 : _a.api;
if (!api) {
Expand All @@ -51369,7 +51504,7 @@ class BCPlugin extends require$$0.Plugin {
return { succ: s, field: oppField };
});
for (const { succ, field } of succInfo) {
if (!limitWriteBCCheckboxStates[field])
if (!limitWriteBCCheckboxes.includes(field))
return;
if (!writeBCsInline) {
await createOrUpdateYaml(field, succ, file, frontmatter, api);
Expand Down Expand Up @@ -51463,11 +51598,21 @@ class BCPlugin extends require$$0.Plugin {
await this.saveSettings();
});
});
const upFields = getFields(settings.userHiers, "up");
for (const field in settings.limitTrailCheckboxStates) {
if (!upFields.includes(field)) {
delete settings.limitTrailCheckboxStates[field];
}
if (settings.hasOwnProperty("limitTrailCheckboxStates")) {
//@ts-ignore
delete settings.limitTrailCheckboxStates;
await this.saveSettings();
}
if (settings.hasOwnProperty("limitWriteBCCheckboxStates")) {
//@ts-ignore
delete settings.limitWriteBCCheckboxStates;
await this.saveSettings();
}
if (!settings.CHECKBOX_STATES_OVERWRITTEN) {
settings.limitWriteBCCheckboxes = getFields(settings.userHiers);
settings.limitTrailCheckboxes = getFields(settings.userHiers, "up");
settings.CHECKBOX_STATES_OVERWRITTEN = true;
await this.saveSettings();
}
this.VIEWS = [
{
Expand Down Expand Up @@ -52389,17 +52534,19 @@ class BCPlugin extends require$$0.Plugin {
return sortedTrails;
}
getLimitedTrailSub() {
const { limitTrailCheckboxStates, userHiers } = this.settings;
const { limitTrailCheckboxes, userHiers } = this.settings;
let subGraph;
if (Object.values(limitTrailCheckboxStates).every((val) => val)) {
if (getFields(userHiers).every((field) => limitTrailCheckboxes.includes(field))) {
subGraph = getSubInDirs(this.mainG, "up", "down");
}
else {
const positiveFields = Object.keys(limitTrailCheckboxStates).filter((field) => limitTrailCheckboxStates[field]);
const oppFields = positiveFields
const oppFields = limitTrailCheckboxes
.map((field) => getOppFields(userHiers, field)[0])
.filter((field) => field !== undefined);
subGraph = getSubForFields(this.mainG, [...positiveFields, ...oppFields]);
subGraph = getSubForFields(this.mainG, [
...limitTrailCheckboxes,
...oppFields,
]);
}
const closed = getReflexiveClosure(subGraph, userHiers);
return getSubInDirs(closed, "up");
Expand Down
Loading

0 comments on commit 389b558

Please sign in to comment.