-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfigure.js
41 lines (41 loc) · 1.31 KB
/
figure.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
CMS.registerEditorComponent({
id: "figure",
label: "Figure",
fields: [{
name: "title",
label: "Title",
widget: "string",
}, {
name: "alt",
label: "Description",
widget: "string"
}, {
name: "src",
label: "Image",
widget: "image"
}],
pattern: /{{<\s*figure(.*)>}}/,
fromBlock: function (input) {
let output = {alt: "", src: "", title: ""}
let options = input[1].match(/\w+\s*=\s*"[^"]*"/g);
if (options) {
options.forEach((i) => {
const keyValue = i.split("=");
output = {...output, [keyValue[0]]: keyValue[1].replace(/"/g, '')}
});
}
return output;
},
toBlock: function (obj) {
let options = "";
options += obj.src ? ` src="${obj.src}"` : '';
options += obj.alt ? ` alt="${obj.alt}"` : '';
options += obj.title ? ` title="${obj.title}"` : '';
return `{{< figure ${options}>}}`;
},
toPreview: ({title, alt, src}, getAsset, fields) => {
const imageField = fields?.find(f => f.get('widget') === 'image');
const imgSrc = getAsset(src, imageField);
return `<figure><img src="${imgSrc}" alt="${alt}"><figcaption><h4>${title}</h4></figcaption></figure>`;
},
});