Move viewport to show particular set of nodes in Svelte Flow - Stack Overflow

I've created a svelte component Text2Chart that transforms algorithm (adhere to certain syntax) to

I've created a svelte component Text2Chart that transforms algorithm (adhere to certain syntax) to flow chart using @xyflow/svelte library. When a user selects lines of algorithm in textarea, I highlight relevant nodes in flow chart. If the chart is not showing the selected nodes currently, I want to move the viewport to show selected nodes.

I couldn't find any API supported by Svelte Flow to set this. We're not allowed to make instance of svelte flow, and we can't use useSvelteFlow outside of tag scope.

People have advised a few hacks in discussion on Github. But I'm not able to understand how it has to be implemented.

I've create a component with following code and including it in SvelteProvider. It is executing but view port is not changing.

<script>
  import {
    useSvelteFlow
  } from '@xyflow/svelte';

  let svelteFlowElement;
  export let selection;
  export let nodes;

  function fitViewToSelection(selectedNodeIds, nodes) {
    if (selectedNodeIds) {
      const nd = nodes.filter(node => selectedNodeIds.includes(node.id));
      // console.log(nd)
      svelteFlowElement.setViewport(
        {nodes: nd});
      }
  }
  $: {
    if (selection && selection.nodeIds) {
      fitViewToSelection(selection.nodeIds, nodes);
    }
  }
  function adjustViewport(detail) {
    console.log("adjusting view")
    const { x, y, width, height } = detail;
    svelteFlowElement.setViewport({ x, y, width, height }, { padding: 20, duration: 200 });
  }

  $: {
    svelteFlowElement = useSvelteFlow();
  }
</script>

I've created a svelte component Text2Chart that transforms algorithm (adhere to certain syntax) to flow chart using @xyflow/svelte library. When a user selects lines of algorithm in textarea, I highlight relevant nodes in flow chart. If the chart is not showing the selected nodes currently, I want to move the viewport to show selected nodes.

I couldn't find any API supported by Svelte Flow to set this. We're not allowed to make instance of svelte flow, and we can't use useSvelteFlow outside of tag scope.

People have advised a few hacks in discussion on Github. But I'm not able to understand how it has to be implemented.

I've create a component with following code and including it in SvelteProvider. It is executing but view port is not changing.

<script>
  import {
    useSvelteFlow
  } from '@xyflow/svelte';

  let svelteFlowElement;
  export let selection;
  export let nodes;

  function fitViewToSelection(selectedNodeIds, nodes) {
    if (selectedNodeIds) {
      const nd = nodes.filter(node => selectedNodeIds.includes(node.id));
      // console.log(nd)
      svelteFlowElement.setViewport(
        {nodes: nd});
      }
  }
  $: {
    if (selection && selection.nodeIds) {
      fitViewToSelection(selection.nodeIds, nodes);
    }
  }
  function adjustViewport(detail) {
    console.log("adjusting view")
    const { x, y, width, height } = detail;
    svelteFlowElement.setViewport({ x, y, width, height }, { padding: 20, duration: 200 });
  }

  $: {
    svelteFlowElement = useSvelteFlow();
  }
</script>

Share Improve this question edited Jan 29 at 7:23 Amit Kumar Gupta asked Jan 29 at 6:01 Amit Kumar GuptaAmit Kumar Gupta 7,40912 gold badges68 silver badges95 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I have solved the issue by using fitView instead of setViewport.

svelteFlowElement.fitView({
    nodes: nd,
    maxZoom: 1,
    duration: 200,
    // includeHiddenNodes: true
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745309780a4621935.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信