block-template-panel.js
5 years ago
block.js
5 years ago
default-templates.js
6 years ago
helper.js
6 years ago
index.js
5 years ago
sidebar.js
5 years ago
block-template-panel.js
129 lines
| 1 | import {vkbBlockEditor} from "./../../blocks/_helper/depModules" |
| 2 | |
| 3 | const { first, last } = window.lodash; |
| 4 | |
| 5 | const { Spinner } = wp.components; |
| 6 | |
| 7 | const { BlockPreview } = vkbBlockEditor; |
| 8 | |
| 9 | const { useState } = wp.element; |
| 10 | |
| 11 | const { dispatch, select } = wp.data; |
| 12 | |
| 13 | const { insertBlocks, replaceBlocks, multiSelect } = dispatch("core/editor"); |
| 14 | |
| 15 | const { __ } = wp.i18n; |
| 16 | |
| 17 | const { |
| 18 | getBlocks, |
| 19 | getBlockCount, |
| 20 | getSelectedBlock, |
| 21 | getBlockInsertionPoint, |
| 22 | } = select("core/block-editor") ? select("core/block-editor") : select("core/editor"); |
| 23 | |
| 24 | import parsedTemplates from "./default-templates"; |
| 25 | |
| 26 | export default ({ slug }) => { |
| 27 | |
| 28 | if (5.3 > parseFloat(wpVersion)) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | const [parts, setParts] = useState(null); |
| 33 | const [resultParts, setResultParts] = useState(null); |
| 34 | |
| 35 | const setupParts = () => { |
| 36 | if (parts) { |
| 37 | return; |
| 38 | } |
| 39 | setParts(parsedTemplates); |
| 40 | }; |
| 41 | |
| 42 | const setupResultParts = () => { |
| 43 | if (resultParts) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | setupParts(); |
| 48 | if (!parts) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | const newResultParts = parts.map((part, index) => { |
| 53 | return ( |
| 54 | <li key={ index }> |
| 55 | <div |
| 56 | className="vkb-menu__template-part__button" |
| 57 | onClick={ () => { |
| 58 | if (part.blocks.length) { |
| 59 | const selectedBlock = getSelectedBlock(); |
| 60 | if (null === selectedBlock) { |
| 61 | const lastRootBlock = last(getBlocks()); |
| 62 | const isEmpty = |
| 63 | undefined !== lastRootBlock && |
| 64 | null === lastRootBlock.rootClientId && |
| 65 | (!getBlockCount(lastRootBlock.clientId) || |
| 66 | ("core/paragraph" === lastRootBlock.name && |
| 67 | "" === lastRootBlock.attributes.content)); |
| 68 | if (isEmpty) { |
| 69 | replaceBlocks(lastRootBlock.clientId, part.blocks); |
| 70 | } else { |
| 71 | insertBlocks(part.blocks); |
| 72 | } |
| 73 | } else { |
| 74 | const isEmpty = |
| 75 | "core/paragraph" === selectedBlock.name && |
| 76 | "" === selectedBlock.attributes.content; |
| 77 | if (!isEmpty) { |
| 78 | const insertionPoint = getBlockInsertionPoint(); |
| 79 | insertBlocks(part.blocks, insertionPoint.index); |
| 80 | } else { |
| 81 | replaceBlocks(selectedBlock.clientId, part.blocks); |
| 82 | } |
| 83 | } |
| 84 | multiSelect( |
| 85 | first(part.blocks).clientId, |
| 86 | last(part.blocks).clientId |
| 87 | ); |
| 88 | } |
| 89 | } } |
| 90 | > |
| 91 | <section className="vkb-menu__template-part__card__container"> |
| 92 | <div |
| 93 | id={ `vkb-menu__template-part__card${index}` } |
| 94 | className="card vkb-menu__template-part__card" |
| 95 | > |
| 96 | <div className="content"> |
| 97 | <h6> |
| 98 | <span className={ "vkb-menu__template-part__header__icon" }> |
| 99 | { part.icon } |
| 100 | </span> |
| 101 | { part.name } |
| 102 | </h6> |
| 103 | <div className="hover_content"> |
| 104 | <div className="inner edit-post-visual-editor editor-styles-wrapper"> |
| 105 | <BlockPreview viewportWidth={ 601 } blocks={ part.blocks } /> |
| 106 | </div> |
| 107 | </div> |
| 108 | </div> |
| 109 | </div> |
| 110 | </section> |
| 111 | </div> |
| 112 | </li> |
| 113 | ); |
| 114 | }); |
| 115 | setResultParts(newResultParts.filter((resultPart) => resultPart)); |
| 116 | }; |
| 117 | |
| 118 | setupResultParts(); |
| 119 | |
| 120 | if (resultParts) { |
| 121 | return <ul className="vkb-menu__template-part">{ resultParts }</ul>; |
| 122 | } |
| 123 | return ( |
| 124 | <div className="vkb-menu__template-part__loading"> |
| 125 | <Spinner /> |
| 126 | </div> |
| 127 | ); |
| 128 | }; |
| 129 |