-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.native.js
213 lines (192 loc) · 5.85 KB
/
index.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* External dependencies
*/
import classnames from 'classnames';
import { View } from 'react-native';
/**
* WordPress dependencies
*/
import { RawHTML } from '@wordpress/element';
import { withDispatch, withSelect } from '@wordpress/data';
import { pasteHandler, isUnmodifiedDefaultBlock } from '@wordpress/blocks';
import { withInstanceId, compose } from '@wordpress/compose';
import { RichText } from '@wordpress/rich-text';
/**
* Internal dependencies
*/
import Autocomplete from '../autocomplete';
import BlockFormatControls from '../block-format-controls';
import FormatToolbar from './format-toolbar';
import { withBlockEditContext } from '../block-edit/context';
const wrapperClasses = 'editor-rich-text block-editor-rich-text';
const classes = 'editor-rich-text__editable block-editor-rich-text__editable';
function RichTextWraper( {
children,
tagName,
value: originalValue,
onChange: originalOnChange,
selectionStart,
selectionEnd,
onSelectionChange,
multiline,
inlineToolbar,
wrapperClassName,
className,
autocompleters,
onReplace,
onRemove,
onMerge,
onSplit,
isCaretWithinFormattedText,
onEnterFormattedText,
onExitFormattedText,
canUserUseUnfilteredHTML,
isSelected: originalIsSelected,
onCreateUndoLevel,
placeholder,
// From experimental filter.
...experimentalProps
} ) {
const adjustedValue = originalValue;
const adjustedOnChange = originalOnChange;
return (
<RichText
{ ...experimentalProps }
value={ adjustedValue }
onChange={ adjustedOnChange }
selectionStart={ selectionStart }
selectionEnd={ selectionEnd }
onSelectionChange={ onSelectionChange }
tagName={ tagName }
wrapperClassName={ classnames( wrapperClasses, wrapperClassName ) }
className={ classnames( classes, className ) }
placeholder={ placeholder }
__unstableIsSelected={ originalIsSelected }
//__unstablePatterns={ getPatterns() }
//__unstableEnterPatterns={ getEnterPatterns() }
__unstablePasteHandler={ pasteHandler }
__unstableAutocomplete={ Autocomplete }
__unstableAutocompleters={ autocompleters }
__unstableOnReplace={ onReplace }
__unstableOnRemove={ onRemove }
__unstableOnMerge={ onMerge }
__unstableOnSplit={ onSplit }
__unstableMultiline={ multiline }
__unstableIsCaretWithinFormattedText={ isCaretWithinFormattedText }
__unstableOnEnterFormattedText={ onEnterFormattedText }
__unstableOnExitFormattedText={ onExitFormattedText }
__unstableCanUserUseUnfilteredHTML={ canUserUseUnfilteredHTML }
__unstableOnCreateUndoLevel={ onCreateUndoLevel }
>
{ ( { isSelected, value, onChange } ) =>
<View>
{ children && children( { value, onChange } ) }
{ isSelected && ! inlineToolbar && (
<BlockFormatControls>
<FormatToolbar />
</BlockFormatControls>
) }
</View>
}
</RichText>
);
}
const RichTextContainer = compose( [
withInstanceId,
withBlockEditContext( ( { clientId, onCaretVerticalPositionChange, isSelected }, ownProps ) => {
return {
clientId,
blockIsSelected: ownProps.isSelected !== undefined ? ownProps.isSelected : isSelected,
onCaretVerticalPositionChange,
};
} ),
withSelect( ( select, {
clientId,
instanceId,
identifier = instanceId,
isSelected,
blockIsSelected,
} ) => {
const { getFormatTypes } = select( 'core/rich-text' );
const {
getSelectionStart,
getSelectionEnd,
__unstableGetBlockWithoutInnerBlocks,
} = select( 'core/block-editor' );
const selectionStart = getSelectionStart();
const selectionEnd = getSelectionEnd();
if ( isSelected === undefined ) {
isSelected = (
selectionStart.clientId === clientId &&
selectionStart.attributeKey === identifier
);
}
// If the block of this RichText is unmodified then it's a candidate for replacing when adding a new block.
// In order to fix https://github.com/wordpress-mobile/gutenberg-mobile/issues/1126, let's blur on unmount in that case.
// This apparently assumes functionality the BlockHlder actually
const block = clientId && __unstableGetBlockWithoutInnerBlocks( clientId );
const shouldBlurOnUnmount = block && isSelected && isUnmodifiedDefaultBlock( block );
return {
formatTypes: getFormatTypes(),
selectionStart: isSelected ? selectionStart.offset : undefined,
selectionEnd: isSelected ? selectionEnd.offset : undefined,
isSelected,
blockIsSelected,
shouldBlurOnUnmount,
};
} ),
withDispatch( ( dispatch, {
clientId,
instanceId,
identifier = instanceId,
} ) => {
const {
__unstableMarkLastChangeAsPersistent,
selectionChange,
} = dispatch( 'core/block-editor' );
return {
onCreateUndoLevel: __unstableMarkLastChangeAsPersistent,
onSelectionChange( start, end ) {
selectionChange( clientId, identifier, start, end );
},
};
} ),
] )( RichTextWraper );
RichTextContainer.Content = ( { value, format, tagName: Tag, multiline, ...props } ) => {
let content;
let html = value;
let MultilineTag;
if ( multiline === true || multiline === 'p' || multiline === 'li' ) {
MultilineTag = multiline === true ? 'p' : multiline;
}
if ( ! html && MultilineTag ) {
html = `<${ MultilineTag }></${ MultilineTag }>`;
}
switch ( format ) {
case 'string':
content = <RawHTML>{ html }</RawHTML>;
break;
}
if ( Tag ) {
return <Tag { ...props }>{ content }</Tag>;
}
return content;
};
RichTextContainer.isEmpty = ( value = '' ) => {
// Handle deprecated `children` and `node` sources.
if ( Array.isArray( value ) ) {
return ! value || value.length === 0;
}
return value.length === 0;
};
RichTextContainer.Content.defaultProps = {
format: 'string',
value: '',
};
/**
* @see https://github.com/WordPress/gutenberg/blob/master/packages/block-editor/src/components/rich-text/README.md
*/
export default RichTextContainer;
export { RichTextShortcut } from './shortcut';
export { RichTextToolbarButton } from './toolbar-button';
export { __unstableRichTextInputEvent } from './input-event';