Skip to content

Commit

Permalink
Add an optional layout data argument to addVariable that is applied…
Browse files Browse the repository at this point in the history
… within the same action.
  • Loading branch information
daemontus committed Jan 22, 2024
1 parent acc8548 commit b03a8a0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src-tauri/src/app/state/editor/_state_editor_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl EditorSession {

while let Some(event) = to_perform.pop() {
let event_path = event.path.iter().map(|it| it.as_str()).collect::<Vec<_>>();
debug!("Executing event: `{:?}`.", event_path);
let result = match self.perform_event(&event, &event_path) {
Ok(result) => result,
Err(error) => {
Expand Down Expand Up @@ -107,6 +108,9 @@ impl EditorSession {
perform.push(p);
reverse.push(r);
}
// Obviously, the "reverse" events need to be execute in the opposite order
// compared to the "perform" events.
reverse.reverse();
let perform = UserAction { events: perform };
let reverse = UserAction { events: reverse };
if !self.undo_stack.do_action(perform, reverse) {
Expand Down
38 changes: 35 additions & 3 deletions src/aeon_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ interface AeonState {
/** VariableData for a newly created variable. */
variableCreated: Observable<VariableData>
/** Create new variable with given ID and name. If only ID string is given, it is used for both ID and name. */
addVariable: (varId: string, varName: string) => void
addVariable: (varId: string, varName?: string, position?: LayoutNodeDataPrototype | LayoutNodeDataPrototype[]) => void
/** VariableData of a removed variable. */
variableRemoved: Observable<VariableData>
/** Remove a variable with given ID. */
Expand Down Expand Up @@ -143,6 +143,16 @@ export interface LayoutNodeData {
py: number
}

/**
* The same as `LayoutNodeData`, but does not have a fixed variable ID because
* it is associated with a variable that does not have an ID yet.
*/
export interface LayoutNodeDataPrototype {
layout: string
px: number
py: number
}

/** A function that is notified when a state value changes. */
export type OnStateValue<T> = (value: T) => void

Expand Down Expand Up @@ -577,14 +587,36 @@ export const aeonState: AeonState = {
layoutRemoved: new Observable<LayoutData>(['model', 'layout', 'remove']),
nodePositionChanged: new Observable<LayoutNodeData>(['model', 'layout', 'update_position']),

addVariable (varId: string, varName: string = ''): void {
addVariable (
varId: string,
varName: string = '',
position: LayoutNodeDataPrototype | LayoutNodeDataPrototype[] = []
): void {
if (varName === '') {
varName = varId
}
aeonEvents.emitAction({
const actions = []
// First action creates the variable.
actions.push({
path: ['model', 'variable', 'add'],
payload: JSON.stringify({ id: varId, name: varName })
})
// Subsequent actions position it in one or more layouts.
if (!Array.isArray(position)) {
position = [position]
}
for (const p of position) {
actions.push({
path: ['model', 'layout', p.layout, 'update_position'],
payload: JSON.stringify({
layout: p.layout,
variable: varId,
px: p.px,
py: p.py
})
})
}
aeonEvents.emitAction(actions)
},
removeVariable (varId: string): void {
aeonEvents.emitAction({
Expand Down
8 changes: 6 additions & 2 deletions src/html/component/root-component/root-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ class RootComponent extends LitElement {

private addVariable (event: Event): void {
const details = (event as CustomEvent).detail
aeonState.model.addVariable(details.id, details.name)
aeonState.model.changeNodePosition(LAYOUT, details.id, details.position.x, details.position.y)
const position = {
layout: LAYOUT,
px: details.position.x,
py: details.position.y
}
aeonState.model.addVariable(details.id, details.name, position)
}

#onVariableCreated (data: VariableData): void {
Expand Down

0 comments on commit b03a8a0

Please sign in to comment.