presentationTextSelection.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // @vitest-environment jsdom
  2. import { afterEach, describe, expect, it, vi } from "vitest";
  3. import { getPresentationElementSelectedText } from "@/presentation/presentationTextSelection.js";
  4. function domRect(
  5. left: number,
  6. top: number,
  7. width: number,
  8. height: number,
  9. ): DOMRect {
  10. return {
  11. x: left,
  12. y: top,
  13. left,
  14. top,
  15. right: left + width,
  16. bottom: top + height,
  17. width,
  18. height,
  19. toJSON: () => ({}),
  20. };
  21. }
  22. function appendText(surface: HTMLElement, text: string, left: number) {
  23. const span = document.createElement("span");
  24. span.dataset.left = String(left);
  25. span.textContent = text;
  26. surface.append(span);
  27. return span.firstChild as Text;
  28. }
  29. function installRangeRects() {
  30. Object.defineProperty(Range.prototype, "getClientRects", {
  31. configurable: true,
  32. value(this: Range) {
  33. const parent = this.startContainer.parentElement;
  34. return parent ? [domRect(Number(parent.dataset.left), 120, 160, 20)] : [];
  35. },
  36. });
  37. }
  38. afterEach(() => {
  39. vi.restoreAllMocks();
  40. window.getSelection()?.removeAllRanges();
  41. document.body.replaceChildren();
  42. });
  43. describe("getPresentationElementSelectedText", () => {
  44. it("跨多个元素划选时只返回当前高亮元素内部的文本", () => {
  45. installRangeRects();
  46. const surface = document.createElement("div");
  47. document.body.append(surface);
  48. vi.spyOn(surface, "getBoundingClientRect").mockReturnValue(
  49. domRect(0, 0, 1_000, 500),
  50. );
  51. const before = appendText(surface, "Before element", 40);
  52. appendText(surface, "Current selected words", 340);
  53. appendText(surface, "Overlapping foreign text", 400);
  54. const after = appendText(surface, "After element", 760);
  55. const selection = window.getSelection();
  56. const range = document.createRange();
  57. range.setStart(before, 7);
  58. range.setEnd(after, 5);
  59. selection?.removeAllRanges();
  60. selection?.addRange(range);
  61. expect(
  62. getPresentationElementSelectedText({
  63. selection: selection ?? null,
  64. renderSurface: surface,
  65. pageSize: { width: 1_000, height: 500 },
  66. elementBounds: { x: 300, y: 100, width: 300, height: 100 },
  67. elementText: "Current selected words",
  68. }),
  69. ).toBe("Current selected words");
  70. });
  71. it("从当前元素划选到其它元素时保留当前元素内的部分文本", () => {
  72. installRangeRects();
  73. const surface = document.createElement("div");
  74. document.body.append(surface);
  75. vi.spyOn(surface, "getBoundingClientRect").mockReturnValue(
  76. domRect(0, 0, 1_000, 500),
  77. );
  78. const current = appendText(surface, "Current selected words", 340);
  79. const after = appendText(surface, "After element", 760);
  80. const selection = window.getSelection();
  81. const range = document.createRange();
  82. range.setStart(current, 8);
  83. range.setEnd(after, 5);
  84. selection?.removeAllRanges();
  85. selection?.addRange(range);
  86. expect(
  87. getPresentationElementSelectedText({
  88. selection: selection ?? null,
  89. renderSurface: surface,
  90. pageSize: { width: 1_000, height: 500 },
  91. elementBounds: { x: 300, y: 100, width: 300, height: 100 },
  92. elementText: "Current selected words",
  93. }),
  94. ).toBe("selected words");
  95. });
  96. it("选区折叠或完全位于其它元素时返回 null", () => {
  97. installRangeRects();
  98. const surface = document.createElement("div");
  99. document.body.append(surface);
  100. vi.spyOn(surface, "getBoundingClientRect").mockReturnValue(
  101. domRect(0, 0, 1_000, 500),
  102. );
  103. const outside = appendText(surface, "Outside", 40);
  104. const selection = window.getSelection();
  105. const range = document.createRange();
  106. range.selectNodeContents(outside);
  107. selection?.removeAllRanges();
  108. selection?.addRange(range);
  109. expect(
  110. getPresentationElementSelectedText({
  111. selection: selection ?? null,
  112. renderSurface: surface,
  113. pageSize: { width: 1_000, height: 500 },
  114. elementBounds: { x: 300, y: 100, width: 300, height: 100 },
  115. elementText: "Current selected words",
  116. }),
  117. ).toBeNull();
  118. selection?.collapse(outside, 2);
  119. expect(
  120. getPresentationElementSelectedText({
  121. selection: selection ?? null,
  122. renderSurface: surface,
  123. pageSize: { width: 1_000, height: 500 },
  124. elementBounds: { x: 300, y: 100, width: 300, height: 100 },
  125. elementText: "Current selected words",
  126. }),
  127. ).toBeNull();
  128. });
  129. });