-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
530 lines (511 loc) · 18.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import axios from 'axios';
import * as cheerio from 'cheerio';
import convert from 'node-html-to-jsx';
import fs from 'fs';
import path from 'path';
import extractAssets from 'fetch-page-assets';
import icon from 'html-screenshots';
import imageToBase64 from 'image-to-base64';
import { imports, panels, images, characters, webpackConfig, packageJson, babelrc, editorStyles } from './globals.js';
;
const block = async (htmlContent, options = { name: 'My block', prefix: 'uai', category: 'common', basePath: process.cwd() }) => {
const attributes = {};
const convertName = (name) => {
return name.replace(new RegExp(/\W|_/, 'g'), '-').toLowerCase();
};
const saveFile = (fileName, contents, options) => {
try {
const filePath = path.join(options.basePath, fileName);
fs.writeFileSync(filePath, contents);
return contents;
}
catch (error) {
logError(error);
}
};
const parseRequirements = async (files) => {
let output = '';
for (const file of files) {
try {
const { data } = await axios.get(file, { responseType: 'text' });
output += data;
}
catch (error) {
logError(error);
}
}
return output;
};
const convertToUnderscores = (string) => {
return `${string.replaceAll('-', '_')}${generateRandomVariableName('func', 3)}`;
};
const getPhp = (options) => {
const { name, prefix } = options;
const newName = convertName(name);
const phpName = convertToUnderscores(name);
const phpPrefix = convertToUnderscores(prefix);
return `
<?php
/*
* Plugin Name: ${name}
* Version: 1.0
* Author: Diogo Angelim
* Author URI: https://github.com/DiogoAngelim/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'enqueue_block_editor_assets', '${phpPrefix}_${phpName}_editor_assets' );
function ${phpPrefix}_${phpName}_editor_assets() {
$filepath = plugin_dir_path(__FILE__) . 'block.build.js';
$version = file_exists($filepath) ? filemtime($filepath) : time();
wp_enqueue_script(
'${prefix}-${newName}',
plugins_url( 'block.build.js', __FILE__ ),
array( 'wp-blocks', 'wp-components', 'wp-element' ,'wp-editor'),
$version
);
wp_localize_script( '${prefix}-${newName}', 'vars', array( 'url' => plugin_dir_url( __FILE__ ) ) );
$filepath = plugin_dir_path(__FILE__) . 'editor.css';
$version = file_exists($filepath) ? filemtime($filepath) : time();
wp_enqueue_style(
'${prefix}-${newName}-editor',
plugins_url( 'editor.css', __FILE__ ),
array( 'wp-edit-blocks' ),
$version
);
}
add_action( 'enqueue_block_assets', '${phpPrefix}_${phpName}_block_assets' );
function ${phpPrefix}_${phpName}_block_assets() {
$args = array(
'handle' => '${prefix}-${newName}-frontend',
'src' => plugins_url( 'style.css', __FILE__ ),
);
wp_enqueue_block_style(
'${prefix}/${newName}',
$args
);
$filepath = plugin_dir_path(__FILE__) . 'script.js';
$version = file_exists($filepath) ? filemtime($filepath) : time();
wp_enqueue_script(
'${prefix}-${newName}-js',
plugins_url( 'scripts.js', __FILE__ ),
array(),
$version
);
wp_localize_script( '${prefix}-${newName}-js', 'vars', array( 'url' => plugin_dir_url( __FILE__ ) ) );
}
`;
};
const setEditor = (css) => {
return `
${editorStyles}
${css}
`;
};
const saveFiles = async (options) => {
const { cssFiles = [] } = options;
const css = await parseRequirements(cssFiles);
saveFile('style.css', css, options);
saveFile('editor.css', setEditor(css), options);
saveFile('scripts.js', '', options);
saveFile('package.json', packageJson, options);
saveFile('webpack.config.js', webpackConfig, options);
saveFile('.babelrc', babelrc, options);
saveFile('index.php', getPhp(options), options);
return saveFile('block.js', (await getBlock(htmlContent, options)), options);
};
const logError = (error) => {
console.error(`[Error] ${error.message}`);
};
const generateRandomVariableName = (prefix = 'content', length = 3) => {
let suffix = '';
for (let i = 0; i < length; i++) {
suffix += characters.charAt(Math.floor(Math.random() * characters.length));
}
return `${prefix}${suffix}`;
};
const setAttributeContent = (randomVariableName, content) => {
attributes[randomVariableName] = { type: 'string', default: content };
};
const hasAbsoluteKeyword = (str) => {
return !!(str && str.toLowerCase().includes('absolute'));
};
const getImageTemplate = (image) => {
const { randomUrlVariable, randomAltVariable, imgClass } = image;
return `
<MediaUpload
onSelect={ (media) => {
setAttributes({
${randomUrlVariable}: media.url,
${randomAltVariable}: media.alt
});
} }
type="image"
render={ ({ open }) => (
<img
src={ attributes.${randomUrlVariable} }
alt={ attributes.${randomAltVariable} }
onClick={ open }
className="${imgClass}"
/>
)}
/>`;
};
const replaceHtmlImage = (html, image) => {
const { randomUrlVariable } = image;
const regex = new RegExp(`(<img.*?${randomUrlVariable}.*?>)`, 'gi');
return html.replace(regex, getImageTemplate(image));
};
const replaceImageComponents = (html) => {
images.forEach((image) => {
html = replaceHtmlImage(html, image);
});
return html;
};
const loadHtml = async (options) => {
const { basePath, htmlContent } = options;
if (htmlContent) {
const newHtml = await extractAssets(htmlContent, {
basePath,
saveFile: false,
verbose: false,
});
return cheerio.load(newHtml, {
xmlMode: true,
decodeEntities: false,
});
}
};
const getImageSource = (imgTag) => {
return imgTag.attr('src') || '';
};
const getImageAlt = (imgTag) => {
return imgTag.attr('alt') || '';
};
const getParentElement = (imgTag) => {
return imgTag.parent();
};
const getImageStyle = (imgTag) => {
return imgTag.attr('style') || '';
};
const getImageClass = (imgTag) => {
return imgTag.attr('class') || imgTag.attr('className') || '';
};
const getPreviousStyle = (parentElement) => {
return parentElement.attr('style') || '';
};
const getParentClass = (parentElement) => {
return parentElement.attr('class') || parentElement.attr('className') || '';
};
const isBackgroundImage = (imgStyle, imgClass, previousStyle, previousClass) => {
return hasAbsoluteKeyword(imgStyle) || hasAbsoluteKeyword(imgClass) || hasAbsoluteKeyword(previousStyle) || hasAbsoluteKeyword(previousClass);
};
const getImageProperties = (imgTag) => {
const parentElement = getParentElement(imgTag);
const imgStyle = getImageStyle(imgTag);
const imgClass = getImageClass(imgTag);
const previousStyle = getPreviousStyle(parentElement);
const previousClass = getParentClass(parentElement);
const isBackground = isBackgroundImage(imgStyle, imgClass, previousStyle, previousClass);
return {
imgTag,
imgClass,
isBackground,
imgSrc: getImageSource(imgTag),
imgAlt: getImageAlt(imgTag),
};
};
const setImageAttribute = (properties) => {
const { imgTag, imgSrc, imgAlt, attribute, type, prefix } = properties;
const newPrefix = prefix ? convertName(prefix) : 'wp';
const randomVariable = generateRandomVariableName(`${type}${newPrefix}`);
attributes[randomVariable] = {
attribute,
type: 'string',
selector: 'img',
default: attribute === 'alt' ? imgAlt : `var.url+'${imgSrc}'`,
};
imgTag.attr(attribute, `{attributes.${randomVariable}}`);
return randomVariable;
};
const processImage = (properties) => {
const { imgClass, type } = properties;
const randomUrlVariable = setImageAttribute({ ...properties, attribute: 'src', prefix: 'Url' });
const randomAltVariable = setImageAttribute({ ...properties, attribute: 'alt', prefix: 'Alt' });
if (type !== 'background') {
images.push({ randomUrlVariable, randomAltVariable, imgClass });
return;
}
createPanel({
type: 'media',
title: 'Background Image',
attributes: [randomUrlVariable, randomAltVariable],
});
};
const getFixedHtml = (html) => {
return html.replace(/ onChange="{" \(newtext\)=""\>/gi, ' onChange={ (newtext) => ').replace(/\<\/RichText\>/gi, '').replace(/value="{(.*?)}"/gi, 'value={$1}').replace(/"{attributes.(.*?)}"/gi, '{attributes.$1}');
};
const processImages = (imgTag) => {
const properties = getImageProperties(imgTag);
const { isBackground } = properties;
if (!isBackground) {
processImage({ ...properties, type: 'image' });
return;
}
processImage({ ...properties, type: 'background' });
};
const loopImages = ($) => {
$('img').each((_index, img) => {
processImages($(img));
});
};
const getHtml = ($) => {
return $.html({ xml: false, decodeEntities: false });
};
const processEditImages = async (options) => {
const $ = await loadHtml(options);
loopImages($);
return replaceImageComponents(getFixedHtml(getHtml($)));
};
const getRichTextTemplate = (randomVariable, variableContent) => {
return `
><RichText
tagName="span"
value={attributes.${randomVariable}}
default="${variableContent.trim()}"
onChange={ (newtext) => {
setAttributes({ ${randomVariable}: newtext });
}}
/><`;
};
const convertToRichText = (variableContent) => {
const randomVariable = generateRandomVariableName('content');
setAttributeContent(randomVariable, variableContent);
return getRichTextTemplate(randomVariable, variableContent);
};
const parseContent = (content) => {
return content.replace(/>([^<]+)</g, (match, variableContent) => {
if (match.replace(/\s\S/g, '').replace(/(<|>)/g, '').trim() === '') {
return match;
}
return convertToRichText(variableContent);
});
};
const editJsxContent = async (options) => {
let content;
if (options.htmlContent) {
content = options.htmlContent.replaceAll(/<!--(.*?)-->/sg, '');
}
content = `<div>${content}</div>`;
return await processEditImages({ ...options, htmlContent: convert(parseContent(content)) });
};
const createPanel = (values) => {
if (values.attributes && values.attributes.length > 0) {
panels.push(values);
}
};
const getSvgTemplate = (_match, group1, group3, randomSVGVariable) => {
return `
<svg
${group1}
dangerouslySetInnerHTML={ { __html: attributes.${randomSVGVariable} }}
>
${group3}
`;
};
const replaceSVGImages = (html) => {
return html.replace(/<\s*svg\b((?:[^>'"]|"[^"]*"|'[^']*')*)>(\s*(?:[^<]|<(?!\/svg\s*>))*)(<\/\s*svg\s*>)/gim, (match, group1, group2, group3) => {
const content = group2.trim();
if (content) {
const randomSVGVariable = generateRandomVariableName('svg');
setAttributeContent(randomSVGVariable, content);
createPanel({
type: 'svg',
title: 'SVG Markup',
attributes: [randomSVGVariable]
});
return getSvgTemplate(match, group1, group3, randomSVGVariable);
}
return match;
});
};
const getSvgPanelTemplate = (panel) => {
return panel.attributes && attributes[panel.attributes] ? `
{ (
<PanelBody title="${panel.title}">
<PanelRow>
<div>
<TextareaControl
label="SVG Content"
help="Enter your SVG content..."
value={ attributes.${panel.attributes} }
onChange={ ( value ) => {
setAttributes({ ${panel.attributes}: value });
} }
/>
</div>
</PanelRow>
</PanelBody>
)}
` : '';
};
const getMediaPanelTemplate = (panel) => {
const mediaAtts = panel.attributes?.[0] && panel.attributes[1] ? `${panel.attributes[0]}: media.url,
${panel.attributes[1]}: media.alt` : '';
return panel.attributes && panel.attributes[0] && attributes[panel.attributes[0]] ? `
<PanelBody title="${panel.title}">
<PanelRow>
<div>
<MediaUpload
onSelect={ (media) => {
setAttributes({
${mediaAtts}
});
} }
type="image"
value={ attributes.${panel.attributes?.[0]} }
render={({ open }) => (
<button onClick={ open }>Select Image</button>
)}
/>
{attributes.${panel.attributes?.[0]} && (
<img src={attributes.${panel.attributes?.[0]}} alt={attributes.${panel.attributes?.[1]}} />
)}
</div>
</PanelRow>
</PanelBody>
` : '';
};
const getPanelTemplate = (panel) => {
switch (panel.type) {
case 'svg':
return getSvgPanelTemplate(panel);
case 'media':
return getMediaPanelTemplate(panel);
default:
return '';
}
};
const getPanelsTemplate = () => {
return panels.map((panel) => {
return getPanelTemplate(panel);
}).join('\n');
};
const createPanels = () => {
return `
<Panel>
${getPanelsTemplate()}
</Panel>`;
};
const getSaveContent = (editContent) => {
return editContent.replace(/<RichText((.|\n)*?)value=\{(.*?)\}((.|\n)*?)\/>/gi, '<RichText.Content value={$3} />');
};
const saveHtmlContent = (editContent) => {
return getSaveContent(editContent).replace(/className=/gi, 'class=');
};
const removeHref = (match) => {
return match.replace(/href="(.*?)"/, '');
};
const replaceRichText = (match, group1, _group2, group3) => {
return removeHref(match).replace(group1, '<span').replace(group3, '</span>');
};
const processLinks = (options) => {
const htmlContent = options.htmlContent ? options.htmlContent.replace(/(<a)[^>]*>([\s\S]*?)(<\/a>)/gim, replaceRichText) : undefined;
return {
...options,
htmlContent,
};
};
const transformOnClickEvent = (img) => {
return img.replace(/onClick={[^}]+}\s*/, '');
};
const processSaveImages = (htmlString) => {
return htmlString.replace(/<MediaUpload\b[^>]*>([\s\S]*?(<img\b[^>]*>*\/>)[\s\S]*?)\/>/g, (_match, _attributes, img) => transformOnClickEvent(img));
};
const getComponentAttributes = () => {
return JSON.stringify(attributes, null, 2);
};
const getEdit = async (options) => {
let { htmlContent } = options;
if (htmlContent) {
htmlContent = await editJsxContent(processLinks(options));
return replaceSVGImages(htmlContent);
}
return '';
};
const getSave = (edit) => {
return processSaveImages(saveHtmlContent(edit));
};
const getBlock = async (htmlContent, settings) => {
let { prefix, name, category, generateIconPreview, basePath, cssFiles, jsFiles } = settings;
const newName = convertName(name);
const newPrefix = convertName(prefix);
cssFiles = cssFiles || [];
jsFiles = jsFiles || [];
let iconPreview = '\'shield\'';
let edit = await getEdit(settings);
edit = edit.replace(/dangerouslySetInnerHTML="{" {="" __html:="" (.*?)="" }}=""/gm, `dangerouslySetInnerHTML={{ __html: $1 }}`);
const save = getSave(edit);
const blockPanels = createPanels();
const blockAttributes = `${JSON.parse(JSON.stringify(getComponentAttributes(), null, 2)).replace(/"var.url\+\'(.*?)\'(.*?)"/g, 'vars.url+\'$1\'$2')}`;
if (generateIconPreview) {
try {
await icon(htmlContent, { basePath, cssFiles, jsFiles });
iconPreview = `(<img src="data:image/jpeg;base64,${await imageToBase64(path.join(basePath, 'preview.jpeg'))}" />)`;
}
catch (error) {
console.log(`There was an error generating preview. ${error.message}`);
}
}
const output = `
${imports}
registerBlockType('${newPrefix}/${newName}', {
title: '${newName}',
icon: ${iconPreview},
category: '${category}',
attributes: ${blockAttributes},
edit(props) {
const { attributes, setAttributes } = props;
return (
<div>
<InspectorControls>
${blockPanels}
</InspectorControls>
${edit}
</div>
);
},
save(props) {
const { attributes } = props;
return (
${save}
);
},
});
`;
if (generateIconPreview) {
return output.replace(/icon: \s * (')([^']*)(')/, 'icon: $2');
}
return output;
};
const setupVariables = async (htmlContent, options) => {
let { basePath = process.cwd(), cssFiles = [], jsFiles = [], name = 'My block' } = options;
const newDir = path.join(basePath, convertName(name));
try {
fs.mkdirSync(newDir, { recursive: true });
return {
...options,
jsFiles,
cssFiles,
htmlContent,
basePath: newDir
};
}
catch (error) {
logError(error);
}
};
return saveFiles(await setupVariables(htmlContent, options));
};
export default block;