Skip to content

Commit

Permalink
Merge pull request #281 from open-source-labs/dev
Browse files Browse the repository at this point in the history
feat: added center prop to node to allow for initial placement in center of viewport
  • Loading branch information
briangregoryholmes authored Apr 24, 2023
2 parents 4e5668a + 6ea6eae commit bece669
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 33 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelvet",
"version": "7.0.23",
"version": "7.0.24",
"description": "A lightweight Svelte component library for building dynamic, node-based user interfaces",
"keywords": [
"svelte",
Expand All @@ -22,7 +22,7 @@
"build": "vite build && npm run package",
"preview": "vite preview",
"package": "svelte-kit sync && svelte-package && publint",
"prepublishOnly": "npm run package",
"prepublishOnly": "yarn format && yarn lint && yarn test && npm run package",
"test": "playwright test",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
Expand Down Expand Up @@ -53,7 +53,6 @@
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-svelte3": "^4.0.0",
"husky": "^8.0.3",
"prettier": "^2.8.0",
"prettier-plugin-svelte": "^2.8.1",
"publint": "^0.1.9",
Expand Down
2 changes: 1 addition & 1 deletion src/example-components/CustomEdge.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let color: CSSColorString = 'yellow';
</script>

<Edge {color} let:destroy width={4} edgeClick={() => (color = 'blue')}>
<Edge {color} let:destroy width={4} animate>
<button id="destroy-edge" on:click={destroy} slot="label">Custom Label</button>
</Edge>

Expand Down
5 changes: 4 additions & 1 deletion src/example-components/CustomNode.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<script lang="ts">
import { Node, Anchor, Slider } from '$lib';
import { writable } from 'svelte/store';
import { get, writable } from 'svelte/store';
import { getContext, onMount } from 'svelte';
import type { Graph } from '$lib/types';
const parameter = writable(10);
const graph: Graph = getContext('graph');
</script>

<Node let:grabHandle let:selected>
Expand Down
3 changes: 2 additions & 1 deletion src/example-components/test-components/CircleColor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ColorAnchor from './ColorAnchor.svelte';
import { generateInput, generateOutput, Resizer, ColorPicker, Node, Anchor } from '$lib';
import type { CSSColorString } from '$lib';
import CustomEdge from '../CustomEdge.svelte';
type Inputs = {
color: CSSColorString;
Expand Down Expand Up @@ -33,9 +34,9 @@
let:connecting
outputStore={output}
output
edgeStyle="bezier"
edgeColor={output}
edgeLabel="Dynamic Edges"
edge={CustomEdge}
locked
>
<ColorAnchor color={output} {connecting} {linked} />
Expand Down
28 changes: 20 additions & 8 deletions src/lib/components/Node/InternalNode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
export let node: Node;
export let isDefault: boolean;
export let center: boolean;
const position = node.position;
const widthStore = node.dimensions.width;
Expand Down Expand Up @@ -57,18 +58,29 @@
$: activeGroup = graph.activeGroup;
$: cursor = graph.cursor;
$: initialNodePositions = graph.initialNodePositions;
$: centerPoint = graph.center;
onMount(() => {
// If the node dimensions got set in previous steps, we don't need to do anything
if ($widthStore && $heightStore) return;
// This only runs when a width and height were not provided via props
// Set the wrapper to fit-content and grab the width and height
[minWidth, minHeight] = calculateFitContentWidth(DOMnode);
if (!$widthStore && !$heightStore) {
// This only runs when a width and height were not provided via props
// Set the wrapper to fit-content and grab the width and height
[minWidth, minHeight] = calculateFitContentWidth(DOMnode);
// Update the node dimensions in the store
$widthStore = minWidth;
$heightStore = minHeight;
}
// Update the node dimensions in the store
$widthStore = minWidth;
$heightStore = minHeight;
if (center) {
const opticalCenter = {
x: $centerPoint.x - $widthStore / 2,
y: $centerPoint.y - $heightStore / 2
};
node.position.set(opticalCenter);
tracking.set(true);
tracking.set(false);
}
$mounted++;
});
Expand Down
9 changes: 4 additions & 5 deletions src/lib/components/Node/Node.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
const graph = getContext<Graph>('graph');
//const storedNode = JSON.parse(localStorage.getItem('state'))?.nodes?[id]
const { nodes } = graph;
export let position = { x: 0, y: 0 };
export let dimensions: InitialDimensions | null = null;
export let id: string | number = 0;
Expand All @@ -44,6 +42,7 @@
export let edge: ComponentType | null = null;
export let connections: Connections = [];
export let useDefaults = false;
export let center = false;
let node: NodeType;
const group: GroupKey = getContext('group');
Expand Down Expand Up @@ -140,8 +139,7 @@
node.zIndex.set(zIndex);
}
// $: if (node) {
// node.position.x.set(position.x);
// node.position.y.set(position.y);
// node.position.set({ x: position.x, y: position.y });
// }
$: if (node) {
node.inputs.set(inputs);
Expand All @@ -151,8 +149,9 @@
}
</script>

{#if node && $nodes[node.id]}
{#if node}
<InternalNode
{center}
isDefault={isDefault || useDefaults}
let:destroy
let:selected
Expand Down
1 change: 1 addition & 0 deletions src/lib/containers/Graph/Graph.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
width: DOMRect.width,
height: DOMRect.height
};
graph.dimensions.set(graphDimensions);
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ThemeToggle from './components/ThemeToggle/ThemeToggle.svelte';
import ColorPicker from './components/data/ColorPicker/ColorWheel.svelte';
import Resizer from './components/Resizer/Resizer.svelte';
import { generateInput, generateOutput } from './utils/creators';

import { getViewportCenter } from './utils/getters/getViewportCenter';
export {
Svelvet,
Controls,
Expand All @@ -28,7 +28,8 @@ export {
ThemeToggle,
ColorPicker,
generateInput,
generateOutput
generateOutput,
getViewportCenter
};

export * from './types';
1 change: 1 addition & 0 deletions src/lib/types/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface Graph {
bottom: number;
}>;
};
center: Readable<XYPair>;
maxZIndex: Writable<number>;
dimensions: Writable<GraphDimensions>;
editable: boolean;
Expand Down
23 changes: 23 additions & 0 deletions src/lib/utils/calculators/calculateViewPortCenter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { GraphDimensions } from '$lib/types';
import { calculateRelativeCursor } from './calculateRelativeCursor';

export function calculateViewportCenter(
dimensions: GraphDimensions,
translationX: number,
translationY: number,
scale: number
) {
const { width, height, top, left } = dimensions;

const viewportCenter = { clientX: width / 2, clientY: height / 2 };
return calculateRelativeCursor(
viewportCenter,
top,
left,
width,
height,
scale,
translationX,
translationY
);
}
23 changes: 12 additions & 11 deletions src/lib/utils/creators/createGraph.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { writable } from 'svelte/store';
import type {
Graph,
Node,
GroupBox,
EdgeKey,
GraphKey,
GroupKey,
GraphDimensions
} from '$lib/types';
import { derived, writable } from 'svelte/store';
import type { Graph, Node, GroupBox, EdgeKey, GraphKey, GroupKey } from '$lib/types';
import { createStore } from './createStore';
import { cursorPositionRaw } from '$lib/stores/CursorStore';
import type { WritableEdge, NodeKey } from '$lib/types';
import { createDerivedCursorStore } from './createDerivedCursoreStore';
import { createBoundsStore } from './createBoundsStore';
import type { GraphConfig } from '$lib/types';
import { calculateViewportCenter } from '../calculators/calculateViewPortCenter';

export function createGraph(id: GraphKey, config: GraphConfig): Graph {
const {
Expand All @@ -30,13 +23,20 @@ export function createGraph(id: GraphKey, config: GraphConfig): Graph {
x: writable(initialTranslation?.x || 0),
y: writable(initialTranslation?.y || 0)
};
const dimensions = writable({} as GraphDimensions);

const dimensions = writable({ top: 0, left: 0, width: 0, height: 0, bottom: 0, right: 0 });

const scale = writable(zoom);

const nodes = createStore<Node, NodeKey>();
const bounds = createBoundsStore(nodes, dimensions, scale, translation.x, translation.y);

const center = derived(
[dimensions, translation.x, translation.y, scale],
([$dimensions, $translationX, $translationY, $scale]) => {
return calculateViewportCenter($dimensions, $translationX, $translationY, $scale);
}
);
const graph: Graph = {
id,
nodes,
Expand All @@ -48,6 +48,7 @@ export function createGraph(id: GraphKey, config: GraphConfig): Graph {
maxZIndex: writable(2),
dimensions,
bounds,
center,
direction: direction || 'LR',
editable: editable || false,
linkingInput: writable(null),
Expand Down
15 changes: 15 additions & 0 deletions src/lib/utils/getters/getViewportCenter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { calculateViewportCenter } from '../calculators/calculateViewPortCenter';
import { graphStore } from '$lib/stores';
import { get } from 'svelte/store';

export function getViewportCenter(graphId: string) {
const graph = graphStore.get(`G-${graphId}`);
if (!graph) throw new Error(`Graph with id ${graphId} not found`);

return calculateViewportCenter(
get(graph.dimensions),
get(graph.transforms.translation.x),
get(graph.transforms.translation.y),
get(graph.transforms.scale)
);
}
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="wrapper">
<Svelvet controls edgeStyle="step" edge={CustomEdge} TD theme="dark" zoom={0.6} minimap>
<CustomNode />
<Node position={{ x: -200, y: 200 }} id="NODE" label="NODE" resizable />
<Node center position={{ x: -200, y: 200 }} id="NODE" label="NODE" resizable />
</Svelvet>
</div>
</body>
Expand Down

0 comments on commit bece669

Please sign in to comment.