|
1 | 1 | /* eslint-disable @typescript-eslint/naming-convention */ |
2 | | -import React, { useState, useRef, useMemo, useEffect } from "react"; |
| 2 | +import React, { useState, useRef, useMemo, useEffect, useCallback } from "react"; |
3 | 3 | import ExtensionApiContextProvider, { |
4 | 4 | useExtensionAPI, |
5 | 5 | } from "roamjs-components/components/ExtensionApiContext"; |
@@ -789,48 +789,51 @@ const InsideEditorAndUiContext = ({ |
789 | 789 | // ); |
790 | 790 | // }; |
791 | 791 |
|
792 | | - const createDiscourseNodeShape = async ({ |
793 | | - uid, |
794 | | - nodeText, |
795 | | - nodeType, |
796 | | - content, |
797 | | - }: { |
798 | | - uid: string; |
799 | | - nodeText: string; |
800 | | - nodeType: string; |
801 | | - content: TLExternalContent & { type: "text" }; |
802 | | - }): Promise<void> => { |
803 | | - const { h, w, imageUrl } = await calcCanvasNodeSizeAndImg({ |
804 | | - nodeText, |
| 792 | + const createDiscourseNodeShape = useCallback( |
| 793 | + async ({ |
805 | 794 | uid, |
| 795 | + nodeText, |
806 | 796 | nodeType, |
807 | | - extensionAPI, |
808 | | - }); |
809 | | - |
810 | | - const position = |
811 | | - content.point ?? |
812 | | - (editor.inputs.shiftKey |
813 | | - ? editor.inputs.currentPagePoint |
814 | | - : editor.getViewportPageBounds().center); |
| 797 | + content, |
| 798 | + }: { |
| 799 | + uid: string; |
| 800 | + nodeText: string; |
| 801 | + nodeType: string; |
| 802 | + content: TLExternalContent & { type: "text" }; |
| 803 | + }): Promise<void> => { |
| 804 | + const { h, w, imageUrl } = await calcCanvasNodeSizeAndImg({ |
| 805 | + nodeText, |
| 806 | + uid, |
| 807 | + nodeType, |
| 808 | + extensionAPI, |
| 809 | + }); |
815 | 810 |
|
816 | | - editor.createShapes([ |
817 | | - { |
818 | | - id: createShapeId(), |
819 | | - type: nodeType, |
820 | | - x: position.x - w / 2, |
821 | | - y: position.y - h / 2, |
822 | | - props: { |
823 | | - uid, |
824 | | - title: nodeText, |
825 | | - w, |
826 | | - h, |
827 | | - ...(imageUrl && { imageUrl }), |
828 | | - size: "s", |
829 | | - fontFamily: "sans", |
| 811 | + const position = |
| 812 | + content.point ?? |
| 813 | + (editor.inputs.shiftKey |
| 814 | + ? editor.inputs.currentPagePoint |
| 815 | + : editor.getViewportPageBounds().center); |
| 816 | + |
| 817 | + editor.createShapes([ |
| 818 | + { |
| 819 | + id: createShapeId(), |
| 820 | + type: nodeType, |
| 821 | + x: position.x - w / 2, |
| 822 | + y: position.y - h / 2, |
| 823 | + props: { |
| 824 | + uid, |
| 825 | + title: nodeText, |
| 826 | + w, |
| 827 | + h, |
| 828 | + ...(imageUrl && { imageUrl }), |
| 829 | + size: "s", |
| 830 | + fontFamily: "sans", |
| 831 | + }, |
830 | 832 | }, |
831 | | - }, |
832 | | - ]); |
833 | | - }; |
| 833 | + ]); |
| 834 | + }, |
| 835 | + [editor, extensionAPI], |
| 836 | + ); |
834 | 837 |
|
835 | 838 | useEffect(() => { |
836 | 839 | // https://tldraw.dev/examples/data/assets/hosted-images |
@@ -1097,13 +1100,13 @@ const InsideEditorAndUiContext = ({ |
1097 | 1100 | }; |
1098 | 1101 | }; |
1099 | 1102 | const cleanupCustomSideEffects = registerCustomSideEffects(); |
1100 | | - const [cleanupSideEffects] = registerDefaultSideEffects(editor); |
| 1103 | + const cleanupSideEffects = registerDefaultSideEffects(editor); |
1101 | 1104 |
|
1102 | 1105 | return () => { |
1103 | 1106 | cleanupSideEffects(); |
1104 | 1107 | cleanupCustomSideEffects(); |
1105 | 1108 | }; |
1106 | | - }, [editor, msg, toasts, extensionAPI]); |
| 1109 | + }, [editor, msg, toasts, extensionAPI, allNodes, createDiscourseNodeShape]); |
1107 | 1110 |
|
1108 | 1111 | return <CustomContextMenu extensionAPI={extensionAPI} allNodes={allNodes} />; |
1109 | 1112 | }; |
|
0 commit comments