marks.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { marks } from 'prosemirror-schema-basic'
  2. import { MarkSpec } from 'prosemirror-model'
  3. const subscript: MarkSpec = {
  4. excludes: 'subscript',
  5. parseDOM: [
  6. { tag: 'sub' },
  7. {
  8. style: 'vertical-align',
  9. getAttrs: value => value === 'sub' && null
  10. },
  11. ],
  12. toDOM: () => ['sub', 0],
  13. }
  14. const superscript: MarkSpec = {
  15. excludes: 'superscript',
  16. parseDOM: [
  17. { tag: 'sup' },
  18. {
  19. style: 'vertical-align',
  20. getAttrs: value => value === 'super' && null
  21. },
  22. ],
  23. toDOM: () => ['sup', 0],
  24. }
  25. const strikethrough: MarkSpec = {
  26. parseDOM: [
  27. { tag: 'strike' },
  28. {
  29. style: 'text-decoration',
  30. getAttrs: value => value === 'line-through' && null
  31. },
  32. {
  33. style: 'text-decoration-line',
  34. getAttrs: value => value === 'line-through' && null
  35. },
  36. ],
  37. toDOM: () => ['span', { style: 'text-decoration-line: line-through' }, 0],
  38. }
  39. const underline: MarkSpec = {
  40. parseDOM: [
  41. { tag: 'u' },
  42. {
  43. style: 'text-decoration',
  44. getAttrs: value => value === 'underline' && null
  45. },
  46. {
  47. style: 'text-decoration-line',
  48. getAttrs: value => value === 'underline' && null
  49. },
  50. ],
  51. toDOM: () => ['span', { style: 'text-decoration: underline' }, 0],
  52. }
  53. const forecolor: MarkSpec = {
  54. attrs: {
  55. color: {},
  56. },
  57. parseDOM: [
  58. {
  59. style: 'color',
  60. getAttrs: color => color ? { color } : {}
  61. },
  62. ],
  63. toDOM: mark => {
  64. const { color } = mark.attrs
  65. let style = ''
  66. if (color) style += `color: ${color};`
  67. return ['span', { style }, 0]
  68. },
  69. }
  70. const backcolor: MarkSpec = {
  71. attrs: {
  72. backcolor: {},
  73. },
  74. inline: true,
  75. group: 'inline',
  76. parseDOM: [
  77. {
  78. tag: 'span[style*=background-color]',
  79. getAttrs: backcolor => backcolor ? { backcolor } : {}
  80. },
  81. ],
  82. toDOM: mark => {
  83. const { backcolor } = mark.attrs
  84. let style = ''
  85. if (backcolor) style += `background-color: ${backcolor};`
  86. return ['span', { style }, 0]
  87. },
  88. }
  89. const fontsize: MarkSpec = {
  90. attrs: {
  91. fontsize: {},
  92. },
  93. inline: true,
  94. group: 'inline',
  95. parseDOM: [
  96. {
  97. style: 'font-size',
  98. getAttrs: fontsize => fontsize ? { fontsize } : {}
  99. },
  100. ],
  101. toDOM: mark => {
  102. const { fontsize } = mark.attrs
  103. let style = ''
  104. if (fontsize) style += `font-size: ${fontsize}`
  105. return ['span', { style }, 0]
  106. },
  107. }
  108. const fontname: MarkSpec = {
  109. attrs: {
  110. fontname: {},
  111. },
  112. inline: true,
  113. group: 'inline',
  114. parseDOM: [
  115. {
  116. style: 'font-family',
  117. getAttrs: fontname => {
  118. return { fontname: fontname && typeof fontname === 'string' ? fontname.replace(/[\"\']/g, '') : '' }
  119. }
  120. },
  121. ],
  122. toDOM: mark => {
  123. const { fontname } = mark.attrs
  124. let style = ''
  125. if (fontname) style += `font-family: ${fontname}`
  126. return ['span', { style }, 0]
  127. },
  128. }
  129. export default {
  130. ...marks,
  131. subscript,
  132. superscript,
  133. strikethrough,
  134. underline,
  135. forecolor,
  136. backcolor,
  137. fontsize,
  138. fontname,
  139. }