Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ui/src/lib/store/canvasSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ export interface CanvasSlice {
parent: string
) => void;

setNodeWidth: (id: string, width: number) => void;

pastingNodes?: Node[];
headPastingNodes?: Set<string>;
mousePos?: XYPosition | undefined;
Expand Down Expand Up @@ -452,6 +454,22 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
}
},

setNodeWidth: (id, width) => {
let nodesMap = get().ydoc.getMap<Node>("pods");
let node = nodesMap.get(id);
if (!node) return;
nodesMap.set(id, { ...node, width });
let geoData = {
parent: node.parentNode ? node.parentNode : "ROOT",
x: node.position.x,
y: node.position.y,
width: node.width!,
height: node.height!,
};
get().setPodGeo(node.id, geoData, true);
get().updateView();
},

isPasting: false,
isCutting: false,

Expand Down
19 changes: 17 additions & 2 deletions ui/src/lib/store/podSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const createPodSlice: StateCreator<MyState, [], [], PodSlice> = (
// @ts-ignore
"setPodName"
),
setPodContent: ({ id, content }) =>
setPodContent: ({ id, content }) => {
set(
produce((state) => {
let pod = state.pods[id];
Expand All @@ -84,7 +84,22 @@ export const createPodSlice: StateCreator<MyState, [], [], PodSlice> = (
false,
// @ts-ignore
"setPodContent"
),
);
// also calculate the sizes of the lines, and update the width of the pod
const sizes = content
.split("\n")
.map((line) => line.trim())
.filter((line) => line.length > 0)
.map((line) => line.length);
// calculate the sum
const sum = sizes.reduce((a, b) => a + b, 0);
// calculate the 70% average width
let width = Math.round((sum / sizes.length) * 0.7);
// the width should be within 60 and 80
width = Math.min(80, Math.max(60, width));
// set the width
get().setNodeWidth(id, Math.round((width / 40) * 300));
},
initPodContent: ({ id, content }) =>
set(
produce((state) => {
Expand Down