| 1 |
"use strict"; |
| 2 |
var wp; |
| 3 |
(wp ||= {}).coreData = (() => { |
| 4 |
var __create = Object.create; |
| 5 |
var __defProp = Object.defineProperty; |
| 6 |
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
| 7 |
var __getOwnPropNames = Object.getOwnPropertyNames; |
| 8 |
var __getProtoOf = Object.getPrototypeOf; |
| 9 |
var __hasOwnProp = Object.prototype.hasOwnProperty; |
| 10 |
var __commonJS = (cb, mod) => function __require() { |
| 11 |
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; |
| 12 |
}; |
| 13 |
var __export = (target, all) => { |
| 14 |
for (var name in all) |
| 15 |
__defProp(target, name, { get: all[name], enumerable: true }); |
| 16 |
}; |
| 17 |
var __copyProps = (to, from, except, desc) => { |
| 18 |
if (from && typeof from === "object" || typeof from === "function") { |
| 19 |
for (let key of __getOwnPropNames(from)) |
| 20 |
if (!__hasOwnProp.call(to, key) && key !== except) |
| 21 |
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
| 22 |
} |
| 23 |
return to; |
| 24 |
}; |
| 25 |
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( |
| 26 |
// If the importer is in node compatibility mode or this is not an ESM |
| 27 |
// file that has been converted to a CommonJS file using a Babel- |
| 28 |
// compatible transform (i.e. "__esModule" has not been set), then set |
| 29 |
// "default" to the CommonJS "module.exports" for node compatibility. |
| 30 |
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, |
| 31 |
mod |
| 32 |
)); |
| 33 |
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); |
| 34 |
|
| 35 |
// package-external:@wordpress/data |
| 36 |
var require_data = __commonJS({ |
| 37 |
"package-external:@wordpress/data"(exports, module) { |
| 38 |
module.exports = window.wp.data; |
| 39 |
} |
| 40 |
}); |
| 41 |
|
| 42 |
// node_modules/fast-deep-equal/es6/index.js |
| 43 |
var require_es6 = __commonJS({ |
| 44 |
"node_modules/fast-deep-equal/es6/index.js"(exports, module) { |
| 45 |
"use strict"; |
| 46 |
module.exports = function equal(a, b) { |
| 47 |
if (a === b) return true; |
| 48 |
if (a && b && typeof a == "object" && typeof b == "object") { |
| 49 |
if (a.constructor !== b.constructor) return false; |
| 50 |
var length, i, keys; |
| 51 |
if (Array.isArray(a)) { |
| 52 |
length = a.length; |
| 53 |
if (length != b.length) return false; |
| 54 |
for (i = length; i-- !== 0; ) |
| 55 |
if (!equal(a[i], b[i])) return false; |
| 56 |
return true; |
| 57 |
} |
| 58 |
if (a instanceof Map && b instanceof Map) { |
| 59 |
if (a.size !== b.size) return false; |
| 60 |
for (i of a.entries()) |
| 61 |
if (!b.has(i[0])) return false; |
| 62 |
for (i of a.entries()) |
| 63 |
if (!equal(i[1], b.get(i[0]))) return false; |
| 64 |
return true; |
| 65 |
} |
| 66 |
if (a instanceof Set && b instanceof Set) { |
| 67 |
if (a.size !== b.size) return false; |
| 68 |
for (i of a.entries()) |
| 69 |
if (!b.has(i[0])) return false; |
| 70 |
return true; |
| 71 |
} |
| 72 |
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) { |
| 73 |
length = a.length; |
| 74 |
if (length != b.length) return false; |
| 75 |
for (i = length; i-- !== 0; ) |
| 76 |
if (a[i] !== b[i]) return false; |
| 77 |
return true; |
| 78 |
} |
| 79 |
if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags; |
| 80 |
if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf(); |
| 81 |
if (a.toString !== Object.prototype.toString) return a.toString() === b.toString(); |
| 82 |
keys = Object.keys(a); |
| 83 |
length = keys.length; |
| 84 |
if (length !== Object.keys(b).length) return false; |
| 85 |
for (i = length; i-- !== 0; ) |
| 86 |
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false; |
| 87 |
for (i = length; i-- !== 0; ) { |
| 88 |
var key = keys[i]; |
| 89 |
if (!equal(a[key], b[key])) return false; |
| 90 |
} |
| 91 |
return true; |
| 92 |
} |
| 93 |
return a !== a && b !== b; |
| 94 |
}; |
| 95 |
} |
| 96 |
}); |
| 97 |
|
| 98 |
// package-external:@wordpress/compose |
| 99 |
var require_compose = __commonJS({ |
| 100 |
"package-external:@wordpress/compose"(exports, module) { |
| 101 |
module.exports = window.wp.compose; |
| 102 |
} |
| 103 |
}); |
| 104 |
|
| 105 |
// package-external:@wordpress/undo-manager |
| 106 |
var require_undo_manager = __commonJS({ |
| 107 |
"package-external:@wordpress/undo-manager"(exports, module) { |
| 108 |
module.exports = window.wp.undoManager; |
| 109 |
} |
| 110 |
}); |
| 111 |
|
| 112 |
// node_modules/equivalent-key-map/equivalent-key-map.js |
| 113 |
var require_equivalent_key_map = __commonJS({ |
| 114 |
"node_modules/equivalent-key-map/equivalent-key-map.js"(exports, module) { |
| 115 |
"use strict"; |
| 116 |
function _typeof(obj) { |
| 117 |
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { |
| 118 |
_typeof = function(obj2) { |
| 119 |
return typeof obj2; |
| 120 |
}; |
| 121 |
} else { |
| 122 |
_typeof = function(obj2) { |
| 123 |
return obj2 && typeof Symbol === "function" && obj2.constructor === Symbol && obj2 !== Symbol.prototype ? "symbol" : typeof obj2; |
| 124 |
}; |
| 125 |
} |
| 126 |
return _typeof(obj); |
| 127 |
} |
| 128 |
function _classCallCheck(instance, Constructor) { |
| 129 |
if (!(instance instanceof Constructor)) { |
| 130 |
throw new TypeError("Cannot call a class as a function"); |
| 131 |
} |
| 132 |
} |
| 133 |
function _defineProperties(target, props) { |
| 134 |
for (var i = 0; i < props.length; i++) { |
| 135 |
var descriptor = props[i]; |
| 136 |
descriptor.enumerable = descriptor.enumerable || false; |
| 137 |
descriptor.configurable = true; |
| 138 |
if ("value" in descriptor) descriptor.writable = true; |
| 139 |
Object.defineProperty(target, descriptor.key, descriptor); |
| 140 |
} |
| 141 |
} |
| 142 |
function _createClass(Constructor, protoProps, staticProps) { |
| 143 |
if (protoProps) _defineProperties(Constructor.prototype, protoProps); |
| 144 |
if (staticProps) _defineProperties(Constructor, staticProps); |
| 145 |
return Constructor; |
| 146 |
} |
| 147 |
function getValuePair(instance, key) { |
| 148 |
var _map = instance._map, _arrayTreeMap = instance._arrayTreeMap, _objectTreeMap = instance._objectTreeMap; |
| 149 |
if (_map.has(key)) { |
| 150 |
return _map.get(key); |
| 151 |
} |
| 152 |
var properties = Object.keys(key).sort(); |
| 153 |
var map = Array.isArray(key) ? _arrayTreeMap : _objectTreeMap; |
| 154 |
for (var i = 0; i < properties.length; i++) { |
| 155 |
var property = properties[i]; |
| 156 |
map = map.get(property); |
| 157 |
if (map === void 0) { |
| 158 |
return; |
| 159 |
} |
| 160 |
var propertyValue = key[property]; |
| 161 |
map = map.get(propertyValue); |
| 162 |
if (map === void 0) { |
| 163 |
return; |
| 164 |
} |
| 165 |
} |
| 166 |
var valuePair = map.get("_ekm_value"); |
| 167 |
if (!valuePair) { |
| 168 |
return; |
| 169 |
} |
| 170 |
_map.delete(valuePair[0]); |
| 171 |
valuePair[0] = key; |
| 172 |
map.set("_ekm_value", valuePair); |
| 173 |
_map.set(key, valuePair); |
| 174 |
return valuePair; |
| 175 |
} |
| 176 |
var EquivalentKeyMap2 = /* @__PURE__ */ (function() { |
| 177 |
function EquivalentKeyMap3(iterable) { |
| 178 |
_classCallCheck(this, EquivalentKeyMap3); |
| 179 |
this.clear(); |
| 180 |
if (iterable instanceof EquivalentKeyMap3) { |
| 181 |
var iterablePairs = []; |
| 182 |
iterable.forEach(function(value, key) { |
| 183 |
iterablePairs.push([key, value]); |
| 184 |
}); |
| 185 |
iterable = iterablePairs; |
| 186 |
} |
| 187 |
if (iterable != null) { |
| 188 |
for (var i = 0; i < iterable.length; i++) { |
| 189 |
this.set(iterable[i][0], iterable[i][1]); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
_createClass(EquivalentKeyMap3, [{ |
| 194 |
key: "set", |
| 195 |
/** |
| 196 |
* Add or update an element with a specified key and value. |
| 197 |
* |
| 198 |
* @param {*} key The key of the element to add. |
| 199 |
* @param {*} value The value of the element to add. |
| 200 |
* |
| 201 |
* @return {EquivalentKeyMap} Map instance. |
| 202 |
*/ |
| 203 |
value: function set(key, value) { |
| 204 |
if (key === null || _typeof(key) !== "object") { |
| 205 |
this._map.set(key, value); |
| 206 |
return this; |
| 207 |
} |
| 208 |
var properties = Object.keys(key).sort(); |
| 209 |
var valuePair = [key, value]; |
| 210 |
var map = Array.isArray(key) ? this._arrayTreeMap : this._objectTreeMap; |
| 211 |
for (var i = 0; i < properties.length; i++) { |
| 212 |
var property = properties[i]; |
| 213 |
if (!map.has(property)) { |
| 214 |
map.set(property, new EquivalentKeyMap3()); |
| 215 |
} |
| 216 |
map = map.get(property); |
| 217 |
var propertyValue = key[property]; |
| 218 |
if (!map.has(propertyValue)) { |
| 219 |
map.set(propertyValue, new EquivalentKeyMap3()); |
| 220 |
} |
| 221 |
map = map.get(propertyValue); |
| 222 |
} |
| 223 |
var previousValuePair = map.get("_ekm_value"); |
| 224 |
if (previousValuePair) { |
| 225 |
this._map.delete(previousValuePair[0]); |
| 226 |
} |
| 227 |
map.set("_ekm_value", valuePair); |
| 228 |
this._map.set(key, valuePair); |
| 229 |
return this; |
| 230 |
} |
| 231 |
/** |
| 232 |
* Returns a specified element. |
| 233 |
* |
| 234 |
* @param {*} key The key of the element to return. |
| 235 |
* |
| 236 |
* @return {?*} The element associated with the specified key or undefined |
| 237 |
* if the key can't be found. |
| 238 |
*/ |
| 239 |
}, { |
| 240 |
key: "get", |
| 241 |
value: function get(key) { |
| 242 |
if (key === null || _typeof(key) !== "object") { |
| 243 |
return this._map.get(key); |
| 244 |
} |
| 245 |
var valuePair = getValuePair(this, key); |
| 246 |
if (valuePair) { |
| 247 |
return valuePair[1]; |
| 248 |
} |
| 249 |
} |
| 250 |
/** |
| 251 |
* Returns a boolean indicating whether an element with the specified key |
| 252 |
* exists or not. |
| 253 |
* |
| 254 |
* @param {*} key The key of the element to test for presence. |
| 255 |
* |
| 256 |
* @return {boolean} Whether an element with the specified key exists. |
| 257 |
*/ |
| 258 |
}, { |
| 259 |
key: "has", |
| 260 |
value: function has(key) { |
| 261 |
if (key === null || _typeof(key) !== "object") { |
| 262 |
return this._map.has(key); |
| 263 |
} |
| 264 |
return getValuePair(this, key) !== void 0; |
| 265 |
} |
| 266 |
/** |
| 267 |
* Removes the specified element. |
| 268 |
* |
| 269 |
* @param {*} key The key of the element to remove. |
| 270 |
* |
| 271 |
* @return {boolean} Returns true if an element existed and has been |
| 272 |
* removed, or false if the element does not exist. |
| 273 |
*/ |
| 274 |
}, { |
| 275 |
key: "delete", |
| 276 |
value: function _delete(key) { |
| 277 |
if (!this.has(key)) { |
| 278 |
return false; |
| 279 |
} |
| 280 |
this.set(key, void 0); |
| 281 |
return true; |
| 282 |
} |
| 283 |
/** |
| 284 |
* Executes a provided function once per each key/value pair, in insertion |
| 285 |
* order. |
| 286 |
* |
| 287 |
* @param {Function} callback Function to execute for each element. |
| 288 |
* @param {*} thisArg Value to use as `this` when executing |
| 289 |
* `callback`. |
| 290 |
*/ |
| 291 |
}, { |
| 292 |
key: "forEach", |
| 293 |
value: function forEach(callback) { |
| 294 |
var _this = this; |
| 295 |
var thisArg = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : this; |
| 296 |
this._map.forEach(function(value, key) { |
| 297 |
if (key !== null && _typeof(key) === "object") { |
| 298 |
value = value[1]; |
| 299 |
} |
| 300 |
callback.call(thisArg, value, key, _this); |
| 301 |
}); |
| 302 |
} |
| 303 |
/** |
| 304 |
* Removes all elements. |
| 305 |
*/ |
| 306 |
}, { |
| 307 |
key: "clear", |
| 308 |
value: function clear() { |
| 309 |
this._map = /* @__PURE__ */ new Map(); |
| 310 |
this._arrayTreeMap = /* @__PURE__ */ new Map(); |
| 311 |
this._objectTreeMap = /* @__PURE__ */ new Map(); |
| 312 |
} |
| 313 |
}, { |
| 314 |
key: "size", |
| 315 |
get: function get() { |
| 316 |
return this._map.size; |
| 317 |
} |
| 318 |
}]); |
| 319 |
return EquivalentKeyMap3; |
| 320 |
})(); |
| 321 |
module.exports = EquivalentKeyMap2; |
| 322 |
} |
| 323 |
}); |
| 324 |
|
| 325 |
// package-external:@wordpress/url |
| 326 |
var require_url = __commonJS({ |
| 327 |
"package-external:@wordpress/url"(exports, module) { |
| 328 |
module.exports = window.wp.url; |
| 329 |
} |
| 330 |
}); |
| 331 |
|
| 332 |
// package-external:@wordpress/api-fetch |
| 333 |
var require_api_fetch = __commonJS({ |
| 334 |
"package-external:@wordpress/api-fetch"(exports, module) { |
| 335 |
module.exports = window.wp.apiFetch; |
| 336 |
} |
| 337 |
}); |
| 338 |
|
| 339 |
// package-external:@wordpress/blocks |
| 340 |
var require_blocks = __commonJS({ |
| 341 |
"package-external:@wordpress/blocks"(exports, module) { |
| 342 |
module.exports = window.wp.blocks; |
| 343 |
} |
| 344 |
}); |
| 345 |
|
| 346 |
// package-external:@wordpress/i18n |
| 347 |
var require_i18n = __commonJS({ |
| 348 |
"package-external:@wordpress/i18n"(exports, module) { |
| 349 |
module.exports = window.wp.i18n; |
| 350 |
} |
| 351 |
}); |
| 352 |
|
| 353 |
// package-external:@wordpress/sync |
| 354 |
var require_sync = __commonJS({ |
| 355 |
"package-external:@wordpress/sync"(exports, module) { |
| 356 |
module.exports = window.wp.sync; |
| 357 |
} |
| 358 |
}); |
| 359 |
|
| 360 |
// package-external:@wordpress/block-editor |
| 361 |
var require_block_editor = __commonJS({ |
| 362 |
"package-external:@wordpress/block-editor"(exports, module) { |
| 363 |
module.exports = window.wp.blockEditor; |
| 364 |
} |
| 365 |
}); |
| 366 |
|
| 367 |
// package-external:@wordpress/rich-text |
| 368 |
var require_rich_text = __commonJS({ |
| 369 |
"package-external:@wordpress/rich-text"(exports, module) { |
| 370 |
module.exports = window.wp.richText; |
| 371 |
} |
| 372 |
}); |
| 373 |
|
| 374 |
// package-external:@wordpress/private-apis |
| 375 |
var require_private_apis = __commonJS({ |
| 376 |
"package-external:@wordpress/private-apis"(exports, module) { |
| 377 |
module.exports = window.wp.privateApis; |
| 378 |
} |
| 379 |
}); |
| 380 |
|
| 381 |
// package-external:@wordpress/deprecated |
| 382 |
var require_deprecated = __commonJS({ |
| 383 |
"package-external:@wordpress/deprecated"(exports, module) { |
| 384 |
module.exports = window.wp.deprecated; |
| 385 |
} |
| 386 |
}); |
| 387 |
|
| 388 |
// package-external:@wordpress/html-entities |
| 389 |
var require_html_entities = __commonJS({ |
| 390 |
"package-external:@wordpress/html-entities"(exports, module) { |
| 391 |
module.exports = window.wp.htmlEntities; |
| 392 |
} |
| 393 |
}); |
| 394 |
|
| 395 |
// package-external:@wordpress/element |
| 396 |
var require_element = __commonJS({ |
| 397 |
"package-external:@wordpress/element"(exports, module) { |
| 398 |
module.exports = window.wp.element; |
| 399 |
} |
| 400 |
}); |
| 401 |
|
| 402 |
// vendor-external:react/jsx-runtime |
| 403 |
var require_jsx_runtime = __commonJS({ |
| 404 |
"vendor-external:react/jsx-runtime"(exports, module) { |
| 405 |
module.exports = window.ReactJSXRuntime; |
| 406 |
} |
| 407 |
}); |
| 408 |
|
| 409 |
// package-external:@wordpress/warning |
| 410 |
var require_warning = __commonJS({ |
| 411 |
"package-external:@wordpress/warning"(exports, module) { |
| 412 |
module.exports = window.wp.warning; |
| 413 |
} |
| 414 |
}); |
| 415 |
|
| 416 |
// packages/core-data/build-module/index.mjs |
| 417 |
var index_exports = {}; |
| 418 |
__export(index_exports, { |
| 419 |
EntityProvider: () => EntityProvider, |
| 420 |
SelectionDirection: () => SelectionDirection, |
| 421 |
SelectionType: () => SelectionType, |
| 422 |
__experimentalFetchLinkSuggestions: () => fetchLinkSuggestions, |
| 423 |
__experimentalFetchUrlData: () => experimental_fetch_url_data_default, |
| 424 |
__experimentalUseEntityRecord: () => __experimentalUseEntityRecord, |
| 425 |
__experimentalUseEntityRecords: () => __experimentalUseEntityRecords, |
| 426 |
__experimentalUseResourcePermissions: () => __experimentalUseResourcePermissions, |
| 427 |
fetchBlockPatterns: () => fetchBlockPatterns, |
| 428 |
privateApis: () => privateApis, |
| 429 |
store: () => store, |
| 430 |
useEntityBlockEditor: () => useEntityBlockEditor, |
| 431 |
useEntityId: () => useEntityId, |
| 432 |
useEntityProp: () => useEntityProp, |
| 433 |
useEntityRecord: () => useEntityRecord, |
| 434 |
useEntityRecords: () => useEntityRecords, |
| 435 |
useResourcePermissions: () => use_resource_permissions_default |
| 436 |
}); |
| 437 |
var import_data16 = __toESM(require_data(), 1); |
| 438 |
|
| 439 |
// packages/core-data/build-module/reducer.mjs |
| 440 |
var import_es64 = __toESM(require_es6(), 1); |
| 441 |
var import_compose2 = __toESM(require_compose(), 1); |
| 442 |
var import_data8 = __toESM(require_data(), 1); |
| 443 |
var import_undo_manager = __toESM(require_undo_manager(), 1); |
| 444 |
|
| 445 |
// packages/core-data/build-module/utils/conservative-map-item.mjs |
| 446 |
var import_es6 = __toESM(require_es6(), 1); |
| 447 |
function conservativeMapItem(item, nextItem) { |
| 448 |
if (!item) { |
| 449 |
return nextItem; |
| 450 |
} |
| 451 |
let hasChanges = false; |
| 452 |
const result = {}; |
| 453 |
for (const key in nextItem) { |
| 454 |
if ((0, import_es6.default)(item[key], nextItem[key])) { |
| 455 |
result[key] = item[key]; |
| 456 |
} else { |
| 457 |
hasChanges = true; |
| 458 |
result[key] = nextItem[key]; |
| 459 |
} |
| 460 |
} |
| 461 |
if (!hasChanges) { |
| 462 |
return item; |
| 463 |
} |
| 464 |
for (const key in item) { |
| 465 |
if (!result.hasOwnProperty(key)) { |
| 466 |
result[key] = item[key]; |
| 467 |
} |
| 468 |
} |
| 469 |
return result; |
| 470 |
} |
| 471 |
|
| 472 |
// packages/core-data/build-module/utils/get-normalized-comma-separable.mjs |
| 473 |
function getNormalizedCommaSeparable(value) { |
| 474 |
if (typeof value === "string") { |
| 475 |
return value.split(","); |
| 476 |
} else if (Array.isArray(value)) { |
| 477 |
return value; |
| 478 |
} |
| 479 |
return null; |
| 480 |
} |
| 481 |
var get_normalized_comma_separable_default = getNormalizedCommaSeparable; |
| 482 |
|
| 483 |
// packages/core-data/build-module/utils/if-matching-action.mjs |
| 484 |
var ifMatchingAction = (isMatch) => (reducer) => (state, action) => { |
| 485 |
if (state === void 0 || isMatch(action)) { |
| 486 |
return reducer(state, action); |
| 487 |
} |
| 488 |
return state; |
| 489 |
}; |
| 490 |
var if_matching_action_default = ifMatchingAction; |
| 491 |
|
| 492 |
// packages/core-data/build-module/utils/forward-resolver.mjs |
| 493 |
var forwardResolver = (resolverName) => (...args) => async ({ resolveSelect: resolveSelect2 }) => { |
| 494 |
await resolveSelect2[resolverName](...args); |
| 495 |
}; |
| 496 |
var forward_resolver_default = forwardResolver; |
| 497 |
|
| 498 |
// packages/core-data/build-module/utils/on-sub-key.mjs |
| 499 |
var onSubKey = (actionProperty) => (reducer) => (state = {}, action) => { |
| 500 |
const key = action[actionProperty]; |
| 501 |
if (key === void 0) { |
| 502 |
return state; |
| 503 |
} |
| 504 |
const nextKeyState = reducer(state[key], action); |
| 505 |
if (nextKeyState === state[key]) { |
| 506 |
return state; |
| 507 |
} |
| 508 |
return { |
| 509 |
...state, |
| 510 |
[key]: nextKeyState |
| 511 |
}; |
| 512 |
}; |
| 513 |
var on_sub_key_default = onSubKey; |
| 514 |
|
| 515 |
// packages/core-data/build-module/utils/replace-action.mjs |
| 516 |
var replaceAction = (replacer) => (reducer) => (state, action) => { |
| 517 |
return reducer(state, replacer(action)); |
| 518 |
}; |
| 519 |
var replace_action_default = replaceAction; |
| 520 |
|
| 521 |
// packages/core-data/build-module/utils/with-weak-map-cache.mjs |
| 522 |
function withWeakMapCache(fn) { |
| 523 |
const cache3 = /* @__PURE__ */ new WeakMap(); |
| 524 |
return (key) => { |
| 525 |
let value; |
| 526 |
if (cache3.has(key)) { |
| 527 |
value = cache3.get(key); |
| 528 |
} else { |
| 529 |
value = fn(key); |
| 530 |
if (key !== null && typeof key === "object") { |
| 531 |
cache3.set(key, value); |
| 532 |
} |
| 533 |
} |
| 534 |
return value; |
| 535 |
}; |
| 536 |
} |
| 537 |
var with_weak_map_cache_default = withWeakMapCache; |
| 538 |
|
| 539 |
// packages/core-data/build-module/utils/set-nested-value.mjs |
| 540 |
function setNestedValue(object, path, value) { |
| 541 |
if (!object || typeof object !== "object") { |
| 542 |
return object; |
| 543 |
} |
| 544 |
const normalizedPath = Array.isArray(path) ? path : path.split("."); |
| 545 |
normalizedPath.reduce((acc, key, idx) => { |
| 546 |
if (acc[key] === void 0) { |
| 547 |
if (Number.isInteger(normalizedPath[idx + 1])) { |
| 548 |
acc[key] = []; |
| 549 |
} else { |
| 550 |
acc[key] = {}; |
| 551 |
} |
| 552 |
} |
| 553 |
if (idx === normalizedPath.length - 1) { |
| 554 |
acc[key] = value; |
| 555 |
} |
| 556 |
return acc[key]; |
| 557 |
}, object); |
| 558 |
return object; |
| 559 |
} |
| 560 |
|
| 561 |
// packages/core-data/build-module/utils/get-nested-value.mjs |
| 562 |
function getNestedValue(object, path, defaultValue) { |
| 563 |
if (!object || typeof object !== "object" || typeof path !== "string" && !Array.isArray(path)) { |
| 564 |
return object; |
| 565 |
} |
| 566 |
const normalizedPath = Array.isArray(path) ? path : path.split("."); |
| 567 |
let value = object; |
| 568 |
normalizedPath.forEach((fieldName) => { |
| 569 |
value = value?.[fieldName]; |
| 570 |
}); |
| 571 |
return value !== void 0 ? value : defaultValue; |
| 572 |
} |
| 573 |
|
| 574 |
// packages/core-data/build-module/utils/is-numeric-id.mjs |
| 575 |
function isNumericID(id) { |
| 576 |
return /^\s*\d+\s*$/.test(id); |
| 577 |
} |
| 578 |
|
| 579 |
// packages/core-data/build-module/utils/user-permissions.mjs |
| 580 |
var ALLOWED_RESOURCE_ACTIONS = [ |
| 581 |
"create", |
| 582 |
"read", |
| 583 |
"update", |
| 584 |
"delete" |
| 585 |
]; |
| 586 |
function getUserPermissionsFromAllowHeader(allowedMethods) { |
| 587 |
const permissions = {}; |
| 588 |
const methods = { |
| 589 |
create: "POST", |
| 590 |
read: "GET", |
| 591 |
update: "PUT", |
| 592 |
delete: "DELETE" |
| 593 |
}; |
| 594 |
for (const [actionName, methodName] of Object.entries(methods)) { |
| 595 |
permissions[actionName] = allowedMethods ? allowedMethods.includes(methodName) : false; |
| 596 |
} |
| 597 |
return permissions; |
| 598 |
} |
| 599 |
function getUserPermissionCacheKey(action, resource, id) { |
| 600 |
const key = (typeof resource === "object" ? [action, resource.kind, resource.name, resource.id] : [action, resource, id]).filter(Boolean).join("/"); |
| 601 |
return key; |
| 602 |
} |
| 603 |
|
| 604 |
// packages/core-data/build-module/utils/receive-intermediate-results.mjs |
| 605 |
var RECEIVE_INTERMEDIATE_RESULTS = /* @__PURE__ */ Symbol( |
| 606 |
"RECEIVE_INTERMEDIATE_RESULTS" |
| 607 |
); |
| 608 |
|
| 609 |
// packages/core-data/build-module/utils/normalize-query-for-resolution.mjs |
| 610 |
function normalizeQueryForResolution(query) { |
| 611 |
if (!query) { |
| 612 |
return void 0; |
| 613 |
} |
| 614 |
const entries = Object.entries(query).filter( |
| 615 |
([k, v]) => (k === "context" || k === "_fields") && v !== void 0 && v !== null |
| 616 |
); |
| 617 |
return entries.length > 0 ? Object.fromEntries(entries) : void 0; |
| 618 |
} |
| 619 |
|
| 620 |
// packages/core-data/build-module/queried-data/actions.mjs |
| 621 |
function receiveItems(items2, edits, meta) { |
| 622 |
return { |
| 623 |
type: "RECEIVE_ITEMS", |
| 624 |
items: items2, |
| 625 |
persistedEdits: edits, |
| 626 |
meta |
| 627 |
}; |
| 628 |
} |
| 629 |
function removeItems(kind, name, records, invalidateCache = false) { |
| 630 |
return { |
| 631 |
type: "REMOVE_ITEMS", |
| 632 |
itemIds: Array.isArray(records) ? records : [records], |
| 633 |
kind, |
| 634 |
name, |
| 635 |
invalidateCache |
| 636 |
}; |
| 637 |
} |
| 638 |
function receiveQueriedItems(items2, query = {}, edits, meta) { |
| 639 |
return { |
| 640 |
...receiveItems(items2, edits, meta), |
| 641 |
query |
| 642 |
}; |
| 643 |
} |
| 644 |
|
| 645 |
// packages/core-data/build-module/queried-data/selectors.mjs |
| 646 |
var import_equivalent_key_map = __toESM(require_equivalent_key_map(), 1); |
| 647 |
var import_data = __toESM(require_data(), 1); |
| 648 |
|
| 649 |
// packages/core-data/build-module/queried-data/get-query-parts.mjs |
| 650 |
var import_url = __toESM(require_url(), 1); |
| 651 |
function getQueryParts(query) { |
| 652 |
const parts = { |
| 653 |
stableKey: "", |
| 654 |
page: 1, |
| 655 |
perPage: 10, |
| 656 |
offset: null, |
| 657 |
fields: null, |
| 658 |
include: null, |
| 659 |
context: "default" |
| 660 |
}; |
| 661 |
const keys = Object.keys(query).sort(); |
| 662 |
for (let i = 0; i < keys.length; i++) { |
| 663 |
const key = keys[i]; |
| 664 |
let value = query[key]; |
| 665 |
switch (key) { |
| 666 |
case "page": |
| 667 |
parts[key] = Number(value); |
| 668 |
break; |
| 669 |
case "per_page": |
| 670 |
parts.perPage = Number(value); |
| 671 |
break; |
| 672 |
case "offset": { |
| 673 |
const numericOffset = Number(value); |
| 674 |
if (Number.isFinite(numericOffset)) { |
| 675 |
parts.offset = numericOffset; |
| 676 |
} |
| 677 |
break; |
| 678 |
} |
| 679 |
case "context": |
| 680 |
parts.context = value; |
| 681 |
break; |
| 682 |
default: |
| 683 |
if (key === "_fields") { |
| 684 |
parts.fields = get_normalized_comma_separable_default(value) ?? []; |
| 685 |
value = parts.fields.join(); |
| 686 |
} |
| 687 |
if (key === "include") { |
| 688 |
if (typeof value === "number") { |
| 689 |
value = value.toString(); |
| 690 |
} |
| 691 |
parts.include = (get_normalized_comma_separable_default(value) ?? []).map(Number); |
| 692 |
value = parts.include.join(); |
| 693 |
} |
| 694 |
parts.stableKey += (parts.stableKey ? "&" : "") + (0, import_url.addQueryArgs)("", { [key]: value }).slice(1); |
| 695 |
} |
| 696 |
} |
| 697 |
return parts; |
| 698 |
} |
| 699 |
var get_query_parts_default = with_weak_map_cache_default(getQueryParts); |
| 700 |
|
| 701 |
// packages/core-data/build-module/queried-data/selectors.mjs |
| 702 |
var queriedItemsCacheByState = /* @__PURE__ */ new WeakMap(); |
| 703 |
function getQueriedItemsUncached(state, query) { |
| 704 |
const { |
| 705 |
stableKey, |
| 706 |
page, |
| 707 |
perPage, |
| 708 |
offset: queryOffset, |
| 709 |
include, |
| 710 |
fields, |
| 711 |
context |
| 712 |
} = get_query_parts_default(query); |
| 713 |
const itemIds = state.queries?.[context]?.[stableKey]?.itemIds; |
| 714 |
if (!itemIds) { |
| 715 |
return null; |
| 716 |
} |
| 717 |
const startOffset = perPage === -1 ? 0 : queryOffset ?? (page - 1) * perPage; |
| 718 |
const endOffset = perPage === -1 ? itemIds.length : Math.min(startOffset + perPage, itemIds.length); |
| 719 |
if (perPage !== -1 && itemIds.length < startOffset + perPage) { |
| 720 |
const totalItems = state.queries[context][stableKey].meta?.totalItems; |
| 721 |
if (Number.isFinite(totalItems) && itemIds.length < totalItems) { |
| 722 |
return null; |
| 723 |
} |
| 724 |
} |
| 725 |
const items2 = []; |
| 726 |
for (let i = startOffset; i < endOffset; i++) { |
| 727 |
const itemId = itemIds[i]; |
| 728 |
if (Array.isArray(include) && !include.includes(itemId)) { |
| 729 |
continue; |
| 730 |
} |
| 731 |
if (itemId === void 0) { |
| 732 |
continue; |
| 733 |
} |
| 734 |
if (!state.items[context]?.hasOwnProperty(itemId)) { |
| 735 |
return null; |
| 736 |
} |
| 737 |
const item = state.items[context][itemId]; |
| 738 |
let filteredItem; |
| 739 |
if (Array.isArray(fields)) { |
| 740 |
filteredItem = {}; |
| 741 |
for (let f = 0; f < fields.length; f++) { |
| 742 |
const field = fields[f].split("."); |
| 743 |
let value = item; |
| 744 |
field.forEach((fieldName) => { |
| 745 |
value = value?.[fieldName]; |
| 746 |
}); |
| 747 |
setNestedValue(filteredItem, field, value); |
| 748 |
} |
| 749 |
} else { |
| 750 |
if (!state.itemIsComplete[context]?.[itemId]) { |
| 751 |
return null; |
| 752 |
} |
| 753 |
filteredItem = item; |
| 754 |
} |
| 755 |
items2.push(filteredItem); |
| 756 |
} |
| 757 |
return items2; |
| 758 |
} |
| 759 |
var getQueriedItems = (0, import_data.createSelector)((state, query = {}) => { |
| 760 |
let queriedItemsCache = queriedItemsCacheByState.get(state); |
| 761 |
if (queriedItemsCache) { |
| 762 |
const queriedItems = queriedItemsCache.get(query); |
| 763 |
if (queriedItems !== void 0) { |
| 764 |
return queriedItems; |
| 765 |
} |
| 766 |
} else { |
| 767 |
queriedItemsCache = new import_equivalent_key_map.default(); |
| 768 |
queriedItemsCacheByState.set(state, queriedItemsCache); |
| 769 |
} |
| 770 |
const items2 = getQueriedItemsUncached(state, query); |
| 771 |
queriedItemsCache.set(query, items2); |
| 772 |
return items2; |
| 773 |
}); |
| 774 |
function getQueriedTotalItems(state, query = {}) { |
| 775 |
const { stableKey, context } = get_query_parts_default(query); |
| 776 |
return state.queries?.[context]?.[stableKey]?.meta?.totalItems ?? null; |
| 777 |
} |
| 778 |
function getQueriedTotalPages(state, query = {}) { |
| 779 |
const { stableKey, context } = get_query_parts_default(query); |
| 780 |
return state.queries?.[context]?.[stableKey]?.meta?.totalPages ?? null; |
| 781 |
} |
| 782 |
|
| 783 |
// packages/core-data/build-module/queried-data/reducer.mjs |
| 784 |
var import_data7 = __toESM(require_data(), 1); |
| 785 |
var import_compose = __toESM(require_compose(), 1); |
| 786 |
|
| 787 |
// node_modules/tslib/tslib.es6.mjs |
| 788 |
var __assign = function() { |
| 789 |
__assign = Object.assign || function __assign2(t) { |
| 790 |
for (var s, i = 1, n = arguments.length; i < n; i++) { |
| 791 |
s = arguments[i]; |
| 792 |
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; |
| 793 |
} |
| 794 |
return t; |
| 795 |
}; |
| 796 |
return __assign.apply(this, arguments); |
| 797 |
}; |
| 798 |
|
| 799 |
// node_modules/lower-case/dist.es2015/index.js |
| 800 |
function lowerCase(str) { |
| 801 |
return str.toLowerCase(); |
| 802 |
} |
| 803 |
|
| 804 |
// node_modules/no-case/dist.es2015/index.js |
| 805 |
var DEFAULT_SPLIT_REGEXP = [/([a-z0-9])([A-Z])/g, /([A-Z])([A-Z][a-z])/g]; |
| 806 |
var DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi; |
| 807 |
function noCase(input, options) { |
| 808 |
if (options === void 0) { |
| 809 |
options = {}; |
| 810 |
} |
| 811 |
var _a = options.splitRegexp, splitRegexp = _a === void 0 ? DEFAULT_SPLIT_REGEXP : _a, _b = options.stripRegexp, stripRegexp = _b === void 0 ? DEFAULT_STRIP_REGEXP : _b, _c = options.transform, transform = _c === void 0 ? lowerCase : _c, _d = options.delimiter, delimiter = _d === void 0 ? " " : _d; |
| 812 |
var result = replace(replace(input, splitRegexp, "$1\0$2"), stripRegexp, "\0"); |
| 813 |
var start = 0; |
| 814 |
var end = result.length; |
| 815 |
while (result.charAt(start) === "\0") |
| 816 |
start++; |
| 817 |
while (result.charAt(end - 1) === "\0") |
| 818 |
end--; |
| 819 |
return result.slice(start, end).split("\0").map(transform).join(delimiter); |
| 820 |
} |
| 821 |
function replace(input, re, value) { |
| 822 |
if (re instanceof RegExp) |
| 823 |
return input.replace(re, value); |
| 824 |
return re.reduce(function(input2, re2) { |
| 825 |
return input2.replace(re2, value); |
| 826 |
}, input); |
| 827 |
} |
| 828 |
|
| 829 |
// node_modules/pascal-case/dist.es2015/index.js |
| 830 |
function pascalCaseTransform(input, index) { |
| 831 |
var firstChar = input.charAt(0); |
| 832 |
var lowerChars = input.substr(1).toLowerCase(); |
| 833 |
if (index > 0 && firstChar >= "0" && firstChar <= "9") { |
| 834 |
return "_" + firstChar + lowerChars; |
| 835 |
} |
| 836 |
return "" + firstChar.toUpperCase() + lowerChars; |
| 837 |
} |
| 838 |
function pascalCase(input, options) { |
| 839 |
if (options === void 0) { |
| 840 |
options = {}; |
| 841 |
} |
| 842 |
return noCase(input, __assign({ delimiter: "", transform: pascalCaseTransform }, options)); |
| 843 |
} |
| 844 |
|
| 845 |
// node_modules/camel-case/dist.es2015/index.js |
| 846 |
function camelCaseTransform(input, index) { |
| 847 |
if (index === 0) |
| 848 |
return input.toLowerCase(); |
| 849 |
return pascalCaseTransform(input, index); |
| 850 |
} |
| 851 |
function camelCase(input, options) { |
| 852 |
if (options === void 0) { |
| 853 |
options = {}; |
| 854 |
} |
| 855 |
return pascalCase(input, __assign({ transform: camelCaseTransform }, options)); |
| 856 |
} |
| 857 |
|
| 858 |
// node_modules/upper-case-first/dist.es2015/index.js |
| 859 |
function upperCaseFirst(input) { |
| 860 |
return input.charAt(0).toUpperCase() + input.substr(1); |
| 861 |
} |
| 862 |
|
| 863 |
// node_modules/capital-case/dist.es2015/index.js |
| 864 |
function capitalCaseTransform(input) { |
| 865 |
return upperCaseFirst(input.toLowerCase()); |
| 866 |
} |
| 867 |
function capitalCase(input, options) { |
| 868 |
if (options === void 0) { |
| 869 |
options = {}; |
| 870 |
} |
| 871 |
return noCase(input, __assign({ delimiter: " ", transform: capitalCaseTransform }, options)); |
| 872 |
} |
| 873 |
|
| 874 |
// packages/core-data/build-module/entities.mjs |
| 875 |
var import_api_fetch = __toESM(require_api_fetch(), 1); |
| 876 |
var import_blocks4 = __toESM(require_blocks(), 1); |
| 877 |
var import_i18n = __toESM(require_i18n(), 1); |
| 878 |
|
| 879 |
// packages/core-data/build-module/awareness/post-editor-awareness.mjs |
| 880 |
var import_data5 = __toESM(require_data(), 1); |
| 881 |
var import_sync8 = __toESM(require_sync(), 1); |
| 882 |
var import_block_editor3 = __toESM(require_block_editor(), 1); |
| 883 |
|
| 884 |
// packages/core-data/build-module/awareness/base-awareness.mjs |
| 885 |
var import_data2 = __toESM(require_data(), 1); |
| 886 |
|
| 887 |
// packages/core-data/build-module/awareness/config.mjs |
| 888 |
var AWARENESS_CURSOR_UPDATE_THROTTLE_IN_MS = 100; |
| 889 |
var LOCAL_CURSOR_UPDATE_DEBOUNCE_IN_MS = 5; |
| 890 |
var REMOVAL_DELAY_IN_MS = 5e3; |
| 891 |
|
| 892 |
// packages/core-data/build-module/awareness/typed-awareness.mjs |
| 893 |
var import_sync = __toESM(require_sync(), 1); |
| 894 |
|
| 895 |
// packages/core-data/build-module/awareness/utils.mjs |
| 896 |
function getBrowserName() { |
| 897 |
const userAgent = window.navigator.userAgent; |
| 898 |
let browserName = "Unknown"; |
| 899 |
if (userAgent.includes("Firefox")) { |
| 900 |
browserName = "Firefox"; |
| 901 |
} else if (userAgent.includes("Edg")) { |
| 902 |
browserName = "Microsoft Edge"; |
| 903 |
} else if (userAgent.includes("Chrome") && !userAgent.includes("Edg")) { |
| 904 |
browserName = "Chrome"; |
| 905 |
} else if (userAgent.includes("Safari") && !userAgent.includes("Chrome")) { |
| 906 |
browserName = "Safari"; |
| 907 |
} else if (userAgent.includes("MSIE") || userAgent.includes("Trident")) { |
| 908 |
browserName = "Internet Explorer"; |
| 909 |
} else if (userAgent.includes("Opera") || userAgent.includes("OPR")) { |
| 910 |
browserName = "Opera"; |
| 911 |
} |
| 912 |
return browserName; |
| 913 |
} |
| 914 |
function areMapsEqual(map1, map2, comparatorFn) { |
| 915 |
if (map1.size !== map2.size) { |
| 916 |
return false; |
| 917 |
} |
| 918 |
for (const [key, value1] of map1.entries()) { |
| 919 |
if (!map2.has(key)) { |
| 920 |
return false; |
| 921 |
} |
| 922 |
if (!comparatorFn(value1, map2.get(key))) { |
| 923 |
return false; |
| 924 |
} |
| 925 |
} |
| 926 |
return true; |
| 927 |
} |
| 928 |
function areCollaboratorInfosEqual(collaboratorInfo1, collaboratorInfo2) { |
| 929 |
if (!collaboratorInfo1 || !collaboratorInfo2) { |
| 930 |
return collaboratorInfo1 === collaboratorInfo2; |
| 931 |
} |
| 932 |
if (Object.keys(collaboratorInfo1).length !== Object.keys(collaboratorInfo2).length) { |
| 933 |
return false; |
| 934 |
} |
| 935 |
return Object.entries(collaboratorInfo1).every(([key, value]) => { |
| 936 |
return value === collaboratorInfo2[key]; |
| 937 |
}); |
| 938 |
} |
| 939 |
function generateCollaboratorInfo(currentCollaborator) { |
| 940 |
const { avatar_urls, id, name, slug } = currentCollaborator; |
| 941 |
return { |
| 942 |
avatar_urls, |
| 943 |
// eslint-disable-line camelcase |
| 944 |
browserType: getBrowserName(), |
| 945 |
enteredAt: Date.now(), |
| 946 |
id, |
| 947 |
name, |
| 948 |
slug |
| 949 |
}; |
| 950 |
} |
| 951 |
function getRecordValue(obj, key) { |
| 952 |
if ("object" === typeof obj && null !== obj && key in obj) { |
| 953 |
return obj[key]; |
| 954 |
} |
| 955 |
return null; |
| 956 |
} |
| 957 |
function getTypedKeys(obj) { |
| 958 |
return Object.keys(obj); |
| 959 |
} |
| 960 |
|
| 961 |
// packages/core-data/build-module/awareness/typed-awareness.mjs |
| 962 |
var TypedAwareness = class extends import_sync.Awareness { |
| 963 |
/** |
| 964 |
* Get the states from an awareness document. |
| 965 |
*/ |
| 966 |
getStates() { |
| 967 |
return super.getStates(); |
| 968 |
} |
| 969 |
/** |
| 970 |
* Get a local state field from an awareness document. |
| 971 |
* @param field |
| 972 |
*/ |
| 973 |
getLocalStateField(field) { |
| 974 |
const state = this.getLocalState(); |
| 975 |
return getRecordValue(state, field); |
| 976 |
} |
| 977 |
/** |
| 978 |
* Set a local state field on an awareness document. |
| 979 |
* @param field |
| 980 |
* @param value |
| 981 |
*/ |
| 982 |
setLocalStateField(field, value) { |
| 983 |
super.setLocalStateField(field, value); |
| 984 |
} |
| 985 |
}; |
| 986 |
|
| 987 |
// packages/core-data/build-module/awareness/awareness-state.mjs |
| 988 |
var AwarenessWithEqualityChecks = class extends TypedAwareness { |
| 989 |
/** OVERRIDDEN METHODS */ |
| 990 |
/** |
| 991 |
* Set a local state field on an awareness document. Calling this method may |
| 992 |
* trigger rerenders of any subscribed components. |
| 993 |
* |
| 994 |
* Equality checks are provided by the abstract `equalityFieldChecks` property. |
| 995 |
* @param field - The field to set. |
| 996 |
* @param value - The value to set. |
| 997 |
*/ |
| 998 |
setLocalStateField(field, value) { |
| 999 |
if (this.isFieldEqual( |
| 1000 |
field, |
| 1001 |
value, |
| 1002 |
this.getLocalStateField(field) ?? void 0 |
| 1003 |
)) { |
| 1004 |
return; |
| 1005 |
} |
| 1006 |
super.setLocalStateField(field, value); |
| 1007 |
} |
| 1008 |
/** CUSTOM METHODS */ |
| 1009 |
/** |
| 1010 |
* Determine if a field value has changed using the provided equality checks. |
| 1011 |
* @param field - The field to check. |
| 1012 |
* @param value1 - The first value to compare. |
| 1013 |
* @param value2 - The second value to compare. |
| 1014 |
*/ |
| 1015 |
isFieldEqual(field, value1, value2) { |
| 1016 |
if (["clientId", "isConnected", "isMe"].includes(field)) { |
| 1017 |
return value1 === value2; |
| 1018 |
} |
| 1019 |
if (field in this.equalityFieldChecks) { |
| 1020 |
const fn = this.equalityFieldChecks[field]; |
| 1021 |
return fn(value1, value2); |
| 1022 |
} |
| 1023 |
throw new Error( |
| 1024 |
`No equality check implemented for awareness state field "${field.toString()}".` |
| 1025 |
); |
| 1026 |
} |
| 1027 |
/** |
| 1028 |
* Determine if two states are equal by comparing each field using the |
| 1029 |
* provided equality checks. |
| 1030 |
* @param state1 - The first state to compare. |
| 1031 |
* @param state2 - The second state to compare. |
| 1032 |
*/ |
| 1033 |
isStateEqual(state1, state2) { |
| 1034 |
return [ |
| 1035 |
.../* @__PURE__ */ new Set([ |
| 1036 |
...getTypedKeys(state1), |
| 1037 |
...getTypedKeys(state2) |
| 1038 |
]) |
| 1039 |
].every((field) => { |
| 1040 |
const value1 = state1[field]; |
| 1041 |
const value2 = state2[field]; |
| 1042 |
return this.isFieldEqual(field, value1, value2); |
| 1043 |
}); |
| 1044 |
} |
| 1045 |
}; |
| 1046 |
var AwarenessState = class extends AwarenessWithEqualityChecks { |
| 1047 |
/** CUSTOM PROPERTIES */ |
| 1048 |
/** |
| 1049 |
* Whether the setUp method has been called, to avoid running it multiple |
| 1050 |
* times. |
| 1051 |
*/ |
| 1052 |
hasSetupRun = false; |
| 1053 |
/** |
| 1054 |
* We keep track of all seen states during the current session for two reasons: |
| 1055 |
* |
| 1056 |
* 1. So that we can represent recently disconnected collaborators in our UI, even |
| 1057 |
* after they have been removed from the awareness document. |
| 1058 |
* 2. So that we can provide debug information about all collaborators seen during |
| 1059 |
* the session. |
| 1060 |
*/ |
| 1061 |
disconnectedCollaborators = /* @__PURE__ */ new Set(); |
| 1062 |
seenStates = /* @__PURE__ */ new Map(); |
| 1063 |
/** |
| 1064 |
* Hold a snapshot of the previous awareness state allows us to compare the |
| 1065 |
* state values and avoid unnecessary updates to subscribers. |
| 1066 |
*/ |
| 1067 |
previousSnapshot = /* @__PURE__ */ new Map(); |
| 1068 |
stateSubscriptions = []; |
| 1069 |
/** |
| 1070 |
* In some cases, we may want to throttle setting local state fields to avoid |
| 1071 |
* overwhelming the awareness document with rapid updates. At the same time, we |
| 1072 |
* want to ensure that when we read our own state locally, we get the latest |
| 1073 |
* value -- even if it hasn't yet been set on the awareness instance. |
| 1074 |
*/ |
| 1075 |
myThrottledState = {}; |
| 1076 |
throttleTimeouts = /* @__PURE__ */ new Map(); |
| 1077 |
/** CUSTOM METHODS */ |
| 1078 |
/** |
| 1079 |
* Set up the awareness state. This method is idempotent and will only run |
| 1080 |
* once. Subclasses should override `onSetUp()` instead of this method to |
| 1081 |
* add their own setup logic. |
| 1082 |
* |
| 1083 |
* This is defined as a readonly arrow function property to prevent |
| 1084 |
* subclasses from overriding it. |
| 1085 |
*/ |
| 1086 |
setUp = () => { |
| 1087 |
if (this.hasSetupRun) { |
| 1088 |
return; |
| 1089 |
} |
| 1090 |
this.hasSetupRun = true; |
| 1091 |
this.onSetUp(); |
| 1092 |
this.on( |
| 1093 |
"change", |
| 1094 |
({ added, removed, updated }) => { |
| 1095 |
[...added, ...updated].forEach((id) => { |
| 1096 |
this.disconnectedCollaborators.delete(id); |
| 1097 |
}); |
| 1098 |
removed.forEach((id) => { |
| 1099 |
this.disconnectedCollaborators.add(id); |
| 1100 |
setTimeout(() => { |
| 1101 |
this.disconnectedCollaborators.delete(id); |
| 1102 |
this.updateSubscribers( |
| 1103 |
true |
| 1104 |
/* force update */ |
| 1105 |
); |
| 1106 |
}, REMOVAL_DELAY_IN_MS); |
| 1107 |
}); |
| 1108 |
this.updateSubscribers(); |
| 1109 |
} |
| 1110 |
); |
| 1111 |
}; |
| 1112 |
/** |
| 1113 |
* Get the most recent state from the last processed change event. |
| 1114 |
* |
| 1115 |
* @return An array of EnhancedState< State >. |
| 1116 |
*/ |
| 1117 |
getCurrentState() { |
| 1118 |
return Array.from(this.previousSnapshot.values()); |
| 1119 |
} |
| 1120 |
/** |
| 1121 |
* Get all seen states in this session to enable debug reporting. |
| 1122 |
*/ |
| 1123 |
getSeenStates() { |
| 1124 |
return this.seenStates; |
| 1125 |
} |
| 1126 |
/** |
| 1127 |
* Allow external code to subscribe to awareness state changes. |
| 1128 |
* @param callback - The callback to subscribe to. |
| 1129 |
*/ |
| 1130 |
onStateChange(callback) { |
| 1131 |
this.stateSubscriptions.push(callback); |
| 1132 |
return () => { |
| 1133 |
this.stateSubscriptions = this.stateSubscriptions.filter( |
| 1134 |
(cb) => cb !== callback |
| 1135 |
); |
| 1136 |
}; |
| 1137 |
} |
| 1138 |
/** |
| 1139 |
* Set a local state field on an awareness document with throttle. See caveats |
| 1140 |
* of this.setLocalStateField. |
| 1141 |
* @param field - The field to set. |
| 1142 |
* @param value - The value to set. |
| 1143 |
* @param wait - The wait time in milliseconds. |
| 1144 |
*/ |
| 1145 |
setThrottledLocalStateField(field, value, wait) { |
| 1146 |
this.setLocalStateField(field, value); |
| 1147 |
this.throttleTimeouts.set( |
| 1148 |
field, |
| 1149 |
setTimeout(() => { |
| 1150 |
this.throttleTimeouts.delete(field); |
| 1151 |
if (this.myThrottledState[field]) { |
| 1152 |
this.setLocalStateField( |
| 1153 |
field, |
| 1154 |
this.myThrottledState[field] |
| 1155 |
); |
| 1156 |
delete this.myThrottledState[field]; |
| 1157 |
} |
| 1158 |
}, wait) |
| 1159 |
); |
| 1160 |
} |
| 1161 |
/** |
| 1162 |
* Set the current collaborator's connection status as awareness state. |
| 1163 |
* @param isConnected - The connection status. |
| 1164 |
*/ |
| 1165 |
setConnectionStatus(isConnected) { |
| 1166 |
if (isConnected) { |
| 1167 |
this.disconnectedCollaborators.delete(this.clientID); |
| 1168 |
} else { |
| 1169 |
this.disconnectedCollaborators.add(this.clientID); |
| 1170 |
} |
| 1171 |
this.updateSubscribers( |
| 1172 |
true |
| 1173 |
/* force update */ |
| 1174 |
); |
| 1175 |
} |
| 1176 |
/** |
| 1177 |
* Update all subscribed listeners with the latest awareness state. |
| 1178 |
* @param forceUpdate - Whether to force an update. |
| 1179 |
*/ |
| 1180 |
updateSubscribers(forceUpdate = false) { |
| 1181 |
if (!this.stateSubscriptions.length) { |
| 1182 |
return; |
| 1183 |
} |
| 1184 |
const states = this.getStates(); |
| 1185 |
this.seenStates = new Map([ |
| 1186 |
...this.seenStates.entries(), |
| 1187 |
...states.entries() |
| 1188 |
]); |
| 1189 |
const updatedStates = new Map( |
| 1190 |
[...this.disconnectedCollaborators, ...states.keys()].filter((clientId) => { |
| 1191 |
return Object.keys(this.seenStates.get(clientId) ?? {}).length > 0; |
| 1192 |
}).map((clientId) => { |
| 1193 |
const rawState = this.seenStates.get(clientId); |
| 1194 |
const isConnected = !this.disconnectedCollaborators.has(clientId); |
| 1195 |
const isMe = clientId === this.clientID; |
| 1196 |
const myState = isMe ? this.myThrottledState : {}; |
| 1197 |
const state = { |
| 1198 |
...rawState, |
| 1199 |
...myState, |
| 1200 |
clientId, |
| 1201 |
isConnected, |
| 1202 |
isMe |
| 1203 |
}; |
| 1204 |
return [clientId, state]; |
| 1205 |
}) |
| 1206 |
); |
| 1207 |
if (!forceUpdate) { |
| 1208 |
if (areMapsEqual( |
| 1209 |
this.previousSnapshot, |
| 1210 |
updatedStates, |
| 1211 |
this.isStateEqual.bind(this) |
| 1212 |
)) { |
| 1213 |
return; |
| 1214 |
} |
| 1215 |
} |
| 1216 |
this.previousSnapshot = updatedStates; |
| 1217 |
this.stateSubscriptions.forEach((callback) => { |
| 1218 |
callback(Array.from(updatedStates.values())); |
| 1219 |
}); |
| 1220 |
} |
| 1221 |
}; |
| 1222 |
|
| 1223 |
// packages/core-data/build-module/name.mjs |
| 1224 |
var STORE_NAME = "core"; |
| 1225 |
|
| 1226 |
// packages/core-data/build-module/awareness/base-awareness.mjs |
| 1227 |
var BaseAwarenessState = class extends AwarenessState { |
| 1228 |
onSetUp() { |
| 1229 |
void this.setCurrentCollaboratorInfo(); |
| 1230 |
} |
| 1231 |
/** |
| 1232 |
* Set the current collaborator info in the local state. |
| 1233 |
*/ |
| 1234 |
async setCurrentCollaboratorInfo() { |
| 1235 |
const currentUser2 = await (0, import_data2.resolveSelect)(STORE_NAME).getCurrentUser(); |
| 1236 |
const collaboratorInfo = generateCollaboratorInfo(currentUser2); |
| 1237 |
this.setLocalStateField("collaboratorInfo", collaboratorInfo); |
| 1238 |
} |
| 1239 |
}; |
| 1240 |
var baseEqualityFieldChecks = { |
| 1241 |
collaboratorInfo: areCollaboratorInfosEqual |
| 1242 |
}; |
| 1243 |
var BaseAwareness = class extends BaseAwarenessState { |
| 1244 |
equalityFieldChecks = baseEqualityFieldChecks; |
| 1245 |
}; |
| 1246 |
|
| 1247 |
// packages/core-data/build-module/awareness/block-lookup.mjs |
| 1248 |
var import_data3 = __toESM(require_data(), 1); |
| 1249 |
var import_sync2 = __toESM(require_sync(), 1); |
| 1250 |
var import_block_editor = __toESM(require_block_editor(), 1); |
| 1251 |
function getBlockPathInYdoc(yType) { |
| 1252 |
const path = []; |
| 1253 |
let current = yType; |
| 1254 |
while (current) { |
| 1255 |
const parentArray = current.parent; |
| 1256 |
if (!parentArray || !(parentArray instanceof import_sync2.Y.Array)) { |
| 1257 |
return null; |
| 1258 |
} |
| 1259 |
let index = -1; |
| 1260 |
for (let i = 0; i < parentArray.length; i++) { |
| 1261 |
if (parentArray.get(i) === current) { |
| 1262 |
index = i; |
| 1263 |
break; |
| 1264 |
} |
| 1265 |
} |
| 1266 |
if (index === -1) { |
| 1267 |
return null; |
| 1268 |
} |
| 1269 |
path.unshift(index); |
| 1270 |
const grandparent = parentArray.parent; |
| 1271 |
if (grandparent instanceof import_sync2.Y.Map && grandparent.get("clientId") !== void 0) { |
| 1272 |
current = grandparent; |
| 1273 |
} else { |
| 1274 |
break; |
| 1275 |
} |
| 1276 |
} |
| 1277 |
return path; |
| 1278 |
} |
| 1279 |
function resolveBlockClientIdByPath(path) { |
| 1280 |
if (path.length === 0) { |
| 1281 |
return null; |
| 1282 |
} |
| 1283 |
const { getBlocks } = (0, import_data3.select)(import_block_editor.store); |
| 1284 |
const postContentBlocks = getPostContentBlocks(getBlocks(), getBlocks); |
| 1285 |
let blocks = postContentBlocks; |
| 1286 |
for (let i = 0; i < path.length; i++) { |
| 1287 |
const block = blocks[path[i]]; |
| 1288 |
if (!block) { |
| 1289 |
return null; |
| 1290 |
} |
| 1291 |
if (i === path.length - 1) { |
| 1292 |
return block.clientId; |
| 1293 |
} |
| 1294 |
blocks = block.innerBlocks; |
| 1295 |
} |
| 1296 |
return null; |
| 1297 |
} |
| 1298 |
function getPostContentBlocks(rootBlocks, getBlocks) { |
| 1299 |
const postContentBlock = findBlockByName(rootBlocks, "core/post-content"); |
| 1300 |
if (postContentBlock) { |
| 1301 |
return getBlocks(postContentBlock.clientId); |
| 1302 |
} |
| 1303 |
return rootBlocks; |
| 1304 |
} |
| 1305 |
function findBlockByName(blocks, name) { |
| 1306 |
for (const block of blocks) { |
| 1307 |
if (block.name === name) { |
| 1308 |
return block; |
| 1309 |
} |
| 1310 |
if (block.innerBlocks?.length) { |
| 1311 |
const found = findBlockByName(block.innerBlocks, name); |
| 1312 |
if (found) { |
| 1313 |
return found; |
| 1314 |
} |
| 1315 |
} |
| 1316 |
} |
| 1317 |
return null; |
| 1318 |
} |
| 1319 |
|
| 1320 |
// packages/core-data/build-module/utils/crdt-utils.mjs |
| 1321 |
var import_sync4 = __toESM(require_sync(), 1); |
| 1322 |
var import_rich_text = __toESM(require_rich_text(), 1); |
| 1323 |
|
| 1324 |
// packages/core-data/build-module/sync.mjs |
| 1325 |
var import_sync3 = __toESM(require_sync(), 1); |
| 1326 |
|
| 1327 |
// packages/core-data/build-module/lock-unlock.mjs |
| 1328 |
var import_private_apis = __toESM(require_private_apis(), 1); |
| 1329 |
var { lock, unlock } = (0, import_private_apis.__dangerousOptInToUnstableAPIsOnlyForCoreModules)( |
| 1330 |
"I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.", |
| 1331 |
"@wordpress/core-data" |
| 1332 |
); |
| 1333 |
|
| 1334 |
// packages/core-data/build-module/sync.mjs |
| 1335 |
var { |
| 1336 |
ConnectionErrorCode, |
| 1337 |
createSyncManager, |
| 1338 |
Delta, |
| 1339 |
CRDT_DOC_META_PERSISTENCE_KEY, |
| 1340 |
CRDT_RECORD_MAP_KEY, |
| 1341 |
LOCAL_EDITOR_ORIGIN, |
| 1342 |
LOCAL_UNDO_IGNORED_ORIGIN, |
| 1343 |
retrySyncConnection |
| 1344 |
} = unlock(import_sync3.privateApis); |
| 1345 |
var syncManager; |
| 1346 |
function getSyncManager() { |
| 1347 |
if (syncManager) { |
| 1348 |
return syncManager; |
| 1349 |
} |
| 1350 |
syncManager = createSyncManager(); |
| 1351 |
return syncManager; |
| 1352 |
} |
| 1353 |
|
| 1354 |
// packages/core-data/build-module/utils/crdt-utils.mjs |
| 1355 |
function getRootMap(doc, key) { |
| 1356 |
return doc.getMap(key); |
| 1357 |
} |
| 1358 |
function createYMap(partial = {}) { |
| 1359 |
return new import_sync4.Y.Map(Object.entries(partial)); |
| 1360 |
} |
| 1361 |
function isYMap(value) { |
| 1362 |
return value instanceof import_sync4.Y.Map; |
| 1363 |
} |
| 1364 |
function findBlockByClientIdInDoc(blockId, ydoc) { |
| 1365 |
const ymap = getRootMap(ydoc, CRDT_RECORD_MAP_KEY); |
| 1366 |
const blocks = ymap.get("blocks"); |
| 1367 |
if (!(blocks instanceof import_sync4.Y.Array)) { |
| 1368 |
return null; |
| 1369 |
} |
| 1370 |
return findBlockByClientIdInBlocks(blockId, blocks); |
| 1371 |
} |
| 1372 |
var MARKER_START = 57344; |
| 1373 |
function pickMarker(text) { |
| 1374 |
const tryCount = 16; |
| 1375 |
for (let code = MARKER_START; code < MARKER_START + tryCount; code++) { |
| 1376 |
const candidate = String.fromCharCode(code); |
| 1377 |
if (!text.includes(candidate)) { |
| 1378 |
return candidate; |
| 1379 |
} |
| 1380 |
} |
| 1381 |
return null; |
| 1382 |
} |
| 1383 |
function htmlIndexToRichTextOffset(html, htmlIndex) { |
| 1384 |
if (!html.includes("<") && !html.includes("&")) { |
| 1385 |
return htmlIndex; |
| 1386 |
} |
| 1387 |
const marker = pickMarker(html); |
| 1388 |
if (!marker) { |
| 1389 |
return htmlIndex; |
| 1390 |
} |
| 1391 |
const withMarker = html.slice(0, htmlIndex) + marker + html.slice(htmlIndex); |
| 1392 |
const value = (0, import_rich_text.create)({ html: withMarker }); |
| 1393 |
const markerPos = value.text.indexOf(marker); |
| 1394 |
return markerPos === -1 ? htmlIndex : markerPos; |
| 1395 |
} |
| 1396 |
function richTextOffsetToHtmlIndex(html, richTextOffset) { |
| 1397 |
if (!html.includes("<") && !html.includes("&")) { |
| 1398 |
return richTextOffset; |
| 1399 |
} |
| 1400 |
const marker = pickMarker(html); |
| 1401 |
if (!marker) { |
| 1402 |
return richTextOffset; |
| 1403 |
} |
| 1404 |
const value = (0, import_rich_text.create)({ html }); |
| 1405 |
const markerValue = (0, import_rich_text.create)({ text: marker }); |
| 1406 |
if (value.formats[richTextOffset]) { |
| 1407 |
markerValue.formats[0] = value.formats[richTextOffset]; |
| 1408 |
} |
| 1409 |
const withMarker = (0, import_rich_text.insert)( |
| 1410 |
value, |
| 1411 |
markerValue, |
| 1412 |
richTextOffset, |
| 1413 |
richTextOffset |
| 1414 |
); |
| 1415 |
const htmlWithMarker = (0, import_rich_text.toHTMLString)({ value: withMarker }); |
| 1416 |
const markerIndex = htmlWithMarker.indexOf(marker); |
| 1417 |
return markerIndex === -1 ? richTextOffset : markerIndex; |
| 1418 |
} |
| 1419 |
function findBlockByClientIdInBlocks(blockId, blocks) { |
| 1420 |
for (const block of blocks) { |
| 1421 |
if (block.get("clientId") === blockId) { |
| 1422 |
return block; |
| 1423 |
} |
| 1424 |
const innerBlocks = block.get("innerBlocks"); |
| 1425 |
if (innerBlocks && innerBlocks.length > 0) { |
| 1426 |
const innerBlock = findBlockByClientIdInBlocks( |
| 1427 |
blockId, |
| 1428 |
innerBlocks |
| 1429 |
); |
| 1430 |
if (innerBlock) { |
| 1431 |
return innerBlock; |
| 1432 |
} |
| 1433 |
} |
| 1434 |
} |
| 1435 |
return null; |
| 1436 |
} |
| 1437 |
|
| 1438 |
// packages/core-data/build-module/utils/crdt-user-selections.mjs |
| 1439 |
var import_data4 = __toESM(require_data(), 1); |
| 1440 |
var import_sync6 = __toESM(require_sync(), 1); |
| 1441 |
var import_block_editor2 = __toESM(require_block_editor(), 1); |
| 1442 |
var SelectionType = /* @__PURE__ */ ((SelectionType2) => { |
| 1443 |
SelectionType2["None"] = "none"; |
| 1444 |
SelectionType2["Cursor"] = "cursor"; |
| 1445 |
SelectionType2["SelectionInOneBlock"] = "selection-in-one-block"; |
| 1446 |
SelectionType2["SelectionInMultipleBlocks"] = "selection-in-multiple-blocks"; |
| 1447 |
SelectionType2["WholeBlock"] = "whole-block"; |
| 1448 |
return SelectionType2; |
| 1449 |
})(SelectionType || {}); |
| 1450 |
function getSelectionState(selectionStart, selectionEnd, yDoc, options) { |
| 1451 |
const { selectionDirection } = options ?? {}; |
| 1452 |
const ymap = getRootMap(yDoc, CRDT_RECORD_MAP_KEY); |
| 1453 |
const yBlocks = ymap.get("blocks"); |
| 1454 |
const isSelectionEmpty = Object.keys(selectionStart).length === 0; |
| 1455 |
const noSelection = { |
| 1456 |
type: "none" |
| 1457 |
/* None */ |
| 1458 |
}; |
| 1459 |
if (isSelectionEmpty || !yBlocks) { |
| 1460 |
return noSelection; |
| 1461 |
} |
| 1462 |
const isSelectionInOneBlock = selectionStart.clientId === selectionEnd.clientId; |
| 1463 |
const isCursorOnly = isSelectionInOneBlock && selectionStart.offset === selectionEnd.offset; |
| 1464 |
const isSelectionAWholeBlock = isSelectionInOneBlock && selectionStart.offset === void 0 && selectionEnd.offset === void 0; |
| 1465 |
if (isSelectionAWholeBlock) { |
| 1466 |
const path = getBlockPathForLocalClientId(selectionStart.clientId); |
| 1467 |
const blockPosition = path ? createRelativePositionForBlockPath(path, yBlocks) : null; |
| 1468 |
if (!blockPosition) { |
| 1469 |
return noSelection; |
| 1470 |
} |
| 1471 |
return { |
| 1472 |
type: "whole-block", |
| 1473 |
blockPosition |
| 1474 |
}; |
| 1475 |
} else if (isCursorOnly) { |
| 1476 |
const cursorPosition = getCursorPosition(selectionStart, yBlocks); |
| 1477 |
if (!cursorPosition) { |
| 1478 |
return noSelection; |
| 1479 |
} |
| 1480 |
return { |
| 1481 |
type: "cursor", |
| 1482 |
cursorPosition |
| 1483 |
}; |
| 1484 |
} else if (isSelectionInOneBlock) { |
| 1485 |
const cursorStartPosition2 = getCursorPosition( |
| 1486 |
selectionStart, |
| 1487 |
yBlocks |
| 1488 |
); |
| 1489 |
const cursorEndPosition2 = getCursorPosition(selectionEnd, yBlocks); |
| 1490 |
if (!cursorStartPosition2 || !cursorEndPosition2) { |
| 1491 |
return noSelection; |
| 1492 |
} |
| 1493 |
return { |
| 1494 |
type: "selection-in-one-block", |
| 1495 |
cursorStartPosition: cursorStartPosition2, |
| 1496 |
cursorEndPosition: cursorEndPosition2, |
| 1497 |
selectionDirection |
| 1498 |
}; |
| 1499 |
} |
| 1500 |
const cursorStartPosition = getCursorPosition(selectionStart, yBlocks); |
| 1501 |
const cursorEndPosition = getCursorPosition(selectionEnd, yBlocks); |
| 1502 |
if (!cursorStartPosition || !cursorEndPosition) { |
| 1503 |
return noSelection; |
| 1504 |
} |
| 1505 |
return { |
| 1506 |
type: "selection-in-multiple-blocks", |
| 1507 |
cursorStartPosition, |
| 1508 |
cursorEndPosition, |
| 1509 |
selectionDirection |
| 1510 |
}; |
| 1511 |
} |
| 1512 |
function getCursorPosition(selection, blocks) { |
| 1513 |
const path = getBlockPathForLocalClientId(selection.clientId); |
| 1514 |
const block = path ? findBlockByPath(path, blocks) : null; |
| 1515 |
if (!block || !selection.attributeKey || void 0 === selection.offset) { |
| 1516 |
return null; |
| 1517 |
} |
| 1518 |
const attributes = block.get("attributes"); |
| 1519 |
const currentYText = attributes?.get(selection.attributeKey); |
| 1520 |
if (!(currentYText instanceof import_sync6.Y.Text)) { |
| 1521 |
return null; |
| 1522 |
} |
| 1523 |
const relativePosition = import_sync6.Y.createRelativePositionFromTypeIndex( |
| 1524 |
currentYText, |
| 1525 |
richTextOffsetToHtmlIndex(currentYText.toString(), selection.offset) |
| 1526 |
); |
| 1527 |
return { |
| 1528 |
relativePosition, |
| 1529 |
absoluteOffset: selection.offset |
| 1530 |
}; |
| 1531 |
} |
| 1532 |
function getBlockPathForLocalClientId(clientId) { |
| 1533 |
const { getBlockIndex, getBlockRootClientId, getBlockName } = (0, import_data4.select)(import_block_editor2.store); |
| 1534 |
const path = []; |
| 1535 |
let current = clientId; |
| 1536 |
while (current) { |
| 1537 |
const index = getBlockIndex(current); |
| 1538 |
if (index === -1) { |
| 1539 |
return null; |
| 1540 |
} |
| 1541 |
path.unshift(index); |
| 1542 |
const parent = getBlockRootClientId(current); |
| 1543 |
if (!parent) { |
| 1544 |
break; |
| 1545 |
} |
| 1546 |
const parentName = getBlockName(parent); |
| 1547 |
if (parentName === "core/post-content") { |
| 1548 |
break; |
| 1549 |
} |
| 1550 |
current = parent; |
| 1551 |
} |
| 1552 |
return path.length > 0 ? path : null; |
| 1553 |
} |
| 1554 |
function findBlockByPath(path, blocks) { |
| 1555 |
let currentBlocks = blocks; |
| 1556 |
for (let i = 0; i < path.length; i++) { |
| 1557 |
if (path[i] >= currentBlocks.length) { |
| 1558 |
return null; |
| 1559 |
} |
| 1560 |
const block = currentBlocks.get(path[i]); |
| 1561 |
if (!block) { |
| 1562 |
return null; |
| 1563 |
} |
| 1564 |
if (i === path.length - 1) { |
| 1565 |
return block; |
| 1566 |
} |
| 1567 |
currentBlocks = block.get("innerBlocks") ?? new import_sync6.Y.Array(); |
| 1568 |
} |
| 1569 |
return null; |
| 1570 |
} |
| 1571 |
function createRelativePositionForBlockPath(path, blocks) { |
| 1572 |
let currentBlocks = blocks; |
| 1573 |
for (let i = 0; i < path.length; i++) { |
| 1574 |
if (path[i] >= currentBlocks.length) { |
| 1575 |
return null; |
| 1576 |
} |
| 1577 |
if (i === path.length - 1) { |
| 1578 |
return import_sync6.Y.createRelativePositionFromTypeIndex( |
| 1579 |
currentBlocks, |
| 1580 |
path[i] |
| 1581 |
); |
| 1582 |
} |
| 1583 |
const block = currentBlocks.get(path[i]); |
| 1584 |
currentBlocks = block?.get("innerBlocks") ?? new import_sync6.Y.Array(); |
| 1585 |
} |
| 1586 |
return null; |
| 1587 |
} |
| 1588 |
function areSelectionsStatesEqual(selection1, selection2) { |
| 1589 |
if (selection1.type !== selection2.type) { |
| 1590 |
return false; |
| 1591 |
} |
| 1592 |
switch (selection1.type) { |
| 1593 |
case "none": |
| 1594 |
return true; |
| 1595 |
case "cursor": |
| 1596 |
return areCursorPositionsEqual( |
| 1597 |
selection1.cursorPosition, |
| 1598 |
selection2.cursorPosition |
| 1599 |
); |
| 1600 |
case "selection-in-one-block": |
| 1601 |
return areCursorPositionsEqual( |
| 1602 |
selection1.cursorStartPosition, |
| 1603 |
selection2.cursorStartPosition |
| 1604 |
) && areCursorPositionsEqual( |
| 1605 |
selection1.cursorEndPosition, |
| 1606 |
selection2.cursorEndPosition |
| 1607 |
) && selection1.selectionDirection === selection2.selectionDirection; |
| 1608 |
case "selection-in-multiple-blocks": |
| 1609 |
return areCursorPositionsEqual( |
| 1610 |
selection1.cursorStartPosition, |
| 1611 |
selection2.cursorStartPosition |
| 1612 |
) && areCursorPositionsEqual( |
| 1613 |
selection1.cursorEndPosition, |
| 1614 |
selection2.cursorEndPosition |
| 1615 |
) && selection1.selectionDirection === selection2.selectionDirection; |
| 1616 |
case "whole-block": |
| 1617 |
return import_sync6.Y.compareRelativePositions( |
| 1618 |
selection1.blockPosition, |
| 1619 |
selection2.blockPosition |
| 1620 |
); |
| 1621 |
default: |
| 1622 |
return false; |
| 1623 |
} |
| 1624 |
} |
| 1625 |
function areCursorPositionsEqual(cursorPosition1, cursorPosition2) { |
| 1626 |
const isRelativePositionEqual = import_sync6.Y.compareRelativePositions( |
| 1627 |
cursorPosition1.relativePosition, |
| 1628 |
cursorPosition2.relativePosition |
| 1629 |
); |
| 1630 |
const isAbsoluteOffsetEqual = cursorPosition1.absoluteOffset === cursorPosition2.absoluteOffset; |
| 1631 |
return isRelativePositionEqual && isAbsoluteOffsetEqual; |
| 1632 |
} |
| 1633 |
|
| 1634 |
// packages/core-data/build-module/types.mjs |
| 1635 |
var SelectionDirection = /* @__PURE__ */ ((SelectionDirection2) => { |
| 1636 |
SelectionDirection2["Forward"] = "f"; |
| 1637 |
SelectionDirection2["Backward"] = "b"; |
| 1638 |
return SelectionDirection2; |
| 1639 |
})(SelectionDirection || {}); |
| 1640 |
|
| 1641 |
// packages/core-data/build-module/awareness/post-editor-awareness.mjs |
| 1642 |
var PostEditorAwareness = class extends BaseAwarenessState { |
| 1643 |
constructor(doc, kind, name, postId) { |
| 1644 |
super(doc); |
| 1645 |
this.kind = kind; |
| 1646 |
this.name = name; |
| 1647 |
this.postId = postId; |
| 1648 |
} |
| 1649 |
equalityFieldChecks = { |
| 1650 |
...baseEqualityFieldChecks, |
| 1651 |
editorState: this.areEditorStatesEqual |
| 1652 |
}; |
| 1653 |
onSetUp() { |
| 1654 |
super.onSetUp(); |
| 1655 |
this.subscribeToCollaboratorSelectionChanges(); |
| 1656 |
} |
| 1657 |
/** |
| 1658 |
* Subscribe to collaborator selection changes and update the selection state. |
| 1659 |
*/ |
| 1660 |
subscribeToCollaboratorSelectionChanges() { |
| 1661 |
const { |
| 1662 |
getSelectionStart, |
| 1663 |
getSelectionEnd, |
| 1664 |
getSelectedBlocksInitialCaretPosition |
| 1665 |
} = (0, import_data5.select)(import_block_editor3.store); |
| 1666 |
let selectionStart = getSelectionStart(); |
| 1667 |
let selectionEnd = getSelectionEnd(); |
| 1668 |
let localCursorTimeout = null; |
| 1669 |
let selectionBeforeDebounce = null; |
| 1670 |
(0, import_data5.subscribe)(() => { |
| 1671 |
const newSelectionStart = getSelectionStart(); |
| 1672 |
const newSelectionEnd = getSelectionEnd(); |
| 1673 |
if (newSelectionStart === selectionStart && newSelectionEnd === selectionEnd) { |
| 1674 |
return; |
| 1675 |
} |
| 1676 |
if (!selectionBeforeDebounce) { |
| 1677 |
selectionBeforeDebounce = { |
| 1678 |
start: selectionStart, |
| 1679 |
end: selectionEnd |
| 1680 |
}; |
| 1681 |
} |
| 1682 |
selectionStart = newSelectionStart; |
| 1683 |
selectionEnd = newSelectionEnd; |
| 1684 |
const initialPosition = getSelectedBlocksInitialCaretPosition(); |
| 1685 |
void this.updateSelectionInEntityRecord( |
| 1686 |
selectionStart, |
| 1687 |
selectionEnd, |
| 1688 |
initialPosition |
| 1689 |
); |
| 1690 |
if (localCursorTimeout) { |
| 1691 |
clearTimeout(localCursorTimeout); |
| 1692 |
} |
| 1693 |
localCursorTimeout = setTimeout(() => { |
| 1694 |
const selectionStateOptions = {}; |
| 1695 |
if (selectionBeforeDebounce) { |
| 1696 |
selectionStateOptions.selectionDirection = detectSelectionDirection( |
| 1697 |
selectionBeforeDebounce.start, |
| 1698 |
selectionBeforeDebounce.end, |
| 1699 |
selectionStart, |
| 1700 |
selectionEnd |
| 1701 |
); |
| 1702 |
selectionBeforeDebounce = null; |
| 1703 |
} |
| 1704 |
const selectionState = getSelectionState( |
| 1705 |
selectionStart, |
| 1706 |
selectionEnd, |
| 1707 |
this.doc, |
| 1708 |
selectionStateOptions |
| 1709 |
); |
| 1710 |
this.setThrottledLocalStateField( |
| 1711 |
"editorState", |
| 1712 |
{ selection: selectionState }, |
| 1713 |
AWARENESS_CURSOR_UPDATE_THROTTLE_IN_MS |
| 1714 |
); |
| 1715 |
}, LOCAL_CURSOR_UPDATE_DEBOUNCE_IN_MS); |
| 1716 |
}); |
| 1717 |
} |
| 1718 |
/** |
| 1719 |
* Update the entity record with the current collaborator's selection. |
| 1720 |
* |
| 1721 |
* @param selectionStart - The start position of the selection. |
| 1722 |
* @param selectionEnd - The end position of the selection. |
| 1723 |
* @param initialPosition - The initial position of the selection. |
| 1724 |
*/ |
| 1725 |
async updateSelectionInEntityRecord(selectionStart, selectionEnd, initialPosition) { |
| 1726 |
const edits = { |
| 1727 |
selection: { selectionStart, selectionEnd, initialPosition } |
| 1728 |
}; |
| 1729 |
const options = { |
| 1730 |
undoIgnore: true |
| 1731 |
}; |
| 1732 |
(0, import_data5.dispatch)(STORE_NAME).editEntityRecord( |
| 1733 |
this.kind, |
| 1734 |
this.name, |
| 1735 |
this.postId, |
| 1736 |
edits, |
| 1737 |
options |
| 1738 |
); |
| 1739 |
} |
| 1740 |
/** |
| 1741 |
* Check if two editor states are equal. |
| 1742 |
* |
| 1743 |
* @param state1 - The first editor state. |
| 1744 |
* @param state2 - The second editor state. |
| 1745 |
* @return True if the editor states are equal, false otherwise. |
| 1746 |
*/ |
| 1747 |
areEditorStatesEqual(state1, state2) { |
| 1748 |
if (!state1 || !state2) { |
| 1749 |
return state1 === state2; |
| 1750 |
} |
| 1751 |
if (!state1.selection || !state2.selection) { |
| 1752 |
return state1.selection === state2.selection; |
| 1753 |
} |
| 1754 |
return areSelectionsStatesEqual(state1.selection, state2.selection); |
| 1755 |
} |
| 1756 |
/** |
| 1757 |
* Resolve a selection state to a text index and block client ID. |
| 1758 |
* |
| 1759 |
* For text-based selections, navigates up from the resolved Y.Text via |
| 1760 |
* AbstractType.parent to find the containing block, then resolves the |
| 1761 |
* local clientId via the block's tree path. |
| 1762 |
* For WholeBlock selections, resolves the block's relative position and |
| 1763 |
* then finds the local clientId via tree path. |
| 1764 |
* |
| 1765 |
* Tree-path resolution is used instead of reading the clientId directly |
| 1766 |
* from the Yjs block because the local block-editor store may use different |
| 1767 |
* clientIds (e.g. in "Show Template" mode where blocks are cloned). |
| 1768 |
* |
| 1769 |
* @param selection - The selection state. |
| 1770 |
* @return The rich-text offset and block client ID, or nulls if not resolvable. |
| 1771 |
*/ |
| 1772 |
convertSelectionStateToAbsolute(selection) { |
| 1773 |
if (selection.type === SelectionType.None) { |
| 1774 |
return { richTextOffset: null, localClientId: null }; |
| 1775 |
} |
| 1776 |
if (selection.type === SelectionType.WholeBlock) { |
| 1777 |
const absolutePos = import_sync8.Y.createAbsolutePositionFromRelativePosition( |
| 1778 |
selection.blockPosition, |
| 1779 |
this.doc |
| 1780 |
); |
| 1781 |
let localClientId2 = null; |
| 1782 |
if (absolutePos && absolutePos.type instanceof import_sync8.Y.Array) { |
| 1783 |
const parentArray = absolutePos.type; |
| 1784 |
const block = parentArray.get(absolutePos.index); |
| 1785 |
if (block instanceof import_sync8.Y.Map) { |
| 1786 |
const path2 = getBlockPathInYdoc(block); |
| 1787 |
localClientId2 = path2 ? resolveBlockClientIdByPath(path2) : null; |
| 1788 |
} |
| 1789 |
} |
| 1790 |
return { richTextOffset: null, localClientId: localClientId2 }; |
| 1791 |
} |
| 1792 |
const cursorPos = "cursorPosition" in selection ? selection.cursorPosition : selection.cursorStartPosition; |
| 1793 |
const absolutePosition = import_sync8.Y.createAbsolutePositionFromRelativePosition( |
| 1794 |
cursorPos.relativePosition, |
| 1795 |
this.doc |
| 1796 |
); |
| 1797 |
if (!absolutePosition) { |
| 1798 |
return { richTextOffset: null, localClientId: null }; |
| 1799 |
} |
| 1800 |
const yType = absolutePosition.type.parent?.parent; |
| 1801 |
const path = yType instanceof import_sync8.Y.Map ? getBlockPathInYdoc(yType) : null; |
| 1802 |
const localClientId = path ? resolveBlockClientIdByPath(path) : null; |
| 1803 |
return { |
| 1804 |
richTextOffset: htmlIndexToRichTextOffset( |
| 1805 |
absolutePosition.type.toString(), |
| 1806 |
absolutePosition.index |
| 1807 |
), |
| 1808 |
localClientId |
| 1809 |
}; |
| 1810 |
} |
| 1811 |
/** |
| 1812 |
* Type guard to check if a struct is a Y.Item (not Y.GC) |
| 1813 |
* @param struct - The struct to check. |
| 1814 |
* @return True if the struct is a Y.Item, false otherwise. |
| 1815 |
*/ |
| 1816 |
isYItem(struct) { |
| 1817 |
return "content" in struct; |
| 1818 |
} |
| 1819 |
/** |
| 1820 |
* Get data for debugging, using the awareness state. |
| 1821 |
* |
| 1822 |
* @return {YDocDebugData} The debug data. |
| 1823 |
*/ |
| 1824 |
getDebugData() { |
| 1825 |
const ydoc = this.doc; |
| 1826 |
const docData = Object.fromEntries( |
| 1827 |
Array.from(ydoc.share, ([key, value]) => [ |
| 1828 |
key, |
| 1829 |
value.toJSON() |
| 1830 |
]) |
| 1831 |
); |
| 1832 |
const collaboratorMapData = new Map( |
| 1833 |
Array.from(this.getSeenStates().entries()).map( |
| 1834 |
([clientId, collaboratorState]) => [ |
| 1835 |
String(clientId), |
| 1836 |
{ |
| 1837 |
name: collaboratorState.collaboratorInfo.name, |
| 1838 |
wpUserId: collaboratorState.collaboratorInfo.id |
| 1839 |
} |
| 1840 |
] |
| 1841 |
) |
| 1842 |
); |
| 1843 |
const serializableClientItems = {}; |
| 1844 |
ydoc.store.clients.forEach((structs, clientId) => { |
| 1845 |
const items2 = structs.filter(this.isYItem); |
| 1846 |
serializableClientItems[clientId] = items2.map((item) => { |
| 1847 |
const { left, right, ...rest } = item; |
| 1848 |
return { |
| 1849 |
...rest, |
| 1850 |
left: left ? { |
| 1851 |
id: left.id, |
| 1852 |
length: left.length, |
| 1853 |
origin: left.origin, |
| 1854 |
content: left.content |
| 1855 |
} : null, |
| 1856 |
right: right ? { |
| 1857 |
id: right.id, |
| 1858 |
length: right.length, |
| 1859 |
origin: right.origin, |
| 1860 |
content: right.content |
| 1861 |
} : null |
| 1862 |
}; |
| 1863 |
}); |
| 1864 |
}); |
| 1865 |
return { |
| 1866 |
doc: docData, |
| 1867 |
clients: serializableClientItems, |
| 1868 |
collaboratorMap: Object.fromEntries(collaboratorMapData) |
| 1869 |
}; |
| 1870 |
} |
| 1871 |
}; |
| 1872 |
function detectSelectionDirection(prevStart, prevEnd, newStart, newEnd) { |
| 1873 |
const startMoved = !areBlockSelectionsEqual(prevStart, newStart); |
| 1874 |
const endMoved = !areBlockSelectionsEqual(prevEnd, newEnd); |
| 1875 |
if (startMoved && !endMoved) { |
| 1876 |
return SelectionDirection.Backward; |
| 1877 |
} |
| 1878 |
return SelectionDirection.Forward; |
| 1879 |
} |
| 1880 |
function areBlockSelectionsEqual(a, b) { |
| 1881 |
return a.clientId === b.clientId && a.attributeKey === b.attributeKey && a.offset === b.offset; |
| 1882 |
} |
| 1883 |
|
| 1884 |
// packages/core-data/build-module/utils/crdt.mjs |
| 1885 |
var import_es63 = __toESM(require_es6(), 1); |
| 1886 |
var import_blocks3 = __toESM(require_blocks(), 1); |
| 1887 |
var import_sync13 = __toESM(require_sync(), 1); |
| 1888 |
|
| 1889 |
// node_modules/uuid/dist/esm-browser/rng.js |
| 1890 |
var getRandomValues; |
| 1891 |
var rnds8 = new Uint8Array(16); |
| 1892 |
function rng() { |
| 1893 |
if (!getRandomValues) { |
| 1894 |
getRandomValues = typeof crypto !== "undefined" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto); |
| 1895 |
if (!getRandomValues) { |
| 1896 |
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported"); |
| 1897 |
} |
| 1898 |
} |
| 1899 |
return getRandomValues(rnds8); |
| 1900 |
} |
| 1901 |
|
| 1902 |
// node_modules/uuid/dist/esm-browser/stringify.js |
| 1903 |
var byteToHex = []; |
| 1904 |
for (let i = 0; i < 256; ++i) { |
| 1905 |
byteToHex.push((i + 256).toString(16).slice(1)); |
| 1906 |
} |
| 1907 |
function unsafeStringify(arr, offset = 0) { |
| 1908 |
return byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]; |
| 1909 |
} |
| 1910 |
|
| 1911 |
// node_modules/uuid/dist/esm-browser/native.js |
| 1912 |
var randomUUID = typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID.bind(crypto); |
| 1913 |
var native_default = { |
| 1914 |
randomUUID |
| 1915 |
}; |
| 1916 |
|
| 1917 |
// node_modules/uuid/dist/esm-browser/v4.js |
| 1918 |
function v4(options, buf, offset) { |
| 1919 |
if (native_default.randomUUID && !buf && !options) { |
| 1920 |
return native_default.randomUUID(); |
| 1921 |
} |
| 1922 |
options = options || {}; |
| 1923 |
const rnds = options.random || (options.rng || rng)(); |
| 1924 |
rnds[6] = rnds[6] & 15 | 64; |
| 1925 |
rnds[8] = rnds[8] & 63 | 128; |
| 1926 |
if (buf) { |
| 1927 |
offset = offset || 0; |
| 1928 |
for (let i = 0; i < 16; ++i) { |
| 1929 |
buf[offset + i] = rnds[i]; |
| 1930 |
} |
| 1931 |
return buf; |
| 1932 |
} |
| 1933 |
return unsafeStringify(rnds); |
| 1934 |
} |
| 1935 |
var v4_default = v4; |
| 1936 |
|
| 1937 |
// packages/core-data/build-module/utils/crdt-blocks.mjs |
| 1938 |
var import_es62 = __toESM(require_es6(), 1); |
| 1939 |
var import_blocks = __toESM(require_blocks(), 1); |
| 1940 |
var import_rich_text3 = __toESM(require_rich_text(), 1); |
| 1941 |
var import_sync9 = __toESM(require_sync(), 1); |
| 1942 |
|
| 1943 |
// packages/core-data/build-module/utils/crdt-text.mjs |
| 1944 |
var import_rich_text2 = __toESM(require_rich_text(), 1); |
| 1945 |
var RICH_TEXT_CACHE_MAX_SIZE = 500; |
| 1946 |
function createRichTextDataCache(maxSize) { |
| 1947 |
const cache3 = /* @__PURE__ */ new Map(); |
| 1948 |
return function(value) { |
| 1949 |
const cached = cache3.get(value); |
| 1950 |
if (cached) { |
| 1951 |
return cached; |
| 1952 |
} |
| 1953 |
const result = import_rich_text2.RichTextData.fromHTMLString(value); |
| 1954 |
if (cache3.size >= maxSize) { |
| 1955 |
cache3.delete(cache3.keys().next().value); |
| 1956 |
} |
| 1957 |
cache3.set(value, result); |
| 1958 |
return result; |
| 1959 |
}; |
| 1960 |
} |
| 1961 |
var getCachedRichTextData = createRichTextDataCache( |
| 1962 |
RICH_TEXT_CACHE_MAX_SIZE |
| 1963 |
); |
| 1964 |
|
| 1965 |
// packages/core-data/build-module/utils/crdt-blocks.mjs |
| 1966 |
var serializableBlocksCache = /* @__PURE__ */ new WeakMap(); |
| 1967 |
function serializeAttributeValue(value) { |
| 1968 |
if (value instanceof import_rich_text3.RichTextData) { |
| 1969 |
return value.valueOf(); |
| 1970 |
} |
| 1971 |
if (Array.isArray(value)) { |
| 1972 |
return value.map(serializeAttributeValue); |
| 1973 |
} |
| 1974 |
if (value && typeof value === "object") { |
| 1975 |
const result = {}; |
| 1976 |
for (const [k, v] of Object.entries(value)) { |
| 1977 |
result[k] = serializeAttributeValue(v); |
| 1978 |
} |
| 1979 |
return result; |
| 1980 |
} |
| 1981 |
return value; |
| 1982 |
} |
| 1983 |
function makeBlockAttributesSerializable(blockName, attributes) { |
| 1984 |
const newAttributes = { ...attributes }; |
| 1985 |
for (const [key, value] of Object.entries(attributes)) { |
| 1986 |
if (isLocalAttribute(blockName, key)) { |
| 1987 |
delete newAttributes[key]; |
| 1988 |
continue; |
| 1989 |
} |
| 1990 |
newAttributes[key] = serializeAttributeValue(value); |
| 1991 |
} |
| 1992 |
return newAttributes; |
| 1993 |
} |
| 1994 |
function makeBlocksSerializable(blocks) { |
| 1995 |
return blocks.map((block) => { |
| 1996 |
const { name, innerBlocks, attributes, ...rest } = block; |
| 1997 |
delete rest.validationIssues; |
| 1998 |
return { |
| 1999 |
...rest, |
| 2000 |
name, |
| 2001 |
attributes: makeBlockAttributesSerializable(name, attributes), |
| 2002 |
innerBlocks: makeBlocksSerializable(innerBlocks) |
| 2003 |
}; |
| 2004 |
}); |
| 2005 |
} |
| 2006 |
function deserializeAttributeValue(schema, value) { |
| 2007 |
if (schema?.type === "rich-text" && typeof value === "string") { |
| 2008 |
return getCachedRichTextData(value); |
| 2009 |
} |
| 2010 |
if (Array.isArray(value)) { |
| 2011 |
return value.map( |
| 2012 |
(item) => deserializeAttributeValue(schema, item) |
| 2013 |
); |
| 2014 |
} |
| 2015 |
if (value && typeof value === "object") { |
| 2016 |
const result = {}; |
| 2017 |
for (const [key, innerValue] of Object.entries( |
| 2018 |
value |
| 2019 |
)) { |
| 2020 |
result[key] = deserializeAttributeValue( |
| 2021 |
schema?.query?.[key], |
| 2022 |
innerValue |
| 2023 |
); |
| 2024 |
} |
| 2025 |
return result; |
| 2026 |
} |
| 2027 |
return value; |
| 2028 |
} |
| 2029 |
function deserializeBlockAttributes(blocks) { |
| 2030 |
return blocks.map((block) => { |
| 2031 |
const { name, innerBlocks, attributes, ...rest } = block; |
| 2032 |
const newAttributes = { ...attributes }; |
| 2033 |
for (const [key, value] of Object.entries(attributes)) { |
| 2034 |
const schema = getBlockAttributeType(name, key); |
| 2035 |
if (schema) { |
| 2036 |
newAttributes[key] = deserializeAttributeValue( |
| 2037 |
schema, |
| 2038 |
value |
| 2039 |
); |
| 2040 |
} |
| 2041 |
} |
| 2042 |
return { |
| 2043 |
...rest, |
| 2044 |
name, |
| 2045 |
attributes: newAttributes, |
| 2046 |
innerBlocks: deserializeBlockAttributes(innerBlocks ?? []) |
| 2047 |
}; |
| 2048 |
}); |
| 2049 |
} |
| 2050 |
function areBlocksEqual(gblock, yblock) { |
| 2051 |
const yblockAsJson = yblock.toJSON(); |
| 2052 |
const overwrites = { |
| 2053 |
innerBlocks: null, |
| 2054 |
clientId: null |
| 2055 |
}; |
| 2056 |
const res = (0, import_es62.default)( |
| 2057 |
Object.assign({}, gblock, overwrites), |
| 2058 |
Object.assign({}, yblockAsJson, overwrites) |
| 2059 |
); |
| 2060 |
const inners = gblock.innerBlocks || []; |
| 2061 |
const yinners = yblock.get("innerBlocks"); |
| 2062 |
return res && inners.length === yinners?.length && inners.every( |
| 2063 |
(block, i) => areBlocksEqual(block, yinners.get(i)) |
| 2064 |
); |
| 2065 |
} |
| 2066 |
function createNewYAttributeMap(blockName, attributes) { |
| 2067 |
return new import_sync9.Y.Map( |
| 2068 |
Object.entries(attributes).map( |
| 2069 |
([attributeName, attributeValue]) => { |
| 2070 |
return [ |
| 2071 |
attributeName, |
| 2072 |
createNewYAttributeValue( |
| 2073 |
blockName, |
| 2074 |
attributeName, |
| 2075 |
attributeValue |
| 2076 |
) |
| 2077 |
]; |
| 2078 |
} |
| 2079 |
) |
| 2080 |
); |
| 2081 |
} |
| 2082 |
function createNewYAttributeValue(blockName, attributeName, attributeValue) { |
| 2083 |
const isRichText = isRichTextAttribute(blockName, attributeName); |
| 2084 |
if (isRichText) { |
| 2085 |
return new import_sync9.Y.Text(attributeValue?.toString() ?? ""); |
| 2086 |
} |
| 2087 |
return attributeValue; |
| 2088 |
} |
| 2089 |
function createNewYBlock(block) { |
| 2090 |
return createYMap( |
| 2091 |
Object.fromEntries( |
| 2092 |
Object.entries(block).map(([key, value]) => { |
| 2093 |
switch (key) { |
| 2094 |
case "attributes": { |
| 2095 |
return [ |
| 2096 |
key, |
| 2097 |
createNewYAttributeMap(block.name, value) |
| 2098 |
]; |
| 2099 |
} |
| 2100 |
case "innerBlocks": { |
| 2101 |
const innerBlocks = new import_sync9.Y.Array(); |
| 2102 |
if (!Array.isArray(value)) { |
| 2103 |
return [key, innerBlocks]; |
| 2104 |
} |
| 2105 |
innerBlocks.insert( |
| 2106 |
0, |
| 2107 |
value.map( |
| 2108 |
(innerBlock) => createNewYBlock(innerBlock) |
| 2109 |
) |
| 2110 |
); |
| 2111 |
return [key, innerBlocks]; |
| 2112 |
} |
| 2113 |
default: |
| 2114 |
return [key, value]; |
| 2115 |
} |
| 2116 |
}) |
| 2117 |
) |
| 2118 |
); |
| 2119 |
} |
| 2120 |
function mergeCrdtBlocks(yblocks, incomingBlocks, cursorPosition) { |
| 2121 |
if (!serializableBlocksCache.has(incomingBlocks)) { |
| 2122 |
serializableBlocksCache.set( |
| 2123 |
incomingBlocks, |
| 2124 |
makeBlocksSerializable(incomingBlocks) |
| 2125 |
); |
| 2126 |
} |
| 2127 |
const blocksToSync = serializableBlocksCache.get(incomingBlocks) ?? []; |
| 2128 |
const numOfCommonEntries = Math.min( |
| 2129 |
blocksToSync.length ?? 0, |
| 2130 |
yblocks.length |
| 2131 |
); |
| 2132 |
let left = 0; |
| 2133 |
let right = 0; |
| 2134 |
for (; left < numOfCommonEntries && areBlocksEqual(blocksToSync[left], yblocks.get(left)); left++) { |
| 2135 |
} |
| 2136 |
for (; right < numOfCommonEntries - left && areBlocksEqual( |
| 2137 |
blocksToSync[blocksToSync.length - right - 1], |
| 2138 |
yblocks.get(yblocks.length - right - 1) |
| 2139 |
); right++) { |
| 2140 |
} |
| 2141 |
const numOfUpdatesNeeded = numOfCommonEntries - left - right; |
| 2142 |
const numOfInsertionsNeeded = Math.max( |
| 2143 |
0, |
| 2144 |
blocksToSync.length - yblocks.length |
| 2145 |
); |
| 2146 |
const numOfDeletionsNeeded = Math.max( |
| 2147 |
0, |
| 2148 |
yblocks.length - blocksToSync.length |
| 2149 |
); |
| 2150 |
for (let i = 0; i < numOfUpdatesNeeded; i++, left++) { |
| 2151 |
const block = blocksToSync[left]; |
| 2152 |
const yblock = yblocks.get(left); |
| 2153 |
Object.entries(block).forEach(([key, value]) => { |
| 2154 |
switch (key) { |
| 2155 |
case "attributes": { |
| 2156 |
const currentAttributes = yblock.get(key); |
| 2157 |
if (!currentAttributes) { |
| 2158 |
yblock.set( |
| 2159 |
key, |
| 2160 |
createNewYAttributeMap(block.name, value) |
| 2161 |
); |
| 2162 |
break; |
| 2163 |
} |
| 2164 |
Object.entries(value).forEach( |
| 2165 |
([attributeName, attributeValue]) => { |
| 2166 |
const currentAttribute = currentAttributes?.get(attributeName); |
| 2167 |
const isExpectedType = isExpectedAttributeType( |
| 2168 |
block.name, |
| 2169 |
attributeName, |
| 2170 |
currentAttribute |
| 2171 |
); |
| 2172 |
const isAttributeChanged = !isExpectedType || !(0, import_es62.default)( |
| 2173 |
currentAttribute, |
| 2174 |
attributeValue |
| 2175 |
); |
| 2176 |
if (isAttributeChanged) { |
| 2177 |
updateYBlockAttribute( |
| 2178 |
block.name, |
| 2179 |
attributeName, |
| 2180 |
attributeValue, |
| 2181 |
currentAttributes, |
| 2182 |
cursorPosition |
| 2183 |
); |
| 2184 |
} |
| 2185 |
} |
| 2186 |
); |
| 2187 |
currentAttributes.forEach( |
| 2188 |
(_attrValue, attrName) => { |
| 2189 |
if (!value.hasOwnProperty(attrName)) { |
| 2190 |
currentAttributes.delete(attrName); |
| 2191 |
} |
| 2192 |
} |
| 2193 |
); |
| 2194 |
break; |
| 2195 |
} |
| 2196 |
case "innerBlocks": { |
| 2197 |
let yInnerBlocks = yblock.get(key); |
| 2198 |
if (!(yInnerBlocks instanceof import_sync9.Y.Array)) { |
| 2199 |
yInnerBlocks = new import_sync9.Y.Array(); |
| 2200 |
yblock.set(key, yInnerBlocks); |
| 2201 |
} |
| 2202 |
mergeCrdtBlocks( |
| 2203 |
yInnerBlocks, |
| 2204 |
value ?? [], |
| 2205 |
cursorPosition |
| 2206 |
); |
| 2207 |
break; |
| 2208 |
} |
| 2209 |
default: |
| 2210 |
if (!(0, import_es62.default)(block[key], yblock.get(key))) { |
| 2211 |
yblock.set(key, value); |
| 2212 |
} |
| 2213 |
} |
| 2214 |
}); |
| 2215 |
yblock.forEach((_v, k) => { |
| 2216 |
if (!block.hasOwnProperty(k)) { |
| 2217 |
yblock.delete(k); |
| 2218 |
} |
| 2219 |
}); |
| 2220 |
} |
| 2221 |
yblocks.delete(left, numOfDeletionsNeeded); |
| 2222 |
for (let i = 0; i < numOfInsertionsNeeded; i++, left++) { |
| 2223 |
const newBlock = [createNewYBlock(blocksToSync[left])]; |
| 2224 |
yblocks.insert(left, newBlock); |
| 2225 |
} |
| 2226 |
const knownClientIds = /* @__PURE__ */ new Set(); |
| 2227 |
for (let j = 0; j < yblocks.length; j++) { |
| 2228 |
const yblock = yblocks.get(j); |
| 2229 |
let clientId = yblock.get("clientId"); |
| 2230 |
if (!clientId) { |
| 2231 |
continue; |
| 2232 |
} |
| 2233 |
if (knownClientIds.has(clientId)) { |
| 2234 |
clientId = v4_default(); |
| 2235 |
yblock.set("clientId", clientId); |
| 2236 |
} |
| 2237 |
knownClientIds.add(clientId); |
| 2238 |
} |
| 2239 |
} |
| 2240 |
function updateYBlockAttribute(blockName, attributeName, attributeValue, currentAttributes, cursorPosition) { |
| 2241 |
const isRichText = isRichTextAttribute(blockName, attributeName); |
| 2242 |
const currentAttribute = currentAttributes.get(attributeName); |
| 2243 |
if (isRichText && "string" === typeof attributeValue && currentAttributes.has(attributeName) && currentAttribute instanceof import_sync9.Y.Text) { |
| 2244 |
mergeRichTextUpdate(currentAttribute, attributeValue, cursorPosition); |
| 2245 |
} else { |
| 2246 |
currentAttributes.set( |
| 2247 |
attributeName, |
| 2248 |
createNewYAttributeValue(blockName, attributeName, attributeValue) |
| 2249 |
); |
| 2250 |
} |
| 2251 |
} |
| 2252 |
var cachedBlockAttributeTypes; |
| 2253 |
function getBlockAttributeType(blockName, attributeName) { |
| 2254 |
if (!cachedBlockAttributeTypes) { |
| 2255 |
cachedBlockAttributeTypes = /* @__PURE__ */ new Map(); |
| 2256 |
for (const blockType of (0, import_blocks.getBlockTypes)()) { |
| 2257 |
cachedBlockAttributeTypes.set( |
| 2258 |
blockType.name, |
| 2259 |
new Map( |
| 2260 |
Object.entries(blockType.attributes ?? {}).map( |
| 2261 |
([name, definition]) => { |
| 2262 |
const { role, type, query } = definition; |
| 2263 |
return [name, { role, type, query }]; |
| 2264 |
} |
| 2265 |
) |
| 2266 |
) |
| 2267 |
); |
| 2268 |
} |
| 2269 |
} |
| 2270 |
return cachedBlockAttributeTypes.get(blockName)?.get(attributeName); |
| 2271 |
} |
| 2272 |
function isExpectedAttributeType(blockName, attributeName, attributeValue) { |
| 2273 |
const expectedAttributeType = getBlockAttributeType( |
| 2274 |
blockName, |
| 2275 |
attributeName |
| 2276 |
)?.type; |
| 2277 |
if (expectedAttributeType === "rich-text") { |
| 2278 |
return attributeValue instanceof import_sync9.Y.Text; |
| 2279 |
} |
| 2280 |
if (expectedAttributeType === "string") { |
| 2281 |
return typeof attributeValue === "string"; |
| 2282 |
} |
| 2283 |
return true; |
| 2284 |
} |
| 2285 |
function isLocalAttribute(blockName, attributeName) { |
| 2286 |
return "local" === getBlockAttributeType(blockName, attributeName)?.role; |
| 2287 |
} |
| 2288 |
function isRichTextAttribute(blockName, attributeName) { |
| 2289 |
return "rich-text" === getBlockAttributeType(blockName, attributeName)?.type; |
| 2290 |
} |
| 2291 |
var localDoc; |
| 2292 |
function mergeRichTextUpdate(blockYText, updatedValue, cursorPosition = null) { |
| 2293 |
if (!localDoc) { |
| 2294 |
localDoc = new import_sync9.Y.Doc(); |
| 2295 |
} |
| 2296 |
const localYText = localDoc.getText("temporary-text"); |
| 2297 |
localYText.delete(0, localYText.length); |
| 2298 |
localYText.insert(0, updatedValue); |
| 2299 |
const currentValueAsDelta = new Delta(blockYText.toDelta()); |
| 2300 |
const updatedValueAsDelta = new Delta(localYText.toDelta()); |
| 2301 |
const deltaDiff = currentValueAsDelta.diffWithCursor( |
| 2302 |
updatedValueAsDelta, |
| 2303 |
cursorPosition |
| 2304 |
); |
| 2305 |
blockYText.applyDelta(deltaDiff.ops); |
| 2306 |
} |
| 2307 |
|
| 2308 |
// packages/core-data/build-module/utils/crdt-selection.mjs |
| 2309 |
var import_data6 = __toESM(require_data(), 1); |
| 2310 |
var import_block_editor4 = __toESM(require_block_editor(), 1); |
| 2311 |
var import_blocks2 = __toESM(require_blocks(), 1); |
| 2312 |
var import_sync12 = __toESM(require_sync(), 1); |
| 2313 |
|
| 2314 |
// packages/core-data/build-module/utils/block-selection-history.mjs |
| 2315 |
var import_sync11 = __toESM(require_sync(), 1); |
| 2316 |
var SELECTION_HISTORY_DEFAULT_SIZE = 5; |
| 2317 |
var YSelectionType = /* @__PURE__ */ ((YSelectionType2) => { |
| 2318 |
YSelectionType2["RelativeSelection"] = "RelativeSelection"; |
| 2319 |
YSelectionType2["BlockSelection"] = "BlockSelection"; |
| 2320 |
return YSelectionType2; |
| 2321 |
})(YSelectionType || {}); |
| 2322 |
function createBlockSelectionHistory(ydoc, historySize = SELECTION_HISTORY_DEFAULT_SIZE) { |
| 2323 |
let history = []; |
| 2324 |
const getSelectionHistory2 = () => { |
| 2325 |
return history.slice(0); |
| 2326 |
}; |
| 2327 |
const updateSelection = (newSelection) => { |
| 2328 |
if (!newSelection?.selectionStart?.clientId || !newSelection?.selectionEnd?.clientId) { |
| 2329 |
return; |
| 2330 |
} |
| 2331 |
const { selectionStart, selectionEnd } = newSelection; |
| 2332 |
const start = convertWPBlockSelectionToSelection( |
| 2333 |
selectionStart, |
| 2334 |
ydoc |
| 2335 |
); |
| 2336 |
const end = convertWPBlockSelectionToSelection(selectionEnd, ydoc); |
| 2337 |
addToHistory({ start, end }); |
| 2338 |
}; |
| 2339 |
const addToHistory = (yFullSelection) => { |
| 2340 |
const startClientId = yFullSelection.start.clientId; |
| 2341 |
const endClientId = yFullSelection.end.clientId; |
| 2342 |
history = history.filter((entry) => { |
| 2343 |
const isSameBlockCombination = entry.start.clientId === startClientId && entry.end.clientId === endClientId; |
| 2344 |
return !isSameBlockCombination; |
| 2345 |
}); |
| 2346 |
history.unshift(yFullSelection); |
| 2347 |
if (history.length > historySize + 1) { |
| 2348 |
history = history.slice(0, historySize + 1); |
| 2349 |
} |
| 2350 |
}; |
| 2351 |
return { |
| 2352 |
getSelectionHistory: getSelectionHistory2, |
| 2353 |
updateSelection |
| 2354 |
}; |
| 2355 |
} |
| 2356 |
function convertWPBlockSelectionToSelection(selection, ydoc) { |
| 2357 |
const clientId = selection.clientId; |
| 2358 |
const block = findBlockByClientIdInDoc(clientId, ydoc); |
| 2359 |
const attributes = block?.get("attributes"); |
| 2360 |
const attributeKey = selection.attributeKey; |
| 2361 |
const changedYText = attributeKey ? attributes?.get(attributeKey) : void 0; |
| 2362 |
const isYText = changedYText instanceof import_sync11.Y.Text; |
| 2363 |
const isFullyDefinedSelection = attributeKey && clientId; |
| 2364 |
if (!isYText || !isFullyDefinedSelection) { |
| 2365 |
return { |
| 2366 |
type: "BlockSelection", |
| 2367 |
clientId |
| 2368 |
}; |
| 2369 |
} |
| 2370 |
const offset = selection.offset ?? 0; |
| 2371 |
const relativePosition = import_sync11.Y.createRelativePositionFromTypeIndex( |
| 2372 |
changedYText, |
| 2373 |
richTextOffsetToHtmlIndex(changedYText.toString(), offset) |
| 2374 |
); |
| 2375 |
return { |
| 2376 |
type: "RelativeSelection", |
| 2377 |
attributeKey, |
| 2378 |
relativePosition, |
| 2379 |
clientId, |
| 2380 |
offset |
| 2381 |
}; |
| 2382 |
} |
| 2383 |
|
| 2384 |
// packages/core-data/build-module/utils/crdt-selection.mjs |
| 2385 |
var selectionHistoryMap = /* @__PURE__ */ new WeakMap(); |
| 2386 |
function getBlockSelectionHistory(ydoc) { |
| 2387 |
let history = selectionHistoryMap.get(ydoc); |
| 2388 |
if (!history) { |
| 2389 |
history = createBlockSelectionHistory(ydoc); |
| 2390 |
selectionHistoryMap.set(ydoc, history); |
| 2391 |
} |
| 2392 |
return history; |
| 2393 |
} |
| 2394 |
function getSelectionHistory(ydoc) { |
| 2395 |
return getBlockSelectionHistory(ydoc).getSelectionHistory(); |
| 2396 |
} |
| 2397 |
function updateSelectionHistory(ydoc, wpSelection) { |
| 2398 |
return getBlockSelectionHistory(ydoc).updateSelection(wpSelection); |
| 2399 |
} |
| 2400 |
function convertYSelectionToBlockSelection(ySelection, ydoc) { |
| 2401 |
if (ySelection.type === YSelectionType.RelativeSelection) { |
| 2402 |
const { relativePosition, attributeKey, clientId } = ySelection; |
| 2403 |
const absolutePosition = import_sync12.Y.createAbsolutePositionFromRelativePosition( |
| 2404 |
relativePosition, |
| 2405 |
ydoc |
| 2406 |
); |
| 2407 |
if (absolutePosition) { |
| 2408 |
return { |
| 2409 |
clientId, |
| 2410 |
attributeKey, |
| 2411 |
offset: htmlIndexToRichTextOffset( |
| 2412 |
absolutePosition.type.toString(), |
| 2413 |
absolutePosition.index |
| 2414 |
) |
| 2415 |
}; |
| 2416 |
} |
| 2417 |
} else if (ySelection.type === YSelectionType.BlockSelection) { |
| 2418 |
return { |
| 2419 |
clientId: ySelection.clientId, |
| 2420 |
attributeKey: void 0, |
| 2421 |
offset: void 0 |
| 2422 |
}; |
| 2423 |
} |
| 2424 |
return null; |
| 2425 |
} |
| 2426 |
function convertYFullSelectionToWPSelection(yFullSelection, ydoc) { |
| 2427 |
const { start, end } = yFullSelection; |
| 2428 |
const startBlock = findBlockByClientIdInDoc(start.clientId, ydoc); |
| 2429 |
const endBlock = findBlockByClientIdInDoc(end.clientId, ydoc); |
| 2430 |
if (!startBlock || !endBlock) { |
| 2431 |
return null; |
| 2432 |
} |
| 2433 |
const startBlockSelection = convertYSelectionToBlockSelection( |
| 2434 |
start, |
| 2435 |
ydoc |
| 2436 |
); |
| 2437 |
const endBlockSelection = convertYSelectionToBlockSelection(end, ydoc); |
| 2438 |
if (startBlockSelection === null || endBlockSelection === null) { |
| 2439 |
return null; |
| 2440 |
} |
| 2441 |
return { |
| 2442 |
selectionStart: startBlockSelection, |
| 2443 |
selectionEnd: endBlockSelection |
| 2444 |
}; |
| 2445 |
} |
| 2446 |
function findSelectionFromHistory(ydoc, selectionHistory) { |
| 2447 |
for (const positionToTry of selectionHistory) { |
| 2448 |
const result = convertYFullSelectionToWPSelection( |
| 2449 |
positionToTry, |
| 2450 |
ydoc |
| 2451 |
); |
| 2452 |
if (result !== null) { |
| 2453 |
return result; |
| 2454 |
} |
| 2455 |
} |
| 2456 |
return null; |
| 2457 |
} |
| 2458 |
function restoreSelection(selectionHistory, ydoc) { |
| 2459 |
const selectionToRestore = findSelectionFromHistory( |
| 2460 |
ydoc, |
| 2461 |
selectionHistory |
| 2462 |
); |
| 2463 |
if (selectionToRestore === null) { |
| 2464 |
return; |
| 2465 |
} |
| 2466 |
const { getBlock } = (0, import_data6.select)(import_block_editor4.store); |
| 2467 |
const { resetSelection } = (0, import_data6.dispatch)(import_block_editor4.store); |
| 2468 |
const { selectionStart, selectionEnd } = selectionToRestore; |
| 2469 |
const isSelectionInSameBlock = selectionStart.clientId === selectionEnd.clientId; |
| 2470 |
if (isSelectionInSameBlock) { |
| 2471 |
const block = getBlock(selectionStart.clientId); |
| 2472 |
const isBlockEmpty = block && (0, import_blocks2.isUnmodifiedBlock)(block); |
| 2473 |
const isBeginningOfEmptyBlock = 0 === selectionStart.offset && 0 === selectionEnd.offset && isBlockEmpty && !selectionStart.attributeKey && !selectionEnd.attributeKey; |
| 2474 |
if (isBeginningOfEmptyBlock) { |
| 2475 |
const selectionStartWithoutOffset = { |
| 2476 |
clientId: selectionStart.clientId |
| 2477 |
}; |
| 2478 |
const selectionEndWithoutOffset = { |
| 2479 |
clientId: selectionEnd.clientId |
| 2480 |
}; |
| 2481 |
resetSelection( |
| 2482 |
selectionStartWithoutOffset, |
| 2483 |
selectionEndWithoutOffset, |
| 2484 |
0 |
| 2485 |
); |
| 2486 |
} else { |
| 2487 |
resetSelection(selectionStart, selectionEnd, 0); |
| 2488 |
} |
| 2489 |
} else { |
| 2490 |
resetSelection(selectionEnd, selectionEnd, 0); |
| 2491 |
} |
| 2492 |
} |
| 2493 |
function getShiftedSelection(ydoc, selectionHistory) { |
| 2494 |
if (selectionHistory.length === 0) { |
| 2495 |
return null; |
| 2496 |
} |
| 2497 |
const { start, end } = selectionHistory[0]; |
| 2498 |
if (start.type === YSelectionType.BlockSelection || end.type === YSelectionType.BlockSelection) { |
| 2499 |
return null; |
| 2500 |
} |
| 2501 |
const selectionStart = convertYSelectionToBlockSelection(start, ydoc); |
| 2502 |
const selectionEnd = convertYSelectionToBlockSelection(end, ydoc); |
| 2503 |
if (!selectionStart || !selectionEnd) { |
| 2504 |
return null; |
| 2505 |
} |
| 2506 |
const startShifted = selectionStart.offset !== start.offset; |
| 2507 |
const endShifted = selectionEnd.offset !== end.offset; |
| 2508 |
if (!startShifted && !endShifted) { |
| 2509 |
return null; |
| 2510 |
} |
| 2511 |
return { selectionStart, selectionEnd }; |
| 2512 |
} |
| 2513 |
|
| 2514 |
// packages/core-data/build-module/utils/crdt.mjs |
| 2515 |
var POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE = "_crdt_document"; |
| 2516 |
var disallowedPostMetaKeys = /* @__PURE__ */ new Set([ |
| 2517 |
POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE |
| 2518 |
]); |
| 2519 |
function defaultApplyChangesToCRDTDoc(ydoc, changes) { |
| 2520 |
const ymap = getRootMap(ydoc, CRDT_RECORD_MAP_KEY); |
| 2521 |
Object.entries(changes).forEach(([key, newValue]) => { |
| 2522 |
if ("function" === typeof newValue) { |
| 2523 |
return; |
| 2524 |
} |
| 2525 |
switch (key) { |
| 2526 |
// Add support for additional data types here. |
| 2527 |
default: { |
| 2528 |
const currentValue = ymap.get(key); |
| 2529 |
updateMapValue(ymap, key, currentValue, newValue); |
| 2530 |
} |
| 2531 |
} |
| 2532 |
}); |
| 2533 |
} |
| 2534 |
function applyPostChangesToCRDTDoc(ydoc, changes, syncedProperties) { |
| 2535 |
const ymap = getRootMap(ydoc, CRDT_RECORD_MAP_KEY); |
| 2536 |
Object.keys(changes).forEach((key) => { |
| 2537 |
if (!syncedProperties.has(key)) { |
| 2538 |
return; |
| 2539 |
} |
| 2540 |
const newValue = changes[key]; |
| 2541 |
if ("function" === typeof newValue) { |
| 2542 |
return; |
| 2543 |
} |
| 2544 |
switch (key) { |
| 2545 |
case "blocks": { |
| 2546 |
if (!newValue) { |
| 2547 |
ymap.set(key, void 0); |
| 2548 |
break; |
| 2549 |
} |
| 2550 |
let currentBlocks = ymap.get(key); |
| 2551 |
if (!(currentBlocks instanceof import_sync13.Y.Array)) { |
| 2552 |
currentBlocks = new import_sync13.Y.Array(); |
| 2553 |
ymap.set(key, currentBlocks); |
| 2554 |
} |
| 2555 |
const cursorPosition = changes.selection?.selectionStart?.offset ?? null; |
| 2556 |
mergeCrdtBlocks(currentBlocks, newValue, cursorPosition); |
| 2557 |
break; |
| 2558 |
} |
| 2559 |
case "content": |
| 2560 |
case "excerpt": |
| 2561 |
case "title": { |
| 2562 |
const currentValue = ymap.get(key); |
| 2563 |
let rawValue = getRawValue(newValue); |
| 2564 |
if (key === "title" && !currentValue?.toString() && "Auto Draft" === rawValue) { |
| 2565 |
rawValue = ""; |
| 2566 |
} |
| 2567 |
if (currentValue instanceof import_sync13.Y.Text) { |
| 2568 |
mergeRichTextUpdate(currentValue, rawValue ?? ""); |
| 2569 |
} else { |
| 2570 |
const newYText = new import_sync13.Y.Text(rawValue ?? ""); |
| 2571 |
ymap.set(key, newYText); |
| 2572 |
} |
| 2573 |
break; |
| 2574 |
} |
| 2575 |
// "Meta" is overloaded term; here, it refers to post meta. |
| 2576 |
case "meta": { |
| 2577 |
let metaMap = ymap.get("meta"); |
| 2578 |
if (!isYMap(metaMap)) { |
| 2579 |
metaMap = createYMap(); |
| 2580 |
ymap.set("meta", metaMap); |
| 2581 |
} |
| 2582 |
Object.entries(newValue ?? {}).forEach( |
| 2583 |
([metaKey, metaValue]) => { |
| 2584 |
if (disallowedPostMetaKeys.has(metaKey)) { |
| 2585 |
return; |
| 2586 |
} |
| 2587 |
updateMapValue( |
| 2588 |
metaMap, |
| 2589 |
metaKey, |
| 2590 |
metaMap.get(metaKey), |
| 2591 |
// current value in CRDT |
| 2592 |
metaValue |
| 2593 |
// new value from changes |
| 2594 |
); |
| 2595 |
} |
| 2596 |
); |
| 2597 |
break; |
| 2598 |
} |
| 2599 |
case "slug": { |
| 2600 |
if (!newValue) { |
| 2601 |
break; |
| 2602 |
} |
| 2603 |
const currentValue = ymap.get(key); |
| 2604 |
updateMapValue(ymap, key, currentValue, newValue); |
| 2605 |
break; |
| 2606 |
} |
| 2607 |
// Add support for additional properties here. |
| 2608 |
default: { |
| 2609 |
const currentValue = ymap.get(key); |
| 2610 |
updateMapValue(ymap, key, currentValue, newValue); |
| 2611 |
} |
| 2612 |
} |
| 2613 |
}); |
| 2614 |
if (changes.selection) { |
| 2615 |
const selection = changes.selection; |
| 2616 |
setTimeout(() => { |
| 2617 |
updateSelectionHistory(ydoc, selection); |
| 2618 |
}, 0); |
| 2619 |
} |
| 2620 |
} |
| 2621 |
function defaultGetChangesFromCRDTDoc(crdtDoc) { |
| 2622 |
return getRootMap(crdtDoc, CRDT_RECORD_MAP_KEY).toJSON(); |
| 2623 |
} |
| 2624 |
function getPostChangesFromCRDTDoc(ydoc, editedRecord, syncedProperties) { |
| 2625 |
const ymap = getRootMap(ydoc, CRDT_RECORD_MAP_KEY); |
| 2626 |
let allowedMetaChanges = {}; |
| 2627 |
const changes = Object.fromEntries( |
| 2628 |
Object.entries(ymap.toJSON()).filter(([key, newValue]) => { |
| 2629 |
if (!syncedProperties.has(key)) { |
| 2630 |
return false; |
| 2631 |
} |
| 2632 |
const currentValue = editedRecord[key]; |
| 2633 |
switch (key) { |
| 2634 |
case "blocks": { |
| 2635 |
if (ydoc.meta?.get(CRDT_DOC_META_PERSISTENCE_KEY) && editedRecord.content) { |
| 2636 |
const blocksJson = ymap.get("blocks")?.toJSON() ?? []; |
| 2637 |
return (0, import_blocks3.__unstableSerializeAndClean)(blocksJson).trim() !== getRawValue(editedRecord.content); |
| 2638 |
} |
| 2639 |
return true; |
| 2640 |
} |
| 2641 |
case "date": { |
| 2642 |
const currentDateIsFloating = null === currentValue || editedRecord.modified === currentValue; |
| 2643 |
if (currentDateIsFloating) { |
| 2644 |
return false; |
| 2645 |
} |
| 2646 |
return haveValuesChanged(currentValue, newValue); |
| 2647 |
} |
| 2648 |
case "meta": { |
| 2649 |
allowedMetaChanges = Object.fromEntries( |
| 2650 |
Object.entries(newValue ?? {}).filter( |
| 2651 |
([metaKey]) => !disallowedPostMetaKeys.has(metaKey) |
| 2652 |
) |
| 2653 |
); |
| 2654 |
const mergedValue = { |
| 2655 |
...currentValue, |
| 2656 |
...allowedMetaChanges |
| 2657 |
}; |
| 2658 |
return haveValuesChanged(currentValue, mergedValue); |
| 2659 |
} |
| 2660 |
case "status": { |
| 2661 |
if ("auto-draft" === newValue) { |
| 2662 |
return false; |
| 2663 |
} |
| 2664 |
return haveValuesChanged(currentValue, newValue); |
| 2665 |
} |
| 2666 |
case "content": |
| 2667 |
case "excerpt": |
| 2668 |
case "title": { |
| 2669 |
return haveValuesChanged( |
| 2670 |
getRawValue(currentValue), |
| 2671 |
newValue |
| 2672 |
); |
| 2673 |
} |
| 2674 |
// Add support for additional data types here. |
| 2675 |
default: { |
| 2676 |
return haveValuesChanged(currentValue, newValue); |
| 2677 |
} |
| 2678 |
} |
| 2679 |
}) |
| 2680 |
); |
| 2681 |
if (changes.blocks) { |
| 2682 |
changes.blocks = deserializeBlockAttributes( |
| 2683 |
changes.blocks |
| 2684 |
); |
| 2685 |
} |
| 2686 |
if ("object" === typeof changes.meta) { |
| 2687 |
changes.meta = { |
| 2688 |
...editedRecord.meta, |
| 2689 |
...allowedMetaChanges |
| 2690 |
}; |
| 2691 |
} |
| 2692 |
const selectionHistory = getSelectionHistory(ydoc); |
| 2693 |
const shiftedSelection = getShiftedSelection(ydoc, selectionHistory); |
| 2694 |
if (shiftedSelection) { |
| 2695 |
changes.selection = { |
| 2696 |
...shiftedSelection, |
| 2697 |
initialPosition: 0 |
| 2698 |
}; |
| 2699 |
} |
| 2700 |
return changes; |
| 2701 |
} |
| 2702 |
var defaultSyncConfig = { |
| 2703 |
applyChangesToCRDTDoc: defaultApplyChangesToCRDTDoc, |
| 2704 |
createAwareness: (ydoc) => new BaseAwareness(ydoc), |
| 2705 |
getChangesFromCRDTDoc: defaultGetChangesFromCRDTDoc |
| 2706 |
}; |
| 2707 |
function getRawValue(value) { |
| 2708 |
if ("string" === typeof value) { |
| 2709 |
return value; |
| 2710 |
} |
| 2711 |
if (value && "object" === typeof value && "raw" in value && "string" === typeof value.raw) { |
| 2712 |
return value.raw; |
| 2713 |
} |
| 2714 |
return void 0; |
| 2715 |
} |
| 2716 |
function haveValuesChanged(currentValue, newValue) { |
| 2717 |
return !(0, import_es63.default)(currentValue, newValue); |
| 2718 |
} |
| 2719 |
function updateMapValue(map, key, currentValue, newValue) { |
| 2720 |
if (void 0 === newValue) { |
| 2721 |
map.delete(key); |
| 2722 |
return; |
| 2723 |
} |
| 2724 |
if (haveValuesChanged(currentValue, newValue)) { |
| 2725 |
map.set(key, newValue); |
| 2726 |
} |
| 2727 |
} |
| 2728 |
|
| 2729 |
// packages/core-data/build-module/entities.mjs |
| 2730 |
var DEFAULT_ENTITY_KEY = "id"; |
| 2731 |
var POST_RAW_ATTRIBUTES = ["title", "excerpt", "content"]; |
| 2732 |
var blocksTransientEdits = { |
| 2733 |
blocks: { |
| 2734 |
read: (record) => (0, import_blocks4.parse)(record.content?.raw ?? ""), |
| 2735 |
write: (record) => ({ |
| 2736 |
content: (0, import_blocks4.__unstableSerializeAndClean)(record.blocks) |
| 2737 |
}) |
| 2738 |
} |
| 2739 |
}; |
| 2740 |
var rootEntitiesConfig = [ |
| 2741 |
{ |
| 2742 |
label: (0, import_i18n.__)("Base"), |
| 2743 |
kind: "root", |
| 2744 |
key: false, |
| 2745 |
name: "__unstableBase", |
| 2746 |
baseURL: "/", |
| 2747 |
baseURLParams: { |
| 2748 |
// Please also change the preload path when changing this. |
| 2749 |
// @see lib/compat/wordpress-7.0/preload.php |
| 2750 |
_fields: [ |
| 2751 |
"description", |
| 2752 |
"gmt_offset", |
| 2753 |
"home", |
| 2754 |
"image_sizes", |
| 2755 |
"image_size_threshold", |
| 2756 |
"image_output_formats", |
| 2757 |
"jpeg_interlaced", |
| 2758 |
"png_interlaced", |
| 2759 |
"gif_interlaced", |
| 2760 |
"name", |
| 2761 |
"site_icon", |
| 2762 |
"site_icon_url", |
| 2763 |
"site_logo", |
| 2764 |
"timezone_string", |
| 2765 |
"url", |
| 2766 |
"page_for_posts", |
| 2767 |
"page_on_front", |
| 2768 |
"show_on_front" |
| 2769 |
].join(",") |
| 2770 |
}, |
| 2771 |
// The entity doesn't support selecting multiple records. |
| 2772 |
// The property is maintained for backward compatibility. |
| 2773 |
plural: "__unstableBases" |
| 2774 |
}, |
| 2775 |
{ |
| 2776 |
label: (0, import_i18n.__)("Post Type"), |
| 2777 |
name: "postType", |
| 2778 |
kind: "root", |
| 2779 |
key: "slug", |
| 2780 |
baseURL: "/wp/v2/types", |
| 2781 |
baseURLParams: { context: "edit" }, |
| 2782 |
plural: "postTypes" |
| 2783 |
}, |
| 2784 |
{ |
| 2785 |
name: "media", |
| 2786 |
kind: "root", |
| 2787 |
baseURL: "/wp/v2/media", |
| 2788 |
baseURLParams: { context: "edit" }, |
| 2789 |
plural: "mediaItems", |
| 2790 |
label: (0, import_i18n.__)("Media"), |
| 2791 |
rawAttributes: ["caption", "title", "description"], |
| 2792 |
supportsPagination: true |
| 2793 |
}, |
| 2794 |
{ |
| 2795 |
name: "taxonomy", |
| 2796 |
kind: "root", |
| 2797 |
key: "slug", |
| 2798 |
baseURL: "/wp/v2/taxonomies", |
| 2799 |
baseURLParams: { context: "edit" }, |
| 2800 |
plural: "taxonomies", |
| 2801 |
label: (0, import_i18n.__)("Taxonomy") |
| 2802 |
}, |
| 2803 |
{ |
| 2804 |
name: "sidebar", |
| 2805 |
kind: "root", |
| 2806 |
baseURL: "/wp/v2/sidebars", |
| 2807 |
baseURLParams: { context: "edit" }, |
| 2808 |
plural: "sidebars", |
| 2809 |
transientEdits: { blocks: true }, |
| 2810 |
label: (0, import_i18n.__)("Widget areas") |
| 2811 |
}, |
| 2812 |
{ |
| 2813 |
name: "widget", |
| 2814 |
kind: "root", |
| 2815 |
baseURL: "/wp/v2/widgets", |
| 2816 |
baseURLParams: { context: "edit" }, |
| 2817 |
plural: "widgets", |
| 2818 |
transientEdits: { blocks: true }, |
| 2819 |
label: (0, import_i18n.__)("Widgets") |
| 2820 |
}, |
| 2821 |
{ |
| 2822 |
name: "widgetType", |
| 2823 |
kind: "root", |
| 2824 |
baseURL: "/wp/v2/widget-types", |
| 2825 |
baseURLParams: { context: "edit" }, |
| 2826 |
plural: "widgetTypes", |
| 2827 |
label: (0, import_i18n.__)("Widget types") |
| 2828 |
}, |
| 2829 |
{ |
| 2830 |
label: (0, import_i18n.__)("User"), |
| 2831 |
name: "user", |
| 2832 |
kind: "root", |
| 2833 |
baseURL: "/wp/v2/users", |
| 2834 |
getTitle: (record) => record?.name || record?.slug, |
| 2835 |
baseURLParams: { context: "edit" }, |
| 2836 |
plural: "users", |
| 2837 |
supportsPagination: true |
| 2838 |
}, |
| 2839 |
{ |
| 2840 |
name: "comment", |
| 2841 |
kind: "root", |
| 2842 |
baseURL: "/wp/v2/comments", |
| 2843 |
baseURLParams: { context: "edit" }, |
| 2844 |
plural: "comments", |
| 2845 |
label: (0, import_i18n.__)("Comment"), |
| 2846 |
supportsPagination: true |
| 2847 |
}, |
| 2848 |
{ |
| 2849 |
name: "menu", |
| 2850 |
kind: "root", |
| 2851 |
baseURL: "/wp/v2/menus", |
| 2852 |
baseURLParams: { context: "edit" }, |
| 2853 |
plural: "menus", |
| 2854 |
label: (0, import_i18n.__)("Menu"), |
| 2855 |
supportsPagination: true |
| 2856 |
}, |
| 2857 |
{ |
| 2858 |
name: "menuItem", |
| 2859 |
kind: "root", |
| 2860 |
baseURL: "/wp/v2/menu-items", |
| 2861 |
baseURLParams: { context: "edit" }, |
| 2862 |
plural: "menuItems", |
| 2863 |
label: (0, import_i18n.__)("Menu Item"), |
| 2864 |
rawAttributes: ["title"], |
| 2865 |
supportsPagination: true |
| 2866 |
}, |
| 2867 |
{ |
| 2868 |
name: "menuLocation", |
| 2869 |
kind: "root", |
| 2870 |
baseURL: "/wp/v2/menu-locations", |
| 2871 |
baseURLParams: { context: "edit" }, |
| 2872 |
plural: "menuLocations", |
| 2873 |
label: (0, import_i18n.__)("Menu Location"), |
| 2874 |
key: "name" |
| 2875 |
}, |
| 2876 |
{ |
| 2877 |
label: (0, import_i18n.__)("Global Styles"), |
| 2878 |
name: "globalStyles", |
| 2879 |
kind: "root", |
| 2880 |
baseURL: "/wp/v2/global-styles", |
| 2881 |
baseURLParams: { context: "edit" }, |
| 2882 |
plural: "globalStylesVariations", |
| 2883 |
// Should be different from name. |
| 2884 |
getTitle: () => (0, import_i18n.__)("Custom Styles"), |
| 2885 |
getRevisionsUrl: (parentId, revisionId) => `/wp/v2/global-styles/${parentId}/revisions${revisionId ? "/" + revisionId : ""}`, |
| 2886 |
supportsPagination: true |
| 2887 |
}, |
| 2888 |
{ |
| 2889 |
label: (0, import_i18n.__)("Themes"), |
| 2890 |
name: "theme", |
| 2891 |
kind: "root", |
| 2892 |
baseURL: "/wp/v2/themes", |
| 2893 |
baseURLParams: { context: "edit" }, |
| 2894 |
plural: "themes", |
| 2895 |
key: "stylesheet" |
| 2896 |
}, |
| 2897 |
{ |
| 2898 |
label: (0, import_i18n.__)("Plugins"), |
| 2899 |
name: "plugin", |
| 2900 |
kind: "root", |
| 2901 |
baseURL: "/wp/v2/plugins", |
| 2902 |
baseURLParams: { context: "edit" }, |
| 2903 |
plural: "plugins", |
| 2904 |
key: "plugin" |
| 2905 |
}, |
| 2906 |
{ |
| 2907 |
label: (0, import_i18n.__)("Status"), |
| 2908 |
name: "status", |
| 2909 |
kind: "root", |
| 2910 |
baseURL: "/wp/v2/statuses", |
| 2911 |
baseURLParams: { context: "edit" }, |
| 2912 |
plural: "statuses", |
| 2913 |
key: "slug" |
| 2914 |
}, |
| 2915 |
{ |
| 2916 |
label: (0, import_i18n.__)("Registered Templates"), |
| 2917 |
name: "registeredTemplate", |
| 2918 |
kind: "root", |
| 2919 |
baseURL: "/wp/v2/registered-templates", |
| 2920 |
key: "id" |
| 2921 |
}, |
| 2922 |
{ |
| 2923 |
label: (0, import_i18n.__)("Font Collections"), |
| 2924 |
name: "fontCollection", |
| 2925 |
kind: "root", |
| 2926 |
baseURL: "/wp/v2/font-collections", |
| 2927 |
baseURLParams: { context: "view" }, |
| 2928 |
plural: "fontCollections", |
| 2929 |
key: "slug", |
| 2930 |
supportsPagination: true |
| 2931 |
}, |
| 2932 |
{ |
| 2933 |
label: (0, import_i18n.__)("Icons"), |
| 2934 |
name: "icon", |
| 2935 |
kind: "root", |
| 2936 |
baseURL: "/wp/v2/icons", |
| 2937 |
baseURLParams: { context: "view" }, |
| 2938 |
plural: "icons", |
| 2939 |
key: "name" |
| 2940 |
} |
| 2941 |
].map((entity2) => { |
| 2942 |
const syncEnabledRootEntities = /* @__PURE__ */ new Set(["comment"]); |
| 2943 |
if (syncEnabledRootEntities.has(entity2.name)) { |
| 2944 |
entity2.syncConfig = defaultSyncConfig; |
| 2945 |
} |
| 2946 |
return entity2; |
| 2947 |
}); |
| 2948 |
var deprecatedEntities = { |
| 2949 |
root: { |
| 2950 |
media: { |
| 2951 |
since: "6.9", |
| 2952 |
alternative: { |
| 2953 |
kind: "postType", |
| 2954 |
name: "attachment" |
| 2955 |
} |
| 2956 |
} |
| 2957 |
} |
| 2958 |
}; |
| 2959 |
var additionalEntityConfigLoaders = [ |
| 2960 |
{ kind: "postType", loadEntities: loadPostTypeEntities }, |
| 2961 |
{ kind: "taxonomy", loadEntities: loadTaxonomyEntities }, |
| 2962 |
{ |
| 2963 |
kind: "root", |
| 2964 |
name: "site", |
| 2965 |
plural: "sites", |
| 2966 |
loadEntities: loadSiteEntity |
| 2967 |
} |
| 2968 |
]; |
| 2969 |
var prePersistPostType = async (persistedRecord, edits, name, isTemplate) => { |
| 2970 |
const newEdits = {}; |
| 2971 |
if (!isTemplate && persistedRecord?.status === "auto-draft") { |
| 2972 |
if (!edits.status && !newEdits.status) { |
| 2973 |
newEdits.status = "draft"; |
| 2974 |
} |
| 2975 |
if ((!edits.title || edits.title === "Auto Draft") && !newEdits.title && (!persistedRecord?.title || persistedRecord?.title === "Auto Draft")) { |
| 2976 |
newEdits.title = ""; |
| 2977 |
} |
| 2978 |
} |
| 2979 |
if (persistedRecord) { |
| 2980 |
const objectType = `postType/${name}`; |
| 2981 |
const objectId = persistedRecord.id; |
| 2982 |
const serializedDoc = await getSyncManager()?.createPersistedCRDTDoc( |
| 2983 |
objectType, |
| 2984 |
objectId |
| 2985 |
); |
| 2986 |
if (serializedDoc) { |
| 2987 |
newEdits.meta = { |
| 2988 |
...edits.meta, |
| 2989 |
[POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE]: serializedDoc |
| 2990 |
}; |
| 2991 |
} |
| 2992 |
} |
| 2993 |
return newEdits; |
| 2994 |
}; |
| 2995 |
async function loadPostTypeEntities() { |
| 2996 |
const postTypesPromise = (0, import_api_fetch.default)({ path: "/wp/v2/types?context=view" }); |
| 2997 |
const taxonomiesPromise = window._wpCollaborationEnabled ? (0, import_api_fetch.default)({ path: "/wp/v2/taxonomies?context=view" }) : Promise.resolve({}); |
| 2998 |
const [postTypes, taxonomies] = await Promise.all([ |
| 2999 |
postTypesPromise, |
| 3000 |
taxonomiesPromise |
| 3001 |
]); |
| 3002 |
return Object.entries(postTypes ?? {}).map(([name, postType]) => { |
| 3003 |
const isTemplate = ["wp_template", "wp_template_part"].includes( |
| 3004 |
name |
| 3005 |
); |
| 3006 |
const namespace = postType?.rest_namespace ?? "wp/v2"; |
| 3007 |
const syncedProperties = /* @__PURE__ */ new Set([ |
| 3008 |
"author", |
| 3009 |
"blocks", |
| 3010 |
"content", |
| 3011 |
"comment_status", |
| 3012 |
"date", |
| 3013 |
"excerpt", |
| 3014 |
"featured_media", |
| 3015 |
"format", |
| 3016 |
"meta", |
| 3017 |
"ping_status", |
| 3018 |
"slug", |
| 3019 |
"status", |
| 3020 |
"sticky", |
| 3021 |
"template", |
| 3022 |
"title", |
| 3023 |
...postType.taxonomies?.map((taxonomy) => taxonomies?.[taxonomy]?.rest_base)?.filter(Boolean) ?? [] |
| 3024 |
]); |
| 3025 |
const entity2 = { |
| 3026 |
kind: "postType", |
| 3027 |
baseURL: `/${namespace}/${postType.rest_base}`, |
| 3028 |
baseURLParams: { context: "edit" }, |
| 3029 |
name, |
| 3030 |
label: postType.name, |
| 3031 |
transientEdits: { |
| 3032 |
...blocksTransientEdits, |
| 3033 |
selection: true |
| 3034 |
}, |
| 3035 |
mergedEdits: { meta: true }, |
| 3036 |
rawAttributes: POST_RAW_ATTRIBUTES, |
| 3037 |
getTitle: (record) => record?.title?.rendered || record?.title || (isTemplate ? capitalCase(record.slug ?? "") : String(record.id)), |
| 3038 |
__unstablePrePersist: (persistedRecord, edits) => prePersistPostType(persistedRecord, edits, name, isTemplate), |
| 3039 |
__unstable_rest_base: postType.rest_base, |
| 3040 |
supportsPagination: true, |
| 3041 |
getRevisionsUrl: (parentId, revisionId) => `/${namespace}/${postType.rest_base}/${parentId}/revisions${revisionId ? "/" + revisionId : ""}`, |
| 3042 |
revisionKey: isTemplate && !window?.__experimentalTemplateActivate ? "wp_id" : DEFAULT_ENTITY_KEY |
| 3043 |
}; |
| 3044 |
entity2.syncConfig = { |
| 3045 |
/** |
| 3046 |
* Apply changes from the local editor to the local CRDT document so |
| 3047 |
* that those changes can be synced to other peers (via the provider). |
| 3048 |
* |
| 3049 |
* @param {import('@wordpress/sync').CRDTDoc} crdtDoc |
| 3050 |
* @param {Partial< import('@wordpress/sync').ObjectData >} changes |
| 3051 |
* @return {void} |
| 3052 |
*/ |
| 3053 |
applyChangesToCRDTDoc: (crdtDoc, changes) => applyPostChangesToCRDTDoc(crdtDoc, changes, syncedProperties), |
| 3054 |
/** |
| 3055 |
* Create the awareness instance for the entity's CRDT document. |
| 3056 |
* |
| 3057 |
* @param {import('@wordpress/sync').CRDTDoc} ydoc |
| 3058 |
* @param {import('@wordpress/sync').ObjectID} objectId |
| 3059 |
* @return {import('@wordpress/sync').Awareness} Awareness instance |
| 3060 |
*/ |
| 3061 |
createAwareness: (ydoc, objectId) => { |
| 3062 |
const kind = "postType"; |
| 3063 |
const id = parseInt(objectId, 10); |
| 3064 |
return new PostEditorAwareness(ydoc, kind, name, id); |
| 3065 |
}, |
| 3066 |
/** |
| 3067 |
* Extract changes from a CRDT document that can be used to update the |
| 3068 |
* local editor state. |
| 3069 |
* |
| 3070 |
* @param {import('@wordpress/sync').CRDTDoc} crdtDoc |
| 3071 |
* @param {import('@wordpress/sync').ObjectData} editedRecord |
| 3072 |
* @return {Partial< import('@wordpress/sync').ObjectData >} Changes to record |
| 3073 |
*/ |
| 3074 |
getChangesFromCRDTDoc: (crdtDoc, editedRecord) => getPostChangesFromCRDTDoc( |
| 3075 |
crdtDoc, |
| 3076 |
editedRecord, |
| 3077 |
syncedProperties |
| 3078 |
), |
| 3079 |
/** |
| 3080 |
* Extract changes from a CRDT document that can be used to update the |
| 3081 |
* local editor state. |
| 3082 |
* |
| 3083 |
* @param {import('@wordpress/sync').ObjectData} record |
| 3084 |
* @return {Partial< import('@wordpress/sync').ObjectData >} Changes to record |
| 3085 |
*/ |
| 3086 |
getPersistedCRDTDoc: (record) => { |
| 3087 |
return record?.meta?.[POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE] || null; |
| 3088 |
} |
| 3089 |
}; |
| 3090 |
return entity2; |
| 3091 |
}); |
| 3092 |
} |
| 3093 |
async function loadTaxonomyEntities() { |
| 3094 |
const taxonomies = await (0, import_api_fetch.default)({ |
| 3095 |
path: "/wp/v2/taxonomies?context=view" |
| 3096 |
}); |
| 3097 |
return Object.entries(taxonomies ?? {}).map(([name, taxonomy]) => { |
| 3098 |
const namespace = taxonomy?.rest_namespace ?? "wp/v2"; |
| 3099 |
const entity2 = { |
| 3100 |
kind: "taxonomy", |
| 3101 |
baseURL: `/${namespace}/${taxonomy.rest_base}`, |
| 3102 |
baseURLParams: { context: "edit" }, |
| 3103 |
name, |
| 3104 |
label: taxonomy.name, |
| 3105 |
getTitle: (record) => record?.name, |
| 3106 |
supportsPagination: true |
| 3107 |
}; |
| 3108 |
entity2.syncConfig = defaultSyncConfig; |
| 3109 |
return entity2; |
| 3110 |
}); |
| 3111 |
} |
| 3112 |
async function loadSiteEntity() { |
| 3113 |
const entity2 = { |
| 3114 |
label: (0, import_i18n.__)("Site"), |
| 3115 |
name: "site", |
| 3116 |
kind: "root", |
| 3117 |
key: false, |
| 3118 |
baseURL: "/wp/v2/settings", |
| 3119 |
meta: {} |
| 3120 |
}; |
| 3121 |
const site = await (0, import_api_fetch.default)({ |
| 3122 |
path: entity2.baseURL, |
| 3123 |
method: "OPTIONS" |
| 3124 |
}); |
| 3125 |
const labels = {}; |
| 3126 |
Object.entries(site?.schema?.properties ?? {}).forEach( |
| 3127 |
([key, value]) => { |
| 3128 |
if (typeof value === "object" && value.title) { |
| 3129 |
labels[key] = value.title; |
| 3130 |
} |
| 3131 |
} |
| 3132 |
); |
| 3133 |
return [{ ...entity2, meta: { labels } }]; |
| 3134 |
} |
| 3135 |
var getMethodName = (kind, name, prefix = "get") => { |
| 3136 |
const kindPrefix = kind === "root" ? "" : pascalCase(kind); |
| 3137 |
const suffix = pascalCase(name); |
| 3138 |
return `${prefix}${kindPrefix}${suffix}`; |
| 3139 |
}; |
| 3140 |
|
| 3141 |
// packages/core-data/build-module/queried-data/reducer.mjs |
| 3142 |
function getContextFromAction(action) { |
| 3143 |
const { query } = action; |
| 3144 |
if (!query) { |
| 3145 |
return "default"; |
| 3146 |
} |
| 3147 |
const queryParts = get_query_parts_default(query); |
| 3148 |
return queryParts.context; |
| 3149 |
} |
| 3150 |
function getMergedItemIds(itemIds = [], nextItemIds, { page = 1, offset, perPage = 10 } = {}) { |
| 3151 |
if (perPage === -1) { |
| 3152 |
return nextItemIds; |
| 3153 |
} |
| 3154 |
const nextItemIdsStartIndex = offset ?? (page - 1) * perPage; |
| 3155 |
const size = Math.max( |
| 3156 |
itemIds.length, |
| 3157 |
nextItemIdsStartIndex + nextItemIds.length |
| 3158 |
); |
| 3159 |
const mergedItemIds = new Array(size); |
| 3160 |
for (let i = 0; i < size; i++) { |
| 3161 |
const isInNextItemsRange = i >= nextItemIdsStartIndex && i < nextItemIdsStartIndex + perPage; |
| 3162 |
if (isInNextItemsRange) { |
| 3163 |
mergedItemIds[i] = nextItemIds[i - nextItemIdsStartIndex]; |
| 3164 |
} else { |
| 3165 |
mergedItemIds[i] = itemIds[i]; |
| 3166 |
} |
| 3167 |
} |
| 3168 |
return mergedItemIds; |
| 3169 |
} |
| 3170 |
function removeEntitiesById(entities2, ids) { |
| 3171 |
return Object.fromEntries( |
| 3172 |
Object.entries(entities2).filter( |
| 3173 |
([id]) => !ids.some((itemId) => { |
| 3174 |
if (Number.isInteger(itemId)) { |
| 3175 |
return itemId === +id; |
| 3176 |
} |
| 3177 |
return itemId === id; |
| 3178 |
}) |
| 3179 |
) |
| 3180 |
); |
| 3181 |
} |
| 3182 |
function items(state = {}, action) { |
| 3183 |
switch (action.type) { |
| 3184 |
case "RECEIVE_ITEMS": { |
| 3185 |
const context = getContextFromAction(action); |
| 3186 |
const key = action.key || DEFAULT_ENTITY_KEY; |
| 3187 |
const itemsList = Array.isArray(action.items) ? action.items : [action.items]; |
| 3188 |
return { |
| 3189 |
...state, |
| 3190 |
[context]: { |
| 3191 |
...state[context], |
| 3192 |
...Object.fromEntries( |
| 3193 |
itemsList.map((item) => [ |
| 3194 |
item?.[key], |
| 3195 |
conservativeMapItem( |
| 3196 |
state?.[context]?.[item?.[key]], |
| 3197 |
item |
| 3198 |
) |
| 3199 |
]) |
| 3200 |
) |
| 3201 |
} |
| 3202 |
}; |
| 3203 |
} |
| 3204 |
case "REMOVE_ITEMS": |
| 3205 |
return Object.fromEntries( |
| 3206 |
Object.entries(state).map(([itemId, contextState]) => [ |
| 3207 |
itemId, |
| 3208 |
removeEntitiesById(contextState, action.itemIds) |
| 3209 |
]) |
| 3210 |
); |
| 3211 |
} |
| 3212 |
return state; |
| 3213 |
} |
| 3214 |
function itemIsComplete(state = {}, action) { |
| 3215 |
switch (action.type) { |
| 3216 |
case "RECEIVE_ITEMS": { |
| 3217 |
const context = getContextFromAction(action); |
| 3218 |
const { query, key = DEFAULT_ENTITY_KEY } = action; |
| 3219 |
const itemsList = Array.isArray(action.items) ? action.items : [action.items]; |
| 3220 |
const queryParts = query ? get_query_parts_default(query) : {}; |
| 3221 |
const isCompleteQuery = !query || !Array.isArray(queryParts.fields); |
| 3222 |
return { |
| 3223 |
...state, |
| 3224 |
[context]: { |
| 3225 |
...state[context], |
| 3226 |
...itemsList.reduce((result, item) => { |
| 3227 |
const itemId = item?.[key]; |
| 3228 |
result[itemId] = state?.[context]?.[itemId] || isCompleteQuery; |
| 3229 |
return result; |
| 3230 |
}, {}) |
| 3231 |
} |
| 3232 |
}; |
| 3233 |
} |
| 3234 |
case "REMOVE_ITEMS": |
| 3235 |
return Object.fromEntries( |
| 3236 |
Object.entries(state).map(([itemId, contextState]) => [ |
| 3237 |
itemId, |
| 3238 |
removeEntitiesById(contextState, action.itemIds) |
| 3239 |
]) |
| 3240 |
); |
| 3241 |
} |
| 3242 |
return state; |
| 3243 |
} |
| 3244 |
var receiveQueries = (0, import_compose.compose)([ |
| 3245 |
// Limit to matching action type so we don't attempt to replace action on |
| 3246 |
// an unhandled action. |
| 3247 |
if_matching_action_default((action) => "query" in action), |
| 3248 |
// Inject query parts into action for use both in `onSubKey` and reducer. |
| 3249 |
replace_action_default((action) => { |
| 3250 |
if (action.query) { |
| 3251 |
return { |
| 3252 |
...action, |
| 3253 |
...get_query_parts_default(action.query) |
| 3254 |
}; |
| 3255 |
} |
| 3256 |
return action; |
| 3257 |
}), |
| 3258 |
on_sub_key_default("context"), |
| 3259 |
// Queries shape is shared, but keyed by query `stableKey` part. Original |
| 3260 |
// reducer tracks only a single query object. |
| 3261 |
on_sub_key_default("stableKey") |
| 3262 |
])((state = {}, action) => { |
| 3263 |
if (action.type !== "RECEIVE_ITEMS") { |
| 3264 |
return state; |
| 3265 |
} |
| 3266 |
if (!Array.isArray(action.items)) { |
| 3267 |
return state; |
| 3268 |
} |
| 3269 |
const key = action.key ?? DEFAULT_ENTITY_KEY; |
| 3270 |
return { |
| 3271 |
itemIds: getMergedItemIds( |
| 3272 |
state.itemIds, |
| 3273 |
action.items.map((item) => item?.[key]).filter(Boolean), |
| 3274 |
{ |
| 3275 |
page: action.page, |
| 3276 |
offset: action.offset, |
| 3277 |
perPage: action.perPage |
| 3278 |
} |
| 3279 |
), |
| 3280 |
meta: action.meta |
| 3281 |
}; |
| 3282 |
}); |
| 3283 |
var queries = (state = {}, action) => { |
| 3284 |
switch (action.type) { |
| 3285 |
case "RECEIVE_ITEMS": |
| 3286 |
return receiveQueries(state, action); |
| 3287 |
case "REMOVE_ITEMS": |
| 3288 |
const removedItems = action.itemIds.reduce((result, itemId) => { |
| 3289 |
result[itemId] = true; |
| 3290 |
return result; |
| 3291 |
}, {}); |
| 3292 |
return Object.fromEntries( |
| 3293 |
Object.entries(state).map( |
| 3294 |
([queryGroup, contextQueries]) => [ |
| 3295 |
queryGroup, |
| 3296 |
Object.fromEntries( |
| 3297 |
Object.entries(contextQueries).map( |
| 3298 |
([query, queryItems]) => [ |
| 3299 |
query, |
| 3300 |
{ |
| 3301 |
...queryItems, |
| 3302 |
itemIds: queryItems.itemIds.filter( |
| 3303 |
(queryId) => !removedItems[queryId] |
| 3304 |
) |
| 3305 |
} |
| 3306 |
] |
| 3307 |
) |
| 3308 |
) |
| 3309 |
] |
| 3310 |
) |
| 3311 |
); |
| 3312 |
default: |
| 3313 |
return state; |
| 3314 |
} |
| 3315 |
}; |
| 3316 |
var reducer_default = (0, import_data7.combineReducers)({ |
| 3317 |
items, |
| 3318 |
itemIsComplete, |
| 3319 |
queries |
| 3320 |
}); |
| 3321 |
|
| 3322 |
// packages/core-data/build-module/reducer.mjs |
| 3323 |
function users(state = { byId: {}, queries: {} }, action) { |
| 3324 |
switch (action.type) { |
| 3325 |
case "RECEIVE_USER_QUERY": |
| 3326 |
return { |
| 3327 |
byId: { |
| 3328 |
...state.byId, |
| 3329 |
// Key users by their ID. |
| 3330 |
...action.users.reduce( |
| 3331 |
(newUsers, user) => ({ |
| 3332 |
...newUsers, |
| 3333 |
[user.id]: user |
| 3334 |
}), |
| 3335 |
{} |
| 3336 |
) |
| 3337 |
}, |
| 3338 |
queries: { |
| 3339 |
...state.queries, |
| 3340 |
[action.queryID]: action.users.map((user) => user.id) |
| 3341 |
} |
| 3342 |
}; |
| 3343 |
} |
| 3344 |
return state; |
| 3345 |
} |
| 3346 |
function currentUser(state = {}, action) { |
| 3347 |
switch (action.type) { |
| 3348 |
case "RECEIVE_CURRENT_USER": |
| 3349 |
return action.currentUser; |
| 3350 |
} |
| 3351 |
return state; |
| 3352 |
} |
| 3353 |
function currentTheme(state = void 0, action) { |
| 3354 |
switch (action.type) { |
| 3355 |
case "RECEIVE_CURRENT_THEME": |
| 3356 |
return action.currentTheme.stylesheet; |
| 3357 |
} |
| 3358 |
return state; |
| 3359 |
} |
| 3360 |
function currentGlobalStylesId(state = void 0, action) { |
| 3361 |
switch (action.type) { |
| 3362 |
case "RECEIVE_CURRENT_GLOBAL_STYLES_ID": |
| 3363 |
return action.id; |
| 3364 |
} |
| 3365 |
return state; |
| 3366 |
} |
| 3367 |
function themeBaseGlobalStyles(state = {}, action) { |
| 3368 |
switch (action.type) { |
| 3369 |
case "RECEIVE_THEME_GLOBAL_STYLES": |
| 3370 |
return { |
| 3371 |
...state, |
| 3372 |
[action.stylesheet]: action.globalStyles |
| 3373 |
}; |
| 3374 |
} |
| 3375 |
return state; |
| 3376 |
} |
| 3377 |
function themeGlobalStyleVariations(state = {}, action) { |
| 3378 |
switch (action.type) { |
| 3379 |
case "RECEIVE_THEME_GLOBAL_STYLE_VARIATIONS": |
| 3380 |
return { |
| 3381 |
...state, |
| 3382 |
[action.stylesheet]: action.variations |
| 3383 |
}; |
| 3384 |
} |
| 3385 |
return state; |
| 3386 |
} |
| 3387 |
var withMultiEntityRecordEdits = (reducer) => (state, action) => { |
| 3388 |
if (action.type === "UNDO" || action.type === "REDO") { |
| 3389 |
const { record } = action; |
| 3390 |
let newState = state; |
| 3391 |
record.forEach(({ id: { kind, name, recordId }, changes }) => { |
| 3392 |
newState = reducer(newState, { |
| 3393 |
type: "EDIT_ENTITY_RECORD", |
| 3394 |
kind, |
| 3395 |
name, |
| 3396 |
recordId, |
| 3397 |
edits: Object.entries(changes).reduce( |
| 3398 |
(acc, [key, value]) => { |
| 3399 |
acc[key] = action.type === "UNDO" ? value.from : value.to; |
| 3400 |
return acc; |
| 3401 |
}, |
| 3402 |
{} |
| 3403 |
) |
| 3404 |
}); |
| 3405 |
}); |
| 3406 |
return newState; |
| 3407 |
} |
| 3408 |
return reducer(state, action); |
| 3409 |
}; |
| 3410 |
function entity(entityConfig) { |
| 3411 |
return (0, import_compose2.compose)([ |
| 3412 |
withMultiEntityRecordEdits, |
| 3413 |
// Limit to matching action type so we don't attempt to replace action on |
| 3414 |
// an unhandled action. |
| 3415 |
if_matching_action_default( |
| 3416 |
(action) => action.name && action.kind && action.name === entityConfig.name && action.kind === entityConfig.kind |
| 3417 |
), |
| 3418 |
// Inject the entity config into the action. |
| 3419 |
replace_action_default((action) => { |
| 3420 |
return { |
| 3421 |
key: entityConfig.key || DEFAULT_ENTITY_KEY, |
| 3422 |
...action |
| 3423 |
}; |
| 3424 |
}) |
| 3425 |
])( |
| 3426 |
(0, import_data8.combineReducers)({ |
| 3427 |
queriedData: reducer_default, |
| 3428 |
edits: (state = {}, action) => { |
| 3429 |
switch (action.type) { |
| 3430 |
case "RECEIVE_ITEMS": |
| 3431 |
const context = action?.query?.context ?? "default"; |
| 3432 |
if (context !== "default") { |
| 3433 |
return state; |
| 3434 |
} |
| 3435 |
const nextState = { ...state }; |
| 3436 |
const itemsList = Array.isArray(action.items) ? action.items : [action.items]; |
| 3437 |
for (const record of itemsList) { |
| 3438 |
const recordId = record?.[action.key]; |
| 3439 |
const edits = nextState[recordId]; |
| 3440 |
if (!edits) { |
| 3441 |
continue; |
| 3442 |
} |
| 3443 |
const nextEdits2 = Object.keys(edits).reduce( |
| 3444 |
(acc, key) => { |
| 3445 |
if ( |
| 3446 |
// Edits are the "raw" attribute values, but records may have |
| 3447 |
// objects with more properties, so we use `get` here for the |
| 3448 |
// comparison. |
| 3449 |
!(0, import_es64.default)( |
| 3450 |
edits[key], |
| 3451 |
record[key]?.raw ?? record[key] |
| 3452 |
) && // Sometimes the server alters the sent value which means |
| 3453 |
// we need to also remove the edits before the api request. |
| 3454 |
(!action.persistedEdits || !(0, import_es64.default)( |
| 3455 |
edits[key], |
| 3456 |
action.persistedEdits[key] |
| 3457 |
)) |
| 3458 |
) { |
| 3459 |
acc[key] = edits[key]; |
| 3460 |
} |
| 3461 |
return acc; |
| 3462 |
}, |
| 3463 |
{} |
| 3464 |
); |
| 3465 |
if (Object.keys(nextEdits2).length) { |
| 3466 |
nextState[recordId] = nextEdits2; |
| 3467 |
} else { |
| 3468 |
delete nextState[recordId]; |
| 3469 |
} |
| 3470 |
} |
| 3471 |
return nextState; |
| 3472 |
case "EDIT_ENTITY_RECORD": |
| 3473 |
const nextEdits = { |
| 3474 |
...state[action.recordId], |
| 3475 |
...action.edits |
| 3476 |
}; |
| 3477 |
Object.keys(nextEdits).forEach((key) => { |
| 3478 |
if (nextEdits[key] === void 0) { |
| 3479 |
delete nextEdits[key]; |
| 3480 |
} |
| 3481 |
}); |
| 3482 |
return { |
| 3483 |
...state, |
| 3484 |
[action.recordId]: nextEdits |
| 3485 |
}; |
| 3486 |
} |
| 3487 |
return state; |
| 3488 |
}, |
| 3489 |
saving: (state = {}, action) => { |
| 3490 |
switch (action.type) { |
| 3491 |
case "SAVE_ENTITY_RECORD_START": |
| 3492 |
case "SAVE_ENTITY_RECORD_FINISH": |
| 3493 |
return { |
| 3494 |
...state, |
| 3495 |
[action.recordId]: { |
| 3496 |
pending: action.type === "SAVE_ENTITY_RECORD_START", |
| 3497 |
error: action.error, |
| 3498 |
isAutosave: action.isAutosave |
| 3499 |
} |
| 3500 |
}; |
| 3501 |
} |
| 3502 |
return state; |
| 3503 |
}, |
| 3504 |
deleting: (state = {}, action) => { |
| 3505 |
switch (action.type) { |
| 3506 |
case "DELETE_ENTITY_RECORD_START": |
| 3507 |
case "DELETE_ENTITY_RECORD_FINISH": |
| 3508 |
return { |
| 3509 |
...state, |
| 3510 |
[action.recordId]: { |
| 3511 |
pending: action.type === "DELETE_ENTITY_RECORD_START", |
| 3512 |
error: action.error |
| 3513 |
} |
| 3514 |
}; |
| 3515 |
} |
| 3516 |
return state; |
| 3517 |
}, |
| 3518 |
revisions: (state = {}, action) => { |
| 3519 |
if (action.type === "RECEIVE_ITEM_REVISIONS") { |
| 3520 |
const recordKey = action.recordKey; |
| 3521 |
delete action.recordKey; |
| 3522 |
const newState = reducer_default(state[recordKey], { |
| 3523 |
...action, |
| 3524 |
type: "RECEIVE_ITEMS" |
| 3525 |
}); |
| 3526 |
return { |
| 3527 |
...state, |
| 3528 |
[recordKey]: newState |
| 3529 |
}; |
| 3530 |
} |
| 3531 |
if (action.type === "REMOVE_ITEMS") { |
| 3532 |
return Object.fromEntries( |
| 3533 |
Object.entries(state).filter( |
| 3534 |
([id]) => !action.itemIds.some((itemId) => { |
| 3535 |
if (Number.isInteger(itemId)) { |
| 3536 |
return itemId === +id; |
| 3537 |
} |
| 3538 |
return itemId === id; |
| 3539 |
}) |
| 3540 |
) |
| 3541 |
); |
| 3542 |
} |
| 3543 |
return state; |
| 3544 |
} |
| 3545 |
}) |
| 3546 |
); |
| 3547 |
} |
| 3548 |
function entitiesConfig(state = rootEntitiesConfig, action) { |
| 3549 |
switch (action.type) { |
| 3550 |
case "ADD_ENTITIES": |
| 3551 |
return [...state, ...action.entities]; |
| 3552 |
} |
| 3553 |
return state; |
| 3554 |
} |
| 3555 |
var entities = (state = {}, action) => { |
| 3556 |
const newConfig = entitiesConfig(state.config, action); |
| 3557 |
let entitiesDataReducer = state.reducer; |
| 3558 |
if (!entitiesDataReducer || newConfig !== state.config) { |
| 3559 |
const entitiesByKind = newConfig.reduce((acc, record) => { |
| 3560 |
const { kind } = record; |
| 3561 |
if (!acc[kind]) { |
| 3562 |
acc[kind] = []; |
| 3563 |
} |
| 3564 |
acc[kind].push(record); |
| 3565 |
return acc; |
| 3566 |
}, {}); |
| 3567 |
entitiesDataReducer = (0, import_data8.combineReducers)( |
| 3568 |
Object.fromEntries( |
| 3569 |
Object.entries(entitiesByKind).map( |
| 3570 |
([kind, subEntities]) => { |
| 3571 |
const kindReducer = (0, import_data8.combineReducers)( |
| 3572 |
Object.fromEntries( |
| 3573 |
subEntities.map((entityConfig) => [ |
| 3574 |
entityConfig.name, |
| 3575 |
entity(entityConfig) |
| 3576 |
]) |
| 3577 |
) |
| 3578 |
); |
| 3579 |
return [kind, kindReducer]; |
| 3580 |
} |
| 3581 |
) |
| 3582 |
) |
| 3583 |
); |
| 3584 |
} |
| 3585 |
const newData = entitiesDataReducer(state.records, action); |
| 3586 |
if (newData === state.records && newConfig === state.config && entitiesDataReducer === state.reducer) { |
| 3587 |
return state; |
| 3588 |
} |
| 3589 |
return { |
| 3590 |
reducer: entitiesDataReducer, |
| 3591 |
records: newData, |
| 3592 |
config: newConfig |
| 3593 |
}; |
| 3594 |
}; |
| 3595 |
function undoManager(state = (0, import_undo_manager.createUndoManager)()) { |
| 3596 |
return state; |
| 3597 |
} |
| 3598 |
function editsReference(state = {}, action) { |
| 3599 |
switch (action.type) { |
| 3600 |
case "EDIT_ENTITY_RECORD": |
| 3601 |
case "UNDO": |
| 3602 |
case "REDO": |
| 3603 |
return {}; |
| 3604 |
} |
| 3605 |
return state; |
| 3606 |
} |
| 3607 |
function embedPreviews(state = {}, action) { |
| 3608 |
switch (action.type) { |
| 3609 |
case "RECEIVE_EMBED_PREVIEW": |
| 3610 |
const { url, preview } = action; |
| 3611 |
return { |
| 3612 |
...state, |
| 3613 |
[url]: preview |
| 3614 |
}; |
| 3615 |
} |
| 3616 |
return state; |
| 3617 |
} |
| 3618 |
function userPermissions(state = {}, action) { |
| 3619 |
switch (action.type) { |
| 3620 |
case "RECEIVE_USER_PERMISSION": |
| 3621 |
return { |
| 3622 |
...state, |
| 3623 |
[action.key]: action.isAllowed |
| 3624 |
}; |
| 3625 |
case "RECEIVE_USER_PERMISSIONS": |
| 3626 |
return { |
| 3627 |
...state, |
| 3628 |
...action.permissions |
| 3629 |
}; |
| 3630 |
} |
| 3631 |
return state; |
| 3632 |
} |
| 3633 |
function autosaves(state = {}, action) { |
| 3634 |
switch (action.type) { |
| 3635 |
case "RECEIVE_AUTOSAVES": |
| 3636 |
const { postId, autosaves: autosavesData } = action; |
| 3637 |
return { |
| 3638 |
...state, |
| 3639 |
[postId]: autosavesData |
| 3640 |
}; |
| 3641 |
} |
| 3642 |
return state; |
| 3643 |
} |
| 3644 |
function blockPatterns(state = [], action) { |
| 3645 |
switch (action.type) { |
| 3646 |
case "RECEIVE_BLOCK_PATTERNS": |
| 3647 |
return action.patterns; |
| 3648 |
} |
| 3649 |
return state; |
| 3650 |
} |
| 3651 |
function blockPatternCategories(state = [], action) { |
| 3652 |
switch (action.type) { |
| 3653 |
case "RECEIVE_BLOCK_PATTERN_CATEGORIES": |
| 3654 |
return action.categories; |
| 3655 |
} |
| 3656 |
return state; |
| 3657 |
} |
| 3658 |
function userPatternCategories(state = [], action) { |
| 3659 |
switch (action.type) { |
| 3660 |
case "RECEIVE_USER_PATTERN_CATEGORIES": |
| 3661 |
return action.patternCategories; |
| 3662 |
} |
| 3663 |
return state; |
| 3664 |
} |
| 3665 |
function navigationFallbackId(state = null, action) { |
| 3666 |
switch (action.type) { |
| 3667 |
case "RECEIVE_NAVIGATION_FALLBACK_ID": |
| 3668 |
return action.fallbackId; |
| 3669 |
} |
| 3670 |
return state; |
| 3671 |
} |
| 3672 |
function themeGlobalStyleRevisions(state = {}, action) { |
| 3673 |
switch (action.type) { |
| 3674 |
case "RECEIVE_THEME_GLOBAL_STYLE_REVISIONS": |
| 3675 |
return { |
| 3676 |
...state, |
| 3677 |
[action.currentId]: action.revisions |
| 3678 |
}; |
| 3679 |
} |
| 3680 |
return state; |
| 3681 |
} |
| 3682 |
function defaultTemplates(state = {}, action) { |
| 3683 |
switch (action.type) { |
| 3684 |
case "RECEIVE_DEFAULT_TEMPLATE": |
| 3685 |
return { |
| 3686 |
...state, |
| 3687 |
[JSON.stringify(action.query)]: action.templateId |
| 3688 |
}; |
| 3689 |
} |
| 3690 |
return state; |
| 3691 |
} |
| 3692 |
function registeredPostMeta(state = {}, action) { |
| 3693 |
switch (action.type) { |
| 3694 |
case "RECEIVE_REGISTERED_POST_META": |
| 3695 |
return { |
| 3696 |
...state, |
| 3697 |
[action.postType]: action.registeredPostMeta |
| 3698 |
}; |
| 3699 |
} |
| 3700 |
return state; |
| 3701 |
} |
| 3702 |
function editorSettings(state = null, action) { |
| 3703 |
switch (action.type) { |
| 3704 |
case "RECEIVE_EDITOR_SETTINGS": |
| 3705 |
return action.settings; |
| 3706 |
} |
| 3707 |
return state; |
| 3708 |
} |
| 3709 |
function editorAssets(state = null, action) { |
| 3710 |
switch (action.type) { |
| 3711 |
case "RECEIVE_EDITOR_ASSETS": |
| 3712 |
return action.assets; |
| 3713 |
} |
| 3714 |
return state; |
| 3715 |
} |
| 3716 |
function syncConnectionStatuses(state = {}, action) { |
| 3717 |
switch (action.type) { |
| 3718 |
case "SET_SYNC_CONNECTION_STATUS": { |
| 3719 |
const key = `${action.kind}/${action.name}:${action.key}`; |
| 3720 |
return { |
| 3721 |
...state, |
| 3722 |
[key]: action.status |
| 3723 |
}; |
| 3724 |
} |
| 3725 |
case "CLEAR_SYNC_CONNECTION_STATUS": { |
| 3726 |
const key = `${action.kind}/${action.name}:${action.key}`; |
| 3727 |
const { [key]: _, ...rest } = state; |
| 3728 |
return rest; |
| 3729 |
} |
| 3730 |
} |
| 3731 |
return state; |
| 3732 |
} |
| 3733 |
function collaborationSupported(state = true, action) { |
| 3734 |
switch (action.type) { |
| 3735 |
case "SET_COLLABORATION_SUPPORTED": |
| 3736 |
return action.supported; |
| 3737 |
case "SET_SYNC_CONNECTION_STATUS": |
| 3738 |
if (ConnectionErrorCode.DOCUMENT_SIZE_LIMIT_EXCEEDED === action.status?.error?.code) { |
| 3739 |
return false; |
| 3740 |
} |
| 3741 |
return state; |
| 3742 |
} |
| 3743 |
return state; |
| 3744 |
} |
| 3745 |
function viewConfigs(state = {}, action) { |
| 3746 |
switch (action.type) { |
| 3747 |
case "RECEIVE_VIEW_CONFIG": |
| 3748 |
return { |
| 3749 |
...state, |
| 3750 |
[`${action.kind}/${action.name}`]: action.config |
| 3751 |
}; |
| 3752 |
} |
| 3753 |
return state; |
| 3754 |
} |
| 3755 |
var reducer_default2 = (0, import_data8.combineReducers)({ |
| 3756 |
users, |
| 3757 |
currentTheme, |
| 3758 |
currentGlobalStylesId, |
| 3759 |
currentUser, |
| 3760 |
themeGlobalStyleVariations, |
| 3761 |
themeBaseGlobalStyles, |
| 3762 |
themeGlobalStyleRevisions, |
| 3763 |
entities, |
| 3764 |
editsReference, |
| 3765 |
undoManager, |
| 3766 |
embedPreviews, |
| 3767 |
userPermissions, |
| 3768 |
autosaves, |
| 3769 |
blockPatterns, |
| 3770 |
blockPatternCategories, |
| 3771 |
userPatternCategories, |
| 3772 |
navigationFallbackId, |
| 3773 |
defaultTemplates, |
| 3774 |
registeredPostMeta, |
| 3775 |
editorSettings, |
| 3776 |
editorAssets, |
| 3777 |
syncConnectionStatuses, |
| 3778 |
collaborationSupported, |
| 3779 |
viewConfigs |
| 3780 |
}); |
| 3781 |
|
| 3782 |
// packages/core-data/build-module/selectors.mjs |
| 3783 |
var selectors_exports = {}; |
| 3784 |
__export(selectors_exports, { |
| 3785 |
__experimentalGetCurrentGlobalStylesId: () => __experimentalGetCurrentGlobalStylesId, |
| 3786 |
__experimentalGetCurrentThemeBaseGlobalStyles: () => __experimentalGetCurrentThemeBaseGlobalStyles, |
| 3787 |
__experimentalGetCurrentThemeGlobalStylesVariations: () => __experimentalGetCurrentThemeGlobalStylesVariations, |
| 3788 |
__experimentalGetDirtyEntityRecords: () => __experimentalGetDirtyEntityRecords, |
| 3789 |
__experimentalGetEntitiesBeingSaved: () => __experimentalGetEntitiesBeingSaved, |
| 3790 |
__experimentalGetEntityRecordNoResolver: () => __experimentalGetEntityRecordNoResolver, |
| 3791 |
canUser: () => canUser, |
| 3792 |
canUserEditEntityRecord: () => canUserEditEntityRecord, |
| 3793 |
getAuthors: () => getAuthors, |
| 3794 |
getAutosave: () => getAutosave, |
| 3795 |
getAutosaves: () => getAutosaves, |
| 3796 |
getBlockPatternCategories: () => getBlockPatternCategories, |
| 3797 |
getBlockPatterns: () => getBlockPatterns, |
| 3798 |
getCurrentTheme: () => getCurrentTheme, |
| 3799 |
getCurrentThemeGlobalStylesRevisions: () => getCurrentThemeGlobalStylesRevisions, |
| 3800 |
getCurrentUser: () => getCurrentUser, |
| 3801 |
getDefaultTemplateId: () => getDefaultTemplateId, |
| 3802 |
getEditedEntityRecord: () => getEditedEntityRecord, |
| 3803 |
getEmbedPreview: () => getEmbedPreview, |
| 3804 |
getEntitiesByKind: () => getEntitiesByKind, |
| 3805 |
getEntitiesConfig: () => getEntitiesConfig, |
| 3806 |
getEntity: () => getEntity, |
| 3807 |
getEntityConfig: () => getEntityConfig, |
| 3808 |
getEntityRecord: () => getEntityRecord, |
| 3809 |
getEntityRecordEdits: () => getEntityRecordEdits, |
| 3810 |
getEntityRecordNonTransientEdits: () => getEntityRecordNonTransientEdits, |
| 3811 |
getEntityRecords: () => getEntityRecords, |
| 3812 |
getEntityRecordsTotalItems: () => getEntityRecordsTotalItems, |
| 3813 |
getEntityRecordsTotalPages: () => getEntityRecordsTotalPages, |
| 3814 |
getLastEntityDeleteError: () => getLastEntityDeleteError, |
| 3815 |
getLastEntitySaveError: () => getLastEntitySaveError, |
| 3816 |
getRawEntityRecord: () => getRawEntityRecord, |
| 3817 |
getRedoEdit: () => getRedoEdit, |
| 3818 |
getReferenceByDistinctEdits: () => getReferenceByDistinctEdits, |
| 3819 |
getRevision: () => getRevision, |
| 3820 |
getRevisions: () => getRevisions, |
| 3821 |
getSyncConnectionStatus: () => getSyncConnectionStatus, |
| 3822 |
getThemeSupports: () => getThemeSupports, |
| 3823 |
getUndoEdit: () => getUndoEdit, |
| 3824 |
getUserPatternCategories: () => getUserPatternCategories, |
| 3825 |
getUserQueryResults: () => getUserQueryResults, |
| 3826 |
hasEditsForEntityRecord: () => hasEditsForEntityRecord, |
| 3827 |
hasEntityRecord: () => hasEntityRecord, |
| 3828 |
hasEntityRecords: () => hasEntityRecords, |
| 3829 |
hasFetchedAutosaves: () => hasFetchedAutosaves, |
| 3830 |
hasRedo: () => hasRedo, |
| 3831 |
hasRevision: () => hasRevision, |
| 3832 |
hasUndo: () => hasUndo, |
| 3833 |
isAutosavingEntityRecord: () => isAutosavingEntityRecord, |
| 3834 |
isDeletingEntityRecord: () => isDeletingEntityRecord, |
| 3835 |
isPreviewEmbedFallback: () => isPreviewEmbedFallback, |
| 3836 |
isRequestingEmbedPreview: () => isRequestingEmbedPreview, |
| 3837 |
isSavingEntityRecord: () => isSavingEntityRecord |
| 3838 |
}); |
| 3839 |
var import_data10 = __toESM(require_data(), 1); |
| 3840 |
var import_url2 = __toESM(require_url(), 1); |
| 3841 |
var import_deprecated2 = __toESM(require_deprecated(), 1); |
| 3842 |
|
| 3843 |
// packages/core-data/build-module/private-selectors.mjs |
| 3844 |
var private_selectors_exports = {}; |
| 3845 |
__export(private_selectors_exports, { |
| 3846 |
getBlockPatternsForPostType: () => getBlockPatternsForPostType, |
| 3847 |
getEditorAssets: () => getEditorAssets, |
| 3848 |
getEditorSettings: () => getEditorSettings, |
| 3849 |
getEntityRecordPermissions: () => getEntityRecordPermissions, |
| 3850 |
getEntityRecordsPermissions: () => getEntityRecordsPermissions, |
| 3851 |
getHomePage: () => getHomePage, |
| 3852 |
getNavigationFallbackId: () => getNavigationFallbackId, |
| 3853 |
getPostsPageId: () => getPostsPageId, |
| 3854 |
getRegisteredPostMeta: () => getRegisteredPostMeta, |
| 3855 |
getTemplateId: () => getTemplateId, |
| 3856 |
getUndoManager: () => getUndoManager, |
| 3857 |
getViewConfig: () => getViewConfig, |
| 3858 |
isCollaborationSupported: () => isCollaborationSupported |
| 3859 |
}); |
| 3860 |
var import_data9 = __toESM(require_data(), 1); |
| 3861 |
|
| 3862 |
// packages/core-data/build-module/utils/log-entity-deprecation.mjs |
| 3863 |
var import_deprecated = __toESM(require_deprecated(), 1); |
| 3864 |
var loggedAlready = false; |
| 3865 |
function logEntityDeprecation(kind, name, functionName, { |
| 3866 |
alternativeFunctionName, |
| 3867 |
isShorthandSelector = false |
| 3868 |
} = {}) { |
| 3869 |
const deprecation = deprecatedEntities[kind]?.[name]; |
| 3870 |
if (!deprecation) { |
| 3871 |
return; |
| 3872 |
} |
| 3873 |
if (!loggedAlready) { |
| 3874 |
const { alternative } = deprecation; |
| 3875 |
const message = isShorthandSelector ? `'${functionName}'` : `The '${kind}', '${name}' entity (used via '${functionName}')`; |
| 3876 |
let alternativeMessage = `the '${alternative.kind}', '${alternative.name}' entity`; |
| 3877 |
if (alternativeFunctionName) { |
| 3878 |
alternativeMessage += ` via the '${alternativeFunctionName}' function`; |
| 3879 |
} |
| 3880 |
(0, import_deprecated.default)(message, { |
| 3881 |
...deprecation, |
| 3882 |
alternative: alternativeMessage |
| 3883 |
}); |
| 3884 |
} |
| 3885 |
loggedAlready = true; |
| 3886 |
setTimeout(() => { |
| 3887 |
loggedAlready = false; |
| 3888 |
}, 0); |
| 3889 |
} |
| 3890 |
|
| 3891 |
// packages/core-data/build-module/private-selectors.mjs |
| 3892 |
var EMPTY_OBJECT = {}; |
| 3893 |
function getUndoManager(state) { |
| 3894 |
return getSyncManager()?.undoManager ?? state.undoManager; |
| 3895 |
} |
| 3896 |
function getNavigationFallbackId(state) { |
| 3897 |
return state.navigationFallbackId; |
| 3898 |
} |
| 3899 |
var getBlockPatternsForPostType = (0, import_data9.createRegistrySelector)( |
| 3900 |
(select5) => (0, import_data9.createSelector)( |
| 3901 |
(state, postType) => select5(STORE_NAME).getBlockPatterns().filter( |
| 3902 |
({ postTypes }) => !postTypes || Array.isArray(postTypes) && postTypes.includes(postType) |
| 3903 |
), |
| 3904 |
() => [select5(STORE_NAME).getBlockPatterns()] |
| 3905 |
) |
| 3906 |
); |
| 3907 |
var getEntityRecordsPermissions = (0, import_data9.createRegistrySelector)( |
| 3908 |
(select5) => (0, import_data9.createSelector)( |
| 3909 |
(state, kind, name, ids) => { |
| 3910 |
const normalizedIds = Array.isArray(ids) ? ids : [ids]; |
| 3911 |
return normalizedIds.map((id) => ({ |
| 3912 |
delete: select5(STORE_NAME).canUser("delete", { |
| 3913 |
kind, |
| 3914 |
name, |
| 3915 |
id |
| 3916 |
}), |
| 3917 |
update: select5(STORE_NAME).canUser("update", { |
| 3918 |
kind, |
| 3919 |
name, |
| 3920 |
id |
| 3921 |
}) |
| 3922 |
})); |
| 3923 |
}, |
| 3924 |
(state) => [state.userPermissions] |
| 3925 |
) |
| 3926 |
); |
| 3927 |
function getEntityRecordPermissions(state, kind, name, id) { |
| 3928 |
logEntityDeprecation(kind, name, "getEntityRecordPermissions"); |
| 3929 |
return getEntityRecordsPermissions(state, kind, name, id)[0]; |
| 3930 |
} |
| 3931 |
function getRegisteredPostMeta(state, postType) { |
| 3932 |
return state.registeredPostMeta?.[postType] ?? {}; |
| 3933 |
} |
| 3934 |
function normalizePageId(value) { |
| 3935 |
if (!value || !["number", "string"].includes(typeof value)) { |
| 3936 |
return null; |
| 3937 |
} |
| 3938 |
if (Number(value) === 0) { |
| 3939 |
return null; |
| 3940 |
} |
| 3941 |
return value.toString(); |
| 3942 |
} |
| 3943 |
var getHomePage = (0, import_data9.createRegistrySelector)( |
| 3944 |
(select5) => (0, import_data9.createSelector)( |
| 3945 |
() => { |
| 3946 |
const siteData = select5(STORE_NAME).getEntityRecord( |
| 3947 |
"root", |
| 3948 |
"__unstableBase" |
| 3949 |
); |
| 3950 |
if (!siteData) { |
| 3951 |
return null; |
| 3952 |
} |
| 3953 |
const homepageId = siteData?.show_on_front === "page" ? normalizePageId(siteData.page_on_front) : null; |
| 3954 |
if (homepageId) { |
| 3955 |
return { postType: "page", postId: homepageId }; |
| 3956 |
} |
| 3957 |
const frontPageTemplateId = select5( |
| 3958 |
STORE_NAME |
| 3959 |
).getDefaultTemplateId({ |
| 3960 |
slug: "front-page" |
| 3961 |
}); |
| 3962 |
if (frontPageTemplateId) { |
| 3963 |
return { |
| 3964 |
postType: "wp_template", |
| 3965 |
postId: frontPageTemplateId |
| 3966 |
}; |
| 3967 |
} |
| 3968 |
if (frontPageTemplateId === "") { |
| 3969 |
return EMPTY_OBJECT; |
| 3970 |
} |
| 3971 |
return null; |
| 3972 |
}, |
| 3973 |
(state) => [ |
| 3974 |
// Even though getDefaultTemplateId.shouldInvalidate returns true when root/site changes, |
| 3975 |
// it doesn't seem to invalidate this cache, I'm not sure why. |
| 3976 |
getEntityRecord(state, "root", "site"), |
| 3977 |
getEntityRecord(state, "root", "__unstableBase"), |
| 3978 |
getDefaultTemplateId(state, { |
| 3979 |
slug: "front-page" |
| 3980 |
}) |
| 3981 |
] |
| 3982 |
) |
| 3983 |
); |
| 3984 |
var getPostsPageId = (0, import_data9.createRegistrySelector)((select5) => () => { |
| 3985 |
const siteData = select5(STORE_NAME).getEntityRecord( |
| 3986 |
"root", |
| 3987 |
"__unstableBase" |
| 3988 |
); |
| 3989 |
return siteData?.show_on_front === "page" ? normalizePageId(siteData.page_for_posts) : null; |
| 3990 |
}); |
| 3991 |
var getTemplateId = (0, import_data9.createRegistrySelector)( |
| 3992 |
(select5) => (state, postType, postId) => { |
| 3993 |
const homepage = unlock(select5(STORE_NAME)).getHomePage(); |
| 3994 |
if (!homepage) { |
| 3995 |
return; |
| 3996 |
} |
| 3997 |
if (postType === "page" && postType === homepage?.postType && postId.toString() === homepage?.postId) { |
| 3998 |
const templates = select5(STORE_NAME).getEntityRecords( |
| 3999 |
"postType", |
| 4000 |
"wp_template", |
| 4001 |
{ |
| 4002 |
per_page: -1 |
| 4003 |
} |
| 4004 |
); |
| 4005 |
if (!templates) { |
| 4006 |
return; |
| 4007 |
} |
| 4008 |
const id = templates.find(({ slug }) => slug === "front-page")?.id; |
| 4009 |
if (id) { |
| 4010 |
return id; |
| 4011 |
} |
| 4012 |
} |
| 4013 |
const editedEntity = select5(STORE_NAME).getEditedEntityRecord( |
| 4014 |
"postType", |
| 4015 |
postType, |
| 4016 |
postId |
| 4017 |
); |
| 4018 |
if (!editedEntity) { |
| 4019 |
return; |
| 4020 |
} |
| 4021 |
const postsPageId = unlock(select5(STORE_NAME)).getPostsPageId(); |
| 4022 |
if (postType === "page" && postsPageId === postId.toString()) { |
| 4023 |
return select5(STORE_NAME).getDefaultTemplateId({ |
| 4024 |
slug: "home" |
| 4025 |
}); |
| 4026 |
} |
| 4027 |
const currentTemplateSlug = editedEntity.template; |
| 4028 |
if (currentTemplateSlug) { |
| 4029 |
const currentTemplate = select5(STORE_NAME).getEntityRecords("postType", "wp_template", { |
| 4030 |
per_page: -1 |
| 4031 |
})?.find(({ slug }) => slug === currentTemplateSlug); |
| 4032 |
if (currentTemplate) { |
| 4033 |
return currentTemplate.id; |
| 4034 |
} |
| 4035 |
} |
| 4036 |
let slugToCheck; |
| 4037 |
if (editedEntity.slug) { |
| 4038 |
slugToCheck = postType === "page" ? `${postType}-${editedEntity.slug}` : `single-${postType}-${editedEntity.slug}`; |
| 4039 |
} else { |
| 4040 |
slugToCheck = postType === "page" ? "page" : `single-${postType}`; |
| 4041 |
} |
| 4042 |
return select5(STORE_NAME).getDefaultTemplateId({ |
| 4043 |
slug: slugToCheck |
| 4044 |
}); |
| 4045 |
} |
| 4046 |
); |
| 4047 |
function getEditorSettings(state) { |
| 4048 |
return state.editorSettings; |
| 4049 |
} |
| 4050 |
function getEditorAssets(state) { |
| 4051 |
return state.editorAssets; |
| 4052 |
} |
| 4053 |
function isCollaborationSupported(state) { |
| 4054 |
return state.collaborationSupported; |
| 4055 |
} |
| 4056 |
function getViewConfig(state, kind, name) { |
| 4057 |
return state.viewConfigs?.[`${kind}/${name}`] ?? { |
| 4058 |
default_view: void 0, |
| 4059 |
default_layouts: void 0, |
| 4060 |
view_list: void 0, |
| 4061 |
form: void 0 |
| 4062 |
}; |
| 4063 |
} |
| 4064 |
|
| 4065 |
// packages/core-data/build-module/selectors.mjs |
| 4066 |
var EMPTY_OBJECT2 = {}; |
| 4067 |
var isRequestingEmbedPreview = (0, import_data10.createRegistrySelector)( |
| 4068 |
(select5) => (state, url) => { |
| 4069 |
return select5(STORE_NAME).isResolving("getEmbedPreview", [ |
| 4070 |
url |
| 4071 |
]); |
| 4072 |
} |
| 4073 |
); |
| 4074 |
function getAuthors(state, query) { |
| 4075 |
(0, import_deprecated2.default)("select( 'core' ).getAuthors()", { |
| 4076 |
since: "5.9", |
| 4077 |
alternative: "select( 'core' ).getUsers({ who: 'authors' })" |
| 4078 |
}); |
| 4079 |
const path = (0, import_url2.addQueryArgs)( |
| 4080 |
"/wp/v2/users/?who=authors&per_page=100", |
| 4081 |
query |
| 4082 |
); |
| 4083 |
return getUserQueryResults(state, path); |
| 4084 |
} |
| 4085 |
function getCurrentUser(state) { |
| 4086 |
return state.currentUser; |
| 4087 |
} |
| 4088 |
var getUserQueryResults = (0, import_data10.createSelector)( |
| 4089 |
(state, queryID) => { |
| 4090 |
const queryResults = state.users.queries[queryID] ?? []; |
| 4091 |
return queryResults.map((id) => state.users.byId[id]); |
| 4092 |
}, |
| 4093 |
(state, queryID) => [ |
| 4094 |
state.users.queries[queryID], |
| 4095 |
state.users.byId |
| 4096 |
] |
| 4097 |
); |
| 4098 |
function getEntitiesByKind(state, kind) { |
| 4099 |
(0, import_deprecated2.default)("wp.data.select( 'core' ).getEntitiesByKind()", { |
| 4100 |
since: "6.0", |
| 4101 |
alternative: "wp.data.select( 'core' ).getEntitiesConfig()" |
| 4102 |
}); |
| 4103 |
return getEntitiesConfig(state, kind); |
| 4104 |
} |
| 4105 |
var getEntitiesConfig = (0, import_data10.createSelector)( |
| 4106 |
(state, kind) => state.entities.config.filter((entity2) => entity2.kind === kind), |
| 4107 |
/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 4108 |
(state, kind) => state.entities.config |
| 4109 |
/* eslint-enable @typescript-eslint/no-unused-vars */ |
| 4110 |
); |
| 4111 |
function getEntity(state, kind, name) { |
| 4112 |
(0, import_deprecated2.default)("wp.data.select( 'core' ).getEntity()", { |
| 4113 |
since: "6.0", |
| 4114 |
alternative: "wp.data.select( 'core' ).getEntityConfig()" |
| 4115 |
}); |
| 4116 |
return getEntityConfig(state, kind, name); |
| 4117 |
} |
| 4118 |
function getEntityConfig(state, kind, name) { |
| 4119 |
logEntityDeprecation(kind, name, "getEntityConfig"); |
| 4120 |
return state.entities.config?.find( |
| 4121 |
(config) => config.kind === kind && config.name === name |
| 4122 |
); |
| 4123 |
} |
| 4124 |
var getEntityRecord = (0, import_data10.createSelector)( |
| 4125 |
((state, kind, name, key, query) => { |
| 4126 |
logEntityDeprecation(kind, name, "getEntityRecord"); |
| 4127 |
const queriedState = state.entities.records?.[kind]?.[name]?.queriedData; |
| 4128 |
if (!queriedState) { |
| 4129 |
return void 0; |
| 4130 |
} |
| 4131 |
const context = query?.context ?? "default"; |
| 4132 |
if (!query || !query._fields) { |
| 4133 |
if (!queriedState.itemIsComplete[context]?.[key]) { |
| 4134 |
return void 0; |
| 4135 |
} |
| 4136 |
return queriedState.items[context][key]; |
| 4137 |
} |
| 4138 |
const item = queriedState.items[context]?.[key]; |
| 4139 |
if (!item) { |
| 4140 |
return item; |
| 4141 |
} |
| 4142 |
const filteredItem = {}; |
| 4143 |
const fields = get_normalized_comma_separable_default(query._fields) ?? []; |
| 4144 |
for (let f = 0; f < fields.length; f++) { |
| 4145 |
const field = fields[f].split("."); |
| 4146 |
let value = item; |
| 4147 |
field.forEach((fieldName) => { |
| 4148 |
value = value?.[fieldName]; |
| 4149 |
}); |
| 4150 |
setNestedValue(filteredItem, field, value); |
| 4151 |
} |
| 4152 |
return filteredItem; |
| 4153 |
}), |
| 4154 |
(state, kind, name, recordId, query) => { |
| 4155 |
const context = query?.context ?? "default"; |
| 4156 |
const queriedState = state.entities.records?.[kind]?.[name]?.queriedData; |
| 4157 |
return [ |
| 4158 |
queriedState?.items[context]?.[recordId], |
| 4159 |
queriedState?.itemIsComplete[context]?.[recordId] |
| 4160 |
]; |
| 4161 |
} |
| 4162 |
); |
| 4163 |
getEntityRecord.__unstableNormalizeArgs = (args) => { |
| 4164 |
const newArgs = [...args]; |
| 4165 |
const recordKey = newArgs?.[2]; |
| 4166 |
newArgs[2] = isNumericID(recordKey) ? Number(recordKey) : recordKey; |
| 4167 |
return newArgs; |
| 4168 |
}; |
| 4169 |
function hasEntityRecord(state, kind, name, key, query) { |
| 4170 |
const queriedState = state.entities.records?.[kind]?.[name]?.queriedData; |
| 4171 |
if (!queriedState) { |
| 4172 |
return false; |
| 4173 |
} |
| 4174 |
const context = query?.context ?? "default"; |
| 4175 |
if (!query || !query._fields) { |
| 4176 |
return !!queriedState.itemIsComplete[context]?.[key]; |
| 4177 |
} |
| 4178 |
const item = queriedState.items[context]?.[key]; |
| 4179 |
if (!item) { |
| 4180 |
return false; |
| 4181 |
} |
| 4182 |
const fields = get_normalized_comma_separable_default(query._fields) ?? []; |
| 4183 |
for (let i = 0; i < fields.length; i++) { |
| 4184 |
const path = fields[i].split("."); |
| 4185 |
let value = item; |
| 4186 |
for (let p = 0; p < path.length; p++) { |
| 4187 |
const part = path[p]; |
| 4188 |
if (!value || !Object.hasOwn(value, part)) { |
| 4189 |
return false; |
| 4190 |
} |
| 4191 |
value = value[part]; |
| 4192 |
} |
| 4193 |
} |
| 4194 |
return true; |
| 4195 |
} |
| 4196 |
function __experimentalGetEntityRecordNoResolver(state, kind, name, key) { |
| 4197 |
return getEntityRecord(state, kind, name, key); |
| 4198 |
} |
| 4199 |
var getRawEntityRecord = (0, import_data10.createSelector)( |
| 4200 |
(state, kind, name, key) => { |
| 4201 |
logEntityDeprecation(kind, name, "getRawEntityRecord"); |
| 4202 |
const record = getEntityRecord( |
| 4203 |
state, |
| 4204 |
kind, |
| 4205 |
name, |
| 4206 |
key |
| 4207 |
); |
| 4208 |
const config = getEntityConfig(state, kind, name); |
| 4209 |
if (!record || !config?.rawAttributes?.length) { |
| 4210 |
return record; |
| 4211 |
} |
| 4212 |
return Object.fromEntries( |
| 4213 |
Object.keys(record).map((_key) => { |
| 4214 |
if (config.rawAttributes.includes(_key)) { |
| 4215 |
const rawValue = record[_key]?.raw; |
| 4216 |
return [ |
| 4217 |
_key, |
| 4218 |
rawValue !== void 0 ? rawValue : record[_key] |
| 4219 |
]; |
| 4220 |
} |
| 4221 |
return [_key, record[_key]]; |
| 4222 |
}) |
| 4223 |
); |
| 4224 |
}, |
| 4225 |
(state, kind, name, recordId, query) => { |
| 4226 |
const context = query?.context ?? "default"; |
| 4227 |
return [ |
| 4228 |
state.entities.config, |
| 4229 |
state.entities.records?.[kind]?.[name]?.queriedData?.items[context]?.[recordId], |
| 4230 |
state.entities.records?.[kind]?.[name]?.queriedData?.itemIsComplete[context]?.[recordId] |
| 4231 |
]; |
| 4232 |
} |
| 4233 |
); |
| 4234 |
function hasEntityRecords(state, kind, name, query) { |
| 4235 |
logEntityDeprecation(kind, name, "hasEntityRecords"); |
| 4236 |
return Array.isArray(getEntityRecords(state, kind, name, query)); |
| 4237 |
} |
| 4238 |
var getEntityRecords = ((state, kind, name, query) => { |
| 4239 |
logEntityDeprecation(kind, name, "getEntityRecords"); |
| 4240 |
const queriedState = state.entities.records?.[kind]?.[name]?.queriedData; |
| 4241 |
if (!queriedState) { |
| 4242 |
return null; |
| 4243 |
} |
| 4244 |
return getQueriedItems(queriedState, query); |
| 4245 |
}); |
| 4246 |
var getEntityRecordsTotalItems = (state, kind, name, query) => { |
| 4247 |
logEntityDeprecation(kind, name, "getEntityRecordsTotalItems"); |
| 4248 |
const queriedState = state.entities.records?.[kind]?.[name]?.queriedData; |
| 4249 |
if (!queriedState) { |
| 4250 |
return null; |
| 4251 |
} |
| 4252 |
return getQueriedTotalItems(queriedState, query); |
| 4253 |
}; |
| 4254 |
var getEntityRecordsTotalPages = (state, kind, name, query) => { |
| 4255 |
logEntityDeprecation(kind, name, "getEntityRecordsTotalPages"); |
| 4256 |
const queriedState = state.entities.records?.[kind]?.[name]?.queriedData; |
| 4257 |
if (!queriedState) { |
| 4258 |
return null; |
| 4259 |
} |
| 4260 |
if (query?.per_page === -1) { |
| 4261 |
return 1; |
| 4262 |
} |
| 4263 |
const totalItems = getQueriedTotalItems(queriedState, query); |
| 4264 |
if (!totalItems) { |
| 4265 |
return totalItems; |
| 4266 |
} |
| 4267 |
if (!query?.per_page) { |
| 4268 |
return getQueriedTotalPages(queriedState, query); |
| 4269 |
} |
| 4270 |
return Math.ceil(totalItems / query.per_page); |
| 4271 |
}; |
| 4272 |
var __experimentalGetDirtyEntityRecords = (0, import_data10.createSelector)( |
| 4273 |
(state) => { |
| 4274 |
const { |
| 4275 |
entities: { records } |
| 4276 |
} = state; |
| 4277 |
const dirtyRecords = []; |
| 4278 |
Object.keys(records).forEach((kind) => { |
| 4279 |
Object.keys(records[kind]).forEach((name) => { |
| 4280 |
const primaryKeys = Object.keys(records[kind][name].edits).filter( |
| 4281 |
(primaryKey) => ( |
| 4282 |
// The entity record must exist (not be deleted), |
| 4283 |
// and it must have edits. |
| 4284 |
getEntityRecord(state, kind, name, primaryKey) && hasEditsForEntityRecord(state, kind, name, primaryKey) |
| 4285 |
) |
| 4286 |
); |
| 4287 |
if (primaryKeys.length) { |
| 4288 |
const entityConfig = getEntityConfig(state, kind, name); |
| 4289 |
primaryKeys.forEach((primaryKey) => { |
| 4290 |
const entityRecord = getEditedEntityRecord( |
| 4291 |
state, |
| 4292 |
kind, |
| 4293 |
name, |
| 4294 |
primaryKey |
| 4295 |
); |
| 4296 |
dirtyRecords.push({ |
| 4297 |
// We avoid using primaryKey because it's transformed into a string |
| 4298 |
// when it's used as an object key. |
| 4299 |
key: entityRecord ? entityRecord[entityConfig.key || DEFAULT_ENTITY_KEY] : void 0, |
| 4300 |
title: entityConfig?.getTitle?.(entityRecord) || "", |
| 4301 |
name, |
| 4302 |
kind |
| 4303 |
}); |
| 4304 |
}); |
| 4305 |
} |
| 4306 |
}); |
| 4307 |
}); |
| 4308 |
return dirtyRecords; |
| 4309 |
}, |
| 4310 |
(state) => [state.entities.records] |
| 4311 |
); |
| 4312 |
var __experimentalGetEntitiesBeingSaved = (0, import_data10.createSelector)( |
| 4313 |
(state) => { |
| 4314 |
const { |
| 4315 |
entities: { records } |
| 4316 |
} = state; |
| 4317 |
const recordsBeingSaved = []; |
| 4318 |
Object.keys(records).forEach((kind) => { |
| 4319 |
Object.keys(records[kind]).forEach((name) => { |
| 4320 |
const primaryKeys = Object.keys(records[kind][name].saving).filter( |
| 4321 |
(primaryKey) => isSavingEntityRecord(state, kind, name, primaryKey) |
| 4322 |
); |
| 4323 |
if (primaryKeys.length) { |
| 4324 |
const entityConfig = getEntityConfig(state, kind, name); |
| 4325 |
primaryKeys.forEach((primaryKey) => { |
| 4326 |
const entityRecord = getEditedEntityRecord( |
| 4327 |
state, |
| 4328 |
kind, |
| 4329 |
name, |
| 4330 |
primaryKey |
| 4331 |
); |
| 4332 |
recordsBeingSaved.push({ |
| 4333 |
// We avoid using primaryKey because it's transformed into a string |
| 4334 |
// when it's used as an object key. |
| 4335 |
key: entityRecord ? entityRecord[entityConfig.key || DEFAULT_ENTITY_KEY] : void 0, |
| 4336 |
title: entityConfig?.getTitle?.(entityRecord) || "", |
| 4337 |
name, |
| 4338 |
kind |
| 4339 |
}); |
| 4340 |
}); |
| 4341 |
} |
| 4342 |
}); |
| 4343 |
}); |
| 4344 |
return recordsBeingSaved; |
| 4345 |
}, |
| 4346 |
(state) => [state.entities.records] |
| 4347 |
); |
| 4348 |
function getEntityRecordEdits(state, kind, name, recordId) { |
| 4349 |
logEntityDeprecation(kind, name, "getEntityRecordEdits"); |
| 4350 |
return state.entities.records?.[kind]?.[name]?.edits?.[recordId]; |
| 4351 |
} |
| 4352 |
var getEntityRecordNonTransientEdits = (0, import_data10.createSelector)( |
| 4353 |
(state, kind, name, recordId) => { |
| 4354 |
logEntityDeprecation(kind, name, "getEntityRecordNonTransientEdits"); |
| 4355 |
const { transientEdits } = getEntityConfig(state, kind, name) || {}; |
| 4356 |
const edits = getEntityRecordEdits(state, kind, name, recordId) || {}; |
| 4357 |
if (!transientEdits) { |
| 4358 |
return edits; |
| 4359 |
} |
| 4360 |
return Object.keys(edits).reduce((acc, key) => { |
| 4361 |
if (!transientEdits[key]) { |
| 4362 |
acc[key] = edits[key]; |
| 4363 |
} |
| 4364 |
return acc; |
| 4365 |
}, {}); |
| 4366 |
}, |
| 4367 |
(state, kind, name, recordId) => [ |
| 4368 |
state.entities.config, |
| 4369 |
state.entities.records?.[kind]?.[name]?.edits?.[recordId] |
| 4370 |
] |
| 4371 |
); |
| 4372 |
function hasEditsForEntityRecord(state, kind, name, recordId) { |
| 4373 |
logEntityDeprecation(kind, name, "hasEditsForEntityRecord"); |
| 4374 |
return isSavingEntityRecord(state, kind, name, recordId) || Object.keys( |
| 4375 |
getEntityRecordNonTransientEdits(state, kind, name, recordId) |
| 4376 |
).length > 0; |
| 4377 |
} |
| 4378 |
var getEditedEntityRecord = (0, import_data10.createSelector)( |
| 4379 |
(state, kind, name, recordId) => { |
| 4380 |
logEntityDeprecation(kind, name, "getEditedEntityRecord"); |
| 4381 |
const raw = getRawEntityRecord(state, kind, name, recordId); |
| 4382 |
const edited = getEntityRecordEdits(state, kind, name, recordId); |
| 4383 |
if (!raw && !edited) { |
| 4384 |
return false; |
| 4385 |
} |
| 4386 |
return { |
| 4387 |
...raw, |
| 4388 |
...edited |
| 4389 |
}; |
| 4390 |
}, |
| 4391 |
(state, kind, name, recordId, query) => { |
| 4392 |
const context = query?.context ?? "default"; |
| 4393 |
return [ |
| 4394 |
state.entities.config, |
| 4395 |
state.entities.records?.[kind]?.[name]?.queriedData.items[context]?.[recordId], |
| 4396 |
state.entities.records?.[kind]?.[name]?.queriedData.itemIsComplete[context]?.[recordId], |
| 4397 |
state.entities.records?.[kind]?.[name]?.edits?.[recordId] |
| 4398 |
]; |
| 4399 |
} |
| 4400 |
); |
| 4401 |
function isAutosavingEntityRecord(state, kind, name, recordId) { |
| 4402 |
logEntityDeprecation(kind, name, "isAutosavingEntityRecord"); |
| 4403 |
const { pending, isAutosave } = state.entities.records?.[kind]?.[name]?.saving?.[recordId] ?? {}; |
| 4404 |
return Boolean(pending && isAutosave); |
| 4405 |
} |
| 4406 |
function isSavingEntityRecord(state, kind, name, recordId) { |
| 4407 |
logEntityDeprecation(kind, name, "isSavingEntityRecord"); |
| 4408 |
return state.entities.records?.[kind]?.[name]?.saving?.[recordId]?.pending ?? false; |
| 4409 |
} |
| 4410 |
function isDeletingEntityRecord(state, kind, name, recordId) { |
| 4411 |
logEntityDeprecation(kind, name, "isDeletingEntityRecord"); |
| 4412 |
return state.entities.records?.[kind]?.[name]?.deleting?.[recordId]?.pending ?? false; |
| 4413 |
} |
| 4414 |
function getLastEntitySaveError(state, kind, name, recordId) { |
| 4415 |
logEntityDeprecation(kind, name, "getLastEntitySaveError"); |
| 4416 |
return state.entities.records?.[kind]?.[name]?.saving?.[recordId]?.error; |
| 4417 |
} |
| 4418 |
function getLastEntityDeleteError(state, kind, name, recordId) { |
| 4419 |
logEntityDeprecation(kind, name, "getLastEntityDeleteError"); |
| 4420 |
return state.entities.records?.[kind]?.[name]?.deleting?.[recordId]?.error; |
| 4421 |
} |
| 4422 |
function getUndoEdit(state) { |
| 4423 |
(0, import_deprecated2.default)("select( 'core' ).getUndoEdit()", { |
| 4424 |
since: "6.3" |
| 4425 |
}); |
| 4426 |
return void 0; |
| 4427 |
} |
| 4428 |
function getRedoEdit(state) { |
| 4429 |
(0, import_deprecated2.default)("select( 'core' ).getRedoEdit()", { |
| 4430 |
since: "6.3" |
| 4431 |
}); |
| 4432 |
return void 0; |
| 4433 |
} |
| 4434 |
function hasUndo(state) { |
| 4435 |
return getUndoManager(state).hasUndo(); |
| 4436 |
} |
| 4437 |
function hasRedo(state) { |
| 4438 |
return getUndoManager(state).hasRedo(); |
| 4439 |
} |
| 4440 |
function getCurrentTheme(state) { |
| 4441 |
if (!state.currentTheme) { |
| 4442 |
return null; |
| 4443 |
} |
| 4444 |
return getEntityRecord(state, "root", "theme", state.currentTheme); |
| 4445 |
} |
| 4446 |
function __experimentalGetCurrentGlobalStylesId(state) { |
| 4447 |
return state.currentGlobalStylesId; |
| 4448 |
} |
| 4449 |
function getThemeSupports(state) { |
| 4450 |
return getCurrentTheme(state)?.theme_supports ?? EMPTY_OBJECT2; |
| 4451 |
} |
| 4452 |
function getEmbedPreview(state, url) { |
| 4453 |
return state.embedPreviews[url]; |
| 4454 |
} |
| 4455 |
function isPreviewEmbedFallback(state, url) { |
| 4456 |
const preview = state.embedPreviews[url]; |
| 4457 |
const oEmbedLinkCheck = '<a href="' + url + '">' + url + "</a>"; |
| 4458 |
if (!preview) { |
| 4459 |
return false; |
| 4460 |
} |
| 4461 |
return preview.html === oEmbedLinkCheck; |
| 4462 |
} |
| 4463 |
function canUser(state, action, resource, id) { |
| 4464 |
const isEntity = typeof resource === "object"; |
| 4465 |
if (isEntity && (!resource.kind || !resource.name)) { |
| 4466 |
return false; |
| 4467 |
} |
| 4468 |
if (isEntity) { |
| 4469 |
logEntityDeprecation(resource.kind, resource.name, "canUser"); |
| 4470 |
} |
| 4471 |
const key = getUserPermissionCacheKey(action, resource, id); |
| 4472 |
return state.userPermissions[key]; |
| 4473 |
} |
| 4474 |
function canUserEditEntityRecord(state, kind, name, recordId) { |
| 4475 |
(0, import_deprecated2.default)(`wp.data.select( 'core' ).canUserEditEntityRecord()`, { |
| 4476 |
since: "6.7", |
| 4477 |
alternative: `wp.data.select( 'core' ).canUser( 'update', { kind, name, id } )` |
| 4478 |
}); |
| 4479 |
return canUser(state, "update", { kind, name, id: recordId }); |
| 4480 |
} |
| 4481 |
function getAutosaves(state, postType, postId) { |
| 4482 |
return state.autosaves[postId]; |
| 4483 |
} |
| 4484 |
function getAutosave(state, postType, postId, authorId) { |
| 4485 |
if (authorId === void 0) { |
| 4486 |
return; |
| 4487 |
} |
| 4488 |
const autosaves2 = state.autosaves[postId]; |
| 4489 |
return autosaves2?.find( |
| 4490 |
(autosave) => autosave.author === authorId |
| 4491 |
); |
| 4492 |
} |
| 4493 |
var hasFetchedAutosaves = (0, import_data10.createRegistrySelector)( |
| 4494 |
(select5) => (state, postType, postId) => { |
| 4495 |
return select5(STORE_NAME).hasFinishedResolution("getAutosaves", [ |
| 4496 |
postType, |
| 4497 |
postId |
| 4498 |
]); |
| 4499 |
} |
| 4500 |
); |
| 4501 |
function getReferenceByDistinctEdits(state) { |
| 4502 |
return state.editsReference; |
| 4503 |
} |
| 4504 |
function __experimentalGetCurrentThemeBaseGlobalStyles(state) { |
| 4505 |
const currentTheme2 = getCurrentTheme(state); |
| 4506 |
if (!currentTheme2) { |
| 4507 |
return null; |
| 4508 |
} |
| 4509 |
return state.themeBaseGlobalStyles[currentTheme2.stylesheet]; |
| 4510 |
} |
| 4511 |
function __experimentalGetCurrentThemeGlobalStylesVariations(state) { |
| 4512 |
const currentTheme2 = getCurrentTheme(state); |
| 4513 |
if (!currentTheme2) { |
| 4514 |
return null; |
| 4515 |
} |
| 4516 |
return state.themeGlobalStyleVariations[currentTheme2.stylesheet]; |
| 4517 |
} |
| 4518 |
function getBlockPatterns(state) { |
| 4519 |
return state.blockPatterns; |
| 4520 |
} |
| 4521 |
function getBlockPatternCategories(state) { |
| 4522 |
return state.blockPatternCategories; |
| 4523 |
} |
| 4524 |
function getUserPatternCategories(state) { |
| 4525 |
return state.userPatternCategories; |
| 4526 |
} |
| 4527 |
function getCurrentThemeGlobalStylesRevisions(state) { |
| 4528 |
(0, import_deprecated2.default)("select( 'core' ).getCurrentThemeGlobalStylesRevisions()", { |
| 4529 |
since: "6.5.0", |
| 4530 |
alternative: "select( 'core' ).getRevisions( 'root', 'globalStyles', ${ recordKey } )" |
| 4531 |
}); |
| 4532 |
const currentGlobalStylesId2 = __experimentalGetCurrentGlobalStylesId(state); |
| 4533 |
if (!currentGlobalStylesId2) { |
| 4534 |
return null; |
| 4535 |
} |
| 4536 |
return state.themeGlobalStyleRevisions[currentGlobalStylesId2]; |
| 4537 |
} |
| 4538 |
function getDefaultTemplateId(state, query) { |
| 4539 |
return state.defaultTemplates[JSON.stringify(query)]; |
| 4540 |
} |
| 4541 |
var getRevisions = (state, kind, name, recordKey, query) => { |
| 4542 |
logEntityDeprecation(kind, name, "getRevisions"); |
| 4543 |
const queriedStateRevisions = state.entities.records?.[kind]?.[name]?.revisions?.[recordKey]; |
| 4544 |
if (!queriedStateRevisions) { |
| 4545 |
return null; |
| 4546 |
} |
| 4547 |
return getQueriedItems(queriedStateRevisions, query); |
| 4548 |
}; |
| 4549 |
function hasRevision(state, kind, name, recordKey, revisionKey, query) { |
| 4550 |
const queriedState = state.entities.records?.[kind]?.[name]?.revisions?.[recordKey]; |
| 4551 |
if (!queriedState) { |
| 4552 |
return false; |
| 4553 |
} |
| 4554 |
const context = query?.context ?? "default"; |
| 4555 |
if (!query || !query._fields) { |
| 4556 |
return !!queriedState.itemIsComplete[context]?.[revisionKey]; |
| 4557 |
} |
| 4558 |
const item = queriedState.items[context]?.[revisionKey]; |
| 4559 |
if (!item) { |
| 4560 |
return false; |
| 4561 |
} |
| 4562 |
const fields = get_normalized_comma_separable_default(query._fields) ?? []; |
| 4563 |
for (let i = 0; i < fields.length; i++) { |
| 4564 |
const path = fields[i].split("."); |
| 4565 |
let value = item; |
| 4566 |
for (let p = 0; p < path.length; p++) { |
| 4567 |
const part = path[p]; |
| 4568 |
if (!value || !Object.hasOwn(value, part)) { |
| 4569 |
return false; |
| 4570 |
} |
| 4571 |
value = value[part]; |
| 4572 |
} |
| 4573 |
} |
| 4574 |
return true; |
| 4575 |
} |
| 4576 |
var getRevision = (0, import_data10.createSelector)( |
| 4577 |
(state, kind, name, recordKey, revisionKey, query) => { |
| 4578 |
logEntityDeprecation(kind, name, "getRevision"); |
| 4579 |
const queriedState = state.entities.records?.[kind]?.[name]?.revisions?.[recordKey]; |
| 4580 |
if (!queriedState) { |
| 4581 |
return void 0; |
| 4582 |
} |
| 4583 |
const context = query?.context ?? "default"; |
| 4584 |
if (!query || !query._fields) { |
| 4585 |
if (!queriedState.itemIsComplete[context]?.[revisionKey]) { |
| 4586 |
return void 0; |
| 4587 |
} |
| 4588 |
return queriedState.items[context][revisionKey]; |
| 4589 |
} |
| 4590 |
const item = queriedState.items[context]?.[revisionKey]; |
| 4591 |
if (!item) { |
| 4592 |
return item; |
| 4593 |
} |
| 4594 |
const filteredItem = {}; |
| 4595 |
const fields = get_normalized_comma_separable_default(query._fields) ?? []; |
| 4596 |
for (let f = 0; f < fields.length; f++) { |
| 4597 |
const field = fields[f].split("."); |
| 4598 |
let value = item; |
| 4599 |
field.forEach((fieldName) => { |
| 4600 |
value = value?.[fieldName]; |
| 4601 |
}); |
| 4602 |
setNestedValue(filteredItem, field, value); |
| 4603 |
} |
| 4604 |
return filteredItem; |
| 4605 |
}, |
| 4606 |
(state, kind, name, recordKey, revisionKey, query) => { |
| 4607 |
const context = query?.context ?? "default"; |
| 4608 |
const queriedState = state.entities.records?.[kind]?.[name]?.revisions?.[recordKey]; |
| 4609 |
return [ |
| 4610 |
queriedState?.items?.[context]?.[revisionKey], |
| 4611 |
queriedState?.itemIsComplete?.[context]?.[revisionKey] |
| 4612 |
]; |
| 4613 |
} |
| 4614 |
); |
| 4615 |
function getSyncConnectionStatus(state) { |
| 4616 |
if (!state.syncConnectionStatuses) { |
| 4617 |
return void 0; |
| 4618 |
} |
| 4619 |
const PRIORITIZED_STATUSES = ["disconnected", "connecting", "connected"]; |
| 4620 |
let coalesced; |
| 4621 |
for (const status of Object.values(state.syncConnectionStatuses)) { |
| 4622 |
if (!coalesced || PRIORITIZED_STATUSES.indexOf(status.status) < PRIORITIZED_STATUSES.indexOf(coalesced.status)) { |
| 4623 |
coalesced = status; |
| 4624 |
} |
| 4625 |
} |
| 4626 |
return coalesced; |
| 4627 |
} |
| 4628 |
|
| 4629 |
// packages/core-data/build-module/actions.mjs |
| 4630 |
var actions_exports = {}; |
| 4631 |
__export(actions_exports, { |
| 4632 |
__experimentalBatch: () => __experimentalBatch, |
| 4633 |
__experimentalReceiveCurrentGlobalStylesId: () => __experimentalReceiveCurrentGlobalStylesId, |
| 4634 |
__experimentalReceiveThemeBaseGlobalStyles: () => __experimentalReceiveThemeBaseGlobalStyles, |
| 4635 |
__experimentalReceiveThemeGlobalStyleVariations: () => __experimentalReceiveThemeGlobalStyleVariations, |
| 4636 |
__experimentalSaveSpecifiedEntityEdits: () => __experimentalSaveSpecifiedEntityEdits, |
| 4637 |
__unstableCreateUndoLevel: () => __unstableCreateUndoLevel, |
| 4638 |
addEntities: () => addEntities, |
| 4639 |
clearEntityRecordEdits: () => clearEntityRecordEdits, |
| 4640 |
deleteEntityRecord: () => deleteEntityRecord, |
| 4641 |
editEntityRecord: () => editEntityRecord, |
| 4642 |
receiveAutosaves: () => receiveAutosaves, |
| 4643 |
receiveCurrentTheme: () => receiveCurrentTheme, |
| 4644 |
receiveCurrentUser: () => receiveCurrentUser, |
| 4645 |
receiveDefaultTemplateId: () => receiveDefaultTemplateId, |
| 4646 |
receiveEmbedPreview: () => receiveEmbedPreview, |
| 4647 |
receiveEntityRecords: () => receiveEntityRecords, |
| 4648 |
receiveNavigationFallbackId: () => receiveNavigationFallbackId, |
| 4649 |
receiveRevisions: () => receiveRevisions, |
| 4650 |
receiveThemeGlobalStyleRevisions: () => receiveThemeGlobalStyleRevisions, |
| 4651 |
receiveThemeSupports: () => receiveThemeSupports, |
| 4652 |
receiveUploadPermissions: () => receiveUploadPermissions, |
| 4653 |
receiveUserPermission: () => receiveUserPermission, |
| 4654 |
receiveUserPermissions: () => receiveUserPermissions, |
| 4655 |
receiveUserQuery: () => receiveUserQuery, |
| 4656 |
redo: () => redo, |
| 4657 |
saveEditedEntityRecord: () => saveEditedEntityRecord, |
| 4658 |
saveEntityRecord: () => saveEntityRecord, |
| 4659 |
setSyncConnectionStatus: () => setSyncConnectionStatus, |
| 4660 |
undo: () => undo |
| 4661 |
}); |
| 4662 |
var import_es65 = __toESM(require_es6(), 1); |
| 4663 |
var import_api_fetch3 = __toESM(require_api_fetch(), 1); |
| 4664 |
var import_url3 = __toESM(require_url(), 1); |
| 4665 |
var import_deprecated3 = __toESM(require_deprecated(), 1); |
| 4666 |
|
| 4667 |
// packages/core-data/build-module/batch/default-processor.mjs |
| 4668 |
var import_api_fetch2 = __toESM(require_api_fetch(), 1); |
| 4669 |
var maxItems = null; |
| 4670 |
function chunk(arr, chunkSize) { |
| 4671 |
const tmp = [...arr]; |
| 4672 |
const cache3 = []; |
| 4673 |
while (tmp.length) { |
| 4674 |
cache3.push(tmp.splice(0, chunkSize)); |
| 4675 |
} |
| 4676 |
return cache3; |
| 4677 |
} |
| 4678 |
async function defaultProcessor(requests) { |
| 4679 |
if (maxItems === null) { |
| 4680 |
const preflightResponse = await (0, import_api_fetch2.default)({ |
| 4681 |
path: "/batch/v1", |
| 4682 |
method: "OPTIONS" |
| 4683 |
}); |
| 4684 |
maxItems = preflightResponse.endpoints[0].args.requests.maxItems; |
| 4685 |
} |
| 4686 |
const results = []; |
| 4687 |
for (const batchRequests of chunk(requests, maxItems)) { |
| 4688 |
const batchResponse = await (0, import_api_fetch2.default)({ |
| 4689 |
path: "/batch/v1", |
| 4690 |
method: "POST", |
| 4691 |
data: { |
| 4692 |
validation: "require-all-validate", |
| 4693 |
requests: batchRequests.map((request) => ({ |
| 4694 |
path: request.path, |
| 4695 |
body: request.data, |
| 4696 |
// Rename 'data' to 'body'. |
| 4697 |
method: request.method, |
| 4698 |
headers: request.headers |
| 4699 |
})) |
| 4700 |
} |
| 4701 |
}); |
| 4702 |
let batchResults; |
| 4703 |
if (batchResponse.failed) { |
| 4704 |
batchResults = batchResponse.responses.map((response) => ({ |
| 4705 |
error: response?.body |
| 4706 |
})); |
| 4707 |
} else { |
| 4708 |
batchResults = batchResponse.responses.map((response) => { |
| 4709 |
const result = {}; |
| 4710 |
if (response.status >= 200 && response.status < 300) { |
| 4711 |
result.output = response.body; |
| 4712 |
} else { |
| 4713 |
result.error = response.body; |
| 4714 |
} |
| 4715 |
return result; |
| 4716 |
}); |
| 4717 |
} |
| 4718 |
results.push(...batchResults); |
| 4719 |
} |
| 4720 |
return results; |
| 4721 |
} |
| 4722 |
|
| 4723 |
// packages/core-data/build-module/batch/create-batch.mjs |
| 4724 |
function createBatch(processor = defaultProcessor) { |
| 4725 |
let lastId = 0; |
| 4726 |
let queue = []; |
| 4727 |
const pending = new ObservableSet(); |
| 4728 |
return { |
| 4729 |
/** |
| 4730 |
* Adds an input to the batch and returns a promise that is resolved or |
| 4731 |
* rejected when the input is processed by `batch.run()`. |
| 4732 |
* |
| 4733 |
* You may also pass a thunk which allows inputs to be added |
| 4734 |
* asynchronously. |
| 4735 |
* |
| 4736 |
* ``` |
| 4737 |
* // Both are allowed: |
| 4738 |
* batch.add( { path: '/v1/books', ... } ); |
| 4739 |
* batch.add( ( add ) => add( { path: '/v1/books', ... } ) ); |
| 4740 |
* ``` |
| 4741 |
* |
| 4742 |
* If a thunk is passed, `batch.run()` will pause until either: |
| 4743 |
* |
| 4744 |
* - The thunk calls its `add` argument, or; |
| 4745 |
* - The thunk returns a promise and that promise resolves, or; |
| 4746 |
* - The thunk returns a non-promise. |
| 4747 |
* |
| 4748 |
* @param {any|Function} inputOrThunk Input to add or thunk to execute. |
| 4749 |
* |
| 4750 |
* @return {Promise|any} If given an input, returns a promise that |
| 4751 |
* is resolved or rejected when the batch is |
| 4752 |
* processed. If given a thunk, returns the return |
| 4753 |
* value of that thunk. |
| 4754 |
*/ |
| 4755 |
add(inputOrThunk) { |
| 4756 |
const id = ++lastId; |
| 4757 |
pending.add(id); |
| 4758 |
const add = (input) => new Promise((resolve, reject) => { |
| 4759 |
queue.push({ |
| 4760 |
input, |
| 4761 |
resolve, |
| 4762 |
reject |
| 4763 |
}); |
| 4764 |
pending.delete(id); |
| 4765 |
}); |
| 4766 |
if (typeof inputOrThunk === "function") { |
| 4767 |
return Promise.resolve(inputOrThunk(add)).finally(() => { |
| 4768 |
pending.delete(id); |
| 4769 |
}); |
| 4770 |
} |
| 4771 |
return add(inputOrThunk); |
| 4772 |
}, |
| 4773 |
/** |
| 4774 |
* Runs the batch. This calls `batchProcessor` and resolves or rejects |
| 4775 |
* all promises returned by `add()`. |
| 4776 |
* |
| 4777 |
* @return {Promise<boolean>} A promise that resolves to a boolean that is true |
| 4778 |
* if the processor returned no errors. |
| 4779 |
*/ |
| 4780 |
async run() { |
| 4781 |
if (pending.size) { |
| 4782 |
await new Promise((resolve) => { |
| 4783 |
const unsubscribe = pending.subscribe(() => { |
| 4784 |
if (!pending.size) { |
| 4785 |
unsubscribe(); |
| 4786 |
resolve(void 0); |
| 4787 |
} |
| 4788 |
}); |
| 4789 |
}); |
| 4790 |
} |
| 4791 |
let results; |
| 4792 |
try { |
| 4793 |
results = await processor( |
| 4794 |
queue.map(({ input }) => input) |
| 4795 |
); |
| 4796 |
if (results.length !== queue.length) { |
| 4797 |
throw new Error( |
| 4798 |
"run: Array returned by processor must be same size as input array." |
| 4799 |
); |
| 4800 |
} |
| 4801 |
} catch (error) { |
| 4802 |
for (const { reject } of queue) { |
| 4803 |
reject(error); |
| 4804 |
} |
| 4805 |
throw error; |
| 4806 |
} |
| 4807 |
let isSuccess = true; |
| 4808 |
results.forEach((result, key) => { |
| 4809 |
const queueItem = queue[key]; |
| 4810 |
if (result?.error) { |
| 4811 |
queueItem?.reject(result.error); |
| 4812 |
isSuccess = false; |
| 4813 |
} else { |
| 4814 |
queueItem?.resolve(result?.output ?? result); |
| 4815 |
} |
| 4816 |
}); |
| 4817 |
queue = []; |
| 4818 |
return isSuccess; |
| 4819 |
} |
| 4820 |
}; |
| 4821 |
} |
| 4822 |
var ObservableSet = class { |
| 4823 |
constructor(...args) { |
| 4824 |
this.set = new Set(...args); |
| 4825 |
this.subscribers = /* @__PURE__ */ new Set(); |
| 4826 |
} |
| 4827 |
get size() { |
| 4828 |
return this.set.size; |
| 4829 |
} |
| 4830 |
add(value) { |
| 4831 |
this.set.add(value); |
| 4832 |
this.subscribers.forEach((subscriber) => subscriber()); |
| 4833 |
return this; |
| 4834 |
} |
| 4835 |
delete(value) { |
| 4836 |
const isSuccess = this.set.delete(value); |
| 4837 |
this.subscribers.forEach((subscriber) => subscriber()); |
| 4838 |
return isSuccess; |
| 4839 |
} |
| 4840 |
subscribe(subscriber) { |
| 4841 |
this.subscribers.add(subscriber); |
| 4842 |
return () => { |
| 4843 |
this.subscribers.delete(subscriber); |
| 4844 |
}; |
| 4845 |
} |
| 4846 |
}; |
| 4847 |
|
| 4848 |
// packages/core-data/build-module/actions.mjs |
| 4849 |
function addTitleToAutoDraft(record) { |
| 4850 |
return record.status === "auto-draft" ? { ...record, title: "" } : record; |
| 4851 |
} |
| 4852 |
function receiveUserQuery(queryID, users2) { |
| 4853 |
return { |
| 4854 |
type: "RECEIVE_USER_QUERY", |
| 4855 |
users: Array.isArray(users2) ? users2 : [users2], |
| 4856 |
queryID |
| 4857 |
}; |
| 4858 |
} |
| 4859 |
function receiveCurrentUser(currentUser2) { |
| 4860 |
return { |
| 4861 |
type: "RECEIVE_CURRENT_USER", |
| 4862 |
currentUser: currentUser2 |
| 4863 |
}; |
| 4864 |
} |
| 4865 |
function addEntities(entities2) { |
| 4866 |
return { |
| 4867 |
type: "ADD_ENTITIES", |
| 4868 |
entities: entities2 |
| 4869 |
}; |
| 4870 |
} |
| 4871 |
function receiveEntityRecords(kind, name, records, query = void 0, invalidateCache = false, edits = void 0, meta = void 0) { |
| 4872 |
if (kind === "postType") { |
| 4873 |
records = Array.isArray(records) ? records.map(addTitleToAutoDraft) : addTitleToAutoDraft(records); |
| 4874 |
} |
| 4875 |
let action; |
| 4876 |
if (query) { |
| 4877 |
action = receiveQueriedItems(records, query, edits, meta); |
| 4878 |
} else { |
| 4879 |
action = receiveItems(records, edits, meta); |
| 4880 |
} |
| 4881 |
return { |
| 4882 |
...action, |
| 4883 |
kind, |
| 4884 |
name, |
| 4885 |
invalidateCache |
| 4886 |
}; |
| 4887 |
} |
| 4888 |
function receiveCurrentTheme(currentTheme2) { |
| 4889 |
return { |
| 4890 |
type: "RECEIVE_CURRENT_THEME", |
| 4891 |
currentTheme: currentTheme2 |
| 4892 |
}; |
| 4893 |
} |
| 4894 |
function __experimentalReceiveCurrentGlobalStylesId(currentGlobalStylesId2) { |
| 4895 |
return { |
| 4896 |
type: "RECEIVE_CURRENT_GLOBAL_STYLES_ID", |
| 4897 |
id: currentGlobalStylesId2 |
| 4898 |
}; |
| 4899 |
} |
| 4900 |
function __experimentalReceiveThemeBaseGlobalStyles(stylesheet, globalStyles) { |
| 4901 |
return { |
| 4902 |
type: "RECEIVE_THEME_GLOBAL_STYLES", |
| 4903 |
stylesheet, |
| 4904 |
globalStyles |
| 4905 |
}; |
| 4906 |
} |
| 4907 |
function __experimentalReceiveThemeGlobalStyleVariations(stylesheet, variations) { |
| 4908 |
return { |
| 4909 |
type: "RECEIVE_THEME_GLOBAL_STYLE_VARIATIONS", |
| 4910 |
stylesheet, |
| 4911 |
variations |
| 4912 |
}; |
| 4913 |
} |
| 4914 |
function receiveThemeSupports() { |
| 4915 |
(0, import_deprecated3.default)("wp.data.dispatch( 'core' ).receiveThemeSupports", { |
| 4916 |
since: "5.9" |
| 4917 |
}); |
| 4918 |
return { |
| 4919 |
type: "DO_NOTHING" |
| 4920 |
}; |
| 4921 |
} |
| 4922 |
function receiveThemeGlobalStyleRevisions(currentId, revisions) { |
| 4923 |
(0, import_deprecated3.default)( |
| 4924 |
"wp.data.dispatch( 'core' ).receiveThemeGlobalStyleRevisions()", |
| 4925 |
{ |
| 4926 |
since: "6.5.0", |
| 4927 |
alternative: "wp.data.dispatch( 'core' ).receiveRevisions" |
| 4928 |
} |
| 4929 |
); |
| 4930 |
return { |
| 4931 |
type: "RECEIVE_THEME_GLOBAL_STYLE_REVISIONS", |
| 4932 |
currentId, |
| 4933 |
revisions |
| 4934 |
}; |
| 4935 |
} |
| 4936 |
function receiveEmbedPreview(url, preview) { |
| 4937 |
return { |
| 4938 |
type: "RECEIVE_EMBED_PREVIEW", |
| 4939 |
url, |
| 4940 |
preview |
| 4941 |
}; |
| 4942 |
} |
| 4943 |
var deleteEntityRecord = (kind, name, recordId, query, { __unstableFetch = import_api_fetch3.default, throwOnError = false } = {}) => async ({ dispatch: dispatch3, resolveSelect: resolveSelect2 }) => { |
| 4944 |
logEntityDeprecation(kind, name, "deleteEntityRecord"); |
| 4945 |
const configs = await resolveSelect2.getEntitiesConfig(kind); |
| 4946 |
const entityConfig = configs.find( |
| 4947 |
(config) => config.kind === kind && config.name === name |
| 4948 |
); |
| 4949 |
let error; |
| 4950 |
let deletedRecord = false; |
| 4951 |
if (!entityConfig) { |
| 4952 |
return; |
| 4953 |
} |
| 4954 |
const lock2 = await dispatch3.__unstableAcquireStoreLock( |
| 4955 |
STORE_NAME, |
| 4956 |
["entities", "records", kind, name, recordId], |
| 4957 |
{ exclusive: true } |
| 4958 |
); |
| 4959 |
try { |
| 4960 |
dispatch3({ |
| 4961 |
type: "DELETE_ENTITY_RECORD_START", |
| 4962 |
kind, |
| 4963 |
name, |
| 4964 |
recordId |
| 4965 |
}); |
| 4966 |
let hasError = false; |
| 4967 |
let { baseURL } = entityConfig; |
| 4968 |
if (kind === "postType" && name === "wp_template" && (recordId && typeof recordId === "string" && !/^\d+$/.test(recordId) || !window?.__experimentalTemplateActivate)) { |
| 4969 |
baseURL = baseURL.slice(0, baseURL.lastIndexOf("/")) + "/templates"; |
| 4970 |
} |
| 4971 |
try { |
| 4972 |
let path = `${baseURL}/${recordId}`; |
| 4973 |
if (query) { |
| 4974 |
path = (0, import_url3.addQueryArgs)(path, query); |
| 4975 |
} |
| 4976 |
deletedRecord = await __unstableFetch({ |
| 4977 |
path, |
| 4978 |
method: "DELETE" |
| 4979 |
}); |
| 4980 |
await dispatch3(removeItems(kind, name, recordId, true)); |
| 4981 |
if (entityConfig.syncConfig) { |
| 4982 |
const objectType = `${kind}/${name}`; |
| 4983 |
const objectId = recordId; |
| 4984 |
getSyncManager()?.unload(objectType, objectId); |
| 4985 |
} |
| 4986 |
} catch (_error) { |
| 4987 |
hasError = true; |
| 4988 |
error = _error; |
| 4989 |
} |
| 4990 |
dispatch3({ |
| 4991 |
type: "DELETE_ENTITY_RECORD_FINISH", |
| 4992 |
kind, |
| 4993 |
name, |
| 4994 |
recordId, |
| 4995 |
error |
| 4996 |
}); |
| 4997 |
if (hasError && throwOnError) { |
| 4998 |
throw error; |
| 4999 |
} |
| 5000 |
return deletedRecord; |
| 5001 |
} finally { |
| 5002 |
dispatch3.__unstableReleaseStoreLock(lock2); |
| 5003 |
} |
| 5004 |
}; |
| 5005 |
var editEntityRecord = (kind, name, recordId, edits, options = {}) => ({ select: select5, dispatch: dispatch3 }) => { |
| 5006 |
logEntityDeprecation(kind, name, "editEntityRecord"); |
| 5007 |
const entityConfig = select5.getEntityConfig(kind, name); |
| 5008 |
if (!entityConfig) { |
| 5009 |
throw new Error( |
| 5010 |
`The entity being edited (${kind}, ${name}) does not have a loaded config.` |
| 5011 |
); |
| 5012 |
} |
| 5013 |
const { mergedEdits = {} } = entityConfig; |
| 5014 |
const record = select5.getRawEntityRecord(kind, name, recordId); |
| 5015 |
const editedRecord = select5.getEditedEntityRecord( |
| 5016 |
kind, |
| 5017 |
name, |
| 5018 |
recordId |
| 5019 |
); |
| 5020 |
const editsWithMerges = Object.keys(edits).reduce((acc, key) => { |
| 5021 |
acc[key] = mergedEdits[key] ? { ...editedRecord[key], ...edits[key] } : edits[key]; |
| 5022 |
return acc; |
| 5023 |
}, {}); |
| 5024 |
const edit = { |
| 5025 |
kind, |
| 5026 |
name, |
| 5027 |
recordId, |
| 5028 |
// Clear edits when they are equal to their persisted counterparts |
| 5029 |
// so that the property is not considered dirty. |
| 5030 |
edits: Object.keys(edits).reduce((acc, key) => { |
| 5031 |
const recordValue = record[key]; |
| 5032 |
const value = editsWithMerges[key]; |
| 5033 |
acc[key] = (0, import_es65.default)(recordValue, value) ? void 0 : value; |
| 5034 |
return acc; |
| 5035 |
}, {}) |
| 5036 |
}; |
| 5037 |
if (entityConfig.syncConfig) { |
| 5038 |
const objectType = `${kind}/${name}`; |
| 5039 |
const objectId = recordId; |
| 5040 |
const isNewUndoLevel = options.undoIgnore ? false : !options.isCached; |
| 5041 |
const origin = options.undoIgnore ? LOCAL_UNDO_IGNORED_ORIGIN : LOCAL_EDITOR_ORIGIN; |
| 5042 |
getSyncManager()?.update( |
| 5043 |
objectType, |
| 5044 |
objectId, |
| 5045 |
editsWithMerges, |
| 5046 |
origin, |
| 5047 |
{ isNewUndoLevel } |
| 5048 |
); |
| 5049 |
} |
| 5050 |
if (!options.undoIgnore) { |
| 5051 |
select5.getUndoManager().addRecord( |
| 5052 |
[ |
| 5053 |
{ |
| 5054 |
id: { kind, name, recordId }, |
| 5055 |
changes: Object.keys(edits).reduce((acc, key) => { |
| 5056 |
acc[key] = { |
| 5057 |
from: editedRecord[key], |
| 5058 |
to: edits[key] |
| 5059 |
}; |
| 5060 |
return acc; |
| 5061 |
}, {}) |
| 5062 |
} |
| 5063 |
], |
| 5064 |
options.isCached |
| 5065 |
); |
| 5066 |
} |
| 5067 |
dispatch3({ |
| 5068 |
type: "EDIT_ENTITY_RECORD", |
| 5069 |
...edit |
| 5070 |
}); |
| 5071 |
}; |
| 5072 |
var clearEntityRecordEdits = (kind, name, recordId) => ({ select: select5, dispatch: dispatch3 }) => { |
| 5073 |
const entityConfig = select5.getEntityConfig(kind, name); |
| 5074 |
logEntityDeprecation(kind, name, "clearEntityRecordEdits"); |
| 5075 |
if (!entityConfig) { |
| 5076 |
throw new Error( |
| 5077 |
`The entity being edited (${kind}, ${name}) does not have a loaded config.` |
| 5078 |
); |
| 5079 |
} |
| 5080 |
const currentEdits = select5.getEntityRecordEdits( |
| 5081 |
kind, |
| 5082 |
name, |
| 5083 |
recordId |
| 5084 |
); |
| 5085 |
if (!currentEdits) { |
| 5086 |
return; |
| 5087 |
} |
| 5088 |
const clearedEdits = Object.keys(currentEdits).reduce( |
| 5089 |
(acc, key) => { |
| 5090 |
acc[key] = void 0; |
| 5091 |
return acc; |
| 5092 |
}, |
| 5093 |
{} |
| 5094 |
); |
| 5095 |
dispatch3({ |
| 5096 |
type: "EDIT_ENTITY_RECORD", |
| 5097 |
kind, |
| 5098 |
name, |
| 5099 |
recordId, |
| 5100 |
edits: clearedEdits |
| 5101 |
}); |
| 5102 |
}; |
| 5103 |
var undo = () => ({ select: select5, dispatch: dispatch3 }) => { |
| 5104 |
const undoRecord = select5.getUndoManager().undo(); |
| 5105 |
if (!undoRecord) { |
| 5106 |
return; |
| 5107 |
} |
| 5108 |
dispatch3({ |
| 5109 |
type: "UNDO", |
| 5110 |
record: undoRecord |
| 5111 |
}); |
| 5112 |
}; |
| 5113 |
var redo = () => ({ select: select5, dispatch: dispatch3 }) => { |
| 5114 |
const redoRecord = select5.getUndoManager().redo(); |
| 5115 |
if (!redoRecord) { |
| 5116 |
return; |
| 5117 |
} |
| 5118 |
dispatch3({ |
| 5119 |
type: "REDO", |
| 5120 |
record: redoRecord |
| 5121 |
}); |
| 5122 |
}; |
| 5123 |
var __unstableCreateUndoLevel = () => ({ select: select5 }) => { |
| 5124 |
select5.getUndoManager().addRecord(); |
| 5125 |
}; |
| 5126 |
var saveEntityRecord = (kind, name, record, { |
| 5127 |
isAutosave = false, |
| 5128 |
__unstableFetch = import_api_fetch3.default, |
| 5129 |
throwOnError = false |
| 5130 |
} = {}) => async ({ select: select5, resolveSelect: resolveSelect2, dispatch: dispatch3 }) => { |
| 5131 |
logEntityDeprecation(kind, name, "saveEntityRecord"); |
| 5132 |
const configs = await resolveSelect2.getEntitiesConfig(kind); |
| 5133 |
const entityConfig = configs.find( |
| 5134 |
(config) => config.kind === kind && config.name === name |
| 5135 |
); |
| 5136 |
if (!entityConfig) { |
| 5137 |
return; |
| 5138 |
} |
| 5139 |
const entityIdKey = entityConfig.key ?? DEFAULT_ENTITY_KEY; |
| 5140 |
const recordId = record[entityIdKey]; |
| 5141 |
const isNewRecord = !!entityIdKey && !recordId; |
| 5142 |
const lock2 = await dispatch3.__unstableAcquireStoreLock( |
| 5143 |
STORE_NAME, |
| 5144 |
["entities", "records", kind, name, recordId || v4_default()], |
| 5145 |
{ exclusive: true } |
| 5146 |
); |
| 5147 |
try { |
| 5148 |
for (const [key, value] of Object.entries(record)) { |
| 5149 |
if (typeof value === "function") { |
| 5150 |
const evaluatedValue = value( |
| 5151 |
select5.getEditedEntityRecord(kind, name, recordId) |
| 5152 |
); |
| 5153 |
dispatch3.editEntityRecord( |
| 5154 |
kind, |
| 5155 |
name, |
| 5156 |
recordId, |
| 5157 |
{ |
| 5158 |
[key]: evaluatedValue |
| 5159 |
}, |
| 5160 |
{ undoIgnore: true } |
| 5161 |
); |
| 5162 |
record[key] = evaluatedValue; |
| 5163 |
} |
| 5164 |
} |
| 5165 |
dispatch3({ |
| 5166 |
type: "SAVE_ENTITY_RECORD_START", |
| 5167 |
kind, |
| 5168 |
name, |
| 5169 |
recordId, |
| 5170 |
isAutosave |
| 5171 |
}); |
| 5172 |
let updatedRecord; |
| 5173 |
let error; |
| 5174 |
let hasError = false; |
| 5175 |
let { baseURL } = entityConfig; |
| 5176 |
if (kind === "postType" && name === "wp_template" && (recordId && typeof recordId === "string" && !/^\d+$/.test(recordId) || !window?.__experimentalTemplateActivate)) { |
| 5177 |
baseURL = baseURL.slice(0, baseURL.lastIndexOf("/")) + "/templates"; |
| 5178 |
} |
| 5179 |
try { |
| 5180 |
const path = `${baseURL}${recordId ? "/" + recordId : ""}`; |
| 5181 |
const persistedRecord = !isNewRecord ? select5.getRawEntityRecord(kind, name, recordId) : {}; |
| 5182 |
if (isAutosave) { |
| 5183 |
const merged = { ...persistedRecord, ...record }; |
| 5184 |
const data = [ |
| 5185 |
"title", |
| 5186 |
"excerpt", |
| 5187 |
"content", |
| 5188 |
"meta" |
| 5189 |
].reduce( |
| 5190 |
(acc, key) => { |
| 5191 |
if (key in merged) { |
| 5192 |
acc[key] = merged[key]; |
| 5193 |
} |
| 5194 |
return acc; |
| 5195 |
}, |
| 5196 |
{ |
| 5197 |
// Do not update the `status` if we have edited it when auto saving. |
| 5198 |
// It's very important to let the user explicitly save this change, |
| 5199 |
// because it can lead to unexpected results. An example would be to |
| 5200 |
// have a draft post and change the status to publish. |
| 5201 |
status: merged.status === "auto-draft" ? "draft" : void 0 |
| 5202 |
} |
| 5203 |
); |
| 5204 |
updatedRecord = await __unstableFetch({ |
| 5205 |
path: `${path}/autosaves`, |
| 5206 |
method: "POST", |
| 5207 |
data |
| 5208 |
}); |
| 5209 |
if (persistedRecord.id === updatedRecord.id) { |
| 5210 |
let newRecord = { |
| 5211 |
...persistedRecord, |
| 5212 |
...data, |
| 5213 |
...updatedRecord |
| 5214 |
}; |
| 5215 |
newRecord = Object.keys(newRecord).reduce( |
| 5216 |
(acc, key) => { |
| 5217 |
if ([ |
| 5218 |
"title", |
| 5219 |
"excerpt", |
| 5220 |
"content", |
| 5221 |
"meta" |
| 5222 |
].includes(key)) { |
| 5223 |
acc[key] = newRecord[key]; |
| 5224 |
} else if (key === "status") { |
| 5225 |
acc[key] = persistedRecord.status === "auto-draft" && newRecord.status === "draft" ? newRecord.status : persistedRecord.status; |
| 5226 |
} else { |
| 5227 |
acc[key] = persistedRecord[key]; |
| 5228 |
} |
| 5229 |
return acc; |
| 5230 |
}, |
| 5231 |
{} |
| 5232 |
); |
| 5233 |
dispatch3.receiveEntityRecords( |
| 5234 |
kind, |
| 5235 |
name, |
| 5236 |
newRecord, |
| 5237 |
void 0, |
| 5238 |
true |
| 5239 |
); |
| 5240 |
} else { |
| 5241 |
dispatch3.receiveAutosaves( |
| 5242 |
persistedRecord.id, |
| 5243 |
updatedRecord |
| 5244 |
); |
| 5245 |
} |
| 5246 |
} else { |
| 5247 |
let edits = record; |
| 5248 |
if (entityConfig.__unstablePrePersist) { |
| 5249 |
edits = { |
| 5250 |
...edits, |
| 5251 |
...await entityConfig.__unstablePrePersist( |
| 5252 |
persistedRecord, |
| 5253 |
edits |
| 5254 |
) |
| 5255 |
}; |
| 5256 |
} |
| 5257 |
updatedRecord = await __unstableFetch({ |
| 5258 |
path, |
| 5259 |
method: recordId ? "PUT" : "POST", |
| 5260 |
data: edits |
| 5261 |
}); |
| 5262 |
dispatch3.receiveEntityRecords( |
| 5263 |
kind, |
| 5264 |
name, |
| 5265 |
updatedRecord, |
| 5266 |
void 0, |
| 5267 |
true, |
| 5268 |
edits |
| 5269 |
); |
| 5270 |
if (entityConfig.syncConfig) { |
| 5271 |
getSyncManager()?.update( |
| 5272 |
`${kind}/${name}`, |
| 5273 |
recordId, |
| 5274 |
updatedRecord, |
| 5275 |
LOCAL_UNDO_IGNORED_ORIGIN, |
| 5276 |
{ isSave: true } |
| 5277 |
); |
| 5278 |
} |
| 5279 |
} |
| 5280 |
} catch (_error) { |
| 5281 |
hasError = true; |
| 5282 |
error = _error; |
| 5283 |
} |
| 5284 |
dispatch3({ |
| 5285 |
type: "SAVE_ENTITY_RECORD_FINISH", |
| 5286 |
kind, |
| 5287 |
name, |
| 5288 |
recordId, |
| 5289 |
error, |
| 5290 |
isAutosave |
| 5291 |
}); |
| 5292 |
if (hasError && throwOnError) { |
| 5293 |
throw error; |
| 5294 |
} |
| 5295 |
return updatedRecord; |
| 5296 |
} finally { |
| 5297 |
dispatch3.__unstableReleaseStoreLock(lock2); |
| 5298 |
} |
| 5299 |
}; |
| 5300 |
var __experimentalBatch = (requests) => async ({ dispatch: dispatch3 }) => { |
| 5301 |
const batch = createBatch(); |
| 5302 |
const api = { |
| 5303 |
saveEntityRecord(kind, name, record, options) { |
| 5304 |
return batch.add( |
| 5305 |
(add) => dispatch3.saveEntityRecord(kind, name, record, { |
| 5306 |
...options, |
| 5307 |
__unstableFetch: add |
| 5308 |
}) |
| 5309 |
); |
| 5310 |
}, |
| 5311 |
saveEditedEntityRecord(kind, name, recordId, options) { |
| 5312 |
return batch.add( |
| 5313 |
(add) => dispatch3.saveEditedEntityRecord(kind, name, recordId, { |
| 5314 |
...options, |
| 5315 |
__unstableFetch: add |
| 5316 |
}) |
| 5317 |
); |
| 5318 |
}, |
| 5319 |
deleteEntityRecord(kind, name, recordId, query, options) { |
| 5320 |
return batch.add( |
| 5321 |
(add) => dispatch3.deleteEntityRecord(kind, name, recordId, query, { |
| 5322 |
...options, |
| 5323 |
__unstableFetch: add |
| 5324 |
}) |
| 5325 |
); |
| 5326 |
} |
| 5327 |
}; |
| 5328 |
const resultPromises = requests.map((request) => request(api)); |
| 5329 |
const [, ...results] = await Promise.all([ |
| 5330 |
batch.run(), |
| 5331 |
...resultPromises |
| 5332 |
]); |
| 5333 |
return results; |
| 5334 |
}; |
| 5335 |
var saveEditedEntityRecord = (kind, name, recordId, options) => async ({ select: select5, dispatch: dispatch3, resolveSelect: resolveSelect2 }) => { |
| 5336 |
logEntityDeprecation(kind, name, "saveEditedEntityRecord"); |
| 5337 |
if (!select5.hasEditsForEntityRecord(kind, name, recordId)) { |
| 5338 |
return; |
| 5339 |
} |
| 5340 |
const configs = await resolveSelect2.getEntitiesConfig(kind); |
| 5341 |
const entityConfig = configs.find( |
| 5342 |
(config) => config.kind === kind && config.name === name |
| 5343 |
); |
| 5344 |
if (!entityConfig) { |
| 5345 |
return; |
| 5346 |
} |
| 5347 |
const entityIdKey = entityConfig.key || DEFAULT_ENTITY_KEY; |
| 5348 |
const edits = select5.getEntityRecordNonTransientEdits( |
| 5349 |
kind, |
| 5350 |
name, |
| 5351 |
recordId |
| 5352 |
); |
| 5353 |
const record = { [entityIdKey]: recordId, ...edits }; |
| 5354 |
return await dispatch3.saveEntityRecord(kind, name, record, options); |
| 5355 |
}; |
| 5356 |
var __experimentalSaveSpecifiedEntityEdits = (kind, name, recordId, itemsToSave, options) => async ({ select: select5, dispatch: dispatch3, resolveSelect: resolveSelect2 }) => { |
| 5357 |
logEntityDeprecation( |
| 5358 |
kind, |
| 5359 |
name, |
| 5360 |
"__experimentalSaveSpecifiedEntityEdits" |
| 5361 |
); |
| 5362 |
if (!select5.hasEditsForEntityRecord(kind, name, recordId)) { |
| 5363 |
return; |
| 5364 |
} |
| 5365 |
const edits = select5.getEntityRecordNonTransientEdits( |
| 5366 |
kind, |
| 5367 |
name, |
| 5368 |
recordId |
| 5369 |
); |
| 5370 |
const editsToSave = {}; |
| 5371 |
for (const item of itemsToSave) { |
| 5372 |
setNestedValue(editsToSave, item, getNestedValue(edits, item)); |
| 5373 |
} |
| 5374 |
const configs = await resolveSelect2.getEntitiesConfig(kind); |
| 5375 |
const entityConfig = configs.find( |
| 5376 |
(config) => config.kind === kind && config.name === name |
| 5377 |
); |
| 5378 |
const entityIdKey = entityConfig?.key || DEFAULT_ENTITY_KEY; |
| 5379 |
if (recordId) { |
| 5380 |
editsToSave[entityIdKey] = recordId; |
| 5381 |
} |
| 5382 |
return await dispatch3.saveEntityRecord( |
| 5383 |
kind, |
| 5384 |
name, |
| 5385 |
editsToSave, |
| 5386 |
options |
| 5387 |
); |
| 5388 |
}; |
| 5389 |
function receiveUploadPermissions(hasUploadPermissions) { |
| 5390 |
(0, import_deprecated3.default)("wp.data.dispatch( 'core' ).receiveUploadPermissions", { |
| 5391 |
since: "5.9", |
| 5392 |
alternative: "receiveUserPermission" |
| 5393 |
}); |
| 5394 |
return receiveUserPermission("create/media", hasUploadPermissions); |
| 5395 |
} |
| 5396 |
function receiveUserPermission(key, isAllowed) { |
| 5397 |
return { |
| 5398 |
type: "RECEIVE_USER_PERMISSION", |
| 5399 |
key, |
| 5400 |
isAllowed |
| 5401 |
}; |
| 5402 |
} |
| 5403 |
function receiveUserPermissions(permissions) { |
| 5404 |
return { |
| 5405 |
type: "RECEIVE_USER_PERMISSIONS", |
| 5406 |
permissions |
| 5407 |
}; |
| 5408 |
} |
| 5409 |
function receiveAutosaves(postId, autosaves2) { |
| 5410 |
return { |
| 5411 |
type: "RECEIVE_AUTOSAVES", |
| 5412 |
postId, |
| 5413 |
autosaves: Array.isArray(autosaves2) ? autosaves2 : [autosaves2] |
| 5414 |
}; |
| 5415 |
} |
| 5416 |
function receiveNavigationFallbackId(fallbackId) { |
| 5417 |
return { |
| 5418 |
type: "RECEIVE_NAVIGATION_FALLBACK_ID", |
| 5419 |
fallbackId |
| 5420 |
}; |
| 5421 |
} |
| 5422 |
function receiveDefaultTemplateId(query, templateId) { |
| 5423 |
return { |
| 5424 |
type: "RECEIVE_DEFAULT_TEMPLATE", |
| 5425 |
query, |
| 5426 |
templateId |
| 5427 |
}; |
| 5428 |
} |
| 5429 |
var receiveRevisions = (kind, name, recordKey, records, query, invalidateCache = false, meta) => async ({ dispatch: dispatch3, resolveSelect: resolveSelect2 }) => { |
| 5430 |
logEntityDeprecation(kind, name, "receiveRevisions"); |
| 5431 |
const configs = await resolveSelect2.getEntitiesConfig(kind); |
| 5432 |
const entityConfig = configs.find( |
| 5433 |
(config) => config.kind === kind && config.name === name |
| 5434 |
); |
| 5435 |
const key = entityConfig?.revisionKey ?? DEFAULT_ENTITY_KEY; |
| 5436 |
dispatch3({ |
| 5437 |
type: "RECEIVE_ITEM_REVISIONS", |
| 5438 |
key, |
| 5439 |
items: records, |
| 5440 |
recordKey, |
| 5441 |
meta, |
| 5442 |
query, |
| 5443 |
kind, |
| 5444 |
name, |
| 5445 |
invalidateCache |
| 5446 |
}); |
| 5447 |
}; |
| 5448 |
function setSyncConnectionStatus(kind, name, key, status) { |
| 5449 |
if (!status) { |
| 5450 |
return { |
| 5451 |
type: "CLEAR_SYNC_CONNECTION_STATUS", |
| 5452 |
kind, |
| 5453 |
name, |
| 5454 |
key |
| 5455 |
}; |
| 5456 |
} |
| 5457 |
return { |
| 5458 |
type: "SET_SYNC_CONNECTION_STATUS", |
| 5459 |
kind, |
| 5460 |
name, |
| 5461 |
key, |
| 5462 |
status |
| 5463 |
}; |
| 5464 |
} |
| 5465 |
|
| 5466 |
// packages/core-data/build-module/private-actions.mjs |
| 5467 |
var private_actions_exports = {}; |
| 5468 |
__export(private_actions_exports, { |
| 5469 |
editMediaEntity: () => editMediaEntity, |
| 5470 |
receiveEditorAssets: () => receiveEditorAssets, |
| 5471 |
receiveEditorSettings: () => receiveEditorSettings, |
| 5472 |
receiveRegisteredPostMeta: () => receiveRegisteredPostMeta, |
| 5473 |
receiveViewConfig: () => receiveViewConfig, |
| 5474 |
setCollaborationSupported: () => setCollaborationSupported |
| 5475 |
}); |
| 5476 |
var import_api_fetch4 = __toESM(require_api_fetch(), 1); |
| 5477 |
function receiveRegisteredPostMeta(postType, registeredPostMeta2) { |
| 5478 |
return { |
| 5479 |
type: "RECEIVE_REGISTERED_POST_META", |
| 5480 |
postType, |
| 5481 |
registeredPostMeta: registeredPostMeta2 |
| 5482 |
}; |
| 5483 |
} |
| 5484 |
var editMediaEntity = (recordId, edits = {}, { __unstableFetch = import_api_fetch4.default, throwOnError = false } = {}) => async ({ dispatch: dispatch3, resolveSelect: resolveSelect2 }) => { |
| 5485 |
if (!recordId) { |
| 5486 |
return; |
| 5487 |
} |
| 5488 |
const kind = "postType"; |
| 5489 |
const name = "attachment"; |
| 5490 |
const configs = await resolveSelect2.getEntitiesConfig(kind); |
| 5491 |
const entityConfig = configs.find( |
| 5492 |
(config) => config.kind === kind && config.name === name |
| 5493 |
); |
| 5494 |
if (!entityConfig) { |
| 5495 |
return; |
| 5496 |
} |
| 5497 |
const lock2 = await dispatch3.__unstableAcquireStoreLock( |
| 5498 |
STORE_NAME, |
| 5499 |
["entities", "records", kind, name, recordId], |
| 5500 |
{ exclusive: true } |
| 5501 |
); |
| 5502 |
let updatedRecord; |
| 5503 |
let error; |
| 5504 |
let hasError = false; |
| 5505 |
try { |
| 5506 |
dispatch3({ |
| 5507 |
type: "SAVE_ENTITY_RECORD_START", |
| 5508 |
kind, |
| 5509 |
name, |
| 5510 |
recordId |
| 5511 |
}); |
| 5512 |
try { |
| 5513 |
const path = `${entityConfig.baseURL}/${recordId}/edit`; |
| 5514 |
const newRecord = await __unstableFetch({ |
| 5515 |
path, |
| 5516 |
method: "POST", |
| 5517 |
data: { |
| 5518 |
...edits |
| 5519 |
} |
| 5520 |
}); |
| 5521 |
if (newRecord) { |
| 5522 |
dispatch3.receiveEntityRecords( |
| 5523 |
kind, |
| 5524 |
name, |
| 5525 |
newRecord, |
| 5526 |
void 0, |
| 5527 |
true, |
| 5528 |
void 0, |
| 5529 |
void 0 |
| 5530 |
); |
| 5531 |
updatedRecord = newRecord; |
| 5532 |
} |
| 5533 |
} catch (e) { |
| 5534 |
error = e; |
| 5535 |
hasError = true; |
| 5536 |
} |
| 5537 |
dispatch3({ |
| 5538 |
type: "SAVE_ENTITY_RECORD_FINISH", |
| 5539 |
kind, |
| 5540 |
name, |
| 5541 |
recordId, |
| 5542 |
error |
| 5543 |
}); |
| 5544 |
if (hasError && throwOnError) { |
| 5545 |
throw error; |
| 5546 |
} |
| 5547 |
return updatedRecord; |
| 5548 |
} finally { |
| 5549 |
dispatch3.__unstableReleaseStoreLock(lock2); |
| 5550 |
} |
| 5551 |
}; |
| 5552 |
function receiveEditorSettings(settings) { |
| 5553 |
return { |
| 5554 |
type: "RECEIVE_EDITOR_SETTINGS", |
| 5555 |
settings |
| 5556 |
}; |
| 5557 |
} |
| 5558 |
function receiveEditorAssets(assets) { |
| 5559 |
return { |
| 5560 |
type: "RECEIVE_EDITOR_ASSETS", |
| 5561 |
assets |
| 5562 |
}; |
| 5563 |
} |
| 5564 |
var setCollaborationSupported = (supported) => ({ dispatch: dispatch3 }) => { |
| 5565 |
dispatch3({ type: "SET_COLLABORATION_SUPPORTED", supported }); |
| 5566 |
}; |
| 5567 |
function receiveViewConfig(kind, name, config) { |
| 5568 |
return { |
| 5569 |
type: "RECEIVE_VIEW_CONFIG", |
| 5570 |
kind, |
| 5571 |
name, |
| 5572 |
config |
| 5573 |
}; |
| 5574 |
} |
| 5575 |
|
| 5576 |
// packages/core-data/build-module/resolvers.mjs |
| 5577 |
var resolvers_exports = {}; |
| 5578 |
__export(resolvers_exports, { |
| 5579 |
__experimentalGetCurrentGlobalStylesId: () => __experimentalGetCurrentGlobalStylesId2, |
| 5580 |
__experimentalGetCurrentThemeBaseGlobalStyles: () => __experimentalGetCurrentThemeBaseGlobalStyles2, |
| 5581 |
__experimentalGetCurrentThemeGlobalStylesVariations: () => __experimentalGetCurrentThemeGlobalStylesVariations2, |
| 5582 |
canUser: () => canUser2, |
| 5583 |
canUserEditEntityRecord: () => canUserEditEntityRecord2, |
| 5584 |
getAuthors: () => getAuthors2, |
| 5585 |
getAutosave: () => getAutosave2, |
| 5586 |
getAutosaves: () => getAutosaves2, |
| 5587 |
getBlockPatternCategories: () => getBlockPatternCategories2, |
| 5588 |
getBlockPatterns: () => getBlockPatterns2, |
| 5589 |
getCurrentTheme: () => getCurrentTheme2, |
| 5590 |
getCurrentThemeGlobalStylesRevisions: () => getCurrentThemeGlobalStylesRevisions2, |
| 5591 |
getCurrentUser: () => getCurrentUser2, |
| 5592 |
getDefaultTemplateId: () => getDefaultTemplateId2, |
| 5593 |
getEditedEntityRecord: () => getEditedEntityRecord2, |
| 5594 |
getEditorAssets: () => getEditorAssets2, |
| 5595 |
getEditorSettings: () => getEditorSettings2, |
| 5596 |
getEmbedPreview: () => getEmbedPreview2, |
| 5597 |
getEntitiesConfig: () => getEntitiesConfig2, |
| 5598 |
getEntityRecord: () => getEntityRecord2, |
| 5599 |
getEntityRecords: () => getEntityRecords2, |
| 5600 |
getEntityRecordsTotalItems: () => getEntityRecordsTotalItems2, |
| 5601 |
getEntityRecordsTotalPages: () => getEntityRecordsTotalPages2, |
| 5602 |
getNavigationFallbackId: () => getNavigationFallbackId2, |
| 5603 |
getRawEntityRecord: () => getRawEntityRecord2, |
| 5604 |
getRegisteredPostMeta: () => getRegisteredPostMeta2, |
| 5605 |
getRevision: () => getRevision2, |
| 5606 |
getRevisions: () => getRevisions2, |
| 5607 |
getThemeSupports: () => getThemeSupports2, |
| 5608 |
getUserPatternCategories: () => getUserPatternCategories2, |
| 5609 |
getViewConfig: () => getViewConfig2 |
| 5610 |
}); |
| 5611 |
var import_url6 = __toESM(require_url(), 1); |
| 5612 |
var import_html_entities2 = __toESM(require_html_entities(), 1); |
| 5613 |
var import_api_fetch8 = __toESM(require_api_fetch(), 1); |
| 5614 |
|
| 5615 |
// packages/core-data/build-module/fetch/index.mjs |
| 5616 |
var import_api_fetch7 = __toESM(require_api_fetch(), 1); |
| 5617 |
|
| 5618 |
// packages/core-data/build-module/fetch/__experimental-fetch-link-suggestions.mjs |
| 5619 |
var import_api_fetch5 = __toESM(require_api_fetch(), 1); |
| 5620 |
var import_url4 = __toESM(require_url(), 1); |
| 5621 |
var import_html_entities = __toESM(require_html_entities(), 1); |
| 5622 |
var import_i18n2 = __toESM(require_i18n(), 1); |
| 5623 |
async function fetchLinkSuggestions(search, searchOptions = {}, editorSettings2 = {}) { |
| 5624 |
const searchOptionsToUse = searchOptions.isInitialSuggestions && searchOptions.initialSuggestionsSearchOptions ? { |
| 5625 |
...searchOptions, |
| 5626 |
...searchOptions.initialSuggestionsSearchOptions |
| 5627 |
} : searchOptions; |
| 5628 |
const { |
| 5629 |
type, |
| 5630 |
subtype, |
| 5631 |
page, |
| 5632 |
perPage = searchOptions.isInitialSuggestions ? 3 : 20 |
| 5633 |
} = searchOptionsToUse; |
| 5634 |
const { disablePostFormats = false } = editorSettings2; |
| 5635 |
const queries2 = []; |
| 5636 |
if (!type || type === "post") { |
| 5637 |
queries2.push( |
| 5638 |
(0, import_api_fetch5.default)({ |
| 5639 |
path: (0, import_url4.addQueryArgs)("/wp/v2/search", { |
| 5640 |
search, |
| 5641 |
page, |
| 5642 |
per_page: perPage, |
| 5643 |
type: "post", |
| 5644 |
subtype |
| 5645 |
}) |
| 5646 |
}).then((results2) => { |
| 5647 |
return results2.map((result) => { |
| 5648 |
return { |
| 5649 |
id: result.id, |
| 5650 |
url: result.url, |
| 5651 |
title: (0, import_html_entities.decodeEntities)(result.title || "") || (0, import_i18n2.__)("(no title)"), |
| 5652 |
type: result.subtype || result.type, |
| 5653 |
kind: "post-type" |
| 5654 |
}; |
| 5655 |
}); |
| 5656 |
}).catch(() => []) |
| 5657 |
// Fail by returning no results. |
| 5658 |
); |
| 5659 |
} |
| 5660 |
if (!type || type === "term") { |
| 5661 |
queries2.push( |
| 5662 |
(0, import_api_fetch5.default)({ |
| 5663 |
path: (0, import_url4.addQueryArgs)("/wp/v2/search", { |
| 5664 |
search, |
| 5665 |
page, |
| 5666 |
per_page: perPage, |
| 5667 |
type: "term", |
| 5668 |
subtype |
| 5669 |
}) |
| 5670 |
}).then((results2) => { |
| 5671 |
return results2.map((result) => { |
| 5672 |
return { |
| 5673 |
id: result.id, |
| 5674 |
url: result.url, |
| 5675 |
title: (0, import_html_entities.decodeEntities)(result.title || "") || (0, import_i18n2.__)("(no title)"), |
| 5676 |
type: result.subtype || result.type, |
| 5677 |
kind: "taxonomy" |
| 5678 |
}; |
| 5679 |
}); |
| 5680 |
}).catch(() => []) |
| 5681 |
// Fail by returning no results. |
| 5682 |
); |
| 5683 |
} |
| 5684 |
if (!disablePostFormats && (!type || type === "post-format")) { |
| 5685 |
queries2.push( |
| 5686 |
(0, import_api_fetch5.default)({ |
| 5687 |
path: (0, import_url4.addQueryArgs)("/wp/v2/search", { |
| 5688 |
search, |
| 5689 |
page, |
| 5690 |
per_page: perPage, |
| 5691 |
type: "post-format", |
| 5692 |
subtype |
| 5693 |
}) |
| 5694 |
}).then((results2) => { |
| 5695 |
return results2.map((result) => { |
| 5696 |
return { |
| 5697 |
id: result.id, |
| 5698 |
url: result.url, |
| 5699 |
title: (0, import_html_entities.decodeEntities)(result.title || "") || (0, import_i18n2.__)("(no title)"), |
| 5700 |
type: result.subtype || result.type, |
| 5701 |
kind: "taxonomy" |
| 5702 |
}; |
| 5703 |
}); |
| 5704 |
}).catch(() => []) |
| 5705 |
// Fail by returning no results. |
| 5706 |
); |
| 5707 |
} |
| 5708 |
if (!type || type === "attachment") { |
| 5709 |
queries2.push( |
| 5710 |
(0, import_api_fetch5.default)({ |
| 5711 |
path: (0, import_url4.addQueryArgs)("/wp/v2/media", { |
| 5712 |
search, |
| 5713 |
page, |
| 5714 |
per_page: perPage |
| 5715 |
}) |
| 5716 |
}).then((results2) => { |
| 5717 |
return results2.map((result) => { |
| 5718 |
return { |
| 5719 |
id: result.id, |
| 5720 |
url: result.source_url, |
| 5721 |
title: (0, import_html_entities.decodeEntities)(result.title.rendered || "") || (0, import_i18n2.__)("(no title)"), |
| 5722 |
type: result.type, |
| 5723 |
kind: "media" |
| 5724 |
}; |
| 5725 |
}); |
| 5726 |
}).catch(() => []) |
| 5727 |
// Fail by returning no results. |
| 5728 |
); |
| 5729 |
} |
| 5730 |
const responses = await Promise.all(queries2); |
| 5731 |
let results = responses.flat(); |
| 5732 |
results = results.filter((result) => !!result.id); |
| 5733 |
results = sortResults(results, search); |
| 5734 |
results = results.slice(0, perPage); |
| 5735 |
return results; |
| 5736 |
} |
| 5737 |
function sortResults(results, search) { |
| 5738 |
const searchTokens = tokenize(search); |
| 5739 |
const scores = {}; |
| 5740 |
for (const result of results) { |
| 5741 |
if (result.title) { |
| 5742 |
const titleTokens = tokenize(result.title); |
| 5743 |
const exactMatchingTokens = titleTokens.filter( |
| 5744 |
(titleToken) => searchTokens.some( |
| 5745 |
(searchToken) => titleToken === searchToken |
| 5746 |
) |
| 5747 |
); |
| 5748 |
const subMatchingTokens = titleTokens.filter( |
| 5749 |
(titleToken) => searchTokens.some( |
| 5750 |
(searchToken) => titleToken !== searchToken && titleToken.includes(searchToken) |
| 5751 |
) |
| 5752 |
); |
| 5753 |
const exactMatchScore = exactMatchingTokens.length / titleTokens.length * 10; |
| 5754 |
const subMatchScore = subMatchingTokens.length / titleTokens.length; |
| 5755 |
scores[result.id] = exactMatchScore + subMatchScore; |
| 5756 |
} else { |
| 5757 |
scores[result.id] = 0; |
| 5758 |
} |
| 5759 |
} |
| 5760 |
return results.sort((a, b) => scores[b.id] - scores[a.id]); |
| 5761 |
} |
| 5762 |
function tokenize(text) { |
| 5763 |
return text.toLowerCase().match(/[\p{L}\p{N}]+/gu) || []; |
| 5764 |
} |
| 5765 |
|
| 5766 |
// packages/core-data/build-module/fetch/__experimental-fetch-url-data.mjs |
| 5767 |
var import_api_fetch6 = __toESM(require_api_fetch(), 1); |
| 5768 |
var import_url5 = __toESM(require_url(), 1); |
| 5769 |
var CACHE = /* @__PURE__ */ new Map(); |
| 5770 |
var fetchUrlData = async (url, options = {}) => { |
| 5771 |
const endpoint = "/wp-block-editor/v1/url-details"; |
| 5772 |
const args = { |
| 5773 |
url: (0, import_url5.prependHTTP)(url) |
| 5774 |
}; |
| 5775 |
if (!(0, import_url5.isURL)(url)) { |
| 5776 |
return Promise.reject(`${url} is not a valid URL.`); |
| 5777 |
} |
| 5778 |
const protocol = (0, import_url5.getProtocol)(url); |
| 5779 |
if (!protocol || !(0, import_url5.isValidProtocol)(protocol) || !protocol.startsWith("http") || !/^https?:\/\/[^\/\s]/i.test(url)) { |
| 5780 |
return Promise.reject( |
| 5781 |
`${url} does not have a valid protocol. URLs must be "http" based` |
| 5782 |
); |
| 5783 |
} |
| 5784 |
if (CACHE.has(url)) { |
| 5785 |
return CACHE.get(url); |
| 5786 |
} |
| 5787 |
return (0, import_api_fetch6.default)({ |
| 5788 |
path: (0, import_url5.addQueryArgs)(endpoint, args), |
| 5789 |
...options |
| 5790 |
}).then((res) => { |
| 5791 |
CACHE.set(url, res); |
| 5792 |
return res; |
| 5793 |
}); |
| 5794 |
}; |
| 5795 |
var experimental_fetch_url_data_default = fetchUrlData; |
| 5796 |
|
| 5797 |
// packages/core-data/build-module/fetch/index.mjs |
| 5798 |
async function fetchBlockPatterns() { |
| 5799 |
const restPatterns = await (0, import_api_fetch7.default)({ |
| 5800 |
path: "/wp/v2/block-patterns/patterns" |
| 5801 |
}); |
| 5802 |
if (!restPatterns) { |
| 5803 |
return []; |
| 5804 |
} |
| 5805 |
return restPatterns.map( |
| 5806 |
(pattern) => Object.fromEntries( |
| 5807 |
Object.entries(pattern).map(([key, value]) => [ |
| 5808 |
camelCase(key), |
| 5809 |
value |
| 5810 |
]) |
| 5811 |
) |
| 5812 |
); |
| 5813 |
} |
| 5814 |
|
| 5815 |
// packages/core-data/build-module/resolvers.mjs |
| 5816 |
var getAuthors2 = (query) => async ({ dispatch: dispatch3 }) => { |
| 5817 |
const path = (0, import_url6.addQueryArgs)( |
| 5818 |
"/wp/v2/users/?who=authors&per_page=100", |
| 5819 |
query |
| 5820 |
); |
| 5821 |
const users2 = await (0, import_api_fetch8.default)({ path }); |
| 5822 |
dispatch3.receiveUserQuery(path, users2); |
| 5823 |
}; |
| 5824 |
var getCurrentUser2 = () => async ({ dispatch: dispatch3 }) => { |
| 5825 |
const currentUser2 = await (0, import_api_fetch8.default)({ path: "/wp/v2/users/me" }); |
| 5826 |
dispatch3.receiveCurrentUser(currentUser2); |
| 5827 |
}; |
| 5828 |
var getEntityRecord2 = (kind, name, key = "", query) => async ({ select: select5, dispatch: dispatch3, registry, resolveSelect: resolveSelect2 }) => { |
| 5829 |
const configs = await resolveSelect2.getEntitiesConfig(kind); |
| 5830 |
const entityConfig = configs.find( |
| 5831 |
(config) => config.name === name && config.kind === kind |
| 5832 |
); |
| 5833 |
if (!entityConfig) { |
| 5834 |
return; |
| 5835 |
} |
| 5836 |
const lock2 = await dispatch3.__unstableAcquireStoreLock( |
| 5837 |
STORE_NAME, |
| 5838 |
["entities", "records", kind, name, key], |
| 5839 |
{ exclusive: false } |
| 5840 |
); |
| 5841 |
try { |
| 5842 |
if (query !== void 0 && query._fields) { |
| 5843 |
query = { |
| 5844 |
...query, |
| 5845 |
_fields: [ |
| 5846 |
.../* @__PURE__ */ new Set([ |
| 5847 |
...get_normalized_comma_separable_default(query._fields) || [], |
| 5848 |
entityConfig.key || DEFAULT_ENTITY_KEY |
| 5849 |
]) |
| 5850 |
].join() |
| 5851 |
}; |
| 5852 |
} |
| 5853 |
if (query !== void 0 && query._fields) { |
| 5854 |
const hasRecord = select5.hasEntityRecord( |
| 5855 |
kind, |
| 5856 |
name, |
| 5857 |
key, |
| 5858 |
query |
| 5859 |
); |
| 5860 |
if (hasRecord) { |
| 5861 |
return; |
| 5862 |
} |
| 5863 |
} |
| 5864 |
let { baseURL } = entityConfig; |
| 5865 |
if (kind === "postType" && name === "wp_template" && (key && typeof key === "string" && !/^\d+$/.test(key) || !window?.__experimentalTemplateActivate)) { |
| 5866 |
baseURL = baseURL.slice(0, baseURL.lastIndexOf("/")) + "/templates"; |
| 5867 |
} |
| 5868 |
const path = (0, import_url6.addQueryArgs)(baseURL + (key ? "/" + key : ""), { |
| 5869 |
...entityConfig.baseURLParams, |
| 5870 |
...query |
| 5871 |
}); |
| 5872 |
const response = await (0, import_api_fetch8.default)({ path, parse: false }); |
| 5873 |
const record = await response.json(); |
| 5874 |
const permissions = getUserPermissionsFromAllowHeader( |
| 5875 |
response.headers?.get("allow") |
| 5876 |
); |
| 5877 |
const canUserResolutionsArgs = []; |
| 5878 |
const receiveUserPermissionArgs = {}; |
| 5879 |
for (const action of ALLOWED_RESOURCE_ACTIONS) { |
| 5880 |
receiveUserPermissionArgs[getUserPermissionCacheKey(action, { |
| 5881 |
kind, |
| 5882 |
name, |
| 5883 |
id: key |
| 5884 |
})] = permissions[action]; |
| 5885 |
canUserResolutionsArgs.push([ |
| 5886 |
action, |
| 5887 |
{ kind, name, id: key } |
| 5888 |
]); |
| 5889 |
} |
| 5890 |
if (entityConfig.syncConfig && isNumericID(key) && !query) { |
| 5891 |
const objectType = `${kind}/${name}`; |
| 5892 |
const objectId = key; |
| 5893 |
const recordWithTransients = { ...record }; |
| 5894 |
Object.entries(entityConfig.transientEdits ?? {}).filter( |
| 5895 |
([propName, transientConfig]) => void 0 === recordWithTransients[propName] && transientConfig && "object" === typeof transientConfig && "read" in transientConfig && "function" === typeof transientConfig.read |
| 5896 |
).forEach(([propName, transientConfig]) => { |
| 5897 |
recordWithTransients[propName] = transientConfig.read(recordWithTransients); |
| 5898 |
}); |
| 5899 |
void getSyncManager()?.load( |
| 5900 |
entityConfig.syncConfig, |
| 5901 |
objectType, |
| 5902 |
objectId, |
| 5903 |
recordWithTransients, |
| 5904 |
{ |
| 5905 |
// Handle edits sourced from the sync manager. |
| 5906 |
editRecord: (edits, options = {}) => { |
| 5907 |
if (!Object.keys(edits).length) { |
| 5908 |
return; |
| 5909 |
} |
| 5910 |
dispatch3({ |
| 5911 |
type: "EDIT_ENTITY_RECORD", |
| 5912 |
kind, |
| 5913 |
name, |
| 5914 |
recordId: key, |
| 5915 |
edits, |
| 5916 |
meta: { |
| 5917 |
undo: void 0 |
| 5918 |
}, |
| 5919 |
options |
| 5920 |
}); |
| 5921 |
}, |
| 5922 |
// Get the current entity record (with edits) |
| 5923 |
getEditedRecord: async () => await resolveSelect2.getEditedEntityRecord( |
| 5924 |
kind, |
| 5925 |
name, |
| 5926 |
key |
| 5927 |
), |
| 5928 |
// Handle sync connection status changes. |
| 5929 |
onStatusChange: (status) => { |
| 5930 |
dispatch3.setSyncConnectionStatus( |
| 5931 |
kind, |
| 5932 |
name, |
| 5933 |
key, |
| 5934 |
status |
| 5935 |
); |
| 5936 |
}, |
| 5937 |
// Refetch the current entity record from the database. |
| 5938 |
refetchRecord: async () => { |
| 5939 |
dispatch3.receiveEntityRecords( |
| 5940 |
kind, |
| 5941 |
name, |
| 5942 |
await (0, import_api_fetch8.default)({ path, parse: true }), |
| 5943 |
query |
| 5944 |
); |
| 5945 |
}, |
| 5946 |
// Persist the CRDT document. |
| 5947 |
// |
| 5948 |
// TODO: Currently, persisted CRDT documents are stored in post meta. |
| 5949 |
// This effectively means that only post entities support CRDT |
| 5950 |
// persistence. As we add support for syncing additional entity, |
| 5951 |
// we'll need to revisit where persisted CRDT documents are stored. |
| 5952 |
persistCRDTDoc: () => { |
| 5953 |
resolveSelect2.getEditedEntityRecord(kind, name, key).then((editedRecord) => { |
| 5954 |
const { meta, status } = editedRecord; |
| 5955 |
if ("auto-draft" === status || !meta) { |
| 5956 |
return; |
| 5957 |
} |
| 5958 |
dispatch3.saveEntityRecord( |
| 5959 |
kind, |
| 5960 |
name, |
| 5961 |
editedRecord |
| 5962 |
); |
| 5963 |
}); |
| 5964 |
}, |
| 5965 |
addUndoMeta: (ydoc, meta) => { |
| 5966 |
const selectionHistory = getSelectionHistory(ydoc); |
| 5967 |
if (selectionHistory) { |
| 5968 |
meta.set( |
| 5969 |
"selectionHistory", |
| 5970 |
selectionHistory |
| 5971 |
); |
| 5972 |
} |
| 5973 |
}, |
| 5974 |
restoreUndoMeta: (ydoc, meta) => { |
| 5975 |
const selectionHistory = meta.get("selectionHistory"); |
| 5976 |
if (selectionHistory) { |
| 5977 |
setTimeout(() => { |
| 5978 |
restoreSelection(selectionHistory, ydoc); |
| 5979 |
}, 0); |
| 5980 |
} |
| 5981 |
} |
| 5982 |
} |
| 5983 |
); |
| 5984 |
} |
| 5985 |
registry.batch(() => { |
| 5986 |
dispatch3.receiveEntityRecords(kind, name, record, query); |
| 5987 |
dispatch3.receiveUserPermissions(receiveUserPermissionArgs); |
| 5988 |
dispatch3.finishResolutions("canUser", canUserResolutionsArgs); |
| 5989 |
}); |
| 5990 |
} finally { |
| 5991 |
dispatch3.__unstableReleaseStoreLock(lock2); |
| 5992 |
} |
| 5993 |
}; |
| 5994 |
getEntityRecord2.shouldInvalidate = (action, kind, name) => { |
| 5995 |
return kind === "root" && name === "site" && (action.type === "RECEIVE_ITEMS" && // Making sure persistedEdits is set seems to be the only way of |
| 5996 |
// knowing whether it's an update or fetch. Only an update would |
| 5997 |
// have persistedEdits. |
| 5998 |
action.persistedEdits && action.persistedEdits.status !== "auto-draft" || action.type === "REMOVE_ITEMS") && action.kind === "postType" && action.name === "wp_template"; |
| 5999 |
}; |
| 6000 |
var getRawEntityRecord2 = forward_resolver_default("getEntityRecord"); |
| 6001 |
var getEditedEntityRecord2 = forward_resolver_default("getEntityRecord"); |
| 6002 |
var getEntityRecords2 = (kind, name, query = {}) => async ({ dispatch: dispatch3, registry, resolveSelect: resolveSelect2 }) => { |
| 6003 |
const configs = await resolveSelect2.getEntitiesConfig(kind); |
| 6004 |
const entityConfig = configs.find( |
| 6005 |
(config) => config.name === name && config.kind === kind |
| 6006 |
); |
| 6007 |
if (!entityConfig) { |
| 6008 |
return; |
| 6009 |
} |
| 6010 |
const lock2 = await dispatch3.__unstableAcquireStoreLock( |
| 6011 |
STORE_NAME, |
| 6012 |
["entities", "records", kind, name], |
| 6013 |
{ exclusive: false } |
| 6014 |
); |
| 6015 |
const rawQuery = { ...query }; |
| 6016 |
const key = entityConfig.key || DEFAULT_ENTITY_KEY; |
| 6017 |
function getResolutionsArgs(records, recordsQuery) { |
| 6018 |
const normalizedQuery = normalizeQueryForResolution(recordsQuery); |
| 6019 |
return records.filter((record) => record?.[key]).map((record) => [ |
| 6020 |
kind, |
| 6021 |
name, |
| 6022 |
record[key], |
| 6023 |
normalizedQuery |
| 6024 |
]); |
| 6025 |
} |
| 6026 |
try { |
| 6027 |
if (query._fields) { |
| 6028 |
query = { |
| 6029 |
...query, |
| 6030 |
_fields: [ |
| 6031 |
.../* @__PURE__ */ new Set([ |
| 6032 |
...get_normalized_comma_separable_default(query._fields) || [], |
| 6033 |
key |
| 6034 |
]) |
| 6035 |
].join() |
| 6036 |
}; |
| 6037 |
} |
| 6038 |
let { baseURL } = entityConfig; |
| 6039 |
const { combinedTemplates = true } = query; |
| 6040 |
if (kind === "postType" && name === "wp_template" && combinedTemplates) { |
| 6041 |
baseURL = baseURL.slice(0, baseURL.lastIndexOf("/")) + "/templates"; |
| 6042 |
} |
| 6043 |
const path = (0, import_url6.addQueryArgs)(baseURL, { |
| 6044 |
...entityConfig.baseURLParams, |
| 6045 |
...query |
| 6046 |
}); |
| 6047 |
let records = [], meta; |
| 6048 |
if (entityConfig.supportsPagination && query.per_page !== -1) { |
| 6049 |
const response = await (0, import_api_fetch8.default)({ path, parse: false }); |
| 6050 |
records = Object.values(await response.json()); |
| 6051 |
meta = { |
| 6052 |
totalItems: parseInt( |
| 6053 |
response.headers.get("X-WP-Total") |
| 6054 |
), |
| 6055 |
totalPages: parseInt( |
| 6056 |
response.headers.get("X-WP-TotalPages") |
| 6057 |
) |
| 6058 |
}; |
| 6059 |
} else if (query.per_page === -1 && query[RECEIVE_INTERMEDIATE_RESULTS] === true) { |
| 6060 |
let page = 1; |
| 6061 |
let totalPages; |
| 6062 |
do { |
| 6063 |
const response = await (0, import_api_fetch8.default)({ |
| 6064 |
path: (0, import_url6.addQueryArgs)(path, { page, per_page: 100 }), |
| 6065 |
parse: false |
| 6066 |
}); |
| 6067 |
const pageRecords = Object.values(await response.json()); |
| 6068 |
totalPages = parseInt( |
| 6069 |
response.headers.get("X-WP-TotalPages") |
| 6070 |
); |
| 6071 |
if (!meta) { |
| 6072 |
meta = { |
| 6073 |
totalItems: parseInt( |
| 6074 |
response.headers.get("X-WP-Total") |
| 6075 |
), |
| 6076 |
totalPages: 1 |
| 6077 |
}; |
| 6078 |
} |
| 6079 |
records.push(...pageRecords); |
| 6080 |
registry.batch(() => { |
| 6081 |
dispatch3.receiveEntityRecords( |
| 6082 |
kind, |
| 6083 |
name, |
| 6084 |
records, |
| 6085 |
query, |
| 6086 |
false, |
| 6087 |
void 0, |
| 6088 |
meta |
| 6089 |
); |
| 6090 |
dispatch3.finishResolutions( |
| 6091 |
"getEntityRecord", |
| 6092 |
getResolutionsArgs(pageRecords, rawQuery) |
| 6093 |
); |
| 6094 |
}); |
| 6095 |
page++; |
| 6096 |
} while (page <= totalPages); |
| 6097 |
} else { |
| 6098 |
records = Object.values(await (0, import_api_fetch8.default)({ path })); |
| 6099 |
meta = { |
| 6100 |
totalItems: records.length, |
| 6101 |
totalPages: 1 |
| 6102 |
}; |
| 6103 |
} |
| 6104 |
if (entityConfig.syncConfig && -1 === query.per_page) { |
| 6105 |
const objectType = `${kind}/${name}`; |
| 6106 |
getSyncManager()?.loadCollection( |
| 6107 |
entityConfig.syncConfig, |
| 6108 |
objectType, |
| 6109 |
{ |
| 6110 |
onStatusChange: (status) => { |
| 6111 |
dispatch3.setSyncConnectionStatus( |
| 6112 |
kind, |
| 6113 |
name, |
| 6114 |
null, |
| 6115 |
status |
| 6116 |
); |
| 6117 |
}, |
| 6118 |
refetchRecords: async () => { |
| 6119 |
dispatch3.receiveEntityRecords( |
| 6120 |
kind, |
| 6121 |
name, |
| 6122 |
await (0, import_api_fetch8.default)({ path, parse: true }), |
| 6123 |
query |
| 6124 |
); |
| 6125 |
} |
| 6126 |
} |
| 6127 |
); |
| 6128 |
} |
| 6129 |
if (query._fields) { |
| 6130 |
records = records.map((record) => { |
| 6131 |
query._fields.split(",").forEach((field) => { |
| 6132 |
if (!record.hasOwnProperty(field)) { |
| 6133 |
record[field] = void 0; |
| 6134 |
} |
| 6135 |
}); |
| 6136 |
return record; |
| 6137 |
}); |
| 6138 |
} |
| 6139 |
registry.batch(() => { |
| 6140 |
dispatch3.receiveEntityRecords( |
| 6141 |
kind, |
| 6142 |
name, |
| 6143 |
records, |
| 6144 |
query, |
| 6145 |
false, |
| 6146 |
void 0, |
| 6147 |
meta |
| 6148 |
); |
| 6149 |
const targetHints = records.filter( |
| 6150 |
(record) => !!record?.[key] && !!record?._links?.self?.[0]?.targetHints?.allow |
| 6151 |
).map((record) => ({ |
| 6152 |
id: record[key], |
| 6153 |
permissions: getUserPermissionsFromAllowHeader( |
| 6154 |
record._links.self[0].targetHints.allow |
| 6155 |
) |
| 6156 |
})); |
| 6157 |
const canUserResolutionsArgs = []; |
| 6158 |
const receiveUserPermissionArgs = {}; |
| 6159 |
for (const targetHint of targetHints) { |
| 6160 |
for (const action of ALLOWED_RESOURCE_ACTIONS) { |
| 6161 |
canUserResolutionsArgs.push([ |
| 6162 |
action, |
| 6163 |
{ kind, name, id: targetHint.id } |
| 6164 |
]); |
| 6165 |
receiveUserPermissionArgs[getUserPermissionCacheKey(action, { |
| 6166 |
kind, |
| 6167 |
name, |
| 6168 |
id: targetHint.id |
| 6169 |
})] = targetHint.permissions[action]; |
| 6170 |
} |
| 6171 |
} |
| 6172 |
if (targetHints.length > 0) { |
| 6173 |
dispatch3.receiveUserPermissions( |
| 6174 |
receiveUserPermissionArgs |
| 6175 |
); |
| 6176 |
dispatch3.finishResolutions( |
| 6177 |
"canUser", |
| 6178 |
canUserResolutionsArgs |
| 6179 |
); |
| 6180 |
} |
| 6181 |
dispatch3.finishResolutions( |
| 6182 |
"getEntityRecord", |
| 6183 |
getResolutionsArgs(records, rawQuery) |
| 6184 |
); |
| 6185 |
dispatch3.__unstableReleaseStoreLock(lock2); |
| 6186 |
}); |
| 6187 |
} catch (e) { |
| 6188 |
dispatch3.__unstableReleaseStoreLock(lock2); |
| 6189 |
} |
| 6190 |
}; |
| 6191 |
getEntityRecords2.shouldInvalidate = (action, kind, name) => { |
| 6192 |
return (action.type === "RECEIVE_ITEMS" || action.type === "REMOVE_ITEMS") && action.invalidateCache && kind === action.kind && name === action.name; |
| 6193 |
}; |
| 6194 |
var getEntityRecordsTotalItems2 = forward_resolver_default("getEntityRecords"); |
| 6195 |
var getEntityRecordsTotalPages2 = forward_resolver_default("getEntityRecords"); |
| 6196 |
var getCurrentTheme2 = () => async ({ dispatch: dispatch3, resolveSelect: resolveSelect2 }) => { |
| 6197 |
const activeThemes = await resolveSelect2.getEntityRecords( |
| 6198 |
"root", |
| 6199 |
"theme", |
| 6200 |
{ status: "active" } |
| 6201 |
); |
| 6202 |
dispatch3.receiveCurrentTheme(activeThemes[0]); |
| 6203 |
}; |
| 6204 |
var getThemeSupports2 = forward_resolver_default("getCurrentTheme"); |
| 6205 |
var getEmbedPreview2 = (url) => async ({ dispatch: dispatch3 }) => { |
| 6206 |
try { |
| 6207 |
const embedProxyResponse = await (0, import_api_fetch8.default)({ |
| 6208 |
path: (0, import_url6.addQueryArgs)("/oembed/1.0/proxy", { url }) |
| 6209 |
}); |
| 6210 |
dispatch3.receiveEmbedPreview(url, embedProxyResponse); |
| 6211 |
} catch (error) { |
| 6212 |
dispatch3.receiveEmbedPreview(url, false); |
| 6213 |
} |
| 6214 |
}; |
| 6215 |
var canUser2 = (requestedAction, resource, id) => async ({ dispatch: dispatch3, registry, resolveSelect: resolveSelect2 }) => { |
| 6216 |
if (!ALLOWED_RESOURCE_ACTIONS.includes(requestedAction)) { |
| 6217 |
throw new Error(`'${requestedAction}' is not a valid action.`); |
| 6218 |
} |
| 6219 |
const { hasStartedResolution } = registry.select(STORE_NAME); |
| 6220 |
for (const relatedAction of ALLOWED_RESOURCE_ACTIONS) { |
| 6221 |
if (relatedAction === requestedAction) { |
| 6222 |
continue; |
| 6223 |
} |
| 6224 |
const isAlreadyResolving = hasStartedResolution("canUser", [ |
| 6225 |
relatedAction, |
| 6226 |
resource, |
| 6227 |
id |
| 6228 |
]); |
| 6229 |
if (isAlreadyResolving) { |
| 6230 |
return; |
| 6231 |
} |
| 6232 |
} |
| 6233 |
let resourcePath = null; |
| 6234 |
if (typeof resource === "object") { |
| 6235 |
if (!resource.kind || !resource.name) { |
| 6236 |
throw new Error("The entity resource object is not valid."); |
| 6237 |
} |
| 6238 |
const configs = await resolveSelect2.getEntitiesConfig( |
| 6239 |
resource.kind |
| 6240 |
); |
| 6241 |
const entityConfig = configs.find( |
| 6242 |
(config) => config.name === resource.name && config.kind === resource.kind |
| 6243 |
); |
| 6244 |
if (!entityConfig) { |
| 6245 |
return; |
| 6246 |
} |
| 6247 |
resourcePath = entityConfig.baseURL + (resource.id ? "/" + resource.id : ""); |
| 6248 |
} else { |
| 6249 |
resourcePath = `/wp/v2/${resource}` + (id ? "/" + id : ""); |
| 6250 |
} |
| 6251 |
let response; |
| 6252 |
try { |
| 6253 |
response = await (0, import_api_fetch8.default)({ |
| 6254 |
path: resourcePath, |
| 6255 |
method: "OPTIONS", |
| 6256 |
parse: false |
| 6257 |
}); |
| 6258 |
} catch (error) { |
| 6259 |
return; |
| 6260 |
} |
| 6261 |
const permissions = getUserPermissionsFromAllowHeader( |
| 6262 |
response.headers?.get("allow") |
| 6263 |
); |
| 6264 |
const receiveUserPermissionArgs = {}; |
| 6265 |
const canUserResolutionsArgs = []; |
| 6266 |
for (const action of ALLOWED_RESOURCE_ACTIONS) { |
| 6267 |
receiveUserPermissionArgs[getUserPermissionCacheKey(action, resource, id)] = permissions[action]; |
| 6268 |
if (action !== requestedAction) { |
| 6269 |
canUserResolutionsArgs.push([action, resource, id]); |
| 6270 |
} |
| 6271 |
} |
| 6272 |
registry.batch(() => { |
| 6273 |
dispatch3.receiveUserPermissions(receiveUserPermissionArgs); |
| 6274 |
dispatch3.finishResolutions("canUser", canUserResolutionsArgs); |
| 6275 |
}); |
| 6276 |
}; |
| 6277 |
var canUserEditEntityRecord2 = (kind, name, recordId) => async ({ dispatch: dispatch3 }) => { |
| 6278 |
await dispatch3(canUser2("update", { kind, name, id: recordId })); |
| 6279 |
}; |
| 6280 |
var getAutosaves2 = (postType, postId) => async ({ dispatch: dispatch3, resolveSelect: resolveSelect2 }) => { |
| 6281 |
const { |
| 6282 |
rest_base: restBase, |
| 6283 |
rest_namespace: restNamespace = "wp/v2", |
| 6284 |
supports |
| 6285 |
} = await resolveSelect2.getPostType(postType); |
| 6286 |
if (!supports?.autosave) { |
| 6287 |
return; |
| 6288 |
} |
| 6289 |
const autosaves2 = await (0, import_api_fetch8.default)({ |
| 6290 |
path: `/${restNamespace}/${restBase}/${postId}/autosaves?context=edit` |
| 6291 |
}); |
| 6292 |
if (autosaves2 && autosaves2.length) { |
| 6293 |
dispatch3.receiveAutosaves(postId, autosaves2); |
| 6294 |
} |
| 6295 |
}; |
| 6296 |
var getAutosave2 = (postType, postId) => async ({ resolveSelect: resolveSelect2 }) => { |
| 6297 |
await resolveSelect2.getAutosaves(postType, postId); |
| 6298 |
}; |
| 6299 |
var __experimentalGetCurrentGlobalStylesId2 = () => async ({ dispatch: dispatch3, resolveSelect: resolveSelect2 }) => { |
| 6300 |
const activeThemes = await resolveSelect2.getEntityRecords( |
| 6301 |
"root", |
| 6302 |
"theme", |
| 6303 |
{ status: "active" } |
| 6304 |
); |
| 6305 |
const globalStylesURL = activeThemes?.[0]?._links?.["wp:user-global-styles"]?.[0]?.href; |
| 6306 |
if (!globalStylesURL) { |
| 6307 |
return; |
| 6308 |
} |
| 6309 |
const matches = globalStylesURL.match(/\/(\d+)(?:\?|$)/); |
| 6310 |
const id = matches ? Number(matches[1]) : null; |
| 6311 |
if (id) { |
| 6312 |
dispatch3.__experimentalReceiveCurrentGlobalStylesId(id); |
| 6313 |
} |
| 6314 |
}; |
| 6315 |
var __experimentalGetCurrentThemeBaseGlobalStyles2 = () => async ({ resolveSelect: resolveSelect2, dispatch: dispatch3 }) => { |
| 6316 |
const currentTheme2 = await resolveSelect2.getCurrentTheme(); |
| 6317 |
const themeGlobalStyles = await (0, import_api_fetch8.default)({ |
| 6318 |
path: `/wp/v2/global-styles/themes/${currentTheme2.stylesheet}?context=view` |
| 6319 |
}); |
| 6320 |
dispatch3.__experimentalReceiveThemeBaseGlobalStyles( |
| 6321 |
currentTheme2.stylesheet, |
| 6322 |
themeGlobalStyles |
| 6323 |
); |
| 6324 |
}; |
| 6325 |
var __experimentalGetCurrentThemeGlobalStylesVariations2 = () => async ({ resolveSelect: resolveSelect2, dispatch: dispatch3 }) => { |
| 6326 |
const currentTheme2 = await resolveSelect2.getCurrentTheme(); |
| 6327 |
const variations = await (0, import_api_fetch8.default)({ |
| 6328 |
path: `/wp/v2/global-styles/themes/${currentTheme2.stylesheet}/variations?context=view` |
| 6329 |
}); |
| 6330 |
dispatch3.__experimentalReceiveThemeGlobalStyleVariations( |
| 6331 |
currentTheme2.stylesheet, |
| 6332 |
variations |
| 6333 |
); |
| 6334 |
}; |
| 6335 |
var getCurrentThemeGlobalStylesRevisions2 = () => async ({ resolveSelect: resolveSelect2, dispatch: dispatch3 }) => { |
| 6336 |
const globalStylesId = await resolveSelect2.__experimentalGetCurrentGlobalStylesId(); |
| 6337 |
const record = globalStylesId ? await resolveSelect2.getEntityRecord( |
| 6338 |
"root", |
| 6339 |
"globalStyles", |
| 6340 |
globalStylesId |
| 6341 |
) : void 0; |
| 6342 |
const revisionsURL = record?._links?.["version-history"]?.[0]?.href; |
| 6343 |
if (revisionsURL) { |
| 6344 |
const resetRevisions = await (0, import_api_fetch8.default)({ |
| 6345 |
url: revisionsURL |
| 6346 |
}); |
| 6347 |
const revisions = resetRevisions?.map( |
| 6348 |
(revision) => Object.fromEntries( |
| 6349 |
Object.entries(revision).map(([key, value]) => [ |
| 6350 |
camelCase(key), |
| 6351 |
value |
| 6352 |
]) |
| 6353 |
) |
| 6354 |
); |
| 6355 |
dispatch3.receiveThemeGlobalStyleRevisions( |
| 6356 |
globalStylesId, |
| 6357 |
revisions |
| 6358 |
); |
| 6359 |
} |
| 6360 |
}; |
| 6361 |
getCurrentThemeGlobalStylesRevisions2.shouldInvalidate = (action) => { |
| 6362 |
return action.type === "SAVE_ENTITY_RECORD_FINISH" && action.kind === "root" && !action.error && action.name === "globalStyles"; |
| 6363 |
}; |
| 6364 |
var getBlockPatterns2 = () => async ({ dispatch: dispatch3 }) => { |
| 6365 |
const patterns = await fetchBlockPatterns(); |
| 6366 |
dispatch3({ type: "RECEIVE_BLOCK_PATTERNS", patterns }); |
| 6367 |
}; |
| 6368 |
var getBlockPatternCategories2 = () => async ({ dispatch: dispatch3 }) => { |
| 6369 |
const categories = await (0, import_api_fetch8.default)({ |
| 6370 |
path: "/wp/v2/block-patterns/categories" |
| 6371 |
}); |
| 6372 |
dispatch3({ type: "RECEIVE_BLOCK_PATTERN_CATEGORIES", categories }); |
| 6373 |
}; |
| 6374 |
var getUserPatternCategories2 = () => async ({ dispatch: dispatch3, resolveSelect: resolveSelect2 }) => { |
| 6375 |
const patternCategories = await resolveSelect2.getEntityRecords( |
| 6376 |
"taxonomy", |
| 6377 |
"wp_pattern_category", |
| 6378 |
{ |
| 6379 |
per_page: -1, |
| 6380 |
_fields: "id,name,description,slug", |
| 6381 |
context: "view" |
| 6382 |
} |
| 6383 |
); |
| 6384 |
const mappedPatternCategories = patternCategories?.map((userCategory) => ({ |
| 6385 |
...userCategory, |
| 6386 |
label: (0, import_html_entities2.decodeEntities)(userCategory.name), |
| 6387 |
name: userCategory.slug |
| 6388 |
})) || []; |
| 6389 |
dispatch3({ |
| 6390 |
type: "RECEIVE_USER_PATTERN_CATEGORIES", |
| 6391 |
patternCategories: mappedPatternCategories |
| 6392 |
}); |
| 6393 |
}; |
| 6394 |
var getNavigationFallbackId2 = () => async ({ dispatch: dispatch3, select: select5, registry }) => { |
| 6395 |
const fallback = await (0, import_api_fetch8.default)({ |
| 6396 |
path: (0, import_url6.addQueryArgs)("/wp-block-editor/v1/navigation-fallback", { |
| 6397 |
_embed: true |
| 6398 |
}) |
| 6399 |
}); |
| 6400 |
const record = fallback?._embedded?.self; |
| 6401 |
registry.batch(() => { |
| 6402 |
dispatch3.receiveNavigationFallbackId(fallback?.id); |
| 6403 |
if (!record) { |
| 6404 |
return; |
| 6405 |
} |
| 6406 |
const existingFallbackEntityRecord = select5.getEntityRecord( |
| 6407 |
"postType", |
| 6408 |
"wp_navigation", |
| 6409 |
fallback.id |
| 6410 |
); |
| 6411 |
const invalidateNavigationQueries = !existingFallbackEntityRecord; |
| 6412 |
dispatch3.receiveEntityRecords( |
| 6413 |
"postType", |
| 6414 |
"wp_navigation", |
| 6415 |
record, |
| 6416 |
void 0, |
| 6417 |
invalidateNavigationQueries |
| 6418 |
); |
| 6419 |
dispatch3.finishResolution("getEntityRecord", [ |
| 6420 |
"postType", |
| 6421 |
"wp_navigation", |
| 6422 |
fallback.id |
| 6423 |
]); |
| 6424 |
}); |
| 6425 |
}; |
| 6426 |
var getDefaultTemplateId2 = (query) => async ({ dispatch: dispatch3, registry, resolveSelect: resolveSelect2 }) => { |
| 6427 |
const template = await (0, import_api_fetch8.default)({ |
| 6428 |
path: (0, import_url6.addQueryArgs)("/wp/v2/templates/lookup", query) |
| 6429 |
}); |
| 6430 |
await resolveSelect2.getEntitiesConfig("postType"); |
| 6431 |
const id = window?.__experimentalTemplateActivate ? template?.wp_id || template?.id : template?.id; |
| 6432 |
registry.batch(() => { |
| 6433 |
dispatch3.receiveDefaultTemplateId(query, id || ""); |
| 6434 |
if (id) { |
| 6435 |
template.id = id; |
| 6436 |
dispatch3.receiveEntityRecords( |
| 6437 |
"postType", |
| 6438 |
template.type, |
| 6439 |
template |
| 6440 |
); |
| 6441 |
dispatch3.finishResolution("getEntityRecord", [ |
| 6442 |
"postType", |
| 6443 |
template.type, |
| 6444 |
id |
| 6445 |
]); |
| 6446 |
} |
| 6447 |
}); |
| 6448 |
}; |
| 6449 |
getDefaultTemplateId2.shouldInvalidate = (action) => { |
| 6450 |
return action.type === "RECEIVE_ITEMS" && action.kind === "root" && action.name === "site"; |
| 6451 |
}; |
| 6452 |
var getRevisions2 = (kind, name, recordKey, query = {}) => async ({ dispatch: dispatch3, registry, resolveSelect: resolveSelect2 }) => { |
| 6453 |
const configs = await resolveSelect2.getEntitiesConfig(kind); |
| 6454 |
const entityConfig = configs.find( |
| 6455 |
(config) => config.name === name && config.kind === kind |
| 6456 |
); |
| 6457 |
if (!entityConfig) { |
| 6458 |
return; |
| 6459 |
} |
| 6460 |
const rawQuery = { ...query }; |
| 6461 |
const lock2 = await dispatch3.__unstableAcquireStoreLock( |
| 6462 |
STORE_NAME, |
| 6463 |
["entities", "records", kind, name, recordKey, "revisions"], |
| 6464 |
{ exclusive: false } |
| 6465 |
); |
| 6466 |
try { |
| 6467 |
if (query._fields) { |
| 6468 |
query = { |
| 6469 |
...query, |
| 6470 |
_fields: [ |
| 6471 |
.../* @__PURE__ */ new Set([ |
| 6472 |
...get_normalized_comma_separable_default(query._fields) || [], |
| 6473 |
entityConfig.revisionKey || DEFAULT_ENTITY_KEY |
| 6474 |
]) |
| 6475 |
].join() |
| 6476 |
}; |
| 6477 |
} |
| 6478 |
const path = (0, import_url6.addQueryArgs)( |
| 6479 |
entityConfig.getRevisionsUrl(recordKey), |
| 6480 |
query |
| 6481 |
); |
| 6482 |
let records, response; |
| 6483 |
const meta = {}; |
| 6484 |
const isPaginated = entityConfig.supportsPagination && query.per_page !== -1; |
| 6485 |
try { |
| 6486 |
response = await (0, import_api_fetch8.default)({ path, parse: !isPaginated }); |
| 6487 |
} catch (error) { |
| 6488 |
return; |
| 6489 |
} |
| 6490 |
if (response) { |
| 6491 |
if (isPaginated) { |
| 6492 |
records = Object.values(await response.json()); |
| 6493 |
meta.totalItems = parseInt( |
| 6494 |
response.headers.get("X-WP-Total") |
| 6495 |
); |
| 6496 |
} else { |
| 6497 |
records = Object.values(response); |
| 6498 |
} |
| 6499 |
if (query._fields) { |
| 6500 |
records = records.map((record) => { |
| 6501 |
query._fields.split(",").forEach((field) => { |
| 6502 |
if (!record.hasOwnProperty(field)) { |
| 6503 |
record[field] = void 0; |
| 6504 |
} |
| 6505 |
}); |
| 6506 |
return record; |
| 6507 |
}); |
| 6508 |
} |
| 6509 |
registry.batch(() => { |
| 6510 |
dispatch3.receiveRevisions( |
| 6511 |
kind, |
| 6512 |
name, |
| 6513 |
recordKey, |
| 6514 |
records, |
| 6515 |
query, |
| 6516 |
false, |
| 6517 |
meta |
| 6518 |
); |
| 6519 |
const key = entityConfig.revisionKey || DEFAULT_ENTITY_KEY; |
| 6520 |
const normalizedQuery = normalizeQueryForResolution(rawQuery); |
| 6521 |
const resolutionsArgs = records.filter((record) => record[key]).map((record) => [ |
| 6522 |
kind, |
| 6523 |
name, |
| 6524 |
recordKey, |
| 6525 |
record[key], |
| 6526 |
normalizedQuery |
| 6527 |
]); |
| 6528 |
dispatch3.finishResolutions( |
| 6529 |
"getRevision", |
| 6530 |
resolutionsArgs |
| 6531 |
); |
| 6532 |
}); |
| 6533 |
} |
| 6534 |
} finally { |
| 6535 |
dispatch3.__unstableReleaseStoreLock(lock2); |
| 6536 |
} |
| 6537 |
}; |
| 6538 |
getRevisions2.shouldInvalidate = (action, kind, name, recordKey) => action.type === "SAVE_ENTITY_RECORD_FINISH" && name === action.name && kind === action.kind && !action.error && recordKey === action.recordId; |
| 6539 |
var getRevision2 = (kind, name, recordKey, revisionKey, query) => async ({ select: select5, dispatch: dispatch3, resolveSelect: resolveSelect2 }) => { |
| 6540 |
const configs = await resolveSelect2.getEntitiesConfig(kind); |
| 6541 |
const entityConfig = configs.find( |
| 6542 |
(config) => config.name === name && config.kind === kind |
| 6543 |
); |
| 6544 |
if (!entityConfig) { |
| 6545 |
return; |
| 6546 |
} |
| 6547 |
if (query !== void 0 && query._fields) { |
| 6548 |
query = { |
| 6549 |
...query, |
| 6550 |
_fields: [ |
| 6551 |
.../* @__PURE__ */ new Set([ |
| 6552 |
...get_normalized_comma_separable_default(query._fields) || [], |
| 6553 |
entityConfig.revisionKey || DEFAULT_ENTITY_KEY |
| 6554 |
]) |
| 6555 |
].join() |
| 6556 |
}; |
| 6557 |
} |
| 6558 |
const lock2 = await dispatch3.__unstableAcquireStoreLock( |
| 6559 |
STORE_NAME, |
| 6560 |
[ |
| 6561 |
"entities", |
| 6562 |
"records", |
| 6563 |
kind, |
| 6564 |
name, |
| 6565 |
recordKey, |
| 6566 |
"revisions", |
| 6567 |
revisionKey |
| 6568 |
], |
| 6569 |
{ exclusive: false } |
| 6570 |
); |
| 6571 |
try { |
| 6572 |
if (select5.hasRevision(kind, name, recordKey, revisionKey, query)) { |
| 6573 |
return; |
| 6574 |
} |
| 6575 |
const path = (0, import_url6.addQueryArgs)( |
| 6576 |
entityConfig.getRevisionsUrl(recordKey, revisionKey), |
| 6577 |
query |
| 6578 |
); |
| 6579 |
let record; |
| 6580 |
try { |
| 6581 |
record = await (0, import_api_fetch8.default)({ path }); |
| 6582 |
} catch (error) { |
| 6583 |
return; |
| 6584 |
} |
| 6585 |
if (record) { |
| 6586 |
dispatch3.receiveRevisions( |
| 6587 |
kind, |
| 6588 |
name, |
| 6589 |
recordKey, |
| 6590 |
record, |
| 6591 |
query |
| 6592 |
); |
| 6593 |
} |
| 6594 |
} finally { |
| 6595 |
dispatch3.__unstableReleaseStoreLock(lock2); |
| 6596 |
} |
| 6597 |
}; |
| 6598 |
var getRegisteredPostMeta2 = (postType) => async ({ dispatch: dispatch3, resolveSelect: resolveSelect2 }) => { |
| 6599 |
let options; |
| 6600 |
try { |
| 6601 |
const { |
| 6602 |
rest_namespace: restNamespace = "wp/v2", |
| 6603 |
rest_base: restBase |
| 6604 |
} = await resolveSelect2.getPostType(postType) || {}; |
| 6605 |
options = await (0, import_api_fetch8.default)({ |
| 6606 |
path: `${restNamespace}/${restBase}/?context=edit`, |
| 6607 |
method: "OPTIONS" |
| 6608 |
}); |
| 6609 |
} catch (error) { |
| 6610 |
return; |
| 6611 |
} |
| 6612 |
if (options) { |
| 6613 |
dispatch3.receiveRegisteredPostMeta( |
| 6614 |
postType, |
| 6615 |
options?.schema?.properties?.meta?.properties |
| 6616 |
); |
| 6617 |
} |
| 6618 |
}; |
| 6619 |
var getEntitiesConfig2 = (kind) => async ({ dispatch: dispatch3 }) => { |
| 6620 |
const loader = additionalEntityConfigLoaders.find( |
| 6621 |
(l) => l.kind === kind |
| 6622 |
); |
| 6623 |
if (!loader) { |
| 6624 |
return; |
| 6625 |
} |
| 6626 |
try { |
| 6627 |
const configs = await loader.loadEntities(); |
| 6628 |
if (!configs.length) { |
| 6629 |
return; |
| 6630 |
} |
| 6631 |
dispatch3.addEntities(configs); |
| 6632 |
} catch { |
| 6633 |
} |
| 6634 |
}; |
| 6635 |
var getEditorSettings2 = () => async ({ dispatch: dispatch3 }) => { |
| 6636 |
const settings = await (0, import_api_fetch8.default)({ |
| 6637 |
path: "/wp-block-editor/v1/settings" |
| 6638 |
}); |
| 6639 |
dispatch3.receiveEditorSettings(settings); |
| 6640 |
}; |
| 6641 |
var getEditorAssets2 = () => async ({ dispatch: dispatch3 }) => { |
| 6642 |
const assets = await (0, import_api_fetch8.default)({ |
| 6643 |
path: "/wp-block-editor/v1/assets" |
| 6644 |
}); |
| 6645 |
dispatch3.receiveEditorAssets(assets); |
| 6646 |
}; |
| 6647 |
var getViewConfig2 = (kind, name) => async ({ dispatch: dispatch3 }) => { |
| 6648 |
const config = await (0, import_api_fetch8.default)({ |
| 6649 |
path: (0, import_url6.addQueryArgs)("/wp/v2/view-config", { kind, name }) |
| 6650 |
}); |
| 6651 |
dispatch3.receiveViewConfig(kind, name, config); |
| 6652 |
}; |
| 6653 |
|
| 6654 |
// packages/core-data/build-module/locks/utils.mjs |
| 6655 |
function deepCopyLocksTreePath(tree, path) { |
| 6656 |
const newTree = { ...tree }; |
| 6657 |
let currentNode = newTree; |
| 6658 |
for (const branchName of path) { |
| 6659 |
currentNode.children = { |
| 6660 |
...currentNode.children, |
| 6661 |
[branchName]: { |
| 6662 |
locks: [], |
| 6663 |
children: {}, |
| 6664 |
...currentNode.children[branchName] |
| 6665 |
} |
| 6666 |
}; |
| 6667 |
currentNode = currentNode.children[branchName]; |
| 6668 |
} |
| 6669 |
return newTree; |
| 6670 |
} |
| 6671 |
function getNode(tree, path) { |
| 6672 |
let currentNode = tree; |
| 6673 |
for (const branchName of path) { |
| 6674 |
const nextNode = currentNode.children[branchName]; |
| 6675 |
if (!nextNode) { |
| 6676 |
return null; |
| 6677 |
} |
| 6678 |
currentNode = nextNode; |
| 6679 |
} |
| 6680 |
return currentNode; |
| 6681 |
} |
| 6682 |
function* iteratePath(tree, path) { |
| 6683 |
let currentNode = tree; |
| 6684 |
yield currentNode; |
| 6685 |
for (const branchName of path) { |
| 6686 |
const nextNode = currentNode.children[branchName]; |
| 6687 |
if (!nextNode) { |
| 6688 |
break; |
| 6689 |
} |
| 6690 |
yield nextNode; |
| 6691 |
currentNode = nextNode; |
| 6692 |
} |
| 6693 |
} |
| 6694 |
function* iterateDescendants(node) { |
| 6695 |
const stack = Object.values(node.children); |
| 6696 |
while (stack.length) { |
| 6697 |
const childNode = stack.pop(); |
| 6698 |
yield childNode; |
| 6699 |
stack.push(...Object.values(childNode.children)); |
| 6700 |
} |
| 6701 |
} |
| 6702 |
function hasConflictingLock({ exclusive }, locks2) { |
| 6703 |
if (exclusive && locks2.length) { |
| 6704 |
return true; |
| 6705 |
} |
| 6706 |
if (!exclusive && locks2.filter((lock2) => lock2.exclusive).length) { |
| 6707 |
return true; |
| 6708 |
} |
| 6709 |
return false; |
| 6710 |
} |
| 6711 |
|
| 6712 |
// packages/core-data/build-module/locks/reducer.mjs |
| 6713 |
var DEFAULT_STATE = { |
| 6714 |
requests: [], |
| 6715 |
tree: { |
| 6716 |
locks: [], |
| 6717 |
children: {} |
| 6718 |
} |
| 6719 |
}; |
| 6720 |
function locks(state = DEFAULT_STATE, action) { |
| 6721 |
switch (action.type) { |
| 6722 |
case "ENQUEUE_LOCK_REQUEST": { |
| 6723 |
const { request } = action; |
| 6724 |
return { |
| 6725 |
...state, |
| 6726 |
requests: [request, ...state.requests] |
| 6727 |
}; |
| 6728 |
} |
| 6729 |
case "GRANT_LOCK_REQUEST": { |
| 6730 |
const { lock: lock2, request } = action; |
| 6731 |
const { store: store2, path } = request; |
| 6732 |
const storePath = [store2, ...path]; |
| 6733 |
const newTree = deepCopyLocksTreePath(state.tree, storePath); |
| 6734 |
const node = getNode(newTree, storePath); |
| 6735 |
node.locks = [...node.locks, lock2]; |
| 6736 |
return { |
| 6737 |
...state, |
| 6738 |
requests: state.requests.filter((r) => r !== request), |
| 6739 |
tree: newTree |
| 6740 |
}; |
| 6741 |
} |
| 6742 |
case "RELEASE_LOCK": { |
| 6743 |
const { lock: lock2 } = action; |
| 6744 |
const storePath = [lock2.store, ...lock2.path]; |
| 6745 |
const newTree = deepCopyLocksTreePath(state.tree, storePath); |
| 6746 |
const node = getNode(newTree, storePath); |
| 6747 |
node.locks = node.locks.filter((l) => l !== lock2); |
| 6748 |
return { |
| 6749 |
...state, |
| 6750 |
tree: newTree |
| 6751 |
}; |
| 6752 |
} |
| 6753 |
} |
| 6754 |
return state; |
| 6755 |
} |
| 6756 |
|
| 6757 |
// packages/core-data/build-module/locks/selectors.mjs |
| 6758 |
function getPendingLockRequests(state) { |
| 6759 |
return state.requests; |
| 6760 |
} |
| 6761 |
function isLockAvailable(state, store2, path, { exclusive }) { |
| 6762 |
const storePath = [store2, ...path]; |
| 6763 |
const locks2 = state.tree; |
| 6764 |
for (const node2 of iteratePath(locks2, storePath)) { |
| 6765 |
if (hasConflictingLock({ exclusive }, node2.locks)) { |
| 6766 |
return false; |
| 6767 |
} |
| 6768 |
} |
| 6769 |
const node = getNode(locks2, storePath); |
| 6770 |
if (!node) { |
| 6771 |
return true; |
| 6772 |
} |
| 6773 |
for (const descendant of iterateDescendants(node)) { |
| 6774 |
if (hasConflictingLock({ exclusive }, descendant.locks)) { |
| 6775 |
return false; |
| 6776 |
} |
| 6777 |
} |
| 6778 |
return true; |
| 6779 |
} |
| 6780 |
|
| 6781 |
// packages/core-data/build-module/locks/engine.mjs |
| 6782 |
function createLocks() { |
| 6783 |
let state = locks(void 0, { type: "@@INIT" }); |
| 6784 |
function processPendingLockRequests() { |
| 6785 |
for (const request of getPendingLockRequests(state)) { |
| 6786 |
const { store: store2, path, exclusive, notifyAcquired } = request; |
| 6787 |
if (isLockAvailable(state, store2, path, { exclusive })) { |
| 6788 |
const lock2 = { store: store2, path, exclusive }; |
| 6789 |
state = locks(state, { |
| 6790 |
type: "GRANT_LOCK_REQUEST", |
| 6791 |
lock: lock2, |
| 6792 |
request |
| 6793 |
}); |
| 6794 |
notifyAcquired(lock2); |
| 6795 |
} |
| 6796 |
} |
| 6797 |
} |
| 6798 |
function acquire(store2, path, exclusive) { |
| 6799 |
return new Promise((resolve) => { |
| 6800 |
state = locks(state, { |
| 6801 |
type: "ENQUEUE_LOCK_REQUEST", |
| 6802 |
request: { store: store2, path, exclusive, notifyAcquired: resolve } |
| 6803 |
}); |
| 6804 |
processPendingLockRequests(); |
| 6805 |
}); |
| 6806 |
} |
| 6807 |
function release(lock2) { |
| 6808 |
state = locks(state, { |
| 6809 |
type: "RELEASE_LOCK", |
| 6810 |
lock: lock2 |
| 6811 |
}); |
| 6812 |
processPendingLockRequests(); |
| 6813 |
} |
| 6814 |
return { acquire, release }; |
| 6815 |
} |
| 6816 |
|
| 6817 |
// packages/core-data/build-module/locks/actions.mjs |
| 6818 |
function createLocksActions() { |
| 6819 |
const locks2 = createLocks(); |
| 6820 |
function __unstableAcquireStoreLock(store2, path, { exclusive }) { |
| 6821 |
return () => locks2.acquire(store2, path, exclusive); |
| 6822 |
} |
| 6823 |
function __unstableReleaseStoreLock(lock2) { |
| 6824 |
return () => locks2.release(lock2); |
| 6825 |
} |
| 6826 |
return { __unstableAcquireStoreLock, __unstableReleaseStoreLock }; |
| 6827 |
} |
| 6828 |
|
| 6829 |
// packages/core-data/build-module/dynamic-entities.mjs |
| 6830 |
var dynamicActions; |
| 6831 |
var dynamicSelectors; |
| 6832 |
|
| 6833 |
// packages/core-data/build-module/entity-provider.mjs |
| 6834 |
var import_element2 = __toESM(require_element(), 1); |
| 6835 |
|
| 6836 |
// packages/core-data/build-module/entity-context.mjs |
| 6837 |
var import_element = __toESM(require_element(), 1); |
| 6838 |
var EntityContext = (0, import_element.createContext)({}); |
| 6839 |
EntityContext.displayName = "EntityContext"; |
| 6840 |
|
| 6841 |
// packages/core-data/build-module/entity-provider.mjs |
| 6842 |
var import_jsx_runtime = __toESM(require_jsx_runtime(), 1); |
| 6843 |
function EntityProvider({ |
| 6844 |
kind, |
| 6845 |
type: name, |
| 6846 |
id, |
| 6847 |
revisionId, |
| 6848 |
children |
| 6849 |
}) { |
| 6850 |
const parent = (0, import_element2.useContext)(EntityContext); |
| 6851 |
const childContext = (0, import_element2.useMemo)( |
| 6852 |
() => ({ |
| 6853 |
...parent, |
| 6854 |
...kind && { |
| 6855 |
[kind]: { |
| 6856 |
...parent?.[kind], |
| 6857 |
[name]: id |
| 6858 |
} |
| 6859 |
}, |
| 6860 |
...revisionId !== void 0 && { revisionId } |
| 6861 |
}), |
| 6862 |
[parent, kind, name, id, revisionId] |
| 6863 |
); |
| 6864 |
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(EntityContext.Provider, { value: childContext, children }); |
| 6865 |
} |
| 6866 |
|
| 6867 |
// packages/core-data/build-module/hooks/use-entity-record.mjs |
| 6868 |
var import_data12 = __toESM(require_data(), 1); |
| 6869 |
var import_deprecated4 = __toESM(require_deprecated(), 1); |
| 6870 |
var import_element3 = __toESM(require_element(), 1); |
| 6871 |
|
| 6872 |
// packages/core-data/build-module/hooks/use-query-select.mjs |
| 6873 |
var import_data11 = __toESM(require_data(), 1); |
| 6874 |
|
| 6875 |
// node_modules/memize/dist/index.js |
| 6876 |
function memize(fn, options) { |
| 6877 |
var size = 0; |
| 6878 |
var head; |
| 6879 |
var tail; |
| 6880 |
options = options || {}; |
| 6881 |
function memoized() { |
| 6882 |
var node = head, len = arguments.length, args, i; |
| 6883 |
searchCache: while (node) { |
| 6884 |
if (node.args.length !== arguments.length) { |
| 6885 |
node = node.next; |
| 6886 |
continue; |
| 6887 |
} |
| 6888 |
for (i = 0; i < len; i++) { |
| 6889 |
if (node.args[i] !== arguments[i]) { |
| 6890 |
node = node.next; |
| 6891 |
continue searchCache; |
| 6892 |
} |
| 6893 |
} |
| 6894 |
if (node !== head) { |
| 6895 |
if (node === tail) { |
| 6896 |
tail = node.prev; |
| 6897 |
} |
| 6898 |
node.prev.next = node.next; |
| 6899 |
if (node.next) { |
| 6900 |
node.next.prev = node.prev; |
| 6901 |
} |
| 6902 |
node.next = head; |
| 6903 |
node.prev = null; |
| 6904 |
head.prev = node; |
| 6905 |
head = node; |
| 6906 |
} |
| 6907 |
return node.val; |
| 6908 |
} |
| 6909 |
args = new Array(len); |
| 6910 |
for (i = 0; i < len; i++) { |
| 6911 |
args[i] = arguments[i]; |
| 6912 |
} |
| 6913 |
node = { |
| 6914 |
args, |
| 6915 |
// Generate the result from original function |
| 6916 |
val: fn.apply(null, args) |
| 6917 |
}; |
| 6918 |
if (head) { |
| 6919 |
head.prev = node; |
| 6920 |
node.next = head; |
| 6921 |
} else { |
| 6922 |
tail = node; |
| 6923 |
} |
| 6924 |
if (size === /** @type {MemizeOptions} */ |
| 6925 |
options.maxSize) { |
| 6926 |
tail = /** @type {MemizeCacheNode} */ |
| 6927 |
tail.prev; |
| 6928 |
tail.next = null; |
| 6929 |
} else { |
| 6930 |
size++; |
| 6931 |
} |
| 6932 |
head = node; |
| 6933 |
return node.val; |
| 6934 |
} |
| 6935 |
memoized.clear = function() { |
| 6936 |
head = null; |
| 6937 |
tail = null; |
| 6938 |
size = 0; |
| 6939 |
}; |
| 6940 |
return memoized; |
| 6941 |
} |
| 6942 |
|
| 6943 |
// packages/core-data/build-module/hooks/memoize.mjs |
| 6944 |
var memoize_default = memize; |
| 6945 |
|
| 6946 |
// packages/core-data/build-module/hooks/constants.mjs |
| 6947 |
var Status = /* @__PURE__ */ ((Status2) => { |
| 6948 |
Status2["Idle"] = "IDLE"; |
| 6949 |
Status2["Resolving"] = "RESOLVING"; |
| 6950 |
Status2["Error"] = "ERROR"; |
| 6951 |
Status2["Success"] = "SUCCESS"; |
| 6952 |
return Status2; |
| 6953 |
})(Status || {}); |
| 6954 |
|
| 6955 |
// packages/core-data/build-module/hooks/use-query-select.mjs |
| 6956 |
var META_SELECTORS = [ |
| 6957 |
"getIsResolving", |
| 6958 |
"hasStartedResolution", |
| 6959 |
"hasFinishedResolution", |
| 6960 |
"isResolving", |
| 6961 |
"getCachedResolvers" |
| 6962 |
]; |
| 6963 |
function useQuerySelect(mapQuerySelect, deps) { |
| 6964 |
return (0, import_data11.useSelect)((select5, registry) => { |
| 6965 |
const resolve = (store2) => enrichSelectors(select5(store2)); |
| 6966 |
return mapQuerySelect(resolve, registry); |
| 6967 |
}, deps); |
| 6968 |
} |
| 6969 |
var enrichSelectors = memoize_default(((selectors) => { |
| 6970 |
const resolvers = {}; |
| 6971 |
for (const selectorName in selectors) { |
| 6972 |
if (META_SELECTORS.includes(selectorName)) { |
| 6973 |
continue; |
| 6974 |
} |
| 6975 |
Object.defineProperty(resolvers, selectorName, { |
| 6976 |
get: () => (...args) => { |
| 6977 |
const data = selectors[selectorName](...args); |
| 6978 |
const resolutionStatus = selectors.getResolutionState( |
| 6979 |
selectorName, |
| 6980 |
args |
| 6981 |
)?.status; |
| 6982 |
let status; |
| 6983 |
switch (resolutionStatus) { |
| 6984 |
case "resolving": |
| 6985 |
status = Status.Resolving; |
| 6986 |
break; |
| 6987 |
case "finished": |
| 6988 |
status = Status.Success; |
| 6989 |
break; |
| 6990 |
case "error": |
| 6991 |
status = Status.Error; |
| 6992 |
break; |
| 6993 |
case void 0: |
| 6994 |
status = Status.Idle; |
| 6995 |
break; |
| 6996 |
} |
| 6997 |
return { |
| 6998 |
data, |
| 6999 |
status, |
| 7000 |
isResolving: status === Status.Resolving, |
| 7001 |
hasStarted: status !== Status.Idle, |
| 7002 |
hasResolved: status === Status.Success || status === Status.Error |
| 7003 |
}; |
| 7004 |
} |
| 7005 |
}); |
| 7006 |
} |
| 7007 |
return resolvers; |
| 7008 |
})); |
| 7009 |
|
| 7010 |
// packages/core-data/build-module/hooks/use-entity-record.mjs |
| 7011 |
var EMPTY_OBJECT3 = {}; |
| 7012 |
function useEntityRecord(kind, name, recordId, options = { enabled: true }) { |
| 7013 |
const { editEntityRecord: editEntityRecord2, saveEditedEntityRecord: saveEditedEntityRecord2 } = (0, import_data12.useDispatch)(store); |
| 7014 |
const mutations = (0, import_element3.useMemo)( |
| 7015 |
() => ({ |
| 7016 |
edit: (record2, editOptions = {}) => editEntityRecord2(kind, name, recordId, record2, editOptions), |
| 7017 |
save: (saveOptions = {}) => saveEditedEntityRecord2(kind, name, recordId, { |
| 7018 |
throwOnError: true, |
| 7019 |
...saveOptions |
| 7020 |
}) |
| 7021 |
}), |
| 7022 |
[editEntityRecord2, kind, name, recordId, saveEditedEntityRecord2] |
| 7023 |
); |
| 7024 |
const { editedRecord, hasEdits, edits } = (0, import_data12.useSelect)( |
| 7025 |
(select5) => { |
| 7026 |
if (!options.enabled) { |
| 7027 |
return { |
| 7028 |
editedRecord: EMPTY_OBJECT3, |
| 7029 |
hasEdits: false, |
| 7030 |
edits: EMPTY_OBJECT3 |
| 7031 |
}; |
| 7032 |
} |
| 7033 |
return { |
| 7034 |
editedRecord: select5(store).getEditedEntityRecord( |
| 7035 |
kind, |
| 7036 |
name, |
| 7037 |
recordId |
| 7038 |
), |
| 7039 |
hasEdits: select5(store).hasEditsForEntityRecord( |
| 7040 |
kind, |
| 7041 |
name, |
| 7042 |
recordId |
| 7043 |
), |
| 7044 |
edits: select5(store).getEntityRecordNonTransientEdits( |
| 7045 |
kind, |
| 7046 |
name, |
| 7047 |
recordId |
| 7048 |
) |
| 7049 |
}; |
| 7050 |
}, |
| 7051 |
[kind, name, recordId, options.enabled] |
| 7052 |
); |
| 7053 |
const { data: record, ...querySelectRest } = useQuerySelect( |
| 7054 |
(query) => { |
| 7055 |
if (!options.enabled) { |
| 7056 |
return { |
| 7057 |
data: null |
| 7058 |
}; |
| 7059 |
} |
| 7060 |
return query(store).getEntityRecord(kind, name, recordId); |
| 7061 |
}, |
| 7062 |
[kind, name, recordId, options.enabled] |
| 7063 |
); |
| 7064 |
return { |
| 7065 |
record, |
| 7066 |
editedRecord, |
| 7067 |
hasEdits, |
| 7068 |
edits, |
| 7069 |
...querySelectRest, |
| 7070 |
...mutations |
| 7071 |
}; |
| 7072 |
} |
| 7073 |
function __experimentalUseEntityRecord(kind, name, recordId, options) { |
| 7074 |
(0, import_deprecated4.default)(`wp.data.__experimentalUseEntityRecord`, { |
| 7075 |
alternative: "wp.data.useEntityRecord", |
| 7076 |
since: "6.1" |
| 7077 |
}); |
| 7078 |
return useEntityRecord(kind, name, recordId, options); |
| 7079 |
} |
| 7080 |
|
| 7081 |
// packages/core-data/build-module/hooks/use-entity-records.mjs |
| 7082 |
var import_url7 = __toESM(require_url(), 1); |
| 7083 |
var import_deprecated5 = __toESM(require_deprecated(), 1); |
| 7084 |
var import_data13 = __toESM(require_data(), 1); |
| 7085 |
var import_element4 = __toESM(require_element(), 1); |
| 7086 |
var EMPTY_ARRAY = []; |
| 7087 |
function useEntityRecords(kind, name, queryArgs = {}, options = { enabled: true }) { |
| 7088 |
const queryAsString = (0, import_url7.addQueryArgs)("", queryArgs); |
| 7089 |
const { data: records, ...rest } = useQuerySelect( |
| 7090 |
(query) => { |
| 7091 |
if (!options.enabled) { |
| 7092 |
return { |
| 7093 |
// Avoiding returning a new reference on every execution. |
| 7094 |
data: EMPTY_ARRAY |
| 7095 |
}; |
| 7096 |
} |
| 7097 |
return query(store).getEntityRecords(kind, name, queryArgs); |
| 7098 |
}, |
| 7099 |
[kind, name, queryAsString, options.enabled] |
| 7100 |
); |
| 7101 |
const { totalItems, totalPages } = (0, import_data13.useSelect)( |
| 7102 |
(select5) => { |
| 7103 |
if (!options.enabled) { |
| 7104 |
return { |
| 7105 |
totalItems: null, |
| 7106 |
totalPages: null |
| 7107 |
}; |
| 7108 |
} |
| 7109 |
return { |
| 7110 |
totalItems: select5(store).getEntityRecordsTotalItems( |
| 7111 |
kind, |
| 7112 |
name, |
| 7113 |
queryArgs |
| 7114 |
), |
| 7115 |
totalPages: select5(store).getEntityRecordsTotalPages( |
| 7116 |
kind, |
| 7117 |
name, |
| 7118 |
queryArgs |
| 7119 |
) |
| 7120 |
}; |
| 7121 |
}, |
| 7122 |
[kind, name, queryAsString, options.enabled] |
| 7123 |
); |
| 7124 |
return { |
| 7125 |
records, |
| 7126 |
totalItems, |
| 7127 |
totalPages, |
| 7128 |
...rest |
| 7129 |
}; |
| 7130 |
} |
| 7131 |
function __experimentalUseEntityRecords(kind, name, queryArgs, options) { |
| 7132 |
(0, import_deprecated5.default)(`wp.data.__experimentalUseEntityRecords`, { |
| 7133 |
alternative: "wp.data.useEntityRecords", |
| 7134 |
since: "6.1" |
| 7135 |
}); |
| 7136 |
return useEntityRecords(kind, name, queryArgs, options); |
| 7137 |
} |
| 7138 |
function useEntityRecordsWithPermissions(kind, name, queryArgs = {}, options = { enabled: true }) { |
| 7139 |
const entityConfig = (0, import_data13.useSelect)( |
| 7140 |
(select5) => select5(store).getEntityConfig(kind, name), |
| 7141 |
[kind, name] |
| 7142 |
); |
| 7143 |
const { records: data, ...ret } = useEntityRecords( |
| 7144 |
kind, |
| 7145 |
name, |
| 7146 |
{ |
| 7147 |
...queryArgs, |
| 7148 |
// If _fields is provided, we need to include _links in the request for permission caching to work. |
| 7149 |
...queryArgs._fields ? { |
| 7150 |
_fields: [ |
| 7151 |
.../* @__PURE__ */ new Set([ |
| 7152 |
...get_normalized_comma_separable_default( |
| 7153 |
queryArgs._fields |
| 7154 |
) || [], |
| 7155 |
"_links" |
| 7156 |
]) |
| 7157 |
].join() |
| 7158 |
} : {} |
| 7159 |
}, |
| 7160 |
options |
| 7161 |
); |
| 7162 |
const ids = (0, import_element4.useMemo)( |
| 7163 |
() => data?.map( |
| 7164 |
// @ts-ignore |
| 7165 |
(record) => record[entityConfig?.key ?? "id"] |
| 7166 |
) ?? [], |
| 7167 |
[data, entityConfig?.key] |
| 7168 |
); |
| 7169 |
const permissions = (0, import_data13.useSelect)( |
| 7170 |
(select5) => { |
| 7171 |
const { getEntityRecordsPermissions: getEntityRecordsPermissions2 } = unlock( |
| 7172 |
select5(store) |
| 7173 |
); |
| 7174 |
return getEntityRecordsPermissions2(kind, name, ids); |
| 7175 |
}, |
| 7176 |
[ids, kind, name] |
| 7177 |
); |
| 7178 |
const dataWithPermissions = (0, import_element4.useMemo)( |
| 7179 |
() => data?.map((record, index) => ({ |
| 7180 |
// @ts-ignore |
| 7181 |
...record, |
| 7182 |
permissions: permissions[index] |
| 7183 |
})) ?? [], |
| 7184 |
[data, permissions] |
| 7185 |
); |
| 7186 |
return { records: dataWithPermissions, ...ret }; |
| 7187 |
} |
| 7188 |
|
| 7189 |
// packages/core-data/build-module/hooks/use-resource-permissions.mjs |
| 7190 |
var import_deprecated6 = __toESM(require_deprecated(), 1); |
| 7191 |
var import_warning = __toESM(require_warning(), 1); |
| 7192 |
function useResourcePermissions(resource, id) { |
| 7193 |
const isEntity = typeof resource === "object"; |
| 7194 |
const resourceAsString = isEntity ? JSON.stringify(resource) : resource; |
| 7195 |
if (isEntity && typeof id !== "undefined") { |
| 7196 |
(0, import_warning.default)( |
| 7197 |
`When 'resource' is an entity object, passing 'id' as a separate argument isn't supported.` |
| 7198 |
); |
| 7199 |
} |
| 7200 |
return useQuerySelect( |
| 7201 |
(resolve) => { |
| 7202 |
const hasId = isEntity ? !!resource.id : !!id; |
| 7203 |
const { canUser: canUser3 } = resolve(store); |
| 7204 |
const create3 = canUser3( |
| 7205 |
"create", |
| 7206 |
isEntity ? { kind: resource.kind, name: resource.name } : resource |
| 7207 |
); |
| 7208 |
if (!hasId) { |
| 7209 |
const read2 = canUser3("read", resource); |
| 7210 |
const isResolving2 = create3.isResolving || read2.isResolving; |
| 7211 |
const hasResolved2 = create3.hasResolved && read2.hasResolved; |
| 7212 |
let status2 = Status.Idle; |
| 7213 |
if (isResolving2) { |
| 7214 |
status2 = Status.Resolving; |
| 7215 |
} else if (hasResolved2) { |
| 7216 |
status2 = Status.Success; |
| 7217 |
} |
| 7218 |
return { |
| 7219 |
status: status2, |
| 7220 |
isResolving: isResolving2, |
| 7221 |
hasResolved: hasResolved2, |
| 7222 |
canCreate: create3.hasResolved && create3.data, |
| 7223 |
canRead: read2.hasResolved && read2.data |
| 7224 |
}; |
| 7225 |
} |
| 7226 |
const read = canUser3("read", resource, id); |
| 7227 |
const update = canUser3("update", resource, id); |
| 7228 |
const _delete = canUser3("delete", resource, id); |
| 7229 |
const isResolving = read.isResolving || create3.isResolving || update.isResolving || _delete.isResolving; |
| 7230 |
const hasResolved = read.hasResolved && create3.hasResolved && update.hasResolved && _delete.hasResolved; |
| 7231 |
let status = Status.Idle; |
| 7232 |
if (isResolving) { |
| 7233 |
status = Status.Resolving; |
| 7234 |
} else if (hasResolved) { |
| 7235 |
status = Status.Success; |
| 7236 |
} |
| 7237 |
return { |
| 7238 |
status, |
| 7239 |
isResolving, |
| 7240 |
hasResolved, |
| 7241 |
canRead: hasResolved && read.data, |
| 7242 |
canCreate: hasResolved && create3.data, |
| 7243 |
canUpdate: hasResolved && update.data, |
| 7244 |
canDelete: hasResolved && _delete.data |
| 7245 |
}; |
| 7246 |
}, |
| 7247 |
[resourceAsString, id] |
| 7248 |
); |
| 7249 |
} |
| 7250 |
var use_resource_permissions_default = useResourcePermissions; |
| 7251 |
function __experimentalUseResourcePermissions(resource, id) { |
| 7252 |
(0, import_deprecated6.default)(`wp.data.__experimentalUseResourcePermissions`, { |
| 7253 |
alternative: "wp.data.useResourcePermissions", |
| 7254 |
since: "6.1" |
| 7255 |
}); |
| 7256 |
return useResourcePermissions(resource, id); |
| 7257 |
} |
| 7258 |
|
| 7259 |
// packages/core-data/build-module/hooks/use-entity-block-editor.mjs |
| 7260 |
var import_element6 = __toESM(require_element(), 1); |
| 7261 |
var import_data14 = __toESM(require_data(), 1); |
| 7262 |
var import_blocks5 = __toESM(require_blocks(), 1); |
| 7263 |
|
| 7264 |
// packages/core-data/build-module/hooks/use-entity-id.mjs |
| 7265 |
var import_element5 = __toESM(require_element(), 1); |
| 7266 |
function useEntityId(kind, name) { |
| 7267 |
const context = (0, import_element5.useContext)(EntityContext); |
| 7268 |
return context?.[kind]?.[name]; |
| 7269 |
} |
| 7270 |
|
| 7271 |
// packages/core-data/build-module/footnotes/index.mjs |
| 7272 |
var import_rich_text4 = __toESM(require_rich_text(), 1); |
| 7273 |
|
| 7274 |
// packages/core-data/build-module/footnotes/get-rich-text-values-cached.mjs |
| 7275 |
var import_block_editor5 = __toESM(require_block_editor(), 1); |
| 7276 |
var unlockedApis; |
| 7277 |
var cache = /* @__PURE__ */ new WeakMap(); |
| 7278 |
function getRichTextValuesCached(block) { |
| 7279 |
if (!unlockedApis) { |
| 7280 |
unlockedApis = unlock(import_block_editor5.privateApis); |
| 7281 |
} |
| 7282 |
if (!cache.has(block)) { |
| 7283 |
const values = unlockedApis.getRichTextValues([block]); |
| 7284 |
cache.set(block, values); |
| 7285 |
} |
| 7286 |
return cache.get(block); |
| 7287 |
} |
| 7288 |
|
| 7289 |
// packages/core-data/build-module/footnotes/get-footnotes-order.mjs |
| 7290 |
var cache2 = /* @__PURE__ */ new WeakMap(); |
| 7291 |
function getBlockFootnotesOrder(block) { |
| 7292 |
if (!cache2.has(block)) { |
| 7293 |
const order = []; |
| 7294 |
for (const value of getRichTextValuesCached(block)) { |
| 7295 |
if (!value) { |
| 7296 |
continue; |
| 7297 |
} |
| 7298 |
value.replacements.forEach(({ type, attributes }) => { |
| 7299 |
if (type === "core/footnote") { |
| 7300 |
order.push(attributes["data-fn"]); |
| 7301 |
} |
| 7302 |
}); |
| 7303 |
} |
| 7304 |
cache2.set(block, order); |
| 7305 |
} |
| 7306 |
return cache2.get(block); |
| 7307 |
} |
| 7308 |
function getFootnotesOrder(blocks) { |
| 7309 |
return blocks.flatMap(getBlockFootnotesOrder); |
| 7310 |
} |
| 7311 |
|
| 7312 |
// packages/core-data/build-module/footnotes/index.mjs |
| 7313 |
var oldFootnotes = {}; |
| 7314 |
function updateFootnotesFromMeta(blocks, meta) { |
| 7315 |
const output = { blocks }; |
| 7316 |
if (!meta) { |
| 7317 |
return output; |
| 7318 |
} |
| 7319 |
if (meta.footnotes === void 0) { |
| 7320 |
return output; |
| 7321 |
} |
| 7322 |
const newOrder = getFootnotesOrder(blocks); |
| 7323 |
const footnotes = meta.footnotes ? JSON.parse(meta.footnotes) : []; |
| 7324 |
const currentOrder = footnotes.map((fn) => fn.id); |
| 7325 |
if (currentOrder.join("") === newOrder.join("")) { |
| 7326 |
return output; |
| 7327 |
} |
| 7328 |
const newFootnotes = newOrder.map( |
| 7329 |
(fnId) => footnotes.find((fn) => fn.id === fnId) || oldFootnotes[fnId] || { |
| 7330 |
id: fnId, |
| 7331 |
content: "" |
| 7332 |
} |
| 7333 |
); |
| 7334 |
function updateAttributes(attributes) { |
| 7335 |
if (!attributes || Array.isArray(attributes) || typeof attributes !== "object") { |
| 7336 |
return attributes; |
| 7337 |
} |
| 7338 |
attributes = { ...attributes }; |
| 7339 |
for (const key in attributes) { |
| 7340 |
const value = attributes[key]; |
| 7341 |
if (Array.isArray(value)) { |
| 7342 |
attributes[key] = value.map(updateAttributes); |
| 7343 |
continue; |
| 7344 |
} |
| 7345 |
if (typeof value !== "string" && !(value instanceof import_rich_text4.RichTextData)) { |
| 7346 |
continue; |
| 7347 |
} |
| 7348 |
const richTextValue = typeof value === "string" ? import_rich_text4.RichTextData.fromHTMLString(value) : new import_rich_text4.RichTextData(value); |
| 7349 |
let hasFootnotes = false; |
| 7350 |
richTextValue.replacements.forEach((replacement) => { |
| 7351 |
if (replacement.type === "core/footnote") { |
| 7352 |
const id = replacement.attributes["data-fn"]; |
| 7353 |
const index = newOrder.indexOf(id); |
| 7354 |
const countValue = (0, import_rich_text4.create)({ |
| 7355 |
html: replacement.innerHTML |
| 7356 |
}); |
| 7357 |
countValue.text = String(index + 1); |
| 7358 |
countValue.formats = Array.from( |
| 7359 |
{ length: countValue.text.length }, |
| 7360 |
() => countValue.formats[0] |
| 7361 |
); |
| 7362 |
countValue.replacements = Array.from( |
| 7363 |
{ length: countValue.text.length }, |
| 7364 |
() => countValue.replacements[0] |
| 7365 |
); |
| 7366 |
replacement.innerHTML = (0, import_rich_text4.toHTMLString)({ |
| 7367 |
value: countValue |
| 7368 |
}); |
| 7369 |
hasFootnotes = true; |
| 7370 |
} |
| 7371 |
}); |
| 7372 |
if (hasFootnotes) { |
| 7373 |
attributes[key] = typeof value === "string" ? richTextValue.toHTMLString() : richTextValue; |
| 7374 |
} |
| 7375 |
} |
| 7376 |
return attributes; |
| 7377 |
} |
| 7378 |
function updateBlocksAttributes(__blocks) { |
| 7379 |
return __blocks.map((block) => { |
| 7380 |
return { |
| 7381 |
...block, |
| 7382 |
attributes: updateAttributes(block.attributes), |
| 7383 |
innerBlocks: updateBlocksAttributes(block.innerBlocks) |
| 7384 |
}; |
| 7385 |
}); |
| 7386 |
} |
| 7387 |
const newBlocks = updateBlocksAttributes(blocks); |
| 7388 |
oldFootnotes = { |
| 7389 |
...oldFootnotes, |
| 7390 |
...footnotes.reduce((acc, fn) => { |
| 7391 |
if (!newOrder.includes(fn.id)) { |
| 7392 |
acc[fn.id] = fn; |
| 7393 |
} |
| 7394 |
return acc; |
| 7395 |
}, {}) |
| 7396 |
}; |
| 7397 |
return { |
| 7398 |
meta: { |
| 7399 |
...meta, |
| 7400 |
footnotes: JSON.stringify(newFootnotes) |
| 7401 |
}, |
| 7402 |
blocks: newBlocks |
| 7403 |
}; |
| 7404 |
} |
| 7405 |
|
| 7406 |
// packages/core-data/build-module/hooks/use-entity-block-editor.mjs |
| 7407 |
var EMPTY_ARRAY2 = []; |
| 7408 |
var parsedBlocksCache = /* @__PURE__ */ new Map(); |
| 7409 |
function useEntityBlockEditor(kind, name, { id: _id } = {}) { |
| 7410 |
const providerId = useEntityId(kind, name); |
| 7411 |
const id = _id ?? providerId; |
| 7412 |
const { content, editedBlocks, meta } = (0, import_data14.useSelect)( |
| 7413 |
(select5) => { |
| 7414 |
if (!id) { |
| 7415 |
return {}; |
| 7416 |
} |
| 7417 |
const { getEditedEntityRecord: getEditedEntityRecord3 } = select5(STORE_NAME); |
| 7418 |
const editedRecord = getEditedEntityRecord3(kind, name, id); |
| 7419 |
return { |
| 7420 |
editedBlocks: editedRecord.blocks, |
| 7421 |
content: editedRecord.content, |
| 7422 |
meta: editedRecord.meta |
| 7423 |
}; |
| 7424 |
}, |
| 7425 |
[kind, name, id] |
| 7426 |
); |
| 7427 |
const { __unstableCreateUndoLevel: __unstableCreateUndoLevel2, editEntityRecord: editEntityRecord2 } = (0, import_data14.useDispatch)(STORE_NAME); |
| 7428 |
const blocks = (0, import_element6.useMemo)(() => { |
| 7429 |
if (!id) { |
| 7430 |
return void 0; |
| 7431 |
} |
| 7432 |
if (editedBlocks) { |
| 7433 |
return editedBlocks; |
| 7434 |
} |
| 7435 |
if (!content || typeof content !== "string") { |
| 7436 |
return EMPTY_ARRAY2; |
| 7437 |
} |
| 7438 |
const cacheKey = `${kind}:${name}:${id}`; |
| 7439 |
const cached = parsedBlocksCache.get(cacheKey); |
| 7440 |
let _blocks; |
| 7441 |
if (cached && cached.content === content) { |
| 7442 |
_blocks = cached.blocks; |
| 7443 |
} else { |
| 7444 |
_blocks = (0, import_blocks5.parse)(content); |
| 7445 |
parsedBlocksCache.set(cacheKey, { content, blocks: _blocks }); |
| 7446 |
} |
| 7447 |
return _blocks; |
| 7448 |
}, [kind, name, id, editedBlocks, content]); |
| 7449 |
const onChange = (0, import_element6.useCallback)( |
| 7450 |
(newBlocks, options) => { |
| 7451 |
const noChange = blocks === newBlocks; |
| 7452 |
if (noChange) { |
| 7453 |
return __unstableCreateUndoLevel2(kind, name, id); |
| 7454 |
} |
| 7455 |
const { selection, ...rest } = options; |
| 7456 |
const edits = { |
| 7457 |
selection, |
| 7458 |
content: ({ blocks: blocksForSerialization = [] }) => (0, import_blocks5.__unstableSerializeAndClean)(blocksForSerialization), |
| 7459 |
...updateFootnotesFromMeta(newBlocks, meta) |
| 7460 |
}; |
| 7461 |
editEntityRecord2(kind, name, id, edits, { |
| 7462 |
isCached: false, |
| 7463 |
...rest |
| 7464 |
}); |
| 7465 |
}, |
| 7466 |
[ |
| 7467 |
kind, |
| 7468 |
name, |
| 7469 |
id, |
| 7470 |
blocks, |
| 7471 |
meta, |
| 7472 |
__unstableCreateUndoLevel2, |
| 7473 |
editEntityRecord2 |
| 7474 |
] |
| 7475 |
); |
| 7476 |
const onInput = (0, import_element6.useCallback)( |
| 7477 |
(newBlocks, options) => { |
| 7478 |
const { selection, ...rest } = options; |
| 7479 |
const edits = { |
| 7480 |
selection, |
| 7481 |
...updateFootnotesFromMeta(newBlocks, meta) |
| 7482 |
}; |
| 7483 |
editEntityRecord2(kind, name, id, edits, { |
| 7484 |
isCached: true, |
| 7485 |
...rest |
| 7486 |
}); |
| 7487 |
}, |
| 7488 |
[kind, name, id, meta, editEntityRecord2] |
| 7489 |
); |
| 7490 |
return [blocks, onInput, onChange]; |
| 7491 |
} |
| 7492 |
|
| 7493 |
// packages/core-data/build-module/hooks/use-entity-prop.mjs |
| 7494 |
var import_element7 = __toESM(require_element(), 1); |
| 7495 |
var import_data15 = __toESM(require_data(), 1); |
| 7496 |
function useEntityProp(kind, name, prop, _id) { |
| 7497 |
const providerId = useEntityId(kind, name); |
| 7498 |
const id = _id ?? providerId; |
| 7499 |
const context = (0, import_element7.useContext)(EntityContext); |
| 7500 |
const revisionId = context?.revisionId; |
| 7501 |
const { value, fullValue } = (0, import_data15.useSelect)( |
| 7502 |
(select5) => { |
| 7503 |
if (revisionId) { |
| 7504 |
const revisions = select5(STORE_NAME).getRevisions( |
| 7505 |
kind, |
| 7506 |
name, |
| 7507 |
id, |
| 7508 |
{ |
| 7509 |
per_page: -1, |
| 7510 |
context: "edit", |
| 7511 |
_fields: "id,date,author,meta,title.raw,excerpt.raw,content.raw" |
| 7512 |
} |
| 7513 |
); |
| 7514 |
const entityConfig = select5(STORE_NAME).getEntityConfig( |
| 7515 |
kind, |
| 7516 |
name |
| 7517 |
); |
| 7518 |
const revKey = entityConfig?.revisionKey || DEFAULT_ENTITY_KEY; |
| 7519 |
const revision = revisions?.find( |
| 7520 |
(r) => r[revKey] === revisionId |
| 7521 |
); |
| 7522 |
return revision ? { |
| 7523 |
value: revision[prop], |
| 7524 |
fullValue: revision[prop] |
| 7525 |
} : {}; |
| 7526 |
} |
| 7527 |
const { getEntityRecord: getEntityRecord3, getEditedEntityRecord: getEditedEntityRecord3 } = select5(STORE_NAME); |
| 7528 |
const record = getEntityRecord3(kind, name, id); |
| 7529 |
const editedRecord = getEditedEntityRecord3(kind, name, id); |
| 7530 |
return record && editedRecord ? { |
| 7531 |
value: editedRecord[prop], |
| 7532 |
fullValue: record[prop] |
| 7533 |
} : {}; |
| 7534 |
}, |
| 7535 |
[kind, name, id, prop, revisionId] |
| 7536 |
); |
| 7537 |
const { editEntityRecord: editEntityRecord2 } = (0, import_data15.useDispatch)(STORE_NAME); |
| 7538 |
const setValue = (0, import_element7.useCallback)( |
| 7539 |
(newValue) => { |
| 7540 |
if (revisionId) { |
| 7541 |
return; |
| 7542 |
} |
| 7543 |
editEntityRecord2(kind, name, id, { |
| 7544 |
[prop]: newValue |
| 7545 |
}); |
| 7546 |
}, |
| 7547 |
[editEntityRecord2, kind, name, id, prop, revisionId] |
| 7548 |
); |
| 7549 |
return [value, setValue, fullValue]; |
| 7550 |
} |
| 7551 |
|
| 7552 |
// packages/core-data/build-module/hooks/use-post-editor-awareness-state.mjs |
| 7553 |
var import_compose3 = __toESM(require_compose(), 1); |
| 7554 |
var import_element8 = __toESM(require_element(), 1); |
| 7555 |
var defaultResolvedSelection = { |
| 7556 |
richTextOffset: null, |
| 7557 |
localClientId: null |
| 7558 |
}; |
| 7559 |
var defaultState = { |
| 7560 |
activeCollaborators: [], |
| 7561 |
resolveSelection: () => defaultResolvedSelection, |
| 7562 |
getDebugData: () => ({ |
| 7563 |
doc: {}, |
| 7564 |
clients: {}, |
| 7565 |
collaboratorMap: {} |
| 7566 |
}), |
| 7567 |
isCurrentCollaboratorDisconnected: false |
| 7568 |
}; |
| 7569 |
function getAwarenessState(awareness, newState) { |
| 7570 |
const activeCollaborators = newState ?? awareness.getCurrentState(); |
| 7571 |
return { |
| 7572 |
activeCollaborators, |
| 7573 |
resolveSelection: (selection) => awareness.convertSelectionStateToAbsolute(selection), |
| 7574 |
getDebugData: () => awareness.getDebugData(), |
| 7575 |
isCurrentCollaboratorDisconnected: activeCollaborators.find((collaborator) => collaborator.isMe)?.isConnected === false |
| 7576 |
}; |
| 7577 |
} |
| 7578 |
function usePostEditorAwarenessState(postId, postType) { |
| 7579 |
const [state, setState] = (0, import_element8.useState)(defaultState); |
| 7580 |
(0, import_element8.useEffect)(() => { |
| 7581 |
if (null === postId || null === postType) { |
| 7582 |
setState(defaultState); |
| 7583 |
return; |
| 7584 |
} |
| 7585 |
const objectType = `postType/${postType}`; |
| 7586 |
const objectId = postId.toString(); |
| 7587 |
const awareness = getSyncManager()?.getAwareness( |
| 7588 |
objectType, |
| 7589 |
objectId |
| 7590 |
); |
| 7591 |
if (!awareness) { |
| 7592 |
setState(defaultState); |
| 7593 |
return; |
| 7594 |
} |
| 7595 |
awareness.setUp(); |
| 7596 |
setState(getAwarenessState(awareness)); |
| 7597 |
const unsubscribe = awareness?.onStateChange( |
| 7598 |
(newState) => { |
| 7599 |
setState(getAwarenessState(awareness, newState)); |
| 7600 |
} |
| 7601 |
); |
| 7602 |
return unsubscribe; |
| 7603 |
}, [postId, postType]); |
| 7604 |
return state; |
| 7605 |
} |
| 7606 |
function useActiveCollaborators(postId, postType) { |
| 7607 |
return usePostEditorAwarenessState(postId, postType).activeCollaborators; |
| 7608 |
} |
| 7609 |
function useResolvedSelection(postId, postType) { |
| 7610 |
return usePostEditorAwarenessState(postId, postType).resolveSelection; |
| 7611 |
} |
| 7612 |
function useLastPostSave(postId, postType) { |
| 7613 |
const [lastSave, setLastSave] = (0, import_element8.useState)(null); |
| 7614 |
(0, import_element8.useEffect)(() => { |
| 7615 |
if (null === postId || null === postType) { |
| 7616 |
setLastSave(null); |
| 7617 |
return; |
| 7618 |
} |
| 7619 |
const awareness = getSyncManager()?.getAwareness( |
| 7620 |
`postType/${postType}`, |
| 7621 |
postId.toString() |
| 7622 |
); |
| 7623 |
if (!awareness) { |
| 7624 |
setLastSave(null); |
| 7625 |
return; |
| 7626 |
} |
| 7627 |
awareness.setUp(); |
| 7628 |
const stateMap = awareness.doc.getMap("state"); |
| 7629 |
const recordMap = awareness.doc.getMap("document"); |
| 7630 |
const setupTime = Date.now(); |
| 7631 |
const observer = (event) => { |
| 7632 |
if (event.keysChanged.has("savedAt")) { |
| 7633 |
const savedAt = stateMap.get("savedAt"); |
| 7634 |
const savedByClientId = stateMap.get("savedBy"); |
| 7635 |
if (typeof savedAt === "number" && typeof savedByClientId === "number" && savedAt > setupTime) { |
| 7636 |
const postStatus = recordMap.get("status"); |
| 7637 |
setLastSave({ savedAt, savedByClientId, postStatus }); |
| 7638 |
} |
| 7639 |
} |
| 7640 |
}; |
| 7641 |
stateMap.observe(observer); |
| 7642 |
return () => { |
| 7643 |
stateMap.unobserve(observer); |
| 7644 |
}; |
| 7645 |
}, [postId, postType]); |
| 7646 |
return lastSave; |
| 7647 |
} |
| 7648 |
function useOnCollaboratorJoin(postId, postType, callback) { |
| 7649 |
const { activeCollaborators } = usePostEditorAwarenessState( |
| 7650 |
postId, |
| 7651 |
postType |
| 7652 |
); |
| 7653 |
const prevCollaborators = (0, import_compose3.usePrevious)(activeCollaborators); |
| 7654 |
(0, import_element8.useEffect)(() => { |
| 7655 |
if (!prevCollaborators || prevCollaborators.length === 0) { |
| 7656 |
return; |
| 7657 |
} |
| 7658 |
const prevMap = new Map( |
| 7659 |
prevCollaborators.map((collaborator) => [ |
| 7660 |
collaborator.clientId, |
| 7661 |
collaborator |
| 7662 |
]) |
| 7663 |
); |
| 7664 |
const me = activeCollaborators.find( |
| 7665 |
(collaborator) => collaborator.isMe |
| 7666 |
); |
| 7667 |
for (const collaborator of activeCollaborators) { |
| 7668 |
if (!prevMap.has(collaborator.clientId) && !collaborator.isMe) { |
| 7669 |
callback(collaborator, me); |
| 7670 |
} |
| 7671 |
} |
| 7672 |
}, [activeCollaborators, prevCollaborators, callback]); |
| 7673 |
} |
| 7674 |
function useOnCollaboratorLeave(postId, postType, callback) { |
| 7675 |
const { activeCollaborators } = usePostEditorAwarenessState( |
| 7676 |
postId, |
| 7677 |
postType |
| 7678 |
); |
| 7679 |
const prevCollaborators = (0, import_compose3.usePrevious)(activeCollaborators); |
| 7680 |
(0, import_element8.useEffect)(() => { |
| 7681 |
if (!prevCollaborators || prevCollaborators.length === 0) { |
| 7682 |
return; |
| 7683 |
} |
| 7684 |
const newMap = new Map( |
| 7685 |
activeCollaborators.map((collaborator) => [ |
| 7686 |
collaborator.clientId, |
| 7687 |
collaborator |
| 7688 |
]) |
| 7689 |
); |
| 7690 |
for (const prevCollab of prevCollaborators) { |
| 7691 |
if (prevCollab.isMe || !prevCollab.isConnected) { |
| 7692 |
continue; |
| 7693 |
} |
| 7694 |
const newCollab = newMap.get(prevCollab.clientId); |
| 7695 |
if (!newCollab?.isConnected) { |
| 7696 |
callback(prevCollab); |
| 7697 |
} |
| 7698 |
} |
| 7699 |
}, [activeCollaborators, prevCollaborators, callback]); |
| 7700 |
} |
| 7701 |
function useOnPostSave(postId, postType, callback) { |
| 7702 |
const { activeCollaborators } = usePostEditorAwarenessState( |
| 7703 |
postId, |
| 7704 |
postType |
| 7705 |
); |
| 7706 |
const lastPostSave = useLastPostSave(postId, postType); |
| 7707 |
const prevPostSave = (0, import_compose3.usePrevious)(lastPostSave); |
| 7708 |
(0, import_element8.useEffect)(() => { |
| 7709 |
if (!lastPostSave) { |
| 7710 |
return; |
| 7711 |
} |
| 7712 |
if (prevPostSave && lastPostSave.savedAt === prevPostSave.savedAt) { |
| 7713 |
return; |
| 7714 |
} |
| 7715 |
const saver = activeCollaborators.find( |
| 7716 |
(collaborator) => collaborator.clientId === lastPostSave.savedByClientId && !collaborator.isMe |
| 7717 |
); |
| 7718 |
if (!saver) { |
| 7719 |
return; |
| 7720 |
} |
| 7721 |
callback(lastPostSave, saver, prevPostSave ?? null); |
| 7722 |
}, [lastPostSave, prevPostSave, activeCollaborators, callback]); |
| 7723 |
} |
| 7724 |
|
| 7725 |
// packages/core-data/build-module/private-apis.mjs |
| 7726 |
var privateApis = {}; |
| 7727 |
lock(privateApis, { |
| 7728 |
useEntityRecordsWithPermissions, |
| 7729 |
RECEIVE_INTERMEDIATE_RESULTS, |
| 7730 |
retrySyncConnection, |
| 7731 |
useActiveCollaborators, |
| 7732 |
useResolvedSelection, |
| 7733 |
useOnCollaboratorJoin, |
| 7734 |
useOnCollaboratorLeave, |
| 7735 |
useOnPostSave |
| 7736 |
}); |
| 7737 |
|
| 7738 |
// packages/core-data/build-module/index.mjs |
| 7739 |
var entitiesConfig2 = [ |
| 7740 |
...rootEntitiesConfig, |
| 7741 |
...additionalEntityConfigLoaders.filter((config) => !!config.name) |
| 7742 |
]; |
| 7743 |
var entitySelectors = entitiesConfig2.reduce((result, entity2) => { |
| 7744 |
const { kind, name, plural } = entity2; |
| 7745 |
const getEntityRecordMethodName = getMethodName(kind, name); |
| 7746 |
result[getEntityRecordMethodName] = (state, key, query) => { |
| 7747 |
logEntityDeprecation(kind, name, getEntityRecordMethodName, { |
| 7748 |
isShorthandSelector: true, |
| 7749 |
alternativeFunctionName: "getEntityRecord" |
| 7750 |
}); |
| 7751 |
return getEntityRecord(state, kind, name, key, query); |
| 7752 |
}; |
| 7753 |
if (plural) { |
| 7754 |
const getEntityRecordsMethodName = getMethodName(kind, plural, "get"); |
| 7755 |
result[getEntityRecordsMethodName] = (state, query) => { |
| 7756 |
logEntityDeprecation(kind, name, getEntityRecordsMethodName, { |
| 7757 |
isShorthandSelector: true, |
| 7758 |
alternativeFunctionName: "getEntityRecords" |
| 7759 |
}); |
| 7760 |
return getEntityRecords(state, kind, name, query); |
| 7761 |
}; |
| 7762 |
} |
| 7763 |
return result; |
| 7764 |
}, {}); |
| 7765 |
var entityResolvers = entitiesConfig2.reduce((result, entity2) => { |
| 7766 |
const { kind, name, plural } = entity2; |
| 7767 |
const getEntityRecordMethodName = getMethodName(kind, name); |
| 7768 |
result[getEntityRecordMethodName] = (key, query) => { |
| 7769 |
logEntityDeprecation(kind, name, getEntityRecordMethodName, { |
| 7770 |
isShorthandSelector: true, |
| 7771 |
alternativeFunctionName: "getEntityRecord" |
| 7772 |
}); |
| 7773 |
return getEntityRecord2(kind, name, key, query); |
| 7774 |
}; |
| 7775 |
if (plural) { |
| 7776 |
const getEntityRecordsMethodName = getMethodName(kind, plural, "get"); |
| 7777 |
result[getEntityRecordsMethodName] = (...args) => { |
| 7778 |
logEntityDeprecation(kind, plural, getEntityRecordsMethodName, { |
| 7779 |
isShorthandSelector: true, |
| 7780 |
alternativeFunctionName: "getEntityRecords" |
| 7781 |
}); |
| 7782 |
return getEntityRecords2(kind, name, ...args); |
| 7783 |
}; |
| 7784 |
result[getEntityRecordsMethodName].shouldInvalidate = (action) => getEntityRecords2.shouldInvalidate(action, kind, name); |
| 7785 |
} |
| 7786 |
return result; |
| 7787 |
}, {}); |
| 7788 |
var entityActions = entitiesConfig2.reduce((result, entity2) => { |
| 7789 |
const { kind, name } = entity2; |
| 7790 |
const saveEntityRecordMethodName = getMethodName(kind, name, "save"); |
| 7791 |
result[saveEntityRecordMethodName] = (record, options) => { |
| 7792 |
logEntityDeprecation(kind, name, saveEntityRecordMethodName, { |
| 7793 |
isShorthandSelector: true, |
| 7794 |
alternativeFunctionName: "saveEntityRecord" |
| 7795 |
}); |
| 7796 |
return saveEntityRecord(kind, name, record, options); |
| 7797 |
}; |
| 7798 |
const deleteEntityRecordMethodName = getMethodName(kind, name, "delete"); |
| 7799 |
result[deleteEntityRecordMethodName] = (key, query, options) => { |
| 7800 |
logEntityDeprecation(kind, name, deleteEntityRecordMethodName, { |
| 7801 |
isShorthandSelector: true, |
| 7802 |
alternativeFunctionName: "deleteEntityRecord" |
| 7803 |
}); |
| 7804 |
return deleteEntityRecord(kind, name, key, query, options); |
| 7805 |
}; |
| 7806 |
return result; |
| 7807 |
}, {}); |
| 7808 |
var storeConfig = () => ({ |
| 7809 |
reducer: reducer_default2, |
| 7810 |
actions: { |
| 7811 |
...dynamicActions, |
| 7812 |
...actions_exports, |
| 7813 |
...entityActions, |
| 7814 |
...createLocksActions() |
| 7815 |
}, |
| 7816 |
selectors: { |
| 7817 |
...dynamicSelectors, |
| 7818 |
...selectors_exports, |
| 7819 |
...entitySelectors |
| 7820 |
}, |
| 7821 |
resolvers: { ...resolvers_exports, ...entityResolvers } |
| 7822 |
}); |
| 7823 |
var store = (0, import_data16.createReduxStore)(STORE_NAME, storeConfig()); |
| 7824 |
unlock(store).registerPrivateSelectors(private_selectors_exports); |
| 7825 |
unlock(store).registerPrivateActions(private_actions_exports); |
| 7826 |
(0, import_data16.register)(store); |
| 7827 |
return __toCommonJS(index_exports); |
| 7828 |
})(); |
| 7829 |
//# sourceMappingURL=index.js.map |
| 7830 |
|