Skip to content

Commit

Permalink
refactor(initD3): pass initial transform
Browse files Browse the repository at this point in the history
  • Loading branch information
moklick committed Nov 9, 2020
1 parent 7cc2306 commit c7bf8ff
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/container/GraphView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const GraphView = ({
onPaneContextMenu,
}: GraphViewProps) => {
const isInitialised = useRef<boolean>(false);
const d3Initialised = useStoreState((state) => state.d3Initialised);
const d3Zoom = useStoreState((state) => state.d3Zoom);
const setOnConnect = useStoreActions((actions) => actions.setOnConnect);
const setOnConnectStart = useStoreActions((actions) => actions.setOnConnectStart);
const setOnConnectStop = useStoreActions((actions) => actions.setOnConnectStop);
Expand All @@ -152,7 +152,7 @@ const GraphView = ({
useElementUpdater(elements);

useEffect(() => {
if (!isInitialised.current && d3Initialised) {
if (!isInitialised.current && d3Zoom) {
if (onLoad) {
onLoad({
fitView: (params = { padding: 0.1 }) => fitView(params),
Expand All @@ -169,7 +169,7 @@ const GraphView = ({

isInitialised.current = true;
}
}, [d3Initialised, onLoad]);
}, [d3Zoom, onLoad]);

useEffect(() => {
if (onConnect) {
Expand Down
9 changes: 3 additions & 6 deletions src/container/ZoomPane/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const ZoomPane = ({

useEffect(() => {
if (zoomPane.current) {
// initD3: action((state, { zoomPane, defaultPosition, defaultZoom, translateExtent }) => {
const state = store.getState();
const currentTranslateExtent = typeof translateExtent !== 'undefined' ? translateExtent : state.translateExtent;
const d3ZoomInstance = zoom().scaleExtent([state.minZoom, state.maxZoom]).translateExtent(currentTranslateExtent);
Expand All @@ -76,18 +75,16 @@ const ZoomPane = ({
const clampedX = clamp(defaultPosition[0], currentTranslateExtent[0][0], currentTranslateExtent[1][0]);
const clampedY = clamp(defaultPosition[1], currentTranslateExtent[0][1], currentTranslateExtent[1][1]);
const clampedZoom = clamp(defaultZoom, state.minZoom, state.maxZoom);

const updatedTransform = zoomIdentity.translate(clampedX, clampedY).scale(clampedZoom);
// selection.property('__zoom', updatedTransform);

const zoomHandler = selection.on('wheel.zoom');

d3ZoomInstance.transform(selection, updatedTransform);

initD3Zoom({
d3Zoom: d3ZoomInstance,
d3Selection: selection,
d3ZoomHandler: zoomHandler,
d3ZoomHandler: selection.on('wheel.zoom'),
// we need to pass transform because zoom handler is not registered when we set the initial transform
transform: [clampedX, clampedY, clampedZoom],
});
}
}, []);
Expand Down
10 changes: 6 additions & 4 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type InitD3Zoom = {
d3Zoom: ZoomBehavior<Element, unknown>;
d3Selection: D3Selection<Element, unknown, null, undefined>;
d3ZoomHandler: ((this: Element, event: any, d: unknown) => void) | undefined;
transform: Transform;
};
export interface StoreModel {
width: number;
Expand All @@ -59,7 +60,6 @@ export interface StoreModel {
d3Zoom: ZoomBehavior<Element, unknown> | null;
d3Selection: D3Selection<Element, unknown, null, undefined> | null;
d3ZoomHandler: ((this: Element, event: any, d: unknown) => void) | undefined;
d3Initialised: boolean;
minZoom: number;
maxZoom: number;
translateExtent: TranslateExtent;
Expand Down Expand Up @@ -155,7 +155,6 @@ export const storeModel: StoreModel = {

d3Zoom: null,
d3Selection: null,
d3Initialised: false,
d3ZoomHandler: undefined,
minZoom: 0.5,
maxZoom: 2,
Expand Down Expand Up @@ -388,11 +387,14 @@ export const storeModel: StoreModel = {
state.height = size.height || 500;
}),

initD3Zoom: action((state, { d3Zoom, d3Selection, d3ZoomHandler }) => {
initD3Zoom: action((state, { d3Zoom, d3Selection, d3ZoomHandler, transform }) => {
state.d3Zoom = d3Zoom;
state.d3Selection = d3Selection;
state.d3ZoomHandler = d3ZoomHandler;
state.d3Initialised = true;

state.transform[0] = transform[0];
state.transform[1] = transform[1];
state.transform[2] = transform[2];
}),

setMinZoom: action((state, minZoom) => {
Expand Down

0 comments on commit c7bf8ff

Please sign in to comment.