UNPKG

@wordpress/editor

Version:
8 lines (7 loc) 6.02 kB
{ "version": 3, "sources": ["../../../src/components/provider/use-autosave-notice.js"], "sourcesContent": ["import { useLayoutEffect } from '@wordpress/element';\nimport { useDispatch, useRegistry } from '@wordpress/data';\nimport { __ } from '@wordpress/i18n';\nimport { store as noticesStore } from '@wordpress/notices';\nimport { getQueryArg } from '@wordpress/url';\nimport { store as editorStore } from '../../store';\nimport { unlock } from '../../lock-unlock';\nimport useEntityContainsSnapshot from './use-entity-contains-snapshot';\n\n/**\n * Creates the warning notice about a more recent autosave.\n *\n * @param {Object} props Function props.\n * @param {Object} props.autosave The `autosave` editor setting.\n * @param {Function} props.createWarningNotice Action that creates the notice.\n * @param {Object} props.registry The data registry.\n * @param {Function} props.setCurrentRevisionId Action that opens a revision.\n */\nfunction showAutosaveExistsNotice( {\n\tautosave,\n\tcreateWarningNotice,\n\tregistry,\n\tsetCurrentRevisionId,\n} ) {\n\t// The only place core exposes the autosave ID is the edit\n\t// link, always `revision.php?revision=<autosave ID>`.\n\tconst autosaveId = Number( getQueryArg( autosave.editLink, 'revision' ) );\n\tcreateWarningNotice(\n\t\t__(\n\t\t\t'There is an autosave of this post that is more recent than the version below.'\n\t\t),\n\t\t{\n\t\t\tid: 'autosave-exists',\n\t\t\tactions: [\n\t\t\t\t{\n\t\t\t\t\tlabel: __( 'View the autosave' ),\n\t\t\t\t\t...( autosaveId\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\tonClick: () => {\n\t\t\t\t\t\t\t\t\t// `disableVisualRevisions` is only set\n\t\t\t\t\t\t\t\t\t// after mount, so read it at click time.\n\t\t\t\t\t\t\t\t\tconst { disableVisualRevisions } = registry\n\t\t\t\t\t\t\t\t\t\t.select( editorStore )\n\t\t\t\t\t\t\t\t\t\t.getEditorSettings();\n\t\t\t\t\t\t\t\t\tif ( disableVisualRevisions ) {\n\t\t\t\t\t\t\t\t\t\twindow.location.href =\n\t\t\t\t\t\t\t\t\t\t\tautosave.editLink;\n\t\t\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tsetCurrentRevisionId( autosaveId );\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t }\n\t\t\t\t\t\t: { url: autosave.editLink } ),\n\t\t\t\t},\n\t\t\t],\n\t\t}\n\t);\n}\n\n/**\n * Shows the \"more recent autosave\" notice when applicable.\n *\n * Outside real-time collaboration, the notice is created immediately on\n * mount whenever the server flagged an autosave (`settings.autosave`).\n *\n * Under real-time collaboration, the autosave content is usually already\n * part of the shared document, making the notice redundant. The autosave\n * records a Yjs snapshot of the document it captured, which the server\n * returns in `settings.autosave.crdtSnapshot`. The notice decision is\n * deferred until the shared document is checked against that snapshot (see\n * `useEntityContainsSnapshot`): a positive result proves this document\n * holds everything the autosave did, so the notice is not necessary.\n *\n * IMPORTANT: Call this hook after the mount effect that dispatches\n * `setupEditor`, so that the collaboration check can read the current post.\n *\n * @param {Object} props Hook props.\n * @param {Object} props.post The post object.\n * @param {boolean} props.recovery Whether the editor is in recovery mode.\n * @param {Object} props.settings The editor settings.\n */\nexport default function useAutosaveNotice( { post, recovery, settings } ) {\n\tconst registry = useRegistry();\n\tconst { createWarningNotice } = useDispatch( noticesStore );\n\tconst { setCurrentRevisionId } = unlock( useDispatch( editorStore ) );\n\n\t// Assume the notice is not needed in the case of an error recovery.\n\t// Passing no snapshot resolves the snapshot status immediately.\n\tconst snapshotStatus = useEntityContainsSnapshot( {\n\t\tpostType: post.type,\n\t\tpostId: post.id,\n\t\tsnapshot: recovery ? undefined : settings.autosave?.crdtSnapshot,\n\t} );\n\n\tuseLayoutEffect( () => {\n\t\tif ( recovery || ! settings.autosave ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// The shared document already accounts for the autosaved content,\n\t\t// so the notice is redundant.\n\t\tif ( 'present' === snapshotStatus ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Keep waiting for the snapshot status to resolve.\n\t\tif ( 'pending' === snapshotStatus ) {\n\t\t\treturn;\n\t\t}\n\n\t\tshowAutosaveExistsNotice( {\n\t\t\tautosave: settings.autosave,\n\t\t\tcreateWarningNotice,\n\t\t\tregistry,\n\t\t\tsetCurrentRevisionId,\n\t\t} );\n\n\t\t// The snapshot status settles at most once, so the notice is\n\t\t// created at most once. `settings.autosave` and the notice actions\n\t\t// are stable for the lifetime of the provider.\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, [ snapshotStatus ] );\n}\n"], "mappings": ";AAAA,SAAS,uBAAuB;AAChC,SAAS,aAAa,mBAAmB;AACzC,SAAS,UAAU;AACnB,SAAS,SAAS,oBAAoB;AACtC,SAAS,mBAAmB;AAC5B,SAAS,SAAS,mBAAmB;AACrC,SAAS,cAAc;AACvB,OAAO,+BAA+B;AAWtC,SAAS,yBAA0B;AAAA,EAClC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAAI;AAGH,QAAM,aAAa,OAAQ,YAAa,SAAS,UAAU,UAAW,CAAE;AACxE;AAAA,IACC;AAAA,MACC;AAAA,IACD;AAAA,IACA;AAAA,MACC,IAAI;AAAA,MACJ,SAAS;AAAA,QACR;AAAA,UACC,OAAO,GAAI,mBAAoB;AAAA,UAC/B,GAAK,aACF;AAAA,YACA,SAAS,MAAM;AAGd,oBAAM,EAAE,uBAAuB,IAAI,SACjC,OAAQ,WAAY,EACpB,kBAAkB;AACpB,kBAAK,wBAAyB;AAC7B,uBAAO,SAAS,OACf,SAAS;AACV;AAAA,cACD;AACA,mCAAsB,UAAW;AAAA,YAClC;AAAA,UACA,IACA,EAAE,KAAK,SAAS,SAAS;AAAA,QAC7B;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;AAwBe,SAAR,kBAAoC,EAAE,MAAM,UAAU,SAAS,GAAI;AACzE,QAAM,WAAW,YAAY;AAC7B,QAAM,EAAE,oBAAoB,IAAI,YAAa,YAAa;AAC1D,QAAM,EAAE,qBAAqB,IAAI,OAAQ,YAAa,WAAY,C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