Skip to content

Commit 2e7affb

Browse files
authored
#973 Add a switch for auto-snapping (#974)
1 parent c4f9901 commit 2e7affb

File tree

9 files changed

+36
-11
lines changed

9 files changed

+36
-11
lines changed

src/components/page-header/settings-modal.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ import { useTranslation } from 'react-i18next';
3232
import { MdArrowBack, MdArrowDownward, MdArrowForward, MdArrowUpward, MdOpenInNew, MdReadMore } from 'react-icons/md';
3333
import { StationCity } from '../../constants/constants';
3434
import { useRootDispatch, useRootSelector } from '../../redux';
35-
import { setAutoParallel, setGridLines, setRandomStationsNames, setTelemetryProject } from '../../redux/app/app-slice';
35+
import {
36+
setAutoParallel,
37+
setGridLines,
38+
setRandomStationsNames,
39+
setSnapLines,
40+
setTelemetryProject,
41+
} from '../../redux/app/app-slice';
3642
import { setKeepLastPath } from '../../redux/runtime/runtime-slice';
3743
import { isMacClient } from '../../util/helpers';
3844
import { MAX_PARALLEL_LINES_FREE, MAX_PARALLEL_LINES_PRO } from '../../util/parallel';
@@ -59,7 +65,7 @@ const SettingsModal = (props: { isOpen: boolean; onClose: () => void }) => {
5965
const { activeSubscriptions } = useRootSelector(state => state.account);
6066
const {
6167
telemetry: { project: isAllowProjectTelemetry },
62-
preference: { autoParallel, randomStationsNames, gridLines },
68+
preference: { autoParallel, randomStationsNames, gridLines, snapLines },
6369
} = useRootSelector(state => state.app);
6470
const { keepLastPath, parallelLinesCount } = useRootSelector(state => state.runtime);
6571
const dispatch = useRootDispatch();
@@ -169,6 +175,13 @@ const SettingsModal = (props: { isOpen: boolean; onClose: () => void }) => {
169175
onChange={({ target: { checked } }) => dispatch(setGridLines(checked))}
170176
/>
171177
</Box>
178+
<Box display="flex" mb="1">
179+
<Text flex="1">{t('header.settings.preference.snapline')}</Text>
180+
<Switch
181+
isChecked={snapLines}
182+
onChange={({ target: { checked } }) => dispatch(setSnapLines(checked))}
183+
/>
184+
</Box>
172185
</Box>
173186
</Box>
174187

src/components/svg-canvas-graph.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const SvgCanvas = () => {
6161
};
6262
const {
6363
telemetry: { project: isAllowProjectTelemetry },
64-
preference: { autoParallel, gridLines: useGridLines },
64+
preference: { autoParallel, gridLines: useGridLines, snapLines: useSnapLines },
6565
} = useRootSelector(state => state.app);
6666
const { svgViewBoxZoom, svgViewBoxMin } = useRootSelector(state => state.param);
6767
const {
@@ -91,8 +91,7 @@ const SvgCanvas = () => {
9191
// note only the nearest 2 of them will be drawn
9292
React.useEffect(
9393
() => {
94-
if (!pointerPosition) return;
95-
console.log('refresh snap lines');
94+
if (!pointerPosition || !useSnapLines) return;
9695
const svgViewRange = getViewpointSize(svgViewBoxMin, svgViewBoxZoom, width, height);
9796
const nodesInViewRange = findNodesInRectangle(
9897
graph.current,
@@ -149,7 +148,7 @@ const SvgCanvas = () => {
149148
const { x, y } = getMousePosition(e);
150149

151150
if (mode === 'free' && active === node) {
152-
if (!e.altKey && !useGridLines) {
151+
if (!e.altKey && useSnapLines) {
153152
// node start position (fromX, fromY)
154153
const fromX = graph.current.getNodeAttribute(node, 'x');
155154
const fromY = graph.current.getNodeAttribute(node, 'y');

src/i18n/translations/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,8 @@
575575
"shmetro": "Shanghai",
576576
"bjsubway": "Beijing"
577577
},
578-
"gridline": "Show grid guide lines and disable auto-snapping"
578+
"gridline": "Show grid guide lines",
579+
"snapline": "Auto snapping to the guide lines and points"
579580
},
580581
"shortcuts": {
581582
"title": "Shortcuts",

src/i18n/translations/ja.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,8 @@
575575
"shmetro": "上海",
576576
"bjsubway": "北京"
577577
},
578-
"gridline": "グリッドガイドラインを表示し、自動スナップを無効にする"
578+
"gridline": "グリッドガイドラインを表示",
579+
"snapline": "ガイドラインおよびポイントに自動的にスナップする"
579580
},
580581
"shortcuts": {
581582
"title": "ショートカット",

src/i18n/translations/ko.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,8 @@
574574
"shmetro": "상하이",
575575
"bjsubway": "베이징"
576576
},
577-
"gridline": "그리드 가이드라인 표시 및 자동 스냅 비활성화"
577+
"gridline": "격자 가이드 라인 표시",
578+
"snapline": "가이드 라인과 포인트에 자동 스냅"
578579
},
579580
"shortcuts": {
580581
"title": "바로 가기",

src/i18n/translations/zh-Hans.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,8 @@
574574
"shmetro": "上海",
575575
"bjsubway": "北京"
576576
},
577-
"gridline": "显示网格参考线并不启用磁性布局"
577+
"gridline": "显示网格参考线",
578+
"snapline": "磁性吸附到参考线"
578579
},
579580
"shortcuts": {
580581
"title": "快捷键",

src/i18n/translations/zh-Hant.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,8 @@
574574
"shmetro": "上海",
575575
"bjsubway": "北京"
576576
},
577-
"gridline": "顯示網格參考線並停用自動吸附"
577+
"gridline": "顯示網格參考線",
578+
"snapline": "磁性吸附到參考線"
578579
},
579580
"shortcuts": {
580581
"title": "捷徑",

src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
setToolsPanelExpansion,
2020
setUnlockSimplePath,
2121
setGridLines,
22+
setSnapLines,
2223
} from './redux/app/app-slice';
2324
import { ParamState, setFullState } from './redux/param/param-slice';
2425
import { refreshEdgesThunk, refreshNodesThunk } from './redux/runtime/runtime-slice';
@@ -65,6 +66,7 @@ const param = localStorage.getItem(LocalStorageKey.PARAM);
6566
if ('randomStationsNames' in app.preference)
6667
store.dispatch(setRandomStationsNames(app.preference.randomStationsNames));
6768
if ('gridLines' in app.preference) store.dispatch(setGridLines(app.preference.gridLines));
69+
if ('snapLines' in app.preference) store.dispatch(setSnapLines(app.preference.snapLines));
6870
}
6971
if ('state' in loginState) {
7072
store.dispatch(setState(loginState.state));

src/redux/app/app-slice.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export interface AppState {
4040
autoParallel: boolean;
4141
randomStationsNames: 'none' | StationCity;
4242
gridLines: boolean;
43+
snapLines: boolean;
4344
};
4445
}
4546

@@ -56,6 +57,7 @@ export const initialState: AppState = {
5657
autoParallel: true,
5758
randomStationsNames: 'none',
5859
gridLines: false,
60+
snapLines: true,
5961
},
6062
};
6163

@@ -84,6 +86,9 @@ const appSlice = createSlice({
8486
setGridLines: (state, action: PayloadAction<boolean>) => {
8587
state.preference.gridLines = action.payload;
8688
},
89+
setSnapLines: (state, action: PayloadAction<boolean>) => {
90+
state.preference.snapLines = action.payload;
91+
},
8792
},
8893
});
8994

@@ -95,5 +100,6 @@ export const {
95100
setAutoParallel,
96101
setRandomStationsNames,
97102
setGridLines,
103+
setSnapLines,
98104
} = appSlice.actions;
99105
export default appSlice.reducer;

0 commit comments

Comments
 (0)