Skip to content

Commit

Permalink
fix regression
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Apr 18, 2024
1 parent d8c740d commit 1bde09d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 61 deletions.
3 changes: 2 additions & 1 deletion src/lib/components/partials/editor.block-options.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
}
const loadBlockOptions = async (key: string) => {
console.log('loading block options', key)
await $MRP?.loader?.loadComponentOptions(key)
const {options:_options, optionsConfig:config} = await $MRP?.loader?.loadComponentOptions(key)
options.set(_options)
Expand Down Expand Up @@ -80,7 +81,7 @@
<Sheet.Description>
{#if $loaded}
<ul>
{#each Object.keys($optionsConfig) as optionKey}
{#each Object.keys($optionsConfig || {}) as optionKey}
{#if inputType(optionKey) === 'text'}
<FormInput>
<svelte:fragment slot="label">
Expand Down
109 changes: 56 additions & 53 deletions src/lib/core/MRPBlockLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,30 @@ export class BlockLoader extends MRPData {
await this.loadAllComponents().catch(BlockLoader.errorHandler)
}

async loadComponentOptions(name: string): Promise<ComponentOptions> {
this.setComponentOptions(name)
return this._options[name] || {}
async loadComponentOptions(key: string): Promise<ComponentOptions> {
const options = await this.getComponentOptionConfig(key) || {}
this.setComponentOptions(key, options)
return this._options[key] || {}
}

async getComponentOptionConfig(name: string): Promise<{optionsConfig: any, defaultOptions: any}> {
async getComponentOptionConfig(key: string): Promise<{optionsConfig: any, defaultOptions: any}> {
const type = this.getType(key)
console.log('getComponentOptionConfig', key, type)
return import(
`../components/blocks/${this.isRepeatable(name)
`../components/blocks/${this.isRepeatable(key)
? 'repeatable'
: 'unique'
}/${this.getType(name)}/${this.getType(name)}.options.ts`)
}/${type}/${type}.options.ts`)
.catch(BlockLoader.errorHandler) || {}
}

async setComponentOptions(name: string){
const currentOptions = this.config.event.blocks[name]?.options || {}
const loadedOptions = await this.getComponentOptionConfig(name)
if(!loadedOptions?.optionsConfig) loadedOptions.optionsConfig = {}
if(!loadedOptions?.defaultOptions) loadedOptions.optionsConfig = {}
const {optionsConfig, defaultOptions} = loadedOptions
this._options[name] = {optionsConfig, options: {...defaultOptions, ...currentOptions}}
this.config.event.blocks[name].options = this._options[name].options
setComponentOptions(key: string, options: {optionsConfig: any, defaultOptions: any}){
const currentOptions = this.config.event.blocks[key]?.options || {}
if(!options?.optionsConfig) options.optionsConfig = {}
if(!options?.defaultOptions) options.optionsConfig = {}
const {optionsConfig, defaultOptions} = options
this._options[key] = {optionsConfig, options: {...defaultOptions, ...currentOptions}}
this.config.event.blocks[key].options = this._options[key].options
}

async loadAllComponents(){
Expand All @@ -69,58 +71,62 @@ export class BlockLoader extends MRPData {
}

async loadUniqueComponents(){
for (let name in this.config.event.blocks){
if(this._components?.[name] !== undefined) continue;
if(this.config.event.blocks[name].custom === true) continue;
await this.loadUniqueComponent(name).catch(BlockLoader.errorHandler)
for (let key in this.config.event.blocks){
if(this._components?.[key] !== undefined) continue;
if(this.config.event.blocks[key].custom === true) continue;
await this.loadUniqueComponent(key).catch(BlockLoader.errorHandler)
}
}

async loadRepeatableComponents(){
let repeatables: string[] | undefined
for (let name in this.config.event.blocks){
if(!name?.includes(':') || !this.config.event.blocks[name]?.repeatable) continue
if(!repeatables) repeatables = this._repeatable.get(name.split(':')[0]) || []
await this.loadRepeatableComponent(name).catch(BlockLoader.errorHandler)
for (let key in this.config.event.blocks){
if(!key?.includes(':') || !this.config.event.blocks[key]?.repeatable) continue
await this.loadRepeatableComponent(key).catch(BlockLoader.errorHandler)
}
}

setupRepeatable = (key: string) => {
const type = this.getType(key)
let repeatables = this.getRepeatablesByType(type)
this._repeatable.set(type, [...repeatables, key])
}

getRepeatablesByType(type: string): string[] {
return this._repeatable.get(type) || []
}

async loadRemoteComponents(){
for(let name in this.config.event.blocks){
const block = this.config.event.blocks[name]
for(let key in this.config.event.blocks){
this.setupRepeatable(key)
const block = this.config.event.blocks[key]
if(!block.enabled || block.custom === false)
continue;
if(block.customType === 'url')
await this.loadRemoteComponent(name, block.customUrl).catch(BlockLoader.errorHandler)
await this.loadRemoteComponent(key, block.customUrl).catch(BlockLoader.errorHandler)

}
}

uniqueComponentPath(name: string): string {
return `../components/blocks/unique/${this._componentsDef[name]}/index.svelte`
}

async addComponent(name: string, order: number, url?: string): Promise<string> {
async addComponent(type: string, order: number, url?: string): Promise<string> {
const totalBlocks = Object.keys(this.config.event.blocks).length
let repeatable = false
let id: string = "";
let key: string = "";
if(!url) {
let repeatables = this._repeatable.get(name)
if(!repeatables) repeatables = []
id = `${name}:${repeatables?.length}`
this._repeatable.set(name, [...repeatables, id])
let repeatables = this.getRepeatablesByType(type)
key = `${type}:${repeatables?.length}`
this.setupRepeatable(key)
repeatable = true
await this.loadRepeatableComponent(name).catch(BlockLoader.errorHandler)
await this.loadRepeatableComponent(type).catch(BlockLoader.errorHandler)
}
else {
await this.loadRemoteComponent(name, url).catch(BlockLoader.errorHandler)
await this.loadRemoteComponent(type, url).catch(BlockLoader.errorHandler)
}
if(typeof order !== 'undefined')
this.config.event.insertBlockAt(id, { enabled: true, repeatable, order, options: {} })
this.config.event.insertBlockAt(key, { enabled: true, repeatable, order, options: {} })
else
this.config.event.setBlock(id, { enabled: true, repeatable, order: totalBlocks, options: {} })
console.log(`${name} component`, this?._components?.[name])
this.config.event.setBlock(key, { enabled: true, repeatable, order: totalBlocks, options: {} })
// console.log(`${name} component`, this?._components?.[name])
this.changed()
return id
return key
}

private async loadUniqueComponent(key: string): Promise<NodeModule | undefined> {
Expand All @@ -136,26 +142,23 @@ export class BlockLoader extends MRPData {
}

private async loadRepeatableComponent(key: string): Promise<NodeModule | undefined> {
const name = key.split(':')[0]
let $component = this?._components?.[name]
console.log(`${name} component`, $component)
const type = this.getType(key)
let $component = this?._components?.[type]
if(!$component){
$component = await import(`../components/blocks/repeatable/${name}/${name}.svelte`).catch(BlockLoader.errorHandler)
console.log(`${name} component`, $component)
this._components[name] = $component?.default? $component.default: $component
console.log(`${name} component`, this?._components?.[name])
$component = await import(`../components/blocks/repeatable/${type}/${type}.svelte`).catch(BlockLoader.errorHandler)
this._components[type] = $component?.default? $component.default: $component
this.changed()
}
await this.loadComponentOptions(key)
return $component
}

private async loadRemoteComponent(name: string, url: string): Promise<NodeModule | undefined> {
const block = this.config.event.blocks?.[name]
private async loadRemoteComponent(key: string, url: string): Promise<NodeModule | undefined> {
const block = this.config.event.blocks?.[key]
let $component;
if(block){
$component = await import(/* @vite-ignore */ url).catch(BlockLoader.errorHandler)
this._components[name] = $component?.default? $component.default: $component
this._components[key] = $component?.default? $component.default: $component
}
return $component
}
Expand Down
14 changes: 7 additions & 7 deletions src/lib/core/kinds/app-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,17 @@ export class AppConfig extends NDKEvent {
}

discardChanges(){
console.log(`hash of unchanged config`, objectHash(this._configUnchanged, { algorithm: 'sha1' }))
console.log('discarding changes', this.deterministicHash)
// console.log(`hash of unchanged config`, objectHash(this._configUnchanged, { algorithm: 'sha1' }))
// console.log('discarding changes', this.deterministicHash)
this.config = deepClone( this._configUnchanged )
this.configHash = this.configHashUnchanged as string;
console.log('hash after', this.deterministicHash)
// console.log('hash after', this.deterministicHash)
}

set config(config: ConfigObj) {
this._config = this.deepProxy(config, () => {
this.content = this.pack();
console.log('content packed', this.content)
// console.log('content packed', this.content)
});
this.setConfigHash(this?._configHashUnchanged? false: true)
}
Expand Down Expand Up @@ -204,7 +204,7 @@ export class AppConfig extends NDKEvent {
}
this.configHash = newHash;
this.changed = this.configHash !== this.configHashUnchanged;
console.log(this.changed ? 'changed' : 'unchanged', this.configHash, this.configHashUnchanged);
// console.log(this.changed ? 'changed' : 'unchanged', this.configHash, this.configHashUnchanged);
}

private deepProxy(obj: any, callback: () => void): any {
Expand Down Expand Up @@ -332,8 +332,8 @@ export class AppConfig extends NDKEvent {
}

toggleBlock(key: string) {
console.log(key, key, key)
console.log(key, this._config.blocks)
// console.log(key, key, key)
// console.log(key, this._config.blocks)
const currentEnabledState = this._config.blocks[key].enabled;
this.setBlockEnabled(key, !currentEnabledState);
}
Expand Down

0 comments on commit 1bde09d

Please sign in to comment.