PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.0
GenerateBlocks v2.0.0
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / components / url-controls / URLControls.jsx
generateblocks / src / components / url-controls Last commit date
URLControls.jsx 1 year ago editor.scss 1 year ago index.js 1 year ago
URLControls.jsx
166 lines
1 import { __ } from '@wordpress/i18n';
2 import { BaseControl, ToggleControl, useBaseControlProps } from '@wordpress/components';
3 import { URLInput } from '@wordpress/block-editor';
4 import { useEffect } from '@wordpress/element';
5
6 import { Stack } from '@edge22/components';
7
8 import { DynamicTagModal } from '../../dynamic-tags';
9 import './editor.scss';
10
11 export function URLControls( {
12 htmlAttributes,
13 setAttributes,
14 attributesName = 'htmlAttributes',
15 label = __( 'Link', 'generateblocks' ),
16 context,
17 tagName,
18 } ) {
19 const url = htmlAttributes?.href ?? '';
20 const target = htmlAttributes?.target ?? '';
21 const rel = htmlAttributes?.rel ?? '';
22
23 useEffect( () => {
24 if ( '_blank' === target ) {
25 if ( ! rel.includes( 'noopener' ) ) {
26 const relItems = rel ? rel.split( ' ' ) : [];
27 relItems.push( 'noopener' );
28
29 setAttributes( {
30 [ attributesName ]: {
31 ...htmlAttributes,
32 rel: relItems.join( ' ' ),
33 },
34 } );
35 }
36 } else if ( rel.includes( 'noopener' ) ) {
37 const relItems = rel ? rel.split( ' ' ) : [];
38 relItems.splice( relItems.indexOf( 'noopener' ), 1 );
39
40 setAttributes( {
41 [ attributesName ]: {
42 ...htmlAttributes,
43 rel: relItems.join( ' ' ),
44 },
45 } );
46 }
47 }, [ target, rel ] );
48
49 const { baseControlProps, controlProps } = useBaseControlProps( {
50 label,
51 } );
52
53 return (
54 <div className="gb-url-controls components-base-control">
55 <BaseControl { ...baseControlProps }>
56 <Stack layout="flex" direction="horizontal" wrap={ false } gap="5px">
57 <URLInput
58 { ...controlProps }
59 className={ 'gb-url-controls__link-input' }
60 value={ url }
61 onChange={ ( value ) => {
62 setAttributes( {
63 [ attributesName ]: {
64 ...htmlAttributes,
65 href: value,
66 },
67 } );
68 } }
69 disableSuggestions={ url.includes( '{{' ) }
70 />
71
72 <DynamicTagModal
73 onInsert={ ( value ) => {
74 setAttributes( {
75 [ attributesName ]: {
76 ...htmlAttributes,
77 href: value,
78 },
79 } );
80 } }
81 selectedText={ url.startsWith( '{{' ) ? url : '' }
82 context={ context }
83 tagName={ tagName }
84 />
85 </Stack>
86 </BaseControl>
87
88 { ( !! url || !! target || !! rel ) && (
89 <>
90 <ToggleControl
91 label={ __( 'Open link in a new tab', 'generateblocks' ) }
92 checked={ target || '' }
93 onChange={ ( value ) => {
94 const newHtmlAttributes = { ...htmlAttributes };
95
96 if ( value ) {
97 newHtmlAttributes.target = '_blank';
98 } else {
99 delete newHtmlAttributes.target;
100 }
101
102 setAttributes( {
103 [ attributesName ]: newHtmlAttributes,
104 } );
105 } }
106 />
107
108 <ToggleControl
109 label={ __( 'Add rel="nofollow"', 'generateblocks' ) }
110 checked={ rel?.includes( 'nofollow' ) || '' }
111 onChange={ ( value ) => {
112 const newHtmlAttributes = { ...htmlAttributes };
113 const relItems = rel ? rel.split( ' ' ) : [];
114
115 if ( value && ! relItems.includes( 'nofollow' ) ) {
116 relItems.push( 'nofollow' );
117 }
118
119 if ( ! value && relItems.includes( 'nofollow' ) ) {
120 relItems.splice( relItems.indexOf( 'nofollow' ), 1 );
121 }
122
123 if ( relItems.length > 0 ) {
124 newHtmlAttributes.rel = relItems.join( ' ' );
125 } else {
126 delete newHtmlAttributes.rel;
127 }
128
129 setAttributes( {
130 [ attributesName ]: newHtmlAttributes,
131 } );
132 } }
133 />
134
135 <ToggleControl
136 label={ __( 'Add rel="sponsored"', 'generateblocks' ) }
137 checked={ rel?.includes( 'sponsored' ) || '' }
138 onChange={ ( value ) => {
139 const newHtmlAttributes = { ...htmlAttributes };
140 const relItems = rel ? rel.split( ' ' ) : [];
141
142 if ( value && ! relItems.includes( 'sponsored' ) ) {
143 relItems.push( 'sponsored' );
144 }
145
146 if ( ! value && relItems.includes( 'sponsored' ) ) {
147 relItems.splice( relItems.indexOf( 'sponsored' ), 1 );
148 }
149
150 if ( relItems.length > 0 ) {
151 newHtmlAttributes.rel = relItems.join( ' ' );
152 } else {
153 delete newHtmlAttributes.rel;
154 }
155
156 setAttributes( {
157 [ attributesName ]: newHtmlAttributes,
158 } );
159 } }
160 />
161 </>
162 ) }
163 </div>
164 );
165 }
166