@media-quest/engine 0.0.21 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/package.json +1 -1
  2. package/src/Delement/DElement.dto.ts +5 -5
  3. package/src/Delement/DElement.ts +88 -88
  4. package/src/Delement/DImg.ts +39 -39
  5. package/src/Delement/DStyle-utils.ts +616 -616
  6. package/src/Delement/DStyle.ts +165 -165
  7. package/src/Delement/DText.ts +13 -13
  8. package/src/Delement/Ddiv.ts +25 -25
  9. package/src/Delement/button-click-action.ts +35 -35
  10. package/src/Delement/css.spec.ts +36 -36
  11. package/src/Delement/css.ts +46 -46
  12. package/src/Delement/element-factory.ts +40 -40
  13. package/src/common/DMaybe.ts +46 -46
  14. package/src/common/DTimestamp.ts +20 -20
  15. package/src/common/DTmestamp.spec.ts +11 -11
  16. package/src/common/result.ts +41 -41
  17. package/src/engine/SchemaDto.ts +24 -24
  18. package/src/engine/SchemaEngine.ts +150 -150
  19. package/src/engine/SchemaResult.ts +10 -10
  20. package/src/engine/dplayer.spec.ts +91 -91
  21. package/src/engine/dplayer.ts +104 -104
  22. package/src/engine/history-que.spec.ts +67 -67
  23. package/src/engine/history-que.ts +17 -17
  24. package/src/engine/next-que.spec.ts +121 -121
  25. package/src/engine/next-que.ts +101 -101
  26. package/src/engine/page-que-ruleengine-action.ts +6 -6
  27. package/src/engine/scale.spec.ts +38 -38
  28. package/src/engine/scale.ts +70 -70
  29. package/src/events/mq-events.ts +63 -63
  30. package/src/page/Page.ts +182 -182
  31. package/src/page/media-player.ts +117 -117
  32. package/src/page/page-component.ts +113 -113
  33. package/src/page/page-result.ts +11 -11
  34. package/src/page/task-manager.ts +240 -240
  35. package/src/page/task-state.ts +55 -55
  36. package/src/page/task.ts +90 -90
  37. package/src/public-api.ts +26 -26
  38. package/src/rules/__test__/complex-condition.spec.ts +15 -15
  39. package/src/rules/__test__/conditon.spec.ts +124 -124
  40. package/src/rules/__test__/numeric-condition.spec.ts +84 -84
  41. package/src/rules/__test__/rule-engine.spec.ts +348 -348
  42. package/src/rules/__test__/rule-evaluation.spec.ts +140 -140
  43. package/src/rules/__test__/string-condition.spec.ts +41 -41
  44. package/src/rules/condition.ts +191 -191
  45. package/src/rules/fact.ts +18 -18
  46. package/src/rules/rule-engine.ts +45 -45
  47. package/src/rules/rule.ts +40 -40
  48. package/src/utils/DUtil.ts +116 -116
  49. package/src/utils/ID.spec.ts +39 -39
  50. package/src/utils/ID.ts +73 -73
  51. package/tsconfig.json +19 -19
