-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #617 from tuanchauict/serialization-js
Define jsonizable for SerializableLineConnector SerializableLineConnector SerializableTextExtra SerializableRectExtra
- Loading branch information
Showing
10 changed files
with
514 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
monosketch-svelte/src/lib/mono/shape/serialization/connector.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2024, tuanchauict | ||
*/ | ||
|
||
import { describe, expect, it } from "vitest"; | ||
import { SerializableLineConnector } from "./connector"; | ||
import { LineAnchor } from "$mono/shape/shape/linehelper"; | ||
import { Point, PointF } from "$libs/graphics-geo/point"; | ||
|
||
describe("SerializableLineConnector", () => { | ||
it("should serialize to JSON correctly", () => { | ||
const original = SerializableLineConnector.create({ | ||
lineId: "line-1", | ||
anchor: LineAnchor.START, | ||
targetId: "shape-1", | ||
ratio: new PointF(0.5, 0.5), | ||
offset: new Point(10, 20), | ||
}); | ||
|
||
// @ts-ignore | ||
const json = original.toJson(); | ||
console.log(json); | ||
expect(json).toStrictEqual({ i: 'line-1', a: 1, t: 'shape-1', r: '0.5|0.5', o: '10|20' }) | ||
}); | ||
|
||
it("should deserialize from JSON correctly", () => { | ||
const json = { i: 'line-2', a: 1, t: 'shape-2', r: '0.25|0.25', o: '30|40' }; | ||
// @ts-ignore | ||
const deserialized = SerializableLineConnector.fromJson(json); | ||
|
||
expect(deserialized).toStrictEqual( | ||
SerializableLineConnector.create( | ||
{ | ||
lineId: "line-2", | ||
anchor: LineAnchor.START, | ||
targetId: "shape-2", | ||
ratio: new PointF(0.25, 0.25), | ||
offset: new Point(30, 40), | ||
} | ||
) | ||
) | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
monosketch-svelte/src/lib/mono/shape/serialization/connector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (c) 2024, tuanchauict | ||
*/ | ||
|
||
import { Point, PointF } from "$libs/graphics-geo/point"; | ||
import { Jsonizable, Serializer, SerialName } from "$mono/shape/serialization/serializable"; | ||
import { AnchorSerializer, PointFSerializer, PointSerializer } from "$mono/shape/serialization/serializers"; | ||
import { LineAnchor } from "$mono/shape/shape/linehelper"; | ||
|
||
@Jsonizable | ||
export class SerializableLineConnector { | ||
@SerialName("i") | ||
public lineId: string = ''; | ||
@SerialName("a") | ||
@Serializer(AnchorSerializer) | ||
public anchor: LineAnchor = LineAnchor.START; | ||
@SerialName("t") | ||
public targetId: string = ''; | ||
@SerialName("r") | ||
@Serializer(PointFSerializer) | ||
public ratio: PointF = PointF.ZERO; | ||
@SerialName("o") | ||
@Serializer(PointSerializer) | ||
public offset: Point = Point.ZERO; | ||
|
||
private constructor() { | ||
} | ||
|
||
static create( | ||
{ | ||
lineId, | ||
anchor, | ||
targetId, | ||
ratio, | ||
offset, | ||
}: { | ||
lineId: string; | ||
anchor: LineAnchor; | ||
targetId: string; | ||
ratio: PointF; | ||
offset: Point; | ||
}): SerializableLineConnector { | ||
const result = new SerializableLineConnector(); | ||
result.lineId = lineId; | ||
result.anchor = anchor; | ||
result.targetId = targetId; | ||
result.ratio = ratio; | ||
result.offset = offset; | ||
return result; | ||
} | ||
} |
177 changes: 177 additions & 0 deletions
177
monosketch-svelte/src/lib/mono/shape/serialization/extra.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* Copyright (c) 2024, tuanchauict | ||
*/ | ||
|
||
import { describe, expect, it } from "vitest"; | ||
import { SerializableLineExtra, SerializableRectExtra, SerializableTextExtra } from './extras'; | ||
|
||
describe('SerializableRectExtra', () => { | ||
it('should create a SerializableRectExtra instance correctly', () => { | ||
const original = SerializableRectExtra.create({ | ||
isFillEnabled: true, | ||
userSelectedFillStyleId: 'fillStyle1', | ||
isBorderEnabled: true, | ||
userSelectedBorderStyleId: 'borderStyle1', | ||
dashPattern: 'dashPattern1', | ||
corner: 'corner1', | ||
}); | ||
|
||
// @ts-ignore | ||
const json = original.toJson(); | ||
console.log(json); | ||
expect(json).toStrictEqual({ | ||
fe: true, | ||
fu: 'fillStyle1', | ||
be: true, | ||
bu: 'borderStyle1', | ||
du: 'dashPattern1', | ||
rc: 'corner1', | ||
}); | ||
}); | ||
|
||
it('should serialize and deserialize correctly', () => { | ||
const json = { | ||
fe: false, | ||
fu: 'fillStyle2', | ||
be: false, | ||
bu: 'borderStyle2', | ||
du: 'dashPattern3', | ||
rc: 'corner4', | ||
} | ||
|
||
// @ts-ignore | ||
const deserialized = SerializableRectExtra.fromJson(json); | ||
expect(deserialized).toStrictEqual( | ||
SerializableRectExtra.create({ | ||
isFillEnabled: false, | ||
userSelectedFillStyleId: 'fillStyle2', | ||
isBorderEnabled: false, | ||
userSelectedBorderStyleId: 'borderStyle2', | ||
dashPattern: 'dashPattern3', | ||
corner: 'corner4', | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe('SerializableTextExtra', () => { | ||
it('should create a SerializableTextExtra instance correctly', () => { | ||
const original = SerializableTextExtra.create({ | ||
boundExtra: SerializableRectExtra.create({ | ||
isFillEnabled: true, | ||
userSelectedFillStyleId: 'fillStyle1', | ||
isBorderEnabled: true, | ||
userSelectedBorderStyleId: 'borderStyle1', | ||
dashPattern: 'dashPattern1', | ||
corner: 'corner1', | ||
}), | ||
textHorizontalAlign: 1, | ||
textVerticalAlign: 2, | ||
}); | ||
|
||
// @ts-ignore | ||
const json = original.toJson(); | ||
console.log(json); | ||
expect(json).toStrictEqual({ | ||
be: { | ||
fe: true, | ||
fu: 'fillStyle1', | ||
be: true, | ||
bu: 'borderStyle1', | ||
du: 'dashPattern1', | ||
rc: 'corner1', | ||
}, | ||
tha: 1, | ||
tva: 2, | ||
}); | ||
}); | ||
|
||
it('should serialize and deserialize correctly', () => { | ||
const json = { | ||
be: { | ||
fe: false, | ||
fu: 'fillStyle2', | ||
be: false, | ||
bu: 'borderStyle2', | ||
du: 'dashPattern3', | ||
rc: 'corner4', | ||
}, | ||
tha: 3, | ||
tva: 4, | ||
} | ||
|
||
// @ts-ignore | ||
const deserialized = SerializableTextExtra.fromJson(json); | ||
expect(deserialized).toStrictEqual( | ||
SerializableTextExtra.create({ | ||
boundExtra: SerializableRectExtra.create({ | ||
isFillEnabled: false, | ||
userSelectedFillStyleId: 'fillStyle2', | ||
isBorderEnabled: false, | ||
userSelectedBorderStyleId: 'borderStyle2', | ||
dashPattern: 'dashPattern3', | ||
corner: 'corner4', | ||
}), | ||
textHorizontalAlign: 3, | ||
textVerticalAlign: 4, | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe('SerializableLineExtra', () => { | ||
it('should create a SerializableLineExtra instance correctly', () => { | ||
const original = SerializableLineExtra.create({ | ||
isStrokeEnabled: true, | ||
userSelectedStrokeStyleId: 'strokeStyle1', | ||
isStartAnchorEnabled: true, | ||
userSelectedStartAnchorId: 'startAnchor1', | ||
isEndAnchorEnabled: true, | ||
userSelectedEndAnchorId: 'endAnchor1', | ||
dashPattern: 'dashPattern1', | ||
isRoundedCorner: true, | ||
}); | ||
|
||
// @ts-ignore | ||
const json = original.toJson(); | ||
console.log(json); | ||
expect(json).toStrictEqual({ | ||
se: true, | ||
su: 'strokeStyle1', | ||
ase: true, | ||
asu: 'startAnchor1', | ||
aee: true, | ||
aeu: 'endAnchor1', | ||
du: 'dashPattern1', | ||
rc: true, | ||
}); | ||
}); | ||
|
||
it('should serialize and deserialize correctly', () => { | ||
const json = { | ||
se: false, | ||
su: 'strokeStyle2', | ||
ase: false, | ||
asu: 'startAnchor2', | ||
aee: false, | ||
aeu: 'endAnchor2', | ||
du: 'dashPattern2', | ||
rc: false, | ||
} | ||
|
||
// @ts-ignore | ||
const deserialized = SerializableLineExtra.fromJson(json); | ||
expect(deserialized).toStrictEqual( | ||
SerializableLineExtra.create({ | ||
isStrokeEnabled: false, | ||
userSelectedStrokeStyleId: 'strokeStyle2', | ||
isStartAnchorEnabled: false, | ||
userSelectedStartAnchorId: 'startAnchor2', | ||
isEndAnchorEnabled: false, | ||
userSelectedEndAnchorId: 'endAnchor2', | ||
dashPattern: 'dashPattern2', | ||
isRoundedCorner: false, | ||
}) | ||
); | ||
}); | ||
}); |
Oops, something went wrong.