import state from '../state/state' ; export const FILE_EXISTS = 1 ; export const FILE_NOT_FOUND = 2 ; export const FILE_OPERATION_SUCCESSFUL = 3 ; const DOCUMENT_LIST_KEY = 'm3DocumentList' ; param documentName returns export function deleteDocument ( documentName ) { const documentList = getSavedDocumentList ( ) ; const documentIndex = documentList . indexOf ( documentName ) ; if ( documentIndex === - 1 ) { return FILE_NOT_FOUND ; } const newDocumentList = documentList . filter ( ( _name , index ) => index !== documentIndex ) ; const documentKey = getSavedDocumentKey ( documentIndex ) ; window . localStorage . setItem ( DOCUMENT_LIST_KEY , JSON . stringify ( newDocumentList ) ) ; window . localStorage . removeItem ( documentKey ) ; return FILE_OPERATION_SUCCESSFUL ; } returns export function getSavedDocumentList ( ) { const documentListJson = window . localStorage . getItem ( DOCUMENT_LIST_KEY ) ; const documentList = JSON . parse ( documentListJson || '[]' ) ; return documentList ; } param name returns export function getSavedDocument ( name ) { const documentList = getSavedDocumentList ( ) ; const documentIndex = documentList . indexOf ( name ) ; if ( documentIndex === - 1 ) { return FILE_NOT_FOUND ; } const documentKey = getSavedDocumentKey ( documentIndex ) ; const doc = window . localStorage . getItem ( documentKey ) || '' ; return doc ; } param baseName returns export function getUniqueFilename ( baseName ) { const existingSavedDocs = getSavedDocumentList ( ) ; let newDocName = baseName ; while ( existingSavedDocs . includes ( newDocName ) ) { newDocName += '-1' ; } return newDocName ; } param currentName param newName returns export function renameDocument ( currentName , newName ) { const documentList = getSavedDocumentList ( ) ; const documentIndex = documentList . indexOf ( currentName ) ; if ( documentIndex === - 1 ) { return FILE_NOT_FOUND ; } documentList [ documentIndex ] = newName ; const documentListJson = JSON . stringify ( documentList ) ; window . localStorage . setItem ( DOCUMENT_LIST_KEY , documentListJson ) ; return FILE_OPERATION_SUCCESSFUL ; } param replaceExisting param docName param doc returns export function saveDocument ( replaceExisting , docName , doc ) { const documentList = getSavedDocumentList ( ) ; const currentDocIndex = documentList . indexOf ( docName ) ; let indexForSaveOp ; if ( currentDocIndex !== - 1 ) { if ( ! replaceExisting ) { return FILE_EXISTS ; } indexForSaveOp = currentDocIndex ; } else { documentList . push ( docName ) ; const documentListJson = JSON . stringify ( documentList ) ; window . localStorage . setItem ( DOCUMENT_LIST_KEY , documentListJson ) ; indexForSaveOp = documentList . length - 1 ; } saveDocumentAtIndex ( indexForSaveOp , doc ) ; return FILE_OPERATION_SUCCESSFUL ; } function getSavedDocumentKey ( index ) { return `m3Document-${ index }` ; } function saveDocumentAtIndex ( docIndex , doc ) { window . localStorage . setItem ( getSavedDocumentKey ( docIndex ) , doc ) ; state . doc . setHasUnsavedChanges ( false ) ; }