@wordpress/core-data 7.41.2-next.v.202603161435.0 → 7.42.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/build/awareness/post-editor-awareness.cjs +12 -5
- package/build/awareness/post-editor-awareness.cjs.map +2 -2
- package/build/entities.cjs +30 -5
- package/build/entities.cjs.map +2 -2
- package/build/hooks/use-post-editor-awareness-state.cjs +1 -1
- package/build/hooks/use-post-editor-awareness-state.cjs.map +2 -2
- package/build/queried-data/get-query-parts.cjs +7 -0
- package/build/queried-data/get-query-parts.cjs.map +2 -2
- package/build/queried-data/selectors.cjs +14 -3
- package/build/queried-data/selectors.cjs.map +2 -2
- package/build/reducer.cjs +6 -0
- package/build/reducer.cjs.map +2 -2
- package/build/sync.cjs +3 -0
- package/build/sync.cjs.map +2 -2
- package/build/types.cjs.map +2 -2
- package/build/utils/block-selection-history.cjs +1 -1
- package/build/utils/block-selection-history.cjs.map +2 -2
- package/build/utils/crdt-blocks.cjs +17 -3
- package/build/utils/crdt-blocks.cjs.map +2 -2
- package/build/utils/crdt-selection.cjs +4 -1
- package/build/utils/crdt-selection.cjs.map +2 -2
- package/build/utils/crdt-user-selections.cjs +1 -1
- package/build/utils/crdt-user-selections.cjs.map +2 -2
- package/build/utils/crdt-utils.cjs +54 -2
- package/build/utils/crdt-utils.cjs.map +2 -2
- package/build/utils/crdt.cjs +4 -23
- package/build/utils/crdt.cjs.map +2 -2
- package/build-module/awareness/post-editor-awareness.mjs +12 -5
- package/build-module/awareness/post-editor-awareness.mjs.map +2 -2
- package/build-module/entities.mjs +30 -5
- package/build-module/entities.mjs.map +2 -2
- package/build-module/hooks/use-post-editor-awareness-state.mjs +1 -1
- package/build-module/hooks/use-post-editor-awareness-state.mjs.map +2 -2
- package/build-module/queried-data/get-query-parts.mjs +7 -0
- package/build-module/queried-data/get-query-parts.mjs.map +2 -2
- package/build-module/queried-data/selectors.mjs +14 -3
- package/build-module/queried-data/selectors.mjs.map +2 -2
- package/build-module/reducer.mjs +6 -0
- package/build-module/reducer.mjs.map +2 -2
- package/build-module/sync.mjs +2 -0
- package/build-module/sync.mjs.map +2 -2
- package/build-module/types.mjs.map +2 -2
- package/build-module/utils/block-selection-history.mjs +5 -2
- package/build-module/utils/block-selection-history.mjs.map +2 -2
- package/build-module/utils/crdt-blocks.mjs +17 -3
- package/build-module/utils/crdt-blocks.mjs.map +2 -2
- package/build-module/utils/crdt-selection.mjs +8 -2
- package/build-module/utils/crdt-selection.mjs.map +2 -2
- package/build-module/utils/crdt-user-selections.mjs +2 -2
- package/build-module/utils/crdt-user-selections.mjs.map +2 -2
- package/build-module/utils/crdt-utils.mjs +51 -1
- package/build-module/utils/crdt-utils.mjs.map +2 -2
- package/build-module/utils/crdt.mjs +4 -23
- package/build-module/utils/crdt.mjs.map +2 -2
- package/build-types/awareness/post-editor-awareness.d.ts +2 -2
- package/build-types/awareness/post-editor-awareness.d.ts.map +1 -1
- package/build-types/entities.d.ts.map +1 -1
- package/build-types/queried-data/get-query-parts.d.ts +7 -0
- package/build-types/queried-data/get-query-parts.d.ts.map +1 -1
- package/build-types/queried-data/selectors.d.ts.map +1 -1
- package/build-types/reducer.d.ts.map +1 -1
- package/build-types/sync.d.ts +2 -2
- package/build-types/sync.d.ts.map +1 -1
- package/build-types/types.d.ts +4 -2
- package/build-types/types.d.ts.map +1 -1
- package/build-types/utils/block-selection-history.d.ts.map +1 -1
- package/build-types/utils/crdt-blocks.d.ts.map +1 -1
- package/build-types/utils/crdt-selection.d.ts.map +1 -1
- package/build-types/utils/crdt-user-selections.d.ts +1 -2
- package/build-types/utils/crdt-user-selections.d.ts.map +1 -1
- package/build-types/utils/crdt-utils.d.ts +20 -0
- package/build-types/utils/crdt-utils.d.ts.map +1 -1
- package/build-types/utils/crdt.d.ts +6 -7
- package/build-types/utils/crdt.d.ts.map +1 -1
- package/build-types/utils/test/crdt-utils.d.ts +2 -0
- package/build-types/utils/test/crdt-utils.d.ts.map +1 -0
- package/package.json +18 -18
- package/src/awareness/post-editor-awareness.ts +13 -6
- package/src/awareness/test/post-editor-awareness.ts +15 -10
- package/src/entities.js +36 -5
- package/src/hooks/test/use-post-editor-awareness-state.ts +3 -3
- package/src/hooks/use-post-editor-awareness-state.ts +1 -1
- package/src/queried-data/get-query-parts.js +13 -0
- package/src/queried-data/selectors.js +22 -4
- package/src/queried-data/test/get-query-parts.js +34 -0
- package/src/queried-data/test/selectors.js +158 -0
- package/src/reducer.js +11 -0
- package/src/sync.ts +2 -0
- package/src/test/entities.js +185 -1
- package/src/types.ts +8 -2
- package/src/utils/block-selection-history.ts +5 -2
- package/src/utils/crdt-blocks.ts +32 -3
- package/src/utils/crdt-selection.ts +8 -2
- package/src/utils/crdt-user-selections.ts +13 -13
- package/src/utils/crdt-utils.ts +99 -0
- package/src/utils/crdt.ts +8 -30
- package/src/utils/test/crdt-blocks.ts +146 -0
- package/src/utils/test/crdt-user-selections.ts +5 -0
- package/src/utils/test/crdt-utils.ts +387 -0
- package/src/utils/test/crdt.ts +120 -53
package/CHANGELOG.md
CHANGED
|
@@ -30,6 +30,7 @@ var import_base_awareness = require("./base-awareness.cjs");
|
|
|
30
30
|
var import_block_lookup = require("./block-lookup.cjs");
|
|
31
31
|
var import_config = require("./config.cjs");
|
|
32
32
|
var import_name = require("../name.cjs");
|
|
33
|
+
var import_crdt_utils = require("../utils/crdt-utils.cjs");
|
|
33
34
|
var import_crdt_user_selections = require("../utils/crdt-user-selections.cjs");
|
|
34
35
|
var import_types = require("../types.cjs");
|
|
35
36
|
var PostEditorAwareness = class extends import_base_awareness.BaseAwarenessState {
|
|
@@ -160,11 +161,11 @@ var PostEditorAwareness = class extends import_base_awareness.BaseAwarenessState
|
|
|
160
161
|
* clientIds (e.g. in "Show Template" mode where blocks are cloned).
|
|
161
162
|
*
|
|
162
163
|
* @param selection - The selection state.
|
|
163
|
-
* @return The text
|
|
164
|
+
* @return The rich-text offset and block client ID, or nulls if not resolvable.
|
|
164
165
|
*/
|
|
165
166
|
convertSelectionStateToAbsolute(selection) {
|
|
166
167
|
if (selection.type === import_crdt_user_selections.SelectionType.None) {
|
|
167
|
-
return {
|
|
168
|
+
return { richTextOffset: null, localClientId: null };
|
|
168
169
|
}
|
|
169
170
|
if (selection.type === import_crdt_user_selections.SelectionType.WholeBlock) {
|
|
170
171
|
const absolutePos = import_sync.Y.createAbsolutePositionFromRelativePosition(
|
|
@@ -180,7 +181,7 @@ var PostEditorAwareness = class extends import_base_awareness.BaseAwarenessState
|
|
|
180
181
|
localClientId2 = path2 ? (0, import_block_lookup.resolveBlockClientIdByPath)(path2) : null;
|
|
181
182
|
}
|
|
182
183
|
}
|
|
183
|
-
return {
|
|
184
|
+
return { richTextOffset: null, localClientId: localClientId2 };
|
|
184
185
|
}
|
|
185
186
|
const cursorPos = "cursorPosition" in selection ? selection.cursorPosition : selection.cursorStartPosition;
|
|
186
187
|
const absolutePosition = import_sync.Y.createAbsolutePositionFromRelativePosition(
|
|
@@ -188,12 +189,18 @@ var PostEditorAwareness = class extends import_base_awareness.BaseAwarenessState
|
|
|
188
189
|
this.doc
|
|
189
190
|
);
|
|
190
191
|
if (!absolutePosition) {
|
|
191
|
-
return {
|
|
192
|
+
return { richTextOffset: null, localClientId: null };
|
|
192
193
|
}
|
|
193
194
|
const yType = absolutePosition.type.parent?.parent;
|
|
194
195
|
const path = yType instanceof import_sync.Y.Map ? (0, import_block_lookup.getBlockPathInYdoc)(yType) : null;
|
|
195
196
|
const localClientId = path ? (0, import_block_lookup.resolveBlockClientIdByPath)(path) : null;
|
|
196
|
-
return {
|
|
197
|
+
return {
|
|
198
|
+
richTextOffset: (0, import_crdt_utils.htmlIndexToRichTextOffset)(
|
|
199
|
+
absolutePosition.type.toString(),
|
|
200
|
+
absolutePosition.index
|
|
201
|
+
),
|
|
202
|
+
localClientId
|
|
203
|
+
};
|
|
197
204
|
}
|
|
198
205
|
/**
|
|
199
206
|
* Type guard to check if a struct is a Y.Item (not Y.GC)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/awareness/post-editor-awareness.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { dispatch, select, subscribe } from '@wordpress/data';\nimport { Y } from '@wordpress/sync';\n// @ts-ignore No exported types for block editor store selectors.\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport { BaseAwarenessState, baseEqualityFieldChecks } from './base-awareness';\nimport { getBlockPathInYdoc, resolveBlockClientIdByPath } from './block-lookup';\nimport {\n\tAWARENESS_CURSOR_UPDATE_THROTTLE_IN_MS,\n\tLOCAL_CURSOR_UPDATE_DEBOUNCE_IN_MS,\n} from './config';\nimport { STORE_NAME as coreStore } from '../name';\nimport {\n\tareSelectionsStatesEqual,\n\tgetSelectionState,\n\tSelectionType,\n} from '../utils/crdt-user-selections';\n\nimport { SelectionDirection } from '../types';\nimport type { SelectionState, WPBlockSelection } from '../types';\nimport type { YBlocks } from '../utils/crdt-blocks';\nimport type {\n\tDebugCollaboratorData,\n\tEditorState,\n\tPostEditorState,\n\tSerializableYItem,\n\tYDocDebugData,\n} from './types';\n\nexport class PostEditorAwareness extends BaseAwarenessState< PostEditorState > {\n\tprotected equalityFieldChecks = {\n\t\t...baseEqualityFieldChecks,\n\t\teditorState: this.areEditorStatesEqual,\n\t};\n\n\tpublic constructor(\n\t\tdoc: Y.Doc,\n\t\tprivate kind: string,\n\t\tprivate name: string,\n\t\tprivate postId: number\n\t) {\n\t\tsuper( doc );\n\t}\n\n\tprotected onSetUp(): void {\n\t\tsuper.onSetUp();\n\n\t\tthis.subscribeToCollaboratorSelectionChanges();\n\t}\n\n\t/**\n\t * Subscribe to collaborator selection changes and update the selection state.\n\t */\n\tprivate subscribeToCollaboratorSelectionChanges(): void {\n\t\tconst {\n\t\t\tgetSelectionStart,\n\t\t\tgetSelectionEnd,\n\t\t\tgetSelectedBlocksInitialCaretPosition,\n\t\t} = select( blockEditorStore );\n\n\t\t// Keep track of the current selection in the outer scope so we can compare\n\t\t// in the subscription.\n\t\tlet selectionStart = getSelectionStart();\n\t\tlet selectionEnd = getSelectionEnd();\n\t\tlet localCursorTimeout: NodeJS.Timeout | null = null;\n\n\t\t// During rapid selection changes (e.g. undo restoring content and\n\t\t// selection), the debounce discards intermediate events. If we use the\n\t\t// last intermediate state instead of the overall change it can produce\n\t\t// the wrong direction.\n\t\t// Use selectionBeforeDebounce to capture the selection state from\n\t\t// before the debounce window so that direction is computed across the\n\t\t// full window when it fires.\n\t\tlet selectionBeforeDebounce: {\n\t\t\tstart: WPBlockSelection;\n\t\t\tend: WPBlockSelection;\n\t\t} | null = null;\n\n\t\tsubscribe( () => {\n\t\t\tconst newSelectionStart = getSelectionStart();\n\t\t\tconst newSelectionEnd = getSelectionEnd();\n\n\t\t\tif (\n\t\t\t\tnewSelectionStart === selectionStart &&\n\t\t\t\tnewSelectionEnd === selectionEnd\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// On the first change of a debounce window, snapshot the state\n\t\t\t// we're moving away from.\n\t\t\tif ( ! selectionBeforeDebounce ) {\n\t\t\t\tselectionBeforeDebounce = {\n\t\t\t\t\tstart: selectionStart,\n\t\t\t\t\tend: selectionEnd,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tselectionStart = newSelectionStart;\n\t\t\tselectionEnd = newSelectionEnd;\n\n\t\t\t// Typically selection position is only persisted after typing in a block, which\n\t\t\t// can cause selection position to be reset by other users making block updates.\n\t\t\t// Ensure we update the controlled selection right away, persisting our cursor position locally.\n\t\t\tconst initialPosition = getSelectedBlocksInitialCaretPosition();\n\t\t\tvoid this.updateSelectionInEntityRecord(\n\t\t\t\tselectionStart,\n\t\t\t\tselectionEnd,\n\t\t\t\tinitialPosition\n\t\t\t);\n\n\t\t\t// We receive two selection changes in quick succession\n\t\t\t// from local selection events:\n\t\t\t// { clientId: \"123...\", attributeKey: \"content\", offset: undefined }\n\t\t\t// { clientId: \"123...\", attributeKey: \"content\", offset: 554 }\n\t\t\t// Add a short debounce to avoid sending the first selection change.\n\t\t\tif ( localCursorTimeout ) {\n\t\t\t\tclearTimeout( localCursorTimeout );\n\t\t\t}\n\n\t\t\tlocalCursorTimeout = setTimeout( () => {\n\t\t\t\t// Compute direction across the full debounce window.\n\t\t\t\tconst selectionStateOptions: {\n\t\t\t\t\tselectionDirection?: SelectionDirection;\n\t\t\t\t} = {};\n\n\t\t\t\tif ( selectionBeforeDebounce ) {\n\t\t\t\t\tselectionStateOptions.selectionDirection =\n\t\t\t\t\t\tdetectSelectionDirection(\n\t\t\t\t\t\t\tselectionBeforeDebounce.start,\n\t\t\t\t\t\t\tselectionBeforeDebounce.end,\n\t\t\t\t\t\t\tselectionStart,\n\t\t\t\t\t\t\tselectionEnd\n\t\t\t\t\t\t);\n\n\t\t\t\t\t// Reset debounced selection state.\n\t\t\t\t\tselectionBeforeDebounce = null;\n\t\t\t\t}\n\n\t\t\t\tconst selectionState = getSelectionState(\n\t\t\t\t\tselectionStart,\n\t\t\t\t\tselectionEnd,\n\t\t\t\t\tthis.doc,\n\t\t\t\t\tselectionStateOptions\n\t\t\t\t);\n\n\t\t\t\tthis.setThrottledLocalStateField(\n\t\t\t\t\t'editorState',\n\t\t\t\t\t{ selection: selectionState },\n\t\t\t\t\tAWARENESS_CURSOR_UPDATE_THROTTLE_IN_MS\n\t\t\t\t);\n\t\t\t}, LOCAL_CURSOR_UPDATE_DEBOUNCE_IN_MS );\n\t\t} );\n\t}\n\n\t/**\n\t * Update the entity record with the current collaborator's selection.\n\t *\n\t * @param selectionStart - The start position of the selection.\n\t * @param selectionEnd - The end position of the selection.\n\t * @param initialPosition - The initial position of the selection.\n\t */\n\tprivate async updateSelectionInEntityRecord(\n\t\tselectionStart: WPBlockSelection,\n\t\tselectionEnd: WPBlockSelection,\n\t\tinitialPosition: number | null\n\t): Promise< void > {\n\t\t// Send an entityRecord `selection` update if we have a selection.\n\t\t//\n\t\t// Normally WordPress updates the `selection` property of the post when changes are made to blocks.\n\t\t// In a multi-user setup, block changes can occur from other users. When an entity is updated from another\n\t\t// user's changes, useBlockSync() in Gutenberg will reset the user's selection to the last saved selection.\n\t\t//\n\t\t// Manually adding an edit for each movement ensures that other user's changes to the document will\n\t\t// not cause the local user's selection to reset to the last local change location.\n\t\tconst edits = {\n\t\t\tselection: { selectionStart, selectionEnd, initialPosition },\n\t\t};\n\n\t\tconst options = {\n\t\t\tundoIgnore: true,\n\t\t};\n\n\t\t// @ts-ignore Types are not provided when using store name instead of store instance.\n\t\tdispatch( coreStore ).editEntityRecord(\n\t\t\tthis.kind,\n\t\t\tthis.name,\n\t\t\tthis.postId,\n\t\t\tedits,\n\t\t\toptions\n\t\t);\n\t}\n\n\t/**\n\t * Check if two editor states are equal.\n\t *\n\t * @param state1 - The first editor state.\n\t * @param state2 - The second editor state.\n\t * @return True if the editor states are equal, false otherwise.\n\t */\n\tprivate areEditorStatesEqual(\n\t\tstate1?: EditorState,\n\t\tstate2?: EditorState\n\t): boolean {\n\t\tif ( ! state1 || ! state2 ) {\n\t\t\treturn state1 === state2;\n\t\t}\n\n\t\tif ( ! state1.selection || ! state2.selection ) {\n\t\t\treturn state1.selection === state2.selection;\n\t\t}\n\n\t\treturn areSelectionsStatesEqual( state1.selection, state2.selection );\n\t}\n\n\t/**\n\t * Resolve a selection state to a text index and block client ID.\n\t *\n\t * For text-based selections, navigates up from the resolved Y.Text via\n\t * AbstractType.parent to find the containing block, then resolves the\n\t * local clientId via the block's tree path.\n\t * For WholeBlock selections, resolves the block's relative position and\n\t * then finds the local clientId via tree path.\n\t *\n\t * Tree-path resolution is used instead of reading the clientId directly\n\t * from the Yjs block because the local block-editor store may use different\n\t * clientIds (e.g. in \"Show Template\" mode where blocks are cloned).\n\t *\n\t * @param selection - The selection state.\n\t * @return The text index and block client ID, or nulls if not resolvable.\n\t */\n\tpublic convertSelectionStateToAbsolute( selection: SelectionState ): {\n\t\ttextIndex: number | null;\n\t\tlocalClientId: string | null;\n\t} {\n\t\tif ( selection.type === SelectionType.None ) {\n\t\t\treturn { textIndex: null, localClientId: null };\n\t\t}\n\n\t\tif ( selection.type === SelectionType.WholeBlock ) {\n\t\t\tconst absolutePos = Y.createAbsolutePositionFromRelativePosition(\n\t\t\t\tselection.blockPosition,\n\t\t\t\tthis.doc\n\t\t\t);\n\n\t\t\tlet localClientId: string | null = null;\n\n\t\t\tif ( absolutePos && absolutePos.type instanceof Y.Array ) {\n\t\t\t\tconst parentArray = absolutePos.type as YBlocks;\n\t\t\t\tconst block = parentArray.get( absolutePos.index );\n\n\t\t\t\tif ( block instanceof Y.Map ) {\n\t\t\t\t\tconst path = getBlockPathInYdoc( block );\n\t\t\t\t\tlocalClientId = path\n\t\t\t\t\t\t? resolveBlockClientIdByPath( path )\n\t\t\t\t\t\t: null;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn { textIndex: null, localClientId };\n\t\t}\n\n\t\t// Text-based selections: resolve cursor position and navigate up.\n\t\tconst cursorPos =\n\t\t\t'cursorPosition' in selection\n\t\t\t\t? selection.cursorPosition\n\t\t\t\t: selection.cursorStartPosition;\n\n\t\tconst absolutePosition = Y.createAbsolutePositionFromRelativePosition(\n\t\t\tcursorPos.relativePosition,\n\t\t\tthis.doc\n\t\t);\n\n\t\tif ( ! absolutePosition ) {\n\t\t\treturn { textIndex: null, localClientId: null };\n\t\t}\n\n\t\t// Navigate up: Y.Text -> attributes Y.Map -> block Y.Map\n\t\tconst yType = absolutePosition.type.parent?.parent;\n\t\tconst path =\n\t\t\tyType instanceof Y.Map ? getBlockPathInYdoc( yType ) : null;\n\t\tconst localClientId = path ? resolveBlockClientIdByPath( path ) : null;\n\n\t\treturn { textIndex: absolutePosition.index, localClientId };\n\t}\n\n\t/**\n\t * Type guard to check if a struct is a Y.Item (not Y.GC)\n\t * @param struct - The struct to check.\n\t * @return True if the struct is a Y.Item, false otherwise.\n\t */\n\tprivate isYItem( struct: Y.Item | Y.GC ): struct is Y.Item {\n\t\treturn 'content' in struct;\n\t}\n\n\t/**\n\t * Get data for debugging, using the awareness state.\n\t *\n\t * @return {YDocDebugData} The debug data.\n\t */\n\tpublic getDebugData(): YDocDebugData {\n\t\tconst ydoc = this.doc;\n\n\t\t// Manually extract doc data to avoid deprecated toJSON method\n\t\tconst docData: Record< string, unknown > = Object.fromEntries(\n\t\t\tArray.from( ydoc.share, ( [ key, value ] ) => [\n\t\t\t\tkey,\n\t\t\t\tvalue.toJSON(),\n\t\t\t] )\n\t\t);\n\n\t\t// Build collaboratorMap from awareness store (all collaborators seen this session)\n\t\tconst collaboratorMapData = new Map< string, DebugCollaboratorData >(\n\t\t\tArray.from( this.getSeenStates().entries() ).map(\n\t\t\t\t( [ clientId, collaboratorState ] ) => [\n\t\t\t\t\tString( clientId ),\n\t\t\t\t\t{\n\t\t\t\t\t\tname: collaboratorState.collaboratorInfo.name,\n\t\t\t\t\t\twpUserId: collaboratorState.collaboratorInfo.id,\n\t\t\t\t\t},\n\t\t\t\t]\n\t\t\t)\n\t\t);\n\n\t\t// Serialize Yjs client items to avoid deep nesting\n\t\tconst serializableClientItems: Record<\n\t\t\tnumber,\n\t\t\tArray< SerializableYItem >\n\t\t> = {};\n\n\t\tydoc.store.clients.forEach( ( structs, clientId ) => {\n\t\t\t// Filter for Y.Item only (skip Y.GC garbage collection structs)\n\t\t\tconst items = structs.filter( this.isYItem );\n\n\t\t\tserializableClientItems[ clientId ] = items.map( ( item ) => {\n\t\t\t\tconst { left, right, ...rest } = item;\n\n\t\t\t\treturn {\n\t\t\t\t\t...rest,\n\t\t\t\t\tleft: left\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\tid: left.id,\n\t\t\t\t\t\t\t\tlength: left.length,\n\t\t\t\t\t\t\t\torigin: left.origin,\n\t\t\t\t\t\t\t\tcontent: left.content,\n\t\t\t\t\t\t }\n\t\t\t\t\t\t: null,\n\t\t\t\t\tright: right\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\tid: right.id,\n\t\t\t\t\t\t\t\tlength: right.length,\n\t\t\t\t\t\t\t\torigin: right.origin,\n\t\t\t\t\t\t\t\tcontent: right.content,\n\t\t\t\t\t\t }\n\t\t\t\t\t\t: null,\n\t\t\t\t};\n\t\t\t} );\n\t\t} );\n\n\t\treturn {\n\t\t\tdoc: docData,\n\t\t\tclients: serializableClientItems,\n\t\t\tcollaboratorMap: Object.fromEntries( collaboratorMapData ),\n\t\t};\n\t}\n}\n\n/**\n * Detect the direction of a selection change by comparing old and new edges.\n *\n * When the user extends a selection backward (e.g. Shift+Left), the\n * selectionStart edge moves while selectionEnd stays fixed, so the caret\n * is at the start. The reverse is true for forward extension.\n *\n * @param prevStart - The previous selectionStart.\n * @param prevEnd - The previous selectionEnd.\n * @param newStart - The new selectionStart.\n * @param newEnd - The new selectionEnd.\n * @return The detected direction, defaulting to Forward when indeterminate.\n */\nfunction detectSelectionDirection(\n\tprevStart: WPBlockSelection,\n\tprevEnd: WPBlockSelection,\n\tnewStart: WPBlockSelection,\n\tnewEnd: WPBlockSelection\n): SelectionDirection {\n\tconst startMoved = ! areBlockSelectionsEqual( prevStart, newStart );\n\tconst endMoved = ! areBlockSelectionsEqual( prevEnd, newEnd );\n\n\tif ( startMoved && ! endMoved ) {\n\t\treturn SelectionDirection.Backward;\n\t}\n\n\treturn SelectionDirection.Forward;\n}\n\n/**\n * Compare two WPBlockSelection objects by value.\n *\n * @param a - First selection.\n * @param b - Second selection.\n * @return True if all fields are equal.\n */\nfunction areBlockSelectionsEqual(\n\ta: WPBlockSelection,\n\tb: WPBlockSelection\n): boolean {\n\treturn (\n\t\ta.clientId === b.clientId &&\n\t\ta.attributeKey === b.attributeKey &&\n\t\ta.offset === b.offset\n\t);\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA4C;AAC5C,kBAAkB;AAElB,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A,UACA,KAAK;AAAA,UACL;AAAA,QACD;AAEA,aAAK;AAAA,UACJ;AAAA,UACA,EAAE,WAAW,eAAe;AAAA,UAC5B;AAAA,QACD;AAAA,MACD,GAAG,gDAAmC;AAAA,IACvC,CAAE;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAc,8BACb,gBACA,cACA,iBACkB;AASlB,UAAM,QAAQ;AAAA,MACb,WAAW,EAAE,gBAAgB,cAAc,gBAAgB;AAAA,IAC5D;AAEA,UAAM,UAAU;AAAA,MACf,YAAY;AAAA,IACb;AAGA,8BAAU,YAAAC,UAAU,EAAE;AAAA,MACrB,KAAK;AAAA,MACL,KAAK;AAAA,MACL,KAAK;AAAA,MACL;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,qBACP,QACA,QACU;AACV,QAAK,CAAE,UAAU,CAAE,QAAS;AAC3B,aAAO,WAAW;AAAA,IACnB;AAEA,QAAK,CAAE,OAAO,aAAa,CAAE,OAAO,WAAY;AAC/C,aAAO,OAAO,cAAc,OAAO;AAAA,IACpC;AAEA,eAAO,sDAA0B,OAAO,WAAW,OAAO,SAAU;AAAA,EACrE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBO,gCAAiC,WAGtC;AACD,QAAK,UAAU,SAAS,0CAAc,MAAO;AAC5C,aAAO,EAAE,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { dispatch, select, subscribe } from '@wordpress/data';\nimport { Y } from '@wordpress/sync';\n// @ts-ignore No exported types for block editor store selectors.\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport { BaseAwarenessState, baseEqualityFieldChecks } from './base-awareness';\nimport { getBlockPathInYdoc, resolveBlockClientIdByPath } from './block-lookup';\nimport {\n\tAWARENESS_CURSOR_UPDATE_THROTTLE_IN_MS,\n\tLOCAL_CURSOR_UPDATE_DEBOUNCE_IN_MS,\n} from './config';\nimport { STORE_NAME as coreStore } from '../name';\nimport { htmlIndexToRichTextOffset } from '../utils/crdt-utils';\nimport {\n\tareSelectionsStatesEqual,\n\tgetSelectionState,\n\tSelectionType,\n} from '../utils/crdt-user-selections';\n\nimport { SelectionDirection } from '../types';\nimport type { SelectionState, WPBlockSelection } from '../types';\nimport type { YBlocks } from '../utils/crdt-blocks';\nimport type {\n\tDebugCollaboratorData,\n\tEditorState,\n\tPostEditorState,\n\tSerializableYItem,\n\tYDocDebugData,\n} from './types';\n\nexport class PostEditorAwareness extends BaseAwarenessState< PostEditorState > {\n\tprotected equalityFieldChecks = {\n\t\t...baseEqualityFieldChecks,\n\t\teditorState: this.areEditorStatesEqual,\n\t};\n\n\tpublic constructor(\n\t\tdoc: Y.Doc,\n\t\tprivate kind: string,\n\t\tprivate name: string,\n\t\tprivate postId: number\n\t) {\n\t\tsuper( doc );\n\t}\n\n\tprotected onSetUp(): void {\n\t\tsuper.onSetUp();\n\n\t\tthis.subscribeToCollaboratorSelectionChanges();\n\t}\n\n\t/**\n\t * Subscribe to collaborator selection changes and update the selection state.\n\t */\n\tprivate subscribeToCollaboratorSelectionChanges(): void {\n\t\tconst {\n\t\t\tgetSelectionStart,\n\t\t\tgetSelectionEnd,\n\t\t\tgetSelectedBlocksInitialCaretPosition,\n\t\t} = select( blockEditorStore );\n\n\t\t// Keep track of the current selection in the outer scope so we can compare\n\t\t// in the subscription.\n\t\tlet selectionStart = getSelectionStart();\n\t\tlet selectionEnd = getSelectionEnd();\n\t\tlet localCursorTimeout: NodeJS.Timeout | null = null;\n\n\t\t// During rapid selection changes (e.g. undo restoring content and\n\t\t// selection), the debounce discards intermediate events. If we use the\n\t\t// last intermediate state instead of the overall change it can produce\n\t\t// the wrong direction.\n\t\t// Use selectionBeforeDebounce to capture the selection state from\n\t\t// before the debounce window so that direction is computed across the\n\t\t// full window when it fires.\n\t\tlet selectionBeforeDebounce: {\n\t\t\tstart: WPBlockSelection;\n\t\t\tend: WPBlockSelection;\n\t\t} | null = null;\n\n\t\tsubscribe( () => {\n\t\t\tconst newSelectionStart = getSelectionStart();\n\t\t\tconst newSelectionEnd = getSelectionEnd();\n\n\t\t\tif (\n\t\t\t\tnewSelectionStart === selectionStart &&\n\t\t\t\tnewSelectionEnd === selectionEnd\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// On the first change of a debounce window, snapshot the state\n\t\t\t// we're moving away from.\n\t\t\tif ( ! selectionBeforeDebounce ) {\n\t\t\t\tselectionBeforeDebounce = {\n\t\t\t\t\tstart: selectionStart,\n\t\t\t\t\tend: selectionEnd,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tselectionStart = newSelectionStart;\n\t\t\tselectionEnd = newSelectionEnd;\n\n\t\t\t// Typically selection position is only persisted after typing in a block, which\n\t\t\t// can cause selection position to be reset by other users making block updates.\n\t\t\t// Ensure we update the controlled selection right away, persisting our cursor position locally.\n\t\t\tconst initialPosition = getSelectedBlocksInitialCaretPosition();\n\t\t\tvoid this.updateSelectionInEntityRecord(\n\t\t\t\tselectionStart,\n\t\t\t\tselectionEnd,\n\t\t\t\tinitialPosition\n\t\t\t);\n\n\t\t\t// We receive two selection changes in quick succession\n\t\t\t// from local selection events:\n\t\t\t// { clientId: \"123...\", attributeKey: \"content\", offset: undefined }\n\t\t\t// { clientId: \"123...\", attributeKey: \"content\", offset: 554 }\n\t\t\t// Add a short debounce to avoid sending the first selection change.\n\t\t\tif ( localCursorTimeout ) {\n\t\t\t\tclearTimeout( localCursorTimeout );\n\t\t\t}\n\n\t\t\tlocalCursorTimeout = setTimeout( () => {\n\t\t\t\t// Compute direction across the full debounce window.\n\t\t\t\tconst selectionStateOptions: {\n\t\t\t\t\tselectionDirection?: SelectionDirection;\n\t\t\t\t} = {};\n\n\t\t\t\tif ( selectionBeforeDebounce ) {\n\t\t\t\t\tselectionStateOptions.selectionDirection =\n\t\t\t\t\t\tdetectSelectionDirection(\n\t\t\t\t\t\t\tselectionBeforeDebounce.start,\n\t\t\t\t\t\t\tselectionBeforeDebounce.end,\n\t\t\t\t\t\t\tselectionStart,\n\t\t\t\t\t\t\tselectionEnd\n\t\t\t\t\t\t);\n\n\t\t\t\t\t// Reset debounced selection state.\n\t\t\t\t\tselectionBeforeDebounce = null;\n\t\t\t\t}\n\n\t\t\t\tconst selectionState = getSelectionState(\n\t\t\t\t\tselectionStart,\n\t\t\t\t\tselectionEnd,\n\t\t\t\t\tthis.doc,\n\t\t\t\t\tselectionStateOptions\n\t\t\t\t);\n\n\t\t\t\tthis.setThrottledLocalStateField(\n\t\t\t\t\t'editorState',\n\t\t\t\t\t{ selection: selectionState },\n\t\t\t\t\tAWARENESS_CURSOR_UPDATE_THROTTLE_IN_MS\n\t\t\t\t);\n\t\t\t}, LOCAL_CURSOR_UPDATE_DEBOUNCE_IN_MS );\n\t\t} );\n\t}\n\n\t/**\n\t * Update the entity record with the current collaborator's selection.\n\t *\n\t * @param selectionStart - The start position of the selection.\n\t * @param selectionEnd - The end position of the selection.\n\t * @param initialPosition - The initial position of the selection.\n\t */\n\tprivate async updateSelectionInEntityRecord(\n\t\tselectionStart: WPBlockSelection,\n\t\tselectionEnd: WPBlockSelection,\n\t\tinitialPosition: number | null\n\t): Promise< void > {\n\t\t// Send an entityRecord `selection` update if we have a selection.\n\t\t//\n\t\t// Normally WordPress updates the `selection` property of the post when changes are made to blocks.\n\t\t// In a multi-user setup, block changes can occur from other users. When an entity is updated from another\n\t\t// user's changes, useBlockSync() in Gutenberg will reset the user's selection to the last saved selection.\n\t\t//\n\t\t// Manually adding an edit for each movement ensures that other user's changes to the document will\n\t\t// not cause the local user's selection to reset to the last local change location.\n\t\tconst edits = {\n\t\t\tselection: { selectionStart, selectionEnd, initialPosition },\n\t\t};\n\n\t\tconst options = {\n\t\t\tundoIgnore: true,\n\t\t};\n\n\t\t// @ts-ignore Types are not provided when using store name instead of store instance.\n\t\tdispatch( coreStore ).editEntityRecord(\n\t\t\tthis.kind,\n\t\t\tthis.name,\n\t\t\tthis.postId,\n\t\t\tedits,\n\t\t\toptions\n\t\t);\n\t}\n\n\t/**\n\t * Check if two editor states are equal.\n\t *\n\t * @param state1 - The first editor state.\n\t * @param state2 - The second editor state.\n\t * @return True if the editor states are equal, false otherwise.\n\t */\n\tprivate areEditorStatesEqual(\n\t\tstate1?: EditorState,\n\t\tstate2?: EditorState\n\t): boolean {\n\t\tif ( ! state1 || ! state2 ) {\n\t\t\treturn state1 === state2;\n\t\t}\n\n\t\tif ( ! state1.selection || ! state2.selection ) {\n\t\t\treturn state1.selection === state2.selection;\n\t\t}\n\n\t\treturn areSelectionsStatesEqual( state1.selection, state2.selection );\n\t}\n\n\t/**\n\t * Resolve a selection state to a text index and block client ID.\n\t *\n\t * For text-based selections, navigates up from the resolved Y.Text via\n\t * AbstractType.parent to find the containing block, then resolves the\n\t * local clientId via the block's tree path.\n\t * For WholeBlock selections, resolves the block's relative position and\n\t * then finds the local clientId via tree path.\n\t *\n\t * Tree-path resolution is used instead of reading the clientId directly\n\t * from the Yjs block because the local block-editor store may use different\n\t * clientIds (e.g. in \"Show Template\" mode where blocks are cloned).\n\t *\n\t * @param selection - The selection state.\n\t * @return The rich-text offset and block client ID, or nulls if not resolvable.\n\t */\n\tpublic convertSelectionStateToAbsolute( selection: SelectionState ): {\n\t\trichTextOffset: number | null;\n\t\tlocalClientId: string | null;\n\t} {\n\t\tif ( selection.type === SelectionType.None ) {\n\t\t\treturn { richTextOffset: null, localClientId: null };\n\t\t}\n\n\t\tif ( selection.type === SelectionType.WholeBlock ) {\n\t\t\tconst absolutePos = Y.createAbsolutePositionFromRelativePosition(\n\t\t\t\tselection.blockPosition,\n\t\t\t\tthis.doc\n\t\t\t);\n\n\t\t\tlet localClientId: string | null = null;\n\n\t\t\tif ( absolutePos && absolutePos.type instanceof Y.Array ) {\n\t\t\t\tconst parentArray = absolutePos.type as YBlocks;\n\t\t\t\tconst block = parentArray.get( absolutePos.index );\n\n\t\t\t\tif ( block instanceof Y.Map ) {\n\t\t\t\t\tconst path = getBlockPathInYdoc( block );\n\t\t\t\t\tlocalClientId = path\n\t\t\t\t\t\t? resolveBlockClientIdByPath( path )\n\t\t\t\t\t\t: null;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn { richTextOffset: null, localClientId };\n\t\t}\n\n\t\t// Text-based selections: resolve cursor position and navigate up.\n\t\tconst cursorPos =\n\t\t\t'cursorPosition' in selection\n\t\t\t\t? selection.cursorPosition\n\t\t\t\t: selection.cursorStartPosition;\n\n\t\tconst absolutePosition = Y.createAbsolutePositionFromRelativePosition(\n\t\t\tcursorPos.relativePosition,\n\t\t\tthis.doc\n\t\t);\n\n\t\tif ( ! absolutePosition ) {\n\t\t\treturn { richTextOffset: null, localClientId: null };\n\t\t}\n\n\t\t// Navigate up: Y.Text -> attributes Y.Map -> block Y.Map\n\t\tconst yType = absolutePosition.type.parent?.parent;\n\t\tconst path =\n\t\t\tyType instanceof Y.Map ? getBlockPathInYdoc( yType ) : null;\n\t\tconst localClientId = path ? resolveBlockClientIdByPath( path ) : null;\n\n\t\treturn {\n\t\t\trichTextOffset: htmlIndexToRichTextOffset(\n\t\t\t\tabsolutePosition.type.toString(),\n\t\t\t\tabsolutePosition.index\n\t\t\t),\n\t\t\tlocalClientId,\n\t\t};\n\t}\n\n\t/**\n\t * Type guard to check if a struct is a Y.Item (not Y.GC)\n\t * @param struct - The struct to check.\n\t * @return True if the struct is a Y.Item, false otherwise.\n\t */\n\tprivate isYItem( struct: Y.Item | Y.GC ): struct is Y.Item {\n\t\treturn 'content' in struct;\n\t}\n\n\t/**\n\t * Get data for debugging, using the awareness state.\n\t *\n\t * @return {YDocDebugData} The debug data.\n\t */\n\tpublic getDebugData(): YDocDebugData {\n\t\tconst ydoc = this.doc;\n\n\t\t// Manually extract doc data to avoid deprecated toJSON method\n\t\tconst docData: Record< string, unknown > = Object.fromEntries(\n\t\t\tArray.from( ydoc.share, ( [ key, value ] ) => [\n\t\t\t\tkey,\n\t\t\t\tvalue.toJSON(),\n\t\t\t] )\n\t\t);\n\n\t\t// Build collaboratorMap from awareness store (all collaborators seen this session)\n\t\tconst collaboratorMapData = new Map< string, DebugCollaboratorData >(\n\t\t\tArray.from( this.getSeenStates().entries() ).map(\n\t\t\t\t( [ clientId, collaboratorState ] ) => [\n\t\t\t\t\tString( clientId ),\n\t\t\t\t\t{\n\t\t\t\t\t\tname: collaboratorState.collaboratorInfo.name,\n\t\t\t\t\t\twpUserId: collaboratorState.collaboratorInfo.id,\n\t\t\t\t\t},\n\t\t\t\t]\n\t\t\t)\n\t\t);\n\n\t\t// Serialize Yjs client items to avoid deep nesting\n\t\tconst serializableClientItems: Record<\n\t\t\tnumber,\n\t\t\tArray< SerializableYItem >\n\t\t> = {};\n\n\t\tydoc.store.clients.forEach( ( structs, clientId ) => {\n\t\t\t// Filter for Y.Item only (skip Y.GC garbage collection structs)\n\t\t\tconst items = structs.filter( this.isYItem );\n\n\t\t\tserializableClientItems[ clientId ] = items.map( ( item ) => {\n\t\t\t\tconst { left, right, ...rest } = item;\n\n\t\t\t\treturn {\n\t\t\t\t\t...rest,\n\t\t\t\t\tleft: left\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\tid: left.id,\n\t\t\t\t\t\t\t\tlength: left.length,\n\t\t\t\t\t\t\t\torigin: left.origin,\n\t\t\t\t\t\t\t\tcontent: left.content,\n\t\t\t\t\t\t }\n\t\t\t\t\t\t: null,\n\t\t\t\t\tright: right\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\tid: right.id,\n\t\t\t\t\t\t\t\tlength: right.length,\n\t\t\t\t\t\t\t\torigin: right.origin,\n\t\t\t\t\t\t\t\tcontent: right.content,\n\t\t\t\t\t\t }\n\t\t\t\t\t\t: null,\n\t\t\t\t};\n\t\t\t} );\n\t\t} );\n\n\t\treturn {\n\t\t\tdoc: docData,\n\t\t\tclients: serializableClientItems,\n\t\t\tcollaboratorMap: Object.fromEntries( collaboratorMapData ),\n\t\t};\n\t}\n}\n\n/**\n * Detect the direction of a selection change by comparing old and new edges.\n *\n * When the user extends a selection backward (e.g. Shift+Left), the\n * selectionStart edge moves while selectionEnd stays fixed, so the caret\n * is at the start. The reverse is true for forward extension.\n *\n * @param prevStart - The previous selectionStart.\n * @param prevEnd - The previous selectionEnd.\n * @param newStart - The new selectionStart.\n * @param newEnd - The new selectionEnd.\n * @return The detected direction, defaulting to Forward when indeterminate.\n */\nfunction detectSelectionDirection(\n\tprevStart: WPBlockSelection,\n\tprevEnd: WPBlockSelection,\n\tnewStart: WPBlockSelection,\n\tnewEnd: WPBlockSelection\n): SelectionDirection {\n\tconst startMoved = ! areBlockSelectionsEqual( prevStart, newStart );\n\tconst endMoved = ! areBlockSelectionsEqual( prevEnd, newEnd );\n\n\tif ( startMoved && ! endMoved ) {\n\t\treturn SelectionDirection.Backward;\n\t}\n\n\treturn SelectionDirection.Forward;\n}\n\n/**\n * Compare two WPBlockSelection objects by value.\n *\n * @param a - First selection.\n * @param b - Second selection.\n * @return True if all fields are equal.\n */\nfunction areBlockSelectionsEqual(\n\ta: WPBlockSelection,\n\tb: WPBlockSelection\n): boolean {\n\treturn (\n\t\ta.clientId === b.clientId &&\n\t\ta.attributeKey === b.attributeKey &&\n\t\ta.offset === b.offset\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA4C;AAC5C,kBAAkB;AAElB,0BAA0C;AAK1C,4BAA4D;AAC5D,0BAA+D;AAC/D,oBAGO;AACP,kBAAwC;AACxC,wBAA0C;AAC1C,kCAIO;AAEP,mBAAmC;AAW5B,IAAM,sBAAN,cAAkC,yCAAsC;AAAA,EAMvE,YACN,KACQ,MACA,MACA,QACP;AACD,UAAO,GAAI;AAJH;AACA;AACA;AAAA,EAGT;AA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I,MAAM,IAAK,CAAE,SAAU;AAC5D,cAAM,EAAE,MAAM,OAAO,GAAG,KAAK,IAAI;AAEjC,eAAO;AAAA,UACN,GAAG;AAAA,UACH,MAAM,OACH;AAAA,YACA,IAAI,KAAK;AAAA,YACT,QAAQ,KAAK;AAAA,YACb,QAAQ,KAAK;AAAA,YACb,SAAS,KAAK;AAAA,UACd,IACA;AAAA,UACH,OAAO,QACJ;AAAA,YACA,IAAI,MAAM;AAAA,YACV,QAAQ,MAAM;AAAA,YACd,QAAQ,MAAM;AAAA,YACd,SAAS,MAAM;AAAA,UACf,IACA;AAAA,QACJ;AAAA,MACD,CAAE;AAAA,IACH,CAAE;AAEF,WAAO;AAAA,MACN,KAAK;AAAA,MACL,SAAS;AAAA,MACT,iBAAiB,OAAO,YAAa,mBAAoB;AAAA,IAC1D;AAAA,EACD;AACD;AAeA,SAAS,yBACR,WACA,SACA,UACA,QACqB;AACrB,QAAM,aAAa,CAAE,wBAAyB,WAAW,QAAS;AAClE,QAAM,WAAW,CAAE,wBAAyB,SAAS,MAAO;AAE5D,MAAK,cAAc,CAAE,UAAW;AAC/B,WAAO,gCAAmB;AAAA,EAC3B;AAEA,SAAO,gCAAmB;AAC3B;AASA,SAAS,wBACR,GACA,GACU;AACV,SACC,EAAE,aAAa,EAAE,YACjB,EAAE,iBAAiB,EAAE,gBACrB,EAAE,WAAW,EAAE;AAEjB;",
|
|
6
6
|
"names": ["blockEditorStore", "coreStore", "localClientId", "path"]
|
|
7
7
|
}
|
package/build/entities.cjs
CHANGED
|
@@ -311,14 +311,35 @@ var prePersistPostType = async (persistedRecord, edits, name, isTemplate) => {
|
|
|
311
311
|
return newEdits;
|
|
312
312
|
};
|
|
313
313
|
async function loadPostTypeEntities() {
|
|
314
|
-
const
|
|
315
|
-
|
|
316
|
-
|
|
314
|
+
const postTypesPromise = (0, import_api_fetch.default)({ path: "/wp/v2/types?context=view" });
|
|
315
|
+
const taxonomiesPromise = window._wpCollaborationEnabled ? (0, import_api_fetch.default)({ path: "/wp/v2/taxonomies?context=view" }) : Promise.resolve({});
|
|
316
|
+
const [postTypes, taxonomies] = await Promise.all([
|
|
317
|
+
postTypesPromise,
|
|
318
|
+
taxonomiesPromise
|
|
319
|
+
]);
|
|
317
320
|
return Object.entries(postTypes ?? {}).map(([name, postType]) => {
|
|
318
321
|
const isTemplate = ["wp_template", "wp_template_part"].includes(
|
|
319
322
|
name
|
|
320
323
|
);
|
|
321
324
|
const namespace = postType?.rest_namespace ?? "wp/v2";
|
|
325
|
+
const syncedProperties = /* @__PURE__ */ new Set([
|
|
326
|
+
"author",
|
|
327
|
+
"blocks",
|
|
328
|
+
"content",
|
|
329
|
+
"comment_status",
|
|
330
|
+
"date",
|
|
331
|
+
"excerpt",
|
|
332
|
+
"featured_media",
|
|
333
|
+
"format",
|
|
334
|
+
"meta",
|
|
335
|
+
"ping_status",
|
|
336
|
+
"slug",
|
|
337
|
+
"status",
|
|
338
|
+
"sticky",
|
|
339
|
+
"template",
|
|
340
|
+
"title",
|
|
341
|
+
...postType.taxonomies?.map((taxonomy) => taxonomies?.[taxonomy]?.rest_base)?.filter(Boolean) ?? []
|
|
342
|
+
]);
|
|
322
343
|
const entity = {
|
|
323
344
|
kind: "postType",
|
|
324
345
|
baseURL: `/${namespace}/${postType.rest_base}`,
|
|
@@ -347,7 +368,7 @@ async function loadPostTypeEntities() {
|
|
|
347
368
|
* @param {Partial< import('@wordpress/sync').ObjectData >} changes
|
|
348
369
|
* @return {void}
|
|
349
370
|
*/
|
|
350
|
-
applyChangesToCRDTDoc: (crdtDoc, changes) => (0, import_crdt.applyPostChangesToCRDTDoc)(crdtDoc, changes,
|
|
371
|
+
applyChangesToCRDTDoc: (crdtDoc, changes) => (0, import_crdt.applyPostChangesToCRDTDoc)(crdtDoc, changes, syncedProperties),
|
|
351
372
|
/**
|
|
352
373
|
* Create the awareness instance for the entity's CRDT document.
|
|
353
374
|
*
|
|
@@ -368,7 +389,11 @@ async function loadPostTypeEntities() {
|
|
|
368
389
|
* @param {import('@wordpress/sync').ObjectData} editedRecord
|
|
369
390
|
* @return {Partial< import('@wordpress/sync').ObjectData >} Changes to record
|
|
370
391
|
*/
|
|
371
|
-
getChangesFromCRDTDoc: (crdtDoc, editedRecord) => (0, import_crdt.getPostChangesFromCRDTDoc)(
|
|
392
|
+
getChangesFromCRDTDoc: (crdtDoc, editedRecord) => (0, import_crdt.getPostChangesFromCRDTDoc)(
|
|
393
|
+
crdtDoc,
|
|
394
|
+
editedRecord,
|
|
395
|
+
syncedProperties
|
|
396
|
+
),
|
|
372
397
|
/**
|
|
373
398
|
* Extract changes from a CRDT document that can be used to update the
|
|
374
399
|
* local editor state.
|
package/build/entities.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/entities.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport { capitalCase, pascalCase } from 'change-case';\n\n/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { __unstableSerializeAndClean, parse } from '@wordpress/blocks';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { PostEditorAwareness } from './awareness/post-editor-awareness';\nimport { getSyncManager } from './sync';\nimport {\n\tapplyPostChangesToCRDTDoc,\n\tdefaultSyncConfig,\n\tgetPostChangesFromCRDTDoc,\n\tPOST_META_KEY_FOR_CRDT_DOC_PERSISTENCE,\n} from './utils/crdt';\n\nexport const DEFAULT_ENTITY_KEY = 'id';\nconst POST_RAW_ATTRIBUTES = [ 'title', 'excerpt', 'content' ];\n\nconst blocksTransientEdits = {\n\tblocks: {\n\t\tread: ( record ) => parse( record.content?.raw ?? '' ),\n\t\twrite: ( record ) => ( {\n\t\t\tcontent: __unstableSerializeAndClean( record.blocks ),\n\t\t} ),\n\t},\n};\n\nexport const rootEntitiesConfig = [\n\t{\n\t\tlabel: __( 'Base' ),\n\t\tkind: 'root',\n\t\tkey: false,\n\t\tname: '__unstableBase',\n\t\tbaseURL: '/',\n\t\tbaseURLParams: {\n\t\t\t// Please also change the preload path when changing this.\n\t\t\t// @see lib/compat/wordpress-7.0/preload.php\n\t\t\t_fields: [\n\t\t\t\t'description',\n\t\t\t\t'gmt_offset',\n\t\t\t\t'home',\n\t\t\t\t'image_sizes',\n\t\t\t\t'image_size_threshold',\n\t\t\t\t'image_output_formats',\n\t\t\t\t'jpeg_interlaced',\n\t\t\t\t'png_interlaced',\n\t\t\t\t'gif_interlaced',\n\t\t\t\t'name',\n\t\t\t\t'site_icon',\n\t\t\t\t'site_icon_url',\n\t\t\t\t'site_logo',\n\t\t\t\t'timezone_string',\n\t\t\t\t'url',\n\t\t\t\t'page_for_posts',\n\t\t\t\t'page_on_front',\n\t\t\t\t'show_on_front',\n\t\t\t].join( ',' ),\n\t\t},\n\t\t// The entity doesn't support selecting multiple records.\n\t\t// The property is maintained for backward compatibility.\n\t\tplural: '__unstableBases',\n\t},\n\t{\n\t\tlabel: __( 'Post Type' ),\n\t\tname: 'postType',\n\t\tkind: 'root',\n\t\tkey: 'slug',\n\t\tbaseURL: '/wp/v2/types',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'postTypes',\n\t},\n\t{\n\t\tname: 'media',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/media',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'mediaItems',\n\t\tlabel: __( 'Media' ),\n\t\trawAttributes: [ 'caption', 'title', 'description' ],\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tname: 'taxonomy',\n\t\tkind: 'root',\n\t\tkey: 'slug',\n\t\tbaseURL: '/wp/v2/taxonomies',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'taxonomies',\n\t\tlabel: __( 'Taxonomy' ),\n\t},\n\t{\n\t\tname: 'sidebar',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/sidebars',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'sidebars',\n\t\ttransientEdits: { blocks: true },\n\t\tlabel: __( 'Widget areas' ),\n\t},\n\t{\n\t\tname: 'widget',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/widgets',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'widgets',\n\t\ttransientEdits: { blocks: true },\n\t\tlabel: __( 'Widgets' ),\n\t},\n\t{\n\t\tname: 'widgetType',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/widget-types',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'widgetTypes',\n\t\tlabel: __( 'Widget types' ),\n\t},\n\t{\n\t\tlabel: __( 'User' ),\n\t\tname: 'user',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/users',\n\t\tgetTitle: ( record ) => record?.name || record?.slug,\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'users',\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tname: 'comment',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/comments',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'comments',\n\t\tlabel: __( 'Comment' ),\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tname: 'menu',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/menus',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'menus',\n\t\tlabel: __( 'Menu' ),\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tname: 'menuItem',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/menu-items',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'menuItems',\n\t\tlabel: __( 'Menu Item' ),\n\t\trawAttributes: [ 'title' ],\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tname: 'menuLocation',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/menu-locations',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'menuLocations',\n\t\tlabel: __( 'Menu Location' ),\n\t\tkey: 'name',\n\t},\n\t{\n\t\tlabel: __( 'Global Styles' ),\n\t\tname: 'globalStyles',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/global-styles',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'globalStylesVariations', // Should be different from name.\n\t\tgetTitle: () => __( 'Custom Styles' ),\n\t\tgetRevisionsUrl: ( parentId, revisionId ) =>\n\t\t\t`/wp/v2/global-styles/${ parentId }/revisions${\n\t\t\t\trevisionId ? '/' + revisionId : ''\n\t\t\t}`,\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tlabel: __( 'Themes' ),\n\t\tname: 'theme',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/themes',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'themes',\n\t\tkey: 'stylesheet',\n\t},\n\t{\n\t\tlabel: __( 'Plugins' ),\n\t\tname: 'plugin',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/plugins',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'plugins',\n\t\tkey: 'plugin',\n\t},\n\t{\n\t\tlabel: __( 'Status' ),\n\t\tname: 'status',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/statuses',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'statuses',\n\t\tkey: 'slug',\n\t},\n\t{\n\t\tlabel: __( 'Registered Templates' ),\n\t\tname: 'registeredTemplate',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/registered-templates',\n\t\tkey: 'id',\n\t},\n\t{\n\t\tlabel: __( 'Font Collections' ),\n\t\tname: 'fontCollection',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/font-collections',\n\t\tbaseURLParams: { context: 'view' },\n\t\tplural: 'fontCollections',\n\t\tkey: 'slug',\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tlabel: __( 'Icons' ),\n\t\tname: 'icon',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/icons',\n\t\tbaseURLParams: { context: 'view' },\n\t\tplural: 'icons',\n\t\tkey: 'name',\n\t},\n].map( ( entity ) => {\n\tconst syncEnabledRootEntities = new Set( [ 'comment' ] );\n\n\tif ( syncEnabledRootEntities.has( entity.name ) ) {\n\t\tentity.syncConfig = defaultSyncConfig;\n\t}\n\treturn entity;\n} );\n\nexport const deprecatedEntities = {\n\troot: {\n\t\tmedia: {\n\t\t\tsince: '6.9',\n\t\t\talternative: {\n\t\t\t\tkind: 'postType',\n\t\t\t\tname: 'attachment',\n\t\t\t},\n\t\t},\n\t},\n};\n\nexport const additionalEntityConfigLoaders = [\n\t{ kind: 'postType', loadEntities: loadPostTypeEntities },\n\t{ kind: 'taxonomy', loadEntities: loadTaxonomyEntities },\n\t{\n\t\tkind: 'root',\n\t\tname: 'site',\n\t\tplural: 'sites',\n\t\tloadEntities: loadSiteEntity,\n\t},\n];\n\n/**\n * Apply extra edits before persisting a post type.\n *\n * @param {Object} persistedRecord Already persisted Post\n * @param {Object} edits Edits.\n * @param {string} name Post type name.\n * @param {boolean} isTemplate Whether the post type is a template.\n * @return {Promise< Object >} Updated edits.\n */\nexport const prePersistPostType = async (\n\tpersistedRecord,\n\tedits,\n\tname,\n\tisTemplate\n) => {\n\tconst newEdits = {};\n\n\tif ( ! isTemplate && persistedRecord?.status === 'auto-draft' ) {\n\t\t// Saving an auto-draft should create a draft by default.\n\t\tif ( ! edits.status && ! newEdits.status ) {\n\t\t\tnewEdits.status = 'draft';\n\t\t}\n\n\t\t// Fix the auto-draft default title.\n\t\tif (\n\t\t\t( ! edits.title || edits.title === 'Auto Draft' ) &&\n\t\t\t! newEdits.title &&\n\t\t\t( ! persistedRecord?.title ||\n\t\t\t\tpersistedRecord?.title === 'Auto Draft' )\n\t\t) {\n\t\t\tnewEdits.title = '';\n\t\t}\n\t}\n\n\t// Add meta for persisted CRDT document.\n\tif ( persistedRecord ) {\n\t\tconst objectType = `postType/${ name }`;\n\t\tconst objectId = persistedRecord.id;\n\t\tconst serializedDoc = await getSyncManager()?.createPersistedCRDTDoc(\n\t\t\tobjectType,\n\t\t\tobjectId\n\t\t);\n\n\t\tif ( serializedDoc ) {\n\t\t\tnewEdits.meta = {\n\t\t\t\t...edits.meta,\n\t\t\t\t[ POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE ]: serializedDoc,\n\t\t\t};\n\t\t}\n\t}\n\n\treturn newEdits;\n};\n\n/**\n * Returns the list of post type entities.\n *\n * @return {Promise} Entities promise\n */\nasync function loadPostTypeEntities() {\n\tconst postTypes = await apiFetch( {\n\t\tpath: '/wp/v2/types?context=view',\n\t} );\n\treturn Object.entries( postTypes ?? {} ).map( ( [ name, postType ] ) => {\n\t\tconst isTemplate = [ 'wp_template', 'wp_template_part' ].includes(\n\t\t\tname\n\t\t);\n\t\tconst namespace = postType?.rest_namespace ?? 'wp/v2';\n\n\t\tconst entity = {\n\t\t\tkind: 'postType',\n\t\t\tbaseURL: `/${ namespace }/${ postType.rest_base }`,\n\t\t\tbaseURLParams: { context: 'edit' },\n\t\t\tname,\n\t\t\tlabel: postType.name,\n\t\t\ttransientEdits: {\n\t\t\t\t...blocksTransientEdits,\n\t\t\t\tselection: true,\n\t\t\t},\n\t\t\tmergedEdits: { meta: true },\n\t\t\trawAttributes: POST_RAW_ATTRIBUTES,\n\t\t\tgetTitle: ( record ) =>\n\t\t\t\trecord?.title?.rendered ||\n\t\t\t\trecord?.title ||\n\t\t\t\t( isTemplate\n\t\t\t\t\t? capitalCase( record.slug ?? '' )\n\t\t\t\t\t: String( record.id ) ),\n\t\t\t__unstablePrePersist: ( persistedRecord, edits ) =>\n\t\t\t\tprePersistPostType( persistedRecord, edits, name, isTemplate ),\n\t\t\t__unstable_rest_base: postType.rest_base,\n\t\t\tsupportsPagination: true,\n\t\t\tgetRevisionsUrl: ( parentId, revisionId ) =>\n\t\t\t\t`/${ namespace }/${\n\t\t\t\t\tpostType.rest_base\n\t\t\t\t}/${ parentId }/revisions${\n\t\t\t\t\trevisionId ? '/' + revisionId : ''\n\t\t\t\t}`,\n\t\t\trevisionKey:\n\t\t\t\tisTemplate && ! window?.__experimentalTemplateActivate\n\t\t\t\t\t? 'wp_id'\n\t\t\t\t\t: DEFAULT_ENTITY_KEY,\n\t\t};\n\n\t\t/**\n\t\t * @type {import('@wordpress/sync').SyncConfig}\n\t\t */\n\t\tentity.syncConfig = {\n\t\t\t/**\n\t\t\t * Apply changes from the local editor to the local CRDT document so\n\t\t\t * that those changes can be synced to other peers (via the provider).\n\t\t\t *\n\t\t\t * @param {import('@wordpress/sync').CRDTDoc} crdtDoc\n\t\t\t * @param {Partial< import('@wordpress/sync').ObjectData >} changes\n\t\t\t * @return {void}\n\t\t\t */\n\t\t\tapplyChangesToCRDTDoc: ( crdtDoc, changes ) =>\n\t\t\t\tapplyPostChangesToCRDTDoc( crdtDoc, changes, postType ),\n\n\t\t\t/**\n\t\t\t * Create the awareness instance for the entity's CRDT document.\n\t\t\t *\n\t\t\t * @param {import('@wordpress/sync').CRDTDoc} ydoc\n\t\t\t * @param {import('@wordpress/sync').ObjectID} objectId\n\t\t\t * @return {import('@wordpress/sync').Awareness} Awareness instance\n\t\t\t */\n\t\t\tcreateAwareness: ( ydoc, objectId ) => {\n\t\t\t\tconst kind = 'postType';\n\t\t\t\tconst id = parseInt( objectId, 10 );\n\t\t\t\treturn new PostEditorAwareness( ydoc, kind, name, id );\n\t\t\t},\n\n\t\t\t/**\n\t\t\t * Extract changes from a CRDT document that can be used to update the\n\t\t\t * local editor state.\n\t\t\t *\n\t\t\t * @param {import('@wordpress/sync').CRDTDoc} crdtDoc\n\t\t\t * @param {import('@wordpress/sync').ObjectData} editedRecord\n\t\t\t * @return {Partial< import('@wordpress/sync').ObjectData >} Changes to record\n\t\t\t */\n\t\t\tgetChangesFromCRDTDoc: ( crdtDoc, editedRecord ) =>\n\t\t\t\tgetPostChangesFromCRDTDoc( crdtDoc, editedRecord, postType ),\n\n\t\t\t/**\n\t\t\t * Extract changes from a CRDT document that can be used to update the\n\t\t\t * local editor state.\n\t\t\t *\n\t\t\t * @param {import('@wordpress/sync').ObjectData} record\n\t\t\t * @return {Partial< import('@wordpress/sync').ObjectData >} Changes to record\n\t\t\t */\n\t\t\tgetPersistedCRDTDoc: ( record ) => {\n\t\t\t\treturn (\n\t\t\t\t\trecord?.meta?.[ POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE ] ||\n\t\t\t\t\tnull\n\t\t\t\t);\n\t\t\t},\n\t\t};\n\n\t\treturn entity;\n\t} );\n}\n\n/**\n * Returns the list of the taxonomies entities.\n *\n * @return {Promise} Entities promise\n */\nasync function loadTaxonomyEntities() {\n\tconst taxonomies = await apiFetch( {\n\t\tpath: '/wp/v2/taxonomies?context=view',\n\t} );\n\treturn Object.entries( taxonomies ?? {} ).map( ( [ name, taxonomy ] ) => {\n\t\tconst namespace = taxonomy?.rest_namespace ?? 'wp/v2';\n\t\tconst entity = {\n\t\t\tkind: 'taxonomy',\n\t\t\tbaseURL: `/${ namespace }/${ taxonomy.rest_base }`,\n\t\t\tbaseURLParams: { context: 'edit' },\n\t\t\tname,\n\t\t\tlabel: taxonomy.name,\n\t\t\tgetTitle: ( record ) => record?.name,\n\t\t\tsupportsPagination: true,\n\t\t};\n\n\t\tentity.syncConfig = defaultSyncConfig;\n\n\t\treturn entity;\n\t} );\n}\n\n/**\n * Returns the Site entity.\n *\n * @return {Promise} Entity promise\n */\nasync function loadSiteEntity() {\n\tconst entity = {\n\t\tlabel: __( 'Site' ),\n\t\tname: 'site',\n\t\tkind: 'root',\n\t\tkey: false,\n\t\tbaseURL: '/wp/v2/settings',\n\t\tmeta: {},\n\t};\n\n\tconst site = await apiFetch( {\n\t\tpath: entity.baseURL,\n\t\tmethod: 'OPTIONS',\n\t} );\n\n\tconst labels = {};\n\tObject.entries( site?.schema?.properties ?? {} ).forEach(\n\t\t( [ key, value ] ) => {\n\t\t\t// Ignore properties `title` and `type` keys.\n\t\t\tif ( typeof value === 'object' && value.title ) {\n\t\t\t\tlabels[ key ] = value.title;\n\t\t\t}\n\t\t}\n\t);\n\n\treturn [ { ...entity, meta: { labels } } ];\n}\n\n/**\n * Returns the entity's getter method name given its kind and name or plural name.\n *\n * @example\n * ```js\n * const nameSingular = getMethodName( 'root', 'theme', 'get' );\n * // nameSingular is getRootTheme\n *\n * const namePlural = getMethodName( 'root', 'themes', 'set' );\n * // namePlural is setRootThemes\n * ```\n *\n * @param {string} kind Entity kind.\n * @param {string} name Entity name or plural name.\n * @param {string} prefix Function prefix.\n *\n * @return {string} Method name\n */\nexport const getMethodName = ( kind, name, prefix = 'get' ) => {\n\tconst kindPrefix = kind === 'root' ? '' : pascalCase( kind );\n\tconst suffix = pascalCase( name );\n\treturn `${ prefix }${ kindPrefix }${ suffix }`;\n};\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,yBAAwC;AAKxC,uBAAqB;AACrB,oBAAmD;AACnD,kBAAmB;AAKnB,mCAAoC;AACpC,kBAA+B;AAC/B,kBAKO;AAEA,IAAM,qBAAqB;AAClC,IAAM,sBAAsB,CAAE,SAAS,WAAW,SAAU;AAE5D,IAAM,uBAAuB;AAAA,EAC5B,QAAQ;AAAA,IACP,MAAM,CAAE,eAAY,qBAAO,OAAO,SAAS,OAAO,EAAG;AAAA,IACrD,OAAO,CAAE,YAAc;AAAA,MACtB,aAAS,2CAA6B,OAAO,MAAO;AAAA,IACrD;AAAA,EACD;AACD;AAEO,IAAM,qBAAqB;AAAA,EACjC;AAAA,IACC,WAAO,gBAAI,MAAO;AAAA,IAClB,MAAM;AAAA,IACN,KAAK;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IACT,eAAe;AAAA;AAAA;AAAA,MAGd,SAAS;AAAA,QACR;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA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U,CAAE;AAEvD,MAAK,wBAAwB,IAAK,OAAO,IAAK,GAAI;AACjD,WAAO,aAAa;AAAA,EACrB;AACA,SAAO;AACR,CAAE;AAEK,IAAM,qBAAqB;AAAA,EACjC,MAAM;AAAA,IACL,OAAO;AAAA,MACN,OAAO;AAAA,MACP,aAAa;AAAA,QACZ,MAAM;AAAA,QACN,MAAM;AAAA,MACP;AAAA,IACD;AAAA,EACD;AACD;AAEO,IAAM,gCAAgC;AAAA,EAC5C,EAAE,MAAM,YAAY,cAAc,qBAAqB;AAAA,EACvD,EAAE,MAAM,YAAY,cAAc,qBAAqB;AAAA,EACvD;AAAA,IACC,MAAM;AAAA,IACN,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,cAAc;AAAA,EACf;AACD;AAWO,IAAM,qBAAqB,OACjC,iBACA,OACA,MACA,eACI;AACJ,QAAM,WAAW,CAAC;AAElB,MAAK,CAAE,cAAc,iBAAiB,WAAW,cAAe;AAE/D,QAAK,CAAE,MAAM,UAAU,CAAE,SAAS,QAAS;AAC1C,eAAS,SAAS;AAAA,IACnB;AAGA,SACG,CAAE,MAAM,SAAS,MAAM,UAAU,iBACnC,CAAE,SAAS,UACT,CAAE,iBAAiB,SACpB,iBAAiB,UAAU,eAC3B;AACD,eAAS,QAAQ;AAAA,IAClB;AAAA,EACD;AAGA,MAAK,iBAAkB;AACtB,UAAM,aAAa,YAAa,IAAK;AACrC,UAAM,WAAW,gBAAgB;AACjC,UAAM,gBAAgB,UAAM,4BAAe,GAAG;AAAA,MAC7C;AAAA,MACA;AAAA,IACD;AAEA,QAAK,eAAgB;AACpB,eAAS,OAAO;AAAA,QACf,GAAG,MAAM;AAAA,QACT,CAAE,kDAAuC,GAAG;AAAA,MAC7C;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AACR;AAOA,eAAe,uBAAuB;AACrC,QAAM,
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport { capitalCase, pascalCase } from 'change-case';\n\n/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { __unstableSerializeAndClean, parse } from '@wordpress/blocks';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { PostEditorAwareness } from './awareness/post-editor-awareness';\nimport { getSyncManager } from './sync';\nimport {\n\tapplyPostChangesToCRDTDoc,\n\tdefaultSyncConfig,\n\tgetPostChangesFromCRDTDoc,\n\tPOST_META_KEY_FOR_CRDT_DOC_PERSISTENCE,\n} from './utils/crdt';\n\nexport const DEFAULT_ENTITY_KEY = 'id';\nconst POST_RAW_ATTRIBUTES = [ 'title', 'excerpt', 'content' ];\n\nconst blocksTransientEdits = {\n\tblocks: {\n\t\tread: ( record ) => parse( record.content?.raw ?? '' ),\n\t\twrite: ( record ) => ( {\n\t\t\tcontent: __unstableSerializeAndClean( record.blocks ),\n\t\t} ),\n\t},\n};\n\nexport const rootEntitiesConfig = [\n\t{\n\t\tlabel: __( 'Base' ),\n\t\tkind: 'root',\n\t\tkey: false,\n\t\tname: '__unstableBase',\n\t\tbaseURL: '/',\n\t\tbaseURLParams: {\n\t\t\t// Please also change the preload path when changing this.\n\t\t\t// @see lib/compat/wordpress-7.0/preload.php\n\t\t\t_fields: [\n\t\t\t\t'description',\n\t\t\t\t'gmt_offset',\n\t\t\t\t'home',\n\t\t\t\t'image_sizes',\n\t\t\t\t'image_size_threshold',\n\t\t\t\t'image_output_formats',\n\t\t\t\t'jpeg_interlaced',\n\t\t\t\t'png_interlaced',\n\t\t\t\t'gif_interlaced',\n\t\t\t\t'name',\n\t\t\t\t'site_icon',\n\t\t\t\t'site_icon_url',\n\t\t\t\t'site_logo',\n\t\t\t\t'timezone_string',\n\t\t\t\t'url',\n\t\t\t\t'page_for_posts',\n\t\t\t\t'page_on_front',\n\t\t\t\t'show_on_front',\n\t\t\t].join( ',' ),\n\t\t},\n\t\t// The entity doesn't support selecting multiple records.\n\t\t// The property is maintained for backward compatibility.\n\t\tplural: '__unstableBases',\n\t},\n\t{\n\t\tlabel: __( 'Post Type' ),\n\t\tname: 'postType',\n\t\tkind: 'root',\n\t\tkey: 'slug',\n\t\tbaseURL: '/wp/v2/types',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'postTypes',\n\t},\n\t{\n\t\tname: 'media',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/media',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'mediaItems',\n\t\tlabel: __( 'Media' ),\n\t\trawAttributes: [ 'caption', 'title', 'description' ],\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tname: 'taxonomy',\n\t\tkind: 'root',\n\t\tkey: 'slug',\n\t\tbaseURL: '/wp/v2/taxonomies',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'taxonomies',\n\t\tlabel: __( 'Taxonomy' ),\n\t},\n\t{\n\t\tname: 'sidebar',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/sidebars',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'sidebars',\n\t\ttransientEdits: { blocks: true },\n\t\tlabel: __( 'Widget areas' ),\n\t},\n\t{\n\t\tname: 'widget',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/widgets',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'widgets',\n\t\ttransientEdits: { blocks: true },\n\t\tlabel: __( 'Widgets' ),\n\t},\n\t{\n\t\tname: 'widgetType',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/widget-types',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'widgetTypes',\n\t\tlabel: __( 'Widget types' ),\n\t},\n\t{\n\t\tlabel: __( 'User' ),\n\t\tname: 'user',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/users',\n\t\tgetTitle: ( record ) => record?.name || record?.slug,\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'users',\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tname: 'comment',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/comments',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'comments',\n\t\tlabel: __( 'Comment' ),\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tname: 'menu',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/menus',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'menus',\n\t\tlabel: __( 'Menu' ),\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tname: 'menuItem',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/menu-items',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'menuItems',\n\t\tlabel: __( 'Menu Item' ),\n\t\trawAttributes: [ 'title' ],\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tname: 'menuLocation',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/menu-locations',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'menuLocations',\n\t\tlabel: __( 'Menu Location' ),\n\t\tkey: 'name',\n\t},\n\t{\n\t\tlabel: __( 'Global Styles' ),\n\t\tname: 'globalStyles',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/global-styles',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'globalStylesVariations', // Should be different from name.\n\t\tgetTitle: () => __( 'Custom Styles' ),\n\t\tgetRevisionsUrl: ( parentId, revisionId ) =>\n\t\t\t`/wp/v2/global-styles/${ parentId }/revisions${\n\t\t\t\trevisionId ? '/' + revisionId : ''\n\t\t\t}`,\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tlabel: __( 'Themes' ),\n\t\tname: 'theme',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/themes',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'themes',\n\t\tkey: 'stylesheet',\n\t},\n\t{\n\t\tlabel: __( 'Plugins' ),\n\t\tname: 'plugin',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/plugins',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'plugins',\n\t\tkey: 'plugin',\n\t},\n\t{\n\t\tlabel: __( 'Status' ),\n\t\tname: 'status',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/statuses',\n\t\tbaseURLParams: { context: 'edit' },\n\t\tplural: 'statuses',\n\t\tkey: 'slug',\n\t},\n\t{\n\t\tlabel: __( 'Registered Templates' ),\n\t\tname: 'registeredTemplate',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/registered-templates',\n\t\tkey: 'id',\n\t},\n\t{\n\t\tlabel: __( 'Font Collections' ),\n\t\tname: 'fontCollection',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/font-collections',\n\t\tbaseURLParams: { context: 'view' },\n\t\tplural: 'fontCollections',\n\t\tkey: 'slug',\n\t\tsupportsPagination: true,\n\t},\n\t{\n\t\tlabel: __( 'Icons' ),\n\t\tname: 'icon',\n\t\tkind: 'root',\n\t\tbaseURL: '/wp/v2/icons',\n\t\tbaseURLParams: { context: 'view' },\n\t\tplural: 'icons',\n\t\tkey: 'name',\n\t},\n].map( ( entity ) => {\n\tconst syncEnabledRootEntities = new Set( [ 'comment' ] );\n\n\tif ( syncEnabledRootEntities.has( entity.name ) ) {\n\t\tentity.syncConfig = defaultSyncConfig;\n\t}\n\treturn entity;\n} );\n\nexport const deprecatedEntities = {\n\troot: {\n\t\tmedia: {\n\t\t\tsince: '6.9',\n\t\t\talternative: {\n\t\t\t\tkind: 'postType',\n\t\t\t\tname: 'attachment',\n\t\t\t},\n\t\t},\n\t},\n};\n\nexport const additionalEntityConfigLoaders = [\n\t{ kind: 'postType', loadEntities: loadPostTypeEntities },\n\t{ kind: 'taxonomy', loadEntities: loadTaxonomyEntities },\n\t{\n\t\tkind: 'root',\n\t\tname: 'site',\n\t\tplural: 'sites',\n\t\tloadEntities: loadSiteEntity,\n\t},\n];\n\n/**\n * Apply extra edits before persisting a post type.\n *\n * @param {Object} persistedRecord Already persisted Post\n * @param {Object} edits Edits.\n * @param {string} name Post type name.\n * @param {boolean} isTemplate Whether the post type is a template.\n * @return {Promise< Object >} Updated edits.\n */\nexport const prePersistPostType = async (\n\tpersistedRecord,\n\tedits,\n\tname,\n\tisTemplate\n) => {\n\tconst newEdits = {};\n\n\tif ( ! isTemplate && persistedRecord?.status === 'auto-draft' ) {\n\t\t// Saving an auto-draft should create a draft by default.\n\t\tif ( ! edits.status && ! newEdits.status ) {\n\t\t\tnewEdits.status = 'draft';\n\t\t}\n\n\t\t// Fix the auto-draft default title.\n\t\tif (\n\t\t\t( ! edits.title || edits.title === 'Auto Draft' ) &&\n\t\t\t! newEdits.title &&\n\t\t\t( ! persistedRecord?.title ||\n\t\t\t\tpersistedRecord?.title === 'Auto Draft' )\n\t\t) {\n\t\t\tnewEdits.title = '';\n\t\t}\n\t}\n\n\t// Add meta for persisted CRDT document.\n\tif ( persistedRecord ) {\n\t\tconst objectType = `postType/${ name }`;\n\t\tconst objectId = persistedRecord.id;\n\t\tconst serializedDoc = await getSyncManager()?.createPersistedCRDTDoc(\n\t\t\tobjectType,\n\t\t\tobjectId\n\t\t);\n\n\t\tif ( serializedDoc ) {\n\t\t\tnewEdits.meta = {\n\t\t\t\t...edits.meta,\n\t\t\t\t[ POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE ]: serializedDoc,\n\t\t\t};\n\t\t}\n\t}\n\n\treturn newEdits;\n};\n\n/**\n * Returns the list of post type entities.\n *\n * @return {Promise} Entities promise\n */\nasync function loadPostTypeEntities() {\n\tconst postTypesPromise = apiFetch( { path: '/wp/v2/types?context=view' } );\n\tconst taxonomiesPromise = window._wpCollaborationEnabled\n\t\t? apiFetch( { path: '/wp/v2/taxonomies?context=view' } )\n\t\t: Promise.resolve( {} );\n\tconst [ postTypes, taxonomies ] = await Promise.all( [\n\t\tpostTypesPromise,\n\t\ttaxonomiesPromise,\n\t] );\n\n\treturn Object.entries( postTypes ?? {} ).map( ( [ name, postType ] ) => {\n\t\tconst isTemplate = [ 'wp_template', 'wp_template_part' ].includes(\n\t\t\tname\n\t\t);\n\t\tconst namespace = postType?.rest_namespace ?? 'wp/v2';\n\n\t\tconst syncedProperties = new Set( [\n\t\t\t'author',\n\t\t\t'blocks',\n\t\t\t'content',\n\t\t\t'comment_status',\n\t\t\t'date',\n\t\t\t'excerpt',\n\t\t\t'featured_media',\n\t\t\t'format',\n\t\t\t'meta',\n\t\t\t'ping_status',\n\t\t\t'slug',\n\t\t\t'status',\n\t\t\t'sticky',\n\t\t\t'template',\n\t\t\t'title',\n\t\t\t...( postType.taxonomies\n\t\t\t\t?.map( ( taxonomy ) => taxonomies?.[ taxonomy ]?.rest_base )\n\t\t\t\t?.filter( Boolean ) ?? [] ),\n\t\t] );\n\n\t\tconst entity = {\n\t\t\tkind: 'postType',\n\t\t\tbaseURL: `/${ namespace }/${ postType.rest_base }`,\n\t\t\tbaseURLParams: { context: 'edit' },\n\t\t\tname,\n\t\t\tlabel: postType.name,\n\t\t\ttransientEdits: {\n\t\t\t\t...blocksTransientEdits,\n\t\t\t\tselection: true,\n\t\t\t},\n\t\t\tmergedEdits: { meta: true },\n\t\t\trawAttributes: POST_RAW_ATTRIBUTES,\n\t\t\tgetTitle: ( record ) =>\n\t\t\t\trecord?.title?.rendered ||\n\t\t\t\trecord?.title ||\n\t\t\t\t( isTemplate\n\t\t\t\t\t? capitalCase( record.slug ?? '' )\n\t\t\t\t\t: String( record.id ) ),\n\t\t\t__unstablePrePersist: ( persistedRecord, edits ) =>\n\t\t\t\tprePersistPostType( persistedRecord, edits, name, isTemplate ),\n\t\t\t__unstable_rest_base: postType.rest_base,\n\t\t\tsupportsPagination: true,\n\t\t\tgetRevisionsUrl: ( parentId, revisionId ) =>\n\t\t\t\t`/${ namespace }/${\n\t\t\t\t\tpostType.rest_base\n\t\t\t\t}/${ parentId }/revisions${\n\t\t\t\t\trevisionId ? '/' + revisionId : ''\n\t\t\t\t}`,\n\t\t\trevisionKey:\n\t\t\t\tisTemplate && ! window?.__experimentalTemplateActivate\n\t\t\t\t\t? 'wp_id'\n\t\t\t\t\t: DEFAULT_ENTITY_KEY,\n\t\t};\n\n\t\t/**\n\t\t * @type {import('@wordpress/sync').SyncConfig}\n\t\t */\n\t\tentity.syncConfig = {\n\t\t\t/**\n\t\t\t * Apply changes from the local editor to the local CRDT document so\n\t\t\t * that those changes can be synced to other peers (via the provider).\n\t\t\t *\n\t\t\t * @param {import('@wordpress/sync').CRDTDoc} crdtDoc\n\t\t\t * @param {Partial< import('@wordpress/sync').ObjectData >} changes\n\t\t\t * @return {void}\n\t\t\t */\n\t\t\tapplyChangesToCRDTDoc: ( crdtDoc, changes ) =>\n\t\t\t\tapplyPostChangesToCRDTDoc( crdtDoc, changes, syncedProperties ),\n\n\t\t\t/**\n\t\t\t * Create the awareness instance for the entity's CRDT document.\n\t\t\t *\n\t\t\t * @param {import('@wordpress/sync').CRDTDoc} ydoc\n\t\t\t * @param {import('@wordpress/sync').ObjectID} objectId\n\t\t\t * @return {import('@wordpress/sync').Awareness} Awareness instance\n\t\t\t */\n\t\t\tcreateAwareness: ( ydoc, objectId ) => {\n\t\t\t\tconst kind = 'postType';\n\t\t\t\tconst id = parseInt( objectId, 10 );\n\t\t\t\treturn new PostEditorAwareness( ydoc, kind, name, id );\n\t\t\t},\n\n\t\t\t/**\n\t\t\t * Extract changes from a CRDT document that can be used to update the\n\t\t\t * local editor state.\n\t\t\t *\n\t\t\t * @param {import('@wordpress/sync').CRDTDoc} crdtDoc\n\t\t\t * @param {import('@wordpress/sync').ObjectData} editedRecord\n\t\t\t * @return {Partial< import('@wordpress/sync').ObjectData >} Changes to record\n\t\t\t */\n\t\t\tgetChangesFromCRDTDoc: ( crdtDoc, editedRecord ) =>\n\t\t\t\tgetPostChangesFromCRDTDoc(\n\t\t\t\t\tcrdtDoc,\n\t\t\t\t\teditedRecord,\n\t\t\t\t\tsyncedProperties\n\t\t\t\t),\n\n\t\t\t/**\n\t\t\t * Extract changes from a CRDT document that can be used to update the\n\t\t\t * local editor state.\n\t\t\t *\n\t\t\t * @param {import('@wordpress/sync').ObjectData} record\n\t\t\t * @return {Partial< import('@wordpress/sync').ObjectData >} Changes to record\n\t\t\t */\n\t\t\tgetPersistedCRDTDoc: ( record ) => {\n\t\t\t\treturn (\n\t\t\t\t\trecord?.meta?.[ POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE ] ||\n\t\t\t\t\tnull\n\t\t\t\t);\n\t\t\t},\n\t\t};\n\n\t\treturn entity;\n\t} );\n}\n\n/**\n * Returns the list of the taxonomies entities.\n *\n * @return {Promise} Entities promise\n */\nasync function loadTaxonomyEntities() {\n\tconst taxonomies = await apiFetch( {\n\t\tpath: '/wp/v2/taxonomies?context=view',\n\t} );\n\treturn Object.entries( taxonomies ?? {} ).map( ( [ name, taxonomy ] ) => {\n\t\tconst namespace = taxonomy?.rest_namespace ?? 'wp/v2';\n\t\tconst entity = {\n\t\t\tkind: 'taxonomy',\n\t\t\tbaseURL: `/${ namespace }/${ taxonomy.rest_base }`,\n\t\t\tbaseURLParams: { context: 'edit' },\n\t\t\tname,\n\t\t\tlabel: taxonomy.name,\n\t\t\tgetTitle: ( record ) => record?.name,\n\t\t\tsupportsPagination: true,\n\t\t};\n\n\t\tentity.syncConfig = defaultSyncConfig;\n\n\t\treturn entity;\n\t} );\n}\n\n/**\n * Returns the Site entity.\n *\n * @return {Promise} Entity promise\n */\nasync function loadSiteEntity() {\n\tconst entity = {\n\t\tlabel: __( 'Site' ),\n\t\tname: 'site',\n\t\tkind: 'root',\n\t\tkey: false,\n\t\tbaseURL: '/wp/v2/settings',\n\t\tmeta: {},\n\t};\n\n\tconst site = await apiFetch( {\n\t\tpath: entity.baseURL,\n\t\tmethod: 'OPTIONS',\n\t} );\n\n\tconst labels = {};\n\tObject.entries( site?.schema?.properties ?? {} ).forEach(\n\t\t( [ key, value ] ) => {\n\t\t\t// Ignore properties `title` and `type` keys.\n\t\t\tif ( typeof value === 'object' && value.title ) {\n\t\t\t\tlabels[ key ] = value.title;\n\t\t\t}\n\t\t}\n\t);\n\n\treturn [ { ...entity, meta: { labels } } ];\n}\n\n/**\n * Returns the entity's getter method name given its kind and name or plural name.\n *\n * @example\n * ```js\n * const nameSingular = getMethodName( 'root', 'theme', 'get' );\n * // nameSingular is getRootTheme\n *\n * const namePlural = getMethodName( 'root', 'themes', 'set' );\n * // namePlural is setRootThemes\n * ```\n *\n * @param {string} kind Entity kind.\n * @param {string} name Entity name or plural name.\n * @param {string} prefix Function prefix.\n *\n * @return {string} Method name\n */\nexport const getMethodName = ( kind, name, prefix = 'get' ) => {\n\tconst kindPrefix = kind === 'root' ? '' : pascalCase( kind );\n\tconst suffix = pascalCase( name );\n\treturn `${ prefix }${ kindPrefix }${ suffix }`;\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,yBAAwC;AAKxC,uBAAqB;AACrB,oBAAmD;AACnD,kBAAmB;AAKnB,mCAAoC;AACpC,kBAA+B;AAC/B,kBAKO;AAEA,IAAM,qBAAqB;AAClC,IAAM,sBAAsB,CAAE,SAAS,WAAW,SAAU;AAE5D,IAAM,uBAAuB;AAAA,EAC5B,QAAQ;AAAA,IACP,MAAM,CAAE,eAAY,qBAAO,OAAO,SAAS,OAAO,EAAG;AAAA,IACrD,OAAO,CAAE,YAAc;AAAA,MACtB,aAAS,2CAA6B,OAAO,MAAO;AAAA,IACrD;AAAA,EACD;AACD;AAEO,IAAM,qBAAqB;AAAA,EACjC;AAAA,IACC,WAAO,gBAAI,MAAO;AAAA,IAClB,MAAM;AAAA,IACN,KAAK;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IACT,eAAe;AAAA;AAAA;AAAA,MAGd,SAAS;AAAA,QACR;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,Q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e,iBAAiB;AAC/B,QAAM,SAAS;AAAA,IACd,WAAO,gBAAI,MAAO;AAAA,IAClB,MAAM;AAAA,IACN,MAAM;AAAA,IACN,KAAK;AAAA,IACL,SAAS;AAAA,IACT,MAAM,CAAC;AAAA,EACR;AAEA,QAAM,OAAO,UAAM,iBAAAA,SAAU;AAAA,IAC5B,MAAM,OAAO;AAAA,IACb,QAAQ;AAAA,EACT,CAAE;AAEF,QAAM,SAAS,CAAC;AAChB,SAAO,QAAS,MAAM,QAAQ,cAAc,CAAC,CAAE,EAAE;AAAA,IAChD,CAAE,CAAE,KAAK,KAAM,MAAO;AAErB,UAAK,OAAO,UAAU,YAAY,MAAM,OAAQ;AAC/C,eAAQ,GAAI,IAAI,MAAM;AAAA,MACvB;AAAA,IACD;AAAA,EACD;AAEA,SAAO,CAAE,EAAE,GAAG,QAAQ,MAAM,EAAE,OAAO,EAAE,CAAE;AAC1C;AAoBO,IAAM,gBAAgB,CAAE,MAAM,MAAM,SAAS,UAAW;AAC9D,QAAM,aAAa,SAAS,SAAS,SAAK,+BAAY,IAAK;AAC3D,QAAM,aAAS,+BAAY,IAAK;AAChC,SAAO,GAAI,MAAO,GAAI,UAAW,GAAI,MAAO;AAC7C;",
|
|
6
6
|
"names": ["apiFetch"]
|
|
7
7
|
}
|
|
@@ -33,7 +33,7 @@ var import_compose = require("@wordpress/compose");
|
|
|
33
33
|
var import_element = require("@wordpress/element");
|
|
34
34
|
var import_sync = require("../sync.cjs");
|
|
35
35
|
var defaultResolvedSelection = {
|
|
36
|
-
|
|
36
|
+
richTextOffset: null,
|
|
37
37
|
localClientId: null
|
|
38
38
|
};
|
|
39
39
|
var defaultState = {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hooks/use-post-editor-awareness-state.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport { usePrevious } from '@wordpress/compose';\nimport { useEffect, useState } from '@wordpress/element';\nimport type { Y } from '@wordpress/sync';\n\n/**\n * Internal dependencies\n */\nimport { getSyncManager } from '../sync';\nimport type {\n\tPostEditorAwarenessState as ActiveCollaborator,\n\tPostSaveEvent,\n\tYDocDebugData,\n} from '../awareness/types';\nimport type { SelectionState, ResolvedSelection } from '../types';\nimport type { PostEditorAwareness } from '../awareness/post-editor-awareness';\n\ninterface AwarenessState {\n\tactiveCollaborators: ActiveCollaborator[];\n\tresolveSelection: ( selection: SelectionState ) => ResolvedSelection;\n\tgetDebugData: () => YDocDebugData;\n\tisCurrentCollaboratorDisconnected: boolean;\n}\n\nconst defaultResolvedSelection: ResolvedSelection = {\n\ttextIndex: null,\n\tlocalClientId: null,\n};\n\nconst defaultState: AwarenessState = {\n\tactiveCollaborators: [],\n\tresolveSelection: () => defaultResolvedSelection,\n\tgetDebugData: () => ( {\n\t\tdoc: {},\n\t\tclients: {},\n\t\tcollaboratorMap: {},\n\t} ),\n\tisCurrentCollaboratorDisconnected: false,\n};\n\nfunction getAwarenessState(\n\tawareness: PostEditorAwareness,\n\tnewState?: ActiveCollaborator[]\n): AwarenessState {\n\tconst activeCollaborators = newState ?? awareness.getCurrentState();\n\n\treturn {\n\t\tactiveCollaborators,\n\t\tresolveSelection: ( selection: SelectionState ) =>\n\t\t\tawareness.convertSelectionStateToAbsolute( selection ),\n\t\tgetDebugData: () => awareness.getDebugData(),\n\t\tisCurrentCollaboratorDisconnected:\n\t\t\tactiveCollaborators.find( ( collaborator ) => collaborator.isMe )\n\t\t\t\t?.isConnected === false,\n\t};\n}\n\nfunction usePostEditorAwarenessState(\n\tpostId: number | null,\n\tpostType: string | null\n): AwarenessState {\n\tconst [ state, setState ] = useState< AwarenessState >( defaultState );\n\n\tuseEffect( () => {\n\t\tif ( null === postId || null === postType ) {\n\t\t\tsetState( defaultState );\n\t\t\treturn;\n\t\t}\n\n\t\tconst objectType = `postType/${ postType }`;\n\t\tconst objectId = postId.toString();\n\t\tconst awareness = getSyncManager()?.getAwareness< PostEditorAwareness >(\n\t\t\tobjectType,\n\t\t\tobjectId\n\t\t);\n\n\t\tif ( ! awareness ) {\n\t\t\tsetState( defaultState );\n\t\t\treturn;\n\t\t}\n\n\t\tawareness.setUp();\n\n\t\t// Initialize with current awareness state.\n\t\tsetState( getAwarenessState( awareness ) );\n\n\t\tconst unsubscribe = awareness?.onStateChange(\n\t\t\t( newState: ActiveCollaborator[] ) => {\n\t\t\t\tsetState( getAwarenessState( awareness, newState ) );\n\t\t\t}\n\t\t);\n\n\t\treturn unsubscribe;\n\t}, [ postId, postType ] );\n\n\treturn state;\n}\n\n/**\n * Hook to get the active collaborators for a post editor.\n *\n * @param postId - The ID of the post.\n * @param postType - The type of the post.\n * @return {ActiveCollaborator[]} The active collaborators.\n */\nexport function useActiveCollaborators(\n\tpostId: number | null,\n\tpostType: string | null\n): ActiveCollaborator[] {\n\treturn usePostEditorAwarenessState( postId, postType ).activeCollaborators;\n}\n\n/**\n * Hook to resolve a selection state to a text index and block client ID.\n *\n * @param postId - The ID of the post.\n * @param postType - The type of the post.\n * @return A function that resolves a selection to its text index and block client ID.\n */\nexport function useResolvedSelection(\n\tpostId: number | null,\n\tpostType: string | null\n): ( selection: SelectionState ) => ResolvedSelection {\n\treturn usePostEditorAwarenessState( postId, postType ).resolveSelection;\n}\n\n/**\n * Hook to get data for debugging, using the awareness state.\n *\n * @param postId - The ID of the post.\n * @param postType - The type of the post.\n * @return {YDocDebugData} The debug data.\n */\nexport function useGetDebugData(\n\tpostId: number | null,\n\tpostType: string | null\n): YDocDebugData {\n\treturn usePostEditorAwarenessState( postId, postType ).getDebugData();\n}\n\n/**\n * Hook to check if the current collaborator is disconnected.\n *\n * @param postId - The ID of the post.\n * @param postType - The type of the post.\n * @return {boolean} Whether the current collaborator is disconnected.\n */\nexport function useIsDisconnected(\n\tpostId: number | null,\n\tpostType: string | null\n): boolean {\n\treturn usePostEditorAwarenessState( postId, postType )\n\t\t.isCurrentCollaboratorDisconnected;\n}\n\n/**\n * Hook that subscribes to the CRDT state map and returns the most recent\n * save event (timestamp + client ID). The state map is updated by\n * `markEntityAsSaved` in `@wordpress/sync`\n *\n * @param postId The ID of the post.\n * @param postType The type of the post.\n */\nfunction useLastPostSave(\n\tpostId: number | null,\n\tpostType: string | null\n): PostSaveEvent | null {\n\tconst [ lastSave, setLastSave ] = useState< PostSaveEvent | null >( null );\n\n\tuseEffect( () => {\n\t\tif ( null === postId || null === postType ) {\n\t\t\tsetLastSave( null );\n\t\t\treturn;\n\t\t}\n\n\t\tconst awareness = getSyncManager()?.getAwareness< PostEditorAwareness >(\n\t\t\t`postType/${ postType }`,\n\t\t\tpostId.toString()\n\t\t);\n\n\t\tif ( ! awareness ) {\n\t\t\tsetLastSave( null );\n\t\t\treturn;\n\t\t}\n\n\t\tawareness.setUp();\n\n\t\tconst stateMap = awareness.doc.getMap( 'state' );\n\t\tconst recordMap = awareness.doc.getMap( 'document' );\n\n\t\t// Only notify for saves that occur after the observer is\n\t\t// set up. This prevents false notifications when the Y.Doc\n\t\t// syncs historical state on page load or peer reconnect.\n\t\tconst setupTime = Date.now();\n\n\t\tconst observer = ( event: Y.YMapEvent< unknown > ) => {\n\t\t\tif ( event.keysChanged.has( 'savedAt' ) ) {\n\t\t\t\tconst savedAt = stateMap.get( 'savedAt' ) as number;\n\t\t\t\tconst savedByClientId = stateMap.get( 'savedBy' ) as number;\n\n\t\t\t\tif (\n\t\t\t\t\ttypeof savedAt === 'number' &&\n\t\t\t\t\ttypeof savedByClientId === 'number' &&\n\t\t\t\t\tsavedAt > setupTime\n\t\t\t\t) {\n\t\t\t\t\tconst postStatus = recordMap.get( 'status' ) as\n\t\t\t\t\t\t| string\n\t\t\t\t\t\t| undefined;\n\t\t\t\t\tsetLastSave( { savedAt, savedByClientId, postStatus } );\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\n\t\tstateMap.observe( observer );\n\n\t\treturn () => {\n\t\t\tstateMap.unobserve( observer );\n\t\t};\n\t}, [ postId, postType ] );\n\n\treturn lastSave;\n}\n\n/**\n * Hook that fires a callback when a new collaborator joins the post.\n * Handles initial hydration and state diffing internally\u2014consumers\n * only receive \"join\" events for collaborators that appear after the\n * initial state has loaded.\n *\n * The callback receives the joining collaborator and, when available,\n * the current user's state (useful for comparing `enteredAt` timestamps).\n *\n * @param postId The ID of the post.\n * @param postType The type of the post.\n * @param callback Invoked for each collaborator that joins.\n */\nexport function useOnCollaboratorJoin(\n\tpostId: number | null,\n\tpostType: string | null,\n\tcallback: (\n\t\tcollaborator: ActiveCollaborator,\n\t\tme?: ActiveCollaborator\n\t) => void\n): void {\n\tconst { activeCollaborators } = usePostEditorAwarenessState(\n\t\tpostId,\n\t\tpostType\n\t);\n\tconst prevCollaborators = usePrevious( activeCollaborators );\n\n\tuseEffect( () => {\n\t\t/*\n\t\t * On first render usePrevious returns undefined. On subsequent\n\t\t * renders the list may still be empty while the store hydrates.\n\t\t * In both cases, skip to avoid spurious \"joined\" callbacks for\n\t\t * users already present.\n\t\t */\n\t\tif ( ! prevCollaborators || prevCollaborators.length === 0 ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst prevMap = new Map< number, ActiveCollaborator >(\n\t\t\tprevCollaborators.map( ( collaborator ) => [\n\t\t\t\tcollaborator.clientId,\n\t\t\t\tcollaborator,\n\t\t\t] )\n\t\t);\n\t\tconst me = activeCollaborators.find(\n\t\t\t( collaborator ) => collaborator.isMe\n\t\t);\n\n\t\tfor ( const collaborator of activeCollaborators ) {\n\t\t\tif (\n\t\t\t\t! prevMap.has( collaborator.clientId ) &&\n\t\t\t\t! collaborator.isMe\n\t\t\t) {\n\t\t\t\tcallback( collaborator, me );\n\t\t\t}\n\t\t}\n\t}, [ activeCollaborators, prevCollaborators, callback ] );\n}\n\n/**\n * Hook that fires a callback when a collaborator leaves the post.\n * A \"leave\" is detected when a previously-connected collaborator either\n * transitions to `isConnected = false` or disappears from the list\n * entirely while still connected. Already-disconnected collaborators\n * that are later removed from the list are silently ignored.\n *\n * @param postId The ID of the post.\n * @param postType The type of the post.\n * @param callback Invoked for each collaborator that leaves.\n */\nexport function useOnCollaboratorLeave(\n\tpostId: number | null,\n\tpostType: string | null,\n\tcallback: ( collaborator: ActiveCollaborator ) => void\n): void {\n\tconst { activeCollaborators } = usePostEditorAwarenessState(\n\t\tpostId,\n\t\tpostType\n\t);\n\tconst prevCollaborators = usePrevious( activeCollaborators );\n\n\tuseEffect( () => {\n\t\tif ( ! prevCollaborators || prevCollaborators.length === 0 ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst newMap = new Map< number, ActiveCollaborator >(\n\t\t\tactiveCollaborators.map( ( collaborator ) => [\n\t\t\t\tcollaborator.clientId,\n\t\t\t\tcollaborator,\n\t\t\t] )\n\t\t);\n\n\t\tfor ( const prevCollab of prevCollaborators ) {\n\t\t\tif ( prevCollab.isMe || ! prevCollab.isConnected ) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst newCollab = newMap.get( prevCollab.clientId );\n\t\t\tif ( ! newCollab?.isConnected ) {\n\t\t\t\tcallback( prevCollab );\n\t\t\t}\n\t\t}\n\t}, [ activeCollaborators, prevCollaborators, callback ] );\n}\n\n/**\n * Hook that fires a callback when a remote collaborator saves the post.\n * Only fires for saves by other collaborators (not the current user).\n * Deduplicates by `savedAt` timestamp so the same save event is never\n * reported twice.\n *\n * @param postId The ID of the post.\n * @param postType The type of the post.\n * @param callback Invoked with the save event, the collaborator who saved,\n * and the previous save event (if any) for transition detection.\n */\nexport function useOnPostSave(\n\tpostId: number | null,\n\tpostType: string | null,\n\tcallback: (\n\t\tevent: PostSaveEvent,\n\t\tsaver: ActiveCollaborator,\n\t\tprevEvent: PostSaveEvent | null\n\t) => void\n): void {\n\tconst { activeCollaborators } = usePostEditorAwarenessState(\n\t\tpostId,\n\t\tpostType\n\t);\n\tconst lastPostSave = useLastPostSave( postId, postType );\n\tconst prevPostSave = usePrevious( lastPostSave );\n\n\tuseEffect( () => {\n\t\tif ( ! lastPostSave ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( prevPostSave && lastPostSave.savedAt === prevPostSave.savedAt ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst saver = activeCollaborators.find(\n\t\t\t( collaborator ) =>\n\t\t\t\tcollaborator.clientId === lastPostSave.savedByClientId &&\n\t\t\t\t! collaborator.isMe\n\t\t);\n\n\t\tif ( ! saver ) {\n\t\t\treturn;\n\t\t}\n\n\t\tcallback( lastPostSave, saver, prevPostSave ?? null );\n\t}, [ lastPostSave, prevPostSave, activeCollaborators, callback ] );\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAA4B;AAC5B,qBAAoC;AAMpC,kBAA+B;AAgB/B,IAAM,2BAA8C;AAAA,EACnD,
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport { usePrevious } from '@wordpress/compose';\nimport { useEffect, useState } from '@wordpress/element';\nimport type { Y } from '@wordpress/sync';\n\n/**\n * Internal dependencies\n */\nimport { getSyncManager } from '../sync';\nimport type {\n\tPostEditorAwarenessState as ActiveCollaborator,\n\tPostSaveEvent,\n\tYDocDebugData,\n} from '../awareness/types';\nimport type { SelectionState, ResolvedSelection } from '../types';\nimport type { PostEditorAwareness } from '../awareness/post-editor-awareness';\n\ninterface AwarenessState {\n\tactiveCollaborators: ActiveCollaborator[];\n\tresolveSelection: ( selection: SelectionState ) => ResolvedSelection;\n\tgetDebugData: () => YDocDebugData;\n\tisCurrentCollaboratorDisconnected: boolean;\n}\n\nconst defaultResolvedSelection: ResolvedSelection = {\n\trichTextOffset: null,\n\tlocalClientId: null,\n};\n\nconst defaultState: AwarenessState = {\n\tactiveCollaborators: [],\n\tresolveSelection: () => defaultResolvedSelection,\n\tgetDebugData: () => ( {\n\t\tdoc: {},\n\t\tclients: {},\n\t\tcollaboratorMap: {},\n\t} ),\n\tisCurrentCollaboratorDisconnected: false,\n};\n\nfunction getAwarenessState(\n\tawareness: PostEditorAwareness,\n\tnewState?: ActiveCollaborator[]\n): AwarenessState {\n\tconst activeCollaborators = newState ?? awareness.getCurrentState();\n\n\treturn {\n\t\tactiveCollaborators,\n\t\tresolveSelection: ( selection: SelectionState ) =>\n\t\t\tawareness.convertSelectionStateToAbsolute( selection ),\n\t\tgetDebugData: () => awareness.getDebugData(),\n\t\tisCurrentCollaboratorDisconnected:\n\t\t\tactiveCollaborators.find( ( collaborator ) => collaborator.isMe )\n\t\t\t\t?.isConnected === false,\n\t};\n}\n\nfunction usePostEditorAwarenessState(\n\tpostId: number | null,\n\tpostType: string | null\n): AwarenessState {\n\tconst [ state, setState ] = useState< AwarenessState >( defaultState );\n\n\tuseEffect( () => {\n\t\tif ( null === postId || null === postType ) {\n\t\t\tsetState( defaultState );\n\t\t\treturn;\n\t\t}\n\n\t\tconst objectType = `postType/${ postType }`;\n\t\tconst objectId = postId.toString();\n\t\tconst awareness = getSyncManager()?.getAwareness< PostEditorAwareness >(\n\t\t\tobjectType,\n\t\t\tobjectId\n\t\t);\n\n\t\tif ( ! awareness ) {\n\t\t\tsetState( defaultState );\n\t\t\treturn;\n\t\t}\n\n\t\tawareness.setUp();\n\n\t\t// Initialize with current awareness state.\n\t\tsetState( getAwarenessState( awareness ) );\n\n\t\tconst unsubscribe = awareness?.onStateChange(\n\t\t\t( newState: ActiveCollaborator[] ) => {\n\t\t\t\tsetState( getAwarenessState( awareness, newState ) );\n\t\t\t}\n\t\t);\n\n\t\treturn unsubscribe;\n\t}, [ postId, postType ] );\n\n\treturn state;\n}\n\n/**\n * Hook to get the active collaborators for a post editor.\n *\n * @param postId - The ID of the post.\n * @param postType - The type of the post.\n * @return {ActiveCollaborator[]} The active collaborators.\n */\nexport function useActiveCollaborators(\n\tpostId: number | null,\n\tpostType: string | null\n): ActiveCollaborator[] {\n\treturn usePostEditorAwarenessState( postId, postType ).activeCollaborators;\n}\n\n/**\n * Hook to resolve a selection state to a text index and block client ID.\n *\n * @param postId - The ID of the post.\n * @param postType - The type of the post.\n * @return A function that resolves a selection to its text index and block client ID.\n */\nexport function useResolvedSelection(\n\tpostId: number | null,\n\tpostType: string | null\n): ( selection: SelectionState ) => ResolvedSelection {\n\treturn usePostEditorAwarenessState( postId, postType ).resolveSelection;\n}\n\n/**\n * Hook to get data for debugging, using the awareness state.\n *\n * @param postId - The ID of the post.\n * @param postType - The type of the post.\n * @return {YDocDebugData} The debug data.\n */\nexport function useGetDebugData(\n\tpostId: number | null,\n\tpostType: string | null\n): YDocDebugData {\n\treturn usePostEditorAwarenessState( postId, postType ).getDebugData();\n}\n\n/**\n * Hook to check if the current collaborator is disconnected.\n *\n * @param postId - The ID of the post.\n * @param postType - The type of the post.\n * @return {boolean} Whether the current collaborator is disconnected.\n */\nexport function useIsDisconnected(\n\tpostId: number | null,\n\tpostType: string | null\n): boolean {\n\treturn usePostEditorAwarenessState( postId, postType )\n\t\t.isCurrentCollaboratorDisconnected;\n}\n\n/**\n * Hook that subscribes to the CRDT state map and returns the most recent\n * save event (timestamp + client ID). The state map is updated by\n * `markEntityAsSaved` in `@wordpress/sync`\n *\n * @param postId The ID of the post.\n * @param postType The type of the post.\n */\nfunction useLastPostSave(\n\tpostId: number | null,\n\tpostType: string | null\n): PostSaveEvent | null {\n\tconst [ lastSave, setLastSave ] = useState< PostSaveEvent | null >( null );\n\n\tuseEffect( () => {\n\t\tif ( null === postId || null === postType ) {\n\t\t\tsetLastSave( null );\n\t\t\treturn;\n\t\t}\n\n\t\tconst awareness = getSyncManager()?.getAwareness< PostEditorAwareness >(\n\t\t\t`postType/${ postType }`,\n\t\t\tpostId.toString()\n\t\t);\n\n\t\tif ( ! awareness ) {\n\t\t\tsetLastSave( null );\n\t\t\treturn;\n\t\t}\n\n\t\tawareness.setUp();\n\n\t\tconst stateMap = awareness.doc.getMap( 'state' );\n\t\tconst recordMap = awareness.doc.getMap( 'document' );\n\n\t\t// Only notify for saves that occur after the observer is\n\t\t// set up. This prevents false notifications when the Y.Doc\n\t\t// syncs historical state on page load or peer reconnect.\n\t\tconst setupTime = Date.now();\n\n\t\tconst observer = ( event: Y.YMapEvent< unknown > ) => {\n\t\t\tif ( event.keysChanged.has( 'savedAt' ) ) {\n\t\t\t\tconst savedAt = stateMap.get( 'savedAt' ) as number;\n\t\t\t\tconst savedByClientId = stateMap.get( 'savedBy' ) as number;\n\n\t\t\t\tif (\n\t\t\t\t\ttypeof savedAt === 'number' &&\n\t\t\t\t\ttypeof savedByClientId === 'number' &&\n\t\t\t\t\tsavedAt > setupTime\n\t\t\t\t) {\n\t\t\t\t\tconst postStatus = recordMap.get( 'status' ) as\n\t\t\t\t\t\t| string\n\t\t\t\t\t\t| undefined;\n\t\t\t\t\tsetLastSave( { savedAt, savedByClientId, postStatus } );\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\n\t\tstateMap.observe( observer );\n\n\t\treturn () => {\n\t\t\tstateMap.unobserve( observer );\n\t\t};\n\t}, [ postId, postType ] );\n\n\treturn lastSave;\n}\n\n/**\n * Hook that fires a callback when a new collaborator joins the post.\n * Handles initial hydration and state diffing internally\u2014consumers\n * only receive \"join\" events for collaborators that appear after the\n * initial state has loaded.\n *\n * The callback receives the joining collaborator and, when available,\n * the current user's state (useful for comparing `enteredAt` timestamps).\n *\n * @param postId The ID of the post.\n * @param postType The type of the post.\n * @param callback Invoked for each collaborator that joins.\n */\nexport function useOnCollaboratorJoin(\n\tpostId: number | null,\n\tpostType: string | null,\n\tcallback: (\n\t\tcollaborator: ActiveCollaborator,\n\t\tme?: ActiveCollaborator\n\t) => void\n): void {\n\tconst { activeCollaborators } = usePostEditorAwarenessState(\n\t\tpostId,\n\t\tpostType\n\t);\n\tconst prevCollaborators = usePrevious( activeCollaborators );\n\n\tuseEffect( () => {\n\t\t/*\n\t\t * On first render usePrevious returns undefined. On subsequent\n\t\t * renders the list may still be empty while the store hydrates.\n\t\t * In both cases, skip to avoid spurious \"joined\" callbacks for\n\t\t * users already present.\n\t\t */\n\t\tif ( ! prevCollaborators || prevCollaborators.length === 0 ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst prevMap = new Map< number, ActiveCollaborator >(\n\t\t\tprevCollaborators.map( ( collaborator ) => [\n\t\t\t\tcollaborator.clientId,\n\t\t\t\tcollaborator,\n\t\t\t] )\n\t\t);\n\t\tconst me = activeCollaborators.find(\n\t\t\t( collaborator ) => collaborator.isMe\n\t\t);\n\n\t\tfor ( const collaborator of activeCollaborators ) {\n\t\t\tif (\n\t\t\t\t! prevMap.has( collaborator.clientId ) &&\n\t\t\t\t! collaborator.isMe\n\t\t\t) {\n\t\t\t\tcallback( collaborator, me );\n\t\t\t}\n\t\t}\n\t}, [ activeCollaborators, prevCollaborators, callback ] );\n}\n\n/**\n * Hook that fires a callback when a collaborator leaves the post.\n * A \"leave\" is detected when a previously-connected collaborator either\n * transitions to `isConnected = false` or disappears from the list\n * entirely while still connected. Already-disconnected collaborators\n * that are later removed from the list are silently ignored.\n *\n * @param postId The ID of the post.\n * @param postType The type of the post.\n * @param callback Invoked for each collaborator that leaves.\n */\nexport function useOnCollaboratorLeave(\n\tpostId: number | null,\n\tpostType: string | null,\n\tcallback: ( collaborator: ActiveCollaborator ) => void\n): void {\n\tconst { activeCollaborators } = usePostEditorAwarenessState(\n\t\tpostId,\n\t\tpostType\n\t);\n\tconst prevCollaborators = usePrevious( activeCollaborators );\n\n\tuseEffect( () => {\n\t\tif ( ! prevCollaborators || prevCollaborators.length === 0 ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst newMap = new Map< number, ActiveCollaborator >(\n\t\t\tactiveCollaborators.map( ( collaborator ) => [\n\t\t\t\tcollaborator.clientId,\n\t\t\t\tcollaborator,\n\t\t\t] )\n\t\t);\n\n\t\tfor ( const prevCollab of prevCollaborators ) {\n\t\t\tif ( prevCollab.isMe || ! prevCollab.isConnected ) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst newCollab = newMap.get( prevCollab.clientId );\n\t\t\tif ( ! newCollab?.isConnected ) {\n\t\t\t\tcallback( prevCollab );\n\t\t\t}\n\t\t}\n\t}, [ activeCollaborators, prevCollaborators, callback ] );\n}\n\n/**\n * Hook that fires a callback when a remote collaborator saves the post.\n * Only fires for saves by other collaborators (not the current user).\n * Deduplicates by `savedAt` timestamp so the same save event is never\n * reported twice.\n *\n * @param postId The ID of the post.\n * @param postType The type of the post.\n * @param callback Invoked with the save event, the collaborator who saved,\n * and the previous save event (if any) for transition detection.\n */\nexport function useOnPostSave(\n\tpostId: number | null,\n\tpostType: string | null,\n\tcallback: (\n\t\tevent: PostSaveEvent,\n\t\tsaver: ActiveCollaborator,\n\t\tprevEvent: PostSaveEvent | null\n\t) => void\n): void {\n\tconst { activeCollaborators } = usePostEditorAwarenessState(\n\t\tpostId,\n\t\tpostType\n\t);\n\tconst lastPostSave = useLastPostSave( postId, postType );\n\tconst prevPostSave = usePrevious( lastPostSave );\n\n\tuseEffect( () => {\n\t\tif ( ! lastPostSave ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( prevPostSave && lastPostSave.savedAt === prevPostSave.savedAt ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst saver = activeCollaborators.find(\n\t\t\t( collaborator ) =>\n\t\t\t\tcollaborator.clientId === lastPostSave.savedByClientId &&\n\t\t\t\t! collaborator.isMe\n\t\t);\n\n\t\tif ( ! saver ) {\n\t\t\treturn;\n\t\t}\n\n\t\tcallback( lastPostSave, saver, prevPostSave ?? null );\n\t}, [ lastPostSave, prevPostSave, activeCollaborators, callback ] );\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAA4B;AAC5B,qBAAoC;AAMpC,kBAA+B;AAgB/B,IAAM,2BAA8C;AAAA,EACnD,gBAAgB;AAAA,EAChB,eAAe;AAChB;AAEA,IAAM,eAA+B;AAAA,EACpC,qBAA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e;AACrB;AAAA,IACD;AAEA,QAAK,gBAAgB,aAAa,YAAY,aAAa,SAAU;AACpE;AAAA,IACD;AAEA,UAAM,QAAQ,oBAAoB;AAAA,MACjC,CAAE,iBACD,aAAa,aAAa,aAAa,mBACvC,CAAE,aAAa;AAAA,IACjB;AAEA,QAAK,CAAE,OAAQ;AACd;AAAA,IACD;AAEA,aAAU,cAAc,OAAO,gBAAgB,IAAK;AAAA,EACrD,GAAG,CAAE,cAAc,cAAc,qBAAqB,QAAS,CAAE;AAClE;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -31,6 +31,7 @@ function getQueryParts(query) {
|
|
|
31
31
|
stableKey: "",
|
|
32
32
|
page: 1,
|
|
33
33
|
perPage: 10,
|
|
34
|
+
offset: void 0,
|
|
34
35
|
fields: null,
|
|
35
36
|
include: null,
|
|
36
37
|
context: "default"
|
|
@@ -50,6 +51,12 @@ function getQueryParts(query) {
|
|
|
50
51
|
parts.context = value;
|
|
51
52
|
break;
|
|
52
53
|
default:
|
|
54
|
+
if (key === "offset") {
|
|
55
|
+
const numericOffset = Number(value);
|
|
56
|
+
if (Number.isFinite(numericOffset)) {
|
|
57
|
+
parts.offset = numericOffset;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
53
60
|
if (key === "_fields") {
|
|
54
61
|
parts.fields = (0, import_utils.getNormalizedCommaSeparable)(value) ?? [];
|
|
55
62
|
value = parts.fields.join();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/queried-data/get-query-parts.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\n\n/**\n * Internal dependencies\n */\nimport { withWeakMapCache, getNormalizedCommaSeparable } from '../utils';\n\n/**\n * An object of properties describing a specific query.\n *\n * @typedef {Object} WPQueriedDataQueryParts\n *\n * @property {number} page The query page (1-based index, default 1).\n * @property {number} perPage Items per page for query (default 10).\n * @property {string} stableKey An encoded stable string of all non-\n * pagination, non-fields query parameters.\n * @property {?(string[])} fields Target subset of fields to derive from\n * item objects.\n * @property {?(number[])} include Specific item IDs to include.\n * @property {string} context Scope under which the request is made;\n * determines returned fields in response.\n */\n\n/**\n * Given a query object, returns an object of parts, including pagination\n * details (`page` and `perPage`, or default values). All other properties are\n * encoded into a stable (idempotent) `stableKey` value.\n *\n * @param {Object} query Optional query object.\n *\n * @return {WPQueriedDataQueryParts} Query parts.\n */\nexport function getQueryParts( query ) {\n\t/**\n\t * @type {WPQueriedDataQueryParts}\n\t */\n\tconst parts = {\n\t\tstableKey: '',\n\t\tpage: 1,\n\t\tperPage: 10,\n\t\tfields: null,\n\t\tinclude: null,\n\t\tcontext: 'default',\n\t};\n\n\t// Ensure stable key by sorting keys. Also more efficient for iterating.\n\tconst keys = Object.keys( query ).sort();\n\n\tfor ( let i = 0; i < keys.length; i++ ) {\n\t\tconst key = keys[ i ];\n\t\tlet value = query[ key ];\n\n\t\tswitch ( key ) {\n\t\t\tcase 'page':\n\t\t\t\tparts[ key ] = Number( value );\n\t\t\t\tbreak;\n\n\t\t\tcase 'per_page':\n\t\t\t\tparts.perPage = Number( value );\n\t\t\t\tbreak;\n\n\t\t\tcase 'context':\n\t\t\t\tparts.context = value;\n\t\t\t\tbreak;\n\n\t\t\tdefault:\n\t\t\t\t// While in theory, we could exclude \"_fields\" from the stableKey\n\t\t\t\t// because two request with different fields have the same results\n\t\t\t\t// We're not able to ensure that because the server can decide to omit\n\t\t\t\t// fields from the response even if we explicitly asked for it.\n\t\t\t\t// Example: Asking for titles in posts without title support.\n\t\t\t\tif ( key === '_fields' ) {\n\t\t\t\t\tparts.fields = getNormalizedCommaSeparable( value ) ?? [];\n\t\t\t\t\t// Make sure to normalize value for `stableKey`\n\t\t\t\t\tvalue = parts.fields.join();\n\t\t\t\t}\n\n\t\t\t\t// Two requests with different include values cannot have same results.\n\t\t\t\tif ( key === 'include' ) {\n\t\t\t\t\tif ( typeof value === 'number' ) {\n\t\t\t\t\t\tvalue = value.toString();\n\t\t\t\t\t}\n\t\t\t\t\tparts.include = (\n\t\t\t\t\t\tgetNormalizedCommaSeparable( value ) ?? []\n\t\t\t\t\t).map( Number );\n\t\t\t\t\t// Normalize value for `stableKey`.\n\t\t\t\t\tvalue = parts.include.join();\n\t\t\t\t}\n\n\t\t\t\t// While it could be any deterministic string, for simplicity's\n\t\t\t\t// sake mimic querystring encoding for stable key.\n\t\t\t\t//\n\t\t\t\t// TODO: For consistency with PHP implementation, addQueryArgs\n\t\t\t\t// should accept a key value pair, which may optimize its\n\t\t\t\t// implementation for our use here, vs. iterating an object\n\t\t\t\t// with only a single key.\n\t\t\t\tparts.stableKey +=\n\t\t\t\t\t( parts.stableKey ? '&' : '' ) +\n\t\t\t\t\taddQueryArgs( '', { [ key ]: value } ).slice( 1 );\n\t\t}\n\t}\n\n\treturn parts;\n}\n\nexport default withWeakMapCache( getQueryParts );\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAA6B;AAK7B,mBAA8D;
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\n\n/**\n * Internal dependencies\n */\nimport { withWeakMapCache, getNormalizedCommaSeparable } from '../utils';\n\n/**\n * An object of properties describing a specific query.\n *\n * @typedef {Object} WPQueriedDataQueryParts\n *\n * @property {number} page The query page (1-based index, default 1).\n * @property {number} perPage Items per page for query (default 10).\n * @property {number} offset Absolute item offset (default undefined).\n * When present, also encoded into stableKey.\n * @property {string} stableKey An encoded stable string of all non-\n * pagination, non-fields query parameters.\n * @property {?(string[])} fields Target subset of fields to derive from\n * item objects.\n * @property {?(number[])} include Specific item IDs to include.\n * @property {string} context Scope under which the request is made;\n * determines returned fields in response.\n */\n\n/**\n * Given a query object, returns an object of parts, including pagination\n * details (`page` and `perPage`, or default values). All other properties are\n * encoded into a stable (idempotent) `stableKey` value.\n *\n * @param {Object} query Optional query object.\n *\n * @return {WPQueriedDataQueryParts} Query parts.\n */\nexport function getQueryParts( query ) {\n\t/**\n\t * @type {WPQueriedDataQueryParts}\n\t */\n\tconst parts = {\n\t\tstableKey: '',\n\t\tpage: 1,\n\t\tperPage: 10,\n\t\toffset: undefined,\n\t\tfields: null,\n\t\tinclude: null,\n\t\tcontext: 'default',\n\t};\n\n\t// Ensure stable key by sorting keys. Also more efficient for iterating.\n\tconst keys = Object.keys( query ).sort();\n\n\tfor ( let i = 0; i < keys.length; i++ ) {\n\t\tconst key = keys[ i ];\n\t\tlet value = query[ key ];\n\n\t\tswitch ( key ) {\n\t\t\tcase 'page':\n\t\t\t\tparts[ key ] = Number( value );\n\t\t\t\tbreak;\n\n\t\t\tcase 'per_page':\n\t\t\t\tparts.perPage = Number( value );\n\t\t\t\tbreak;\n\n\t\t\tcase 'context':\n\t\t\t\tparts.context = value;\n\t\t\t\tbreak;\n\n\t\t\tdefault:\n\t\t\t\t// Extract offset for use in pagination calculations while\n\t\t\t\t// still including it in the stableKey (different offsets\n\t\t\t\t// produce different result sets).\n\t\t\t\tif ( key === 'offset' ) {\n\t\t\t\t\tconst numericOffset = Number( value );\n\t\t\t\t\tif ( Number.isFinite( numericOffset ) ) {\n\t\t\t\t\t\tparts.offset = numericOffset;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// While in theory, we could exclude \"_fields\" from the stableKey\n\t\t\t\t// because two request with different fields have the same results\n\t\t\t\t// We're not able to ensure that because the server can decide to omit\n\t\t\t\t// fields from the response even if we explicitly asked for it.\n\t\t\t\t// Example: Asking for titles in posts without title support.\n\t\t\t\tif ( key === '_fields' ) {\n\t\t\t\t\tparts.fields = getNormalizedCommaSeparable( value ) ?? [];\n\t\t\t\t\t// Make sure to normalize value for `stableKey`\n\t\t\t\t\tvalue = parts.fields.join();\n\t\t\t\t}\n\n\t\t\t\t// Two requests with different include values cannot have same results.\n\t\t\t\tif ( key === 'include' ) {\n\t\t\t\t\tif ( typeof value === 'number' ) {\n\t\t\t\t\t\tvalue = value.toString();\n\t\t\t\t\t}\n\t\t\t\t\tparts.include = (\n\t\t\t\t\t\tgetNormalizedCommaSeparable( value ) ?? []\n\t\t\t\t\t).map( Number );\n\t\t\t\t\t// Normalize value for `stableKey`.\n\t\t\t\t\tvalue = parts.include.join();\n\t\t\t\t}\n\n\t\t\t\t// While it could be any deterministic string, for simplicity's\n\t\t\t\t// sake mimic querystring encoding for stable key.\n\t\t\t\t//\n\t\t\t\t// TODO: For consistency with PHP implementation, addQueryArgs\n\t\t\t\t// should accept a key value pair, which may optimize its\n\t\t\t\t// implementation for our use here, vs. iterating an object\n\t\t\t\t// with only a single key.\n\t\t\t\tparts.stableKey +=\n\t\t\t\t\t( parts.stableKey ? '&' : '' ) +\n\t\t\t\t\taddQueryArgs( '', { [ key ]: value } ).slice( 1 );\n\t\t}\n\t}\n\n\treturn parts;\n}\n\nexport default withWeakMapCache( getQueryParts );\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAA6B;AAK7B,mBAA8D;AA6BvD,SAAS,cAAe,OAAQ;AAItC,QAAM,QAAQ;AAAA,IACb,WAAW;AAAA,IACX,MAAM;AAAA,IACN,SAAS;AAAA,IACT,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,SAAS;AAAA,IACT,SAAS;AAAA,EACV;AAGA,QAAM,OAAO,OAAO,KAAM,KAAM,EAAE,KAAK;AAEvC,WAAU,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAM;AACvC,UAAM,MAAM,KAAM,CAAE;AACpB,QAAI,QAAQ,MAAO,GAAI;AAEvB,YAAS,KAAM;AAAA,MACd,KAAK;AACJ,cAAO,GAAI,IAAI,OAAQ,KAAM;AAC7B;AAAA,MAED,KAAK;AACJ,cAAM,UAAU,OAAQ,KAAM;AAC9B;AAAA,MAED,KAAK;AACJ,cAAM,UAAU;AAChB;AAAA,MAED;AAIC,YAAK,QAAQ,UAAW;AACvB,gBAAM,gBAAgB,OAAQ,KAAM;AACpC,cAAK,OAAO,SAAU,aAAc,GAAI;AACvC,kBAAM,SAAS;AAAA,UAChB;AAAA,QACD;AAOA,YAAK,QAAQ,WAAY;AACxB,gBAAM,aAAS,0CAA6B,KAAM,KAAK,CAAC;AAExD,kBAAQ,MAAM,OAAO,KAAK;AAAA,QAC3B;AAGA,YAAK,QAAQ,WAAY;AACxB,cAAK,OAAO,UAAU,UAAW;AAChC,oBAAQ,MAAM,SAAS;AAAA,UACxB;AACA,gBAAM,eACL,0CAA6B,KAAM,KAAK,CAAC,GACxC,IAAK,MAAO;AAEd,kBAAQ,MAAM,QAAQ,KAAK;AAAA,QAC5B;AASA,cAAM,cACH,MAAM,YAAY,MAAM,UAC1B,yBAAc,IAAI,EAAE,CAAE,GAAI,GAAG,MAAM,CAAE,EAAE,MAAO,CAAE;AAAA,IACnD;AAAA,EACD;AAEA,SAAO;AACR;AAEA,IAAO,8BAAQ,+BAAkB,aAAc;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -41,7 +41,15 @@ var import_get_query_parts = __toESM(require("./get-query-parts.cjs"));
|
|
|
41
41
|
var import_utils = require("../utils/index.cjs");
|
|
42
42
|
var queriedItemsCacheByState = /* @__PURE__ */ new WeakMap();
|
|
43
43
|
function getQueriedItemsUncached(state, query) {
|
|
44
|
-
const {
|
|
44
|
+
const {
|
|
45
|
+
stableKey,
|
|
46
|
+
page,
|
|
47
|
+
perPage,
|
|
48
|
+
offset: queryOffset,
|
|
49
|
+
include,
|
|
50
|
+
fields,
|
|
51
|
+
context
|
|
52
|
+
} = (0, import_get_query_parts.default)(query);
|
|
45
53
|
const itemIds = state.queries?.[context]?.[stableKey]?.itemIds;
|
|
46
54
|
if (!itemIds) {
|
|
47
55
|
return null;
|
|
@@ -50,8 +58,11 @@ function getQueriedItemsUncached(state, query) {
|
|
|
50
58
|
const endOffset = perPage === -1 ? itemIds.length : Math.min(startOffset + perPage, itemIds.length);
|
|
51
59
|
if (perPage !== -1 && itemIds.length < startOffset + perPage) {
|
|
52
60
|
const totalItems = state.queries[context][stableKey].meta?.totalItems;
|
|
53
|
-
if (Number.isFinite(totalItems)
|
|
54
|
-
|
|
61
|
+
if (Number.isFinite(totalItems)) {
|
|
62
|
+
const effectiveTotal = queryOffset !== void 0 ? totalItems - queryOffset : totalItems;
|
|
63
|
+
if (itemIds.length < effectiveTotal) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
55
66
|
}
|
|
56
67
|
}
|
|
57
68
|
const items = [];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/queried-data/selectors.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport EquivalentKeyMap from 'equivalent-key-map';\n\n/**\n * WordPress dependencies\n */\nimport { createSelector } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport getQueryParts from './get-query-parts';\nimport { setNestedValue } from '../utils';\n\n/**\n * Cache of state keys to EquivalentKeyMap where the inner map tracks queries\n * to their resulting items set. WeakMap allows garbage collection on expired\n * state references.\n *\n * @type {WeakMap<Object,EquivalentKeyMap>}\n */\nconst queriedItemsCacheByState = new WeakMap();\n\n/**\n * Returns items for a given query, or null if the items are not known.\n *\n * @param {Object} state State object.\n * @param {?Object} query Optional query.\n *\n * @return {?Array} Query items.\n */\nfunction getQueriedItemsUncached( state, query ) {\n\tconst {
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,gCAA6B;AAK7B,kBAA+B;AAK/B,6BAA0B;AAC1B,mBAA+B;AAS/B,IAAM,2BAA2B,oBAAI,QAAQ;AAU7C,SAAS,wBAAyB,OAAO,OAAQ;AAChD,QAAM,
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport EquivalentKeyMap from 'equivalent-key-map';\n\n/**\n * WordPress dependencies\n */\nimport { createSelector } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport getQueryParts from './get-query-parts';\nimport { setNestedValue } from '../utils';\n\n/**\n * Cache of state keys to EquivalentKeyMap where the inner map tracks queries\n * to their resulting items set. WeakMap allows garbage collection on expired\n * state references.\n *\n * @type {WeakMap<Object,EquivalentKeyMap>}\n */\nconst queriedItemsCacheByState = new WeakMap();\n\n/**\n * Returns items for a given query, or null if the items are not known.\n *\n * @param {Object} state State object.\n * @param {?Object} query Optional query.\n *\n * @return {?Array} Query items.\n */\nfunction getQueriedItemsUncached( state, query ) {\n\tconst {\n\t\tstableKey,\n\t\tpage,\n\t\tperPage,\n\t\toffset: queryOffset,\n\t\tinclude,\n\t\tfields,\n\t\tcontext,\n\t} = getQueryParts( query );\n\n\tconst itemIds = state.queries?.[ context ]?.[ stableKey ]?.itemIds;\n\tif ( ! itemIds ) {\n\t\treturn null;\n\t}\n\n\tconst startOffset = perPage === -1 ? 0 : ( page - 1 ) * perPage;\n\tconst endOffset =\n\t\tperPage === -1\n\t\t\t? itemIds.length\n\t\t\t: Math.min( startOffset + perPage, itemIds.length );\n\n\t// If the requested page range exceeds the stored itemIds, the data for\n\t// this specific pagination window may not have been fetched yet. Return\n\t// null unless totalItems confirms we already have all available items.\n\tif ( perPage !== -1 && itemIds.length < startOffset + perPage ) {\n\t\tconst totalItems =\n\t\t\tstate.queries[ context ][ stableKey ].meta?.totalItems;\n\t\tif ( Number.isFinite( totalItems ) ) {\n\t\t\t// For offset-based queries, totalItems (from X-WP-Total)\n\t\t\t// reflects the global count of all matching items, not the\n\t\t\t// count remaining after the offset. The number of items\n\t\t\t// available for this query is (totalItems - offset), so a\n\t\t\t// partial last page is expected and valid.\n\t\t\tconst effectiveTotal =\n\t\t\t\tqueryOffset !== undefined\n\t\t\t\t\t? totalItems - queryOffset\n\t\t\t\t\t: totalItems;\n\t\t\tif ( itemIds.length < effectiveTotal ) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n\t}\n\n\tconst items = [];\n\tfor ( let i = startOffset; i < endOffset; i++ ) {\n\t\tconst itemId = itemIds[ i ];\n\t\tif ( Array.isArray( include ) && ! include.includes( itemId ) ) {\n\t\t\tcontinue;\n\t\t}\n\t\tif ( itemId === undefined ) {\n\t\t\tcontinue;\n\t\t}\n\t\t// Having a target item ID doesn't guarantee that this object has been queried.\n\t\tif ( ! state.items[ context ]?.hasOwnProperty( itemId ) ) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst item = state.items[ context ][ itemId ];\n\n\t\tlet filteredItem;\n\t\tif ( Array.isArray( fields ) ) {\n\t\t\tfilteredItem = {};\n\n\t\t\tfor ( let f = 0; f < fields.length; f++ ) {\n\t\t\t\tconst field = fields[ f ].split( '.' );\n\t\t\t\tlet value = item;\n\t\t\t\tfield.forEach( ( fieldName ) => {\n\t\t\t\t\tvalue = value?.[ fieldName ];\n\t\t\t\t} );\n\n\t\t\t\tsetNestedValue( filteredItem, field, value );\n\t\t\t}\n\t\t} else {\n\t\t\t// If expecting a complete item, validate that completeness, or\n\t\t\t// otherwise abort.\n\t\t\tif ( ! state.itemIsComplete[ context ]?.[ itemId ] ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tfilteredItem = item;\n\t\t}\n\n\t\titems.push( filteredItem );\n\t}\n\n\treturn items;\n}\n\n/**\n * Returns items for a given query, or null if the items are not known. Caches\n * result both per state (by reference) and per query (by deep equality).\n * The caching approach is intended to be durable to query objects which are\n * deeply but not referentially equal, since otherwise:\n *\n * `getQueriedItems( state, {} ) !== getQueriedItems( state, {} )`\n *\n * @param {Object} state State object.\n * @param {?Object} query Optional query.\n *\n * @return {?Array} Query items.\n */\nexport const getQueriedItems = createSelector( ( state, query = {} ) => {\n\tlet queriedItemsCache = queriedItemsCacheByState.get( state );\n\tif ( queriedItemsCache ) {\n\t\tconst queriedItems = queriedItemsCache.get( query );\n\t\tif ( queriedItems !== undefined ) {\n\t\t\treturn queriedItems;\n\t\t}\n\t} else {\n\t\tqueriedItemsCache = new EquivalentKeyMap();\n\t\tqueriedItemsCacheByState.set( state, queriedItemsCache );\n\t}\n\n\tconst items = getQueriedItemsUncached( state, query );\n\tqueriedItemsCache.set( query, items );\n\treturn items;\n} );\n\nexport function getQueriedTotalItems( state, query = {} ) {\n\tconst { stableKey, context } = getQueryParts( query );\n\n\treturn state.queries?.[ context ]?.[ stableKey ]?.meta?.totalItems ?? null;\n}\n\nexport function getQueriedTotalPages( state, query = {} ) {\n\tconst { stableKey, context } = getQueryParts( query );\n\n\treturn state.queries?.[ context ]?.[ stableKey ]?.meta?.totalPages ?? null;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,gCAA6B;AAK7B,kBAA+B;AAK/B,6BAA0B;AAC1B,mBAA+B;AAS/B,IAAM,2BAA2B,oBAAI,QAAQ;AAU7C,SAAS,wBAAyB,OAAO,OAAQ;AAChD,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,IACA;AAAA,IACA;AAAA,EACD,QAAI,uBAAAA,SAAe,KAAM;AAEzB,QAAM,UAAU,MAAM,UAAW,OAAQ,IAAK,SAAU,GAAG;AAC3D,MAAK,CAAE,SAAU;AAChB,WAAO;AAAA,EACR;AAEA,QAAM,cAAc,YAAY,KAAK,KAAM,OAAO,KAAM;AACxD,QAAM,YACL,YAAY,KACT,QAAQ,SACR,KAAK,IAAK,cAAc,SAAS,QAAQ,MAAO;AAKpD,MAAK,YAAY,MAAM,QAAQ,SAAS,cAAc,SAAU;AAC/D,UAAM,aACL,MAAM,QAAS,OAAQ,EAAG,SAAU,EAAE,MAAM;AAC7C,QAAK,OAAO,SAAU,UAAW,GAAI;AAMpC,YAAM,iBACL,gBAAgB,SACb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AkB;AAAA,EACxD;AAEA,QAAM,QAAQ,wBAAyB,OAAO,KAAM;AACpD,oBAAkB,IAAK,OAAO,KAAM;AACpC,SAAO;AACR,CAAE;AAEK,SAAS,qBAAsB,OAAO,QAAQ,CAAC,GAAI;AACzD,QAAM,EAAE,WAAW,QAAQ,QAAI,uBAAAD,SAAe,KAAM;AAEpD,SAAO,MAAM,UAAW,OAAQ,IAAK,SAAU,GAAG,MAAM,cAAc;AACvE;AAEO,SAAS,qBAAsB,OAAO,QAAQ,CAAC,GAAI;AACzD,QAAM,EAAE,WAAW,QAAQ,QAAI,uBAAAA,SAAe,KAAM;AAEpD,SAAO,MAAM,UAAW,OAAQ,IAAK,SAAU,GAAG,MAAM,cAAc;AACvE;",
|
|
6
6
|
"names": ["getQueryParts", "EquivalentKeyMap"]
|
|
7
7
|
}
|
package/build/reducer.cjs
CHANGED
|
@@ -64,6 +64,7 @@ var import_undo_manager = require("@wordpress/undo-manager");
|
|
|
64
64
|
var import_utils = require("./utils/index.cjs");
|
|
65
65
|
var import_queried_data = require("./queried-data/index.cjs");
|
|
66
66
|
var import_entities = require("./entities.cjs");
|
|
67
|
+
var import_sync = require("./sync.cjs");
|
|
67
68
|
function users(state = { byId: {}, queries: {} }, action) {
|
|
68
69
|
switch (action.type) {
|
|
69
70
|
case "RECEIVE_USER_QUERY":
|
|
@@ -478,6 +479,11 @@ function collaborationSupported(state = true, action) {
|
|
|
478
479
|
switch (action.type) {
|
|
479
480
|
case "SET_COLLABORATION_SUPPORTED":
|
|
480
481
|
return action.supported;
|
|
482
|
+
case "SET_SYNC_CONNECTION_STATUS":
|
|
483
|
+
if (import_sync.ConnectionErrorCode.DOCUMENT_SIZE_LIMIT_EXCEEDED === action.status?.error?.code) {
|
|
484
|
+
return false;
|
|
485
|
+
}
|
|
486
|
+
return state;
|
|
481
487
|
}
|
|
482
488
|
return state;
|
|
483
489
|
}
|