json-joy
Version:
Collection of libraries for building collaborative editing apps.
40 lines (39 loc) • 1.34 kB
JavaScript
export class CompositionController {
opts;
composing = false;
data = '';
constructor(opts) {
this.opts = opts;
}
/** -------------------------------------------------- {@link UiLifeCycles} */
start() {
const onStart = (event) => {
this.composing = true;
this.data = event.data;
};
const onUpdate = (event) => {
this.composing = true;
this.data = event.data;
};
const onEnd = (event) => {
this.composing = false;
this.data = '';
const text = event.data;
if (text)
this.opts.et.insert(text);
};
const el = this.opts.source;
el.addEventListener('compositionstart', onStart);
el.addEventListener('compositionupdate', onUpdate);
el.addEventListener('compositionend', onEnd);
return () => {
el.removeEventListener('compositionstart', onStart);
el.removeEventListener('compositionupdate', onUpdate);
el.removeEventListener('compositionend', onEnd);
};
}
/** ----------------------------------------------------- {@link Printable} */
toString(tab) {
return `composition { composing: ${this.composing}, data: ${JSON.stringify(this.data)} }`;
}
}