Add onCreate in customElementConfig

This commit is contained in:
ad1992 2022-04-19 21:58:38 +05:30
parent 4e75f10b2c
commit ba48aa24a0
4 changed files with 48 additions and 28 deletions

View File

@ -452,6 +452,16 @@ class App extends React.Component<AppProps, AppState> {
width, width,
height, height,
}); });
this.scene.addCallback(() => {
const customElementConfig = getCustomElementConfig(
this.props.customElementsConfig,
customElement.customType,
);
if (customElementConfig && customElementConfig.onCreate) {
customElementConfig.onCreate(customElement);
}
});
this.scene.replaceAllElements([ this.scene.replaceAllElements([
...this.scene.getElementsIncludingDeleted(), ...this.scene.getElementsIncludingDeleted(),
customElement, customElement,

View File

@ -160,6 +160,7 @@ export const DEFAULT_CUSTOM_ELEMENT_CONFIG: Required<CustomElementConfig> = {
width: 40, width: 40,
height: 40, height: 40,
stackedOnTop: false, stackedOnTop: false,
onCreate: () => {},
}; };
export const MQ_MAX_WIDTH_PORTRAIT = 730; export const MQ_MAX_WIDTH_PORTRAIT = 730;
export const MQ_MAX_WIDTH_LANDSCAPE = 1000; export const MQ_MAX_WIDTH_LANDSCAPE = 1000;

View File

@ -194,6 +194,10 @@ export default function App() {
); );
}; };
const onCreate = (element) => {
setTimeout(() => addTextArea(element), 0);
};
const getCustomElementsConfig = () => { const getCustomElementsConfig = () => {
return [ return [
{ {
@ -217,6 +221,7 @@ export default function App() {
</svg>`)}`, </svg>`)}`,
transformHandles: false, transformHandles: false,
stackedOnTop: true, stackedOnTop: true,
onCreate,
}, },
{ {
type: "custom", type: "custom",
@ -244,8 +249,7 @@ export default function App() {
]; ];
}; };
const onElementClick = (element) => { const addTextArea = (element) => {
if (element.type === "custom" && element.customType === "comment") {
const { x: viewPortX, y: viewPortY } = sceneCoordsToViewportCoords( const { x: viewPortX, y: viewPortY } = sceneCoordsToViewportCoords(
{ {
sceneX: element.x, sceneX: element.x,
@ -267,13 +271,17 @@ export default function App() {
fontSize: "13px", fontSize: "13px",
}); });
textarea.placeholder = "Start typing your comments"; textarea.placeholder = "Start typing your comments";
textarea.onblur = () => { textarea.onblur = () => {
textarea.remove(); textarea.remove();
}; };
excalidrawWrapperRef.current excalidrawWrapperRef.current.querySelector(".excalidraw").append(textarea);
.querySelector(".excalidraw")
.append(textarea);
textarea.focus(); textarea.focus();
};
const onElementClick = (element) => {
if (element.type === "custom" && element.customType === "comment") {
addTextArea(element);
} }
}; };
return ( return (

View File

@ -232,6 +232,7 @@ export type CustomElementConfig = {
width?: number; width?: number;
height?: number; height?: number;
stackedOnTop: boolean; stackedOnTop: boolean;
onCreate?: (element: ExcalidrawElement) => void;
}; };
export interface ExcalidrawProps { export interface ExcalidrawProps {
onChange?: ( onChange?: (