mirror of
https://github.com/manyfold3d/manyfold.git
synced 2026-10-22 03:32:18 -05:00
43 lines
958 B
TypeScript
43 lines
958 B
TypeScript
import { Controller } from '@hotwired/stimulus'
|
|
|
|
// Connects to data-controller="editable"
|
|
export default class extends Controller {
|
|
input (): HTMLInputElement {
|
|
return this.element as HTMLInputElement
|
|
}
|
|
|
|
field (): string {
|
|
return this.input().dataset.editableField ?? ''
|
|
}
|
|
|
|
path (): string {
|
|
return this.input().dataset.editablePath ?? ''
|
|
}
|
|
|
|
value (): string {
|
|
return this.input().innerText?.trim()
|
|
}
|
|
|
|
initialText: string | null = null
|
|
|
|
onFocus (): void {
|
|
this.initialText = this.value()
|
|
}
|
|
|
|
onBlur (): void {
|
|
if (this.initialText !== this.value()) {
|
|
const data = new FormData()
|
|
data.append(this.field(), this.value())
|
|
data.append('authenticity_token', document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') ?? '')
|
|
void fetch(
|
|
this.path(),
|
|
{
|
|
method: 'PATCH',
|
|
redirect: 'manual',
|
|
body: data
|
|
}
|
|
)
|
|
}
|
|
}
|
|
}
|