Skip to content

Commit

Permalink
Fixed non-existing path issue
Browse files Browse the repository at this point in the history
  • Loading branch information
rinkalpagdar committed Dec 10, 2024
1 parent 7159a78 commit 81058c5
Showing 1 changed file with 81 additions and 26 deletions.
107 changes: 81 additions & 26 deletions packages/edit-site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,98 @@ import { store as editSiteStore } from './store';
import { unlock } from './lock-unlock';
import App from './components/app';

const { registerCoreBlockBindingsSources } = unlock( editorPrivateApis );
const { registerCoreBlockBindingsSources } = unlock(editorPrivateApis);

/**
* Checks if the given path exists.
*
* @param {string} path The path to check.
* @return {boolean} True if the path exists, false otherwise.
*/
async function checkPathExists(path) {
const response = await fetch(`/wp-json/wp/v2/templates${path}`);
return response.ok;
}

/**
* Initializes the site editor screen.
*
* @param {string} id ID of the root element to render the screen in.
* @param {Object} settings Editor settings.
*/
export function initializeEditor( id, settings ) {
const target = document.getElementById( id );
const root = createRoot( target );
export async function initializeEditor(id, settings) {
const target = document.getElementById(id);
const root = createRoot(target);
// Extract the 'p' parameter from the URL
const urlParams = new URLSearchParams(window.location.search);
const path = urlParams.get('p');

// Decode the URI component for the path
const decodedPath = decodeURIComponent(path || '');

if (decodedPath && !(await checkPathExists(decodedPath))) {
// Render a "Not Found" message
root.render(
<StrictMode>
<div
style={{
textAlign: 'center',
marginTop: '50px',
fontSize: '24px',
color: '#fff',
}}
>
<strong>404:</strong> Template Not Found
<div style={{ marginTop: '20px' }}>
<button
onClick={() => {
// Redirect to the site editor
window.location.href = '/wp-admin/site-editor.php';
}}
style={{
padding: '10px 20px',
backgroundColor: '#0073aa',
color: 'white',
border: 'none',
borderRadius: '5px',
cursor: 'pointer',
fontSize: '16px',
}}
>
Go to Site Editor
</button>
</div>
</div>
</StrictMode>
);
return root;
}

dispatch( blocksStore ).reapplyBlockTypeFilters();
dispatch(blocksStore).reapplyBlockTypeFilters();
const coreBlocks = __experimentalGetCoreBlocks().filter(
( { name } ) => name !== 'core/freeform'
({ name }) => name !== 'core/freeform'
);
registerCoreBlocks( coreBlocks );
registerCoreBlocks(coreBlocks);
registerCoreBlockBindingsSources();
dispatch( blocksStore ).setFreeformFallbackBlockName( 'core/html' );
registerLegacyWidgetBlock( { inserter: false } );
registerWidgetGroupBlock( { inserter: false } );
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
__experimentalRegisterExperimentalCoreBlocks( {
dispatch(blocksStore).setFreeformFallbackBlockName('core/html');
registerLegacyWidgetBlock({ inserter: false });
registerWidgetGroupBlock({ inserter: false });
if (globalThis.IS_GUTENBERG_PLUGIN) {
__experimentalRegisterExperimentalCoreBlocks({
enableFSEBlocks: true,
} );
});
}

// We dispatch actions and update the store synchronously before rendering
// so that we won't trigger unnecessary re-renders with useEffect.
dispatch( preferencesStore ).setDefaults( 'core/edit-site', {
dispatch(preferencesStore).setDefaults('core/edit-site', {
welcomeGuide: true,
welcomeGuideStyles: true,
welcomeGuidePage: true,
welcomeGuideTemplate: true,
} );
});

dispatch( preferencesStore ).setDefaults( 'core', {
dispatch(preferencesStore).setDefaults('core', {
allowRightClickOverrides: true,
distractionFree: false,
editorMode: 'visual',
Expand All @@ -70,24 +125,24 @@ export function initializeEditor( id, settings ) {
focusMode: false,
inactivePanels: [],
keepCaretInsideBlock: false,
openPanels: [ 'post-status' ],
openPanels: ['post-status'],
showBlockBreadcrumbs: true,
showListViewByDefault: false,
enableChoosePatternModal: true,
} );
});

if ( window.__experimentalMediaProcessing ) {
dispatch( preferencesStore ).setDefaults( 'core/media', {
if (window.__experimentalMediaProcessing) {
dispatch(preferencesStore).setDefaults('core/media', {
requireApproval: true,
optimizeOnUpload: true,
} );
});
}

dispatch( editSiteStore ).updateSettings( settings );
dispatch(editSiteStore).updateSettings(settings);

// Prevent the default browser action for files dropped outside of dropzones.
window.addEventListener( 'dragover', ( e ) => e.preventDefault(), false );
window.addEventListener( 'drop', ( e ) => e.preventDefault(), false );
window.addEventListener('dragover', (e) => e.preventDefault(), false);
window.addEventListener('drop', (e) => e.preventDefault(), false);

root.render(
<StrictMode>
Expand All @@ -99,10 +154,10 @@ export function initializeEditor( id, settings ) {
}

export function reinitializeEditor() {
deprecated( 'wp.editSite.reinitializeEditor', {
deprecated('wp.editSite.reinitializeEditor', {
since: '6.2',
version: '6.3',
} );
});
}

export { default as PluginTemplateSettingPanel } from './components/plugin-template-setting-panel';
Expand Down

0 comments on commit 81058c5

Please sign in to comment.