-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
320 lines (310 loc) · 12.3 KB
/
index.html
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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>ppt2json</title>
<link rel="stylesheet" href="static/jsoneditor.min.css">
<script src="static/jsoneditor.min.js"></script>
<style>
::-webkit-scrollbar {
width: 5px;
height: 5px;
background-color: #fff;
}
::-webkit-scrollbar-thumb {
background-color: #c1c1c1;
}
.docmee {
z-index: 9999;
background-image: linear-gradient(to left, rgb(107, 90, 196), rgb(96, 84, 189));
background-clip: text;
color: transparent;
font-weight: bold;
display: inline-block;
font-size: 1.125rem;
line-height: 1.75rem;
user-select: none;
cursor: pointer;
position: fixed;
top: 10px;
left: 15px;
}
#json {
padding: 10px;
}
.jsoneditor-mode-view {
border: 2px solid #ddd;
}
.jsoneditor-menu {
display: none;
}
.jsoneditor-navigation-bar {
display: none;
}
.jsoneditor-outer.has-nav-bar.has-main-menu-bar {
margin-top: 0 !important;
padding-top: 0 !important;
}
#pageList {
list-style-type: none;
}
#pageList li {
display: inline;
padding: 1px 4px;
margin-left: 8px;
cursor: pointer;
}
.selected {
color: green;
border: 1px solid green;
}
#container > svg {
border: 1px solid #ccc;
}
</style>
</head>
<body style="margin: 20px">
<a href="https://docmee.cn" target="_blank" class="docmee" title="进入文多多AiPPT官网">文多多 AiPPT</a>
<div style="margin-top: 50px;">
<input id="file" type="file" accept="application/vnd.openxmlformats-officedocument.presentationml.presentation, application/json" onclick="document.getElementById('file').value = ''" onchange="ppt2json()" />
<span id="refresh_json" style="color: #999;cursor: pointer;margin-right: 15px;" title="刷新JSON数据" onclick="refreshJson()">刷新</span>
<span id="download_json" style="color: #317ef3;display: none;cursor: pointer;margin-right: 15px;" title="下载json数据" onclick="downloadJson()">下载json文件</span>
<span id="download_pptx" style="color: #317ef3;display: none;cursor: pointer;" title="渲染pptx并下载" onclick="json2ppt()">渲染pptx并下载</span>
</div>
<div id="json" style="margin-top: 20px"></div>
<div>
<ul id="pageList"></ul>
<span>编辑模式:</span>
<input id="mode_checkbox" type="checkbox" checked onchange="changeMode()" />
<select id="insert_element" style="display: none;margin-left: 10px" onchange="insertElement(this.value.split(',')[0], this.value.split(',')[1])">
<option value="">-- 插入元素 --</option>
<option value="text,title1">插入大标题</option>
<option value="text,title2">插入副标题</option>
<option value="text,content">插入正文文本</option>
<option value="image">插入图片</option>
<option value="geometry">插入随机形状</option>
<option value="table">插入表格</option>
<option value="chart,bar">插入柱状图</option>
<option value="chart,pie">插入饼图</option>
<option value="chart,doughnut">插入环形图</option>
<option value="chart,line">插入折线图</option>
</select>
</div>
<div id="container" style="margin-top: 10px;">
<svg id="ppt_svg"></svg>
</div>
</body>
<script src="static/chart.js"></script>
<script src="static/geometry.js"></script>
<script src="static/ppt2svg.js"></script>
<script src="static/element.js"></script>
<script>
const apiKey = '4u2Fo50Alk1ym2Os'
var editor = new JSONEditor(document.querySelector('#json'), {}, {})
var pptxObj = null
var selectIdx = 0
var mTimer = null
var ppt2svg = new Ppt2Svg('ppt_svg', 960, 540)
function ppt2json() {
let file = document.getElementById('file').files[0]
if (file.name.endsWith('.json')) {
let reader = new FileReader()
reader.onload = function(e) {
let json = e.target.result
let pptxObj = JSON.parse(json)
editor.set(pptxObj)
drawPageList(pptxObj)
}
reader.onerror = function(e) {
editor.set({ message: '读取错误: ' + e.target.error })
}
reader.readAsText(file)
return
}
if (file.size > 1048576 * 50) {
alert('文件不能超过50M')
return
}
if (file) {
let formData = new FormData()
formData.append('file', file)
let xhr = new XMLHttpRequest()
xhr.open('POST', 'https://docmee.cn/api/public/ppt/ppt2json?apiKey=' + apiKey)
editor.set({ message: '解析中,请稍后...' })
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
// 解析
let pptxObj = JSON.parse(xhr.responseText)
if (pptxObj.code == -1 && pptxObj.message) {
alert(pptxObj.message)
return
}
editor.set(pptxObj)
drawPageList(pptxObj)
} else {
editor.set({ message: 'http status: ' + xhr.status })
}
}
}
xhr.send(formData)
} else {
editor.set({ message: '请选择文件' })
}
}
function json2ppt() {
if (!pptxObj) {
return
}
// 渲染pptx并下载
if (download_pptx.innerText.indexOf('下载') == -1) {
return
}
download_pptx.innerText = '正在渲染中...'
let xhr = new XMLHttpRequest()
xhr.responseType = 'blob'
xhr.open('POST', 'https://docmee.cn/api/public/ppt/json2ppt?apiKey=' + apiKey)
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.onload = function() {
if (this.status == 200) {
download_pptx.innerText = '渲染pptx并下载'
let blob = this.response
if (blob.type == 'application/json') {
const reader = new FileReader()
reader.onload = function(e) {
let json = JSON.parse(e.target.result)
alert('下载失败:' + json.message)
}
reader.readAsText(blob)
return
}
let a = document.createElement('a')
a.href = window.URL.createObjectURL(blob)
let name = 'download'
let file = document.getElementById('file').files[0]
if (file) {
name = file.name.replace('.pptx', '').replace('.json', '')
}
a.download = name + '.pptx'
a.click()
}
}
xhr.send(JSON.stringify(pptxObj))
}
function downloadJson() {
if (!pptxObj) {
return
}
let urlObject = window.URL || window.webkitURL || window
let export_blob = new Blob([JSON.stringify(pptxObj)])
let save_link = document.createElement('a')
save_link.href = urlObject.createObjectURL(export_blob)
let name = 'download'
let file = document.getElementById('file').files[0]
if (file) {
name = file.name.replace('.pptx', '').replace('.json', '')
}
save_link.download = name + '.json'
save_link.click()
}
function drawPageList(_pptxObj) {
counter = 0
selectIdx = 0
imageCache = {}
pptxObj = _pptxObj
document.getElementById('download_json').style.display = 'inline'
document.getElementById('download_pptx').style.display = 'inline'
document.getElementById('insert_element').style.display = document.getElementById('mode_checkbox').checked ? 'inline-block' : 'none'
let pageList = document.getElementById('pageList')
pageList.innerHTML = '';
for (let i = 0; i < pptxObj.pages.length; i++) {
let page = pptxObj.pages[i]
let li = document.createElement('li')
li.id = 'li_' + i
li.innerText = i + 1 + ''
li.addEventListener('click', () => {
document.getElementById('li_' + selectIdx).classList.remove('selected')
selectIdx = i
li.classList.add('selected')
ppt2svg.drawPptx(pptxObj, selectIdx)
})
if (i == selectIdx) {
li.classList.add('selected')
}
pageList.appendChild(li)
}
ppt2svg.drawPptx(pptxObj, selectIdx)
}
function refreshJson() {
pptxObj = editor.get()
ppt2svg.drawPptx(pptxObj, selectIdx)
}
function changeMode() {
ppt2svg.setMode(document.getElementById('mode_checkbox').checked ? 'edit' : 'view')
document.getElementById('insert_element').style.display = pptxObj && document.getElementById('mode_checkbox').checked ? 'inline-block' : 'none'
}
ppt2svg.onchange = (elementObj) => {
editor.set(pptxObj)
}
changeMode()
function insertElement(type, subType) {
let element = null
if (type == 'text') {
// 文本
element = createTextBox(subType)
} else if (type == 'image') {
// 图片
let input = document.createElement('input')
input.type = 'file'
input.accept = '.jpg,.png'
input.onchange = function(e1) {
const file = e1.target.files[0]
if (!file) return
const reader = new FileReader()
reader.onload = function(e2) {
const base64 = e2.target.result
const img = new Image()
img.src = base64
img.onload = function() {
const width = img.width > 300 ? 300 : img.width
const height = img.height * (width / img.width)
element = createImage(base64, width, height)
pptxObj.pages[selectIdx].children.push(element)
ppt2svg.drawPptx(pptxObj, selectIdx)
}
}
reader.readAsDataURL(file)
}
input.click()
} else if (type == 'geometry') {
// 形状
let geometryName = subType
if (!geometryName) {
// 随机形状
let keys = Object.keys(geometryMap)
geometryName = keys[Math.floor(Math.random() * keys.length)]
console.log('geometry', geometryName)
}
element = createGeometry(geometryName)
} else if (type == 'table') {
// 表格
let rowColumnDataList = [['第1行1列', '第1行2列', '第1行3列'], ['第2行1列', '第2行2列', '第2行3列'], ['第3行1列', '第3行2列', '第3行3列']]
element = createTable(rowColumnDataList)
} else if (type == 'chart') {
// 图表
let rowColumnDataList = []
if (subType == 'pie' || subType == 'doughnut') {
rowColumnDataList = [[' ','销售额'], ['第一季度','8.2'], ['第二季度','3.2'], ['第三季度','1.4'], ['第四季度','1.2']]
} else {
rowColumnDataList = [[' ','系列 1','系列 2','系列 3'], ['类别 1','4.3','2.4','2'], ['类别 2','2.5','4.4','2'], ['类别 3','3.5','1.8','3'], ['类别 4','4.5','2.8','5']]
}
element = createChart('图表标题', subType, rowColumnDataList)
}
if (element) {
pptxObj.pages[selectIdx].children.push(element)
ppt2svg.drawPptx(pptxObj, selectIdx)
}
document.getElementById('insert_element').value = ''
}
</script>
</html>