owncast/web/components/chat/ChatTextField/ContentEditable.tsx
renovate[bot] 8069ca782f
chore(deps): update dependency @types/react to v18.2.52 (#3479)
* chore(deps): update dependency @types/react to v18.2.52

* fix(chat): missing placeholder prop in interface

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Gabe Kangas <gabek@real-ity.com>
2024-02-06 20:07:31 -08:00

66 lines
1.6 KiB
TypeScript

import * as React from 'react';
export interface ContentEditableProps extends React.HTMLAttributes<HTMLElement> {
onRootRef?: Function;
onContentChange?: Function;
tagName?: string;
html: string;
placeholder: string;
disabled: boolean;
}
export default class ContentEditable extends React.Component<ContentEditableProps> {
private root: HTMLElement;
private mutationObserver: MutationObserver;
private innerHTMLBuffer: string;
public componentDidMount() {
this.mutationObserver = new MutationObserver(this.onContentChange);
this.mutationObserver.observe(this.root, {
childList: true,
subtree: true,
characterData: true,
});
}
private onContentChange = (mutations: MutationRecord[]) => {
mutations.forEach(() => {
const { innerHTML } = this.root;
if (this.innerHTMLBuffer === undefined || this.innerHTMLBuffer !== innerHTML) {
this.innerHTMLBuffer = innerHTML;
if (this.props.onContentChange) {
this.props.onContentChange({
target: {
value: innerHTML,
},
});
}
}
});
};
private onRootRef = (elt: HTMLElement) => {
this.root = elt;
if (this.props.onRootRef) {
this.props.onRootRef(this.root);
}
};
public render() {
const { tagName, html, ...newProps } = this.props;
delete newProps.onRootRef;
delete newProps.onContentChange;
return React.createElement(tagName || 'div', {
...newProps,
ref: this.onRootRef,
contentEditable: !this.props.disabled,
dangerouslySetInnerHTML: { __html: html },
});
}
}