build/scripts/widgets
blocks
7 months ago
index.js
50.1 KB
6 months ago
index.js.map
68.6 KB
6 months ago
index.min.asset.php
264 B
4 months ago
index.min.js
20.8 KB
4 months ago
index.min.js.map
72.3 KB
6 months ago
index.js in Gutenberg 23.2.0, at build/scripts/widgets/index.js
| 1 | var wp; |
| 2 | (wp ||= {}).widgets = (() => { |
| 3 | var __create = Object.create; |
| 4 | var __defProp = Object.defineProperty; |
| 5 | var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
| 6 | var __getOwnPropNames = Object.getOwnPropertyNames; |
| 7 | var __getProtoOf = Object.getPrototypeOf; |
| 8 | var __hasOwnProp = Object.prototype.hasOwnProperty; |
| 9 | var __commonJS = (cb, mod) => function __require() { |
| 10 | return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; |
| 11 | }; |
| 12 | var __export = (target, all) => { |
| 13 | for (var name3 in all) |
| 14 | __defProp(target, name3, { get: all[name3], enumerable: true }); |
| 15 | }; |
| 16 | var __copyProps = (to, from, except, desc) => { |
| 17 | if (from && typeof from === "object" || typeof from === "function") { |
| 18 | for (let key of __getOwnPropNames(from)) |
| 19 | if (!__hasOwnProp.call(to, key) && key !== except) |
| 20 | __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
| 21 | } |
| 22 | return to; |
| 23 | }; |
| 24 | var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( |
| 25 | // If the importer is in node compatibility mode or this is not an ESM |
| 26 | // file that has been converted to a CommonJS file using a Babel- |
| 27 | // compatible transform (i.e. "__esModule" has not been set), then set |
| 28 | // "default" to the CommonJS "module.exports" for node compatibility. |
| 29 | isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, |
| 30 | mod |
| 31 | )); |
| 32 | var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); |
| 33 | |
| 34 | // package-external:@wordpress/blocks |
| 35 | var require_blocks = __commonJS({ |
| 36 | "package-external:@wordpress/blocks"(exports, module) { |
| 37 | module.exports = window.wp.blocks; |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | // package-external:@wordpress/element |
| 42 | var require_element = __commonJS({ |
| 43 | "package-external:@wordpress/element"(exports, module) { |
| 44 | module.exports = window.wp.element; |
| 45 | } |
| 46 | }); |
| 47 | |
| 48 | // package-external:@wordpress/primitives |
| 49 | var require_primitives = __commonJS({ |
| 50 | "package-external:@wordpress/primitives"(exports, module) { |
| 51 | module.exports = window.wp.primitives; |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | // vendor-external:react/jsx-runtime |
| 56 | var require_jsx_runtime = __commonJS({ |
| 57 | "vendor-external:react/jsx-runtime"(exports, module) { |
| 58 | module.exports = window.ReactJSXRuntime; |
| 59 | } |
| 60 | }); |
| 61 | |
| 62 | // package-external:@wordpress/block-editor |
| 63 | var require_block_editor = __commonJS({ |
| 64 | "package-external:@wordpress/block-editor"(exports, module) { |
| 65 | module.exports = window.wp.blockEditor; |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | // package-external:@wordpress/components |
| 70 | var require_components = __commonJS({ |
| 71 | "package-external:@wordpress/components"(exports, module) { |
| 72 | module.exports = window.wp.components; |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | // package-external:@wordpress/i18n |
| 77 | var require_i18n = __commonJS({ |
| 78 | "package-external:@wordpress/i18n"(exports, module) { |
| 79 | module.exports = window.wp.i18n; |
| 80 | } |
| 81 | }); |
| 82 | |
| 83 | // package-external:@wordpress/core-data |
| 84 | var require_core_data = __commonJS({ |
| 85 | "package-external:@wordpress/core-data"(exports, module) { |
| 86 | module.exports = window.wp.coreData; |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | // package-external:@wordpress/data |
| 91 | var require_data = __commonJS({ |
| 92 | "package-external:@wordpress/data"(exports, module) { |
| 93 | module.exports = window.wp.data; |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | // package-external:@wordpress/notices |
| 98 | var require_notices = __commonJS({ |
| 99 | "package-external:@wordpress/notices"(exports, module) { |
| 100 | module.exports = window.wp.notices; |
| 101 | } |
| 102 | }); |
| 103 | |
| 104 | // package-external:@wordpress/compose |
| 105 | var require_compose = __commonJS({ |
| 106 | "package-external:@wordpress/compose"(exports, module) { |
| 107 | module.exports = window.wp.compose; |
| 108 | } |
| 109 | }); |
| 110 | |
| 111 | // package-external:@wordpress/api-fetch |
| 112 | var require_api_fetch = __commonJS({ |
| 113 | "package-external:@wordpress/api-fetch"(exports, module) { |
| 114 | module.exports = window.wp.apiFetch; |
| 115 | } |
| 116 | }); |
| 117 | |
| 118 | // packages/widgets/build-module/index.mjs |
| 119 | var index_exports = {}; |
| 120 | __export(index_exports, { |
| 121 | MoveToWidgetArea: () => MoveToWidgetArea, |
| 122 | addWidgetIdToBlock: () => addWidgetIdToBlock, |
| 123 | getWidgetIdFromBlock: () => getWidgetIdFromBlock, |
| 124 | registerLegacyWidgetBlock: () => registerLegacyWidgetBlock, |
| 125 | registerLegacyWidgetVariations: () => registerLegacyWidgetVariations, |
| 126 | registerWidgetGroupBlock: () => registerWidgetGroupBlock |
| 127 | }); |
| 128 | var import_blocks5 = __toESM(require_blocks(), 1); |
| 129 | |
| 130 | // packages/widgets/build-module/blocks/legacy-widget/index.mjs |
| 131 | var legacy_widget_exports = {}; |
| 132 | __export(legacy_widget_exports, { |
| 133 | metadata: () => block_default, |
| 134 | name: () => name, |
| 135 | settings: () => settings |
| 136 | }); |
| 137 | |
| 138 | // packages/icons/build-module/library/brush.mjs |
| 139 | var import_primitives = __toESM(require_primitives(), 1); |
| 140 | var import_jsx_runtime = __toESM(require_jsx_runtime(), 1); |
| 141 | var brush_default = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_primitives.SVG, { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_primitives.Path, { d: "M4 20h8v-1.5H4V20zM18.9 3.5c-.6-.6-1.5-.6-2.1 0l-7.2 7.2c-.4-.1-.7 0-1.1.1-.5.2-1.5.7-1.9 2.2-.4 1.7-.8 2.2-1.1 2.7-.1.1-.2.3-.3.4l-.6 1.1H6c2 0 3.4-.4 4.7-1.4.8-.6 1.2-1.4 1.3-2.3 0-.3 0-.5-.1-.7L19 5.7c.5-.6.5-1.6-.1-2.2zM9.7 14.7c-.7.5-1.5.8-2.4 1 .2-.5.5-1.2.8-2.3.2-.6.4-1 .8-1.1.5-.1 1 .1 1.3.3.2.2.3.5.2.8 0 .3-.1.9-.7 1.3z" }) }); |
| 142 | |
| 143 | // packages/icons/build-module/library/group.mjs |
| 144 | var import_primitives2 = __toESM(require_primitives(), 1); |
| 145 | var import_jsx_runtime2 = __toESM(require_jsx_runtime(), 1); |
| 146 | var group_default = /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_primitives2.SVG, { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_primitives2.Path, { d: "M18 4h-7c-1.1 0-2 .9-2 2v3H6c-1.1 0-2 .9-2 2v7c0 1.1.9 2 2 2h7c1.1 0 2-.9 2-2v-3h3c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm-4.5 14c0 .3-.2.5-.5.5H6c-.3 0-.5-.2-.5-.5v-7c0-.3.2-.5.5-.5h3V13c0 1.1.9 2 2 2h2.5v3zm0-4.5H11c-.3 0-.5-.2-.5-.5v-2.5H13c.3 0 .5.2.5.5v2.5zm5-.5c0 .3-.2.5-.5.5h-3V11c0-1.1-.9-2-2-2h-2.5V6c0-.3.2-.5.5-.5h7c.3 0 .5.2.5.5v7z" }) }); |
| 147 | |
| 148 | // packages/icons/build-module/library/move-to.mjs |
| 149 | var import_primitives3 = __toESM(require_primitives(), 1); |
| 150 | var import_jsx_runtime3 = __toESM(require_jsx_runtime(), 1); |
| 151 | var move_to_default = /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_primitives3.SVG, { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_primitives3.Path, { d: "M19.75 9c0-1.257-.565-2.197-1.39-2.858-.797-.64-1.827-1.017-2.815-1.247-1.802-.42-3.703-.403-4.383-.396L11 4.5V6l.177-.001c.696-.006 2.416-.02 4.028.356.887.207 1.67.518 2.216.957.52.416.829.945.829 1.688 0 .592-.167.966-.407 1.23-.255.281-.656.508-1.236.674-1.19.34-2.82.346-4.607.346h-.077c-1.692 0-3.527 0-4.942.404-.732.209-1.424.545-1.935 1.108-.526.579-.796 1.33-.796 2.238 0 1.257.565 2.197 1.39 2.858.797.64 1.827 1.017 2.815 1.247 1.802.42 3.703.403 4.383.396L13 19.5h.714V22L18 18.5 13.714 15v3H13l-.177.001c-.696.006-2.416.02-4.028-.356-.887-.207-1.67-.518-2.216-.957-.52-.416-.829-.945-.829-1.688 0-.592.167-.966.407-1.23.255-.281.656-.508 1.237-.674 1.189-.34 2.819-.346 4.606-.346h.077c1.692 0 3.527 0 4.941-.404.732-.209 1.425-.545 1.936-1.108.526-.579.796-1.33.796-2.238z" }) }); |
| 152 | |
| 153 | // packages/icons/build-module/library/widget.mjs |
| 154 | var import_primitives4 = __toESM(require_primitives(), 1); |
| 155 | var import_jsx_runtime4 = __toESM(require_jsx_runtime(), 1); |
| 156 | var widget_default = /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_primitives4.SVG, { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_primitives4.Path, { d: "M6 3H8V5H16V3H18V5C19.1046 5 20 5.89543 20 7V19C20 20.1046 19.1046 21 18 21H6C4.89543 21 4 20.1046 4 19V7C4 5.89543 4.89543 5 6 5V3ZM18 6.5H6C5.72386 6.5 5.5 6.72386 5.5 7V8H18.5V7C18.5 6.72386 18.2761 6.5 18 6.5ZM18.5 9.5H5.5V19C5.5 19.2761 5.72386 19.5 6 19.5H18C18.2761 19.5 18.5 19.2761 18.5 19V9.5ZM11 11H13V13H11V11ZM7 11V13H9V11H7ZM15 13V11H17V13H15Z" }) }); |
| 157 | |
| 158 | // packages/widgets/build-module/blocks/legacy-widget/block.json |
| 159 | var block_default = { |
| 160 | $schema: "https://schemas.wp.org/trunk/block.json", |
| 161 | apiVersion: 3, |
| 162 | name: "core/legacy-widget", |
| 163 | title: "Legacy Widget", |
| 164 | category: "widgets", |
| 165 | description: "Display a legacy widget.", |
| 166 | textdomain: "default", |
| 167 | attributes: { |
| 168 | id: { |
| 169 | type: "string", |
| 170 | default: null |
| 171 | }, |
| 172 | idBase: { |
| 173 | type: "string", |
| 174 | default: null |
| 175 | }, |
| 176 | instance: { |
| 177 | type: "object", |
| 178 | default: null |
| 179 | } |
| 180 | }, |
| 181 | supports: { |
| 182 | html: false, |
| 183 | customClassName: false, |
| 184 | reusable: false |
| 185 | }, |
| 186 | editorStyle: "wp-block-legacy-widget-editor" |
| 187 | }; |
| 188 | |
| 189 | // node_modules/clsx/dist/clsx.mjs |
| 190 | function r(e) { |
| 191 | var t, f, n = ""; |
| 192 | if ("string" == typeof e || "number" == typeof e) n += e; |
| 193 | else if ("object" == typeof e) if (Array.isArray(e)) { |
| 194 | var o = e.length; |
| 195 | for (t = 0; t < o; t++) e[t] && (f = r(e[t])) && (n && (n += " "), n += f); |
| 196 | } else for (f in e) e[f] && (n && (n += " "), n += f); |
| 197 | return n; |
| 198 | } |
| 199 | function clsx() { |
| 200 | for (var e, t, f = 0, n = "", o = arguments.length; f < o; f++) (e = arguments[f]) && (t = r(e)) && (n && (n += " "), n += t); |
| 201 | return n; |
| 202 | } |
| 203 | var clsx_default = clsx; |
| 204 | |
| 205 | // packages/widgets/build-module/blocks/legacy-widget/edit/index.mjs |
| 206 | var import_block_editor3 = __toESM(require_block_editor(), 1); |
| 207 | var import_components5 = __toESM(require_components(), 1); |
| 208 | var import_i18n7 = __toESM(require_i18n(), 1); |
| 209 | var import_element3 = __toESM(require_element(), 1); |
| 210 | var import_core_data2 = __toESM(require_core_data(), 1); |
| 211 | |
| 212 | // packages/widgets/build-module/blocks/legacy-widget/edit/widget-type-selector.mjs |
| 213 | var import_components = __toESM(require_components(), 1); |
| 214 | var import_i18n = __toESM(require_i18n(), 1); |
| 215 | var import_data = __toESM(require_data(), 1); |
| 216 | var import_core_data = __toESM(require_core_data(), 1); |
| 217 | var import_block_editor = __toESM(require_block_editor(), 1); |
| 218 | var import_jsx_runtime5 = __toESM(require_jsx_runtime(), 1); |
| 219 | function WidgetTypeSelector({ selectedId, onSelect }) { |
| 220 | const widgetTypes = (0, import_data.useSelect)((select2) => { |
| 221 | const hiddenIds = select2(import_block_editor.store).getSettings()?.widgetTypesToHideFromLegacyWidgetBlock ?? []; |
| 222 | return select2(import_core_data.store).getWidgetTypes({ per_page: -1 })?.filter((widgetType) => !hiddenIds.includes(widgetType.id)); |
| 223 | }, []); |
| 224 | if (!widgetTypes) { |
| 225 | return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_components.Spinner, {}); |
| 226 | } |
| 227 | if (widgetTypes.length === 0) { |
| 228 | return (0, import_i18n.__)("There are no widgets available."); |
| 229 | } |
| 230 | return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)( |
| 231 | import_components.SelectControl, |
| 232 | { |
| 233 | __next40pxDefaultSize: true, |
| 234 | label: (0, import_i18n.__)("Legacy widget"), |
| 235 | value: selectedId ?? "", |
| 236 | options: [ |
| 237 | { value: "", label: (0, import_i18n.__)("Select widget") }, |
| 238 | ...widgetTypes.map((widgetType) => ({ |
| 239 | value: widgetType.id, |
| 240 | label: widgetType.name |
| 241 | })) |
| 242 | ], |
| 243 | onChange: (value) => { |
| 244 | if (value) { |
| 245 | const selected = widgetTypes.find( |
| 246 | (widgetType) => widgetType.id === value |
| 247 | ); |
| 248 | onSelect({ |
| 249 | selectedId: selected.id, |
| 250 | isMulti: selected.is_multi |
| 251 | }); |
| 252 | } else { |
| 253 | onSelect({ selectedId: null }); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | // packages/widgets/build-module/blocks/legacy-widget/edit/inspector-card.mjs |
| 261 | var import_jsx_runtime6 = __toESM(require_jsx_runtime(), 1); |
| 262 | function InspectorCard({ name: name3, description }) { |
| 263 | return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "wp-block-legacy-widget-inspector-card", children: [ |
| 264 | /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("h3", { className: "wp-block-legacy-widget-inspector-card__name", children: name3 }), |
| 265 | /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { children: description }) |
| 266 | ] }); |
| 267 | } |
| 268 | |
| 269 | // packages/widgets/build-module/blocks/legacy-widget/edit/form.mjs |
| 270 | var import_element = __toESM(require_element(), 1); |
| 271 | var import_data2 = __toESM(require_data(), 1); |
| 272 | var import_notices = __toESM(require_notices(), 1); |
| 273 | var import_i18n3 = __toESM(require_i18n(), 1); |
| 274 | var import_components2 = __toESM(require_components(), 1); |
| 275 | var import_compose2 = __toESM(require_compose(), 1); |
| 276 | |
| 277 | // packages/widgets/build-module/blocks/legacy-widget/edit/control.mjs |
| 278 | var import_api_fetch = __toESM(require_api_fetch(), 1); |
| 279 | var import_compose = __toESM(require_compose(), 1); |
| 280 | var import_i18n2 = __toESM(require_i18n(), 1); |
| 281 | var Control = class { |
| 282 | /** |
| 283 | * Creates and loads a new control. |
| 284 | * |
| 285 | * @access public |
| 286 | * @param {Object} params |
| 287 | * @param {string} params.id |
| 288 | * @param {string} params.idBase |
| 289 | * @param {Object} params.instance |
| 290 | * @param {Function} params.onChangeInstance |
| 291 | * @param {Function} params.onChangeHasPreview |
| 292 | * @param {Function} params.onError |
| 293 | */ |
| 294 | constructor({ |
| 295 | id, |
| 296 | idBase, |
| 297 | instance, |
| 298 | onChangeInstance, |
| 299 | onChangeHasPreview, |
| 300 | onError |
| 301 | }) { |
| 302 | this.id = id; |
| 303 | this.idBase = idBase; |
| 304 | this._instance = instance; |
| 305 | this._hasPreview = null; |
| 306 | this.onChangeInstance = onChangeInstance; |
| 307 | this.onChangeHasPreview = onChangeHasPreview; |
| 308 | this.onError = onError; |
| 309 | this.number = ++lastNumber; |
| 310 | this.handleFormChange = (0, import_compose.debounce)( |
| 311 | this.handleFormChange.bind(this), |
| 312 | 200 |
| 313 | ); |
| 314 | this.handleFormSubmit = this.handleFormSubmit.bind(this); |
| 315 | this.initDOM(); |
| 316 | this.bindEvents(); |
| 317 | this.loadContent(); |
| 318 | } |
| 319 | /** |
| 320 | * Clean up the control so that it can be garbage collected. |
| 321 | * |
| 322 | * @access public |
| 323 | */ |
| 324 | destroy() { |
| 325 | this.unbindEvents(); |
| 326 | this.element.remove(); |
| 327 | } |
| 328 | /** |
| 329 | * Creates the control's DOM structure. |
| 330 | * |
| 331 | * @access private |
| 332 | */ |
| 333 | initDOM() { |
| 334 | this.element = el("div", { class: "widget open" }, [ |
| 335 | el("div", { class: "widget-inside" }, [ |
| 336 | this.form = el("form", { class: "form", method: "post" }, [ |
| 337 | // These hidden form inputs are what most widgets' scripts |
| 338 | // use to access data about the widget. |
| 339 | el("input", { |
| 340 | class: "widget-id", |
| 341 | type: "hidden", |
| 342 | name: "widget-id", |
| 343 | value: this.id ?? `${this.idBase}-${this.number}` |
| 344 | }), |
| 345 | el("input", { |
| 346 | class: "id_base", |
| 347 | type: "hidden", |
| 348 | name: "id_base", |
| 349 | value: this.idBase ?? this.id |
| 350 | }), |
| 351 | el("input", { |
| 352 | class: "widget-width", |
| 353 | type: "hidden", |
| 354 | name: "widget-width", |
| 355 | value: "250" |
| 356 | }), |
| 357 | el("input", { |
| 358 | class: "widget-height", |
| 359 | type: "hidden", |
| 360 | name: "widget-height", |
| 361 | value: "200" |
| 362 | }), |
| 363 | el("input", { |
| 364 | class: "widget_number", |
| 365 | type: "hidden", |
| 366 | name: "widget_number", |
| 367 | value: this.idBase ? this.number.toString() : "" |
| 368 | }), |
| 369 | this.content = el("div", { class: "widget-content" }), |
| 370 | // Non-multi widgets can be saved via a Save button. |
| 371 | this.id && el( |
| 372 | "button", |
| 373 | { |
| 374 | class: "button is-primary", |
| 375 | type: "submit" |
| 376 | }, |
| 377 | (0, import_i18n2.__)("Save") |
| 378 | ) |
| 379 | ]) |
| 380 | ]) |
| 381 | ]); |
| 382 | } |
| 383 | /** |
| 384 | * Adds the control's event listeners. |
| 385 | * |
| 386 | * @access private |
| 387 | */ |
| 388 | bindEvents() { |
| 389 | if (window.jQuery) { |
| 390 | const { jQuery: $ } = window; |
| 391 | $(this.form).on("change", null, this.handleFormChange); |
| 392 | $(this.form).on("input", null, this.handleFormChange); |
| 393 | $(this.form).on("submit", this.handleFormSubmit); |
| 394 | } else { |
| 395 | this.form.addEventListener("change", this.handleFormChange); |
| 396 | this.form.addEventListener("input", this.handleFormChange); |
| 397 | this.form.addEventListener("submit", this.handleFormSubmit); |
| 398 | } |
| 399 | } |
| 400 | /** |
| 401 | * Removes the control's event listeners. |
| 402 | * |
| 403 | * @access private |
| 404 | */ |
| 405 | unbindEvents() { |
| 406 | if (window.jQuery) { |
| 407 | const { jQuery: $ } = window; |
| 408 | $(this.form).off("change", null, this.handleFormChange); |
| 409 | $(this.form).off("input", null, this.handleFormChange); |
| 410 | $(this.form).off("submit", this.handleFormSubmit); |
| 411 | } else { |
| 412 | this.form.removeEventListener("change", this.handleFormChange); |
| 413 | this.form.removeEventListener("input", this.handleFormChange); |
| 414 | this.form.removeEventListener("submit", this.handleFormSubmit); |
| 415 | } |
| 416 | } |
| 417 | /** |
| 418 | * Fetches the widget's form HTML from the REST API and loads it into the |
| 419 | * control's form. |
| 420 | * |
| 421 | * @access private |
| 422 | */ |
| 423 | async loadContent() { |
| 424 | try { |
| 425 | if (this.id) { |
| 426 | const { form } = await saveWidget(this.id); |
| 427 | this.content.innerHTML = form; |
| 428 | } else if (this.idBase) { |
| 429 | const { form, preview } = await encodeWidget({ |
| 430 | idBase: this.idBase, |
| 431 | instance: this.instance, |
| 432 | number: this.number |
| 433 | }); |
| 434 | this.content.innerHTML = form; |
| 435 | this.hasPreview = !isEmptyHTML(preview); |
| 436 | if (!this.instance.hash) { |
| 437 | const { instance } = await encodeWidget({ |
| 438 | idBase: this.idBase, |
| 439 | instance: this.instance, |
| 440 | number: this.number, |
| 441 | formData: serializeForm(this.form) |
| 442 | }); |
| 443 | this.instance = instance; |
| 444 | } |
| 445 | } |
| 446 | if (window.jQuery) { |
| 447 | const { jQuery: $ } = window; |
| 448 | $(document).trigger("widget-added", [$(this.element)]); |
| 449 | } |
| 450 | } catch (error) { |
| 451 | this.onError(error); |
| 452 | } |
| 453 | } |
| 454 | /** |
| 455 | * Perform a save when a multi widget's form is changed. Non-multi widgets |
| 456 | * are saved manually. |
| 457 | * |
| 458 | * @access private |
| 459 | */ |
| 460 | handleFormChange() { |
| 461 | if (this.idBase) { |
| 462 | this.saveForm(); |
| 463 | } |
| 464 | } |
| 465 | /** |
| 466 | * Perform a save when the control's form is manually submitted. |
| 467 | * |
| 468 | * @access private |
| 469 | * @param {Event} event |
| 470 | */ |
| 471 | handleFormSubmit(event) { |
| 472 | event.preventDefault(); |
| 473 | this.saveForm(); |
| 474 | } |
| 475 | /** |
| 476 | * Serialize the control's form, send it to the REST API, and update the |
| 477 | * instance with the encoded instance that the REST API returns. |
| 478 | * |
| 479 | * @access private |
| 480 | */ |
| 481 | async saveForm() { |
| 482 | const formData = serializeForm(this.form); |
| 483 | try { |
| 484 | if (this.id) { |
| 485 | const { form } = await saveWidget(this.id, formData); |
| 486 | this.content.innerHTML = form; |
| 487 | if (window.jQuery) { |
| 488 | const { jQuery: $ } = window; |
| 489 | $(document).trigger("widget-updated", [ |
| 490 | $(this.element) |
| 491 | ]); |
| 492 | } |
| 493 | } else if (this.idBase) { |
| 494 | const { instance, preview } = await encodeWidget({ |
| 495 | idBase: this.idBase, |
| 496 | instance: this.instance, |
| 497 | number: this.number, |
| 498 | formData |
| 499 | }); |
| 500 | this.instance = instance; |
| 501 | this.hasPreview = !isEmptyHTML(preview); |
| 502 | } |
| 503 | } catch (error) { |
| 504 | this.onError(error); |
| 505 | } |
| 506 | } |
| 507 | /** |
| 508 | * The widget's instance object. |
| 509 | * |
| 510 | * @access private |
| 511 | */ |
| 512 | get instance() { |
| 513 | return this._instance; |
| 514 | } |
| 515 | /** |
| 516 | * The widget's instance object. |
| 517 | * |
| 518 | * @access private |
| 519 | */ |
| 520 | set instance(instance) { |
| 521 | if (this._instance !== instance) { |
| 522 | this._instance = instance; |
| 523 | this.onChangeInstance(instance); |
| 524 | } |
| 525 | } |
| 526 | /** |
| 527 | * Whether or not the widget can be previewed. |
| 528 | * |
| 529 | * @access public |
| 530 | */ |
| 531 | get hasPreview() { |
| 532 | return this._hasPreview; |
| 533 | } |
| 534 | /** |
| 535 | * Whether or not the widget can be previewed. |
| 536 | * |
| 537 | * @access private |
| 538 | */ |
| 539 | set hasPreview(hasPreview) { |
| 540 | if (this._hasPreview !== hasPreview) { |
| 541 | this._hasPreview = hasPreview; |
| 542 | this.onChangeHasPreview(hasPreview); |
| 543 | } |
| 544 | } |
| 545 | }; |
| 546 | var lastNumber = 0; |
| 547 | function el(tagName, attributes = {}, content = null) { |
| 548 | const element = document.createElement(tagName); |
| 549 | for (const [attribute, value] of Object.entries(attributes)) { |
| 550 | element.setAttribute(attribute, value); |
| 551 | } |
| 552 | if (Array.isArray(content)) { |
| 553 | for (const child of content) { |
| 554 | if (child) { |
| 555 | element.appendChild(child); |
| 556 | } |
| 557 | } |
| 558 | } else if (typeof content === "string") { |
| 559 | element.innerText = content; |
| 560 | } |
| 561 | return element; |
| 562 | } |
| 563 | async function saveWidget(id, formData = null) { |
| 564 | let widget; |
| 565 | if (formData) { |
| 566 | widget = await (0, import_api_fetch.default)({ |
| 567 | path: `/wp/v2/widgets/${id}?context=edit`, |
| 568 | method: "PUT", |
| 569 | data: { |
| 570 | form_data: formData |
| 571 | } |
| 572 | }); |
| 573 | } else { |
| 574 | widget = await (0, import_api_fetch.default)({ |
| 575 | path: `/wp/v2/widgets/${id}?context=edit`, |
| 576 | method: "GET" |
| 577 | }); |
| 578 | } |
| 579 | return { form: widget.rendered_form }; |
| 580 | } |
| 581 | async function encodeWidget({ idBase, instance, number, formData = null }) { |
| 582 | const response = await (0, import_api_fetch.default)({ |
| 583 | path: `/wp/v2/widget-types/${idBase}/encode`, |
| 584 | method: "POST", |
| 585 | data: { |
| 586 | instance, |
| 587 | number, |
| 588 | form_data: formData |
| 589 | } |
| 590 | }); |
| 591 | return { |
| 592 | instance: response.instance, |
| 593 | form: response.form, |
| 594 | preview: response.preview |
| 595 | }; |
| 596 | } |
| 597 | function isEmptyHTML(html) { |
| 598 | const element = document.createElement("div"); |
| 599 | element.innerHTML = html; |
| 600 | return isEmptyNode(element); |
| 601 | } |
| 602 | function isEmptyNode(node) { |
| 603 | switch (node.nodeType) { |
| 604 | case node.TEXT_NODE: |
| 605 | return node.nodeValue.trim() === ""; |
| 606 | case node.ELEMENT_NODE: |
| 607 | if ([ |
| 608 | "AUDIO", |
| 609 | "CANVAS", |
| 610 | "EMBED", |
| 611 | "IFRAME", |
| 612 | "IMG", |
| 613 | "MATH", |
| 614 | "OBJECT", |
| 615 | "SVG", |
| 616 | "VIDEO" |
| 617 | ].includes(node.tagName)) { |
| 618 | return false; |
| 619 | } |
| 620 | if (!node.hasChildNodes()) { |
| 621 | return true; |
| 622 | } |
| 623 | return Array.from(node.childNodes).every(isEmptyNode); |
| 624 | default: |
| 625 | return true; |
| 626 | } |
| 627 | } |
| 628 | function serializeForm(form) { |
| 629 | return new window.URLSearchParams( |
| 630 | Array.from(new window.FormData(form)) |
| 631 | ).toString(); |
| 632 | } |
| 633 | |
| 634 | // packages/widgets/build-module/blocks/legacy-widget/edit/form.mjs |
| 635 | var import_jsx_runtime7 = __toESM(require_jsx_runtime(), 1); |
| 636 | function Form({ |
| 637 | title, |
| 638 | isVisible, |
| 639 | id, |
| 640 | idBase, |
| 641 | instance, |
| 642 | isWide, |
| 643 | onChangeInstance, |
| 644 | onChangeHasPreview |
| 645 | }) { |
| 646 | const ref = (0, import_element.useRef)(); |
| 647 | const isMediumLargeViewport = (0, import_compose2.useViewportMatch)("small"); |
| 648 | const outgoingInstances = (0, import_element.useRef)(/* @__PURE__ */ new Set()); |
| 649 | const incomingInstances = (0, import_element.useRef)(/* @__PURE__ */ new Set()); |
| 650 | const { createNotice } = (0, import_data2.useDispatch)(import_notices.store); |
| 651 | (0, import_element.useEffect)(() => { |
| 652 | if (incomingInstances.current.has(instance)) { |
| 653 | incomingInstances.current.delete(instance); |
| 654 | return; |
| 655 | } |
| 656 | const control = new Control({ |
| 657 | id, |
| 658 | idBase, |
| 659 | instance, |
| 660 | onChangeInstance(nextInstance) { |
| 661 | outgoingInstances.current.add(instance); |
| 662 | incomingInstances.current.add(nextInstance); |
| 663 | onChangeInstance(nextInstance); |
| 664 | }, |
| 665 | onChangeHasPreview, |
| 666 | onError(error) { |
| 667 | window.console.error(error); |
| 668 | createNotice( |
| 669 | "error", |
| 670 | (0, import_i18n3.sprintf)( |
| 671 | /* translators: %s: the name of the affected block. */ |
| 672 | (0, import_i18n3.__)( |
| 673 | 'The "%s" block was affected by errors and may not function properly. Check the developer tools for more details.' |
| 674 | ), |
| 675 | idBase || id |
| 676 | ) |
| 677 | ); |
| 678 | } |
| 679 | }); |
| 680 | ref.current.appendChild(control.element); |
| 681 | return () => { |
| 682 | if (outgoingInstances.current.has(instance)) { |
| 683 | outgoingInstances.current.delete(instance); |
| 684 | return; |
| 685 | } |
| 686 | control.destroy(); |
| 687 | }; |
| 688 | }, [ |
| 689 | id, |
| 690 | idBase, |
| 691 | instance, |
| 692 | onChangeInstance, |
| 693 | onChangeHasPreview, |
| 694 | isMediumLargeViewport |
| 695 | ]); |
| 696 | if (isWide && isMediumLargeViewport) { |
| 697 | return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)( |
| 698 | "div", |
| 699 | { |
| 700 | className: clsx_default({ |
| 701 | "wp-block-legacy-widget__container": isVisible |
| 702 | }), |
| 703 | children: [ |
| 704 | isVisible && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("h3", { className: "wp-block-legacy-widget__edit-form-title", children: title }), |
| 705 | /* @__PURE__ */ (0, import_jsx_runtime7.jsx)( |
| 706 | import_components2.Popover, |
| 707 | { |
| 708 | focusOnMount: false, |
| 709 | placement: "right", |
| 710 | offset: 32, |
| 711 | resize: false, |
| 712 | flip: false, |
| 713 | shift: true, |
| 714 | children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)( |
| 715 | "div", |
| 716 | { |
| 717 | ref, |
| 718 | className: "wp-block-legacy-widget__edit-form", |
| 719 | hidden: !isVisible |
| 720 | } |
| 721 | ) |
| 722 | } |
| 723 | ) |
| 724 | ] |
| 725 | } |
| 726 | ); |
| 727 | } |
| 728 | return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)( |
| 729 | "div", |
| 730 | { |
| 731 | ref, |
| 732 | className: "wp-block-legacy-widget__edit-form", |
| 733 | hidden: !isVisible, |
| 734 | children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("h3", { className: "wp-block-legacy-widget__edit-form-title", children: title }) |
| 735 | } |
| 736 | ); |
| 737 | } |
| 738 | |
| 739 | // packages/widgets/build-module/blocks/legacy-widget/edit/preview.mjs |
| 740 | var import_compose3 = __toESM(require_compose(), 1); |
| 741 | var import_element2 = __toESM(require_element(), 1); |
| 742 | var import_components3 = __toESM(require_components(), 1); |
| 743 | var import_i18n4 = __toESM(require_i18n(), 1); |
| 744 | var import_api_fetch2 = __toESM(require_api_fetch(), 1); |
| 745 | var import_jsx_runtime8 = __toESM(require_jsx_runtime(), 1); |
| 746 | function Preview({ idBase, instance, isVisible }) { |
| 747 | const [isLoaded, setIsLoaded] = (0, import_element2.useState)(false); |
| 748 | const [srcDoc, setSrcDoc] = (0, import_element2.useState)(""); |
| 749 | (0, import_element2.useEffect)(() => { |
| 750 | const abortController = typeof window.AbortController === "undefined" ? void 0 : new window.AbortController(); |
| 751 | async function fetchPreviewHTML() { |
| 752 | const restRoute = `/wp/v2/widget-types/${idBase}/render`; |
| 753 | return await (0, import_api_fetch2.default)({ |
| 754 | path: restRoute, |
| 755 | method: "POST", |
| 756 | signal: abortController?.signal, |
| 757 | data: instance ? { instance } : {} |
| 758 | }); |
| 759 | } |
| 760 | fetchPreviewHTML().then((response) => { |
| 761 | setSrcDoc(response.preview); |
| 762 | }).catch((error) => { |
| 763 | if ("AbortError" === error.name) { |
| 764 | return; |
| 765 | } |
| 766 | throw error; |
| 767 | }); |
| 768 | return () => abortController?.abort(); |
| 769 | }, [idBase, instance]); |
| 770 | const ref = (0, import_compose3.useRefEffect)( |
| 771 | (iframe) => { |
| 772 | if (!isLoaded) { |
| 773 | return; |
| 774 | } |
| 775 | function setHeight() { |
| 776 | const height = Math.max( |
| 777 | iframe.contentDocument.documentElement?.offsetHeight ?? 0, |
| 778 | iframe.contentDocument.body?.offsetHeight ?? 0 |
| 779 | ); |
| 780 | iframe.style.height = `${height !== 0 ? height : 100}px`; |
| 781 | } |
| 782 | const { IntersectionObserver } = iframe.ownerDocument.defaultView; |
| 783 | const intersectionObserver = new IntersectionObserver( |
| 784 | ([entry]) => { |
| 785 | if (entry.isIntersecting) { |
| 786 | setHeight(); |
| 787 | } |
| 788 | }, |
| 789 | { |
| 790 | threshold: 1 |
| 791 | } |
| 792 | ); |
| 793 | intersectionObserver.observe(iframe); |
| 794 | iframe.addEventListener("load", setHeight); |
| 795 | return () => { |
| 796 | intersectionObserver.disconnect(); |
| 797 | iframe.removeEventListener("load", setHeight); |
| 798 | }; |
| 799 | }, |
| 800 | [isLoaded] |
| 801 | ); |
| 802 | return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(import_jsx_runtime8.Fragment, { children: [ |
| 803 | isVisible && !isLoaded && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_components3.Placeholder, { children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_components3.Spinner, {}) }), |
| 804 | /* @__PURE__ */ (0, import_jsx_runtime8.jsx)( |
| 805 | "div", |
| 806 | { |
| 807 | className: clsx_default("wp-block-legacy-widget__edit-preview", { |
| 808 | "is-offscreen": !isVisible || !isLoaded |
| 809 | }), |
| 810 | children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(import_components3.Disabled, { children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)( |
| 811 | "iframe", |
| 812 | { |
| 813 | ref, |
| 814 | className: "wp-block-legacy-widget__edit-preview-iframe", |
| 815 | tabIndex: "-1", |
| 816 | title: (0, import_i18n4.__)("Legacy Widget Preview"), |
| 817 | srcDoc, |
| 818 | onLoad: (event) => { |
| 819 | event.target.contentDocument.body.style.overflow = "hidden"; |
| 820 | setIsLoaded(true); |
| 821 | }, |
| 822 | height: 100 |
| 823 | } |
| 824 | ) }) |
| 825 | } |
| 826 | ) |
| 827 | ] }); |
| 828 | } |
| 829 | |
| 830 | // packages/widgets/build-module/blocks/legacy-widget/edit/no-preview.mjs |
| 831 | var import_i18n5 = __toESM(require_i18n(), 1); |
| 832 | var import_jsx_runtime9 = __toESM(require_jsx_runtime(), 1); |
| 833 | function NoPreview({ name: name3 }) { |
| 834 | return /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("div", { className: "wp-block-legacy-widget__edit-no-preview", children: [ |
| 835 | name3 && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("h3", { children: name3 }), |
| 836 | /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("p", { children: (0, import_i18n5.__)("No preview available.") }) |
| 837 | ] }); |
| 838 | } |
| 839 | |
| 840 | // packages/widgets/build-module/blocks/legacy-widget/edit/convert-to-blocks-button.mjs |
| 841 | var import_data3 = __toESM(require_data(), 1); |
| 842 | var import_block_editor2 = __toESM(require_block_editor(), 1); |
| 843 | var import_components4 = __toESM(require_components(), 1); |
| 844 | var import_blocks = __toESM(require_blocks(), 1); |
| 845 | var import_i18n6 = __toESM(require_i18n(), 1); |
| 846 | var import_jsx_runtime10 = __toESM(require_jsx_runtime(), 1); |
| 847 | function ConvertToBlocksButton({ clientId, rawInstance }) { |
| 848 | const { replaceBlocks } = (0, import_data3.useDispatch)(import_block_editor2.store); |
| 849 | return /* @__PURE__ */ (0, import_jsx_runtime10.jsx)( |
| 850 | import_components4.ToolbarButton, |
| 851 | { |
| 852 | onClick: () => { |
| 853 | if (rawInstance.title) { |
| 854 | replaceBlocks(clientId, [ |
| 855 | (0, import_blocks.createBlock)("core/heading", { |
| 856 | content: rawInstance.title |
| 857 | }), |
| 858 | ...(0, import_blocks.rawHandler)({ HTML: rawInstance.text }) |
| 859 | ]); |
| 860 | } else { |
| 861 | replaceBlocks( |
| 862 | clientId, |
| 863 | (0, import_blocks.rawHandler)({ HTML: rawInstance.text }) |
| 864 | ); |
| 865 | } |
| 866 | }, |
| 867 | children: (0, import_i18n6.__)("Convert to blocks") |
| 868 | } |
| 869 | ); |
| 870 | } |
| 871 | |
| 872 | // packages/widgets/build-module/blocks/legacy-widget/edit/index.mjs |
| 873 | var import_jsx_runtime11 = __toESM(require_jsx_runtime(), 1); |
| 874 | function Edit(props) { |
| 875 | const { id, idBase } = props.attributes; |
| 876 | const { isWide = false } = props; |
| 877 | const blockProps = (0, import_block_editor3.useBlockProps)({ |
| 878 | className: clsx_default({ |
| 879 | "is-wide-widget": isWide |
| 880 | }) |
| 881 | }); |
| 882 | return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("div", { ...blockProps, children: !id && !idBase ? /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(Empty, { ...props }) : /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(NotEmpty, { ...props }) }); |
| 883 | } |
| 884 | function Empty({ attributes: { id, idBase }, setAttributes }) { |
| 885 | return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)( |
| 886 | import_components5.Placeholder, |
| 887 | { |
| 888 | icon: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_block_editor3.BlockIcon, { icon: brush_default }), |
| 889 | label: (0, import_i18n7.__)("Legacy Widget"), |
| 890 | children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_components5.Flex, { children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_components5.FlexBlock, { children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)( |
| 891 | WidgetTypeSelector, |
| 892 | { |
| 893 | selectedId: id ?? idBase, |
| 894 | onSelect: ({ selectedId, isMulti }) => { |
| 895 | if (!selectedId) { |
| 896 | setAttributes({ |
| 897 | id: null, |
| 898 | idBase: null, |
| 899 | instance: null |
| 900 | }); |
| 901 | } else if (isMulti) { |
| 902 | setAttributes({ |
| 903 | id: null, |
| 904 | idBase: selectedId, |
| 905 | instance: {} |
| 906 | }); |
| 907 | } else { |
| 908 | setAttributes({ |
| 909 | id: selectedId, |
| 910 | idBase: null, |
| 911 | instance: null |
| 912 | }); |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | ) }) }) |
| 917 | } |
| 918 | ); |
| 919 | } |
| 920 | function NotEmpty({ |
| 921 | attributes: { id, idBase, instance }, |
| 922 | setAttributes, |
| 923 | clientId, |
| 924 | isSelected, |
| 925 | isWide = false |
| 926 | }) { |
| 927 | const [hasPreview, setHasPreview] = (0, import_element3.useState)(null); |
| 928 | const widgetTypeId = id ?? idBase; |
| 929 | const { record: widgetType, hasResolved: hasResolvedWidgetType } = (0, import_core_data2.useEntityRecord)("root", "widgetType", widgetTypeId); |
| 930 | const setInstance = (0, import_element3.useCallback)((nextInstance) => { |
| 931 | setAttributes({ instance: nextInstance }); |
| 932 | }, []); |
| 933 | if (!widgetType && hasResolvedWidgetType) { |
| 934 | return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)( |
| 935 | import_components5.Placeholder, |
| 936 | { |
| 937 | icon: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_block_editor3.BlockIcon, { icon: brush_default }), |
| 938 | label: (0, import_i18n7.__)("Legacy Widget"), |
| 939 | children: (0, import_i18n7.__)("Widget is missing.") |
| 940 | } |
| 941 | ); |
| 942 | } |
| 943 | if (!hasResolvedWidgetType) { |
| 944 | return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_components5.Placeholder, { children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_components5.Spinner, {}) }); |
| 945 | } |
| 946 | const mode = idBase && !isSelected ? "preview" : "edit"; |
| 947 | return /* @__PURE__ */ (0, import_jsx_runtime11.jsxs)(import_jsx_runtime11.Fragment, { children: [ |
| 948 | idBase === "text" && /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_block_editor3.BlockControls, { group: "other", children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)( |
| 949 | ConvertToBlocksButton, |
| 950 | { |
| 951 | clientId, |
| 952 | rawInstance: instance.raw |
| 953 | } |
| 954 | ) }), |
| 955 | /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_block_editor3.InspectorControls, { children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)( |
| 956 | InspectorCard, |
| 957 | { |
| 958 | name: widgetType.name, |
| 959 | description: widgetType.description |
| 960 | } |
| 961 | ) }), |
| 962 | /* @__PURE__ */ (0, import_jsx_runtime11.jsx)( |
| 963 | Form, |
| 964 | { |
| 965 | title: widgetType.name, |
| 966 | isVisible: mode === "edit", |
| 967 | id, |
| 968 | idBase, |
| 969 | instance, |
| 970 | isWide, |
| 971 | onChangeInstance: setInstance, |
| 972 | onChangeHasPreview: setHasPreview |
| 973 | } |
| 974 | ), |
| 975 | idBase && /* @__PURE__ */ (0, import_jsx_runtime11.jsxs)(import_jsx_runtime11.Fragment, { children: [ |
| 976 | hasPreview === null && mode === "preview" && /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_components5.Placeholder, { children: /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_components5.Spinner, {}) }), |
| 977 | hasPreview === true && /* @__PURE__ */ (0, import_jsx_runtime11.jsx)( |
| 978 | Preview, |
| 979 | { |
| 980 | idBase, |
| 981 | instance, |
| 982 | isVisible: mode === "preview" |
| 983 | } |
| 984 | ), |
| 985 | hasPreview === false && mode === "preview" && /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(NoPreview, { name: widgetType.name }) |
| 986 | ] }) |
| 987 | ] }); |
| 988 | } |
| 989 | |
| 990 | // packages/widgets/build-module/blocks/legacy-widget/transforms.mjs |
| 991 | var import_blocks2 = __toESM(require_blocks(), 1); |
| 992 | var legacyWidgetTransforms = [ |
| 993 | { |
| 994 | block: "core/calendar", |
| 995 | widget: "calendar" |
| 996 | }, |
| 997 | { |
| 998 | block: "core/search", |
| 999 | widget: "search" |
| 1000 | }, |
| 1001 | { |
| 1002 | block: "core/html", |
| 1003 | widget: "custom_html", |
| 1004 | transform: ({ content }) => ({ |
| 1005 | content |
| 1006 | }) |
| 1007 | }, |
| 1008 | { |
| 1009 | block: "core/archives", |
| 1010 | widget: "archives", |
| 1011 | transform: ({ count, dropdown }) => { |
| 1012 | return { |
| 1013 | displayAsDropdown: !!dropdown, |
| 1014 | showPostCounts: !!count |
| 1015 | }; |
| 1016 | } |
| 1017 | }, |
| 1018 | { |
| 1019 | block: "core/latest-posts", |
| 1020 | widget: "recent-posts", |
| 1021 | transform: ({ show_date: displayPostDate, number }) => { |
| 1022 | return { |
| 1023 | displayPostDate: !!displayPostDate, |
| 1024 | postsToShow: number |
| 1025 | }; |
| 1026 | } |
| 1027 | }, |
| 1028 | { |
| 1029 | block: "core/latest-comments", |
| 1030 | widget: "recent-comments", |
| 1031 | transform: ({ number }) => { |
| 1032 | return { |
| 1033 | commentsToShow: number |
| 1034 | }; |
| 1035 | } |
| 1036 | }, |
| 1037 | { |
| 1038 | block: "core/tag-cloud", |
| 1039 | widget: "tag_cloud", |
| 1040 | transform: ({ taxonomy, count }) => { |
| 1041 | return { |
| 1042 | showTagCounts: !!count, |
| 1043 | taxonomy |
| 1044 | }; |
| 1045 | } |
| 1046 | }, |
| 1047 | { |
| 1048 | block: "core/categories", |
| 1049 | widget: "categories", |
| 1050 | transform: ({ count, dropdown, hierarchical }) => { |
| 1051 | return { |
| 1052 | displayAsDropdown: !!dropdown, |
| 1053 | showPostCounts: !!count, |
| 1054 | showHierarchy: !!hierarchical |
| 1055 | }; |
| 1056 | } |
| 1057 | }, |
| 1058 | { |
| 1059 | block: "core/audio", |
| 1060 | widget: "media_audio", |
| 1061 | transform: ({ url, preload, loop, attachment_id: id }) => { |
| 1062 | return { |
| 1063 | src: url, |
| 1064 | id, |
| 1065 | preload, |
| 1066 | loop |
| 1067 | }; |
| 1068 | } |
| 1069 | }, |
| 1070 | { |
| 1071 | block: "core/video", |
| 1072 | widget: "media_video", |
| 1073 | transform: ({ url, preload, loop, attachment_id: id }) => { |
| 1074 | return { |
| 1075 | src: url, |
| 1076 | id, |
| 1077 | preload, |
| 1078 | loop |
| 1079 | }; |
| 1080 | } |
| 1081 | }, |
| 1082 | { |
| 1083 | block: "core/image", |
| 1084 | widget: "media_image", |
| 1085 | transform: ({ |
| 1086 | alt, |
| 1087 | attachment_id: id, |
| 1088 | caption, |
| 1089 | height, |
| 1090 | link_classes: linkClass, |
| 1091 | link_rel: rel, |
| 1092 | link_target_blank: targetBlack, |
| 1093 | link_type: linkDestination, |
| 1094 | link_url: link, |
| 1095 | size: sizeSlug, |
| 1096 | url, |
| 1097 | width |
| 1098 | }) => { |
| 1099 | return { |
| 1100 | alt, |
| 1101 | caption, |
| 1102 | height, |
| 1103 | id, |
| 1104 | link, |
| 1105 | linkClass, |
| 1106 | linkDestination, |
| 1107 | linkTarget: targetBlack ? "_blank" : void 0, |
| 1108 | rel, |
| 1109 | sizeSlug, |
| 1110 | url, |
| 1111 | width |
| 1112 | }; |
| 1113 | } |
| 1114 | }, |
| 1115 | { |
| 1116 | block: "core/gallery", |
| 1117 | widget: "media_gallery", |
| 1118 | transform: ({ ids, link_type: linkTo, size, number }) => { |
| 1119 | return { |
| 1120 | ids, |
| 1121 | columns: number, |
| 1122 | linkTo, |
| 1123 | sizeSlug: size, |
| 1124 | images: ids.map((id) => ({ |
| 1125 | id |
| 1126 | })) |
| 1127 | }; |
| 1128 | } |
| 1129 | }, |
| 1130 | { |
| 1131 | block: "core/rss", |
| 1132 | widget: "rss", |
| 1133 | transform: ({ |
| 1134 | url, |
| 1135 | show_author: displayAuthor, |
| 1136 | show_date: displayDate, |
| 1137 | show_summary: displayExcerpt, |
| 1138 | items |
| 1139 | }) => { |
| 1140 | return { |
| 1141 | feedURL: url, |
| 1142 | displayAuthor: !!displayAuthor, |
| 1143 | displayDate: !!displayDate, |
| 1144 | displayExcerpt: !!displayExcerpt, |
| 1145 | itemsToShow: items |
| 1146 | }; |
| 1147 | } |
| 1148 | } |
| 1149 | ].map(({ block, widget, transform }) => { |
| 1150 | return { |
| 1151 | type: "block", |
| 1152 | blocks: [block], |
| 1153 | isMatch: ({ idBase, instance }) => { |
| 1154 | return idBase === widget && !!instance?.raw; |
| 1155 | }, |
| 1156 | transform: ({ instance }) => { |
| 1157 | const transformedBlock = (0, import_blocks2.createBlock)( |
| 1158 | block, |
| 1159 | transform ? transform(instance.raw) : void 0 |
| 1160 | ); |
| 1161 | if (!instance.raw?.title) { |
| 1162 | return transformedBlock; |
| 1163 | } |
| 1164 | return [ |
| 1165 | (0, import_blocks2.createBlock)("core/heading", { |
| 1166 | content: instance.raw.title |
| 1167 | }), |
| 1168 | transformedBlock |
| 1169 | ]; |
| 1170 | } |
| 1171 | }; |
| 1172 | }); |
| 1173 | var transforms = { |
| 1174 | to: legacyWidgetTransforms |
| 1175 | }; |
| 1176 | var transforms_default = transforms; |
| 1177 | |
| 1178 | // packages/widgets/build-module/blocks/legacy-widget/index.mjs |
| 1179 | var { name } = block_default; |
| 1180 | var settings = { |
| 1181 | icon: widget_default, |
| 1182 | edit: Edit, |
| 1183 | transforms: transforms_default |
| 1184 | }; |
| 1185 | |
| 1186 | // packages/widgets/build-module/blocks/widget-group/index.mjs |
| 1187 | var widget_group_exports = {}; |
| 1188 | __export(widget_group_exports, { |
| 1189 | metadata: () => block_default2, |
| 1190 | name: () => name2, |
| 1191 | settings: () => settings2 |
| 1192 | }); |
| 1193 | var import_i18n9 = __toESM(require_i18n(), 1); |
| 1194 | var import_blocks3 = __toESM(require_blocks(), 1); |
| 1195 | |
| 1196 | // packages/widgets/build-module/blocks/widget-group/block.json |
| 1197 | var block_default2 = { |
| 1198 | $schema: "https://schemas.wp.org/trunk/block.json", |
| 1199 | apiVersion: 3, |
| 1200 | name: "core/widget-group", |
| 1201 | title: "Widget Group", |
| 1202 | category: "widgets", |
| 1203 | attributes: { |
| 1204 | title: { |
| 1205 | type: "string" |
| 1206 | } |
| 1207 | }, |
| 1208 | supports: { |
| 1209 | html: false, |
| 1210 | inserter: true, |
| 1211 | customClassName: true, |
| 1212 | reusable: false |
| 1213 | }, |
| 1214 | editorStyle: "wp-block-widget-group-editor", |
| 1215 | style: "wp-block-widget-group" |
| 1216 | }; |
| 1217 | |
| 1218 | // packages/widgets/build-module/blocks/widget-group/edit.mjs |
| 1219 | var import_block_editor4 = __toESM(require_block_editor(), 1); |
| 1220 | var import_components6 = __toESM(require_components(), 1); |
| 1221 | var import_i18n8 = __toESM(require_i18n(), 1); |
| 1222 | var import_data4 = __toESM(require_data(), 1); |
| 1223 | var import_jsx_runtime12 = __toESM(require_jsx_runtime(), 1); |
| 1224 | function Edit2(props) { |
| 1225 | const { clientId } = props; |
| 1226 | const hasInnerBlocks = (0, import_data4.useSelect)( |
| 1227 | (select2) => select2(import_block_editor4.store).getBlockCount(clientId) > 0, |
| 1228 | [clientId] |
| 1229 | ); |
| 1230 | return /* @__PURE__ */ (0, import_jsx_runtime12.jsx)("div", { ...(0, import_block_editor4.useBlockProps)({ className: "widget" }), children: !hasInnerBlocks ? /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(PlaceholderContent, { ...props }) : /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(PreviewContent, { ...props }) }); |
| 1231 | } |
| 1232 | function PlaceholderContent({ clientId }) { |
| 1233 | return /* @__PURE__ */ (0, import_jsx_runtime12.jsxs)(import_jsx_runtime12.Fragment, { children: [ |
| 1234 | /* @__PURE__ */ (0, import_jsx_runtime12.jsx)( |
| 1235 | import_components6.Placeholder, |
| 1236 | { |
| 1237 | className: "wp-block-widget-group__placeholder", |
| 1238 | icon: /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(import_block_editor4.BlockIcon, { icon: group_default }), |
| 1239 | label: (0, import_i18n8.__)("Widget Group"), |
| 1240 | children: /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(import_block_editor4.ButtonBlockAppender, { rootClientId: clientId }) |
| 1241 | } |
| 1242 | ), |
| 1243 | /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(import_block_editor4.InnerBlocks, { renderAppender: false }) |
| 1244 | ] }); |
| 1245 | } |
| 1246 | function PreviewContent({ attributes, setAttributes }) { |
| 1247 | return /* @__PURE__ */ (0, import_jsx_runtime12.jsxs)(import_jsx_runtime12.Fragment, { children: [ |
| 1248 | /* @__PURE__ */ (0, import_jsx_runtime12.jsx)( |
| 1249 | import_block_editor4.RichText, |
| 1250 | { |
| 1251 | tagName: "h2", |
| 1252 | identifier: "title", |
| 1253 | className: "widget-title", |
| 1254 | allowedFormats: [], |
| 1255 | placeholder: (0, import_i18n8.__)("Title"), |
| 1256 | value: attributes.title ?? "", |
| 1257 | onChange: (title) => setAttributes({ title }) |
| 1258 | } |
| 1259 | ), |
| 1260 | /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(import_block_editor4.InnerBlocks, {}) |
| 1261 | ] }); |
| 1262 | } |
| 1263 | |
| 1264 | // packages/widgets/build-module/blocks/widget-group/save.mjs |
| 1265 | var import_block_editor5 = __toESM(require_block_editor(), 1); |
| 1266 | var import_jsx_runtime13 = __toESM(require_jsx_runtime(), 1); |
| 1267 | function save({ attributes }) { |
| 1268 | return /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)(import_jsx_runtime13.Fragment, { children: [ |
| 1269 | /* @__PURE__ */ (0, import_jsx_runtime13.jsx)( |
| 1270 | import_block_editor5.RichText.Content, |
| 1271 | { |
| 1272 | tagName: "h2", |
| 1273 | className: "widget-title", |
| 1274 | value: attributes.title |
| 1275 | } |
| 1276 | ), |
| 1277 | /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("div", { className: "wp-widget-group__inner-blocks", children: /* @__PURE__ */ (0, import_jsx_runtime13.jsx)(import_block_editor5.InnerBlocks.Content, {}) }) |
| 1278 | ] }); |
| 1279 | } |
| 1280 | |
| 1281 | // packages/widgets/build-module/blocks/widget-group/deprecated.mjs |
| 1282 | var import_block_editor6 = __toESM(require_block_editor(), 1); |
| 1283 | var import_jsx_runtime14 = __toESM(require_jsx_runtime(), 1); |
| 1284 | var v1 = { |
| 1285 | attributes: { |
| 1286 | title: { |
| 1287 | type: "string" |
| 1288 | } |
| 1289 | }, |
| 1290 | supports: { |
| 1291 | html: false, |
| 1292 | inserter: true, |
| 1293 | customClassName: true, |
| 1294 | reusable: false |
| 1295 | }, |
| 1296 | save({ attributes }) { |
| 1297 | return /* @__PURE__ */ (0, import_jsx_runtime14.jsxs)(import_jsx_runtime14.Fragment, { children: [ |
| 1298 | /* @__PURE__ */ (0, import_jsx_runtime14.jsx)( |
| 1299 | import_block_editor6.RichText.Content, |
| 1300 | { |
| 1301 | tagName: "h2", |
| 1302 | className: "widget-title", |
| 1303 | value: attributes.title |
| 1304 | } |
| 1305 | ), |
| 1306 | /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(import_block_editor6.InnerBlocks.Content, {}) |
| 1307 | ] }); |
| 1308 | } |
| 1309 | }; |
| 1310 | var deprecated_default = [v1]; |
| 1311 | |
| 1312 | // packages/widgets/build-module/blocks/widget-group/index.mjs |
| 1313 | var { name: name2 } = block_default2; |
| 1314 | var settings2 = { |
| 1315 | title: (0, import_i18n9.__)("Widget Group"), |
| 1316 | description: (0, import_i18n9.__)( |
| 1317 | "Create a classic widget layout with a title that\u2019s styled by your theme for your widget areas." |
| 1318 | ), |
| 1319 | icon: group_default, |
| 1320 | __experimentalLabel: ({ name: label }) => label, |
| 1321 | edit: Edit2, |
| 1322 | save, |
| 1323 | transforms: { |
| 1324 | from: [ |
| 1325 | { |
| 1326 | type: "block", |
| 1327 | isMultiBlock: true, |
| 1328 | blocks: ["*"], |
| 1329 | isMatch(attributes, blocks) { |
| 1330 | return !blocks.some( |
| 1331 | (block) => block.name === "core/widget-group" |
| 1332 | ); |
| 1333 | }, |
| 1334 | __experimentalConvert(blocks) { |
| 1335 | let innerBlocks = [ |
| 1336 | ...blocks.map((block) => { |
| 1337 | return (0, import_blocks3.createBlock)( |
| 1338 | block.name, |
| 1339 | block.attributes, |
| 1340 | block.innerBlocks |
| 1341 | ); |
| 1342 | }) |
| 1343 | ]; |
| 1344 | const firstHeadingBlock = innerBlocks[0].name === "core/heading" ? innerBlocks[0] : null; |
| 1345 | innerBlocks = innerBlocks.filter( |
| 1346 | (block) => block !== firstHeadingBlock |
| 1347 | ); |
| 1348 | return (0, import_blocks3.createBlock)( |
| 1349 | "core/widget-group", |
| 1350 | { |
| 1351 | ...firstHeadingBlock && { |
| 1352 | title: firstHeadingBlock.attributes.content |
| 1353 | } |
| 1354 | }, |
| 1355 | innerBlocks |
| 1356 | ); |
| 1357 | } |
| 1358 | } |
| 1359 | ] |
| 1360 | }, |
| 1361 | deprecated: deprecated_default |
| 1362 | }; |
| 1363 | |
| 1364 | // packages/widgets/build-module/components/move-to-widget-area/index.mjs |
| 1365 | var import_components7 = __toESM(require_components(), 1); |
| 1366 | var import_i18n10 = __toESM(require_i18n(), 1); |
| 1367 | var import_jsx_runtime15 = __toESM(require_jsx_runtime(), 1); |
| 1368 | function MoveToWidgetArea({ |
| 1369 | currentWidgetAreaId, |
| 1370 | widgetAreas, |
| 1371 | onSelect |
| 1372 | }) { |
| 1373 | return /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_components7.ToolbarGroup, { children: /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_components7.ToolbarItem, { children: (toggleProps) => /* @__PURE__ */ (0, import_jsx_runtime15.jsx)( |
| 1374 | import_components7.DropdownMenu, |
| 1375 | { |
| 1376 | icon: move_to_default, |
| 1377 | label: (0, import_i18n10.__)("Move to widget area"), |
| 1378 | toggleProps, |
| 1379 | children: ({ onClose }) => /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_components7.MenuGroup, { label: (0, import_i18n10.__)("Move to"), children: /* @__PURE__ */ (0, import_jsx_runtime15.jsx)( |
| 1380 | import_components7.MenuItemsChoice, |
| 1381 | { |
| 1382 | choices: widgetAreas.map( |
| 1383 | (widgetArea) => ({ |
| 1384 | value: widgetArea.id, |
| 1385 | label: widgetArea.name, |
| 1386 | info: widgetArea.description |
| 1387 | }) |
| 1388 | ), |
| 1389 | value: currentWidgetAreaId, |
| 1390 | onSelect: (value) => { |
| 1391 | onSelect(value); |
| 1392 | onClose(); |
| 1393 | } |
| 1394 | } |
| 1395 | ) }) |
| 1396 | } |
| 1397 | ) }) }); |
| 1398 | } |
| 1399 | |
| 1400 | // packages/widgets/build-module/utils.mjs |
| 1401 | function getWidgetIdFromBlock(block) { |
| 1402 | return block.attributes.__internalWidgetId; |
| 1403 | } |
| 1404 | function addWidgetIdToBlock(block, widgetId) { |
| 1405 | return { |
| 1406 | ...block, |
| 1407 | attributes: { |
| 1408 | ...block.attributes || {}, |
| 1409 | __internalWidgetId: widgetId |
| 1410 | } |
| 1411 | }; |
| 1412 | } |
| 1413 | |
| 1414 | // packages/widgets/build-module/register-legacy-widget-variations.mjs |
| 1415 | var import_data5 = __toESM(require_data(), 1); |
| 1416 | var import_core_data3 = __toESM(require_core_data(), 1); |
| 1417 | var import_blocks4 = __toESM(require_blocks(), 1); |
| 1418 | function registerLegacyWidgetVariations(settings3) { |
| 1419 | const unsubscribe = (0, import_data5.subscribe)(() => { |
| 1420 | const hiddenIds = settings3?.widgetTypesToHideFromLegacyWidgetBlock ?? []; |
| 1421 | const widgetTypes = (0, import_data5.select)(import_core_data3.store).getWidgetTypes({ per_page: -1 })?.filter((widgetType) => !hiddenIds.includes(widgetType.id)); |
| 1422 | if (widgetTypes) { |
| 1423 | unsubscribe(); |
| 1424 | (0, import_data5.dispatch)(import_blocks4.store).addBlockVariations( |
| 1425 | "core/legacy-widget", |
| 1426 | widgetTypes.map((widgetType) => ({ |
| 1427 | name: widgetType.id, |
| 1428 | title: widgetType.name, |
| 1429 | description: widgetType.description, |
| 1430 | attributes: widgetType.is_multi ? { |
| 1431 | idBase: widgetType.id, |
| 1432 | instance: {} |
| 1433 | } : { |
| 1434 | id: widgetType.id |
| 1435 | } |
| 1436 | })) |
| 1437 | ); |
| 1438 | } |
| 1439 | }); |
| 1440 | } |
| 1441 | |
| 1442 | // packages/widgets/build-module/index.mjs |
| 1443 | function registerLegacyWidgetBlock(supports = {}) { |
| 1444 | const { metadata, settings: settings3, name: name3 } = legacy_widget_exports; |
| 1445 | (0, import_blocks5.registerBlockType)( |
| 1446 | { name: name3, ...metadata }, |
| 1447 | { |
| 1448 | ...settings3, |
| 1449 | supports: { |
| 1450 | ...settings3.supports, |
| 1451 | ...supports |
| 1452 | } |
| 1453 | } |
| 1454 | ); |
| 1455 | } |
| 1456 | function registerWidgetGroupBlock(supports = {}) { |
| 1457 | const { metadata, settings: settings3, name: name3 } = widget_group_exports; |
| 1458 | (0, import_blocks5.registerBlockType)( |
| 1459 | { name: name3, ...metadata }, |
| 1460 | { |
| 1461 | ...settings3, |
| 1462 | supports: { |
| 1463 | ...settings3.supports, |
| 1464 | ...supports |
| 1465 | } |
| 1466 | } |
| 1467 | ); |
| 1468 | } |
| 1469 | return __toCommonJS(index_exports); |
| 1470 | })(); |
| 1471 | //# sourceMappingURL=index.js.map |
| 1472 |