| @@ -57,14 +57,24 @@ | ||
| 57 | 57 | var import_core_data = __toESM(require_core_data()); |
| 58 | 58 | var import_html_entities = __toESM(require_html_entities()); |
| 59 | 59 | var import_i18n = __toESM(require_i18n()); |
| 60 | 60 | import { notFound } from "@wordpress/route"; |
| 61 | +var TEMPLATE_POST_TYPES = ["wp_template", "wp_template_part"]; | |
| 62 | +function getPostId(params) { | |
| 63 | + const id = params["*"]; | |
| 64 | + if (!id) { | |
| 65 | + throw notFound(); | |
| 66 | + } | |
| 67 | + try { | |
| 68 | + return decodeURIComponent(id); | |
| 69 | + } catch { | |
| 70 | + return id; | |
| 71 | + } | |
| 72 | +} | |
| 61 | 73 | var route = { |
| 62 | - beforeLoad: async ({ | |
| 63 | - params | |
| 64 | - }) => { | |
| 65 | - const postId = parseInt(params.id, 10); | |
| 66 | - if (Number.isNaN(postId)) { | |
| 74 | + beforeLoad: async ({ params }) => { | |
| 75 | + const postId = getPostId(params); | |
| 76 | + if (!TEMPLATE_POST_TYPES.includes(params.type) && !/^\d+$/.test(postId)) { | |
| 67 | 77 | throw notFound(); |
| 68 | 78 | } |
| 69 | 79 | try { |
| 70 | 80 | const [postType, post] = await Promise.all([ |
| @@ -81,19 +91,21 @@ | ||
| 81 | 91 | } catch { |
| 82 | 92 | throw notFound(); |
| 83 | 93 | } |
| 84 | 94 | }, |
| 85 | - title: async ({ | |
| 86 | - params | |
| 87 | - }) => { | |
| 95 | + title: async ({ params }) => { | |
| 96 | + const postId = getPostId(params); | |
| 88 | 97 | const post = await (0, import_data.resolveSelect)(import_core_data.store).getEntityRecord( |
| 89 | 98 | "postType", |
| 90 | 99 | params.type, |
| 91 | - params.id | |
| 100 | + postId | |
| 92 | 101 | ); |
| 93 | 102 | if (post?.title?.rendered) { |
| 94 | 103 | return (0, import_html_entities.decodeEntities)(post.title.rendered); |
| 95 | 104 | } |
| 105 | + if (post?.title?.raw) { | |
| 106 | + return post.title.raw; | |
| 107 | + } | |
| 96 | 108 | const postType = await (0, import_data.resolveSelect)(import_core_data.store).getPostType( |
| 97 | 109 | params.type |
| 98 | 110 | ); |
| 99 | 111 | return postType?.labels?.edit_item || (0, import_i18n.__)("Edit"); |
| @@ -99,12 +111,30 @@ | ||
| 99 | 111 | return postType?.labels?.edit_item || (0, import_i18n.__)("Edit"); |
| 100 | 112 | }, |
| 101 | 113 | async canvas(context) { |
| 102 | 114 | const { params } = context; |
| 115 | + const postId = getPostId(params); | |
| 103 | 116 | return { |
| 104 | 117 | postType: params.type, |
| 105 | - postId: params.id | |
| 118 | + postId | |
| 106 | 119 | }; |
| 120 | + }, | |
| 121 | + async loader(context) { | |
| 122 | + const { params, search } = context; | |
| 123 | + if (search.selectedBlock) { | |
| 124 | + (0, import_data.dispatch)(import_core_data.store).editEntityRecord( | |
| 125 | + "postType", | |
| 126 | + params.type, | |
| 127 | + getPostId(params), | |
| 128 | + { | |
| 129 | + selection: { | |
| 130 | + selectionStart: { clientId: search.selectedBlock }, | |
| 131 | + selectionEnd: { clientId: search.selectedBlock } | |
| 132 | + } | |
| 133 | + }, | |
| 134 | + { undoIgnore: true } | |
| 135 | + ); | |
| 136 | + } | |
| 107 | 137 | } |
| 108 | 138 | }; |
| 109 | 139 | export { |
| 110 | 140 | route |