folding unfolding frontmatter..

This commit is contained in:
Marcell Mars 2021-06-08 12:39:07 +02:00
parent 0c7164b05e
commit b24f80e496
7 changed files with 361 additions and 236 deletions

View file

@ -60,7 +60,7 @@ button:focus {
/* custom part */ /* custom part */
form { .formgrid {
display: grid; display: grid;
grid-template-columns: auto 1fr; grid-template-columns: auto 1fr;
grid-gap: 1rem; grid-gap: 1rem;
@ -145,4 +145,17 @@ select {
margin-top: 0.5rem; margin-top: 0.5rem;
} }
.fmHidden {
display: none;
}
.fmShown {
display: grid;
grid-template-columns: auto 1fr;
grid-gap: 1rem;
}
.togglefold {
margin-top: 1rem;
margin-bottom: 1rem;
}

View file

@ -1,128 +1,194 @@
<script> <script>
import SpTitle from './SpTitle.svelte' import SpTitle from "./SpTitle.svelte";
import SpTiers from './SpTiers.svelte' import SpJournal from "./SpJournal.svelte";
import SpKeys from './SpKeys.svelte' import SpTiers from "./SpTiers.svelte";
import SpCandidates from './SpCandidates.svelte' import SpKeys from "./SpKeys.svelte";
import { onMount } from 'svelte'; import SpCandidates from "./SpCandidates.svelte";
import { onMount } from "svelte";
let v = "loading..."; let v = "loading...";
let relpath = ""; let relpath = "";
let relpermalink = ""; let relpermalink = "";
let protocol = ""; let protocol = "";
let title = "Foo bar"; let title = "Foo bar";
let hases = []; let hases = [];
let hasesCandidates = []; let hasesCandidates = [];
let frontmatter = {} let frontmatter = {};
frontmatter['triad'] = []; frontmatter["triad"] = [];
frontmatter['ascriptions'] = []; frontmatter["ascriptions"] = [];
frontmatter["notsand"] = [];
function keyUp(e) { function keyUp(e) {
if (e.key == "Escape") { if (e.key == "Escape") {
window.history.back() window.history.back();
} }
} }
function diffArrr(arr, arrr) { function diffArrr(arr, arrr) {
let diff = new Set(arr) let diff = new Set(arr);
for (let e of arrr) { for (let e of arrr) {
diff.delete(e) diff.delete(e);
} }
return Array.from(diff) return Array.from(diff);
} }
function newHasTiers(arrgs) { function newHasTiers(arrgs) {
let [fmKeyType, index, oldFmKey, newFmKey] = arrgs.detail; let [fmKeyType, index, oldFmKey, newFmKey] = arrgs.detail;
frontmatter[fmKeyType][index] = {'fmKey': newFmKey, 'tiers': [], 'candidates': METASP[newFmKey].tiers} frontmatter[fmKeyType][index] = {
fmKey: newFmKey,
tiers: [],
candidates: METASP[newFmKey].tiers,
};
hasesCandidates = [...hasesCandidates, oldFmKey]; hasesCandidates = [...hasesCandidates, oldFmKey];
hasesCandidates = hasesCandidates.filter(h => h != newFmKey); hasesCandidates = hasesCandidates.filter((h) => h != newFmKey);
} }
function loadHugoPageMetadata() { function loadHugoPageMetadata() {
var el = document.createElement('script'); var el = document.createElement("script");
el.onload = ()=> { el.onload = () => {
v = repo.content; v = repo.content;
if (v.startsWith("\n")) { if (v.startsWith("\n")) {
v = v.substring(1) v = v.substring(1);
} }
relpath = repo.path; relpath = repo.path;
relpermalink = repo.relpermalink relpermalink = repo.relpermalink;
protocol = document.location.protocol.substring(0,4) protocol = document.location.protocol.substring(0, 4);
hases = Object.keys(METASP) hases = Object.keys(METASP);
Object.entries(repo.frontmatter).forEach(([fmKey,fmValue]) => Object.entries(repo.frontmatter).forEach(([fmKey, fmValue]) => {
{ let fmKeyType = "";
let fmKeyType = ''
if (fmKey.toLowerCase().startsWith("has_")) { if (fmKey.toLowerCase().startsWith("has_")) {
if (hases.includes(fmKey.substr(4))) { if (hases.includes(fmKey.substr(4))) {
fmKeyType = 'triad' fmKeyType = "triad";
fmKey = fmKey.substr(4) fmKey = fmKey.substr(4);
} }
} else if (hases.includes(fmKey.toLowerCase())) { } else if (hases.includes(fmKey.toLowerCase())) {
fmKeyType = 'ascriptions' fmKeyType = "ascriptions";
} else if (["abstract", "keywords"].includes(fmKey.toLowerCase())) {
fmKeyType = "journal";
} else { } else {
fmKeyType = 'notSandpoints' fmKeyType = "notsand";
} }
if (['triad', 'ascriptions'].includes(fmKeyType)) { if (["triad", "ascriptions"].includes(fmKeyType)) {
if (Array.isArray(fmValue)) { if (Array.isArray(fmValue)) {
let tiers = [] let tiers = [];
let candidates = [] let candidates = [];
METASP[fmKey].tiers.forEach(t => { METASP[fmKey].tiers.forEach((t) => {
if (fmValue.includes(t.file)) { if (fmValue.includes(t.file)) {
tiers.push(t) tiers.push(t);
} else { } else {
candidates.push(t) candidates.push(t);
} }
}) });
frontmatter[fmKeyType].push({'fmKey': fmKey, 'tiers': tiers, 'candidates': candidates}) frontmatter[fmKeyType].push({
fmKey: fmKey,
tiers: tiers,
candidates: candidates,
});
} }
} else if (fmKeyType == "journal") {
frontmatter["journal"] || (frontmatter["journal"] = []);
frontmatter[fmKeyType].push({ fmKey: fmKey, fmValue: JSON.stringify(fmValue) });
} }
} });
) hasesCandidates = diffArrr(hases, [
hasesCandidates = diffArrr(hases, [...frontmatter['triad'].map(t => { return t.fmKey }), ...frontmatter['ascriptions'].map(t => { return t.fmKey })]) ...frontmatter["triad"].map((t) => {
return t.fmKey;
}),
...frontmatter["ascriptions"].map((t) => {
return t.fmKey;
}),
]);
let tkey = Object.keys(repo.frontmatter).filter(t => t.toLowerCase() == "title")[0] let tkey = Object.keys(repo.frontmatter).filter((t) => t.toLowerCase() == "title")[0];
title = repo.frontmatter[tkey] title = repo.frontmatter[tkey];
} };
el.src = `../js/repo/${location.hash.substring(1)}.js` el.src = `../js/repo/${location.hash.substring(1)}.js`;
document.body.appendChild(el) document.body.appendChild(el);
} }
function addToCandidatesRemoveFromTiers(arrgs) { function addToCandidatesRemoveFromTiers(arrgs) {
let [fmKeyType, index, fmKey, tier] = arrgs.detail; let [fmKeyType, index, fmKey, tier] = arrgs.detail;
frontmatter[fmKeyType][index]['fmKey'] = fmKey; frontmatter[fmKeyType][index]["fmKey"] = fmKey;
frontmatter[fmKeyType][index]['tiers'] = frontmatter[fmKeyType][index]['tiers'].filter(t => t !== tier); frontmatter[fmKeyType][index]["tiers"] = frontmatter[fmKeyType][index]["tiers"].filter(
frontmatter[fmKeyType][index]['candidates'] = [...frontmatter[fmKeyType][index]['candidates'], tier] (t) => t !== tier
);
frontmatter[fmKeyType][index]["candidates"] = [...frontmatter[fmKeyType][index]["candidates"], tier];
} }
function addToTiersRemoveFromCandidates(arrgs) { function addToTiersRemoveFromCandidates(arrgs) {
let [fmKeyType, index, fmKey, value] = arrgs.detail; let [fmKeyType, index, , value] = arrgs.detail;
const tier = frontmatter[fmKeyType][index]['candidates'].filter(t => t.file == value) const tier = frontmatter[fmKeyType][index]["candidates"].filter((t) => t.file == value);
if (tier.length > 0) { if (tier.length > 0) {
frontmatter[fmKeyType][index]['tiers'] = [...frontmatter[fmKeyType][index]['tiers'], tier[0]] frontmatter[fmKeyType][index]["tiers"] = [...frontmatter[fmKeyType][index]["tiers"], tier[0]];
frontmatter[fmKeyType][index]['candidates'] = frontmatter[fmKeyType][index]['candidates'].filter(t=> t != tier[0]) frontmatter[fmKeyType][index]["candidates"] = frontmatter[fmKeyType][index]["candidates"].filter(
(t) => t != tier[0]
);
} }
} }
onMount(()=> {loadHugoPageMetadata()}) function toggleFold() {
let e = document.getElementById("frontmatter");
if (e && e.classList.contains("fmHidden")) {
e.classList.remove("fmHidden");
e.classList.add("fmShown");
} else if (e && e.classList.contains("fmShown")) {
e.classList.remove("fmShown");
e.classList.add("fmHidden");
}
}
onMount(() => {
loadHugoPageMetadata();
console.dir(frontmatter);
});
</script> </script>
<svelte:window on:keyup={keyUp}/> <svelte:window on:keyup={keyUp} />
<main id="sandpoints"> <main id="sandpoints">
<form> <form>
<SpTitle relpath={relpath} title={title} /> <div class="formgrid">
<SpTitle {relpath} {title} />
</div>
<div on:click={toggleFold} class="togglefold">[fold/unfold frontmatter]</div>
<span id="frontmatter" class="fmHidden">
{#each Object.entries(frontmatter) as fmItems} {#each Object.entries(frontmatter) as fmItems}
{#if ['ascriptions', 'triad'].includes(fmItems[0])}
{#each fmItems[1] as fmItem, i} {#each fmItems[1] as fmItem, i}
<SpKeys index={i} fmKeyType={fmItems[0]} fmItem={fmItem} hases={hases} hasesCandidates={hasesCandidates} on:hasTiersSelected={newHasTiers} /> {#if ["ascriptions", "triad"].includes(fmItems[0])}
<SpTiers on:addToCandidatesRemoveFromTiers={addToCandidatesRemoveFromTiers} index={i} fmKeyType={fmItems[0]} fmItem={fmItem} /> <SpKeys
<SpCandidates on:addToTiersRemoveFromCandidates={addToTiersRemoveFromCandidates} index={i} fmKeyType={fmItems[0]} fmItem={fmItem} /> index={i}
{/each} fmKeyType={fmItems[0]}
{fmItem}
{hases}
{hasesCandidates}
on:hasTiersSelected={newHasTiers}
/>
<SpTiers
on:addToCandidatesRemoveFromTiers={addToCandidatesRemoveFromTiers}
index={i}
fmKeyType={fmItems[0]}
{fmItem}
/>
<SpCandidates
on:addToTiersRemoveFromCandidates={addToTiersRemoveFromCandidates}
index={i}
fmKeyType={fmItems[0]}
{fmItem}
/>
{:else if fmItems[0] == "journal"}
<label for={fmItem.fmKey}>{fmItem.fmKey}:</label>
<input type="text" name="journal.fmKey" value={fmItem.fmValue} />
{/if} {/if}
{/each} {/each}
{/each}
</span>
<div class="formgrid">
<label for="content">Content:</label> <label for="content">Content:</label>
<textarea bind:value={v} oninput='this.style.height = "";this.style.height = this.scrollHeight + 3 + "px"'></textarea> <textarea
{#if document.location.protocol.substring(0,4) != "file"} bind:value={v}
oninput="this.style.height = '';this.style.height = this.scrollHeight + 3 + 'px'"
/>
{#if document.location.protocol.substring(0, 4) != "file"}
<label for="publish">Publish</label> <label for="publish">Publish</label>
<input type="checkbox" id="publish" name="publish" /> <input type="checkbox" id="publish" name="publish" />
{/if} {/if}
@ -132,5 +198,6 @@
<input type="hidden" name="relpath" bind:value={relpath} /> <input type="hidden" name="relpath" bind:value={relpath} />
<input type="hidden" name="protocol" bind:value={protocol} /> <input type="hidden" name="protocol" bind:value={protocol} />
<button id="sandpointsButton">COMMIT/SAVE</button> <button id="sandpointsButton">COMMIT/SAVE</button>
</div>
</form> </form>
</main> </main>

View file

@ -1,6 +1,6 @@
<script> <script>
import { createEventDispatcher } from 'svelte'; import { createEventDispatcher } from "svelte";
import { afterUpdate } from 'svelte'; import { afterUpdate } from "svelte";
export let index; export let index;
export let fmItem; export let fmItem;
export let fmKeyType; export let fmKeyType;
@ -9,23 +9,34 @@
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
afterUpdate(() => { afterUpdate(() => {
let addnew = document.getElementById('addnew') let addnew = document.getElementById("addnew");
if (addnew) { if (addnew) {
addnew.selected = 'selected'; addnew.selected = "selected";
} }
}); });
$: { $: {
dispatch("addToTiersRemoveFromCandidates", [fmKeyType, index, fmItem.fmKey, value]) dispatch("addToTiersRemoveFromCandidates", [
fmKeyType,
index,
fmItem.fmKey,
value,
]);
value = 1; value = 1;
} }
</script> </script>
{#if fmItem.candidates && fmItem.candidates.length > 0} {#if fmItem.candidates && fmItem.candidates.length > 0}
<select class="selecttier" bind:value> <select class="selecttier" bind:value>
<option id="addnew" value="1" disabled selected>Add new...</option> <option id="addnew" value="1" disabled selected>Add new...</option>
{#each fmItem.candidates as tier (tier.file)} {#each fmItem.candidates as tier (tier.file)}
<option title="{tier.title}" value={tier.file}>{tier.file} {#if tier.title.length < 70}({tier.title}){:else}({tier.title.slice(0, 68) + '…'}){/if}</option> <option title={tier.title} value={tier.file}
>{tier.file}
{#if tier.title.length < 70}({tier.title}){:else}({tier.title.slice(
0,
68
) + "…"}){/if}</option
>
{/each} {/each}
</select> </select>
{/if} {/if}

View file

@ -1,6 +1,6 @@
<script> <script>
import { createEventDispatcher } from 'svelte'; import { createEventDispatcher } from "svelte";
import { onMount } from 'svelte'; import { onMount } from "svelte";
export let fmItem; export let fmItem;
export let fmKeyType; export let fmKeyType;
export let index; export let index;
@ -9,28 +9,47 @@
let value; let value;
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
function optselName(has) { function optselName(has) {
return `_${has}_${fmKeyType}` return `_${has}_${fmKeyType}`;
} }
onMount(()=> { onMount(() => {
const selectEvent = document.getElementById(`${fmKeyType}_${fmItem.fmKey}`) const selectEvent = document.getElementById(
`${fmKeyType}_${fmItem.fmKey}`
);
if (selectEvent) { if (selectEvent) {
selectEvent.addEventListener('change', (e) => { selectEvent.addEventListener("change", (e) => {
dispatch("hasTiersSelected", [fmKeyType, index, fmItem.fmKey, e.target.value.split("_")[1]]);}) dispatch("hasTiersSelected", [
fmKeyType,
index,
fmItem.fmKey,
e.target.value.split("_")[1],
]);
});
} }
}) });
</script> </script>
<select id="{fmKeyType}_{fmItem.fmKey}" bind:value> <select id="{fmKeyType}_{fmItem.fmKey}" bind:value>
{#each hases as has} {#each hases as has}
{#if has == fmItem.fmKey} {#if has == fmItem.fmKey}
<option id="{optselName(has)}" class="hasoption" selected title="{has}" value={optselName(has)}>{'triad' == fmKeyType ? "Has ":""}{has}:</option> <option
id={optselName(has)}
class="hasoption"
selected
title={has}
value={optselName(has)}
>{"triad" == fmKeyType ? "Has " : ""}{has}:</option
>
{/if} {/if}
{/each} {/each}
{#each hasesCandidates as has} {#each hasesCandidates as has}
<option id="{optselName(has)}" class="hasoption" title="{has}" value={optselName(has)}>{'triad' == fmKeyType ? "Has ":""}{has}:</option> <option
id={optselName(has)}
class="hasoption"
title={has}
value={optselName(has)}
>{"triad" == fmKeyType ? "Has " : ""}{has}:</option
>
{/each} {/each}
</select> </select>

View file

@ -1,39 +1,54 @@
<script> <script>
import { createEventDispatcher } from 'svelte'; import { createEventDispatcher } from "svelte";
export let index; export let index;
export let fmItem; export let fmItem;
export let fmKeyType; export let fmKeyType;
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
function removeTier(tier) { function removeTier(tier) {
dispatch("addToCandidatesRemoveFromTiers", [fmKeyType, index, fmItem.fmKey, tier]) dispatch("addToCandidatesRemoveFromTiers", [fmKeyType, index, fmItem.fmKey, tier]);
} }
function swapTier(tier, n) { function swapTier(tier, n) {
fmItem.tiers.some((t, i)=> { fmItem.tiers.some((t, i) => {
if (t==tier) { if (t == tier) {
fmItem.tiers[i] = fmItem.tiers[i+n] fmItem.tiers[i] = fmItem.tiers[i + n];
fmItem.tiers[i+n] = tier fmItem.tiers[i + n] = tier;
return true return true;
} }
}) });
} }
</script> </script>
{#if fmItem.tiers && fmItem.tiers.length >0} {#if fmItem.tiers && fmItem.tiers.length > 0}
<div class="tierbox"> <div class="tierbox">
{#each fmItem.tiers as tier, i (tier.file)} {#each fmItem.tiers as tier, i (tier.file)}
<div class="tierline"> <div class="tierline">
<div class="filetitle"> <div class="filetitle">
<input readonly type="text" name="{'triad' == fmKeyType ? "has_":"" }{fmItem.fmKey}[]" class="hasinput" value="{tier.file}"><span class="tiertitle">({tier.title})</span> <input
readonly
type="text"
name="{'triad' == fmKeyType ? 'has_' : ''}{fmItem.fmKey}[]"
class="hasinput"
value={tier.file}
/><span class="tiertitle">({tier.title})</span>
</div> </div>
<div class="buttons"> <div class="buttons">
<button type="button" class="buttondown" on:click="{swapTier(tier, 1)}" disabled={i+1 == fmItem.tiers.length ? true:false}>⇓</button> <button
<button type="button" class="buttonup" on:click="{swapTier(tier, -1)}" disabled={i == 0 ? true:false}>⇑</button> type="button"
<button type="button" on:click="{removeTier(tier)}">×</button> class="buttondown"
on:click={swapTier(tier, 1)}
disabled={i + 1 == fmItem.tiers.length ? true : false}>⇓</button
>
<button
type="button"
class="buttonup"
on:click={swapTier(tier, -1)}
disabled={i == 0 ? true : false}>⇑</button
>
<button type="button" on:click={removeTier(tier)}>×</button>
</div> </div>
</div> </div>
{/each} {/each}
</div> </div>
{/if} {/if}

View file

@ -4,6 +4,6 @@
</script> </script>
<label for="relpath">File path:</label> <label for="relpath">File path:</label>
<input type="text" name="relpath" value="{relpath}"> <input type="text" name="relpath" value={relpath} />
<label for="title">Title:</label> <label for="title">Title:</label>
<input type="text" name="title" value="{title}"> <input type="text" name="title" value={title} />