Skip to content
Comvi

In-context editor

View .md

With an i18n instance from the Quick start, a Comvi project from Connect to Comvi and an API key, let a translator hold Alt and click any rendered text to edit it in the browser. @comvi/plugin-in-context-editor injects invisible key markers into t() output and watches the DOM for them. Saves go to the Comvi platform over its API, so it needs a project and an API key; it has no offline mode.

Terminal window
npm install @comvi/plugin-in-context-editor
src/i18n.ts
import { createI18n } from '@comvi/core';
import { cdnLoader } from '@comvi/plugin-fetch-loader';
import { inContextEditor } from '@comvi/plugin-in-context-editor';
export const i18n = createI18n({
locale: 'en',
apiKey: import.meta.env.DEV ? import.meta.env.VITE_COMVI_EDITOR_API_KEY : undefined,
})
.use(cdnLoader({ cdnUrl: import.meta.env.VITE_COMVI_CDN_URL }))
.use(inContextEditor());
await i18n.init();

Create an API key in the project’s settings with project read plus translation read and write; read alone loads the editor but every save fails. The editor reads the key off i18n.apiKey, the same option the fetch loader reads for API mode. Compose a loader or an inline messages catalog next to it: the editor can only edit text the app rendered through t().

An API key in a browser bundle is readable by every visitor, and this one can overwrite translations, so the editor key must never be in a production build or a public preview build. The import.meta.env.DEV test is what keeps it out: Vite replaces it with false in a build, and the bundler drops the key with the dead branch.

inContextEditor() takes the i18n instance, composes the devtools and plugin capabilities onto it, queues inContextEditorPlugin() and returns the instance with its type unchanged. The plugin runs inside init(): on the server it registers only the marker post-processor, so server-rendered HTML carries the markers the client expects; in the browser it also starts the DOM observer and the UI. Compose it before init().

Do this And
Hold Alt (Option on macOS) and hover translated elements light up with an overlay naming the key
Alt + click a highlighted element the edit modal opens on that key
Cmd or Ctrl + S in the modal saves without closing
Escape closes the modal, or asks about unsaved edits

Nothing highlights on hover alone. Release the modifier, or switch windows, and the overlay disappears. With no key configured the plugin starts in demo mode: the console says [@comvi/plugin-in-context-editor] Running in demo mode - API key not configured. Changes cannot be saved. once, and every click opens one panel naming the key, with no translation list.

src/i18n.ts
import { createI18n } from '@comvi/core';
import { devtools } from '@comvi/core/devtools';
import { cdnLoader } from '@comvi/plugin-fetch-loader';
import { inContextEditor } from '@comvi/plugin-in-context-editor';
const editorEnabled = import.meta.env.DEV;
const base = createI18n({
locale: 'en',
fallbackLocale: 'en',
apiKey: editorEnabled ? import.meta.env.VITE_COMVI_EDITOR_API_KEY : undefined,
}).use(cdnLoader({ cdnUrl: import.meta.env.VITE_COMVI_CDN_URL }));
export const i18n = (editorEnabled ? base.use(inContextEditor()) : base).use(devtools());
await i18n.init();

Gating the key is required. Two things keep the editor code out of a production build: the package’s production export condition resolves to an entry whose installer is an identity function, with no editor bytes, and inContextEditorPlugin() returns a no-op when process.env.NODE_ENV is "production". Neither touches your own createI18n call, so an ungated apiKey still ships in dist/assets, where anyone can read it and overwrite translations with it. Put the key and the installer behind the same condition, and never widen that condition with a flag a deployed build can set: a preview deployment is a production build, so the editor would not run there and the key would still ship. Both branches of the ternary have the same type. .use(devtools()) sits outside the condition: it is what the browser extension needs to edit the production build. It carries no editor code and no key; it does add the discovery and loader code to the bundle and publishes the instance on window.__COMVI__ for every visitor, so leave it out where the extension should not reach production.

Option Default Behaviour
targetElement document.body The DOM subtree to watch.
tagAttributes built-in map HTML tag name to the attributes treated as translated values. Replaces the built-in map.
debug false Verbose console logging.
highlightStyle { borderColor: '#D97706', backgroundColor: 'rgba(217, 119, 6, 0.12)', borderWidth: 2 } The overlay. Its z-index: 10000 is fixed.
collectContext true Passive context collection while the editor runs.
screenGroupResolver none () => '/users/:id', a PII-free route template shown instead of a URL digest; return null to keep the digest.
apiKeyOverride none Overrides i18n.apiKey for this plugin only.

