-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathedit.native.js
196 lines (181 loc) · 5.09 KB
/
edit.native.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
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* External dependencies
*/
import { View } from 'react-native';
/**
* WordPress dependencies
*/
import {
RichText,
useBlockProps,
useInnerBlocksProps,
BlockControls,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { usePreferredColorSchemeStyle } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { useState, useCallback, useRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import { useSplit, useMerge, useEnter } from './hooks';
import { convertToListItems } from './utils';
import { IndentUI } from './edit.js';
import styles from './style.scss';
import ListStyleType from './list-style-type';
const OPACITY = '9e';
export default function ListItemEdit( {
attributes,
setAttributes,
onReplace,
clientId,
style,
mergeBlocks,
} ) {
const [ contentWidth, setContentWidth ] = useState();
const { placeholder, content } = attributes;
const {
blockIndex,
hasInnerBlocks,
indentationLevel,
numberOfListItems,
ordered,
reversed,
start,
} = useSelect(
( select ) => {
const {
getBlockAttributes,
getBlockCount,
getBlockIndex,
getBlockParentsByBlockName,
getBlockRootClientId,
} = select( blockEditorStore );
const currentIdentationLevel = getBlockParentsByBlockName(
clientId,
'core/list-item',
true
).length;
const currentBlockIndex = getBlockIndex( clientId );
const blockWithInnerBlocks = getBlockCount( clientId ) > 0;
const rootClientId = getBlockRootClientId( clientId );
const blockAttributes = getBlockAttributes( rootClientId );
const totalListItems = getBlockCount( rootClientId );
const {
ordered: isOrdered,
reversed: isReversed,
start: startValue,
} = blockAttributes || {};
return {
blockIndex: currentBlockIndex,
hasInnerBlocks: blockWithInnerBlocks,
indentationLevel: currentIdentationLevel,
numberOfListItems: totalListItems,
ordered: isOrdered,
reversed: isReversed,
start: startValue,
};
},
[ clientId ]
);
const blockProps = useBlockProps( {
...( hasInnerBlocks && styles[ 'wp-block-list-item__nested-blocks' ] ),
} );
const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: [ 'core/list' ],
useCompactList: true,
} );
// Set default placeholder text color from light/dark scheme or base colors
const defaultPlaceholderFromScheme = usePreferredColorSchemeStyle(
styles[ 'wp-block-list-item__list-item-placeholder' ],
styles[ 'wp-block-list-item__list-item-placeholder--dark' ]
);
const currentTextColor = style?.color || style?.baseColors?.color?.text;
const defaultPlaceholderTextColor = currentTextColor
? currentTextColor
: defaultPlaceholderFromScheme?.color;
// Add hex opacity to default placeholder text color and style object
const defaultPlaceholderTextColorWithOpacity =
defaultPlaceholderTextColor + OPACITY;
const styleWithPlaceholderOpacity = {
...style,
...( style?.color && {
placeholderColor: style.color + OPACITY,
} ),
};
const preventDefault = useRef( false );
const { onEnter } = useEnter( { content, clientId }, preventDefault );
const onSplit = useSplit( clientId );
const onMerge = useMerge( clientId, mergeBlocks );
const onSplitList = useCallback(
( value ) => {
if ( ! preventDefault.current ) {
return onSplit( value );
}
},
[ clientId, onSplit ]
);
const onReplaceList = useCallback(
( blocks, ...args ) => {
if ( ! preventDefault.current ) {
onReplace( convertToListItems( blocks ), ...args );
}
},
[ clientId, onReplace, convertToListItems ]
);
const onLayout = useCallback( ( { nativeEvent } ) => {
setContentWidth( ( prevState ) => {
const { width } = nativeEvent.layout;
if ( ! prevState || prevState.width !== width ) {
return Math.floor( width );
}
return prevState;
} );
}, [] );
return (
<View style={ styles[ 'wp-block-list-item__list-item-parent' ] }>
<View style={ styles[ 'wp-block-list-item__list-item' ] }>
<View style={ styles[ 'wp-block-list-item__list-item-icon' ] }>
<ListStyleType
blockIndex={ blockIndex }
indentationLevel={ indentationLevel }
numberOfListItems={ numberOfListItems }
ordered={ ordered }
reversed={ reversed }
start={ start }
style={ style }
/>
</View>
<View
style={ styles[ 'wp-block-list-item__list-item-content' ] }
onLayout={ onLayout }
>
<RichText
identifier="content"
tagName="p"
onChange={ ( nextContent ) =>
setAttributes( { content: nextContent } )
}
value={ content }
placeholder={ placeholder || __( 'List' ) }
placeholderTextColor={
defaultPlaceholderTextColorWithOpacity
}
onSplit={ onSplitList }
onMerge={ onMerge }
onReplace={ onReplaceList }
onEnter={ onEnter }
style={ styleWithPlaceholderOpacity }
deleteEnter={ true }
containerWidth={ contentWidth }
/>
</View>
</View>
<View { ...innerBlocksProps }></View>
<BlockControls group="block">
<IndentUI clientId={ clientId } />
</BlockControls>
</View>
);
}