-
Notifications
You must be signed in to change notification settings - Fork 5
/
doc-media.js
342 lines (296 loc) · 12.4 KB
/
doc-media.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
//noinspection JSLint
var g = typeof global === 'undefined' ? window : global;
if (!g.JST) {
g._ = require('underscore');
g.JST = require('../min/nanoislands.jst.js').JST;
var jst_citrea = require('../min/citrea.jst.js').JST;
var tmpl_name;
for (tmpl_name in jst_citrea) {
if (!jst_citrea.hasOwnProperty(tmpl_name)) { continue; }
g.JST[tmpl_name] = jst_citrea[tmpl_name];
}
}
//noinspection JSLint,JSHint
(function (papyrus, JST) {
require('./doc-text.js');
require('./file-icons.js');
var spcf = papyrus.spcf;
var Doc = papyrus.Doc;
Doc._p.insertMediaContent = function (pid, mediaInfo) {
var doc = this;
if (!pid) { pid = ';03+02'; }
pid = spcf.get(pid, ';');
if (!doc.has(pid + '.pw')) { throw new Error('no paragraph found "' + pid + '"'); }
if (mediaInfo.mediatype !== 'embed-video') {
mediaInfo.url = '/yadisk/download?private_hash=' + encodeURIComponent(mediaInfo.hash);
mediaInfo.previewUrl = '/yadisk/preview?private_hash=' + encodeURIComponent(mediaInfo.hash) + '&size=M';
mediaInfo.fullsizeUrl = '/yadisk/preview?private_hash=' + encodeURIComponent(mediaInfo.hash) + '&size=ORIGINAL';
}
var val = {
pid: pid,
filename: mediaInfo.filename,
mediatype: mediaInfo.mediatype,
mimetype: mediaInfo.mimetype,
hash: mediaInfo.hash,
url: mediaInfo.url,
previewUrl: mediaInfo.previewUrl,
viewerUrl: mediaInfo.viewerUrl,
fullsizeUrl: mediaInfo.fullsizeUrl
};
return doc.set('!00.ma', JSON.stringify(val));
};
Doc._p.removeMediaContent = function (mediaId) {
var doc = this;
if (!doc.has(mediaId + '.ma')) { throw new Error('no media-content found with id="' + mediaId + '"'); }
return doc.set('!00.md', mediaId);
};
/**
* @returns {Array} list of all media contents inserted into the document
*/
Doc._p.listAllMediaContent = function () {
var doc = this;
var res = [];
function addMediaInfo(mid, mediaInfo) {
if (mediaInfo.deleted) {
return;
}
mediaInfo.id = mid;
if (mediaInfo.mediatype !== 'embed-video') {
mediaInfo.url = '/yadisk/download?private_hash=' + encodeURIComponent(mediaInfo.hash);
mediaInfo.previewUrl = '/yadisk/preview?private_hash=' + encodeURIComponent(mediaInfo.hash) + '&size=M';
mediaInfo.fullsizeUrl = '/yadisk/preview?private_hash=' + encodeURIComponent(mediaInfo.hash) + '&size=ORIGINAL';
}
res.push(mediaInfo);
}
var pid;
for (pid = ';03+02'; pid; pid = doc.get(pid + '.pn')) {
doc.walkParagraphActualMediaContent(pid, addMediaInfo);
}
return res;
};
papyrus.addFilter('.ma', function (spec, val) {
var doc = this;
var id = spcf.get(spec, '!');
var valParsed = JSON.parse(val);
var mediaAdd = doc.get(valParsed.pid + '.mA') || '';
doc.set(valParsed.pid + '.mA', mediaAdd + id);
});
papyrus.addFilter('.md', function (spec, mediaIdForDel) {
var doc = this;
var id = spcf.get(spec, '!');
var media = JSON.parse(doc.get(mediaIdForDel + '.ma'));
var mediaDel = doc.get(media.pid + '.mD') || '';
doc.set(media.pid + '.mD', mediaDel + id);
});
function reset_media_content() {
var doc = this;
for (var pid = ';03+02'; pid; pid = doc.get(pid + '.pn')) {
if (doc.has(pid + '.mA')) doc.reset(pid + '.mA');
}
}
papyrus.addFilter('.BV', reset_media_content);
papyrus.addFilter('.RL', reset_media_content);
papyrus.addFilter('.UC', reset_media_content);
Doc._p.copyMediaContent = function (from_pid, to_pid) {
var doc = this;
doc.walkParagraphActualMediaContent(from_pid, function (mid, mediaInfo) {
if (!mediaInfo.deleted) {
doc.insertMediaContent(to_pid, mediaInfo);
}
});
};
var RE_YOUTUBE = /(?:https?:\/\/)(?:www\.)?youtube\.com\/watch\?v=([a-z0-9\-]+)/i;
var RE_VIMEO = /(?:https?:\/\/)vimeo\.com\/(\d+)/i;
papyrus.buildVideoContent = function (url, forExport) {
var m;
//YouTube
RE_YOUTUBE.lastIndex = 0;
m = RE_YOUTUBE.exec(url);
if (m) {
return '<iframe width="500" height="281"' +
' src="' + (forExport?'http:':'') + '//www.youtube.com/embed/' + m[1] + '?wmode=opaque"' +
' frameborder="0"' +
' webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>';
}
//Vimeo
RE_VIMEO.lastIndex = 0;
m = RE_VIMEO.exec(url);
if (m) {
return '<iframe width="500" height="281"' +
' src="' + (forExport?'http:':'') + '//player.vimeo.com/video/' + m[1] + '"' +
' frameborder="0"' +
' webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>';
}
//TODO other video hosting services
return null;
};
function getMediaCSSClass(media_info) {
return ((!media_info.added && !media_info.removed && 'old_on') || '') +
((media_info.removed && ' rm_on') || '');
}
function getPreviewAttrs(doc, mid, pid) {
var uid = doc.get('!' + spcf.parse(mid).ssn + '.sn');
var pform = doc.get(pid + '.tr');
var off_color_uids = doc.get('.UC') || '';
var preview_attrs = {};
var p_uid = spcf.get(pform, '@');
if (!doc.redliningOn && uid !== p_uid &&
(!off_color_uids || ((off_color_uids.indexOf(p_uid) > -1) ^ (off_color_uids.indexOf(uid) > -1)))) {
preview_attrs.au = uid;
}
return preview_attrs;
}
var mediatype2renderer = {
document: function (mid, media_info, forExport) {
var doc = this;
if (forExport) {
return '<a href="' + '/yadisk/download?private_hash=' + encodeURIComponent(media_info.hash) + '">' + (media_info.filename || 'вложение') + '</a><br />';
}
return JST.filecard({
id: mid,
'class': getMediaCSSClass(media_info),
url: '/yadisk/download?private_hash=' + encodeURIComponent(media_info.hash),
viewerUrl: media_info.viewerUrl,
mediatype: media_info.mediatype,
mimetype: media_info.mimetype,
title: media_info.filename || 'вложение',
icon: papyrus.getFileIcon(media_info),
withActions: true,
attrs: {
'data-focus': 'capture'
},
preview_attrs: getPreviewAttrs(doc, mid, media_info.pid)
});
},
image: function (mid, media_info, forExport) {
var doc = this;
if (forExport) {
return '<a href="' + '/yadisk/download?private_hash=' + encodeURIComponent(media_info.hash) + '">' + (media_info.filename || 'вложение') + '</a><br />';
}
return JST.filecard({
id: mid,
'class': getMediaCSSClass(media_info),
url: '/yadisk/download?private_hash=' + encodeURIComponent(media_info.hash),
previewUrl: '/yadisk/preview?private_hash=' + encodeURIComponent(media_info.hash) + '&size=M',
viewerUrl: media_info.viewerUrl,
mediatype: media_info.mediatype,
mimetype: media_info.mimetype,
title: media_info.filename || 'документ',
icon: papyrus.getFileIcon(media_info),
withActions: true,
attrs: {
'data-focus': 'capture'
},
preview_attrs: getPreviewAttrs(doc, mid, media_info.pid)
});
},
'embed-video': function (mid, media_info, forExport) {
var doc = this;
var video_content = papyrus.buildVideoContent(media_info.url, forExport);
if (!video_content) { return ''; }
if (forExport) {
return video_content + '<br />';
}
return JST.filecard({
id: mid,
'class': 'nb-filecard_video ' + getMediaCSSClass(media_info),
url: media_info.url,
content: video_content,
withActions: true,
attrs: {
'data-focus': 'capture'
},
preview_attrs: getPreviewAttrs(doc, mid, media_info.pid)
});
}
};
papyrus.mediaRenderers = mediatype2renderer;
/**
* iterate through paragraph' "live" media-content
* @param {string} pid paragraph id
* @param {function(string, {pid, mediatype, mimetype, url, previewUrl, filename})} cb callback
*/
Doc._p.walkParagraphActualMediaContent = function (pid, cb) {
var doc = this,
base_version = spcf.ssnMap(doc.get('.BV') || ''),
media_remove_list = spcf.split(doc.get(pid + '.mD') || ''),
old_deleted = [],
newly_deleted = [],
i, l, mid;
for (i = 0, l = media_remove_list.length; i < l; i++) {
mid = doc.get(media_remove_list[i] + '.md');
if (!doc.redliningOn) {
old_deleted.push(mid);
} else {
var parsed = spcf.getParsed(media_remove_list[i], '!');
if (parsed.seq > (base_version[parsed.ssn] || '00')) {
newly_deleted.push(mid);
} else {
old_deleted.push(mid);
}
}
}
spcf.split(doc.get(pid + '.mA') || '').forEach(function (mid) {
if (old_deleted.indexOf(mid) > -1) {
media_info = {'deleted': true};
cb(mid, media_info);
return;
}
var media_info = doc.get(mid + '.ma');
if (!media_info) {
if (papyrus.DEBUG) { console.warn('missing media: pid=', pid, 'mid=', mid); }
return;
}
var mid_parsed = spcf.getParsed(mid, '!');
media_info = JSON.parse(media_info);
media_info.removed = newly_deleted.indexOf(mid) > -1;
media_info.added = !doc.redliningOn || (mid_parsed.seq > (base_version[mid_parsed.ssn] || '00'));
cb(mid, media_info);
});
};
Doc._p.getMediaBlockHtml = function (pid, forExport) {
var doc = this;
var m_html = ['<ul class="mediabox">','<div class="mediaindentbox"></div>'];
var counter = 0;
var uidMediaCount = {};
doc.walkParagraphActualMediaContent(pid, function (mid, mediaInfo) {
var midParsed = spcf.getParsed(mid, '!');
var uid = doc.get('!' + midParsed.ssn + '.sn');
uidMediaCount[uid] = 0;
if (!mediaInfo.deleted && !mediaInfo.removed) {
var mediaType = mediaInfo.mediatype || 'document';
var renderer = mediatype2renderer[mediaType] || mediatype2renderer.document;
var mediaHtml = renderer.call(doc, mid, mediaInfo, forExport);
if (!mediaHtml) { return; }
m_html.push(mediaHtml);
counter++;
uidMediaCount[uid]++;
}
});
for (var uid in uidMediaCount) {
var mediaValue = uidMediaCount[uid] * 100;
doc.set(uid + pid + '.mc', mediaValue);
}
if (!counter) { return ''; }
m_html.push('</ul>');
m_html.push('<div style="clear:both;"></div>');
return m_html.join('').replace(/(<br\s?\/>\s*){2}/, '<br/>');
};
var getAntiOperations_text = Doc._p.getAntiOperations;
Doc._p.getAntiOperations = function (sv) {
var doc = this;
var res = [];
switch (spcf.type(sv.spec)) {
case '.ma':
var mid = spcf.get(sv.spec, '!');
res.push({spec: '!00.md', val: mid});
break;
case '.md':
res.push({spec: '!00.ma', val: doc.get(sv.val + '.ma')});
break;
default:
res = getAntiOperations_text.apply(this, arguments);
}
return res;
};
}((g['papyrus'] = g['papyrus'] || {}), g.JST));