conversation-selection-comment.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { TID_V4_SESSION_PANE, testId } from "@zcode/shared/test-ids";
  2. const MAIN_PANE = testId(TID_V4_SESSION_PANE, "workspace-main");
  3. /** 读取主时间线中目标文字的首尾坐标;表格单元格等整段匹配的文本节点优先。 */
  4. function locateTimelineText(paneTestId: string, targetText: string, scroll: boolean) {
  5. const pane = document.querySelector<HTMLElement>(`[data-testid="${paneTestId}"]`);
  6. if (!pane) throw new Error("缺少主 pane");
  7. const walker = document.createTreeWalker(pane, NodeFilter.SHOW_TEXT);
  8. let found: { node: Node; offset: number } | null = null;
  9. for (let node = walker.nextNode(); node; node = walker.nextNode()) {
  10. const value = node.textContent ?? "";
  11. const offset = value.indexOf(targetText);
  12. if (offset < 0 || !node.parentElement?.closest("[data-row-id]")) continue;
  13. found ??= { node, offset };
  14. if (value.trim() === targetText) {
  15. found = { node, offset };
  16. break;
  17. }
  18. }
  19. if (!found) throw new Error(`主时间线缺少选区文本: ${targetText}`);
  20. if (scroll) found.node.parentElement?.scrollIntoView({ block: "center", behavior: "instant" });
  21. const range = document.createRange();
  22. range.setStart(found.node, found.offset);
  23. range.setEnd(found.node, found.offset + 1);
  24. const first = range.getBoundingClientRect();
  25. range.setStart(found.node, found.offset + targetText.length - 1);
  26. range.setEnd(found.node, found.offset + targetText.length);
  27. const last = range.getBoundingClientRect();
  28. return {
  29. x1: Math.ceil(first.left),
  30. y1: Math.round(first.top + first.height / 2),
  31. x2: Math.floor(last.right),
  32. y2: Math.round(last.top + last.height / 2),
  33. };
  34. }
  35. /** 真实 pointer 拖选主时间线文字,并等待选区浮层出现。 */
  36. export async function dragSelectMainTimelineText(text: string): Promise<void> {
  37. // 上一次动作可能保留选区;在旧选区上拖动会变成 HTML 拖放,而不是重新划词。
  38. await browser.execute(() => window.getSelection()?.removeAllRanges());
  39. await browser.execute(locateTimelineText, MAIN_PANE, text, true);
  40. const position = await browser.execute(locateTimelineText, MAIN_PANE, text, false);
  41. await browser.performActions([
  42. {
  43. type: "pointer",
  44. id: "selection-comment-drag",
  45. parameters: { pointerType: "mouse" },
  46. actions: [
  47. { type: "pointerMove", x: position.x1, y: position.y1, duration: 0 },
  48. { type: "pointerDown", button: 0 },
  49. { type: "pointerMove", x: position.x2, y: position.y2, duration: 200 },
  50. { type: "pointerUp", button: 0 },
  51. ],
  52. },
  53. ]);
  54. await browser.releaseActions();
  55. await browser.waitUntil(
  56. () => browser.execute((expected) => window.getSelection()?.toString() === expected, text),
  57. { timeout: 5000, timeoutMsg: `真实拖选未选中目标文本: ${text}` },
  58. );
  59. await $("[data-conversation-selection-tooltip]").waitForDisplayed({ timeout: 5000 });
  60. }
  61. /** 浮层可能在 selectionchange/rAF 中重挂载;检查与点击合成一次原子脚本并整体重试。 */
  62. export async function clickSelectionAction(
  63. action: "comment" | "add-to-task" | "ask-in-side-chat",
  64. ): Promise<void> {
  65. await browser.waitUntil(
  66. async () =>
  67. browser.execute((nextAction) => {
  68. const button = document.querySelector<HTMLButtonElement>(
  69. `[data-conversation-selection-action="${nextAction}"]`,
  70. );
  71. if (!button || button.disabled) return false;
  72. button.click();
  73. return true;
  74. }, action),
  75. { timeout: 30000, timeoutMsg: `划词动作不可用: ${action}` },
  76. );
  77. }
  78. /** 在批注浮层里用真实键盘输入批注。 */
  79. export async function typeSelectionComment(comment: string): Promise<void> {
  80. const input = await $("[data-conversation-selection-comment-input]");
  81. await input.waitForDisplayed({ timeout: 5000 });
  82. await browser.waitUntil(
  83. () =>
  84. browser.execute(
  85. () =>
  86. document.activeElement?.hasAttribute("data-conversation-selection-comment-input") ===
  87. true,
  88. ),
  89. { timeout: 5000, timeoutMsg: "批注输入框没有自动获得焦点" },
  90. );
  91. await browser.keys(comment.split(""));
  92. await browser.waitUntil(async () => (await input.getValue()) === comment, {
  93. timeout: 5000,
  94. timeoutMsg: "批注输入在键入过程中被清空或浮层被关闭",
  95. });
  96. }
  97. /** 主 composer 的引用 chip 状态:引用条数与 aria-label(含批注数)。 */
  98. export async function readMainComposerSelectionChip(): Promise<{
  99. count: number | null;
  100. label: string | null;
  101. }> {
  102. return browser.execute((paneTestId) => {
  103. const pane = document.querySelector<HTMLElement>(`[data-testid="${paneTestId}"]`);
  104. const chips = Array.from(
  105. pane?.querySelectorAll<HTMLElement>("[data-conversation-selection-reference-count]") ?? [],
  106. ).filter((chip) => !chip.closest("[data-row-id]"));
  107. const chip = chips.at(-1);
  108. if (!chip) return { count: null, label: null };
  109. return {
  110. count: Number(chip.dataset.conversationSelectionReferenceCount),
  111. label: chip.getAttribute("aria-label"),
  112. };
  113. }, MAIN_PANE);
  114. }
  115. /** 主时间线正文里的注释编号标记(含已发送的灰色标记),按编号排序。 */
  116. export async function readMainTimelineAnnotationMarks(): Promise<
  117. { number: number; variant: string }[]
  118. > {
  119. return browser.execute((paneTestId) => {
  120. const pane = document.querySelector<HTMLElement>(`[data-testid="${paneTestId}"]`);
  121. return Array.from(
  122. pane?.querySelectorAll<HTMLElement>(
  123. "[data-conversation-annotation-marks] [data-annotation-number-variant]",
  124. ) ?? [],
  125. )
  126. .map((mark) => ({
  127. number: Number(mark.textContent),
  128. variant: mark.dataset.annotationNumberVariant ?? "",
  129. }))
  130. .sort((a, b) => a.number - b.number || a.variant.localeCompare(b.variant));
  131. }, MAIN_PANE);
  132. }
  133. /** 点正文里第 n 号注释气泡,等待编辑浮层出现。 */
  134. export async function openMainTimelineAnnotation(number: number): Promise<void> {
  135. await browser.waitUntil(
  136. async () =>
  137. browser.execute(
  138. (paneTestId, target) => {
  139. const pane = document.querySelector<HTMLElement>(`[data-testid="${paneTestId}"]`);
  140. const button = pane?.querySelector<HTMLButtonElement>(
  141. `[data-annotation-mark-number="${target}"]`,
  142. );
  143. if (!button) return false;
  144. button.click();
  145. return true;
  146. },
  147. MAIN_PANE,
  148. number,
  149. ),
  150. { timeout: 10000, timeoutMsg: `正文里没有第 ${number} 号注释标记` },
  151. );
  152. await $('[data-conversation-selection-comment-mode="edit"]').waitForDisplayed({ timeout: 5000 });
  153. }