Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tooltip): adds detection for mobile youtube video #1489

Merged
merged 1 commit into from
Jun 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import './unit/modules/keyboard';
import './unit/modules/toolbar';

import './unit/ui/picker';
import './unit/theme/base/tooltip';


export default Quill;
57 changes: 57 additions & 0 deletions test/unit/theme/base/tooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Quill from '../../../../core';
import { BaseTooltip } from '../../../../themes/base'

class Tooltip extends BaseTooltip {
}

Tooltip.TEMPLATE = '<input type="text">';

describe('BaseTooltip', function() {
describe('save', function() {
beforeEach(function() {
this.quill = this.initialize(Quill, '');
this.tooltip = new Tooltip(this.quill)
});

it('converts youtube video url to embedded', function() {
insertVideo(this.tooltip, 'http://youtube.com/watch?v=QHH3iSeDBLo');
expect(this.container.querySelector('.ql-video').src).toContain('http://www.youtube.com/embed/QHH3iSeDBLo');
});

it('converts www.youtube video url to embedded', function() {
insertVideo(this.tooltip, 'http://www.youtube.com/watch?v=QHH3iSeDBLo');
expect(this.container.querySelector('.ql-video').src).toContain('http://www.youtube.com/embed/QHH3iSeDBLo');
});

it('converts m.youtube video url to embedded', function() {
insertVideo(this.tooltip, 'http://m.youtube.com/watch?v=QHH3iSeDBLo');
expect(this.container.querySelector('.ql-video').src).toContain('http://www.youtube.com/embed/QHH3iSeDBLo');
});

it('preserves youtube video url protocol', function() {
insertVideo(this.tooltip, 'https://m.youtube.com/watch?v=QHH3iSeDBLo');
expect(this.container.querySelector('.ql-video').src).toContain('https://www.youtube.com/embed/QHH3iSeDBLo');
});

it('converts vimeo video url to embedded', function() {
insertVideo(this.tooltip, 'http://vimeo.com/47762693');
expect(this.container.querySelector('.ql-video').src).toContain('http://player.vimeo.com/video/47762693/');
});

it('converts www.vimeo video url to embedded', function() {
insertVideo(this.tooltip, 'http://www.vimeo.com/47762693');
expect(this.container.querySelector('.ql-video').src).toContain('http://player.vimeo.com/video/47762693/');
});

it('preserves vimeo video url protocol', function() {
insertVideo(this.tooltip, 'https://www.vimeo.com/47762693');
expect(this.container.querySelector('.ql-video').src).toContain('https://player.vimeo.com/video/47762693/');
});

function insertVideo(tooltip, url) {
tooltip.textbox.value = url;
tooltip.root.setAttribute('data-mode', 'video');
tooltip.save();
}
});
});
23 changes: 16 additions & 7 deletions themes/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ class BaseTooltip extends Tooltip {
this.quill.scrollingContainer.scrollTop = scrollTop;
}

convertVideoUrlToEmbeded(url) {
let match = url.match(/^(https?):\/\/(?:(?:www|m)\.)?youtube\.com\/watch.*v=([a-zA-Z0-9_-]+)/) ||
url.match(/^(https?):\/\/(?:(?:www|m)\.)?youtu\.be\/([a-zA-Z0-9_-]+)/);

if (match) {
return match[1] + '://www.youtube.com/embed/' + match[2] + '?showinfo=0';
}

if (match = url.match(/^(https?):\/\/(?:www\.)?vimeo\.com\/(\d+)/)) { // eslint-disable-line no-cond-assign
return match[1] + '://player.vimeo.com/video/' + match[2] + '/';
}

return url;
}

save() {
let value = this.textbox.value;
switch(this.root.getAttribute('data-mode')) {
Expand All @@ -212,13 +227,7 @@ class BaseTooltip extends Tooltip {
break;
}
case 'video': {
let match = value.match(/^(https?):\/\/(www\.)?youtube\.com\/watch.*v=([a-zA-Z0-9_-]+)/) ||
value.match(/^(https?):\/\/(www\.)?youtu\.be\/([a-zA-Z0-9_-]+)/);
if (match) {
value = match[1] + '://www.youtube.com/embed/' + match[3] + '?showinfo=0';
} else if (match = value.match(/^(https?):\/\/(www\.)?vimeo\.com\/(\d+)/)) { // eslint-disable-line no-cond-assign
value = match[1] + '://player.vimeo.com/video/' + match[3] + '/';
}
value = this.convertVideoUrlToEmbeded(value);
} // eslint-disable-next-line no-fallthrough
case 'formula': {
if (!value) break;
Expand Down