leaf.ts 877 B

123456789101112131415161718192021222324252627282930313233
  1. import { Formattable, Leaf } from './blot';
  2. import ShadowBlot from './shadow';
  3. import * as Registry from '../../registry';
  4. class LeafBlot extends ShadowBlot implements Leaf {
  5. static scope = Registry.Scope.INLINE_BLOT;
  6. static value(domNode: Node): any {
  7. return true;
  8. }
  9. index(node: Node, offset: number): number {
  10. if (
  11. this.domNode === node ||
  12. this.domNode.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_CONTAINED_BY
  13. ) {
  14. return Math.min(offset, 1);
  15. }
  16. return -1;
  17. }
  18. position(index: number, inclusive?: boolean): [Node, number] {
  19. let offset = [].indexOf.call(this.parent.domNode.childNodes, this.domNode);
  20. if (index > 0) offset += 1;
  21. return [this.parent.domNode, offset];
  22. }
  23. value(): any {
  24. return { [this.statics.blotName]: this.statics.value(this.domNode) || true };
  25. }
  26. }
  27. export default LeafBlot;