Skip to content

Commit

Permalink
fix defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Apr 12, 2024
1 parent 113db3a commit c59936b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
16 changes: 13 additions & 3 deletions src/lib/core/MRPBlockLoader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { R } from "vitest/dist/reporters-P7C2ytIv.js"
import type { MRPConfig, ConfigBlocks } from "./MRPConfig"
import type { MRPConfig } from "./MRPConfig"
import type { MRPState } from "./MRP"
import { MRPData } from "./MRPData"
import { AppConfig } from "./kinds/app-config"

type Components = {
[key: string]: string | boolean
Expand Down Expand Up @@ -100,7 +99,18 @@ export class BlockLoader extends MRPData {
return this._config
}

get components(): Record<string, NodeModule> {
get sortedComponents(): NodeModule[] {
return this.config.event.getBlocksSorted().map( (name: string) => this._components[name])
}

get sortedKeys(): string[] {
return this.config.event.getBlocksSorted().map( (name: string) => this._components[name])
// return Object.keys(this.config.event.blocks).sort((a, b) => this.config.event.blocks[a].order - this.config.event.blocks[b].order)
}

get components(): NodeModule[] {
return this._components
// console.log(this?.config?.event?.getBlocksSorted())
// return this.config.event.getBlocksSorted().map( (name: string) => this._components[name])
}
}
33 changes: 30 additions & 3 deletions src/lib/core/kinds/app-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ const defaults = {
order: 0
},
'relay-feed': {
enabled: false,
enabled: true,
order: 1
},
'map': {
enabled: false,
enabled: true,
order: 2
}
}
Expand Down Expand Up @@ -224,7 +224,34 @@ export class AppConfig extends NDKEvent {
return this.blocks?.[key]
}

getBlocksOrder(): string[] {
getBlocksSorted(): string[] {
return Object.keys(this.blocks).sort((a, b) => this.getBlock(a).order - this.getBlock(b).order)
}

private shiftOrder(key: string, direction: 'up' | 'down'): void {
const block = this.config.blocks[key];
let targetOrder = block.order + (direction === 'up' ? 1 : -1);
for (let key in this.config.blocks) {
if (this.config.blocks[key].order === targetOrder) {
this.config.blocks[key].order = block.order;
block.order = targetOrder;
break;
}
}
}

shiftBlockUp(key: string): void {
const block = this.config.blocks[key];
const maxOrder = Object.keys(this.config.blocks).length - 1;
if (block.order < maxOrder) {
this.shiftOrder(key, 'up');
}
}

shiftBlockDown(key: string): void {
if (this.config.blocks[key].order > 0) {
this.shiftOrder(key, 'down');
}
}

}
6 changes: 4 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

<FollowsPresent />

{#if browser}
{#each Object.values( mrp?.loader?.components || {} ) as Component}


{#if browser && mrp?.loader?.sortedKeys?.length}
{#each mrp?.loader?.sortedComponents as Component}
{#if Component}
<svelte:component this={Component} />
{/if}
Expand Down

0 comments on commit c59936b

Please sign in to comment.