-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathselectors.js
189 lines (150 loc) · 4.42 KB
/
selectors.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
/**
* External dependencies
*/
import { first, last, get } from 'lodash';
/**
* Internal dependencies
*/
import { addQueryArgs } from './utils/url';
export function getEditorMode( state ) {
return state.mode;
}
export function isEditorSidebarOpened( state ) {
return state.isSidebarOpened;
}
export function hasEditorUndo( state ) {
return state.editor.history.past.length > 0;
}
export function hasEditorRedo( state ) {
return state.editor.history.future.length > 0;
}
export function isEditedPostNew( state ) {
return ! state.currentPost.id;
}
export function isEditedPostDirty( state ) {
return state.editor.dirty;
}
export function getCurrentPost( state ) {
return state.currentPost;
}
export function getPostEdits( state ) {
return state.editor.edits;
}
export function getEditedPostAttribute( state, attributeName ) {
return state.editor.edits[ attributeName ] === undefined
? state.currentPost[ attributeName ]
: state.editor.edits[ attributeName ];
}
export function getEditedPostStatus( state ) {
return getEditedPostAttribute( state, 'status' );
}
export function getEditedPostVisibility( state ) {
const status = getEditedPostStatus( state );
const password = getEditedPostAttribute( state, 'password' );
if ( status === 'private' ) {
return 'private';
} else if ( password !== undefined && password !== null ) {
return 'password';
}
return 'public';
}
export function getEditedPostTitle( state ) {
return state.editor.edits.title === undefined
? get( state.currentPost, 'title.raw' )
: state.editor.edits.title;
}
export function getEditedPostExcerpt( state ) {
return state.editor.edits.excerpt === undefined
? get( state.currentPost, 'excerpt.raw' )
: state.editor.edits.excerpt;
}
export function getEditedPostPreviewLink( state ) {
const link = state.currentPost.link;
if ( ! link ) {
return null;
}
return addQueryArgs( link, { preview: 'true' } );
}
export function getBlock( state, uid ) {
return state.editor.blocksByUid[ uid ];
}
export function getBlocks( state ) {
return state.editor.blockOrder.map( ( uid ) => (
state.editor.blocksByUid[ uid ]
) );
}
export function getSelectedBlock( state ) {
if ( ! state.selectedBlock.uid ) {
return null;
}
return state.editor.blocksByUid[ state.selectedBlock.uid ];
}
export function getBlockUids( state ) {
return state.editor.blockOrder;
}
export function getBlockOrder( state, uid ) {
return state.editor.blockOrder.indexOf( uid );
}
export function isFirstBlock( state, uid ) {
return first( state.editor.blockOrder ) === uid;
}
export function isLastBlock( state, uid ) {
return last( state.editor.blockOrder ) === uid;
}
export function getPreviousBlock( state, uid ) {
const order = getBlockOrder( state, uid );
return state.editor.blocksByUid[ state.editor.blockOrder[ order - 1 ] ] || null;
}
export function getNextBlock( state, uid ) {
const order = getBlockOrder( state, uid );
return state.editor.blocksByUid[ state.editor.blockOrder[ order + 1 ] ] || null;
}
export function isBlockSelected( state, uid ) {
return state.selectedBlock.uid === uid;
}
export function isBlockHovered( state, uid ) {
return state.hoveredBlock === uid;
}
export function getBlockFocus( state, uid ) {
return state.selectedBlock.uid === uid ? state.selectedBlock.focus : null;
}
export function isTypingInBlock( state, uid ) {
return state.selectedBlock.uid === uid ? state.selectedBlock.typing : false;
}
export function getBlockInsertionPoint( state ) {
if ( ! state.insertionPoint.show ) {
return null;
}
const blockToInsertAfter = state.insertionPoint.uid;
return blockToInsertAfter || last( state.editor.blockOrder );
}
export function isSavingPost( state ) {
return state.saving.requesting;
}
export function didPostSaveRequestSucceed( state ) {
return state.saving.successful;
}
export function didPostSaveRequestFail( state ) {
return !! state.saving.error;
}
export function isSavingNewPost( state ) {
return state.saving.isNew;
}
export function getSuggestedPostFormat( state ) {
const blocks = state.editor.blockOrder;
let type;
// If there is only one block in the content of the post grab its
// `blockType` name so we can derive a suitable post format from it.
if ( blocks.length === 1 ) {
type = state.editor.blocksByUid[ blocks[ 0 ] ].blockType;
}
// We only convert to default post formats in core.
switch ( type ) {
case 'core/image':
return 'Image';
case 'core/quote':
return 'Quote';
default:
return false;
}
}