ba58a745f067aa2f2c5b6582e0bf70edffbf245a4263a7bd91d255ef6809a4e133f3419ec48fd25c81cc2314b24f30a81d29c97b508a38551916ac154bd607 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import Attributor from '../attributor/attributor.js';
  2. import AttributorStore from '../attributor/store.js';
  3. import Scope from '../scope.js';
  4. import type {
  5. Blot,
  6. BlotConstructor,
  7. Formattable,
  8. Root,
  9. } from './abstract/blot.js';
  10. import LeafBlot from './abstract/leaf.js';
  11. import ParentBlot from './abstract/parent.js';
  12. import InlineBlot from './inline.js';
  13. class BlockBlot extends ParentBlot implements Formattable {
  14. public static blotName = 'block';
  15. public static scope = Scope.BLOCK_BLOT;
  16. public static tagName: string | string[] = 'P';
  17. public static allowedChildren: BlotConstructor[] = [
  18. InlineBlot,
  19. BlockBlot,
  20. LeafBlot,
  21. ];
  22. static create(value?: unknown) {
  23. return super.create(value) as HTMLElement;
  24. }
  25. public static formats(domNode: HTMLElement, scroll: Root): any {
  26. const match = scroll.query(BlockBlot.blotName);
  27. if (
  28. match != null &&
  29. domNode.tagName === (match as BlotConstructor).tagName
  30. ) {
  31. return undefined;
  32. } else if (typeof this.tagName === 'string') {
  33. return true;
  34. } else if (Array.isArray(this.tagName)) {
  35. return domNode.tagName.toLowerCase();
  36. }
  37. }
  38. protected attributes: AttributorStore;
  39. constructor(scroll: Root, domNode: Node) {
  40. super(scroll, domNode);
  41. this.attributes = new AttributorStore(this.domNode);
  42. }
  43. public format(name: string, value: any): void {
  44. const format = this.scroll.query(name, Scope.BLOCK);
  45. if (format == null) {
  46. return;
  47. } else if (format instanceof Attributor) {
  48. this.attributes.attribute(format, value);
  49. } else if (name === this.statics.blotName && !value) {
  50. this.replaceWith(BlockBlot.blotName);
  51. } else if (
  52. value &&
  53. (name !== this.statics.blotName || this.formats()[name] !== value)
  54. ) {
  55. this.replaceWith(name, value);
  56. }
  57. }
  58. public formats(): { [index: string]: any } {
  59. const formats = this.attributes.values();
  60. const format = this.statics.formats(this.domNode, this.scroll);
  61. if (format != null) {
  62. formats[this.statics.blotName] = format;
  63. }
  64. return formats;
  65. }
  66. public formatAt(
  67. index: number,
  68. length: number,
  69. name: string,
  70. value: any,
  71. ): void {
  72. if (this.scroll.query(name, Scope.BLOCK) != null) {
  73. this.format(name, value);
  74. } else {
  75. super.formatAt(index, length, name, value);
  76. }
  77. }
  78. public insertAt(index: number, value: string, def?: any): void {
  79. if (def == null || this.scroll.query(value, Scope.INLINE) != null) {
  80. // Insert text or inline
  81. super.insertAt(index, value, def);
  82. } else {
  83. const after = this.split(index);
  84. if (after != null) {
  85. const blot = this.scroll.create(value, def);
  86. after.parent.insertBefore(blot, after);
  87. } else {
  88. throw new Error('Attempt to insertAt after block boundaries');
  89. }
  90. }
  91. }
  92. public replaceWith(name: string | Blot, value?: any): Blot {
  93. const replacement = super.replaceWith(name, value) as BlockBlot;
  94. this.attributes.copy(replacement);
  95. return replacement;
  96. }
  97. public update(
  98. mutations: MutationRecord[],
  99. context: { [key: string]: any },
  100. ): void {
  101. super.update(mutations, context);
  102. const attributeChanged = mutations.some(
  103. (mutation) =>
  104. mutation.target === this.domNode && mutation.type === 'attributes',
  105. );
  106. if (attributeChanged) {
  107. this.attributes.build();
  108. }
  109. }
  110. }
  111. export default BlockBlot;