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