gallery.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { Node, mergeAttributes } from '@tiptap/core'
  2. export default Node.create({
  3. name: 'nodeView',
  4. group: 'block',
  5. content: 'inline*',
  6. parseHTML () {
  7. return [
  8. {
  9. tag: 'div[class="gallery"]',
  10. }
  11. ]
  12. },
  13. defaultOptions: {
  14. HTMLAttributes: {
  15. class: 'gallery',
  16. }
  17. },
  18. addAttributes () {
  19. return {
  20. id: {
  21. default: null,
  22. parseHTML: element => element.getAttribute('id')
  23. }
  24. }
  25. },
  26. renderHTML ({ HTMLAttributes }) {
  27. return [
  28. 'div',
  29. mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
  30. 0
  31. ]
  32. },
  33. addNodeView () {
  34. return ({ node }) => {
  35. const dom = document.createElement('div')
  36. dom.classList.add('editor-gallery')
  37. dom.setAttribute('id', node.attrs.id);
  38. dom.contentEditable = false
  39. const content = document.createElement('div')
  40. content.classList.add('content')
  41. content.contentEditable = false
  42. content.onclick = (e) => e.preventDefault();
  43. const buttonEdit = document.createElement('button')
  44. buttonEdit.innerHTML = 'Редактировать'
  45. buttonEdit.classList.add('button', 'button-edit')
  46. buttonEdit.contentEditable = false
  47. const buttonDelete = document.createElement('button')
  48. buttonDelete.innerHTML = 'Удалить'
  49. buttonDelete.classList.add('button', 'button-delete')
  50. buttonDelete.contentEditable = false
  51. dom.append(content, buttonEdit, buttonDelete)
  52. return {
  53. dom,
  54. contentDOM: content
  55. }
  56. }
  57. }
  58. })