list.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import Parchment from 'parchment';
  2. import Block from '../blots/block';
  3. import Container from '../blots/container';
  4. class ListItem extends Block {
  5. static formats(domNode) {
  6. return domNode.tagName === this.tagName ? undefined : super.formats(domNode);
  7. }
  8. format(name, value) {
  9. if (name === List.blotName && !value) {
  10. this.replaceWith(Parchment.create(this.statics.scope));
  11. } else {
  12. super.format(name, value);
  13. }
  14. }
  15. remove() {
  16. if (this.prev == null && this.next == null) {
  17. this.parent.remove();
  18. } else {
  19. super.remove();
  20. }
  21. }
  22. replaceWith(name, value) {
  23. this.parent.isolate(this.offset(this.parent), this.length());
  24. if (name === this.parent.statics.blotName) {
  25. this.parent.replaceWith(name, value);
  26. return this;
  27. } else {
  28. this.parent.unwrap();
  29. return super.replaceWith(name, value);
  30. }
  31. }
  32. }
  33. ListItem.blotName = 'list-item';
  34. ListItem.tagName = 'LI';
  35. class List extends Container {
  36. static create(value) {
  37. let tagName = value === 'ordered' ? 'OL' : 'UL';
  38. let node = super.create(tagName);
  39. if (value === 'checked' || value === 'unchecked') {
  40. node.setAttribute('data-checked', value === 'checked');
  41. }
  42. return node;
  43. }
  44. static formats(domNode) {
  45. if (domNode.tagName === 'OL') return 'ordered';
  46. if (domNode.tagName === 'UL') {
  47. if (domNode.hasAttribute('data-checked')) {
  48. return domNode.getAttribute('data-checked') === 'true' ? 'checked' : 'unchecked';
  49. } else {
  50. return 'bullet';
  51. }
  52. }
  53. return undefined;
  54. }
  55. constructor(domNode) {
  56. super(domNode);
  57. const listEventHandler = (e) => {
  58. if (e.target.parentNode !== domNode) return;
  59. let format = this.statics.formats(domNode);
  60. let blot = Parchment.find(e.target);
  61. if (format === 'checked') {
  62. blot.format('list', 'unchecked');
  63. } else if(format === 'unchecked') {
  64. blot.format('list', 'checked');
  65. }
  66. }
  67. domNode.addEventListener('touchstart', listEventHandler);
  68. domNode.addEventListener('mousedown', listEventHandler);
  69. }
  70. format(name, value) {
  71. if (this.children.length > 0) {
  72. this.children.tail.format(name, value);
  73. }
  74. }
  75. formats() {
  76. // We don't inherit from FormatBlot
  77. return { [this.statics.blotName]: this.statics.formats(this.domNode) };
  78. }
  79. insertBefore(blot, ref) {
  80. if (blot instanceof ListItem) {
  81. super.insertBefore(blot, ref);
  82. } else {
  83. let index = ref == null ? this.length() : ref.offset(this);
  84. let after = this.split(index);
  85. after.parent.insertBefore(blot, after);
  86. }
  87. }
  88. optimize(context) {
  89. super.optimize(context);
  90. let next = this.next;
  91. if (next != null && next.prev === this &&
  92. next.statics.blotName === this.statics.blotName &&
  93. next.domNode.tagName === this.domNode.tagName &&
  94. next.domNode.getAttribute('data-checked') === this.domNode.getAttribute('data-checked')) {
  95. next.moveChildren(this);
  96. next.remove();
  97. }
  98. }
  99. replace(target) {
  100. if (target.statics.blotName !== this.statics.blotName) {
  101. let item = Parchment.create(this.statics.defaultChild);
  102. target.moveChildren(item);
  103. this.appendChild(item);
  104. }
  105. super.replace(target);
  106. }
  107. }
  108. List.blotName = 'list';
  109. List.scope = Parchment.Scope.BLOCK_BLOT;
  110. List.tagName = ['OL', 'UL'];
  111. List.defaultChild = 'list-item';
  112. List.allowedChildren = [ListItem];
  113. export { ListItem, List as default };