Skip to content
Merged
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
2 changes: 1 addition & 1 deletion packages/common/src/algorithms/a-star.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class AStar {
if (currentPoint[0] === end[0] && currentPoint[1] === end[1]) {
break;
}
current.node.adjacentNodes.forEach(next => {
current.node.adjacentNodes.forEach((next) => {
let newCost = costSoFar.get(current!.node)! + this.heuristic(next.data, current!.node.data);
const previousNode = this.cameFrom.get(current!.node);
// Inflection point weight, if an inflection point occurs, cost + 1 to avoid the inflection point path
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/plugins/with-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ describe('with group plugin', () => {
it('should remove the group if there is only one element left within after deletion', () => {
addSelectedElement(board, [board.children[0]]);
deleteFragment(board);
const group = board.children.find(item => item.id === board.children[0].groupId);
const group = board.children.find((item) => item.id === board.children[0].groupId);
expect(group).toBe(undefined);
});

it('should update element groupId if there is only one element left within after deletion', () => {
addSelectedElement(board, [board.children[4]]);
deleteFragment(board);
const group = board.children.find(item => item.id === board.children[4].groupId);
const group = board.children.find((item) => item.id === board.children[4].groupId);
expect(group).toBe(undefined);
expect(board.children[3].groupId).toBe(group2[group2.length - 1].id);

Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/plugins/with-resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const withResize = <T extends PlaitElementOrArray = PlaitElementOrArray,
}
startPoint = [event.x, event.y];
const path = Array.isArray(resizeHitTestRef.element)
? resizeHitTestRef.element.map(el => PlaitBoard.findPath(board, el))
? resizeHitTestRef.element.map((el) => PlaitBoard.findPath(board, el))
: PlaitBoard.findPath(board, resizeHitTestRef.element);
resizeRef = {
path,
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/text/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BaseElement } from 'slate';

export type ElementSize = {
export type ElementSize = {
width: number;
height: number;
}
};

export enum Alignment {
left = 'left',
Expand Down
14 changes: 7 additions & 7 deletions packages/common/src/transforms/align.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const alignRight = (board: PlaitBoard) => {
function setOffset(board: PlaitBoard, getOffset: (outerRectangle: RectangleClient, rectangle: RectangleClient) => Point) {
const elements = getHighestSelectedElements(board);
const outerRectangle = getRectangleByElements(board, elements, false);
elements.forEach(element => {
elements.forEach((element) => {
if (!element.points && !PlaitGroupElement.isGroup(element)) return;
const rectangle = board.getRectangle(element)!;
const offset = getOffset(outerRectangle, rectangle);
Expand All @@ -70,8 +70,8 @@ function setOffset(board: PlaitBoard, getOffset: (outerRectangle: RectangleClien
} else if (element.points) {
updateElements = [element];
}
updateElements.forEach(item => {
const newPoints = item.points!.map(p => [p[0] + offset[0], p[1] + offset[1]]) as Point[];
updateElements.forEach((item) => {
const newPoints = item.points!.map((p) => [p[0] + offset[0], p[1] + offset[1]]) as Point[];
const path = PlaitBoard.findPath(board, item);
Transforms.setNode(
board,
Expand All @@ -98,14 +98,14 @@ const distribute = (board: PlaitBoard, isHorizontal: boolean) => {
const axis = isHorizontal ? 'x' : 'y';
const side = isHorizontal ? 'width' : 'height';
const highestSelectedElements = getHighestSelectedElements(board);
const refs = highestSelectedElements.map(element => {
const refs = highestSelectedElements.map((element) => {
return { element, rectangle: board.getRectangle(element)! };
});
const outerRectangle = getRectangleByElements(board, highestSelectedElements, false);
const minRectangleRef = refs.sort((a, b) => a.rectangle[axis] - b.rectangle[axis])[0];
const maxRectangleRef = refs.sort((a, b) => b.rectangle[axis] + b.rectangle[side] - (a.rectangle[axis] + a.rectangle[side]))[0];
const minIndex = refs.findIndex(ref => ref === minRectangleRef);
const maxIndex = refs.findIndex(ref => ref === maxRectangleRef);
const minIndex = refs.findIndex((ref) => ref === minRectangleRef);
const maxIndex = refs.findIndex((ref) => ref === maxRectangleRef);
let distributeRefs = refs.filter((element, index) => index !== minIndex && index !== maxIndex);
const sum = distributeRefs.reduce((accumulator, current) => current.rectangle[side] + accumulator, 0);
const offset =
Expand All @@ -118,7 +118,7 @@ const distribute = (board: PlaitBoard, isHorizontal: boolean) => {
const moveAxis = isHorizontal ? 0 : 1;
moveOffset[moveAxis] = position - rectangle[axis];
const path = PlaitBoard.findPath(board, distributeRefs[i].element);
const newPoints = distributeRefs[i].element.points!.map(p => [p[0] + moveOffset[0], p[1] + moveOffset[1]]) as Point[];
const newPoints = distributeRefs[i].element.points!.map((p) => [p[0] + moveOffset[0], p[1] + moveOffset[1]]) as Point[];
Transforms.setNode(
board,
{
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/transforms/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface SetOptions<T extends PlaitElement = PlaitElement> {

export const setProperty = <T extends PlaitElement = PlaitElement>(board: PlaitBoard, properties: Partial<T>, options?: SetOptions<T>) => {
const selectedElements = getSelectedElements(board) as T[];
selectedElements.forEach(element => {
selectedElements.forEach((element) => {
if (options?.match && !options?.match(element)) return;
const path = PlaitBoard.findPath(board, element);
const memorizeKey = options?.getMemorizeKey ? options?.getMemorizeKey(element) : '';
Expand Down
10 changes: 5 additions & 5 deletions packages/common/src/utils/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export const buildClipboardData = (
startPoint: Point,
elementBuilder?: (element: PlaitElement) => PlaitElement | undefined
) => {
return elements.map(element => {
return elements.map((element) => {
const newElement = elementBuilder && elementBuilder(element);
if (newElement) {
return newElement;
}
if (element.points) {
const points = element.points.map(point => [point[0] - startPoint[0], point[1] - startPoint[1]]);
const points = element.points.map((point) => [point[0] - startPoint[0], point[1] - startPoint[1]]);
return { ...element, points };
}
return element;
Expand All @@ -26,14 +26,14 @@ export const insertClipboardData = (
elementHandler?: (element: PlaitElement, idsMap: Record<string, string>) => void
) => {
const idsMap: Record<string, string> = {};
elements.forEach(element => {
elements.forEach((element) => {
idsMap[element.id] = idCreator();
});
elements.forEach(element => {
elements.forEach((element) => {
element.id = idsMap[element.id];
elementHandler && elementHandler(element, idsMap);
if (element.points) {
element.points = element.points.map(point => [startPoint[0] + point[0], startPoint[1] + point[1]]) as [Point, Point];
element.points = element.points.map((point) => [startPoint[0] + point[0], startPoint[1] + point[1]]) as [Point, Point];
}
Transforms.insertNode(board, element, [board.children.length]);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/utils/default-orthogonal-routing.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Credits to xyflow
// https://github.com/xyflow/xyflow/blob/main/packages/system/src/utils/edges/smoothstep-edge.ts

import { Direction, Point } from "@plait/core";
import { getDirectionFactor } from "./direction";
import { Direction, Point } from '@plait/core';
import { getDirectionFactor } from './direction';

export const getPoints = (source: Point, sourcePosition: Direction, target: Point, targetPosition: Direction, offset: number) => {
const sourceDirectionFactors = getDirectionFactor(sourcePosition);
Expand Down Expand Up @@ -99,4 +99,4 @@ function getEdgeCenter({
const centerY = targetY < sourceY ? targetY + yOffset : targetY - yOffset;

return [centerX, centerY, xOffset, yOffset];
}
}
28 changes: 20 additions & 8 deletions packages/common/src/utils/elbow-line-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ export const generateElbowLineRoute = (options: ElbowLineRouteOptions, board?: P
const isHitY = RectangleClient.isHitY(options.sourceOuterRectangle, options.targetOuterRectangle);
const centerX = isHitX ? undefined : RectangleClient.getGapCenter(options.sourceOuterRectangle, options.targetOuterRectangle, true);
const centerY = isHitY ? undefined : RectangleClient.getGapCenter(options.sourceOuterRectangle, options.targetOuterRectangle, false);
route = routeAdjust(route, { centerX, centerY, sourceRectangle: options.sourceRectangle, targetRectangle: options.targetRectangle }, board);
route = routeAdjust(
route,
{ centerX, centerY, sourceRectangle: options.sourceRectangle, targetRectangle: options.targetRectangle },
board
);
return route;
};

Expand All @@ -61,7 +65,11 @@ export const routeAdjust = (path: Point[], options: RouteAdjustOptions, board?:
const optionsX = getAdjustOptions(path, centerX, true);
const resultX =
optionsX.pointOfHit &&
adjust(path, { parallelPaths: optionsX.parallelPaths, pointOfHit: optionsX.pointOfHit, sourceRectangle, targetRectangle }, board);
adjust(
path,
{ parallelPaths: optionsX.parallelPaths, pointOfHit: optionsX.pointOfHit, sourceRectangle, targetRectangle },
board
);
if (resultX) {
path = resultX;
}
Expand All @@ -70,7 +78,11 @@ export const routeAdjust = (path: Point[], options: RouteAdjustOptions, board?:
const optionsY = getAdjustOptions(path, centerY, false);
const resultY =
optionsY.pointOfHit &&
adjust(path, { parallelPaths: optionsY.parallelPaths, pointOfHit: optionsY.pointOfHit, sourceRectangle, targetRectangle }, board);
adjust(
path,
{ parallelPaths: optionsY.parallelPaths, pointOfHit: optionsY.pointOfHit, sourceRectangle, targetRectangle },
board
);
if (resultY) {
path = resultY;
}
Expand All @@ -81,7 +93,7 @@ export const routeAdjust = (path: Point[], options: RouteAdjustOptions, board?:
const adjust = (route: Point[], options: AdjustOptions, board?: PlaitBoard): null | Point[] => {
const { parallelPaths, pointOfHit, sourceRectangle, targetRectangle } = options;
let result = null;
parallelPaths.forEach(parallelPath => {
parallelPaths.forEach((parallelPath) => {
// Construct a rectangle
const tempRectPoints = [pointOfHit, parallelPath[0], parallelPath[1]];
// directly use getCornerPoints will bring the precision issue (eg: 263.6923375175286 - 57.130859375)
Expand All @@ -94,7 +106,7 @@ const adjust = (route: Point[], options: AdjustOptions, board?: PlaitBoard): nul
const indexRangeInPath: number[] = [];
const indexRangeInCorner: number[] = [];
route.forEach((point, index) => {
const cornerResult = tempCorners.findIndex(corner => Point.isEquals(point, corner));
const cornerResult = tempCorners.findIndex((corner) => Point.isEquals(point, corner));
if (cornerResult !== -1) {
indexRangeInPath.push(index);
indexRangeInCorner.push(cornerResult);
Expand Down Expand Up @@ -153,7 +165,7 @@ export const getGraphPoints = (options: ElbowLineRouteOptions) => {
const y: number[] = [];
let result: Point[] = [];

[sourceOuterRectangle, targetOuterRectangle].forEach(rectangle => {
[sourceOuterRectangle, targetOuterRectangle].forEach((rectangle) => {
x.push(rectangle.x, rectangle.x + rectangle.width / 2, rectangle.x + rectangle.width);
y.push(rectangle.y, rectangle.y + rectangle.height / 2, rectangle.y + rectangle.height);
});
Expand Down Expand Up @@ -181,7 +193,7 @@ export const getGraphPoints = (options: ElbowLineRouteOptions) => {
}
}
}
result = removeDuplicatePoints(result).filter(point => {
result = removeDuplicatePoints(result).filter((point) => {
const isInSource = RectangleClient.isPointInRectangle(sourceOuterRectangle, point);
const isInTarget = RectangleClient.isPointInRectangle(targetOuterRectangle, point);
return !isInSource && !isInTarget;
Expand All @@ -193,7 +205,7 @@ export const createGraph = (points: Point[]) => {
const graph = new PointGraph();
const Xs: number[] = [];
const Ys: number[] = [];
points.forEach(p => {
points.forEach((p) => {
const x = p[0],
y = p[1];
if (Xs.indexOf(x) < 0) Xs.push(x);
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/utils/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function getImageSize(file: File, defaultImageWidth: number): Promise<{ width: n
const image = new Image();
image.src = URL.createObjectURL(file);

image.onload = function() {
image.onload = function () {
const width = defaultImageWidth;
const height = (defaultImageWidth * image.naturalHeight) / image.naturalWidth;
resolve(
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/utils/line-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export function getRatioByPoint(points: Point[], point: Point) {

export const removeDuplicatePoints = (points: Point[]) => {
const newArray: Point[] = [];
points.forEach(point => {
const index = newArray.findIndex(otherPoint => {
points.forEach((point) => {
const index = newArray.findIndex((otherPoint) => {
return Point.isEquals(point, otherPoint);
});
if (index === -1) newArray.push(point);
Expand Down
20 changes: 10 additions & 10 deletions packages/common/src/utils/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const getFirstTextManage = (element: PlaitElement) => {
};

export const getTextEditorsByElement: TextInterface['getTextEditorsByElement'] = (element: PlaitElement) => {
return getTextManages(element).map(manage => {
return getTextManages(element).map((manage) => {
return manage.editor;
});
};
Expand All @@ -42,7 +42,7 @@ export const getFirstTextEditor: TextInterface['getFirstTextEditor'] = (element:
export const findFirstTextEditor: TextInterface['findFirstTextEditor'] = (board: PlaitBoard) => {
const selectedElements = getSelectedElements(board);
let firstEditor: Editor | null = null;
selectedElements.forEach(element => {
selectedElements.forEach((element) => {
const editors = getTextEditorsByElement(element);
if (!firstEditor && editors && editors.length > 0) {
firstEditor = editors[0];
Expand All @@ -53,12 +53,12 @@ export const findFirstTextEditor: TextInterface['findFirstTextEditor'] = (board:

export const getElementsText = (elements: PlaitElement[]) => {
return elements
.map(item => {
.map((item) => {
try {
const editors = getTextEditorsByElement(item);
if (editors.length) {
return editors
.map(editor => {
.map((editor) => {
const textsEntry = Node.texts(editor);
return Array.from(textsEntry).reduce((total, text) => (total += text[0].text), '');
})
Expand All @@ -69,22 +69,22 @@ export const getElementsText = (elements: PlaitElement[]) => {
return '';
}
})
.filter(item => item)
.filter((item) => item)
.join(' ');
};

export const getTextEditors: TextInterface['getTextEditors'] = (board: PlaitBoard, elements?: PlaitElement[]) => {
const selectedElements = elements || getSelectedElements(board);
if (selectedElements.length) {
const textManages: TextManage[] = [];
selectedElements.forEach(item => {
selectedElements.forEach((item) => {
textManages.push(...getTextManages(item));
});
const editingTextManage = textManages.find(textManage => textManage.isEditing);
const editingTextManage = textManages.find((textManage) => textManage.isEditing);
if (editingTextManage) {
return [editingTextManage.editor];
}
return textManages.map(item => {
return textManages.map((item) => {
return item.editor;
});
}
Expand All @@ -94,10 +94,10 @@ export const getTextEditors: TextInterface['getTextEditors'] = (board: PlaitBoar
export const getEditingTextEditor: TextInterface['getEditingTextEditor'] = (board: PlaitBoard, elements?: PlaitElement[]) => {
const selectedElements = elements || getSelectedElements(board);
const textManages: TextManage[] = [];
selectedElements.forEach(item => {
selectedElements.forEach((item) => {
textManages.push(...getTextManages(item));
});
const editingTextManage = textManages.find(textManage => textManage.isEditing);
const editingTextManage = textManages.find((textManage) => textManage.isEditing);
if (editingTextManage) {
return editingTextManage.editor;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/utils/vector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ describe('vector', () => {
let start = [0, 0] as Point;
let end = [40, 30] as Point;
const unitVector = getUnitVectorByPointAndPoint(start, end);
expect(unitVector[0]).toEqual(4/5);
expect(unitVector[1]).toEqual(3/5)
expect(unitVector[0]).toEqual(4 / 5);
expect(unitVector[1]).toEqual(3 / 5);
});
});
describe('get point on vector by vector and vector component', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ export class PlaitBoardContext {
private uploadingFiles: ImageEntry[] = [];

getUploadingFile(url: string) {
return this.uploadingFiles.find(file => file.url === url);
return this.uploadingFiles.find((file) => file.url === url);
}

setUploadingFile(file: ImageEntry) {
return this.uploadingFiles.push(file);
}

removeUploadingFile(fileEntry: ImageEntry) {
this.uploadingFiles = this.uploadingFiles.filter(file => file.url !== fileEntry.url);
this.uploadingFiles = this.uploadingFiles.filter((file) => file.url !== fileEntry.url);
}
}
4 changes: 1 addition & 3 deletions packages/core/src/core/element/element-ref.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
* store the instance ref for element
*/
export interface PlaitElementRef {
}

export interface PlaitElementRef {}
16 changes: 5 additions & 11 deletions packages/core/src/core/list-render.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
describe('mountElementG', () => {
describe('default', () => {
it('container g should have correct position', () => {

});
it('children element should have a root container g', () => {

});
it(`children element's g should have been before of it's parent element container g`, () => {

});
it('container g should have correct position', () => {});
it('children element should have a root container g', () => {});
it(`children element's g should have been before of it's parent element container g`, () => {});
it(`should add to correct position when insert a element`, () => {});
it(`should add to correct position when insert a child element`, () => {});
it(`should add to correct position when insert a element at start position`, () => {})
it(`should add to correct position when insert a child element at start position`, () => {})
it(`should add to correct position when insert a element at start position`, () => {});
it(`should add to correct position when insert a child element at start position`, () => {});
});
describe('move', () => {
it('should move container g to correct when move element', () => {});
Expand Down
Loading
Loading