components
1 year ago
data
1 year ago
editor.scss
3 years ago
export.js
1 year ago
import.js
1 year ago
index.js
5 years ago
template-library.js
2 years ago
template-library.js
224 lines
| 1 | /* eslint-disable no-undef */ |
| 2 | /* global elementor, $e, elementorCommon */ |
| 3 | import { withDispatch, withSelect } from '@wordpress/data'; |
| 4 | import { compose } from '@wordpress/compose'; |
| 5 | import { Fragment, useState } from '@wordpress/element'; |
| 6 | |
| 7 | import Header from './components/header.js'; |
| 8 | import Content from './components/content.js'; |
| 9 | import { importTemplate } from './data/templates-cloud/index.js'; |
| 10 | import { setPostModel } from './data/utils'; |
| 11 | |
| 12 | const TemplateLibrary = ( { currentTab, setFetching } ) => { |
| 13 | const [ searchQuery, setSearchQuery ] = useState( { |
| 14 | templates: '', |
| 15 | library: '', |
| 16 | } ); |
| 17 | |
| 18 | const [ sortingOrder, setSortingOrder ] = useState( { |
| 19 | templates: { |
| 20 | order: 'DESC', |
| 21 | orderby: 'date', |
| 22 | }, |
| 23 | library: { |
| 24 | order: 'DESC', |
| 25 | orderby: 'date', |
| 26 | }, |
| 27 | } ); |
| 28 | |
| 29 | const [ isSearch, setSearch ] = useState( { |
| 30 | templates: false, |
| 31 | library: false, |
| 32 | } ); |
| 33 | |
| 34 | const isGeneral = currentTab === 'templates'; |
| 35 | |
| 36 | const setQuery = ( query ) => { |
| 37 | if ( isGeneral ) { |
| 38 | return setSearchQuery( { |
| 39 | ...searchQuery, |
| 40 | templates: query, |
| 41 | } ); |
| 42 | } |
| 43 | |
| 44 | return setSearchQuery( { |
| 45 | ...searchQuery, |
| 46 | library: query, |
| 47 | } ); |
| 48 | }; |
| 49 | |
| 50 | const getSearchQuery = () => { |
| 51 | if ( isGeneral ) { |
| 52 | return searchQuery.templates; |
| 53 | } |
| 54 | |
| 55 | return searchQuery.library; |
| 56 | }; |
| 57 | |
| 58 | const setSorting = ( order ) => { |
| 59 | if ( isGeneral ) { |
| 60 | return setSortingOrder( { |
| 61 | ...sortingOrder, |
| 62 | templates: order, |
| 63 | } ); |
| 64 | } |
| 65 | |
| 66 | return setSortingOrder( { |
| 67 | ...sortingOrder, |
| 68 | library: order, |
| 69 | } ); |
| 70 | }; |
| 71 | |
| 72 | const getOrder = () => { |
| 73 | if ( isGeneral ) { |
| 74 | return sortingOrder.templates; |
| 75 | } |
| 76 | |
| 77 | return sortingOrder.library; |
| 78 | }; |
| 79 | |
| 80 | const setSearchStatus = ( status ) => { |
| 81 | if ( isGeneral ) { |
| 82 | return setSearch( { |
| 83 | ...isSearch, |
| 84 | templates: status, |
| 85 | } ); |
| 86 | } |
| 87 | |
| 88 | return setSearch( { |
| 89 | ...isSearch, |
| 90 | library: status, |
| 91 | } ); |
| 92 | }; |
| 93 | |
| 94 | const getSearchStatus = () => { |
| 95 | if ( isGeneral ) { |
| 96 | return isSearch.templates; |
| 97 | } |
| 98 | |
| 99 | return isSearch.library; |
| 100 | }; |
| 101 | |
| 102 | const changeID = ( element ) => { |
| 103 | element.id = elementorCommon.helpers.getUniqueId(); |
| 104 | |
| 105 | if ( 0 < element.elements.length ) { |
| 106 | for ( let i = 0; i < element.elements.length; i++ ) { |
| 107 | element.elements[ i ] = changeID( element.elements[ i ] ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return element; |
| 112 | }; |
| 113 | |
| 114 | const tryParseJSON = ( jsonString ) => { |
| 115 | try { |
| 116 | const o = JSON.parse( jsonString ); |
| 117 | |
| 118 | // Handle non-exception-throwing cases: |
| 119 | // Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking, |
| 120 | // but... JSON.parse(null) returns null, and typeof null === "object", |
| 121 | // so we must check for that, too. Thankfully, null is falsey, so this suffices: |
| 122 | // Source: https://stackoverflow.com/a/20392392 |
| 123 | if ( o && typeof o === 'object' ) { |
| 124 | return o; |
| 125 | } |
| 126 | } catch ( e ) {} |
| 127 | |
| 128 | return false; |
| 129 | }; |
| 130 | |
| 131 | const onImport = async ( { id, title, meta = undefined } ) => { |
| 132 | setFetching( true ); |
| 133 | const data = await importTemplate( id ); |
| 134 | |
| 135 | if ( ! data ) { |
| 136 | return setFetching( false ); |
| 137 | } |
| 138 | |
| 139 | const history = $e.internal( 'document/history/start-log', { |
| 140 | type: 'add', |
| 141 | title: `${ window.tiTpc.library.historyMessage } ${ title }`, |
| 142 | } ); |
| 143 | |
| 144 | let index = Number( window.tiTpc.placeholder ); |
| 145 | |
| 146 | const content = data.content; |
| 147 | |
| 148 | for ( let i = 0; i < content.length; i++ ) { |
| 149 | content[ i ] = changeID( content[ i ] ); |
| 150 | $e.run( 'document/elements/create', { |
| 151 | container: elementor.getPreviewContainer(), |
| 152 | model: content[ i ], |
| 153 | options: index >= 0 ? { at: index++ } : {}, |
| 154 | } ); |
| 155 | } |
| 156 | |
| 157 | const elementorId = elementor.config.document.id; |
| 158 | await setPostModel( elementorId ); |
| 159 | |
| 160 | if ( |
| 161 | undefined !== meta && |
| 162 | 0 < Object.keys( tryParseJSON( meta ) || {} ).length |
| 163 | ) { |
| 164 | meta = { ...JSON.parse( meta ) }; |
| 165 | const { _wp_page_template, ...filteredFields } = meta; |
| 166 | |
| 167 | window.tiTpc.postModel.set( 'meta', { |
| 168 | ...filteredFields, |
| 169 | } ); |
| 170 | window.tiTpc.postModel.save(); |
| 171 | |
| 172 | if ( meta._wp_page_template ) { |
| 173 | elementor.documents.getCurrent().container.settings.set( { |
| 174 | template: meta._wp_page_template, |
| 175 | } ); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | $e.internal( 'document/history/end-log', { |
| 180 | id: history, |
| 181 | } ); |
| 182 | |
| 183 | window.tiTpcModal.hide(); |
| 184 | |
| 185 | setFetching( false ); |
| 186 | }; |
| 187 | |
| 188 | return ( |
| 189 | <Fragment> |
| 190 | <Header |
| 191 | getSearchQuery={ getSearchQuery } |
| 192 | getOrder={ getOrder } |
| 193 | onImport={ onImport } |
| 194 | /> |
| 195 | <Content |
| 196 | setQuery={ setQuery } |
| 197 | getSearchQuery={ getSearchQuery } |
| 198 | setSorting={ setSorting } |
| 199 | getOrder={ getOrder } |
| 200 | isSearch={ getSearchStatus() } |
| 201 | setSearch={ setSearchStatus } |
| 202 | onImport={ onImport } |
| 203 | /> |
| 204 | </Fragment> |
| 205 | ); |
| 206 | }; |
| 207 | |
| 208 | export default compose( |
| 209 | withSelect( ( select ) => { |
| 210 | const { getCurrentTab } = select( 'tpc/elementor' ); |
| 211 | |
| 212 | return { |
| 213 | currentTab: getCurrentTab(), |
| 214 | }; |
| 215 | } ), |
| 216 | withDispatch( ( dispatch ) => { |
| 217 | const { setFetching } = dispatch( 'tpc/elementor' ); |
| 218 | |
| 219 | return { |
| 220 | setFetching, |
| 221 | }; |
| 222 | } ) |
| 223 | )( TemplateLibrary ); |
| 224 |