2019-03-05 18:56:42 +00:00
|
|
|
function prepareForExport(doc, settings = settings) {
|
2019-03-04 18:17:27 +00:00
|
|
|
let states = JSON.parse(JSON.stringify(doc.states));
|
|
|
|
|
|
|
|
states.forEach(state => {
|
|
|
|
delete state.mouseOffsetX;
|
|
|
|
delete state.mouseOffsetY;
|
|
|
|
delete state.activeTime;
|
|
|
|
delete state.isActive;
|
|
|
|
});
|
|
|
|
|
|
|
|
let _connections = JSON.parse(JSON.stringify(doc.connections)).map((conn, index) => {
|
|
|
|
conn.type = doc.connections[index].constructor.name;
|
|
|
|
return conn;
|
|
|
|
});
|
|
|
|
|
|
|
|
_connections = _connections.map(conn => {
|
|
|
|
switch(conn.type) {
|
|
|
|
case 'Connection':
|
|
|
|
conn.stateA = conn.stateA.id;
|
|
|
|
conn.stateB = conn.stateB.id;
|
|
|
|
break;
|
|
|
|
case 'StartConnection':
|
|
|
|
conn.state = conn.state.id;
|
|
|
|
break;
|
|
|
|
case 'SelfConnection':
|
|
|
|
conn.state = conn.state.id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return conn;
|
|
|
|
});
|
|
|
|
|
|
|
|
const _doc = JSON.parse(JSON.stringify(doc));
|
|
|
|
_doc.states = states;
|
|
|
|
_doc.connections = _connections;
|
|
|
|
|
|
|
|
delete _doc.unsaved;
|
|
|
|
delete _doc.lastSavedHash;
|
|
|
|
delete _doc.element;
|
|
|
|
|
2019-03-05 18:56:42 +00:00
|
|
|
const _settings = JSON.parse(JSON.stringify(settings));
|
|
|
|
|
|
|
|
delete _settings.colors.getColor;
|
|
|
|
|
2019-03-04 18:17:27 +00:00
|
|
|
return {
|
|
|
|
document: _doc,
|
2019-03-05 18:56:42 +00:00
|
|
|
settings: _settings,
|
2019-03-04 18:17:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function exportToJson(doc, _settings = settings) {
|
|
|
|
return JSON.stringify(prepareForExport(doc, _settings));
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseFromJson(json) {
|
|
|
|
const data = JSON.parse(json);
|
|
|
|
|
|
|
|
const doc = Object.assign(new FSMDocument(''), data.document);
|
|
|
|
|
|
|
|
doc.states = doc.states.map(state => Object.assign(new State(0, 0), state));
|
|
|
|
doc.connections = doc.connections.map(connection => {
|
2019-03-05 19:31:53 +00:00
|
|
|
console.log(connection.type);
|
2019-03-04 18:17:27 +00:00
|
|
|
switch(connection.type) {
|
|
|
|
case 'Connection':
|
|
|
|
connection = Object.assign(new Connection(null, null), connection);
|
|
|
|
|
|
|
|
connection.stateA = doc.states.find(state => state.id === connection.stateA);
|
|
|
|
connection.stateB = doc.states.find(state => state.id === connection.stateB);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'SelfConnection':
|
|
|
|
connection = Object.assign(new SelfConnection(null, {x: 0, y: 0}), connection);
|
|
|
|
|
|
|
|
connection.state = doc.states.find(state => state.id === connection.state);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'StartConnection':
|
2019-03-05 19:31:53 +00:00
|
|
|
connection = Object.assign(new StartConnection(), connection);
|
2019-03-04 18:17:27 +00:00
|
|
|
|
|
|
|
connection.state = doc.states.find(state => state.id === connection.state);
|
2019-03-05 19:31:53 +00:00
|
|
|
console.log(connection.state);
|
2019-03-04 18:17:27 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete connection.type;
|
|
|
|
return connection;
|
|
|
|
});
|
|
|
|
|
|
|
|
doc.lastSavedHash = doc.hashCode();
|
|
|
|
|
|
|
|
return {
|
|
|
|
document: doc,
|
|
|
|
settings,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function importFromJson(json) {
|
|
|
|
const doc = parseFromJson(json);
|
|
|
|
|
2019-03-05 18:56:42 +00:00
|
|
|
addDocument(doc.document);
|
|
|
|
switchDocument(doc.document);
|
2019-03-04 18:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function exportToFile() {
|
2019-03-05 18:18:21 +00:00
|
|
|
if(activeDocument === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const document = documents[activeDocument];
|
|
|
|
const name = `${new Date(document.createdAt).toLocaleDateString()} - ${document.name || 'Unbenannt'}.fsm`;
|
|
|
|
const json = exportToJson(document);
|
2019-03-04 18:17:27 +00:00
|
|
|
downloadFile(name, json, 'application/json');
|
|
|
|
}
|
|
|
|
|
|
|
|
function downloadFile(name, content, type) {
|
|
|
|
const element = document.createElement('a');
|
|
|
|
|
2019-03-05 18:56:42 +00:00
|
|
|
element.setAttribute('href', `data:${type};base64,${btoa(content)}`);
|
2019-03-04 18:17:27 +00:00
|
|
|
element.setAttribute('download', name);
|
|
|
|
|
|
|
|
element.style.display = 'none';
|
|
|
|
document.body.appendChild(element);
|
|
|
|
|
|
|
|
element.click();
|
|
|
|
document.body.removeChild(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveToLocalStorage() {
|
|
|
|
if(activeDocument === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newDoc = documents[activeDocument];
|
|
|
|
const data = getLocalStorageEntries();
|
|
|
|
|
|
|
|
const index = data.findIndex(entry => entry.document.id === newDoc.id);
|
|
|
|
|
|
|
|
if(index !== -1) {
|
|
|
|
data[index] = {
|
|
|
|
document: newDoc,
|
|
|
|
settings,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
data.push({
|
|
|
|
document: newDoc,
|
|
|
|
settings,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const newData = data.map(entry => prepareForExport(entry.document, entry.settings));
|
|
|
|
|
|
|
|
localStorage.setItem('documents', JSON.stringify(newData));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLocalStorageEntries() {
|
|
|
|
const entries = JSON.parse(localStorage.getItem('documents'));
|
|
|
|
return !!entries ? entries.map(entry => parseFromJson(JSON.stringify(entry))) : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function openFromLocalStorage(id) {
|
|
|
|
const entry = getLocalStorageEntries().find(entry => entry.document.id === id);
|
|
|
|
addDocument(entry.document);
|
|
|
|
switchDocument(entry.document);
|
2019-03-05 18:56:42 +00:00
|
|
|
}
|