Skip to content

Commit

Permalink
fix interval implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LaviniaStiliadou committed Jul 26, 2023
1 parent 8cdef3d commit 5aeb69f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import './modeler.css';
import React from 'react';
import { createRoot } from 'react-dom/client';
import ButtonToolbar from "./editor/ui/ButtonToolbar";
import { createNewDiagram, loadDiagram } from "./editor/util/IoUtilities";
import { createNewDiagram, loadDiagram, setAutoSaveInterval } from "./editor/util/IoUtilities";
import NotificationHandler from "./editor/ui/notifications/NotificationHandler";
import { createModeler, getModeler } from "./editor/ModelerHandler";
import { getPluginButtons, getTransformationButtons } from "./editor/plugin/PluginHandler";
Expand Down Expand Up @@ -335,6 +335,7 @@ export class QuantumWorkflowModeler extends HTMLElement {
// restart modeler to apply plugin config when shadow dom is rendered
requestAnimationFrame(() => {
this.startModeler();
setAutoSaveInterval();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default function EditorTab() {
editorConfig.setCamundaEndpoint(camundaEndpoint);
editorConfig.setTransformedWorkflowHandler(workflowHandler);
editorConfig.setAutoSaveFileOption(autoSaveFileOption);
modeler.get('eventBus').fire('autoSaveOptionChanged', { autoSaveFileOption });
editorConfig.setFileName(fileName);
editorConfig.setFileFormat(fileFormat);
editorConfig.setAutoSaveIntervalSize(autoSaveIntervalSize);
Expand Down
34 changes: 13 additions & 21 deletions components/bpmn-q/modeler-component/editor/util/IoUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,32 +232,24 @@ export function openInNewTab(workflowXml, fileName) {
};
}

export function resetAutosaveTimeout(autosaveTimeout, hasChanges, autoSaveFileOption = editorConfig.getAutoSaveFileOption()) {
clearTimeout(autosaveTimeout);

export function setAutoSaveInterval(autoSaveFileOption = editorConfig.getAutoSaveFileOption()) {
if (autoSaveFileOption === autoSaveFile.INTERVAL) {
setTimeout(() => autosave(hasChanges), editorConfig.getAutoSaveIntervalSize());
getModeler().autosaveIntervalId = setInterval(() => { saveFile(); }, editorConfig.getAutoSaveIntervalSize());
} else {
const timestamp = getTimestamp();
saveModelerAsLocalFile(getModeler(), `autosave_${timestamp}_${editorConfig.getFileName()}`, saveFileFormats.BPMN, false);
saveFile();
}
}

function autosave(hasChanges) {
if (hasChanges) {
// extract the xml and save it to a file
getModeler().saveXML({ format: true }, function (err, xml) {
if (!err) {
// Save the XML
console.log('Autosaved:', xml);
const timestamp = getTimestamp();
saveXmlAsLocalFile(xml, `autosave_${timestamp}_${editorConfig.getFileName()}`);
}
});
}

// Reset the timer after the autosave is completed
resetAutosaveTimeout();
export function saveFile() {
// extract the xml and save it to a file
getModeler().saveXML({ format: true }, function (err, xml) {
if (!err) {
// Save the XML
console.log('Autosaved:', xml);
const timestamp = getTimestamp();
saveXmlAsLocalFile(xml, `autosave_${timestamp}_${editorConfig.getFileName()}`);
}
});
}

function getTimestamp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
} from 'bpmn-js/lib/features/modeling/util/ModelingUtil';
import * as consts from '../Constants';
import { isConnectedWith } from '../../../editor/util/ModellingUtilities';
import { resetAutosaveTimeout } from '../../../editor/util/IoUtilities';
import { saveFile, setAutoSaveInterval } from '../../../editor/util/IoUtilities';
import { getModeler } from '../../../editor/ModelerHandler';
import ace from 'ace-builds';
import * as editorConfig from "../../../editor/config/EditorConfigManager";
import { autoSaveFile } from '../../../editor/EditorConstants';

/**
* Custom rules provider for the DataFlow elements. Extends the BpmnRules.
Expand All @@ -20,7 +22,6 @@ export default class CustomRulesProvider extends BpmnRules {
const canConnectDataExtension = this.canConnectDataExtension;
const canConnect = this.canConnect.bind(this);
const canCreate = this.canCreate.bind(this);
let autosaveTimeout = 0;

// persist into local storage whenever copy took place
eventBus.on('copyPaste.elementsCopied', event => {
Expand Down Expand Up @@ -69,10 +70,20 @@ export default class CustomRulesProvider extends BpmnRules {
);
});

eventBus.on("commandStack.changed", function() {

// Reset the timeout on any change event
resetAutosaveTimeout(autosaveTimeout, true);
// save every change when the autosave option is on action
eventBus.on("commandStack.changed", function () {
if (editorConfig.getAutoSaveFileOption() === autoSaveFile.ON_ACTION) {
saveFile();
}
});

// remove interval when autosave option is on action
eventBus.on("autoSaveOptionChanged", function (context) {
if (context.autoSaveFileOption === autoSaveFile.ON_ACTION) {
clearInterval(getModeler().autosaveIntervalId);
} else {
setAutoSaveInterval();
}
});

// update xml viewer on diagram change
Expand Down

0 comments on commit 5aeb69f

Please sign in to comment.