Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function editor bugfixes #30

Merged
merged 6 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { css, LitElement, type PropertyValues, unsafeCSS } from 'lit'
import { css, LitElement, unsafeCSS } from 'lit'
import { property, query, state } from 'lit/decorators.js'
import style_less from './editor-tile.less?inline'
import { Essentiality, type IFunctionData, type IRegulationData, Monotonicity } from '../../../util/data-interfaces'
Expand Down Expand Up @@ -52,10 +52,6 @@ export abstract class EditorTile extends LitElement {
this.aceEditor.renderer.attachToShadowRoot()
}

protected updated (_changedProperties: PropertyValues): void {
super.updated(_changedProperties)
}

protected updateEditor (name: string, func: string): void {
if (this.nameField !== undefined) {
this.nameField.value = name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ace, { type Ace } from 'ace-builds'
import langTools from 'ace-builds/src-noconflict/ext-language_tools'
import 'ace-builds/esm-resolver'
import { EditorTile } from './editor-tile'
import { functionDebounceTimer } from '../../../util/config'

library.add(faTrash, faMagnifyingGlass)

Expand Down Expand Up @@ -38,6 +39,7 @@ export class FunctionTile extends EditorTile {
})))
}
})
this.aceEditor.setOption('placeholder', '$f_' + this.functions[this.index].id + '(...)')
}

protected firstUpdated (_changedProperties: PropertyValues): void {
Expand All @@ -54,8 +56,7 @@ export class FunctionTile extends EditorTile {
bubbles: true,
composed: true
}))
},
300
}, functionDebounceTimer
)

functionUpdated = debounce(() => {
Expand All @@ -67,8 +68,7 @@ export class FunctionTile extends EditorTile {
bubbles: true,
composed: true
}))
},
300
}, functionDebounceTimer
)

async removeVariable (): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ace, { type Ace } from 'ace-builds'
import langTools from 'ace-builds/src-noconflict/ext-language_tools'
import 'ace-builds/esm-resolver'
import { EditorTile } from './editor-tile'
import { functionDebounceTimer } from '../../../util/config'
library.add(faTrash, faMagnifyingGlass)

@customElement('variable-tile')
Expand Down Expand Up @@ -40,6 +41,7 @@ export class VariableTile extends EditorTile {
'constant.language': this.variables.map(v => v.id).join('|'),
'support.function.dom': this.functions.map(f => f.id).join('|')
})
this.aceEditor.setOption('placeholder', '$f_' + this.variables[this.index].id + '(...)')
}

private getVariables (): IVariableData[] {
Expand All @@ -55,8 +57,7 @@ export class VariableTile extends EditorTile {
bubbles: true,
composed: true
}))
},
300
}, functionDebounceTimer
)

functionUpdated = debounce(() => {
Expand All @@ -68,8 +69,7 @@ export class VariableTile extends EditorTile {
bubbles: true,
composed: true
}))
},
300
}, functionDebounceTimer
)

toggleEssentiality (regulation: IRegulationData): void {
Expand Down
62 changes: 28 additions & 34 deletions src/html/component/root-component/root-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,18 @@ export default class RootComponent extends LitElement {
this.data = this.data.copy({ functions })
}

private saveData (variables: IVariableData[], regulations: IRegulationData[], layout: ILayoutData): void {
// save variable/regulation/layout data, leave functions as is

// sort nodes to keep alphabetical order in lists
private saveVariables (variables: IVariableData[]): void {
variables.sort((a, b) => (a.id > b.id ? 1 : -1))
this.data = this.data.copy({ variables })
}

private saveRegulations (regulations: IRegulationData[]): void {
regulations.sort((a, b) => (a.source + a.target > b.source + b.target ? 1 : -1))
this.data = this.data.copy({ regulations })
}

this.data = ContentData.create({
variables,
regulations,
layout,
functions: this.data.functions
})
private saveLayout (layout: ILayoutData): void {
this.data = this.data.copy({ layout })
}

renameVariable (event: Event): void {
Expand All @@ -140,7 +139,7 @@ export default class RootComponent extends LitElement {
id: data.id,
name: data.name
}
this.saveData(variables, this.data.regulations, this.data.layout)
this.saveVariables(variables)
}

private addVariable (event: Event): void {
Expand All @@ -160,7 +159,7 @@ export default class RootComponent extends LitElement {
name: data.name,
function: ''
})
this.saveData(variables, this.data.regulations, this.data.layout)
this.saveVariables(variables)
}

private addRegulation (event: Event): void {
Expand All @@ -177,7 +176,7 @@ export default class RootComponent extends LitElement {
essential: data.essential,
monotonicity: data.sign
})
this.saveData(this.data.variables, regulations, this.data.layout)
this.saveRegulations(regulations)
}