@@ -1,121 +1,121 @@
1
- import { NextQue } from "./next-que";
2
- import { PageID } from "../utils/ID";
3
- import { PageDto } from "../page/Page";
4
-
5
- const tag1 = "tag1";
6
- const tag2 = "tag2";
7
- const tag3 = "tag3";
8
-
9
- let que = new NextQue();
10
- const createPage = (tags: string[]): PageDto => {
11
- const dto: PageDto = {
12
- id: PageID.create(),
13
- prefix: "prefix",
14
- staticElements: [],
15
- background: "white",
16
- components: [],
17
- initialTasks: [],
18
- tags,
19
- };
20
- return dto;
21
- };
22
-
23
- const p1 = createPage([tag1]);
24
- const p2 = createPage([]);
25
- const p3 = createPage([tag2]);
26
- const p4 = createPage([]);
27
- const p5 = createPage([tag3, tag2]);
28
- const p6 = createPage([tag3]);
29
- const all = [p1, p2, p3, p4, p5, p6];
30
-
31
- beforeEach(() => {
32
- que = new NextQue();
33
- });
34
-
35
- describe("NextQue testing", () => {
36
- it("Can pop", () => {
37
- que.resetQue(all);
38
- expect(que.pop()).toBe(p1);
39
- expect(que.pop()).toBe(p2);
40
- expect(que.size).toBe(all.length - 2);
41
- });
42
-
43
- it("Can remove by tag", () => {
44
- que.resetQue(all);
45
- expect(que.size).toBe(6);
46
- que.removeByTag(tag3);
47
- expect(que.size).toBe(4);
48
- expect(que.peek()).toBe(p1);
49
- que.pop();
50
- expect(que.peek()).toBe(p2);
51
- expect(que.size).toBe(3);
52
- });
53
-
54
- it("Can remove by tags[]", () => {
55
- que.resetQue(all);
56
- que.removeByTag([tag3, tag2, tag1]);
57
- expect(que.size).toBe(2);
58
- });
59
-
60
- it("Can remove by pageIds[]", () => {
61
- que.resetQue(all);
62
- que.removeByPageId([p1.id, p2.id, p3.id]);
63
- expect(que.pop()).toBe(p4);
64
- expect(que.size).toBe(2);
65
- });
66
-
67
- it("Can remove by pageId", () => {
68
- que.resetQue(all);
69
- que.removeByPageId(p1.id);
70
- expect(que.peek()).toBe(p2);
71
- que.pop();
72
- expect(que.peek()).toBe(p3);
73
- expect(que.size).toBe(4);
74
- que.removeByPageId(p3.id);
75
- expect(que.size).toBe(3);
76
- });
77
-
78
- it("Can set pages in constuctor", () => {
79
- que = new NextQue(all);
80
- expect(que.size).toBe(all.length);
81
- });
82
-
83
- it("Can set pages in constuctor", () => {
84
- que = new NextQue(all);
85
- expect(que.size).toBe(all.length);
86
- });
87
-
88
- it("Can reset que to start over.", () => {
89
- que = new NextQue(all);
90
- expect(que.size).toBe(all.length);
91
- });
92
-
93
- it("Can insert new pages in the middle of the test.", () => {
94
- que = new NextQue(all);
95
- que.pop();
96
- que.pop();
97
- que.pop();
98
- expect(que.peek()).toBe(p4);
99
- que.insertAsNextByForce([p1, p2]);
100
- expect(que.peek()).toBe(p1);
101
- expect(que.size).toBe(5);
102
- expect(que.pop()).toBe(p1);
103
- expect(que.pop()).toBe(p2);
104
- expect(que.pop()).toBe(p4);
105
- });
106
-
107
- it("Can jump to page by id", () => {
108
- que = new NextQue(all);
109
- que.pop();
110
- que.jumpToPageById(p5.id);
111
- // expect(que.size).toBe(5);
112
- expect(que.peek()).toBe(p5);
113
- que.jumpToPageById("noncence");
114
- expect(que.peek()).toBe(p5);
115
- // CAn not jump back
116
- que.jumpToPageById(p2.id);
117
- expect(que.peek()).toBe(p5);
118
- que.jumpToPageById(p6.id);
119
- expect(que.peek()).toBe(p6);
120
- });
121
- });
1
+ import { NextQue } from "./next-que";
2
+ import { PageID } from "../utils/ID";
3
+ import { PageDto } from "../page/Page";
4
+
5
+ const tag1 = "tag1";
6
+ const tag2 = "tag2";
7
+ const tag3 = "tag3";
8
+
9
+ let que = new NextQue();
10
+ const createPage = (tags: string[]): PageDto => {
11
+ const dto: PageDto = {
12
+ id: PageID.create(),
13
+ prefix: "prefix",
14
+ staticElements: [],
15
+ background: "white",
16
+ components: [],
17
+ initialTasks: [],
18
+ tags,
19
+ };
20
+ return dto;
21
+ };
22
+
23
+ const p1 = createPage([tag1]);
24
+ const p2 = createPage([]);
25
+ const p3 = createPage([tag2]);
26
+ const p4 = createPage([]);
27
+ const p5 = createPage([tag3, tag2]);
28
+ const p6 = createPage([tag3]);
29
+ const all = [p1, p2, p3, p4, p5, p6];
30
+
31
+ beforeEach(() => {
32
+ que = new NextQue();
33
+ });
34
+
35
+ describe("NextQue testing", () => {
36
+ it("Can pop", () => {
37
+ que.resetQue(all);
38
+ expect(que.pop()).toBe(p1);
39
+ expect(que.pop()).toBe(p2);
40
+ expect(que.size).toBe(all.length - 2);
41
+ });
42
+
43
+ it("Can remove by tag", () => {
44
+ que.resetQue(all);
45
+ expect(que.size).toBe(6);
46
+ que.removeByTag(tag3);
47
+ expect(que.size).toBe(4);
48
+ expect(que.peek()).toBe(p1);
49
+ que.pop();
50
+ expect(que.peek()).toBe(p2);
51
+ expect(que.size).toBe(3);
52
+ });
53
+
54
+ it("Can remove by tags[]", () => {
55
+ que.resetQue(all);
56
+ que.removeByTag([tag3, tag2, tag1]);
57
+ expect(que.size).toBe(2);
58
+ });
59
+
60
+ it("Can remove by pageIds[]", () => {
61
+ que.resetQue(all);
62
+ que.removeByPageId([p1.id, p2.id, p3.id]);
63
+ expect(que.pop()).toBe(p4);
64
+ expect(que.size).toBe(2);
65
+ });
66
+
67
+ it("Can remove by pageId", () => {
68
+ que.resetQue(all);
69
+ que.removeByPageId(p1.id);
70
+ expect(que.peek()).toBe(p2);
71
+ que.pop();
72
+ expect(que.peek()).toBe(p3);
73
+ expect(que.size).toBe(4);
74
+ que.removeByPageId(p3.id);
75
+ expect(que.size).toBe(3);
76
+ });
77
+
78
+ it("Can set pages in constuctor", () => {
79
+ que = new NextQue(all);
80
+ expect(que.size).toBe(all.length);
81
+ });
82
+
83
+ it("Can set pages in constuctor", () => {
84
+ que = new NextQue(all);
85
+ expect(que.size).toBe(all.length);
86
+ });
87
+
88
+ it("Can reset que to start over.", () => {
89
+ que = new NextQue(all);
90
+ expect(que.size).toBe(all.length);
91
+ });
92
+
93
+ it("Can insert new pages in the middle of the test.", () => {
94
+ que = new NextQue(all);
95
+ que.pop();
96
+ que.pop();
97
+ que.pop();
98
+ expect(que.peek()).toBe(p4);
99
+ que.insertAsNextByForce([p1, p2]);
100
+ expect(que.peek()).toBe(p1);
101
+ expect(que.size).toBe(5);
102
+ expect(que.pop()).toBe(p1);
103
+ expect(que.pop()).toBe(p2);
104
+ expect(que.pop()).toBe(p4);
105
+ });
106
+
107
+ it("Can jump to page by id", () => {
108
+ que = new NextQue(all);
109
+ que.pop();
110
+ que.jumpToPageById(p5.id);
111
+ // expect(que.size).toBe(5);
112
+ expect(que.peek()).toBe(p5);
113
+ que.jumpToPageById("noncence");
114
+ expect(que.peek()).toBe(p5);
115
+ // CAn not jump back
116
+ que.jumpToPageById(p2.id);
117
+ expect(que.peek()).toBe(p5);
118
+ que.jumpToPageById(p6.id);
119
+ expect(que.peek()).toBe(p6);
120
+ });
121
+ });
@@ -1,101 +1,101 @@
1
- import { PageDto } from "../page/Page";
2
-
3
- export class NextQue {
4
- private originalOrder: ReadonlyArray<string> = [];
5
- private allPages: PageDto[] = [];
6
- private excludedTags = new Set<string>();
7
- private excludedByPageId = new Set<string>();
8
- private remaining: PageDto[] = [];
9
- constructor(pages: PageDto[] = []) {
10
- this.resetQue(pages);
11
- }
12
-
13
- /**
14
- * Will reset que with the new pages.
15
- * @param pages
16
- */
17
- resetQue(pages: PageDto[]) {
18
- this.allPages = [...pages];
19
- this.remaining = [...pages];
20
- this.excludedTags = new Set();
21
- this.excludedByPageId = new Set();
22
- this.originalOrder = this.allPages.map((p) => p.id);
23
- }
24
-
25
- pop(): PageDto | false {
26
- const next = this.remaining.shift();
27
- // TODO CLONE??
28
- return next ?? false;
29
- }
30
-
31
- peek(): PageDto | false {
32
- const next = this.remaining[0];
33
- return next ?? false;
34
- }
35
-
36
- jumpToPageById(pageId: string): boolean {
37
- const index = this.remaining.findIndex((p) => p.id === pageId);
38
- if (index < 0) {
39
- return false;
40
- }
41
- this.remaining = this.remaining.slice(index);
42
- return true;
43
- }
44
-
45
- removeByTag(tag: string | string[]) {
46
- if (Array.isArray(tag)) {
47
- tag.forEach((tag) => {
48
- this.excludedTags.add(tag);
49
- });
50
- } else {
51
- this.excludedTags.add(tag);
52
- }
53
- this.filterRemaining();
54
- }
55
-
56
- /**
57
- * Will not be included
58
- * @param pages
59
- */
60
- insertAsNextByForce(pages: PageDto[]) {
61
- this.remaining.unshift(...pages);
62
- }
63
-
64
- removeByPageId(pageId: string | string[]) {
65
- if (Array.isArray(pageId)) {
66
- pageId.forEach((id) => {
67
- this.excludedByPageId.add(id);
68
- });
69
- } else {
70
- this.excludedByPageId.add(pageId);
71
- }
72
- this.filterRemaining();
73
- // this.excludedByPageId.add(pageId);
74
- }
75
-
76
- private filterRemaining() {
77
- this.remaining = this.remaining.filter((p) => {
78
- const tags = p.tags ?? [];
79
- const isIncluededByTag = !tags.some((tag) => this.excludedTags.has(tag));
80
- const isIncludedByPageId = !this.excludedByPageId.has(p.id);
81
- return isIncludedByPageId && isIncluededByTag;
82
- });
83
- }
84
- get isEmpty(): boolean {
85
- return this.remaining.length === 0;
86
- }
87
-
88
- /**
89
- * Total number of pages left in que
90
- */
91
- get size(): number {
92
- return this.remaining.length;
93
- }
94
-
95
- /**
96
- * Total number of pages in test
97
- */
98
- get pageCount(): number {
99
- return this.originalOrder.length;
100
- }
101
- }
1
+ import { PageDto } from "../page/Page";
2
+
3
+ export class NextQue {
4
+ private originalOrder: ReadonlyArray<string> = [];
5
+ private allPages: PageDto[] = [];
6
+ private excludedTags = new Set<string>();
7
+ private excludedByPageId = new Set<string>();
8
+ private remaining: PageDto[] = [];
9
+ constructor(pages: PageDto[] = []) {
10
+ this.resetQue(pages);
11
+ }
12
+
13
+ /**
14
+ * Will reset que with the new pages.
15
+ * @param pages
16
+ */
17
+ resetQue(pages: PageDto[]) {
18
+ this.allPages = [...pages];
19
+ this.remaining = [...pages];
20
+ this.excludedTags = new Set();
21
+ this.excludedByPageId = new Set();
22
+ this.originalOrder = this.allPages.map((p) => p.id);
23
+ }
24
+
25
+ pop(): PageDto | false {
26
+ const next = this.remaining.shift();
27
+ // TODO CLONE??
28
+ return next ?? false;
29
+ }
30
+
31
+ peek(): PageDto | false {
32
+ const next = this.remaining[0];
33
+ return next ?? false;
34
+ }
35
+
36
+ jumpToPageById(pageId: string): boolean {
37
+ const index = this.remaining.findIndex((p) => p.id === pageId);
38
+ if (index < 0) {
39
+ return false;
40
+ }
41
+ this.remaining = this.remaining.slice(index);
42
+ return true;
43
+ }
44
+
45
+ removeByTag(tag: string | string[]) {
46
+ if (Array.isArray(tag)) {
47
+ tag.forEach((tag) => {
48
+ this.excludedTags.add(tag);
49
+ });
50
+ } else {
51
+ this.excludedTags.add(tag);
52
+ }
53
+ this.filterRemaining();
54
+ }
55
+
56
+ /**
57
+ * Will not be included
58
+ * @param pages
59
+ */
60
+ insertAsNextByForce(pages: PageDto[]) {
61
+ this.remaining.unshift(...pages);
62
+ }
63
+
64
+ removeByPageId(pageId: string | string[]) {
65
+ if (Array.isArray(pageId)) {
66
+ pageId.forEach((id) => {
67
+ this.excludedByPageId.add(id);
68
+ });
69
+ } else {
70
+ this.excludedByPageId.add(pageId);
71
+ }
72
+ this.filterRemaining();
73
+ // this.excludedByPageId.add(pageId);
74
+ }
75
+
76
+ private filterRemaining() {
77
+ this.remaining = this.remaining.filter((p) => {
78
+ const tags = p.tags ?? [];
79
+ const isIncluededByTag = !tags.some((tag) => this.excludedTags.has(tag));
80
+ const isIncludedByPageId = !this.excludedByPageId.has(p.id);
81
+ return isIncludedByPageId && isIncluededByTag;
82
+ });
83
+ }
84
+ get isEmpty(): boolean {
85
+ return this.remaining.length === 0;
86
+ }
87
+
88
+ /**
89
+ * Total number of pages left in que
90
+ */
91
+ get size(): number {
92
+ return this.remaining.length;
93
+ }
94
+
95
+ /**
96
+ * Total number of pages in test
97
+ */
98
+ get pageCount(): number {
99
+ return this.originalOrder.length;
100
+ }
101
+ }
@@ -1,6 +1,6 @@
1
- import { PageID } from "../utils/ID";
2
-
3
- export type RuleActionPageQue =
4
- | { kind: "excludeByTag"; tagIds: string[] }
5
- | { kind: "excludeByPageId"; pageIds: Array<PageID> }
6
- | { kind: "jumpToPage"; pageId: PageID };
1
+ import { PageID } from "../utils/ID";
2
+
3
+ export type RuleActionPageQue =
4
+ | { kind: "excludeByTag"; tagIds: string[] }
5
+ | { kind: "excludeByPageId"; pageIds: Array<PageID> }
6
+ | { kind: "jumpToPage"; pageId: PageID };
@@ -1,38 +1,38 @@
1
- import { Scale, ScaleService } from "./scale";
2
-
3
- describe("Scale works", () => {
4
- test("scaleService", () => {
5
- const scaleService = new ScaleService({
6
- baseHeight: 1300,
7
- baseWidth: 1024,
8
- containerWidth: 600,
9
- containerHeight: 650,
10
- });
11
- expect(scaleService.scale).toBe(0.5);
12
- const unsub = scaleService.onChange((scale) => {
13
- expect(scale).toBe(0.5);
14
- }, "ScaleService test-function");
15
- unsub();
16
-
17
- scaleService.setContainerBounds({ height: 1170, width: 1000 });
18
-
19
- expect(scaleService.scale).toBe(0.9);
20
- });
21
- test("scaleFn", () => {
22
- const scaleFunction = Scale.calc(1200, 1200);
23
- expect(scaleFunction({ height: 600, width: 600 })).toBe(0.5);
24
- expect(scaleFunction({ height: 900, width: 600 })).toBe(0.5);
25
- expect(scaleFunction({ height: 400, width: 600 })).toBe(4 / 12);
26
- expect(scaleFunction({ height: 900, width: 300 })).toBe(0.25);
27
- expect(scaleFunction({ height: 300, width: 400 })).toBe(0.25);
28
- expect(scaleFunction({ height: 0, width: 0 })).toBe(0);
29
- const scaleFunction2 = Scale.calc(1200, 1024);
30
- expect(scaleFunction2({ height: 600, width: 6000 })).toBe(0.5);
31
- expect(scaleFunction2({ height: 400, width: 800 })).toBe(4 / 12);
32
- expect(scaleFunction2({ height: 800, width: 512 })).toBe(0.5);
33
- const scale = Scale.scaleFunctionCreator(0.5);
34
-
35
- expect(scale(500)).toBe(250);
36
- expect(scale(200)).toBe(100);
37
- });
38
- });
1
+ import { Scale, ScaleService } from "./scale";
2
+
3
+ describe("Scale works", () => {
4
+ test("scaleService", () => {
5
+ const scaleService = new ScaleService({
6
+ baseHeight: 1300,
7
+ baseWidth: 1024,
8
+ containerWidth: 600,
9
+ containerHeight: 650,
10
+ });
11
+ expect(scaleService.scale).toBe(0.5);
12
+ const unsub = scaleService.onChange((scale) => {
13
+ expect(scale).toBe(0.5);
14
+ }, "ScaleService test-function");
15
+ unsub();
16
+
17
+ scaleService.setContainerBounds({ height: 1170, width: 1000 });
18
+
19
+ expect(scaleService.scale).toBe(0.9);
20
+ });
21
+ test("scaleFn", () => {
22
+ const scaleFunction = Scale.calc(1200, 1200);
23
+ expect(scaleFunction({ height: 600, width: 600 })).toBe(0.5);
24
+ expect(scaleFunction({ height: 900, width: 600 })).toBe(0.5);
25
+ expect(scaleFunction({ height: 400, width: 600 })).toBe(4 / 12);
26
+ expect(scaleFunction({ height: 900, width: 300 })).toBe(0.25);
27
+ expect(scaleFunction({ height: 300, width: 400 })).toBe(0.25);
28
+ expect(scaleFunction({ height: 0, width: 0 })).toBe(0);
29
+ const scaleFunction2 = Scale.calc(1200, 1024);
30
+ expect(scaleFunction2({ height: 600, width: 6000 })).toBe(0.5);
31
+ expect(scaleFunction2({ height: 400, width: 800 })).toBe(4 / 12);
32
+ expect(scaleFunction2({ height: 800, width: 512 })).toBe(0.5);
33
+ const scale = Scale.scaleFunctionCreator(0.5);
34
+
35
+ expect(scale(500)).toBe(250);
36
+ expect(scale(200)).toBe(100);
37
+ });
38
+ });
@@ -1,70 +1,70 @@
1
- export class ScaleService {
2
- private readonly baseHeight;
3
- private readonly baseWidth;
4
- private containerHeight = 1300;
5
- private containerWidth = 1300;
6
-
7
- get scale() {
8
- return this._scale;
9
- }
10
-
11
- get pageHeight() {
12
- return this.baseHeight * this.scale;
13
- }
14
- get pageWidth() {
15
- return this.baseWidth * this._scale;
16
- }
17
-
18
- private _scale = 1;
19
-
20
- private readonly subscribers = new Set<(scale: number) => void>();
21
-
22
- constructor(config: { baseHeight: number; baseWidth: number; containerHeight: number; containerWidth: number }) {
23
- this.baseHeight = config.baseHeight;
24
- this.baseWidth = config.baseWidth;
25
- this.containerHeight = config.containerHeight;
26
- this.containerWidth = config.containerWidth;
27
- this.updateScale();
28
- }
29
-
30
- setContainerBounds(bounds: { height: number; width: number }) {
31
- this.containerWidth = bounds.width;
32
- this.containerHeight = bounds.height;
33
- this.updateScale();
34
- }
35
- private updateScale() {
36
- const scaleFn = Scale.calc(this.baseHeight, this.baseWidth);
37
- const scale = scaleFn({ height: this.containerHeight, width: this.containerWidth });
38
- const hasChanged = this.scale !== scale;
39
- this._scale = scale;
40
- if (hasChanged) {
41
- this.subscribers.forEach((fn) => {
42
- fn(this._scale);
43
- });
44
- }
45
- }
46
-
47
- onChange(scaleChangeHandler: (scale: number) => void, subscriberId: string) {
48
- // console.log(subscriberId);
49
- this.subscribers.add(scaleChangeHandler);
50
- scaleChangeHandler(this._scale);
51
- return () => {
52
- this.subscribers.delete(scaleChangeHandler);
53
- };
54
- }
55
- }
56
- export namespace Scale {
57
- export const calc = (baseHeight: number, baseWidth: number) => {
58
- return (container: { height: number; width: number }) => {
59
- const heightRatio = container.height / baseHeight;
60
- const widthRatio = container.width / baseWidth;
61
- return Math.min(heightRatio, widthRatio);
62
- };
63
- };
64
-
65
- type ScalePosInput = { x: number; y: number; h: number; w: number };
66
-
67
- export const scaleFunctionCreator = (scale: number) => {
68
- return (value: number) => value * scale;
69
- };
70
- }
1
+ export class ScaleService {
2
+ private readonly baseHeight;
3
+ private readonly baseWidth;
4
+ private containerHeight = 1300;
5
+ private containerWidth = 1300;
6
+
7
+ get scale() {
8
+ return this._scale;
9
+ }
10
+
11
+ get pageHeight() {
12
+ return this.baseHeight * this.scale;
13
+ }
14
+ get pageWidth() {
15
+ return this.baseWidth * this._scale;
16
+ }
17
+
18
+ private _scale = 1;
19
+
20
+ private readonly subscribers = new Set<(scale: number) => void>();
21
+
22
+ constructor(config: { baseHeight: number; baseWidth: number; containerHeight: number; containerWidth: number }) {
23
+ this.baseHeight = config.baseHeight;
24
+ this.baseWidth = config.baseWidth;
25
+ this.containerHeight = config.containerHeight;
26
+ this.containerWidth = config.containerWidth;
27
+ this.updateScale();
28
+ }
29
+
30
+ setContainerBounds(bounds: { height: number; width: number }) {
31
+ this.containerWidth = bounds.width;
32
+ this.containerHeight = bounds.height;
33
+ this.updateScale();
34
+ }
35
+ private updateScale() {
36
+ const scaleFn = Scale.calc(this.baseHeight, this.baseWidth);
37
+ const scale = scaleFn({ height: this.containerHeight, width: this.containerWidth });
38
+ const hasChanged = this.scale !== scale;
39
+ this._scale = scale;
40
+ if (hasChanged) {
41
+ this.subscribers.forEach((fn) => {
42
+ fn(this._scale);
43
+ });
44
+ }
45
+ }
46
+
47
+ onChange(scaleChangeHandler: (scale: number) => void, subscriberId: string) {
48
+ // console.log(subscriberId);
49
+ this.subscribers.add(scaleChangeHandler);
50
+ scaleChangeHandler(this._scale);
51
+ return () => {
52
+ this.subscribers.delete(scaleChangeHandler);
53
+ };
54
+ }
55
+ }
56
+ export namespace Scale {
57
+ export const calc = (baseHeight: number, baseWidth: number) => {
58
+ return (container: { height: number; width: number }) => {
59
+ const heightRatio = container.height / baseHeight;
60
+ const widthRatio = container.width / baseWidth;
61
+ return Math.min(heightRatio, widthRatio);
62
+ };
63
+ };
64
+
65
+ type ScalePosInput = { x: number; y: number; h: number; w: number };
66
+
67
+ export const scaleFunctionCreator = (scale: number) => {
68
+ return (value: number) => value * scale;
69
+ };
70
+ }