@wordpress/edit-post
Version:
Edit Post module for WordPress.
252 lines (251 loc) • 8.61 kB
JavaScript
// packages/edit-post/src/index.jsx
import { store as blocksStore } from "@wordpress/blocks";
import {
registerCoreBlocks,
__experimentalRegisterExperimentalCoreBlocks
} from "@wordpress/block-library";
import deprecated from "@wordpress/deprecated";
import { createRoot, StrictMode } from "@wordpress/element";
import { dispatch, resolveSelect, select } from "@wordpress/data";
import { store as preferencesStore } from "@wordpress/preferences";
import {
registerLegacyWidgetBlock,
registerWidgetGroupBlock
} from "@wordpress/widgets";
import {
store as editorStore,
privateApis as editorPrivateApis
} from "@wordpress/editor";
import { store as coreDataStore } from "@wordpress/core-data";
import apiFetch from "@wordpress/api-fetch";
import Layout from "./components/layout/index.mjs";
import { unlock } from "./lock-unlock.mjs";
import { default as default2 } from "./components/back-button/fullscreen-mode-close.mjs";
import { store } from "./store/index.mjs";
export * from "./deprecated.mjs";
import { jsx } from "react/jsx-runtime";
var {
BackButton: __experimentalMainDashboardButton,
registerCoreBlockBindingsSources
} = unlock(editorPrivateApis);
var { enablePreloadMultiUse, clearPreloadedData } = unlock(
apiFetch.privateApis
);
function initializeEditor(id, postType, postId, settings, initialEdits) {
const target = document.getElementById(id);
const root = createRoot(target);
dispatch(preferencesStore).setDefaults("core/edit-post", {
fullscreenMode: true,
themeStyles: true,
welcomeGuide: true,
welcomeGuideTemplate: true
});
dispatch(preferencesStore).setDefaults("core", {
allowRightClickOverrides: true,
editorMode: "visual",
fixedToolbar: false,
hiddenBlockTypes: [],
inactivePanels: [],
openPanels: ["post-status"],
showBlockBreadcrumbs: true,
showIconLabels: false,
showListViewByDefault: false,
enableChoosePatternModal: true,
isPublishSidebarEnabled: true,
showCollaborationCursor: false,
showCollaborationJoinNotifications: true,
showCollaborationLeaveNotifications: true,
showCollaborationPostSaveNotifications: true
});
if (window.__clientSideMediaProcessing) {
dispatch(preferencesStore).setDefaults("core/media", {
requireApproval: true,
optimizeOnUpload: true
});
}
dispatch(blocksStore).reapplyBlockTypeFilters();
registerCoreBlocks();
registerCoreBlockBindingsSources();
registerLegacyWidgetBlock({ inserter: false });
registerWidgetGroupBlock({ inserter: false });
if (globalThis.IS_GUTENBERG_PLUGIN) {
__experimentalRegisterExperimentalCoreBlocks({
enableFSEBlocks: settings.__unstableEnableFullSiteEditingBlocks
});
}
const documentMode = document.compatMode === "CSS1Compat" ? "Standards" : "Quirks";
if (documentMode !== "Standards") {
console.warn(
"Your browser is using Quirks Mode. \nThis can cause rendering issues such as blocks overlaying meta boxes in the editor. Quirks Mode can be triggered by PHP errors or HTML code appearing before the opening <!DOCTYPE html>. Try checking the raw page source or your site's PHP error log and resolving errors there, removing any HTML before the doctype, or disabling plugins."
);
}
window.addEventListener("dragover", (e) => e.preventDefault(), false);
window.addEventListener("drop", (e) => e.preventDefault(), false);
enablePreloadMultiUse();
const preloadedResolutions = preloadResolutions(postType, postId);
preloadedResolutions.finally(() => {
clearPreloadedData();
if (postType && postId) {
const post = select(coreDataStore).getEntityRecord(
"postType",
postType,
postId
);
if (post) {
dispatch(editorStore).setupEditor(
post,
initialEdits,
settings.template
);
}
}
root.render(
/* @__PURE__ */ jsx(StrictMode, { children: /* @__PURE__ */ jsx(
Layout,
{
settings,
postId,
postType,
initialEdits
}
) })
);
});
return root;
}
async function preloadResolutions(postType, postId) {
const core = resolveSelect(coreDataStore);
const coreSelect = select(coreDataStore);
try {
await Promise.all([
core.getCurrentUser(),
core.getEntitiesConfig("postType"),
core.getEntitiesConfig("taxonomy"),
core.getEntitiesConfig("root"),
core.getEntityRecords("root", "taxonomy"),
core.getCurrentTheme(),
// Forward-resolver alias of `getCurrentTheme` with its own
// resolution metadata, so it needs a separate kick.
core.getThemeSupports(),
core.getBlockPatternCategories(),
core.__experimentalGetCurrentGlobalStylesId(),
core.__experimentalGetCurrentThemeBaseGlobalStyles(),
core.__experimentalGetCurrentThemeGlobalStylesVariations(),
core.getEntityRecord("root", "__unstableBase"),
core.canUser("read", { kind: "root", name: "site" }),
core.canUser("create", { kind: "postType", name: "attachment" }),
core.canUser("create", { kind: "postType", name: "page" }),
core.canUser("create", { kind: "postType", name: "wp_block" }),
core.canUser("create", {
kind: "postType",
name: "wp_template"
}),
// The inspector requests the entity form config when the document
// sidebar mounts. The args must match the `useViewConfig` call in
// `@wordpress/views` exactly for the resolution (and its preload
// entry) to be shared.
...postType ? [
unlock(core).getViewConfig("postType", postType, {
fields: "form"
}),
// The fields of the post type, which the DataForm post
// summary reads with `useFields`. The summary reports a
// failure of its own, so this must not cut the rest of
// the kickoff short.
unlock(core).getFieldsConfig("postType", postType).catch(() => {
})
] : [],
// The actions of the post type, which the actions menu of the
// document sidebar reads with `useEntityActions`. It reports a failure
// of its own, so this must not cut the rest of the kickoff short.
...postType ? [
unlock(core).getEntityActionsConfig("postType", postType).catch(() => {
})
] : [],
// Per-post resolvers. `getPostType` and `getEditedEntityRecord`
// are shorthand/forward-resolver aliases with their own
// resolution metadata, so they need separate kicks.
...postType && postId ? [
core.getPostType(postType),
core.getEntityRecord("postType", postType, postId),
core.getEditedEntityRecord(
"postType",
postType,
postId
),
core.getAutosaves(postType, postId),
core.getDefaultTemplateId({ slug: "front-page" }),
core.canUser("create", {
kind: "postType",
name: postType
})
] : []
]);
const tasks = [];
if (coreSelect.canUser("read", { kind: "root", name: "site" })) {
tasks.push(core.getEntityRecord("root", "site"));
}
const globalStylesId = coreSelect.__experimentalGetCurrentGlobalStylesId();
if (globalStylesId) {
tasks.push(
core.canUser("update", {
kind: "root",
name: "globalStyles",
id: globalStylesId
})
);
}
if (postType && postId) {
const post = coreSelect.getEntityRecord(
"postType",
postType,
postId
);
if (post) {
let slug = "page" === postType ? "page" : "single-" + postType;
if (post.slug) {
slug += "-" + post.slug;
}
tasks.push(core.getDefaultTemplateId({ slug }));
}
}
if (tasks.length) {
await Promise.all(tasks);
}
if (globalStylesId) {
if (coreSelect.canUser("update", {
kind: "root",
name: "globalStyles",
id: globalStylesId
})) {
await core.getEntityRecord(
"root",
"globalStyles",
globalStylesId
);
} else {
await core.getEntityRecord(
"root",
"globalStyles",
globalStylesId,
{ context: "view" }
);
}
}
} catch {
}
}
function reinitializeEditor() {
deprecated("wp.editPost.reinitializeEditor", {
since: "6.2",
version: "6.3"
});
}
export {
default2 as __experimentalFullscreenModeClose,
__experimentalMainDashboardButton,
initializeEditor,
reinitializeEditor,
store
};
//# sourceMappingURL=index.mjs.map