PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.7.0
Block Animations, Motion & Scroll Effects – Ghost Kit v3.7.0
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / settings / pages / css-js.js

css-js.js in Block Animations, Motion & Scroll Effects – Ghost Kit 3.7.0, at settings/pages/css-js.js

187 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import apiFetch from '@wordpress/api-fetch';
2 import { Spinner } from '@wordpress/components';
3 import { compose } from '@wordpress/compose';
4 import { withDispatch, withSelect } from '@wordpress/data';
5 import { Component } from '@wordpress/element';
6 import { __ } from '@wordpress/i18n';
7 import { debounce } from 'throttle-debounce';
8
9 import CodeEditor from '../../gutenberg/components/code-editor';
10
11 const { GHOSTKIT } = window;
12
13 class CssJs extends Component {
14 constructor(props) {
15 super(props);
16
17 this.state = {
18 settings: GHOSTKIT.settings || {},
19 customCSS: false,
20 customJSHead: false,
21 customJSFoot: false,
22 };
23
24 this.maybePrepareCode = this.maybePrepareCode.bind(this);
25 this.updateCustomCode = this.updateCustomCode.bind(this);
26 this.updateCustomCodeDebounce = debounce(
27 1000,
28 this.updateCustomCodeDebounce.bind(this)
29 );
30 }
31
32 componentDidMount() {
33 this.maybePrepareCode();
34 }
35
36 componentDidUpdate() {
37 this.maybePrepareCode();
38 }
39
40 maybePrepareCode() {
41 const { customCode = {} } = this.props;
42
43 if (
44 customCode &&
45 this.state.customCSS === false &&
46 this.state.customJSHead === false &&
47 this.state.customJSFoot === false
48 ) {
49 this.setState({
50 customCSS: customCode.ghostkit_custom_css || '',
51 customJSHead: customCode.ghostkit_custom_js_head || '',
52 customJSFoot: customCode.ghostkit_custom_js_foot || '',
53 });
54 }
55 }
56
57 updateCustomCode(name, val) {
58 if (this.state[name] === val) {
59 return;
60 }
61
62 this.setState(
63 {
64 [name]: val,
65 },
66 () => {
67 this.updateCustomCodeDebounce();
68 }
69 );
70 }
71
72 updateCustomCodeDebounce() {
73 this.props.updateCustomCode({
74 ghostkit_custom_css: this.state.customCSS,
75 ghostkit_custom_js_head: this.state.customJSHead,
76 ghostkit_custom_js_foot: this.state.customJSFoot,
77 });
78 }
79
80 render() {
81 const { icons } = GHOSTKIT;
82
83 return (
84 <div className="ghostkit-settings-content-wrapper ghostkit-settings-css-js">
85 {icons && Object.keys(icons).length ? (
86 <>
87 <h4 style={{ marginTop: 0 }}>
88 {__('CSS', 'ghostkit')}
89 </h4>
90 {this.state.customCSS !== false ? (
91 <CodeEditor
92 mode="css"
93 onChange={(value) => {
94 this.updateCustomCode('customCSS', value);
95 }}
96 value={this.state.customCSS || ''}
97 maxLines={20}
98 minLines={5}
99 height="300px"
100 />
101 ) : (
102 <Spinner />
103 )}
104 <h4>{__('JavaScript', 'ghostkit')}</h4>
105 {this.state.customJSHead !== false &&
106 this.state.customJSFoot !== false ? (
107 <>
108 <p className="ghostkit-help-text">
109 {__(
110 'Add custom JavaScript code in <head> section or in the end of <body> tag. Insert Google Analytics, Tag Manager or other JavaScript code snippets.',
111 'ghostkit'
112 )}
113 </p>
114 <p>
115 <code className="ghostkit-code">
116 {'<head>'}
117 </code>{' '}
118 :
119 </p>
120 <CodeEditor
121 mode="javascript"
122 onChange={(value) => {
123 this.updateCustomCode(
124 'customJSHead',
125 value
126 );
127 }}
128 value={this.state.customJSHead}
129 maxLines={20}
130 minLines={5}
131 height="300px"
132 />
133 <p>
134 <code className="ghostkit-code">
135 {'<foot>'}
136 </code>{' '}
137 :
138 </p>
139 <CodeEditor
140 mode="javascript"
141 onChange={(value) => {
142 this.updateCustomCode(
143 'customJSFoot',
144 value
145 );
146 }}
147 value={this.state.customJSFoot}
148 maxLines={20}
149 minLines={5}
150 height="300px"
151 />
152 </>
153 ) : (
154 <Spinner />
155 )}
156 </>
157 ) : null}
158 </div>
159 );
160 }
161 }
162
163 export default compose([
164 withSelect((select) => {
165 const customCode = select(
166 'ghostkit/plugins/custom-code'
167 ).getCustomCode();
168
169 return {
170 customCode,
171 };
172 }),
173 withDispatch((dispatch) => ({
174 updateCustomCode(value) {
175 dispatch('ghostkit/plugins/custom-code').setCustomCode(value);
176
177 apiFetch({
178 path: '/ghostkit/v1/update_custom_code',
179 method: 'POST',
180 data: {
181 data: value,
182 },
183 });
184 },
185 })),
186 ])(CssJs);
187