video.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { BlockEmbed } from '../blots/block';
  2. import Link from './link';
  3. const ATTRIBUTES = ['height', 'width'];
  4. class Video extends BlockEmbed {
  5. static create(value) {
  6. const node = super.create(value);
  7. node.setAttribute('frameborder', '0');
  8. node.setAttribute('allowfullscreen', true);
  9. node.setAttribute('src', this.sanitize(value));
  10. return node;
  11. }
  12. static formats(domNode) {
  13. return ATTRIBUTES.reduce((formats, attribute) => {
  14. if (domNode.hasAttribute(attribute)) {
  15. formats[attribute] = domNode.getAttribute(attribute);
  16. }
  17. return formats;
  18. }, {});
  19. }
  20. static sanitize(url) {
  21. return Link.sanitize(url); // eslint-disable-line import/no-named-as-default-member
  22. }
  23. static value(domNode) {
  24. return domNode.getAttribute('src');
  25. }
  26. format(name, value) {
  27. if (ATTRIBUTES.indexOf(name) > -1) {
  28. if (value) {
  29. this.domNode.setAttribute(name, value);
  30. } else {
  31. this.domNode.removeAttribute(name);
  32. }
  33. } else {
  34. super.format(name, value);
  35. }
  36. }
  37. html() {
  38. const { video } = this.value();
  39. return `<a href="${video}">${video}</a>`;
  40. }
  41. }
  42. Video.blotName = 'video';
  43. Video.className = 'ql-video';
  44. Video.tagName = 'IFRAME';
  45. export default Video;