summaryrefslogtreecommitdiff
path: root/src/background/menu-translation.js
blob: 1465c5369fbf5e07a9b00d07086fa33a556d4cbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
 * Context-menu translation entry (background).
 *
 * Receives the raw `menus.OnClickData.selectionText`, validates it through
 * the shared pipeline and starts a translation without a sink. The popup
 * observes results via the shared translation state (GET_POPUP_STATE).
 */

import { validateSelectionText, runTranslation, messageForCode } from './translation-service.js';
import { createRequestId } from '../shared/messages.js';
import { ERROR_CODES } from '../shared/constants.js';

/**
 * Start a translation from a context-menu selection.
 * @param {*} selectionText raw value from menus.OnClickData
 * @returns {{ok: boolean, requestId?: string, error?: string, message?: string}}
 */
export function startFromSelection(selectionText) {
  const validated = validateSelectionText(selectionText);
  if (!validated.ok) {
    return { ok: false, error: validated.code, message: messageForCode(validated.code) };
  }
  const requestId = createRequestId();
  // No sink: the popup polls GET_POPUP_STATE and receives streaming updates
  // through the shared translation state.
  void runTranslation({ text: validated.text, requestId, source: 'context-menu', sink: null });
  return { ok: true, requestId };
}