@asyra/ui-context 0.2.0

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 (131) hide show
  1. package/.turbo/turbo-test$colon$ci.log +117 -0
  2. package/.turbo/turbo-test$colon$local.log +23 -0
  3. package/.turbo/turbo-test.log +0 -0
  4. package/README.md +1 -0
  5. package/coverage/base.css +224 -0
  6. package/coverage/block-navigation.js +87 -0
  7. package/coverage/clover.xml +538 -0
  8. package/coverage/coverage-final.json +7443 -0
  9. package/coverage/favicon.png +0 -0
  10. package/coverage/index.html +176 -0
  11. package/coverage/prettify.css +1 -0
  12. package/coverage/prettify.js +2 -0
  13. package/coverage/sort-arrow-sprite.png +0 -0
  14. package/coverage/sorter.js +196 -0
  15. package/coverage/src/index.html +146 -0
  16. package/coverage/src/index.ts.html +97 -0
  17. package/coverage/src/stores/index.html +146 -0
  18. package/coverage/src/stores/render.ts.html +106 -0
  19. package/coverage/src/stores/scene-tree.ts.html +790 -0
  20. package/coverage/src/stores/selection.ts.html +220 -0
  21. package/coverage/src/stores/system-context/index.html +146 -0
  22. package/coverage/src/stores/system-context/index.ts.html +88 -0
  23. package/coverage/src/stores/system-context/primary-tool.ts.html +109 -0
  24. package/coverage/src/stores/system-context/system-context.ts.html +85 -0
  25. package/coverage/src/subscribes/index.html +161 -0
  26. package/coverage/src/subscribes/index.ts.html +124 -0
  27. package/coverage/src/subscribes/render.ts.html +151 -0
  28. package/coverage/src/subscribes/scene-tree.ts.html +373 -0
  29. package/coverage/src/subscribes/selection.ts.html +253 -0
  30. package/coverage/src/subscribes/system-context/index.html +131 -0
  31. package/coverage/src/subscribes/system-context/index.ts.html +100 -0
  32. package/coverage/src/subscribes/system-context/primary-tool.ts.html +151 -0
  33. package/coverage/src/types.ts.html +124 -0
  34. package/coverage/src/ui-context.ts.html +568 -0
  35. package/dist/__tests__/ui-context.test.d.ts +2 -0
  36. package/dist/__tests__/ui-context.test.d.ts.map +1 -0
  37. package/dist/__tests__/ui-context.test.js +170 -0
  38. package/dist/__tests__/ui-context.test.js.map +1 -0
  39. package/dist/index.d.ts +4 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +4 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/stores/__tests__/render.test.d.ts +2 -0
  44. package/dist/stores/__tests__/render.test.d.ts.map +1 -0
  45. package/dist/stores/__tests__/render.test.js +30 -0
  46. package/dist/stores/__tests__/render.test.js.map +1 -0
  47. package/dist/stores/__tests__/selection.test.d.ts +2 -0
  48. package/dist/stores/__tests__/selection.test.d.ts.map +1 -0
  49. package/dist/stores/__tests__/selection.test.js +128 -0
  50. package/dist/stores/__tests__/selection.test.js.map +1 -0
  51. package/dist/stores/render.d.ts +4 -0
  52. package/dist/stores/render.d.ts.map +1 -0
  53. package/dist/stores/render.js +7 -0
  54. package/dist/stores/render.js.map +1 -0
  55. package/dist/stores/scene-tree.d.ts +35 -0
  56. package/dist/stores/scene-tree.d.ts.map +1 -0
  57. package/dist/stores/scene-tree.js +158 -0
  58. package/dist/stores/scene-tree.js.map +1 -0
  59. package/dist/stores/selection.d.ts +8 -0
  60. package/dist/stores/selection.d.ts.map +1 -0
  61. package/dist/stores/selection.js +38 -0
  62. package/dist/stores/selection.js.map +1 -0
  63. package/dist/stores/system-context/__tests__/primary-tool.test.d.ts +2 -0
  64. package/dist/stores/system-context/__tests__/primary-tool.test.d.ts.map +1 -0
  65. package/dist/stores/system-context/__tests__/primary-tool.test.js +27 -0
  66. package/dist/stores/system-context/__tests__/primary-tool.test.js.map +1 -0
  67. package/dist/stores/system-context/index.d.ts +2 -0
  68. package/dist/stores/system-context/index.d.ts.map +1 -0
  69. package/dist/stores/system-context/index.js +2 -0
  70. package/dist/stores/system-context/index.js.map +1 -0
  71. package/dist/stores/system-context/primary-tool.d.ts +5 -0
  72. package/dist/stores/system-context/primary-tool.d.ts.map +1 -0
  73. package/dist/stores/system-context/primary-tool.js +7 -0
  74. package/dist/stores/system-context/primary-tool.js.map +1 -0
  75. package/dist/stores/system-context/system-context.d.ts +1 -0
  76. package/dist/stores/system-context/system-context.d.ts.map +1 -0
  77. package/dist/stores/system-context/system-context.js +2 -0
  78. package/dist/stores/system-context/system-context.js.map +1 -0
  79. package/dist/subscribes/index.d.ts +5 -0
  80. package/dist/subscribes/index.d.ts.map +1 -0
  81. package/dist/subscribes/index.js +12 -0
  82. package/dist/subscribes/index.js.map +1 -0
  83. package/dist/subscribes/render.d.ts +2 -0
  84. package/dist/subscribes/render.d.ts.map +1 -0
  85. package/dist/subscribes/render.js +15 -0
  86. package/dist/subscribes/render.js.map +1 -0
  87. package/dist/subscribes/scene-tree.d.ts +5 -0
  88. package/dist/subscribes/scene-tree.d.ts.map +1 -0
  89. package/dist/subscribes/scene-tree.js +71 -0
  90. package/dist/subscribes/scene-tree.js.map +1 -0
  91. package/dist/subscribes/selection.d.ts +5 -0
  92. package/dist/subscribes/selection.d.ts.map +1 -0
  93. package/dist/subscribes/selection.js +42 -0
  94. package/dist/subscribes/selection.js.map +1 -0
  95. package/dist/subscribes/system-context/index.d.ts +2 -0
  96. package/dist/subscribes/system-context/index.d.ts.map +1 -0
  97. package/dist/subscribes/system-context/index.js +5 -0
  98. package/dist/subscribes/system-context/index.js.map +1 -0
  99. package/dist/subscribes/system-context/primary-tool.d.ts +4 -0
  100. package/dist/subscribes/system-context/primary-tool.d.ts.map +1 -0
  101. package/dist/subscribes/system-context/primary-tool.js +15 -0
  102. package/dist/subscribes/system-context/primary-tool.js.map +1 -0
  103. package/dist/types.d.ts +11 -0
  104. package/dist/types.d.ts.map +1 -0
  105. package/dist/types.js +2 -0
  106. package/dist/types.js.map +1 -0
  107. package/dist/ui-context.d.ts +28 -0
  108. package/dist/ui-context.d.ts.map +1 -0
  109. package/dist/ui-context.js +101 -0
  110. package/dist/ui-context.js.map +1 -0
  111. package/package.json +36 -0
  112. package/src/__tests__/ui-context.test.ts +220 -0
  113. package/src/index.ts +4 -0
  114. package/src/stores/__tests__/render.test.ts +38 -0
  115. package/src/stores/__tests__/selection.test.ts +190 -0
  116. package/src/stores/render.ts +7 -0
  117. package/src/stores/scene-tree.ts +235 -0
  118. package/src/stores/selection.ts +45 -0
  119. package/src/stores/system-context/__tests__/primary-tool.test.ts +37 -0
  120. package/src/stores/system-context/index.ts +1 -0
  121. package/src/stores/system-context/primary-tool.ts +8 -0
  122. package/src/stores/system-context/system-context.ts +0 -0
  123. package/src/subscribes/index.ts +13 -0
  124. package/src/subscribes/render.ts +22 -0
  125. package/src/subscribes/scene-tree.ts +96 -0
  126. package/src/subscribes/selection.ts +56 -0
  127. package/src/subscribes/system-context/index.ts +5 -0
  128. package/src/subscribes/system-context/primary-tool.ts +22 -0
  129. package/src/types.ts +13 -0
  130. package/src/ui-context.ts +161 -0
  131. package/tsconfig.json +17 -0
