Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"autosaver.min.js","sources":["../src/autosaver.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Storage helper for the Moodle Tiny Autosave plugin.\n *\n * @module      tiny_autosave/autosaver\n * @copyright   2022 Andrew Lyons <andrew@nicols.co.uk>\n * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport * as Options from './options';\nimport * as Storage from './storage';\nimport Log from 'core/log';\nimport {eventTypes} from 'core_form/events';\nimport {getLogSource} from './common';\n\nexport const register = (editor) => {\n    const undoHandler = () => {\n        if (!editor.undoManager.hasUndo()) {\n            Log.debug(`Ignoring undo event as there is no undo history`, getLogSource(editor));\n            return;\n        }\n        Storage.saveDraft(editor);\n    };\n\n    const visibilityChangedHandler = () => {\n        if (document.visibilityState === 'hidden') {\n            if (Options.isInitialised(editor)) {\n                Storage.saveDraft(editor);\n            }\n        }\n    };\n\n    // Javascript form submission handler.\n    const handleFormSubmittedByJavascript = (e) => {\n        if (Options.isInitialised(editor) && e.target.contains(editor.getElement())) {\n            removeAutoSaveSession();\n        }\n    };\n\n    // Remove the auto save session.\n    const removeAutoSaveSession = () => {\n        document.removeEventListener('visibilitychange', visibilityChangedHandler);\n        document.removeEventListener(eventTypes.formSubmittedByJavascript, handleFormSubmittedByJavascript);\n        Storage.removeAutosaveSession(editor);\n    };\n\n    // Attempt to store the draft one final time before the page unloads.\n    // Note: This may need to be sent as a beacon instead.\n    document.addEventListener('visibilitychange', visibilityChangedHandler);\n\n    // When the page is submitted as a form, remove the draft.\n    editor.on('submit', removeAutoSaveSession);\n    document.addEventListener(eventTypes.formSubmittedByJavascript, handleFormSubmittedByJavascript);\n\n    editor.on('init', () => {\n        // Setup the Undo handler.\n        editor.on('AddUndo', undoHandler);\n\n        if (editor.dom.isEmpty(editor.getBody())) {\n            Log.info(`Attempting to restore draft`, getLogSource(editor));\n            Storage.restoreDraft(editor);\n        } else {\n            // There was nothing to restore, so we can mark the editor as initialised.\n            Log.warn(`Skipping draft restoration. The editor is not empty.`, getLogSource(editor));\n            Options.markInitialised(editor);\n        }\n    });\n};\n"],"names":["editor","undoHandler","undoManager","hasUndo","Storage","saveDraft","debug","visibilityChangedHandler","document","visibilityState","Options","isInitialised","handleFormSubmittedByJavascript","e","target","contains","getElement","removeAutoSaveSession","removeEventListener","eventTypes","formSubmittedByJavascript","removeAutosaveSession","addEventListener","on","dom","isEmpty","getBody","info","restoreDraft","warn","markInitialised"],"mappings":";;;;;;;4OA6ByBA,eACfC,YAAc,KACXD,OAAOE,YAAYC,UAIxBC,QAAQC,UAAUL,qBAHVM,yDAAyD,wBAAaN,UAM5EO,yBAA2B,KACI,WAA7BC,SAASC,iBACLC,QAAQC,cAAcX,SACtBI,QAAQC,UAAUL,SAMxBY,gCAAmCC,IACjCH,QAAQC,cAAcX,SAAWa,EAAEC,OAAOC,SAASf,OAAOgB,eAC1DC,yBAKFA,sBAAwB,KAC1BT,SAASU,oBAAoB,mBAAoBX,0BACjDC,SAASU,oBAAoBC,mBAAWC,0BAA2BR,iCACnER,QAAQiB,sBAAsBrB,SAKlCQ,SAASc,iBAAiB,mBAAoBf,0BAG9CP,OAAOuB,GAAG,SAAUN,uBACpBT,SAASc,iBAAiBH,mBAAWC,0BAA2BR,iCAEhEZ,OAAOuB,GAAG,QAAQ,KAEdvB,OAAOuB,GAAG,UAAWtB,aAEjBD,OAAOwB,IAAIC,QAAQzB,OAAO0B,yBACtBC,oCAAoC,wBAAa3B,SACrDI,QAAQwB,aAAa5B,uBAGjB6B,6DAA6D,wBAAa7B,SAC9EU,QAAQoB,gBAAgB9B"}