msc-ai-assistant
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -21,2 +21,4 @@ import { _wcl } from './common-lib.js'; | ||
- DOMPurify: https://github.com/cure53/DOMPurify | ||
- HTMLInputElement: selectionEnd property: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/selectionEnd | ||
- https://stackoverflow.com/questions/48448718/insert-a-new-line-after-current-position-in-a-textarea | ||
*/ | ||
@@ -717,3 +719,6 @@ | ||
session: '', | ||
sessionController: '' | ||
sessionController: '', | ||
histories: [], | ||
currentHistoryIndex: 0, | ||
inputed: false | ||
}; | ||
@@ -729,2 +734,3 @@ | ||
form: this.shadowRoot.querySelector('.ai-assistant__body__form'), | ||
btnSubmit: this.shadowRoot.querySelector('.ai-assistant__body__form__submit'), | ||
textarea: this.shadowRoot.querySelector('.ai-assistant__body__form__textarea'), | ||
@@ -742,6 +748,9 @@ autoscroll: this.shadowRoot.querySelector('.ai-assistant__body_autoscroll') | ||
this._onSubmit = this._onSubmit.bind(this); | ||
this._onKeydown = this._onKeydown.bind(this); | ||
this._onInput = this._onInput.bind(this); | ||
} | ||
async connectedCallback() { | ||
const { config, error } = await _wcl.getWCConfig(this); | ||
const { config, error } = await _wcl.getWCConfig(this); | ||
const { textarea, form } = this.#nodes; | ||
@@ -770,3 +779,10 @@ if (error) { | ||
const signal = this.#data.controller.signal; | ||
this.#nodes.form.addEventListener('submit', this._onSubmit, { signal }); | ||
form.addEventListener('submit', this._onSubmit, { signal }); | ||
textarea.addEventListener('input', this._onInput, { signal }); | ||
// apply 「shift」+ 「Enter」for line break. (desktop only) | ||
const mql = window.matchMedia('(hover: hover)'); | ||
if (mql.matches) { | ||
textarea.addEventListener('keydown', this._onKeydown, { signal, capture: true }); | ||
} | ||
} | ||
@@ -968,2 +984,55 @@ | ||
_onKeydownBK(evt) { | ||
const { key, shiftKey } = evt; | ||
const { btnSubmit } = this.#nodes; | ||
if (key !== 'Enter') { | ||
return; | ||
} | ||
if (!shiftKey) { | ||
evt.preventDefault(); | ||
btnSubmit.click(); | ||
} | ||
} | ||
_onInput() { | ||
const { textarea } = this.#nodes; | ||
this.#data.inputed = !!textarea.value.length; | ||
} | ||
_onKeydown(evt) { | ||
const { key, shiftKey } = evt; | ||
const { btnSubmit, textarea } = this.#nodes; | ||
switch (key) { | ||
case 'Enter': { | ||
if (!shiftKey) { | ||
evt.preventDefault(); | ||
btnSubmit.click(); | ||
} | ||
break; | ||
} | ||
case 'ArrowUp': | ||
case 'ArrowDown': { | ||
if (this.#data.inputed) { | ||
return; | ||
} | ||
evt.preventDefault(); | ||
const { histories, currentHistoryIndex } = this.#data; | ||
const count = histories.length; | ||
const index = (currentHistoryIndex + (key === 'ArrowUp' ? -1 : 1) + count) % count; | ||
this.#data.currentHistoryIndex = index; | ||
textarea.value = histories[index]; | ||
textarea.selectionEnd = histories[index].length; | ||
break; | ||
} | ||
} | ||
} | ||
async _onSubmit(evt) { | ||
@@ -1000,3 +1069,3 @@ const { form, results, textarea, autoscroll } = this.#nodes; | ||
const newChunk = chunk.startsWith(previousChunk) | ||
? chunk.slice(previousChunk.length) : chunk; | ||
? chunk.slice(previousChunk.length) : chunk; | ||
@@ -1020,3 +1089,14 @@ result += newChunk; | ||
// update histories | ||
const index = this.#data.histories.findIndex((value) => value === prompts); | ||
if (index !== -1) { | ||
this.#data.histories.splice(index, 1); | ||
} | ||
this.#data.histories.push(prompts); | ||
this.#data.currentHistoryIndex = this.#data.histories.length; | ||
this.#data.inputed = false; | ||
form.inert = false; | ||
textarea.focus(); | ||
} | ||
@@ -1023,0 +1103,0 @@ |
{ | ||
"name": "msc-ai-assistant", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "<msc-ai-assistant /> is a web component based on Chrome Built-in AI Prompt API. Web developers could use <msc-ai-assistant /> to help user consult anything they like to know.", | ||
@@ -5,0 +5,0 @@ "main": "index.html", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
121710
2622