From 41076584f6b6552a9d98068a826da3dcf19b00b6 Mon Sep 17 00:00:00 2001 From: jimafisk Date: Sun, 8 Sep 2024 22:39:16 -0400 Subject: [PATCH] Redirect if evaluated route changes on update (#232). --- defaults/core/cms/post_local.js | 18 +++++++++++------- defaults/core/cms/publish.js | 18 +++++++++++------- defaults/core/cms/route_eval.js | 5 +++-- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/defaults/core/cms/post_local.js b/defaults/core/cms/post_local.js index 217e6897..72e17ec4 100644 --- a/defaults/core/cms/post_local.js +++ b/defaults/core/cms/post_local.js @@ -22,15 +22,19 @@ export async function postLocal(commitList, shadowContent, action, encoding) { body: JSON.stringify(body), }); if (response.ok) { - if (commitList.length === 1 && action === 'create') { - let evaluatedRoute = evaluateRoute(commitList[0]); - history.pushState({ - isNew: true, - route: evaluatedRoute - }, '', evaluatedRoute); - } if (action === 'create' || action === 'update') { shadowContent?.onSave?.(); + // Make sure saving single content file, not list of media items + if (commitList.length === 1 && commitList[0].file.lastIndexOf('.json') > 0) { + let evaluatedRoute = evaluateRoute(commitList[0]); + // Redirect only if new route is being created + if (evaluatedRoute !== location.pathname) { + history.pushState({ + isNew: true, + route: evaluatedRoute + }, '', evaluatedRoute); + } + } } if (action === 'delete') { shadowContent?.onDelete?.(); diff --git a/defaults/core/cms/publish.js b/defaults/core/cms/publish.js index 94dc112e..d4e00e1f 100644 --- a/defaults/core/cms/publish.js +++ b/defaults/core/cms/publish.js @@ -64,15 +64,19 @@ export async function publish(commitList, shadowContent, action, encoding, user) body: JSON.stringify(payload), }); if (response.ok) { - if (commitList.length === 1 && action === 'create') { - let evaluatedRoute = evaluateRoute(commitList[0]); - history.pushState({ - isNew: true, - route: evaluatedRoute - }, '', evaluatedRoute); - } if (action === 'create' || action === 'update') { shadowContent?.onSave?.(); + // Make sure saving single content file, not list of media items + if (commitList.length === 1 && commitList[0].file.lastIndexOf('.json') > 0) { + let evaluatedRoute = evaluateRoute(commitList[0]); + // Redirect only if new route is being created + if (evaluatedRoute !== location.pathname) { + history.pushState({ + isNew: true, + route: evaluatedRoute + }, '', evaluatedRoute); + } + } } if (action === 'delete') { shadowContent?.onDelete?.(); diff --git a/defaults/core/cms/route_eval.js b/defaults/core/cms/route_eval.js index 7eb29b6d..87a20ca4 100644 --- a/defaults/core/cms/route_eval.js +++ b/defaults/core/cms/route_eval.js @@ -1,8 +1,9 @@ import { env } from '../../generated/env.js'; export default function evaluateRoute(commitItem) { - let content_type = window.location.hash.split("/")[1]; - let filename = window.location.hash.split("/")[2]; + let content_type = commitItem.file.split("/")[1]; + let filenameWithExt = commitItem.file.split("/")[2]; // Should probably account for nested content folders + let filename = filenameWithExt.substring(0, filenameWithExt.lastIndexOf('.json')); let filepath = commitItem.file; let fields = JSON.parse(commitItem.contents); let default_route = "/" + content_type + "/" + filename;