forked from veasion/AiPPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppt2json.html
215 lines (206 loc) · 7.35 KB
/
ppt2json.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
<!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;
}
#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">
<div>
<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()" />
</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>
const apiKey = '9664f0ea9a4cee65c'
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) {
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
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'
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')
}
ppt2svg.onchange = (elementObj) => {
editor.set(pptxObj)
}
changeMode()
</script>
</html>