-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppt-producer.js
142 lines (133 loc) · 3.19 KB
/
ppt-producer.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
import React, {useState, useEffect} from 'react';
import pptxgen from 'pptxgenjs';
import sanityClient from 'part:@sanity/base/client';
import {withDocument} from 'part:@sanity/form-builder';
import bgimg from './static/bgimg.jpg';
import mainbg from './static/mainbg.jpg';
function toPlainText(blocks = []) {
return (
blocks
// Loop through each block
.map(block => {
// If it's not a text block with children,
// return nothing
if (block._type !== 'block' || !block.children) {
return '';
}
// Loop through the children spans, and join the
// text strings
return block.children.map(child => child.text).join('\n');
})
);
}
function slugify(text) {
return text
.toString()
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
const pptx = new pptxgen();
pptx.layout = 'LAYOUT_16x10';
pptx.defineSlideMaster({
title: 'TITLE_SLIDE',
objects: [
{image: {x: 0, y: 0, w: '100%', h: '100%', path: bgimg}},
{
placeholder: {
options: {
name: 'title',
type: 'title',
x: 1.22,
y: 0.76,
w: 8.46,
h: 3.07,
align: 'left',
bold: true,
fontSize: 44,
fontFace: 'Arial',
color: 'D6FEFF',
shadow: {
type: 'outer',
angle: 45,
blur: 3,
offset: 3
}
},
text: 'title here'
}
},
{
placeholder: {
options: {
name: 'hymnNumber',
type: 'body',
x: 1.28,
y: 2.85,
w: 7,
h: 1.6,
align: 'left',
bold: true,
fontSize: 21,
fontFace: 'Arial',
color: 'FFFFFF',
shadow: {
type: 'outer',
angle: 45,
blur: 3,
offset: 3
}
},
text: 'hymn number here'
}
}
]
});
pptx.defineSlideMaster({
title: 'LYRIC_SLIDE',
bkgd: {path: mainbg},
objects: [
{
placeholder: {
options: {
name: 'body',
type: 'body',
x: 0.17,
y: 0.21,
w: 9.58,
h: 5.42,
align: 'left',
fontSize: 37,
bold: true,
fontFace: 'Arial',
color: 'FFFFFF',
shadow: {
angle: 45,
blur: 3,
offset: 3
}
},
text: 'Lyrics here'
}
}
]
});
function produceSlide({title, content, hymnNumber}) {
const verses = toPlainText(content);
pptx
.addSlide('TITLE_SLIDE')
.addText(title, {placeholder: 'title'})
.addText(hymnNumber === 0 ? '' : `Rejoice ${hymnNumber}`, {
placeholder: 'hymnNumber'
});
verses.map(para =>
pptx.addSlide('LYRIC_SLIDE').addText(para, {placeholder: 'body'})
);
pptx.writeFile(
`${hymnNumber !== 0 ? hymnNumber + '-' : ''}${slugify(title)}.pptx`
);
}
export default produceSlide;