@corti/dictation-web
Version:
Web component for Corti Dictation
291 lines • 14.7 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _DictationRoot_languagesController, _DictationRoot_devicesController, _DictationRoot_handleLanguageChanged, _DictationRoot_handleDeviceChanged, _DictationRoot_handleRecordingStateChanged, _DictationRoot_handleContextRequest, _DictationRoot_handleKeybindingChanged;
import { createContext, provide } from "@lit/context";
import { html, LitElement } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { DevicesController } from "../controllers/devices-controller.js";
import { LanguagesController } from "../controllers/languages-controller.js";
import ComponentStyles from "../styles/component-styles.js";
import { getInitialToken } from "../utils/auth.js";
import { commaSeparatedConverter } from "../utils/converters.js";
import { errorEvent, keybindingChangedEvent, } from "../utils/events.js";
import { decodeToken } from "../utils/token.js";
export const regionContext = createContext(Symbol("region"));
export const tenantNameContext = createContext(Symbol("tenantName"));
export const languagesContext = createContext(Symbol("languages"));
export const devicesContext = createContext(Symbol("devices"));
export const selectedDeviceContext = createContext(Symbol("selectedDevice"));
export const recordingStateContext = createContext(Symbol("recordingState"));
export const accessTokenContext = createContext(Symbol("accessToken"));
export const dictationConfigContext = createContext(Symbol("dictationConfig"));
export const authConfigContext = createContext(Symbol("authConfig"));
export const socketUrlContext = createContext(Symbol("socketUrl"));
export const socketProxyContext = createContext(Symbol("socketProxy"));
export const debugDisplayAudioContext = createContext(Symbol("debugDisplayAudio"));
export const pushToTalkKeybindingContext = createContext(Symbol("pushToTalkKeybinding"));
export const toggleToTalkKeybindingContext = createContext(Symbol("toggleToTalkKeybinding"));
let DictationRoot = class DictationRoot extends LitElement {
set accessToken(token) {
this.setAccessToken(token);
}
get accessToken() {
return this._accessToken;
}
set authConfig(config) {
this.setAuthConfig(config);
}
get authConfig() {
return this._authConfig;
}
set languages(value) {
this._languages = value;
// Clear auto-loaded flag when languages are set via property
if (value !== undefined) {
__classPrivateFieldGet(this, _DictationRoot_languagesController, "f").clearAutoLoadedFlag();
}
}
get languages() {
return this._languages;
}
set devices(value) {
this._devices = value;
// Clear auto-loaded flag when devices are set via property
if (value !== undefined) {
__classPrivateFieldGet(this, _DictationRoot_devicesController, "f").clearAutoLoadedFlag();
}
}
get devices() {
return this._devices;
}
// ─────────────────────────────────────────────────────────────────────────────
// Lifecycle
// ─────────────────────────────────────────────────────────────────────────────
constructor() {
super();
this.recordingState = "stopped";
_DictationRoot_languagesController.set(this, new LanguagesController(this));
_DictationRoot_devicesController.set(this, new DevicesController(this));
this.noWrapper = false;
// ─────────────────────────────────────────────────────────────────────────────
// Private event handlers
// ─────────────────────────────────────────────────────────────────────────────
_DictationRoot_handleLanguageChanged.set(this, (e) => {
const event = e;
this.dictationConfig = {
...this.dictationConfig,
primaryLanguage: event.detail.selectedLanguage,
};
});
_DictationRoot_handleDeviceChanged.set(this, (e) => {
const event = e;
this.selectedDevice = event.detail.selectedDevice;
});
_DictationRoot_handleRecordingStateChanged.set(this, (e) => {
const event = e;
this.recordingState = event.detail.state;
});
_DictationRoot_handleContextRequest.set(this, (e) => {
if (e.context === languagesContext) {
__classPrivateFieldGet(this, _DictationRoot_languagesController, "f").initialize();
}
else if (e.context === devicesContext) {
__classPrivateFieldGet(this, _DictationRoot_devicesController, "f").initialize();
}
else if (e.contextTarget.tagName.toLowerCase() === "dictation-keybinding-selector") {
if (e.context === pushToTalkKeybindingContext &&
this.pushToTalkKeybinding === undefined) {
this.pushToTalkKeybinding = "Space";
this.dispatchEvent(keybindingChangedEvent(" ", "Space", "Space", "push-to-talk"));
}
if (e.context === toggleToTalkKeybindingContext &&
this.toggleToTalkKeybinding === undefined) {
this.toggleToTalkKeybinding = "Enter";
this.dispatchEvent(keybindingChangedEvent("Enter", "Enter", "Enter", "toggle-to-talk"));
}
}
});
_DictationRoot_handleKeybindingChanged.set(this, (e) => {
const event = e;
const keybinding = event.detail.keybinding;
if (event.detail.type === "push-to-talk") {
this.pushToTalkKeybinding = keybinding;
}
else if (event.detail.type === "toggle-to-talk") {
this.toggleToTalkKeybinding = keybinding;
}
});
this.addEventListener("languages-changed", __classPrivateFieldGet(this, _DictationRoot_handleLanguageChanged, "f"));
this.addEventListener("recording-devices-changed", __classPrivateFieldGet(this, _DictationRoot_handleDeviceChanged, "f"));
this.addEventListener("recording-state-changed", __classPrivateFieldGet(this, _DictationRoot_handleRecordingStateChanged, "f"));
this.addEventListener("context-request", __classPrivateFieldGet(this, _DictationRoot_handleContextRequest, "f"));
this.addEventListener("keybinding-changed", __classPrivateFieldGet(this, _DictationRoot_handleKeybindingChanged, "f"));
}
// ─────────────────────────────────────────────────────────────────────────────
// Public methods
// ─────────────────────────────────────────────────────────────────────────────
/**
* Sets the access token and parses region/tenant from it.
* @returns ServerConfig with environment, tenant, and accessToken
* @deprecated Use 'accessToken' property instead.
*/
setAccessToken(token) {
this._accessToken = token;
this.region = undefined;
this.tenantName = undefined;
if (!token) {
return { accessToken: token, environment: undefined, tenant: undefined };
}
try {
const decoded = decodeToken(token);
this.region = decoded?.environment;
this.tenantName = decoded?.tenant;
return {
accessToken: token,
environment: decoded?.environment,
tenant: decoded?.tenant,
};
}
catch (error) {
this.dispatchEvent(errorEvent(error));
}
return { accessToken: token, environment: undefined, tenant: undefined };
}
/**
* Sets the auth config and parses region/tenant from the initial token.
* @returns Promise with ServerConfig containing environment, tenant, and accessToken
* @deprecated Use 'authConfig' property instead.
*/
async setAuthConfig(config) {
this._authConfig = config;
if (!config) {
return {
accessToken: undefined,
environment: undefined,
tenant: undefined,
};
}
try {
const { accessToken } = await getInitialToken(config);
return this.setAccessToken(accessToken);
}
catch (error) {
this.dispatchEvent(errorEvent(error));
}
return {
accessToken: undefined,
environment: undefined,
tenant: undefined,
};
}
// ─────────────────────────────────────────────────────────────────────────────
// Render
// ─────────────────────────────────────────────────────────────────────────────
render() {
if (this.noWrapper) {
return html `<slot></slot>`;
}
return html `<div class="wrapper">
<slot></slot>
</div>`;
}
};
_DictationRoot_languagesController = new WeakMap();
_DictationRoot_devicesController = new WeakMap();
_DictationRoot_handleLanguageChanged = new WeakMap();
_DictationRoot_handleDeviceChanged = new WeakMap();
_DictationRoot_handleRecordingStateChanged = new WeakMap();
_DictationRoot_handleContextRequest = new WeakMap();
_DictationRoot_handleKeybindingChanged = new WeakMap();
// ─────────────────────────────────────────────────────────────────────────────
// Static
// ─────────────────────────────────────────────────────────────────────────────
DictationRoot.styles = [ComponentStyles];
__decorate([
provide({ context: regionContext }),
state()
], DictationRoot.prototype, "region", void 0);
__decorate([
provide({ context: tenantNameContext }),
state()
], DictationRoot.prototype, "tenantName", void 0);
__decorate([
provide({ context: recordingStateContext }),
state()
], DictationRoot.prototype, "recordingState", void 0);
__decorate([
provide({ context: accessTokenContext }),
state()
], DictationRoot.prototype, "_accessToken", void 0);
__decorate([
property({ type: String })
], DictationRoot.prototype, "accessToken", null);
__decorate([
provide({ context: authConfigContext }),
state()
], DictationRoot.prototype, "_authConfig", void 0);
__decorate([
property({ attribute: false, type: Object })
], DictationRoot.prototype, "authConfig", null);
__decorate([
provide({ context: socketUrlContext }),
property({ type: String })
], DictationRoot.prototype, "socketUrl", void 0);
__decorate([
provide({ context: socketProxyContext }),
property({ attribute: false, type: Object })
], DictationRoot.prototype, "socketProxy", void 0);
__decorate([
provide({ context: dictationConfigContext }),
property({ attribute: false, type: Object })
], DictationRoot.prototype, "dictationConfig", void 0);
__decorate([
provide({ context: languagesContext }),
state()
], DictationRoot.prototype, "_languages", void 0);
__decorate([
property({
converter: commaSeparatedConverter,
type: Array,
})
], DictationRoot.prototype, "languages", null);
__decorate([
provide({ context: devicesContext }),
state()
], DictationRoot.prototype, "_devices", void 0);
__decorate([
property({ attribute: false, type: Array })
], DictationRoot.prototype, "devices", null);
__decorate([
provide({ context: selectedDeviceContext }),
property({ attribute: false, type: Object })
], DictationRoot.prototype, "selectedDevice", void 0);
__decorate([
provide({ context: debugDisplayAudioContext }),
property({ attribute: "debug-display-audio", type: Boolean })
], DictationRoot.prototype, "debug_displayAudio", void 0);
__decorate([
provide({ context: pushToTalkKeybindingContext }),
property({ type: String })
], DictationRoot.prototype, "pushToTalkKeybinding", void 0);
__decorate([
provide({ context: toggleToTalkKeybindingContext }),
property({ type: String })
], DictationRoot.prototype, "toggleToTalkKeybinding", void 0);
__decorate([
property({ type: Boolean })
], DictationRoot.prototype, "noWrapper", void 0);
DictationRoot = __decorate([
customElement("dictation-root")
], DictationRoot);
export { DictationRoot };
//# sourceMappingURL=dictation-context.js.map