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