phantasmal-world/src/ui/quest_editor/RendererComponent.tsx

45 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-07-03 02:56:33 +08:00
import React, { ReactNode, Component } from "react";
import { Area, Quest } from "../../domain";
import { get_quest_renderer } from "../../rendering/QuestRenderer";
type Props = {
quest?: Quest;
area?: Area;
};
2019-07-03 02:56:33 +08:00
export class RendererComponent extends Component<Props> {
private renderer = get_quest_renderer();
2019-07-03 02:56:33 +08:00
render(): ReactNode {
return <div style={{ overflow: "hidden" }} ref={this.modifyDom} />;
}
2019-07-03 02:56:33 +08:00
componentDidMount(): void {
window.addEventListener("resize", this.onResize);
}
2019-07-03 02:56:33 +08:00
componentWillUnmount(): void {
window.removeEventListener("resize", this.onResize);
}
2019-07-03 02:56:33 +08:00
componentWillReceiveProps({ quest, area }: Props): void {
this.renderer.set_quest_and_area(quest, area);
}
2019-07-03 02:56:33 +08:00
shouldComponentUpdate(): boolean {
return false;
}
private modifyDom = (div: HTMLDivElement | null) => {
if (div) {
this.renderer.set_size(div.clientWidth, div.clientHeight);
div.appendChild(this.renderer.dom_element);
}
};
private onResize = () => {
2019-07-02 23:00:24 +08:00
const wrapper_div = this.renderer.dom_element.parentNode as HTMLDivElement;
this.renderer.set_size(wrapper_div.clientWidth, wrapper_div.clientHeight);
};
}