blocks
3 days ago
hooks
9 months ago
plugins
9 months ago
scripts
9 months ago
shared
3 days ago
store
2 years ago
blocks.js
3 days ago
util.js
1 year ago
blocks.js
146 lines
| 1 | /** |
| 2 | * Dynamically locate, load & register all Editor Blocks & Plugins. |
| 3 | */ |
| 4 | const { registerBlockType, unregisterBlockType } = wp.blocks; |
| 5 | const { registerPlugin, unregisterPlugin } = wp.plugins; |
| 6 | const { select, dispatch } = wp.data; |
| 7 | const { render } = wp.element; |
| 8 | |
| 9 | import store from "./store/player"; |
| 10 | |
| 11 | import "@/admin/blocks/shared/styles/gutenberg/index.scss"; |
| 12 | import ProUpgradeModal from "./shared/ProUpgradeModal"; |
| 13 | import "./hooks/apply-popup-preview-toggle"; |
| 14 | import "./hooks/apply-popup-image-trigger-placeholder"; |
| 15 | import "./hooks/apply-popup-button-trigger-ui"; |
| 16 | |
| 17 | /** |
| 18 | * No-op function for use as a default argument value. |
| 19 | * |
| 20 | * @returns null |
| 21 | */ |
| 22 | const noop = () => null; |
| 23 | |
| 24 | /** |
| 25 | * Require a set of modules and configure them for hot module replacement. |
| 26 | * |
| 27 | * The first argument should be a function returning a `require.context()` |
| 28 | * call. All modules loaded from this context are cached, and on each rebuild |
| 29 | * the incoming updated modules are checked against the cache. Updated modules |
| 30 | * which already exist in the cache are unregistered with the provided function, |
| 31 | * then any incoming (new or updated) modules will be registered. |
| 32 | * |
| 33 | * @param {Function} getContext Execute and return a `require.context()` call. |
| 34 | * @param {Function} register Function to register accepted modules. |
| 35 | * @param {Function} unregister Function to unregister replaced modules. |
| 36 | * @param {Function} [before] Function to run before updating moules. |
| 37 | * @param {Function} [after] Function to run after updating moules. |
| 38 | */ |
| 39 | const autoload = ({ |
| 40 | getContext, |
| 41 | register, |
| 42 | unregister, |
| 43 | before = noop, |
| 44 | after = noop, |
| 45 | }) => { |
| 46 | const cache = {}; |
| 47 | const loadModules = () => { |
| 48 | before(); |
| 49 | |
| 50 | const context = getContext(); |
| 51 | const changedNames = []; |
| 52 | context.keys().forEach((key) => { |
| 53 | const module = context(key); |
| 54 | if (module === cache[key]) { |
| 55 | // Module unchanged: no further action needed. |
| 56 | return; |
| 57 | } |
| 58 | if (cache[key]) { |
| 59 | // Module changed, and prior copy detected: unregister old module. |
| 60 | unregister(cache[key]); |
| 61 | } |
| 62 | // Register new module and update cache. |
| 63 | register(module); |
| 64 | changedNames.push(module.name); |
| 65 | cache[key] = module; |
| 66 | }); |
| 67 | |
| 68 | after(changedNames); |
| 69 | |
| 70 | // Return the context for HMR initialization. |
| 71 | return context; |
| 72 | }; |
| 73 | |
| 74 | const context = loadModules(); |
| 75 | |
| 76 | if (module.hot) { |
| 77 | module.hot.accept(context.id, loadModules); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | // Maintain the selected block ID across HMR updates. |
| 82 | let selectedBlockId = null; |
| 83 | const storeSelectedBlock = () => { |
| 84 | selectedBlockId = select("core/editor").getSelectedBlockClientId(); |
| 85 | dispatch("core/editor").clearSelectedBlock(); |
| 86 | }; |
| 87 | const refreshAllBlocks = (changedNames = []) => { |
| 88 | // Refresh all blocks by iteratively selecting each one. |
| 89 | select("core/editor") |
| 90 | .getBlocks() |
| 91 | .forEach(({ name, clientId }) => { |
| 92 | if (changedNames.includes(name)) { |
| 93 | dispatch("core/editor").selectBlock(clientId); |
| 94 | } |
| 95 | }); |
| 96 | // Reselect whatever was selected in the beginning. |
| 97 | if (selectedBlockId) { |
| 98 | dispatch("core/editor").selectBlock(selectedBlockId); |
| 99 | } else { |
| 100 | dispatch("core/editor").clearSelectedBlock(); |
| 101 | } |
| 102 | selectedBlockId = null; |
| 103 | }; |
| 104 | |
| 105 | // Load all block index files. |
| 106 | autoload({ |
| 107 | getContext: () => require.context("./blocks", true, /index\.js$/), |
| 108 | register: ({ name, metadata, options }) => { |
| 109 | const blockId = metadata ? { name, ...metadata } : name; |
| 110 | if (prestoPlayer?.isPremium) { |
| 111 | registerBlockType(blockId, options); |
| 112 | } else if (!options.premium) { |
| 113 | registerBlockType(blockId, options); |
| 114 | } |
| 115 | }, |
| 116 | unregister: ({ name }) => unregisterBlockType(name), |
| 117 | before: storeSelectedBlock, |
| 118 | after: refreshAllBlocks, |
| 119 | }); |
| 120 | |
| 121 | // Load all plugin files. |
| 122 | autoload({ |
| 123 | getContext: () => require.context("./plugins", true, /index\.js$/), |
| 124 | register: ({ name, options }) => registerPlugin(name, options), |
| 125 | unregister: ({ name }) => unregisterPlugin(name), |
| 126 | }); |
| 127 | |
| 128 | jQuery("body").append('<div id="presto-plugin-app"></div>'); |
| 129 | render(<ProUpgradeModal />, document.getElementById("presto-plugin-app")); |
| 130 | |
| 131 | // fetch settings |
| 132 | dispatch("presto-player/player").fetchOptions(); |
| 133 | |
| 134 | // Manage the is-editing-pp-video-block class for styling/functionality |
| 135 | wp.data.subscribe(function () { |
| 136 | const settings = wp.data.select("core/editor").getEditorSettings(); |
| 137 | if ( |
| 138 | !!settings?.onNavigateToPreviousEntityRecord && |
| 139 | wp.data.select("core/editor").getCurrentPostType() === "pp_video_block" |
| 140 | ) { |
| 141 | document.body.classList.add("is-editing-pp-video-block"); |
| 142 | } else { |
| 143 | document.body.classList.remove("is-editing-pp-video-block"); |
| 144 | } |
| 145 | }); |
| 146 |