block.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import FormatBlot from './abstract/format';
  2. import * as Registry from '../registry';
  3. class BlockBlot extends FormatBlot {
  4. static blotName = 'block';
  5. static scope = Registry.Scope.BLOCK_BLOT;
  6. static tagName = 'P';
  7. static formats(domNode: HTMLElement): any {
  8. let tagName = (<any>Registry.query(BlockBlot.blotName)).tagName;
  9. if (domNode.tagName === tagName) return undefined;
  10. return super.formats(domNode);
  11. }
  12. format(name: string, value: any) {
  13. if (Registry.query(name, Registry.Scope.BLOCK) == null) {
  14. return;
  15. } else if (name === this.statics.blotName && !value) {
  16. this.replaceWith(BlockBlot.blotName);
  17. } else {
  18. super.format(name, value);
  19. }
  20. }
  21. formatAt(index: number, length: number, name: string, value: any): void {
  22. if (Registry.query(name, Registry.Scope.BLOCK) != null) {
  23. this.format(name, value);
  24. } else {
  25. super.formatAt(index, length, name, value);
  26. }
  27. }
  28. insertAt(index: number, value: string, def?: any): void {
  29. if (def == null || Registry.query(value, Registry.Scope.INLINE) != null) {
  30. // Insert text or inline
  31. super.insertAt(index, value, def);
  32. } else {
  33. let after = this.split(index);
  34. let blot = Registry.create(value, def);
  35. after.parent.insertBefore(blot, after);
  36. }
  37. }
  38. update(mutations: MutationRecord[], context: { [key: string]: any }): void {
  39. if (navigator.userAgent.match(/Trident/)) {
  40. this.build();
  41. } else {
  42. super.update(mutations, context);
  43. }
  44. }
  45. }
  46. export default BlockBlot;