nodes.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { nodes } from 'prosemirror-schema-basic'
  2. import { Node, NodeSpec } from 'prosemirror-model'
  3. import { orderedList, bulletList, listItem } from 'prosemirror-schema-list'
  4. const _orderedList: NodeSpec = {
  5. ...orderedList,
  6. content: 'list_item+',
  7. group: 'block',
  8. }
  9. const _bulletList: NodeSpec = {
  10. ...bulletList,
  11. content: 'list_item+',
  12. group: 'block',
  13. }
  14. const _listItem: NodeSpec = {
  15. ...listItem,
  16. content: 'paragraph block*',
  17. group: 'block',
  18. }
  19. const paragraph: NodeSpec = {
  20. attrs: {
  21. align: {
  22. default: '',
  23. },
  24. },
  25. content: 'inline*',
  26. group: 'block',
  27. parseDOM: [
  28. {
  29. tag: 'p',
  30. getAttrs: dom => {
  31. const { textAlign } = (dom as HTMLElement).style
  32. let align = (dom as HTMLElement).getAttribute('align') || textAlign || ''
  33. align = /(left|right|center|justify)/.test(align) ? align : ''
  34. return { align }
  35. }
  36. }
  37. ],
  38. toDOM: (node: Node) => {
  39. const { align } = node.attrs
  40. let style = ''
  41. if (align && align !== 'left') style += `text-align: ${align};`
  42. return ['p', { style }, 0]
  43. },
  44. }
  45. export default {
  46. ...nodes,
  47. 'ordered_list': _orderedList,
  48. 'bullet_list': _bulletList,
  49. 'list_item': _listItem,
  50. paragraph,
  51. }