-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathmodal.js
More file actions
230 lines (221 loc) · 6.14 KB
/
Copy pathmodal.js
File metadata and controls
230 lines (221 loc) · 6.14 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import {
Modal,
Button,
Flex,
Notice,
privateApis as componentsPrivateApis,
__experimentalVStack as VStack,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { PlainText, store as blockEditorStore } from '@wordpress/block-editor';
import { fullscreen, square } from '@wordpress/icons';
import { useViewportMatch } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { unlock } from '../lock-unlock';
import Preview from './preview';
import { parseContent, serializeContent } from './utils';
const { Tabs } = unlock( componentsPrivateApis );
export default function HTMLEditModal( {
isOpen,
onRequestClose,
content,
setAttributes,
} ) {
// Parse content into separate sections and use as initial state
const { html, css, js } = parseContent( content );
const [ editedHtml, setEditedHtml ] = useState( html );
const [ editedCss, setEditedCss ] = useState( css );
const [ editedJs, setEditedJs ] = useState( js );
const [ isFullscreen, setIsFullscreen ] = useState( false );
const isMobileViewport = useViewportMatch( 'small', '<' );
// Check if user has permission to save scripts and get editor styles
const { canUserUseUnfilteredHTML } = useSelect( ( select ) => {
const settings = select( blockEditorStore ).getSettings();
return {
canUserUseUnfilteredHTML:
settings.__experimentalCanUserUseUnfilteredHTML,
};
}, [] );
// Determine if we should show a warning about CSS/JS content being stripped
const hasRestrictedContent =
! canUserUseUnfilteredHTML && ( css.trim() || js.trim() );
if ( ! isOpen ) {
return null;
}
const handleUpdate = () => {
// For users without unfiltered_html capability, strip CSS and JS content
// to prevent kses from leaving broken content
setAttributes( {
content: serializeContent( {
html: editedHtml,
css: canUserUseUnfilteredHTML ? editedCss : '',
js: canUserUseUnfilteredHTML ? editedJs : '',
} ),
} );
};
const handleUpdateAndClose = () => {
handleUpdate();
onRequestClose();
};
const toggleFullscreen = () => {
setIsFullscreen( ( prevState ) => ! prevState );
};
return (
<>
<Modal
title={ __( 'Edit HTML' ) }
onRequestClose={ onRequestClose }
className="block-library-html__modal"
size="large"
isDismissible={ false }
shouldCloseOnClickOutside={ false }
isFullScreen={ isFullscreen }
__experimentalHideHeader
>
<Tabs orientation="horizontal" defaultTabId="html">
<VStack expanded>
<HStack
justify="space-between"
className="block-library-html__modal-header"
>
<div>
<Tabs.TabList>
<Tabs.Tab tabId="html">HTML</Tabs.Tab>
{ canUserUseUnfilteredHTML && (
<Tabs.Tab tabId="css">CSS</Tabs.Tab>
) }
{ canUserUseUnfilteredHTML && (
<Tabs.Tab tabId="js">
{ __( 'JavaScript' ) }
</Tabs.Tab>
) }
</Tabs.TabList>
</div>
{ ! isMobileViewport && (
<div>
<Button
__next40pxDefaultSize
icon={
isFullscreen ? square : fullscreen
}
label={ __(
'Enable/disable fullscreen'
) }
onClick={ toggleFullscreen }
variant="tertiary"
/>
</div>
) }
</HStack>
{ hasRestrictedContent && (
<Notice
status="warning"
isDismissible={ false }
className="block-library-html__modal-notice"
>
{ __(
'This block contains CSS or JavaScript that will be removed when you save because you do not have permission to use unfiltered HTML.'
) }
</Notice>
) }
<Flex
direction={ isMobileViewport ? 'column' : 'row' }
className="block-library-html__modal-tabs"
align="stretch"
gap={ 8 }
>
<div className="block-library-html__modal-content">
<Tabs.TabPanel
tabId="html"
focusable={ false }
className="block-library-html__modal-tab"
>
<PlainText
value={ editedHtml }
onChange={ setEditedHtml }
placeholder={ __( 'Write HTML…' ) }
aria-label={ __( 'HTML' ) }
className="block-library-html__modal-editor"
async
/>
</Tabs.TabPanel>
{ canUserUseUnfilteredHTML && (
<Tabs.TabPanel
tabId="css"
focusable={ false }
className="block-library-html__modal-tab"
>
<PlainText
value={ editedCss }
onChange={ setEditedCss }
placeholder={ __( 'Write CSS…' ) }
aria-label={ __( 'CSS' ) }
className="block-library-html__modal-editor"
async
/>
</Tabs.TabPanel>
) }
{ canUserUseUnfilteredHTML && (
<Tabs.TabPanel
tabId="js"
focusable={ false }
className="block-library-html__modal-tab"
>
<PlainText
value={ editedJs }
onChange={ setEditedJs }
placeholder={ __(
'Write JavaScript…'
) }
aria-label={ __( 'JavaScript' ) }
className="block-library-html__modal-editor"
async
/>
</Tabs.TabPanel>
) }
</div>
<div className="block-library-html__preview">
<Preview
content={ serializeContent( {
html: editedHtml,
css: editedCss,
js: editedJs,
} ) }
/>
</div>
</Flex>
<HStack
alignment="center"
justify="flex-end"
spacing={ 4 }
className="block-library-html__modal-footer"
>
<Button
__next40pxDefaultSize
variant="tertiary"
onClick={ onRequestClose }
>
{ __( 'Cancel' ) }
</Button>
<Button
__next40pxDefaultSize
variant="primary"
onClick={ handleUpdateAndClose }
>
{ __( 'Update' ) }
</Button>
</HStack>
</VStack>
</Tabs>
</Modal>
</>
);
}