private async removeVariable (event: Event): Promise<void> {
Expand All @@ -187,16 +186,14 @@ export default class RootComponent extends LitElement {
}

#onVariableRemoved (data: VariableData): void {
this.saveData(
this.data.variables.filter((variable) => variable.id !== data.id),
this.data.regulations,
this.data.layout
this.saveVariables(
this.data.variables.filter((variable) => variable.id !== data.id)
)
}

private adjustRegEditor (): void {
const visible = this.visibleTabs()
if (window.outerWidth <= 800 || visible.includes(this.tabs[0])) return
if (window.outerWidth <= 800 || !visible.includes(this.tabs[0])) return
window.dispatchEvent(new CustomEvent('adjust-graph', {
detail: {
tabCount: visible.length
Expand Down Expand Up @@ -224,11 +221,12 @@ export default class RootComponent extends LitElement {

#onNodePositionChanged (data: LayoutNodeData): void {
// TODO: add support for layouts
this.data.layout.set(data.variable, {
const layout = new Map(this.data.layout)
layout.set(data.variable, {
x: data.px,
y: data.py
})
this.saveData(this.data.variables, this.data.regulations, this.data.layout)
this.saveLayout(layout)
}

private setVariableId (event: Event): void {
Expand Down Expand Up @@ -256,7 +254,7 @@ export default class RootComponent extends LitElement {
regulations[index].target = data.new_id
}
})
this.saveData(variables, this.data.regulations, this.data.layout)
this.saveVariables(variables)

// TODO: this refresh is a temporary solution to get potentially modified update function expressions
aeonState.model.refreshVariables()
Expand All @@ -275,7 +273,7 @@ export default class RootComponent extends LitElement {
...regulations[index],
essential: data.essential
}
this.saveData(this.data.variables, regulations, this.data.layout)
this.saveRegulations(regulations)
}

private toggleRegulationMonotonicity (event: Event): void {
Expand All @@ -291,7 +289,7 @@ export default class RootComponent extends LitElement {
...regulations[index],
monotonicity: data.sign
}
this.saveData(this.data.variables, regulations, this.data.layout)
this.saveRegulations(regulations)
}

private setVariableFunction (event: Event): void {
Expand All @@ -307,7 +305,7 @@ export default class RootComponent extends LitElement {
...variables[variableIndex],
function: data.update_fn
}
this.saveData(variables, this.data.regulations, this.data.layout)
this.saveVariables(variables)
}

private async removeRegulation (event: Event): Promise<void> {
Expand All @@ -317,18 +315,14 @@ export default class RootComponent extends LitElement {
}

#onRegulationRemoved (data: RegulationData): void {
this.saveData(
this.data.variables,
this.data.regulations.filter((regulation) => regulation.source !== data.regulator || regulation.target !== data.target),
this.data.layout
this.saveRegulations(
this.data.regulations.filter((regulation) => regulation.source !== data.regulator || regulation.target !== data.target)
)
}

#onVariablesRefreshed (variables: VariableData[]): void {
this.saveData(
variables.map(v => { return { ...v, function: v.update_fn } }),
this.data.regulations,
this.data.layout
this.saveVariables(
variables.map(v => { return { ...v, function: v.update_fn } })
)
}

Expand All @@ -337,7 +331,7 @@ export default class RootComponent extends LitElement {
layoutNodes.forEach(layoutNode => {
layout.set(layoutNode.variable, { x: layoutNode.px, y: layoutNode.py })
})
this.saveData(this.data.variables, this.data.regulations, layout)
this.saveLayout(layout)
}

#onRegulationsRefreshed (regulations: RegulationData[]): void {
Expand All @@ -350,7 +344,7 @@ export default class RootComponent extends LitElement {
monotonicity: data.sign
}
})
this.saveData(this.data.variables, regs, this.data.layout)
this.saveRegulations(regs)
}

async loadDummy (): Promise<void> {
Expand Down
11 changes: 9 additions & 2 deletions src/html/component/undo-redo/undo-redo.less
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@
.uk-button {
background: rgb(34, 34, 34);
}


}

@media (prefers-color-scheme: light) {
.uk-button {
background: rgb(222, 222, 222);
}

.uk-button:hover {
background: rgb(200, 200, 200);
}


.fa-arrow-left, .fa-arrow-right {
color: black;
}
}
4 changes: 2 additions & 2 deletions src/html/component/undo-redo/undo-redo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export default class UndoRedo extends LitElement {
render (): TemplateResult {
return html`
<div class="undo-redo uk-flex-nowrap">
<button class="uk-button uk-button-secondary uk-button-small"
<button class="uk-button uk-button-secondary uk-button-small ${!this.canUndo ? 'disabled' : ''}"
@click=${aeonState.undoStack.undo} ?disabled=${!this.canUndo}>${icon(faArrowLeft).node}</button>
<button class="uk-button uk-button-secondary uk-button-small"
<button class="uk-button uk-button-secondary uk-button-small ${!this.canRedo ? 'disabled' : ''}"
@click=${aeonState.undoStack.redo} ?disabled=${!this.canRedo}>${icon(faArrowRight).node}</button>
</div>
`
Expand Down
2 changes: 2 additions & 0 deletions src/html/util/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ export const tabList: TabData[] = [
icon: 'a'
})
]

export const functionDebounceTimer = 1000
Loading