1
0
Fork 0

Convert CodeJarEditor to not use any legacy compat preact code.

This commit is contained in:
Kitteh 2021-05-25 18:35:08 +01:00
parent ffa9eecb9a
commit 69d8d836ce
2 changed files with 37 additions and 41 deletions

View file

@ -1,53 +1,49 @@
import { CodeJar } from "codejar";
import { JSX } from "preact";
import { Ref, useEffect, useRef } from "preact/compat";
import { Component, JSX, createRef } from "preact";
import { CodeJar as _CodeJar } from "codejar";
interface EditorProps {
highlight: unknown;
options?: { tab: string };
interface CodeJarProps {
language: string;
tabSize: number;
code: string;
onUpdate: (code: string) => void;
}
export const useCodeJar = (props: EditorProps): Ref<HTMLDivElement> => {
const editorRef = useRef<HTMLDivElement>(null);
const jar = useRef<CodeJar | null>(null);
export class CodeJarEditor extends Component<CodeJarProps, unknown> {
editorRef = createRef<HTMLDivElement>();
jar = createRef<_CodeJar | null>();
useEffect(() => {
if (!editorRef.current) return;
jar.current = CodeJar(
editorRef.current,
props.highlight as (e: HTMLElement, pos?: unknown) => void,
{ ...props.options, window: window },
);
jar.current.updateCode(props.code);
jar.current.onUpdate((txt) => {
if (!editorRef.current) return;
props.onUpdate(txt);
componentDidMount(): void {
this.jar.current = _CodeJar(this.editorRef.current, () => {}, {
tab: " ".repeat(this.props.tabSize),
window: window,
});
return () => jar.current.destroy();
}, []);
this.jar.current.updateCode(this.props.code);
useEffect(() => {
if (!jar.current || !editorRef.current) return;
jar.current.updateCode(props.code);
}, [props.code]);
useEffect(() => {
if (!jar.current || !props.options) return;
jar.current.updateOptions(props.options);
}, [props.options]);
return editorRef;
};
export function CodeJarEditor(props: EditorProps): JSX.Element {
const editorRef = useCodeJar(props);
return <div class="editor language-json" ref={editorRef}></div>;
this.jar.current.onUpdate((txt) => {
this.props.onUpdate(txt);
});
}
componentWillUnmount(): void {
if (this.jar.current) {
this.jar.current.destroy();
}
}
componentDidUpdate(prevProps: CodeJarProps): void {
if (!this.jar.current) return;
if (
prevProps.code !== this.props.code ||
prevProps.tabSize !== this.props.tabSize ||
prevProps.language !== this.props.language
) {
this.componentDidMount();
}
}
render(): JSX.Element {
return <div class={"editor language-" + this.props.language} ref={this.editorRef} />;
}
}

View file

@ -91,10 +91,10 @@ export class KVEditor extends Component<KVEditProps, KVEditState> {
<div>
<p class="uk-text-danger" id="errorText" />
<CodeJarEditor
highlight={() => {}}
language="json"
tabSize={4}
code={this.getStringKVData(this.state.kvData)}
onUpdate={(code) => this.onCodeUpdate(code)}
options={{ tab: " ".repeat(4) }}
/>
<button class="uk-button uk-button-primary" onClick={() => this.editorSave()}>
{i18next.t("kv_sec_edit_btn")}