{
  "version": 3,
  "sources": ["../../../src/components/local-autosave-monitor/index.js"],
  "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useCallback, useEffect, useRef } from '@wordpress/element';\nimport { ifCondition, usePrevious } from '@wordpress/compose';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { __ } from '@wordpress/i18n';\nimport { parse } from '@wordpress/blocks';\nimport { store as noticesStore } from '@wordpress/notices';\n\n/**\n * Internal dependencies\n */\nimport AutosaveMonitor from '../autosave-monitor';\nimport {\n\tlocalAutosaveGet,\n\tlocalAutosaveClear,\n} from '../../store/local-autosave';\nimport { store as editorStore } from '../../store';\n\nconst requestIdleCallback = window.requestIdleCallback\n\t? window.requestIdleCallback\n\t: window.requestAnimationFrame;\n\nlet hasStorageSupport;\n\n/**\n * Function which returns true if the current environment supports browser\n * sessionStorage, or false otherwise. The result of this function is cached and\n * reused in subsequent invocations.\n */\nconst hasSessionStorageSupport = () => {\n\tif ( hasStorageSupport !== undefined ) {\n\t\treturn hasStorageSupport;\n\t}\n\n\ttry {\n\t\t// Private Browsing in Safari 10 and earlier will throw an error when\n\t\t// attempting to set into sessionStorage. The test here is intentional in\n\t\t// causing a thrown error as condition bailing from local autosave.\n\t\twindow.sessionStorage.setItem( '__wpEditorTestSessionStorage', '' );\n\t\twindow.sessionStorage.removeItem( '__wpEditorTestSessionStorage' );\n\t\thasStorageSupport = true;\n\t} catch {\n\t\thasStorageSupport = false;\n\t}\n\n\treturn hasStorageSupport;\n};\n\n/**\n * Custom hook which manages the creation of a notice prompting the user to\n * restore a local autosave, if one exists.\n */\nfunction useAutosaveNotice() {\n\tconst { postId, isEditedPostNew, hasRemoteAutosave } = useSelect(\n\t\t( select ) => ( {\n\t\t\tpostId: select( editorStore ).getCurrentPostId(),\n\t\t\tisEditedPostNew: select( editorStore ).isEditedPostNew(),\n\t\t\thasRemoteAutosave:\n\t\t\t\t!! select( editorStore ).getEditorSettings().autosave,\n\t\t} ),\n\t\t[]\n\t);\n\tconst { getEditedPostAttribute } = useSelect( editorStore );\n\n\tconst { createWarningNotice, removeNotice } = useDispatch( noticesStore );\n\tconst { editPost, resetEditorBlocks } = useDispatch( editorStore );\n\n\tuseEffect( () => {\n\t\tlet localAutosave = localAutosaveGet( postId, isEditedPostNew );\n\t\tif ( ! localAutosave ) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tlocalAutosave = JSON.parse( localAutosave );\n\t\t} catch {\n\t\t\t// Not usable if it can't be parsed.\n\t\t\treturn;\n\t\t}\n\n\t\tconst { post_title: title, content, excerpt } = localAutosave;\n\t\tconst edits = { title, content, excerpt };\n\n\t\t{\n\t\t\t// Only display a notice if there is a difference between what has been\n\t\t\t// saved and that which is stored in sessionStorage.\n\t\t\tconst hasDifference = Object.keys( edits ).some( ( key ) => {\n\t\t\t\treturn edits[ key ] !== getEditedPostAttribute( key );\n\t\t\t} );\n\n\t\t\tif ( ! hasDifference ) {\n\t\t\t\t// If there is no difference, it can be safely ejected from storage.\n\t\t\t\tlocalAutosaveClear( postId, isEditedPostNew );\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\tif ( hasRemoteAutosave ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst id = 'wpEditorAutosaveRestore';\n\n\t\tcreateWarningNotice(\n\t\t\t__(\n\t\t\t\t'The backup of this post in your browser is different from the version below.'\n\t\t\t),\n\t\t\t{\n\t\t\t\tid,\n\t\t\t\tactions: [\n\t\t\t\t\t{\n\t\t\t\t\t\tlabel: __( 'Restore the backup' ),\n\t\t\t\t\t\tonClick() {\n\t\t\t\t\t\t\tconst {\n\t\t\t\t\t\t\t\tcontent: editsContent,\n\t\t\t\t\t\t\t\t...editsWithoutContent\n\t\t\t\t\t\t\t} = edits;\n\t\t\t\t\t\t\teditPost( editsWithoutContent );\n\t\t\t\t\t\t\tresetEditorBlocks( parse( edits.content ) );\n\t\t\t\t\t\t\tremoveNotice( id );\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t}\n\t\t);\n\t}, [ isEditedPostNew, postId ] );\n}\n\n/**\n * Custom hook which ejects a local autosave after a successful save occurs.\n */\nfunction useAutosavePurge() {\n\tconst { postId, isEditedPostNew, isDirty, isAutosaving, didError } =\n\t\tuseSelect(\n\t\t\t( select ) => ( {\n\t\t\t\tpostId: select( editorStore ).getCurrentPostId(),\n\t\t\t\tisEditedPostNew: select( editorStore ).isEditedPostNew(),\n\t\t\t\tisDirty: select( editorStore ).isEditedPostDirty(),\n\t\t\t\tisAutosaving: select( editorStore ).isAutosavingPost(),\n\t\t\t\tdidError: select( editorStore ).didPostSaveRequestFail(),\n\t\t\t} ),\n\t\t\t[]\n\t\t);\n\n\tconst lastIsDirtyRef = useRef( isDirty );\n\tconst lastIsAutosavingRef = useRef( isAutosaving );\n\n\tuseEffect( () => {\n\t\tif (\n\t\t\t! didError &&\n\t\t\t( ( lastIsAutosavingRef.current && ! isAutosaving ) ||\n\t\t\t\t( lastIsDirtyRef.current && ! isDirty ) )\n\t\t) {\n\t\t\tlocalAutosaveClear( postId, isEditedPostNew );\n\t\t}\n\n\t\tlastIsDirtyRef.current = isDirty;\n\t\tlastIsAutosavingRef.current = isAutosaving;\n\t}, [ isDirty, isAutosaving, didError ] );\n\n\t// Once the isEditedPostNew changes from true to false, let's clear the auto-draft autosave.\n\tconst wasEditedPostNew = usePrevious( isEditedPostNew );\n\tconst prevPostId = usePrevious( postId );\n\tuseEffect( () => {\n\t\tif ( prevPostId === postId && wasEditedPostNew && ! isEditedPostNew ) {\n\t\t\tlocalAutosaveClear( postId, true );\n\t\t}\n\t}, [ isEditedPostNew, postId ] );\n}\n\nfunction LocalAutosaveMonitor() {\n\tconst { autosave } = useDispatch( editorStore );\n\tconst deferredAutosave = useCallback( () => {\n\t\trequestIdleCallback( () => autosave( { local: true } ) );\n\t}, [] );\n\tuseAutosaveNotice();\n\tuseAutosavePurge();\n\n\tconst localAutosaveInterval = useSelect(\n\t\t( select ) =>\n\t\t\tselect( editorStore ).getEditorSettings().localAutosaveInterval,\n\t\t[]\n\t);\n\n\treturn (\n\t\t<AutosaveMonitor\n\t\t\tinterval={ localAutosaveInterval }\n\t\t\tautosave={ deferredAutosave }\n\t\t/>\n\t);\n}\n\n/**\n * Monitors local autosaves of a post in the editor.\n * It uses several hooks and functions to manage autosave behavior:\n * - `useAutosaveNotice` hook: Manages the creation of a notice prompting the user to restore a local autosave, if one exists.\n * - `useAutosavePurge` hook: Ejects a local autosave after a successful save occurs.\n * - `hasSessionStorageSupport` function: Checks if the current environment supports browser sessionStorage.\n * - `LocalAutosaveMonitor` component: Uses the `AutosaveMonitor` component to perform autosaves at a specified interval.\n *\n * The module also checks for sessionStorage support and conditionally exports the `LocalAutosaveMonitor` component based on that.\n *\n * @module LocalAutosaveMonitor\n */\nexport default ifCondition( hasSessionStorageSupport )( LocalAutosaveMonitor );\n"],
  "mappings": ";AAGA,SAAS,aAAa,WAAW,cAAc;AAC/C,SAAS,aAAa,mBAAmB;AACzC,SAAS,WAAW,mBAAmB;AACvC,SAAS,UAAU;AACnB,SAAS,aAAa;AACtB,SAAS,SAAS,oBAAoB;AAKtC,OAAO,qBAAqB;AAC5B;AAAA,EACC;AAAA,EACA;AAAA,OACM;AACP,SAAS,SAAS,mBAAmB;AAyKnC;AAvKF,IAAM,sBAAsB,OAAO,sBAChC,OAAO,sBACP,OAAO;AAEV,IAAI;AAOJ,IAAM,2BAA2B,MAAM;AACtC,MAAK,sBAAsB,QAAY;AACtC,WAAO;AAAA,EACR;AAEA,MAAI;AAIH,WAAO,eAAe,QAAS,gCAAgC,EAAG;AAClE,WAAO,eAAe,WAAY,8BAA+B;AACjE,wBAAoB;AAAA,EACrB,QAAQ;AACP,wBAAoB;AAAA,EACrB;AAEA,SAAO;AACR;AAMA,SAAS,oBAAoB;AAC5B,QAAM,EAAE,QAAQ,iBAAiB,kBAAkB,IAAI;AAAA,IACtD,CAAE,YAAc;AAAA,MACf,QAAQ,OAAQ,WAAY,EAAE,iB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