UNPKG

@wordpress/editor

Version:
8 lines (7 loc) 3.63 kB
{ "version": 3, "sources": ["../../src/store/local-autosave.js"], "sourcesContent": ["import { privateApis as coreDataPrivateApis } from '@wordpress/core-data';\nimport { unlock } from '../lock-unlock';\n\nconst { CRDT_AUTOSAVE_SNAPSHOT_KEY } = unlock( coreDataPrivateApis );\n\n/**\n * Function returning a sessionStorage key to set or retrieve a given post's\n * automatic session backup.\n *\n * Keys are crucially prefixed with 'wp-autosave-' so that wp-login.php's\n * `loggedout` handler can clear sessionStorage of any user-private content.\n *\n * @see https://github.com/WordPress/wordpress-develop/blob/6dad32d2aed47e6c0cf2aee8410645f6d7aba6bd/src/wp-login.php#L103\n *\n * @param {string} postId Post ID.\n * @param {boolean} isPostNew Whether post new.\n *\n * @return {string} sessionStorage key\n */\nfunction postKey( postId, isPostNew ) {\n\treturn `wp-autosave-block-editor-post-${\n\t\tisPostNew ? 'auto-draft' : postId\n\t}`;\n}\n\nexport function localAutosaveGet( postId, isPostNew ) {\n\treturn window.sessionStorage.getItem( postKey( postId, isPostNew ) );\n}\n\nexport function localAutosaveSet(\n\tpostId,\n\tisPostNew,\n\ttitle,\n\tcontent,\n\texcerpt,\n\tcrdtSnapshot\n) {\n\tconst backup = {\n\t\tpost_title: title,\n\t\tcontent,\n\t\texcerpt,\n\t};\n\n\t// During real-time collaboration, record which shared document state this\n\t// backup captured. A later session can then verify that the shared\n\t// document already accounts for the backup's changes and skip the\n\t// \"restore the backup\" notice.\n\tif ( crdtSnapshot ) {\n\t\tbackup[ CRDT_AUTOSAVE_SNAPSHOT_KEY ] = crdtSnapshot;\n\t}\n\n\tconst key = postKey( postId, isPostNew );\n\n\ttry {\n\t\twindow.sessionStorage.setItem( key, JSON.stringify( backup ) );\n\t} catch ( error ) {\n\t\tif ( ! backup[ CRDT_AUTOSAVE_SNAPSHOT_KEY ] ) {\n\t\t\tthrow error;\n\t\t}\n\n\t\t// In the unlikely event that the snapshot is too large for storage,\n\t\t// ensure we still store content without a snapshot. At worst, this results\n\t\t// in a notice that locally-saved content is available that isn't necessary.\n\t\tdelete backup[ CRDT_AUTOSAVE_SNAPSHOT_KEY ];\n\t\twindow.sessionStorage.setItem( key, JSON.stringify( backup ) );\n\t}\n}\n\n/**\n * Returns the CRDT snapshot recorded in a parsed local autosave backup, if any.\n *\n * @param {Object|null} backup Parsed local autosave backup.\n *\n * @return {string|undefined} Base64-encoded snapshot, or undefined when the\n * backup does not record one.\n */\nexport function localAutosaveGetSnapshot( backup ) {\n\treturn backup?.[ CRDT_AUTOSAVE_SNAPSHOT_KEY ];\n}\n\nexport function localAutosaveClear( postId, isPostNew ) {\n\twindow.sessionStorage.removeItem( postKey( postId, isPostNew ) );\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAmD;AACnD,yBAAuB;AAEvB,IAAM,EAAE,2BAA2B,QAAI,2BAAQ,iBAAAA,WAAoB;AAgBnE,SAAS,QAAS,QAAQ,WAAY;AACrC,SAAO,iCACN,YAAY,eAAe,MAC5B;AACD;AAEO,SAAS,iBAAkB,QAAQ,WAAY;AACrD,SAAO,OAAO,eAAe,QAAS,QAAS,QAAQ,SAAU,CAAE;AACpE;AAEO,SAAS,iBACf,QACA,WACA,OACA,SACA,SACA,cACC;AACD,QAAM,SAAS;AAAA,IACd,YAAY;AAAA,IACZ;AAAA,IACA;AAAA,EACD;AAMA,MAAK,cAAe;AACnB,WAAQ,0BAA2B,IAAI;AAAA,EACxC;AAEA,QAAM,MAAM,QAAS,QAAQ,SAAU;AAEvC,MAAI;AACH,WAAO,eAAe,QAAS,KAAK,KAAK,UAAW,MAAO,CAAE;AAAA,EAC9D,SAAU,OAAQ;AACjB,QAAK,CAAE,OAAQ,0BAA2B,GAAI;AAC7C,YAAM;AAAA,IACP;AAKA,WAAO,OAAQ,0BAA2B;AAC1C,WAAO,eAAe,QAAS,KAAK,KAAK,UAAW,MAAO,CAAE;AAAA,EAC9D;AACD;AAUO,SAAS,yBAA0B,QAAS;AAClD,SAAO,SAAU,0BAA2B;AAC7C;AAEO,SAAS,mBAAoB,QAAQ,WAAY;AACvD,SAAO,eAAe,WAAY,QAAS,QAAQ,SAAU,CAAE;AAChE;", "names": ["coreDataPrivateApis"] }