@asyra/ui-context 0.2.4 → 0.5.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 (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -1
  3. package/dist/index.d.ts +5 -3
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +4 -3
  6. package/dist/index.js.map +1 -1
  7. package/dist/property-registry.d.ts +78 -0
  8. package/dist/property-registry.d.ts.map +1 -0
  9. package/dist/property-registry.js +119 -0
  10. package/dist/property-registry.js.map +1 -0
  11. package/dist/ui-context.d.ts +9 -21
  12. package/dist/ui-context.d.ts.map +1 -1
  13. package/dist/ui-context.js +58 -81
  14. package/dist/ui-context.js.map +1 -1
  15. package/package.json +24 -9
  16. package/.turbo/turbo-test$colon$ci.log +0 -117
  17. package/.turbo/turbo-test$colon$local.log +0 -23
  18. package/.turbo/turbo-test.log +0 -0
  19. package/CHANGELOG.md +0 -49
  20. package/coverage/base.css +0 -224
  21. package/coverage/block-navigation.js +0 -87
  22. package/coverage/clover.xml +0 -538
  23. package/coverage/coverage-final.json +0 -7443
  24. package/coverage/favicon.png +0 -0
  25. package/coverage/index.html +0 -176
  26. package/coverage/prettify.css +0 -1
  27. package/coverage/prettify.js +0 -2
  28. package/coverage/sort-arrow-sprite.png +0 -0
  29. package/coverage/sorter.js +0 -196
  30. package/coverage/src/index.html +0 -146
  31. package/coverage/src/index.ts.html +0 -97
  32. package/coverage/src/stores/index.html +0 -146
  33. package/coverage/src/stores/render.ts.html +0 -106
  34. package/coverage/src/stores/scene-tree.ts.html +0 -790
  35. package/coverage/src/stores/selection.ts.html +0 -220
  36. package/coverage/src/stores/system-context/index.html +0 -146
  37. package/coverage/src/stores/system-context/index.ts.html +0 -88
  38. package/coverage/src/stores/system-context/primary-tool.ts.html +0 -109
  39. package/coverage/src/stores/system-context/system-context.ts.html +0 -85
  40. package/coverage/src/subscribes/index.html +0 -161
  41. package/coverage/src/subscribes/index.ts.html +0 -124
  42. package/coverage/src/subscribes/render.ts.html +0 -151
  43. package/coverage/src/subscribes/scene-tree.ts.html +0 -373
  44. package/coverage/src/subscribes/selection.ts.html +0 -253
  45. package/coverage/src/subscribes/system-context/index.html +0 -131
  46. package/coverage/src/subscribes/system-context/index.ts.html +0 -100
  47. package/coverage/src/subscribes/system-context/primary-tool.ts.html +0 -151
  48. package/coverage/src/types.ts.html +0 -124
  49. package/coverage/src/ui-context.ts.html +0 -568
  50. package/dist/__tests__/ui-context.test.d.ts +0 -2
  51. package/dist/__tests__/ui-context.test.d.ts.map +0 -1
  52. package/dist/__tests__/ui-context.test.js +0 -170
  53. package/dist/__tests__/ui-context.test.js.map +0 -1
  54. package/dist/stores/__tests__/render.test.d.ts +0 -2
  55. package/dist/stores/__tests__/render.test.d.ts.map +0 -1
  56. package/dist/stores/__tests__/render.test.js +0 -30
  57. package/dist/stores/__tests__/render.test.js.map +0 -1
  58. package/dist/stores/__tests__/selection.test.d.ts +0 -2
  59. package/dist/stores/__tests__/selection.test.d.ts.map +0 -1
  60. package/dist/stores/__tests__/selection.test.js +0 -128
  61. package/dist/stores/__tests__/selection.test.js.map +0 -1
  62. package/dist/stores/render.d.ts +0 -4
  63. package/dist/stores/render.d.ts.map +0 -1
  64. package/dist/stores/render.js +0 -7
  65. package/dist/stores/render.js.map +0 -1
  66. package/dist/stores/scene-tree.d.ts +0 -35
  67. package/dist/stores/scene-tree.d.ts.map +0 -1
  68. package/dist/stores/scene-tree.js +0 -158
  69. package/dist/stores/scene-tree.js.map +0 -1
  70. package/dist/stores/selection.d.ts +0 -8
  71. package/dist/stores/selection.d.ts.map +0 -1
  72. package/dist/stores/selection.js +0 -38
  73. package/dist/stores/selection.js.map +0 -1
  74. package/dist/stores/system-context/__tests__/primary-tool.test.d.ts +0 -2
  75. package/dist/stores/system-context/__tests__/primary-tool.test.d.ts.map +0 -1
  76. package/dist/stores/system-context/__tests__/primary-tool.test.js +0 -27
  77. package/dist/stores/system-context/__tests__/primary-tool.test.js.map +0 -1
  78. package/dist/stores/system-context/index.d.ts +0 -2
  79. package/dist/stores/system-context/index.d.ts.map +0 -1
  80. package/dist/stores/system-context/index.js +0 -2
  81. package/dist/stores/system-context/index.js.map +0 -1
  82. package/dist/stores/system-context/primary-tool.d.ts +0 -5
  83. package/dist/stores/system-context/primary-tool.d.ts.map +0 -1
  84. package/dist/stores/system-context/primary-tool.js +0 -7
  85. package/dist/stores/system-context/primary-tool.js.map +0 -1
  86. package/dist/stores/system-context/system-context.d.ts +0 -1
  87. package/dist/stores/system-context/system-context.d.ts.map +0 -1
  88. package/dist/stores/system-context/system-context.js +0 -2
  89. package/dist/stores/system-context/system-context.js.map +0 -1
  90. package/dist/subscribes/index.d.ts +0 -5
  91. package/dist/subscribes/index.d.ts.map +0 -1
  92. package/dist/subscribes/index.js +0 -12
  93. package/dist/subscribes/index.js.map +0 -1
  94. package/dist/subscribes/render.d.ts +0 -2
  95. package/dist/subscribes/render.d.ts.map +0 -1
  96. package/dist/subscribes/render.js +0 -15
  97. package/dist/subscribes/render.js.map +0 -1
  98. package/dist/subscribes/scene-tree.d.ts +0 -5
  99. package/dist/subscribes/scene-tree.d.ts.map +0 -1
  100. package/dist/subscribes/scene-tree.js +0 -71
  101. package/dist/subscribes/scene-tree.js.map +0 -1
  102. package/dist/subscribes/selection.d.ts +0 -5
  103. package/dist/subscribes/selection.d.ts.map +0 -1
  104. package/dist/subscribes/selection.js +0 -42
  105. package/dist/subscribes/selection.js.map +0 -1
  106. package/dist/subscribes/system-context/index.d.ts +0 -2
  107. package/dist/subscribes/system-context/index.d.ts.map +0 -1
  108. package/dist/subscribes/system-context/index.js +0 -5
  109. package/dist/subscribes/system-context/index.js.map +0 -1
  110. package/dist/subscribes/system-context/primary-tool.d.ts +0 -4
  111. package/dist/subscribes/system-context/primary-tool.d.ts.map +0 -1
  112. package/dist/subscribes/system-context/primary-tool.js +0 -15
  113. package/dist/subscribes/system-context/primary-tool.js.map +0 -1
  114. package/dist/types.d.ts +0 -11
  115. package/dist/types.d.ts.map +0 -1
  116. package/dist/types.js +0 -2
  117. package/dist/types.js.map +0 -1
  118. package/src/__tests__/ui-context.test.ts +0 -220
  119. package/src/index.ts +0 -4
  120. package/src/stores/__tests__/render.test.ts +0 -38
  121. package/src/stores/__tests__/selection.test.ts +0 -190
  122. package/src/stores/render.ts +0 -7
  123. package/src/stores/scene-tree.ts +0 -235
  124. package/src/stores/selection.ts +0 -45
  125. package/src/stores/system-context/__tests__/primary-tool.test.ts +0 -37
  126. package/src/stores/system-context/index.ts +0 -1
  127. package/src/stores/system-context/primary-tool.ts +0 -8
  128. package/src/stores/system-context/system-context.ts +0 -0
  129. package/src/subscribes/index.ts +0 -13
  130. package/src/subscribes/render.ts +0 -22
  131. package/src/subscribes/scene-tree.ts +0 -96
  132. package/src/subscribes/selection.ts +0 -56
  133. package/src/subscribes/system-context/index.ts +0 -5
  134. package/src/subscribes/system-context/primary-tool.ts +0 -22
  135. package/src/types.ts +0 -13
  136. package/src/ui-context.ts +0 -161
  137. package/tsconfig.json +0 -17
@@ -1,96 +0,0 @@
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
- }
@@ -1,56 +0,0 @@
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
- }
@@ -1,5 +0,0 @@
1
- import { initPrimaryToolSubscribe } from './primary-tool'
2
-
3
- export const initSystemContextSubscribe = () => {
4
- initPrimaryToolSubscribe()
5
- }
@@ -1,22 +0,0 @@
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 DELETED
@@ -1,13 +0,0 @@
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
- }
package/src/ui-context.ts DELETED
@@ -1,161 +0,0 @@
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 DELETED
@@ -1,17 +0,0 @@
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
- }