PluginProbe
Extendify / 0.4.0
Extendify v0.4.0
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / utility-control / index.js

index.js in Extendify 0.4.0, at src/utility-control/index.js

124 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __ } from '@wordpress/i18n'
2 import { InspectorAdvancedControls } from '@wordpress/block-editor'
3 import { createHigherOrderComponent } from '@wordpress/compose'
4 import { addFilter } from '@wordpress/hooks'
5 import { FormTokenField } from '@wordpress/components'
6
7 import suggestions from '../../utility-framework/suggestions.json'
8
9 function addAttributes(settings) {
10 // Add new extUtilities attribute to block settings.
11 return {
12 ...settings,
13 attributes: {
14 ...settings.attributes,
15 extUtilities: {
16 type: 'array',
17 default: [],
18 },
19 },
20 }
21 }
22
23 function addEditProps(settings) {
24 const existingGetEditWrapperProps = settings.getEditWrapperProps
25 settings.getEditWrapperProps = (attributes) => {
26 let props = {}
27
28 if (existingGetEditWrapperProps) {
29 props = existingGetEditWrapperProps(attributes)
30 }
31
32 return addSaveProps(props, settings, attributes)
33 }
34
35 return settings
36 }
37
38 // Create HOC to add Extendify Utility to Advanced Panel of block.
39 const utilityClassEdit = createHigherOrderComponent((BlockEdit) => {
40 return function editPanel(props) {
41 const classes = props?.attributes?.extUtilities ?? []
42 const suggestionList = suggestions.suggestions.map((s) => {
43 // Remove all extra // and . from classnames
44 return s.replace('.', '').replace(new RegExp('\\\\', 'g'), '')
45 })
46
47 return (
48 <>
49 <BlockEdit {...props} />
50 {classes && (
51 <InspectorAdvancedControls>
52 <FormTokenField
53 label={__('Extendify Utilities', 'extendify')}
54 tokenizeOnSpace={true}
55 value={classes}
56 suggestions={suggestionList}
57 onChange={(value) => {
58 props.setAttributes({
59 extUtilities: value,
60 })
61 }}
62 />
63 </InspectorAdvancedControls>
64 )}
65 </>
66 )
67 }
68 }, 'utilityClassEdit')
69
70 function addSaveProps(saveElementProps, blockType, attributes) {
71 const generatedClasses = saveElementProps?.className ?? []
72 const classes = attributes?.extUtilities ?? []
73 const additionalClasses = attributes?.className ?? []
74
75 if (!classes || !Object.keys(classes).length) {
76 return saveElementProps
77 }
78
79 // EK seems to be converting string values to objects in some situations
80 const normalizeAsArray = (item) => {
81 switch (Object.prototype.toString.call(item)) {
82 case '[object String]':
83 return item.split(' ')
84 case '[object Array]':
85 return item
86 default:
87 return []
88 }
89 }
90 const classesCombined = new Set([
91 ...normalizeAsArray(additionalClasses),
92 ...normalizeAsArray(generatedClasses),
93 ...normalizeAsArray(classes),
94 ])
95
96 return Object.assign({}, saveElementProps, {
97 className: [...classesCombined].join(' '),
98 })
99 }
100
101 addFilter(
102 'blocks.registerBlockType',
103 'extendify/utilities/attributes',
104 addAttributes,
105 )
106
107 addFilter(
108 'blocks.registerBlockType',
109 'extendify/utilities/addEditProps',
110 addEditProps,
111 )
112
113 addFilter(
114 'editor.BlockEdit',
115 'extendify/utilities/advancedClassControls',
116 utilityClassEdit,
117 )
118
119 addFilter(
120 'blocks.getSaveContent.extraProps',
121 'extendify/utilities/extra-props',
122 addSaveProps,
123 )
124