-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
183 lines (172 loc) · 4.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
AsyncModeProvider,
useSelect,
useDispatch,
useRegistry,
} from '@wordpress/data';
import {
useViewportMatch,
useMergeRefs,
useDebounce,
} from '@wordpress/compose';
import { createContext, useMemo, useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import BlockListBlock from './block';
import BlockListAppender from '../block-list-appender';
import { useInBetweenInserter } from './use-in-between-inserter';
import { store as blockEditorStore } from '../../store';
import { LayoutProvider, defaultLayout } from './layout';
import { useBlockSelectionClearer } from '../block-selection-clearer';
import { useInnerBlocksProps } from '../inner-blocks';
import {
BlockEditContextProvider,
DEFAULT_BLOCK_EDIT_CONTEXT,
} from '../block-edit/context';
import { useTypingObserver } from '../observe-typing';
export const IntersectionObserver = createContext();
const pendingBlockVisibilityUpdatesPerRegistry = new WeakMap();
function Root( { className, ...settings } ) {
const isLargeViewport = useViewportMatch( 'medium' );
const { isOutlineMode, isFocusMode, editorMode } = useSelect(
( select ) => {
const { getSettings, __unstableGetEditorMode } =
select( blockEditorStore );
const { outlineMode, focusMode } = getSettings();
return {
isOutlineMode: outlineMode,
isFocusMode: focusMode,
editorMode: __unstableGetEditorMode(),
};
},
[]
);
const registry = useRegistry();
const { setBlockVisibility } = useDispatch( blockEditorStore );
const delayedBlockVisibilityUpdates = useDebounce(
useCallback( () => {
const updates = {};
pendingBlockVisibilityUpdatesPerRegistry
.get( registry )
.forEach( ( [ id, isIntersecting ] ) => {
updates[ id ] = isIntersecting;
} );
setBlockVisibility( updates );
}, [ registry ] ),
300,
{
trailing: true,
}
);
const intersectionObserver = useMemo( () => {
const { IntersectionObserver: Observer } = window;
if ( ! Observer ) {
return;
}
return new Observer( ( entries ) => {
if ( ! pendingBlockVisibilityUpdatesPerRegistry.get( registry ) ) {
pendingBlockVisibilityUpdatesPerRegistry.set( registry, [] );
}
for ( const entry of entries ) {
const clientId = entry.target.getAttribute( 'data-block' );
pendingBlockVisibilityUpdatesPerRegistry
.get( registry )
.push( [ clientId, entry.isIntersecting ] );
}
delayedBlockVisibilityUpdates();
} );
}, [] );
const innerBlocksProps = useInnerBlocksProps(
{
ref: useMergeRefs( [
useBlockSelectionClearer(),
useInBetweenInserter(),
useTypingObserver(),
] ),
className: classnames( 'is-root-container', className, {
'is-outline-mode': isOutlineMode,
'is-focus-mode': isFocusMode && isLargeViewport,
'is-navigate-mode': editorMode === 'navigation',
} ),
},
settings
);
return (
<IntersectionObserver.Provider value={ intersectionObserver }>
<div { ...innerBlocksProps } />
</IntersectionObserver.Provider>
);
}
export default function BlockList( settings ) {
return (
<BlockEditContextProvider value={ DEFAULT_BLOCK_EDIT_CONTEXT }>
<Root { ...settings } />
</BlockEditContextProvider>
);
}
function Items( {
placeholder,
rootClientId,
renderAppender,
__experimentalAppenderTagName,
layout = defaultLayout,
} ) {
const { order, selectedBlocks, visibleBlocks } = useSelect(
( select ) => {
const {
getBlockOrder,
getSelectedBlockClientIds,
__unstableGetVisibleBlocks,
} = select( blockEditorStore );
return {
order: getBlockOrder( rootClientId ),
selectedBlocks: getSelectedBlockClientIds(),
visibleBlocks: __unstableGetVisibleBlocks(),
};
},
[ rootClientId ]
);
return (
<LayoutProvider value={ layout }>
{ order.map( ( clientId ) => (
<AsyncModeProvider
key={ clientId }
value={
// Only provide data asynchronously if the block is
// not visible and not selected.
! visibleBlocks.has( clientId ) &&
! selectedBlocks.includes( clientId )
}
>
<BlockListBlock
rootClientId={ rootClientId }
clientId={ clientId }
/>
</AsyncModeProvider>
) ) }
{ order.length < 1 && placeholder }
<BlockListAppender
tagName={ __experimentalAppenderTagName }
rootClientId={ rootClientId }
renderAppender={ renderAppender }
/>
</LayoutProvider>
);
}
export function BlockListItems( props ) {
// This component needs to always be synchronous as it's the one changing
// the async mode depending on the block selection.
return (
<AsyncModeProvider value={ false }>
<Items { ...props } />
</AsyncModeProvider>
);
}