iframe.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { Node } from '@tiptap/core'
  2. export interface IframeOptions {
  3. allowFullscreen: boolean,
  4. HTMLAttributes: {
  5. [key: string]: any
  6. },
  7. }
  8. declare module '@tiptap/core' {
  9. interface Commands<ReturnType> {
  10. iframe: {
  11. /**
  12. * Add an iframe
  13. */
  14. setIframe: (options: { src: string }) => ReturnType,
  15. }
  16. }
  17. }
  18. export default Node.create({
  19. name: 'Iframe',
  20. group: 'block',
  21. atom: true,
  22. defaultOptions: <IframeOptions>{
  23. allowFullscreen: true,
  24. HTMLAttributes: {
  25. class: 'iframe-wrapper',
  26. },
  27. },
  28. addAttributes() {
  29. return {
  30. src: {
  31. default: null,
  32. },
  33. frameborder: {
  34. default: 0,
  35. },
  36. scrolling: {
  37. default: "auto",
  38. },
  39. width: {
  40. default: "500",
  41. },
  42. height: {
  43. default: "300"
  44. },
  45. class: {
  46. default: "",
  47. },
  48. allowfullscreen: {
  49. default: this.options.allowFullscreen,
  50. },
  51. }
  52. },
  53. parseHTML() {
  54. return [{
  55. tag: 'iframe',
  56. }]
  57. },
  58. renderHTML({ HTMLAttributes }) {
  59. return ['div', this.options.HTMLAttributes, ['iframe', HTMLAttributes]]
  60. },
  61. addCommands() {
  62. return {
  63. setIframe: (options: { src: string }) => ({ tr, dispatch }) => {
  64. const { selection } = tr
  65. const node = this.type.create(options)
  66. if (dispatch) {
  67. tr.replaceRangeWith(selection.from, selection.to, node)
  68. }
  69. return true
  70. },
  71. }
  72. },
  73. })