@@ -0,0 +1,45 @@
1
+ import selectionManager, { SelectionManager } from '@asyra/selection'
2
+ import { ComputedAttrs, SELECTION_TYPES } from '@asyra/utils'
3
+ import sceneTree from '@asyra/scene-tree'
4
+ import uiContext from '../ui-context'
5
+
6
+ export default class SelectionStore {
7
+ selectionManager: SelectionManager
8
+
9
+ constructor() {
10
+ this.selectionManager = selectionManager
11
+ }
12
+
13
+ updateSelection(type: SELECTION_TYPES) {
14
+ const selection = this.selectionManager.get(type)
15
+ if (!selection) {
16
+ return
17
+ }
18
+
19
+ const selectedIds = selection.getSelectedIds()
20
+
21
+ switch (type) {
22
+ case SELECTION_TYPES.ELEMENT: {
23
+ uiContext.updateElementSelection(selectedIds)
24
+ const allElementData = [...selectedIds].reduce((acc, elementId) => {
25
+ const element = sceneTree.getElementById(elementId)
26
+ if (!element) {
27
+ return acc
28
+ }
29
+
30
+ const elementData = element.getAllComputedData() as ComputedAttrs
31
+ acc.push(elementData)
32
+ return acc
33
+ }, [] as ComputedAttrs[])
34
+
35
+ if (selectedIds.size) {
36
+ uiContext.updateComputedProperties(allElementData)
37
+ }
38
+ break
39
+ }
40
+ case SELECTION_TYPES.VERTEX:
41
+ uiContext.updateVertexSelection(selectedIds)
42
+ break
43
+ }
44
+ }
45
+ }
@@ -0,0 +1,37 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest'
2
+ import { PrimaryToolStore } from '../primary-tool'
3
+ import * as UIContextModule from '../../../ui-context'
4
+ import { PrimaryToolType } from '@asyra/utils'
5
+
6
+ // Mock ui-context
7
+ vi.mock('../../../ui-context', () => ({
8
+ default: {
9
+ updatePrimaryTool: vi.fn()
10
+ }
11
+ }))
12
+
13
+ describe('PrimaryToolStore', () => {
14
+ let primaryToolStore: PrimaryToolStore
15
+
16
+ beforeEach(() => {
17
+ vi.clearAllMocks()
18
+ vi.resetAllMocks()
19
+ primaryToolStore = new PrimaryToolStore()
20
+ })
21
+
22
+ it('should update primary tool in ui context', () => {
23
+ primaryToolStore.updatePrimaryTool(PrimaryToolType.RECTANGLE)
24
+
25
+ expect(UIContextModule.default.updatePrimaryTool).toHaveBeenCalledWith(
26
+ PrimaryToolType.RECTANGLE
27
+ )
28
+ })
29
+
30
+ it('should update primary tool to SELECT', () => {
31
+ primaryToolStore.updatePrimaryTool(PrimaryToolType.SELECT)
32
+
33
+ expect(UIContextModule.default.updatePrimaryTool).toHaveBeenCalledWith(
34
+ PrimaryToolType.SELECT
35
+ )
36
+ })
37
+ })
@@ -0,0 +1 @@
1
+ export { PrimaryToolStore } from './primary-tool'
@@ -0,0 +1,8 @@
1
+ import { PrimaryToolType } from '@asyra/utils'
2
+ import uiContext from '../../ui-context'
3
+
4
+ export class PrimaryToolStore {
5
+ updatePrimaryTool(tool: PrimaryToolType) {
6
+ uiContext.updatePrimaryTool(tool)
7
+ }
8
+ }
File without changes
@@ -0,0 +1,13 @@
1
+ import { sceneTreeStore, initSceneTreeDataSubscribe } from './scene-tree'
2
+ import { selectionStore, initSelectionDataSubscribe } from './selection'
3
+ import { initRenderDataSubscribe } from './render'
4
+ import { initSystemContextSubscribe } from './system-context'
5
+
6
+ export const initDataContexts = () => {
7
+ initRenderDataSubscribe()
8
+ initSceneTreeDataSubscribe()
9
+ initSelectionDataSubscribe()
10
+ initSystemContextSubscribe()
11
+ }
12
+
13
+ export { sceneTreeStore, selectionStore }
@@ -0,0 +1,22 @@
1
+ import {
2
+ requestRenderZoom,
3
+ subscribeToEmitZoomFit
4
+ } from '@asyra/reactive-events'
5
+ import RenderStore from '../stores/render'
6
+
7
+ const renderStore = new RenderStore()
8
+
9
+ let hasInit = false
10
+
11
+ export const initRenderDataSubscribe = () => {
12
+ if (hasInit) {
13
+ return
14
+ }
15
+
16
+ subscribeToEmitZoomFit(async () => {
17
+ const zoom = await requestRenderZoom()
18
+ renderStore.updateZoom(zoom)
19
+ })
20
+
21
+ hasInit = true
22
+ }
@@ -0,0 +1,96 @@
1
+ import type {
2
+ SceneTreeYjsChange,
3
+ AddRemoveElementChange,
4
+ UpdateElementChange,
5
+ ComputedAttrs
6
+ } from '@asyra/utils'
7
+ import { SCENE_TREE_ACTIONS } from '@asyra/utils'
8
+ import factory from '@asyra/factory'
9
+ import sceneTree from '@asyra/scene-tree'
10
+ import {
11
+ requestElementSelection,
12
+ subscribeToEndTransaction,
13
+ subscribeToSceneTreeLoadComplete
14
+ } from '@asyra/reactive-events'
15
+ import SceneTreeStore from '../stores/scene-tree'
16
+ import uiContext from '../ui-context'
17
+
18
+ export const sceneTreeStore = new SceneTreeStore(sceneTree)
19
+
20
+ const updateUISceneTree = (change: SceneTreeYjsChange['payload']) => {
21
+ switch (change.action) {
22
+ case SCENE_TREE_ACTIONS.ADD_ELEMENT: {
23
+ const { data } = change as AddRemoveElementChange
24
+ sceneTreeStore.addElement(data)
25
+ break
26
+ }
27
+ case SCENE_TREE_ACTIONS.REMOVE_ELEMENT: {
28
+ const { parentId, data } = change as AddRemoveElementChange
29
+ sceneTreeStore.removeElement(data, parentId as string)
30
+ break
31
+ }
32
+ case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA: {
33
+ const { id, key, after } = change as UpdateElementChange
34
+ sceneTreeStore.updateElement(id, key, after)
35
+ break
36
+ }
37
+ }
38
+ }
39
+
40
+ // @ts-expect-error: It's YJS event
41
+ export const collectSceneTreeChange = (event) => {
42
+ const updatedComputedDataKeys = new Set() as Set<keyof ComputedAttrs>
43
+
44
+ const processChanges = (
45
+ items: typeof event.changes.added | typeof event.changes.deleted
46
+ ) => {
47
+ // @ts-expect-error: It's YJS event
48
+ items.forEach((item) => {
49
+ item.content
50
+ .getContent()
51
+ .forEach((change: SceneTreeYjsChange['payload']) => {
52
+ if (
53
+ change.action === SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA &&
54
+ 'key' in change
55
+ ) {
56
+ updatedComputedDataKeys.add(change.key as keyof ComputedAttrs)
57
+ }
58
+ updateUISceneTree(change)
59
+ })
60
+ })
61
+ }
62
+
63
+ processChanges(event.changes.added)
64
+ processChanges(event.changes.deleted)
65
+
66
+ updatedComputedDataKeys.forEach(async (key) => {
67
+ const elementSelection = await requestElementSelection()
68
+ const propertyData = [...elementSelection].map((elementId) => {
69
+ const elementData = sceneTreeStore.getElementGeneralData(elementId)
70
+ return elementData?.[key]
71
+ }) as ComputedAttrs[keyof ComputedAttrs][]
72
+ uiContext.updateComputedProperty(key, propertyData)
73
+ })
74
+ }
75
+
76
+ let hasInit = false
77
+
78
+ export const initSceneTreeDataSubscribe = () => {
79
+ if (hasInit) {
80
+ return
81
+ }
82
+
83
+ const sceneTreeArray = factory.sceneTreeMap
84
+ sceneTreeArray.observe(collectSceneTreeChange)
85
+
86
+ subscribeToSceneTreeLoadComplete(() => {
87
+ sceneTreeStore.reload()
88
+ sceneTreeStore.fireChange()
89
+ })
90
+
91
+ subscribeToEndTransaction(() => {
92
+ sceneTreeStore.fireChange()
93
+ })
94
+
95
+ hasInit = true
96
+ }
@@ -0,0 +1,56 @@
1
+ import type { SelectionYjsChange } from '@asyra/utils'
2
+ import { SELECTION_ACTIONS, SELECTION_TYPES } from '@asyra/utils'
3
+ import factory from '@asyra/factory'
4
+ import {
5
+ finishRequestElementSelection,
6
+ subscribeToRequestElementSelection
7
+ } from '@asyra/reactive-events'
8
+ import SelectionStore from '../stores/selection'
9
+ import uiContext from '../ui-context'
10
+
11
+ export const selectionStore = new SelectionStore()
12
+
13
+ const updateUIElementSelection = (change: SelectionYjsChange['payload']) => {
14
+ switch (change.action) {
15
+ case SELECTION_ACTIONS.SELECT_ELEMENTS:
16
+ case SELECTION_ACTIONS.DESELECT_ELEMENTS:
17
+ selectionStore.updateSelection(SELECTION_TYPES.ELEMENT)
18
+ break
19
+ case SELECTION_ACTIONS.SELECT_VERTICES:
20
+ case SELECTION_ACTIONS.DESELECT_VERTICES:
21
+ selectionStore.updateSelection(SELECTION_TYPES.VERTEX)
22
+ break
23
+ }
24
+ }
25
+
26
+ // @ts-expect-error: It's YJS event
27
+ export const collectElementSelectionChange = (event) => {
28
+ const processChanges = (
29
+ items: typeof event.changes.added | typeof event.changes.deleted
30
+ ) => {
31
+ // @ts-expect-error: It's YJS event
32
+ items.forEach((item) => {
33
+ item.content.getContent().forEach(updateUIElementSelection)
34
+ })
35
+ }
36
+
37
+ processChanges(event.changes.added)
38
+ processChanges(event.changes.deleted)
39
+ }
40
+
41
+ let hasInit = false
42
+
43
+ export const initSelectionDataSubscribe = () => {
44
+ if (hasInit) {
45
+ return
46
+ }
47
+
48
+ const elementSelectionArray = factory.elementSelectionMap
49
+ elementSelectionArray.observe(collectElementSelectionChange)
50
+
51
+ subscribeToRequestElementSelection(() => {
52
+ finishRequestElementSelection(uiContext.elementSelection.getValue())
53
+ })
54
+
55
+ hasInit = true
56
+ }
@@ -0,0 +1,5 @@
1
+ import { initPrimaryToolSubscribe } from './primary-tool'
2
+
3
+ export const initSystemContextSubscribe = () => {
4
+ initPrimaryToolSubscribe()
5
+ }
@@ -0,0 +1,22 @@
1
+ import {
2
+ requestCurrentPrimaryTool,
3
+ subscribeToEmitSwitchPrimaryTool
4
+ } from '@asyra/reactive-events'
5
+ import { PrimaryToolStore } from '../../stores/system-context'
6
+
7
+ export const primaryToolStore = new PrimaryToolStore()
8
+
9
+ let hasInit = false
10
+
11
+ export const initPrimaryToolSubscribe = () => {
12
+ if (hasInit) {
13
+ return
14
+ }
15
+
16
+ subscribeToEmitSwitchPrimaryTool(async () => {
17
+ const primaryTool = await requestCurrentPrimaryTool()
18
+ primaryToolStore.updatePrimaryTool(primaryTool)
19
+ })
20
+
21
+ hasInit = true
22
+ }
package/src/types.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { MIXED_STRING } from '@asyra/utils'
2
+
3
+ type MixedType = typeof MIXED_STRING
4
+
5
+ export interface ElementProperties {
6
+ x: number | MixedType
7
+ y: number | MixedType
8
+ width: number | MixedType
9
+ height: number | MixedType
10
+ rotation: number | MixedType
11
+ // fills: string[] | MixedType
12
+ // fill: FillAttrs | MixedType
13
+ }
@@ -0,0 +1,161 @@
1
+ import { BehaviorSubject } from 'rxjs'
2
+ import { ComputedAttrs, MIXED_STRING, PrimaryToolType } from '@asyra/utils'
3
+ import { isEqual } from 'lodash'
4
+ import { ElementProperties } from './types'
5
+
6
+ const generalKeysToCompare: (keyof ElementProperties)[] = [
7
+ 'x',
8
+ 'y',
9
+ 'width',
10
+ 'height',
11
+ 'rotation'
12
+ ]
13
+
14
+ class UIContext {
15
+ zoom: BehaviorSubject<number>
16
+ flattenedElementIds: BehaviorSubject<string[]>
17
+ elementSelection: BehaviorSubject<Set<string>>
18
+ vertexSelection: BehaviorSubject<Set<string>>
19
+ x: BehaviorSubject<ElementProperties['x']>
20
+ y: BehaviorSubject<ElementProperties['y']>
21
+ width: BehaviorSubject<ElementProperties['width']>
22
+ height: BehaviorSubject<ElementProperties['height']>
23
+ rotation: BehaviorSubject<ElementProperties['rotation']>
24
+ // fills: BehaviorSubject<ElementProperties['fills']>
25
+
26
+ // System Context
27
+ primaryTool: BehaviorSubject<PrimaryToolType>
28
+
29
+ constructor() {
30
+ this.zoom = new BehaviorSubject<number>(1)
31
+ this.flattenedElementIds = new BehaviorSubject<string[]>([])
32
+ this.elementSelection = new BehaviorSubject<Set<string>>(new Set())
33
+ this.vertexSelection = new BehaviorSubject<Set<string>>(new Set())
34
+ this.x = new BehaviorSubject<ElementProperties['x']>(0)
35
+ this.y = new BehaviorSubject<ElementProperties['y']>(0)
36
+ this.width = new BehaviorSubject<ElementProperties['width']>(0)
37
+ this.height = new BehaviorSubject<ElementProperties['height']>(0)
38
+ this.rotation = new BehaviorSubject<ElementProperties['rotation']>(0)
39
+ // this.fills = new BehaviorSubject<ElementProperties['fills']>([])
40
+
41
+ this.primaryTool = new BehaviorSubject<PrimaryToolType>(
42
+ PrimaryToolType.SELECT
43
+ )
44
+ }
45
+
46
+ updateElementSelection(selectedIds: Set<string>) {
47
+ if (!isEqual(this.elementSelection.getValue(), selectedIds)) {
48
+ this.elementSelection.next(selectedIds)
49
+ }
50
+ }
51
+
52
+ updateVertexSelection(selectedIds: Set<string>) {
53
+ if (!isEqual(this.vertexSelection.getValue(), selectedIds)) {
54
+ this.vertexSelection.next(selectedIds)
55
+ }
56
+ }
57
+
58
+ updateComputedProperty<K extends keyof ComputedAttrs>(
59
+ key: K,
60
+ data: ComputedAttrs[K][]
61
+ ) {
62
+ if (!data.length) {
63
+ return
64
+ }
65
+
66
+ const compareKey = key as keyof ElementProperties
67
+ if (generalKeysToCompare.includes(compareKey)) {
68
+ const result = this.computedSharedProperty(
69
+ data as ElementProperties[keyof ElementProperties][]
70
+ )
71
+ if (result !== this[compareKey].getValue()) {
72
+ this[compareKey].next(result)
73
+ }
74
+ }
75
+ }
76
+
77
+ updateComputedProperties(allElementData: ComputedAttrs[]) {
78
+ const result = this.computeSharedProperties(allElementData)
79
+
80
+ generalKeysToCompare.forEach((key) => {
81
+ if (result[key] !== this[key].getValue()) {
82
+ this[key].next(result[key])
83
+ }
84
+ })
85
+ }
86
+
87
+ computedSharedProperty<K extends keyof ElementProperties>(
88
+ data: ElementProperties[K][]
89
+ ) {
90
+ return compareValue(data)
91
+ }
92
+
93
+ computeSharedProperties(allElementData: ComputedAttrs[]): ElementProperties {
94
+ const result = {} as ElementProperties
95
+
96
+ result.x = compareValues(allElementData, 'x') as ElementProperties['x']
97
+ result.y = compareValues(allElementData, 'y') as ElementProperties['y']
98
+ result.width = compareValues(
99
+ allElementData,
100
+ 'width'
101
+ ) as ElementProperties['width']
102
+ result.height = compareValues(
103
+ allElementData,
104
+ 'height'
105
+ ) as ElementProperties['height']
106
+ result.rotation = compareValues(
107
+ allElementData,
108
+ 'rotation'
109
+ ) as ElementProperties['rotation']
110
+
111
+ return result
112
+ }
113
+
114
+ updateZoom(newZoom: number) {
115
+ this.zoom.next(newZoom)
116
+ }
117
+
118
+ updatePrimaryTool(tool: PrimaryToolType) {
119
+ if (tool !== this.primaryTool.getValue()) {
120
+ this.primaryTool.next(tool)
121
+ }
122
+ }
123
+ }
124
+
125
+ const uiContext = new UIContext()
126
+
127
+ export default uiContext
128
+ export { UIContext }
129
+
130
+ const compareValue = <K extends keyof ElementProperties>(
131
+ allElementData: ElementProperties[K][]
132
+ ) => {
133
+ const firstValue = allElementData[0]
134
+ let isMixed = false
135
+
136
+ for (let i = 1; i < allElementData.length; i++) {
137
+ if (allElementData[i] !== firstValue) {
138
+ isMixed = true
139
+ break
140
+ }
141
+ }
142
+
143
+ return isMixed ? MIXED_STRING : firstValue
144
+ }
145
+
146
+ const compareValues = (
147
+ allElementData: ComputedAttrs[],
148
+ compareKey: keyof ComputedAttrs
149
+ ) => {
150
+ const firstValue = allElementData[0][compareKey]
151
+ let isMixed = false
152
+
153
+ for (let i = 1; i < allElementData.length; i++) {
154
+ if (allElementData[i][compareKey] !== firstValue) {
155
+ isMixed = true
156
+ break
157
+ }
158
+ }
159
+
160
+ return isMixed ? MIXED_STRING : firstValue
161
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "declaration": true,
6
+ "declarationMap": true,
7
+ "sourceMap": true,
8
+ "outDir": "./dist",
9
+ "strict": true,
10
+ "esModuleInterop": true,
11
+ "skipLibCheck": true,
12
+ "moduleResolution": "Node",
13
+ "resolveJsonModule": true
14
+ },
15
+ "include": ["src"],
16
+ "exclude": ["node_modules", "dist"]
17
+ }