| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __ } from "@wordpress/i18n"; |
| 5 |
|
| 6 |
/** |
| 7 |
* Internal dependencies |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Define constants |
| 12 |
*/ |
| 13 |
export const MAX_GRID_ITEMS = 12; |
| 14 |
|
| 15 |
export const SORTING_SUGGESTIONS = [ |
| 16 |
{ |
| 17 |
label: __("Random order", "content-blocks-builder"), |
| 18 |
value: "rand", |
| 19 |
}, |
| 20 |
{ |
| 21 |
label: __("Page order in decending order", "content-blocks-builder"), |
| 22 |
value: "menu_order/desc", |
| 23 |
}, |
| 24 |
{ |
| 25 |
label: __("Page order in ascending order", "content-blocks-builder"), |
| 26 |
value: "menu_order/asc", |
| 27 |
}, |
| 28 |
{ |
| 29 |
label: __("Recently modified first", "content-blocks-builder"), |
| 30 |
value: "modified/desc", |
| 31 |
}, |
| 32 |
{ |
| 33 |
label: __("Last modified first", "content-blocks-builder"), |
| 34 |
value: "modified/asc", |
| 35 |
}, |
| 36 |
{ |
| 37 |
label: __("Number of comments", "content-blocks-builder"), |
| 38 |
value: "comment_count/desc", |
| 39 |
}, |
| 40 |
{ |
| 41 |
label: __("Newest to oldest"), |
| 42 |
value: "date/desc", |
| 43 |
}, |
| 44 |
{ |
| 45 |
label: __("Oldest to newest"), |
| 46 |
value: "date/asc", |
| 47 |
}, |
| 48 |
{ |
| 49 |
label: __("A → Z"), |
| 50 |
value: "title/asc", |
| 51 |
}, |
| 52 |
{ |
| 53 |
label: __("Z → A"), |
| 54 |
value: "title/desc", |
| 55 |
}, |
| 56 |
]; |
| 57 |
|
| 58 |
export const findPostTemplateBlock = (innerBlocks = []) => { |
| 59 |
let foundBlock = null; |
| 60 |
for (const block of innerBlocks) { |
| 61 |
if (block.name === "core/post-template") { |
| 62 |
foundBlock = block; |
| 63 |
break; |
| 64 |
} else if (block.innerBlocks.length) { |
| 65 |
foundBlock = findPostTemplateBlock(block.innerBlocks); |
| 66 |
} |
| 67 |
} |
| 68 |
return foundBlock; |
| 69 |
}; |
| 70 |
|
| 71 |
export const buildVariables = (styleObject) => { |
| 72 |
return Object.keys(styleObject).reduce( |
| 73 |
(accumulator, current) => |
| 74 |
`${accumulator}${current}:${styleObject[current]};`, |
| 75 |
"", |
| 76 |
); |
| 77 |
}; |
| 78 |
|
| 79 |
/** |
| 80 |
* Build grid style in the editor |
| 81 |
* |
| 82 |
* @param {Object} |
| 83 |
* @returns {String} |
| 84 |
*/ |
| 85 |
export const buildGridEditorStyle = ({ |
| 86 |
gridStyleObject, |
| 87 |
itemsStyleObject, |
| 88 |
gridSelector, |
| 89 |
itemSelector, |
| 90 |
}) => { |
| 91 |
const devices = [ |
| 92 |
{ breakpoint: "sm", mediaQuery: "" }, |
| 93 |
{ breakpoint: "md", mediaQuery: "@media (min-width: 768px)" }, |
| 94 |
{ breakpoint: "lg", mediaQuery: "@media (min-width: 1024px)" }, |
| 95 |
]; |
| 96 |
const gridStyle = devices.reduce( |
| 97 |
(accumulator, { breakpoint, mediaQuery }) => { |
| 98 |
let breakpointStyle = ""; |
| 99 |
if (gridStyleObject[`--bb--grid--columns--${breakpoint}`]) { |
| 100 |
breakpointStyle = `${breakpointStyle}grid-template-columns: var(--bb--grid--columns--${breakpoint});`; |
| 101 |
} |
| 102 |
if (gridStyleObject[`--bb--grid--rows--${breakpoint}`]) { |
| 103 |
breakpointStyle = `${breakpointStyle}grid-template-rows: var(--bb--grid--rows--${breakpoint});`; |
| 104 |
} |
| 105 |
if (gridStyleObject[`--bb--grid--auto-rows--${breakpoint}`]) { |
| 106 |
breakpointStyle = `${breakpointStyle}grid-auto-rows: var(--bb--grid--auto-rows--${breakpoint});`; |
| 107 |
} |
| 108 |
if (gridStyleObject[`--bb--grid--gap--column--${breakpoint}`]) { |
| 109 |
breakpointStyle = `${breakpointStyle}column-gap: var(--bb--grid--gap--column--${breakpoint});`; |
| 110 |
} |
| 111 |
if (gridStyleObject[`--bb--grid--gap--row--${breakpoint}`]) { |
| 112 |
breakpointStyle = `${breakpointStyle}row-gap: var(--bb--grid--gap--row--${breakpoint});`; |
| 113 |
} |
| 114 |
|
| 115 |
if (breakpointStyle) { |
| 116 |
breakpointStyle = `${gridSelector}{${breakpointStyle}}`; |
| 117 |
|
| 118 |
if (mediaQuery) { |
| 119 |
breakpointStyle = `${mediaQuery}{${breakpointStyle}}`; |
| 120 |
} |
| 121 |
|
| 122 |
accumulator = `${accumulator}${breakpointStyle}`; |
| 123 |
} |
| 124 |
|
| 125 |
return accumulator; |
| 126 |
}, |
| 127 |
"", |
| 128 |
); |
| 129 |
const itemsStyle = devices.reduce( |
| 130 |
(accumulator, { breakpoint, mediaQuery }) => { |
| 131 |
let breakpointStyle = ""; |
| 132 |
Object.keys(itemsStyleObject).forEach((index) => { |
| 133 |
const itemStyleObject = itemsStyleObject[index] ?? {}; |
| 134 |
let itemStyle = ""; |
| 135 |
|
| 136 |
if (itemStyleObject[`--bb--grid-item--column--${breakpoint}`]) { |
| 137 |
itemStyle = `${itemStyle}grid-column: var(--bb--grid-item--column--${breakpoint});`; |
| 138 |
} |
| 139 |
if (itemStyleObject[`--bb--grid-item--row--${breakpoint}`]) { |
| 140 |
itemStyle = `${itemStyle}grid-row: var(--bb--grid-item--row--${breakpoint});`; |
| 141 |
} |
| 142 |
if (itemStyleObject[`--bb--grid-item--order--${breakpoint}`]) { |
| 143 |
itemStyle = `${itemStyle}order: var(--bb--grid-item--order--${breakpoint});`; |
| 144 |
} |
| 145 |
|
| 146 |
if (itemStyle) { |
| 147 |
itemStyle = `${gridSelector} > [data-order="${index}"]{${buildVariables( |
| 148 |
itemStyleObject, |
| 149 |
)}${itemStyle}}`; |
| 150 |
|
| 151 |
breakpointStyle = `${breakpointStyle}${itemStyle}`; |
| 152 |
} |
| 153 |
}); |
| 154 |
|
| 155 |
if (breakpointStyle) { |
| 156 |
if (mediaQuery) { |
| 157 |
breakpointStyle = `${mediaQuery}{${breakpointStyle}}`; |
| 158 |
} |
| 159 |
|
| 160 |
accumulator = `${accumulator}${breakpointStyle}`; |
| 161 |
} |
| 162 |
|
| 163 |
return accumulator; |
| 164 |
}, |
| 165 |
"", |
| 166 |
); |
| 167 |
|
| 168 |
let styles = ` |
| 169 |
${gridSelector} {${buildVariables(gridStyleObject)}display:grid;} |
| 170 |
${gridStyle} |
| 171 |
${itemsStyle} |
| 172 |
.editor-styles-wrapper ${gridSelector} > ${itemSelector} + ${itemSelector}{margin-block-start:0;margin-block-end:0;} |
| 173 |
`; |
| 174 |
|
| 175 |
return styles; |
| 176 |
}; |
| 177 |
|