feature: cytoscape node positions are remembered

This commit is contained in:
Cindy Chang
2024-06-28 16:20:41 +08:00
parent b890df9de6
commit 2110388a2d
8 changed files with 134 additions and 19 deletions

View File

@@ -0,0 +1,60 @@
import { defineStore } from 'pinia';
// interface NodePosition {
// id: string;
// position: { x: number; y: number };
// graphId: string;
// }
export default defineStore('useCytoscapeStore', {
state: () => ({
nodePositions: [],
currentGraphId: "",
}),
actions: {
/**
* 儲存或更新單個節點的位置資訊。
* @param {string} id
* @param {object} position {x, y}
* @param {string} graphId
*/
saveNodePosition(id, position) {
const existingNode = this.nodePositions.find(node => node.id === id && node.graphId === this.currentGraphId);
if (existingNode) { // 如果存在,更新位置資訊
existingNode.position = position;
} else { // 如果不存在,新增一個新的位置資訊
this.nodePositions.push({
id,
position,
graphId: this.currentGraphId,
});
}
},
/**
* 根據節點 ID 獲取該節點的位置資訊
* @param {string} id
* @returns
*/
getNodePosition(id) { // 返回該節點的位置資訊,如果不存在則返回 undefined
return this.nodePositions.find(node => node.id === id)?.position;
},
/**
* 從本地存儲中加載節點位置資訊。
*/
loadPositionsFromStorage() {
const storedPositions = localStorage.getItem('cy-node-positions');
if (storedPositions) {
this.nodePositions = JSON.parse(storedPositions);
}
},
/**
* 將節點位置資訊儲存到本地存儲中。
*/
savePositionsToStorage() {
localStorage.setItem('cy-node-positions', JSON.stringify(this.nodePositions));
},
setCurrentGraphId(currentGraphId) {
this.currentGraphId = currentGraphId;
},
},
});

View File

@@ -2,6 +2,7 @@ import { defineStore } from "pinia";
import axios from 'axios';
import apiError from '@/module/apiError.js';
import { deleteCookie, setCookie, getCookie } from "../utils/cookieUtil";
import LoginStore from "@/stores/login.js";
export default defineStore('loginStore', {
// data, methods, computed
@@ -60,7 +61,6 @@ export default defineStore('loginStore', {
* Refresh Token
*/
async refreshToken() {
console.log('TODO:TODO:', this.auth);
const api = '/api/oauth/token';
this.auth.grant_type = 'refresh_token';
@@ -123,6 +123,9 @@ export default defineStore('loginStore', {
},
setRememberedReturnToUrl(returnToUrl){
this.rememberedReturnToUrl = returnToUrl
}
}
},
setIsLoggedIn(boolean) {
this.isLoggedIn = boolean;
},
},
});