@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
8 lines (7 loc) • 7.55 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/store/utils/notice-builder.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { ATTACHMENT_POST_TYPE } from '../constants';\n\n/**\n * Builds the arguments for a success notification dispatch.\n *\n * @param {Object} data Incoming data to build the arguments from.\n *\n * @return {Array} Arguments for dispatch. An empty array signals no\n * notification should be sent.\n */\nexport function getNotificationArgumentsForSaveSuccess( data ) {\n\tconst { previousPost, post, postType } = data;\n\t// Autosaves are neither shown a notice nor redirected.\n\tif ( data.options?.isAutosave ) {\n\t\treturn [];\n\t}\n\n\tconst publishStatus = [ 'publish', 'private', 'future' ];\n\tconst isPublished = publishStatus.includes( previousPost.status );\n\tconst willPublish = publishStatus.includes( post.status );\n\tconst willTrash =\n\t\tpost.status === 'trash' && previousPost.status !== 'trash';\n\n\tlet noticeMessage;\n\tlet shouldShowLink = postType?.viewable ?? false;\n\tlet isDraft;\n\n\t// Always should a notice, which will be spoken for accessibility.\n\tif ( willTrash ) {\n\t\tnoticeMessage = postType.labels.item_trashed;\n\t\tshouldShowLink = false;\n\t} else if ( post.type === ATTACHMENT_POST_TYPE ) {\n\t\t// Attachments should always show a simple updated message because they don't have a draft state.\n\t\tnoticeMessage = __( 'Media updated.' );\n\t\tshouldShowLink = false;\n\t} else if ( ! isPublished && ! willPublish ) {\n\t\t// If saving a non-published post, don't show notice.\n\t\tnoticeMessage = __( 'Draft saved.' );\n\t\tisDraft = true;\n\t} else if ( isPublished && ! willPublish ) {\n\t\t// If undoing publish status, show specific notice.\n\t\tnoticeMessage = postType.labels.item_reverted_to_draft;\n\t\tshouldShowLink = false;\n\t} else if ( ! isPublished && willPublish ) {\n\t\t// If publishing or scheduling a post, show the corresponding\n\t\t// publish message.\n\t\tnoticeMessage = {\n\t\t\tpublish: postType.labels.item_published,\n\t\t\tprivate: postType.labels.item_published_privately,\n\t\t\tfuture: postType.labels.item_scheduled,\n\t\t}[ post.status ];\n\t} else {\n\t\t// Generic fallback notice.\n\t\tnoticeMessage = postType.labels.item_updated;\n\t}\n\n\tconst actions = [];\n\tif ( shouldShowLink ) {\n\t\tactions.push( {\n\t\t\tlabel: isDraft ? __( 'View Preview' ) : postType.labels.view_item,\n\t\t\turl: post.link,\n\t\t\topenInNewTab: true,\n\t\t} );\n\t}\n\treturn [\n\t\tnoticeMessage,\n\t\t{\n\t\t\tid: 'editor-save',\n\t\t\ttype: 'snackbar',\n\t\t\tactions,\n\t\t},\n\t];\n}\n\n/**\n * Builds the fail notification arguments for dispatch.\n *\n * @param {Object} data Incoming data to build the arguments with.\n *\n * @return {Array} Arguments for dispatch. An empty array signals no\n * notification should be sent.\n */\nexport function getNotificationArgumentsForSaveFail( data ) {\n\tconst { post, edits, error } = data;\n\tif ( error && 'rest_autosave_no_changes' === error.code ) {\n\t\t// Autosave requested a new autosave, but there were no changes. This shouldn't\n\t\t// result in an error notice for the user.\n\t\treturn [];\n\t}\n\n\tconst publishStatus = [ 'publish', 'private', 'future' ];\n\tconst isPublished = publishStatus.indexOf( post.status ) !== -1;\n\n\tif ( error.code === 'offline_error' ) {\n\t\tconst messages = {\n\t\t\tpublish: __(\n\t\t\t\t'Publishing failed because you were offline. Please verify your connection and try again.'\n\t\t\t),\n\t\t\tprivate: __(\n\t\t\t\t'Publishing failed because you were offline. Please verify your connection and try again.'\n\t\t\t),\n\t\t\tfuture: __(\n\t\t\t\t'Scheduling failed because you were offline. Please verify your connection and try again.'\n\t\t\t),\n\t\t\tdefault: __(\n\t\t\t\t'Updating failed because you were offline. Please verify your connection and try again.'\n\t\t\t),\n\t\t};\n\n\t\tconst noticeMessage =\n\t\t\t! isPublished && edits.status in messages\n\t\t\t\t? messages[ edits.status ]\n\t\t\t\t: messages.default;\n\n\t\treturn [ noticeMessage, { id: 'editor-save' } ];\n\t}\n\n\tconst messages = {\n\t\tpublish: __( 'Publishing failed.' ),\n\t\tprivate: __( 'Publishing failed.' ),\n\t\tfuture: __( 'Scheduling failed.' ),\n\t\tdefault: __( 'Updating failed.' ),\n\t};\n\n\tlet noticeMessage =\n\t\t! isPublished && edits.status in messages\n\t\t\t? messages[ edits.status ]\n\t\t\t: messages.default;\n\n\t// Check if message string contains HTML. Notice text is currently only\n\t// supported as plaintext, and stripping the tags may muddle the meaning.\n\tif ( error.message && ! /<\\/?[^>]*>/.test( error.message ) ) {\n\t\tnoticeMessage = [ noticeMessage, error.message ].join( ' ' );\n\t}\n\treturn [\n\t\tnoticeMessage,\n\t\t{\n\t\t\tid: 'editor-save',\n\t\t},\n\t];\n}\n\n/**\n * Builds the trash fail notification arguments for dispatch.\n *\n * @param {Object} data\n *\n * @return {Array} Arguments for dispatch.\n */\nexport function getNotificationArgumentsForTrashFail( data ) {\n\treturn [\n\t\tdata.error.message && data.error.code !== 'unknown_error'\n\t\t\t? data.error.message\n\t\t\t: __( 'Trashing failed' ),\n\t\t{\n\t\t\tid: 'editor-trash-fail',\n\t\t},\n\t];\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAmB;AAKnB,uBAAqC;AAU9B,SAAS,uCAAwC,MAAO;AAC9D,QAAM,EAAE,cAAc,MAAM,SAAS,IAAI;AAEzC,MAAK,KAAK,SAAS,YAAa;AAC/B,WAAO,CAAC;AAAA,EACT;AAEA,QAAM,gBAAgB,CAAE,WAAW,WAAW,QAAS;AACvD,QAAM,cAAc,cAAc,SAAU,aAAa,MAAO;AAChE,QAAM,cAAc,cAAc,SAAU,KAAK,MAAO;AACxD,QAAM,YACL,KAAK,WAAW,WAAW,aAAa,WAAW;AAEpD,MAAI;AACJ,MAAI,iBAAiB,UAAU,YAAY;AAC3C,MAAI;AAGJ,MAAK,WAAY;AAChB,oBAAgB,SAAS,OAAO;AAChC,qBAAiB;AAAA,EAClB,WAAY,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