Skip to content

Commit d757fe7

Browse files
committed
support for Drafty video tag
1 parent 7123927 commit d757fe7

File tree

5 files changed

+166
-8
lines changed

5 files changed

+166
-8
lines changed

src/drafty.js

+100-1
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ const FORMAT_TAGS = {
232232
md_tag: undefined,
233233
isVoid: false
234234
},
235+
VD: {
236+
html_tag: 'video',
237+
md_tag: undefined,
238+
isVoid: false
239+
}
235240
};
236241

237242
// Convert base64-encoded string into Blob.
@@ -434,6 +439,26 @@ const DECORATORS = {
434439
};
435440
}
436441
},
442+
// Video.
443+
VD: {
444+
open: (data) => {
445+
const url = data.ref || base64toObjectUrl(data.val, data.mime, Drafty.logger);
446+
return '<video controls src="' + url + '">';
447+
},
448+
close: _ => '</video>',
449+
props: (data) => {
450+
if (!data) return null;
451+
return {
452+
// Embedded data or external link.
453+
src: data.ref || base64toObjectUrl(data.val, data.mime, Drafty.logger),
454+
'data-preload': data.ref ? 'metadata' : 'auto',
455+
'data-duration': data.duration,
456+
'data-name': data.name,
457+
'data-size': data.val ? ((data.val.length * 0.75) | 0) : (data.size | 0),
458+
'data-mime': data.mime,
459+
};
460+
}
461+
},
437462
};
438463

439464
/**
@@ -726,6 +751,80 @@ Drafty.insertImage = function(content, at, imageDesc) {
726751
return content;
727752
}
728753

754+
/**
755+
* @typedef Drafty.VideoDesc
756+
* @memberof Drafty
757+
* @type Object
758+
* @param {string} mime - mime-type of the video, e.g. "video/mpeg"
759+
* @param {string} preview - base64-encoded video content (or preview, if large video is attached). Could be null/undefined.
760+
* @param {integer} width - width of the video
761+
* @param {integer} height - height of the video
762+
* @param {integer} duration - duration of the video.
763+
* @param {string} filename - file name suggestion for downloading the video.
764+
* @param {integer} size - size of the video in bytes. Treat is as an untrusted hint.
765+
* @param {string} refurl - reference to the content. Could be null/undefined.
766+
* @param {string} _tempPreview - base64-encoded image preview used during upload process; not serializable.
767+
* @param {Promise} urlPromise - Promise which returns content URL when resolved.
768+
*/
769+
770+
/**
771+
* Insert inline image into Drafty document.
772+
* @memberof Drafty
773+
* @static
774+
*
775+
* @param {Drafty} content - document to add video to.
776+
* @param {integer} at - index where the object is inserted. The length of the video is always 1.
777+
* @param {VideoDesc} videoDesc - object with video paramenets and data.
778+
*
779+
* @return {Drafty} updated document.
780+
*/
781+
Drafty.insertVideo = function(content, at, videoDesc) {
782+
content = content || {
783+
txt: ' '
784+
};
785+
content.ent = content.ent || [];
786+
content.fmt = content.fmt || [];
787+
788+
content.fmt.push({
789+
at: at | 0,
790+
len: 1,
791+
key: content.ent.length
792+
});
793+
794+
const ex = {
795+
tp: 'VD',
796+
data: {
797+
mime: videoDesc.mime,
798+
val: videoDesc.preview,
799+
width: videoDesc.width,
800+
height: videoDesc.height,
801+
name: videoDesc.filename,
802+
size: videoDesc.size | 0,
803+
ref: videoDesc.refurl
804+
}
805+
};
806+
807+
if (videoDesc.urlPromise) {
808+
ex.data._tempPreview = videoDesc._tempPreview;
809+
ex.data._processing = true;
810+
videoDesc.urlPromise.then(
811+
url => {
812+
ex.data.ref = url;
813+
ex.data._tempPreview = undefined;
814+
ex.data._processing = undefined;
815+
},
816+
_ => {
817+
// Catch the error, otherwise it will appear in the console.
818+
ex.data._processing = undefined;
819+
}
820+
);
821+
}
822+
823+
content.ent.push(ex);
824+
825+
return content;
826+
}
827+
729828
/**
730829
* @typedef Drafty.AudioDesc
731830
* @memberof Drafty
@@ -830,7 +929,7 @@ Drafty.videoCall = function() {
830929
*
831930
* @returns the same document with update applied.
832931
*/
833-
Drafty.updateVideoEnt = function(content, params) {
932+
Drafty.updateVideoCall = function(content, params) {
834933
// The video element could be just a format or a format + entity.
835934
// Must ensure it's the latter first.
836935
const fmt = ((content || {}).fmt || [])[0];

src/topic.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ export class Topic {
15361536

15371537
if (data.head && data.head.webrtc && data.head.mime == Drafty.getContentType() && data.content) {
15381538
// Rewrite VC body with info from the headers.
1539-
data.content = Drafty.updateVideoEnt(data.content, {
1539+
data.content = Drafty.updateVideoCall(data.content, {
15401540
state: data.head.webrtc,
15411541
duration: data.head['webrtc-duration'],
15421542
incoming: !outgoing,

src/utils.js

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export function jsonParseHelper(key, val) {
1515
// Try to convert string timestamps with optional milliseconds to Date,
1616
// e.g. 2015-09-02T01:45:43[.123]Z
1717
if (typeof val == 'string' && val.length >= 20 && val.length <= 24 && ['ts', 'touched', 'updated', 'created', 'when', 'deleted', 'expires'].includes(key)) {
18-
1918
const date = new Date(val);
2019
if (!isNaN(date)) {
2120
return date;

umd/tinode.dev.js

+63-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

umd/tinode.prod.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)