|
|
@@ -19,9 +19,16 @@
|
|
|
children?: TreeData[];
|
|
|
}
|
|
|
|
|
|
- const props = defineProps<{
|
|
|
- treeData: TreeData;
|
|
|
- }>();
|
|
|
+ const props = withDefaults(
|
|
|
+ defineProps<{
|
|
|
+ treeData: TreeData;
|
|
|
+ /** 与左侧组织树选中的 orgId 一致(不含 `org-` 前缀),用于同步节点高亮 */
|
|
|
+ highlightOrgId?: string | number | null;
|
|
|
+ }>(),
|
|
|
+ {
|
|
|
+ highlightOrgId: null,
|
|
|
+ },
|
|
|
+ );
|
|
|
|
|
|
const emits = defineEmits<{
|
|
|
(event: 'node-click', nodeData: any): void;
|
|
|
@@ -32,6 +39,47 @@
|
|
|
const container = ref<HTMLDivElement | null>(null); // 图表容器引用
|
|
|
let graph: any = null;
|
|
|
let pending = false; // 防止并发 initGraph
|
|
|
+ /** 上一次由外部同步高亮的图节点 id(`org-xxx`) */
|
|
|
+ let prevExternalHighlightNodeId: string | null = null;
|
|
|
+
|
|
|
+ function resolveGraphNodeId(raw: string | number | null | undefined): string | null {
|
|
|
+ if (raw === null || raw === undefined) return null;
|
|
|
+ const s = String(raw).trim();
|
|
|
+ if (!s) return null;
|
|
|
+ return s.startsWith('org-') ? s : `org-${s}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 根据左侧选中 orgId 同步节点 selected 状态(与 click-select 的 selected 样式一致) */
|
|
|
+ async function applyExternalHighlight() {
|
|
|
+ if (!graph) return;
|
|
|
+
|
|
|
+ const nextId = resolveGraphNodeId(props.highlightOrgId as string | number | null | undefined);
|
|
|
+
|
|
|
+ if (prevExternalHighlightNodeId && prevExternalHighlightNodeId !== nextId) {
|
|
|
+ try {
|
|
|
+ await graph.setElementState(prevExternalHighlightNodeId, []);
|
|
|
+ } catch {
|
|
|
+ /* 图重建过程中可能无效,忽略 */
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!nextId) {
|
|
|
+ prevExternalHighlightNodeId = null;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const node = graph.getNodeData(nextId);
|
|
|
+ if (!node) {
|
|
|
+ prevExternalHighlightNodeId = null;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ await graph.setElementState(nextId, 'selected');
|
|
|
+ prevExternalHighlightNodeId = nextId;
|
|
|
+ } catch {
|
|
|
+ prevExternalHighlightNodeId = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
// 初始化图表
|
|
|
const initGraph = async () => {
|
|
|
@@ -44,6 +92,7 @@
|
|
|
graph.destroy();
|
|
|
graph = null;
|
|
|
}
|
|
|
+ prevExternalHighlightNodeId = null;
|
|
|
|
|
|
// 等待下一帧,确保销毁完成
|
|
|
await nextTick();
|
|
|
@@ -135,6 +184,7 @@
|
|
|
|
|
|
// 渲染
|
|
|
graph.render();
|
|
|
+ await applyExternalHighlight();
|
|
|
|
|
|
// 监听节点点击事件
|
|
|
graph.on(NodeEvent.CLICK, (evt) => {
|
|
|
@@ -171,6 +221,14 @@
|
|
|
{ deep: true },
|
|
|
);
|
|
|
|
|
|
+ watch(
|
|
|
+ () => props.highlightOrgId,
|
|
|
+ async () => {
|
|
|
+ await nextTick();
|
|
|
+ await applyExternalHighlight();
|
|
|
+ },
|
|
|
+ );
|
|
|
+
|
|
|
// 生命周期钩子
|
|
|
onMounted(() => {
|
|
|
data = treeToGraphData(props.treeData);
|