960c41577fad6a09d6cf01d4a338ff02716bc49d564c2500db9b2bfe899d49faaaa5a9e5a87815ecc5c68f91602dae21be9fb9435ed568c9665af8999c5b66 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import Scope from '../../scope.js';
  2. import type { Leaf } from './blot.js';
  3. import ShadowBlot from './shadow.js';
  4. class LeafBlot extends ShadowBlot implements Leaf {
  5. public static scope = Scope.INLINE_BLOT;
  6. /**
  7. * Returns the value represented by domNode if it is this Blot's type
  8. * No checking that domNode can represent this Blot type is required so
  9. * applications needing it should check externally before calling.
  10. */
  11. public static value(_domNode: Node): any {
  12. return true;
  13. }
  14. /**
  15. * Given location represented by node and offset from DOM Selection Range,
  16. * return index to that location.
  17. */
  18. public index(node: Node, offset: number): number {
  19. if (
  20. this.domNode === node ||
  21. this.domNode.compareDocumentPosition(node) &
  22. Node.DOCUMENT_POSITION_CONTAINED_BY
  23. ) {
  24. return Math.min(offset, 1);
  25. }
  26. return -1;
  27. }
  28. /**
  29. * Given index to location within blot, return node and offset representing
  30. * that location, consumable by DOM Selection Range
  31. */
  32. public position(index: number, _inclusive?: boolean): [Node, number] {
  33. const childNodes: Node[] = Array.from(this.parent.domNode.childNodes);
  34. let offset = childNodes.indexOf(this.domNode);
  35. if (index > 0) {
  36. offset += 1;
  37. }
  38. return [this.parent.domNode, offset];
  39. }
  40. /**
  41. * Return value represented by this blot
  42. * Should not change without interaction from API or
  43. * user change detectable by update()
  44. */
  45. public value(): any {
  46. return {
  47. [this.statics.blotName]: this.statics.value(this.domNode) || true,
  48. };
  49. }
  50. }
  51. export default LeafBlot;