Context collection sends the keys it sees, layout hints and screen groups to your project to improve suggestions. The plugin option reaches the editor you composed; the collectContext: false option on createI18n outranks it and also covers the extension. Collection is off in demo mode.

Edit a production build with the Comvi browser extension, with no editor code and no API key in the bundle. Compose devtools() on the i18n instance and, if the site sends a Content Security Policy, allow the editor’s CDN in script-src.

src/i18n.ts
import { createI18n } from '@comvi/core';
import { devtools } from '@comvi/core/devtools';
import { cdnLoader } from '@comvi/plugin-fetch-loader';
export const i18n = createI18n({ locale: 'en' })
.use(cdnLoader({ cdnUrl: import.meta.env.VITE_COMVI_CDN_URL }))
.use(devtools({ instanceId: 'checkout' }));
Content-Security-Policy
script-src 'self' https://cdn.jsdelivr.net

devtools() publishes the instance on window.__COMVI__, which is how the extension finds it, and installs a loader the extension can call. The extension carries no editor. When a user with the extension presses Enable editor, the extension calls that loader, and the site’s SDK inserts one <script> for https://cdn.jsdelivr.net/npm/@comvi/plugin-in-context-editor@latest/dist/standalone.iife.js. Nothing is requested from the CDN before that click, and a visitor without the extension loads nothing. The API key stays in the extension: every request the editor makes goes through it. Extension 0.6.0 edits a page on @comvi/core 0.6.0 or newer, and on an older SDK its popup says it cannot load the editor. Upgrade the site first: a 0.5.0 extension activates the editor it still bundles on a 0.6.0 page, so that pairing keeps working while translators update.

A base instance publishes nothing, so an app that never composes devtools() is invisible to the extension. inContextEditor() composes devtools() for you in development, but the package’s production entry is an identity function, so compose devtools() yourself, outside any development condition, as in Gate it out of production. On Next, add .use(devtools()) to the result createNextI18n returns, with devtools from @comvi/next. The <script> global build of @comvi/core is the one composition that installs discovery and the loader on its own, always on latest, and devtools({ exposeGlobal: false }) installs neither. Discovery is removed on destroy(), and the loader stays.

devtools({ editorVersion: '0.6.0' }) requests that exact editor version instead of latest, and the first instance on a page decides. The pin takes an exact version and nothing else: any other value loads no editor at all, the extension’s popup says the pin must look like 1.2.3, and a development build warns. It never quietly falls back to latest, so a typo is visible rather than served. In development inContextEditor() has already composed discovery, so options on a later devtools() configure nothing there; to set instanceId or editorVersion for both builds, compose devtools({ … }) on base, before the condition.

On a page that sets require-trusted-types-for 'script', the site needs two more things: a default policy whose createScriptURL accepts the script URL, because the loader assigns script.src as a plain string, and vue in its trusted-types directive, the policy the editor’s Vue runtime creates for the HTML it renders. Without vue the editor loads but its UI is blocked. style-src needs no change in current browsers, because the editor adopts a constructable stylesheet. The extension’s messages for a site that cannot supply the editor are on Troubleshooting.

The loaded script is @comvi/plugin-in-context-editor/standalone, an IIFE that exposes window.ComviInContextEditor with activate(), deactivate(), isActive(), getStatus() and version. Its activate() takes EditorOptions plus apiKey, a transport proxy so the credential stays in the extension, instanceId, refreshMessages and onLifecycle, and returns null with a warning when a session is already running. Activation is announced on window as a credential-free comvi-in-context-editor:lifecycle CustomEvent. An app that bundles the standalone build itself is activated as it is, and nothing is loaded.

src/chart.ts
import { i18n } from './i18n';
export function drawLabel(ctx: CanvasRenderingContext2D, x: number, y: number) {
ctx.fillText(i18n.t('chart.label', { markers: false }), x, y);
}

Pass markers: false when the invisible markers cause a problem for one call: a canvas or a font that draws them as boxes, a width-sensitive layout, a comparison or sort that sees the extra characters. The marker injector is the only reader of the option, so the text is otherwise the same t() result, and that one string is not editable in context. <T> takes the same option as a prop, markers={false} or :markers="false" in Vue. The plugin returns a cleanup function, so destroy() unhooks everything it registered.

  • Overview: installers, plugin factories and registration options.
  • Fetch loader: the loader the editor edits on top of.
  • Devtools API: devtools(), instanceId, exposeGlobal, editorVersion and the editor loader.