-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.js
More file actions
103 lines (89 loc) · 2.8 KB
/
Copy pathstate.js
File metadata and controls
103 lines (89 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// --- Single Source of Truth for Application State ---
// This module now has ZERO outgoing dependencies, which is critical.
export let MACHINE = {};
export let UNDO_STACK = [];
export let REDO_STACK = [];
export let CURRENT_MODE = 'addclick';
export let TRANS_FROM = null;
export let CURRENT_PRACTICE = null;
export const simState = {
steps: [],
index: 0,
timer: null,
};
// This will hold the renderAll function from renderer.js, passed in from main.js.
let renderFunction = () => { console.error("Render function not set!"); };
/**
* Injects the main render function into the state module to avoid circular dependencies.
* @param {function} fn The renderAll function.
*/
export function setRenderFunction(fn) {
renderFunction = fn;
}
// --- State Mutation Functions ---
/**
* Sets the machine object for the FA studio.
* NOTE: When called via StudioContext, rendering is handled there.
* When called internally (like during Undo/Redo), we call renderFunction().
* @param {object} newMachine The new machine state.
*/
export function setMachine(newMachine) {
MACHINE = {
type: 'DFA',
states: [],
transitions: [],
alphabet: [],
title: 'Untitled Automaton',
...newMachine
};
}
export function setCurrentMode(mode) {
CURRENT_MODE = mode;
}
export function setTransFrom(stateId) {
TRANS_FROM = stateId;
}
export function setCurrentPractice(practice) {
CURRENT_PRACTICE = practice;
}
export function pushUndo(updateUIFunction) {
UNDO_STACK.push(JSON.parse(JSON.stringify(MACHINE)));
REDO_STACK.length = 0;
updateUIFunction(); // This function (e.g., updateUndoRedoButtons) is passed in from the UI module.
}
export function doUndo(updateUIFunction) {
if (UNDO_STACK.length > 0) {
REDO_STACK.push(JSON.parse(JSON.stringify(MACHINE)));
// Note: setMachine is called, but render is handled locally here for undo/redo consistency
MACHINE = UNDO_STACK.pop();
renderFunction();
updateUIFunction();
}
}
export function doRedo(updateUIFunction) {
if (REDO_STACK.length > 0) {
UNDO_STACK.push(JSON.parse(JSON.stringify(MACHINE)));
// Note: setMachine is called, but render is handled locally here for undo/redo consistency
MACHINE = REDO_STACK.pop();
renderFunction();
updateUIFunction();
}
}
export function initializeState(updateUIFunction) {
setMachine({
type: 'DFA',
states: [],
transitions: [],
alphabet: []
});
UNDO_STACK = [];
REDO_STACK = [];
setCurrentMode('addclick');
setTransFrom(null);
setCurrentPractice(null);
simState.steps = [];
simState.index = 0;
if (simState.timer) clearTimeout(simState.timer);
simState.timer = null;
if (updateUIFunction) updateUIFunction();
}