/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ import { html, nothing } from "chrome://global/content/vendor/lit.all.mjs"; import { MozLitElement } from "chrome://global/content/lit-utils.mjs"; // eslint-disable-next-line import-x/no-unassigned-import import "chrome://global/content/elements/moz-button.mjs"; // eslint-disable-next-line import-x/no-unassigned-import import "chrome://global/content/elements/moz-promo.mjs"; // eslint-disable-next-line import-x/no-unassigned-import import "chrome://browser/content/aiwindow/components/smartwindow-chip.mjs"; const TIP_PREF = "browser.widgets.generated.promptTip.seen"; const { XPCOMUtils } = ChromeUtils.importESModule( "resource://gre/modules/XPCOMUtils.sys.mjs" ); const lazy = {}; XPCOMUtils.defineLazyPreferenceGetter(lazy, "promptTipSeen", TIP_PREF, false); const EXAMPLE_IDS = [ "widget-composer-example-plants", "widget-composer-example-fitness", "widget-composer-example-trivia", "widget-composer-example-books", "widget-composer-example-recipe", "widget-composer-example-packing", "widget-composer-example-word", ]; export const SUBMIT_ICON = "chrome://browser/content/aiwindow/assets/ask-icon.svg"; const WIDGET_ICON = "chrome://browser/skin/topsites.svg"; /** * A prompt for creating widgets. * * @property {string} value - The text of the prompt. * @property {boolean} submitDisabled - Whether submitting the prompt is disabled. * @property {?{id: string, name: string}} widget - The widget the prompt updates, * shown as a removable chip. * @property {boolean} edited - Whether the user typed the prompt or picked an * example. */ export class WidgetComposer extends MozLitElement { static properties = { value: { type: String }, submitDisabled: { type: Boolean }, widget: { type: Object }, focused: { type: Boolean, state: true }, showTip: { type: Boolean, state: true }, }; static queries = { textareaEl: "textarea", }; static shadowRootOptions = { ...MozLitElement.shadowRootOptions, delegatesFocus: true, }; #examples = EXAMPLE_IDS.map(id => ({ id, key: Math.random() })) .sort((a, b) => a.key - b.key) .slice(0, 3) .map(({ id }) => id); constructor() { super(); this.value = ""; this.submitDisabled = false; this.widget = null; this.edited = false; this.focused = false; this.showTip = false; this.addEventListener("keydown", event => this.#onHostKeyDown(event)); } #onFocus() { this.focused = true; } #onBlur() { this.focused = false; } #onInput(event) { this.value = event.target.value; this.edited = true; this.#maybeShowTip(); } #maybeShowTip() { if (this.value.trim() && !lazy.promptTipSeen) { Services.prefs.setBoolPref(TIP_PREF, true); this.showTip = true; } } #onExampleClick(event) { let text = event.currentTarget.getAttribute("prompt"); if (!text) { return; } this.value = text; this.edited = true; this.#maybeShowTip(); this.textareaEl.focus(); } #onTipDismissed(event) { event.preventDefault(); this.showTip = false; this.textareaEl.focus(); } #onRemoveWidget() { this.dispatchEvent( new CustomEvent("WidgetComposer:RemoveWidget", { bubbles: true }) ); this.textareaEl.focus(); } #onHostKeyDown(event) { if (event.key === "Escape" && !event.isComposing) { event.preventDefault(); this.dispatchEvent( new CustomEvent("WidgetComposer:Dismiss", { bubbles: true }) ); } } #onKeyDown(event) { if (event.key === "Enter" && !event.shiftKey && !event.isComposing) { event.preventDefault(); this.#submit(); } } #submit() { let text = this.value.trim(); if (!text || this.submitDisabled) { return; } this.dispatchEvent( new CustomEvent("WidgetComposer:Submit", { bubbles: true, detail: { prompt: text, widgetId: this.widget?.id ?? null }, }) ); } #renderChip() { if (!this.widget) { return nothing; } return html`
`; } render() { let hasText = !!this.value.trim(); let canSubmit = hasText && !this.submitDisabled; let concealed = this.value !== "" || this.showTip || !!this.widget; return html`
${this.showTip ? html`

` : nothing}

${this.#examples.map( id => html`` )}
${this.#renderChip()}

`; } } customElements.define("widget-composer", WidgetComposer);