@microsoft/live-share-react 1.0.0-preview.10

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 (233) hide show
  1. package/.nycrc.json +10 -0
  2. package/LICENSE +87 -0
  3. package/README.md +544 -0
  4. package/bin/index.d.ts +7 -0
  5. package/bin/index.d.ts.map +1 -0
  6. package/bin/index.js +7 -0
  7. package/bin/index.js.map +1 -0
  8. package/bin/interfaces/LiveInterfaceExtensions.d.ts +9 -0
  9. package/bin/interfaces/LiveInterfaceExtensions.d.ts.map +1 -0
  10. package/bin/interfaces/LiveInterfaceExtensions.js +6 -0
  11. package/bin/interfaces/LiveInterfaceExtensions.js.map +1 -0
  12. package/bin/interfaces/index.d.ts +2 -0
  13. package/bin/interfaces/index.d.ts.map +1 -0
  14. package/bin/interfaces/index.js +2 -0
  15. package/bin/interfaces/index.js.map +1 -0
  16. package/bin/internal-hooks/index.d.ts +2 -0
  17. package/bin/internal-hooks/index.d.ts.map +1 -0
  18. package/bin/internal-hooks/index.js +2 -0
  19. package/bin/internal-hooks/index.js.map +1 -0
  20. package/bin/internal-hooks/useDynamicDDSRegistry.d.ts +14 -0
  21. package/bin/internal-hooks/useDynamicDDSRegistry.d.ts.map +1 -0
  22. package/bin/internal-hooks/useDynamicDDSRegistry.js +142 -0
  23. package/bin/internal-hooks/useDynamicDDSRegistry.js.map +1 -0
  24. package/bin/internal-hooks/useSharedStateRegistry.d.ts +31 -0
  25. package/bin/internal-hooks/useSharedStateRegistry.d.ts.map +1 -0
  26. package/bin/internal-hooks/useSharedStateRegistry.js +100 -0
  27. package/bin/internal-hooks/useSharedStateRegistry.js.map +1 -0
  28. package/bin/internal-hooks/useStateRef.d.ts +7 -0
  29. package/bin/internal-hooks/useStateRef.d.ts.map +1 -0
  30. package/bin/internal-hooks/useStateRef.js +15 -0
  31. package/bin/internal-hooks/useStateRef.js.map +1 -0
  32. package/bin/live-hooks/index.d.ts +7 -0
  33. package/bin/live-hooks/index.d.ts.map +1 -0
  34. package/bin/live-hooks/index.js +7 -0
  35. package/bin/live-hooks/index.js.map +1 -0
  36. package/bin/live-hooks/useLiveAICompletion.d.ts +29 -0
  37. package/bin/live-hooks/useLiveAICompletion.d.ts.map +1 -0
  38. package/bin/live-hooks/useLiveAICompletion.js +186 -0
  39. package/bin/live-hooks/useLiveAICompletion.js.map +1 -0
  40. package/bin/live-hooks/useLiveCanvas.d.ts +28 -0
  41. package/bin/live-hooks/useLiveCanvas.d.ts.map +1 -0
  42. package/bin/live-hooks/useLiveCanvas.js +167 -0
  43. package/bin/live-hooks/useLiveCanvas.js.map +1 -0
  44. package/bin/live-hooks/useLiveCoPilot.d.ts +28 -0
  45. package/bin/live-hooks/useLiveCoPilot.d.ts.map +1 -0
  46. package/bin/live-hooks/useLiveCoPilot.js +178 -0
  47. package/bin/live-hooks/useLiveCoPilot.js.map +1 -0
  48. package/bin/live-hooks/useLiveEvent.d.ts +22 -0
  49. package/bin/live-hooks/useLiveEvent.d.ts.map +1 -0
  50. package/bin/live-hooks/useLiveEvent.js +86 -0
  51. package/bin/live-hooks/useLiveEvent.js.map +1 -0
  52. package/bin/live-hooks/useLivePresence.d.ts +26 -0
  53. package/bin/live-hooks/useLivePresence.d.ts.map +1 -0
  54. package/bin/live-hooks/useLivePresence.js +93 -0
  55. package/bin/live-hooks/useLivePresence.js.map +1 -0
  56. package/bin/live-hooks/useLiveState.d.ts +19 -0
  57. package/bin/live-hooks/useLiveState.d.ts.map +1 -0
  58. package/bin/live-hooks/useLiveState.js +61 -0
  59. package/bin/live-hooks/useLiveState.js.map +1 -0
  60. package/bin/live-hooks/useLiveTimer.d.ts +8 -0
  61. package/bin/live-hooks/useLiveTimer.d.ts.map +1 -0
  62. package/bin/live-hooks/useLiveTimer.js +132 -0
  63. package/bin/live-hooks/useLiveTimer.js.map +1 -0
  64. package/bin/live-hooks/useMediaSynchronizer.d.ts +23 -0
  65. package/bin/live-hooks/useMediaSynchronizer.d.ts.map +1 -0
  66. package/bin/live-hooks/useMediaSynchronizer.js +162 -0
  67. package/bin/live-hooks/useMediaSynchronizer.js.map +1 -0
  68. package/bin/live-share-turbo/AzureTurboClient.d.ts +39 -0
  69. package/bin/live-share-turbo/AzureTurboClient.d.ts.map +1 -0
  70. package/bin/live-share-turbo/AzureTurboClient.js +56 -0
  71. package/bin/live-share-turbo/AzureTurboClient.js.map +1 -0
  72. package/bin/live-share-turbo/LiveShareTurboClient.d.ts +53 -0
  73. package/bin/live-share-turbo/LiveShareTurboClient.d.ts.map +1 -0
  74. package/bin/live-share-turbo/LiveShareTurboClient.js +69 -0
  75. package/bin/live-share-turbo/LiveShareTurboClient.js.map +1 -0
  76. package/bin/live-share-turbo/dds-objects/TurboDataObject.d.ts +31 -0
  77. package/bin/live-share-turbo/dds-objects/TurboDataObject.d.ts.map +1 -0
  78. package/bin/live-share-turbo/dds-objects/TurboDataObject.js +75 -0
  79. package/bin/live-share-turbo/dds-objects/TurboDataObject.js.map +1 -0
  80. package/bin/live-share-turbo/dds-objects/TurboLiveCanvas.d.ts +48 -0
  81. package/bin/live-share-turbo/dds-objects/TurboLiveCanvas.d.ts.map +1 -0
  82. package/bin/live-share-turbo/dds-objects/TurboLiveCanvas.js +104 -0
  83. package/bin/live-share-turbo/dds-objects/TurboLiveCanvas.js.map +1 -0
  84. package/bin/live-share-turbo/dds-objects/TurboLiveEvent.d.ts +28 -0
  85. package/bin/live-share-turbo/dds-objects/TurboLiveEvent.d.ts.map +1 -0
  86. package/bin/live-share-turbo/dds-objects/TurboLiveEvent.js +68 -0
  87. package/bin/live-share-turbo/dds-objects/TurboLiveEvent.js.map +1 -0
  88. package/bin/live-share-turbo/dds-objects/TurboLiveMediaSession.d.ts +60 -0
  89. package/bin/live-share-turbo/dds-objects/TurboLiveMediaSession.d.ts.map +1 -0
  90. package/bin/live-share-turbo/dds-objects/TurboLiveMediaSession.js +111 -0
  91. package/bin/live-share-turbo/dds-objects/TurboLiveMediaSession.js.map +1 -0
  92. package/bin/live-share-turbo/dds-objects/TurboLivePresence.d.ts +77 -0
  93. package/bin/live-share-turbo/dds-objects/TurboLivePresence.d.ts.map +1 -0
  94. package/bin/live-share-turbo/dds-objects/TurboLivePresence.js +137 -0
  95. package/bin/live-share-turbo/dds-objects/TurboLivePresence.js.map +1 -0
  96. package/bin/live-share-turbo/dds-objects/TurboLiveState.d.ts +30 -0
  97. package/bin/live-share-turbo/dds-objects/TurboLiveState.d.ts.map +1 -0
  98. package/bin/live-share-turbo/dds-objects/TurboLiveState.js +74 -0
  99. package/bin/live-share-turbo/dds-objects/TurboLiveState.js.map +1 -0
  100. package/bin/live-share-turbo/dds-objects/TurboSharedMap.d.ts +61 -0
  101. package/bin/live-share-turbo/dds-objects/TurboSharedMap.d.ts.map +1 -0
  102. package/bin/live-share-turbo/dds-objects/TurboSharedMap.js +108 -0
  103. package/bin/live-share-turbo/dds-objects/TurboSharedMap.js.map +1 -0
  104. package/bin/live-share-turbo/dds-objects/index.d.ts +8 -0
  105. package/bin/live-share-turbo/dds-objects/index.d.ts.map +1 -0
  106. package/bin/live-share-turbo/dds-objects/index.js +8 -0
  107. package/bin/live-share-turbo/dds-objects/index.js.map +1 -0
  108. package/bin/live-share-turbo/index.d.ts +5 -0
  109. package/bin/live-share-turbo/index.d.ts.map +1 -0
  110. package/bin/live-share-turbo/index.js +5 -0
  111. package/bin/live-share-turbo/index.js.map +1 -0
  112. package/bin/live-share-turbo/interfaces/IFluidTurboClient.d.ts +15 -0
  113. package/bin/live-share-turbo/interfaces/IFluidTurboClient.d.ts.map +1 -0
  114. package/bin/live-share-turbo/interfaces/IFluidTurboClient.js +2 -0
  115. package/bin/live-share-turbo/interfaces/IFluidTurboClient.js.map +1 -0
  116. package/bin/live-share-turbo/interfaces/index.d.ts +3 -0
  117. package/bin/live-share-turbo/interfaces/index.d.ts.map +1 -0
  118. package/bin/live-share-turbo/interfaces/index.js +3 -0
  119. package/bin/live-share-turbo/interfaces/index.js.map +1 -0
  120. package/bin/live-share-turbo/internals/FluidTurboClient.d.ts +39 -0
  121. package/bin/live-share-turbo/internals/FluidTurboClient.d.ts.map +1 -0
  122. package/bin/live-share-turbo/internals/FluidTurboClient.js +121 -0
  123. package/bin/live-share-turbo/internals/FluidTurboClient.js.map +1 -0
  124. package/bin/live-share-turbo/internals/index.d.ts +2 -0
  125. package/bin/live-share-turbo/internals/index.d.ts.map +1 -0
  126. package/bin/live-share-turbo/internals/index.js +2 -0
  127. package/bin/live-share-turbo/internals/index.js.map +1 -0
  128. package/bin/live-share-turbo/internals/types.d.ts +4 -0
  129. package/bin/live-share-turbo/internals/types.d.ts.map +1 -0
  130. package/bin/live-share-turbo/internals/types.js +2 -0
  131. package/bin/live-share-turbo/internals/types.js.map +1 -0
  132. package/bin/providers/AzureProvider.d.ts +34 -0
  133. package/bin/providers/AzureProvider.d.ts.map +1 -0
  134. package/bin/providers/AzureProvider.js +102 -0
  135. package/bin/providers/AzureProvider.js.map +1 -0
  136. package/bin/providers/FluidContextProvider.d.ts +34 -0
  137. package/bin/providers/FluidContextProvider.d.ts.map +1 -0
  138. package/bin/providers/FluidContextProvider.js +99 -0
  139. package/bin/providers/FluidContextProvider.js.map +1 -0
  140. package/bin/providers/LiveShareContextProvider.d.ts +42 -0
  141. package/bin/providers/LiveShareContextProvider.d.ts.map +1 -0
  142. package/bin/providers/LiveShareContextProvider.js +63 -0
  143. package/bin/providers/LiveShareContextProvider.js.map +1 -0
  144. package/bin/providers/LiveShareProvider.d.ts +42 -0
  145. package/bin/providers/LiveShareProvider.d.ts.map +1 -0
  146. package/bin/providers/LiveShareProvider.js +66 -0
  147. package/bin/providers/LiveShareProvider.js.map +1 -0
  148. package/bin/providers/index.d.ts +3 -0
  149. package/bin/providers/index.d.ts.map +1 -0
  150. package/bin/providers/index.js +3 -0
  151. package/bin/providers/index.js.map +1 -0
  152. package/bin/shared-hooks/index.d.ts +5 -0
  153. package/bin/shared-hooks/index.d.ts.map +1 -0
  154. package/bin/shared-hooks/index.js +5 -0
  155. package/bin/shared-hooks/index.js.map +1 -0
  156. package/bin/shared-hooks/useDynamicDDS.d.ts +19 -0
  157. package/bin/shared-hooks/useDynamicDDS.d.ts.map +1 -0
  158. package/bin/shared-hooks/useDynamicDDS.js +68 -0
  159. package/bin/shared-hooks/useDynamicDDS.js.map +1 -0
  160. package/bin/shared-hooks/useLockedTask.d.ts +11 -0
  161. package/bin/shared-hooks/useLockedTask.d.ts.map +1 -0
  162. package/bin/shared-hooks/useLockedTask.js +82 -0
  163. package/bin/shared-hooks/useLockedTask.js.map +1 -0
  164. package/bin/shared-hooks/useSharedMap.d.ts +22 -0
  165. package/bin/shared-hooks/useSharedMap.d.ts.map +1 -0
  166. package/bin/shared-hooks/useSharedMap.js +106 -0
  167. package/bin/shared-hooks/useSharedMap.js.map +1 -0
  168. package/bin/shared-hooks/useSharedState.d.ts +21 -0
  169. package/bin/shared-hooks/useSharedState.d.ts.map +1 -0
  170. package/bin/shared-hooks/useSharedState.js +63 -0
  171. package/bin/shared-hooks/useSharedState.js.map +1 -0
  172. package/bin/shared-hooks/useTaskManager.d.ts +13 -0
  173. package/bin/shared-hooks/useTaskManager.d.ts.map +1 -0
  174. package/bin/shared-hooks/useTaskManager.js +86 -0
  175. package/bin/shared-hooks/useTaskManager.js.map +1 -0
  176. package/bin/types/ActionTypes.d.ts +30 -0
  177. package/bin/types/ActionTypes.d.ts.map +1 -0
  178. package/bin/types/ActionTypes.js +6 -0
  179. package/bin/types/ActionTypes.js.map +1 -0
  180. package/bin/types/PropTypes.d.ts +8 -0
  181. package/bin/types/PropTypes.d.ts.map +1 -0
  182. package/bin/types/PropTypes.js +6 -0
  183. package/bin/types/PropTypes.js.map +1 -0
  184. package/bin/types/ResultTypes.d.ts +163 -0
  185. package/bin/types/ResultTypes.d.ts.map +1 -0
  186. package/bin/types/ResultTypes.js +6 -0
  187. package/bin/types/ResultTypes.js.map +1 -0
  188. package/bin/types/index.d.ts +4 -0
  189. package/bin/types/index.d.ts.map +1 -0
  190. package/bin/types/index.js +4 -0
  191. package/bin/types/index.js.map +1 -0
  192. package/bin/utils/getContainerSchema.d.ts +8 -0
  193. package/bin/utils/getContainerSchema.d.ts.map +1 -0
  194. package/bin/utils/getContainerSchema.js +41 -0
  195. package/bin/utils/getContainerSchema.js.map +1 -0
  196. package/bin/utils/index.d.ts +2 -0
  197. package/bin/utils/index.d.ts.map +1 -0
  198. package/bin/utils/index.js +2 -0
  199. package/bin/utils/index.js.map +1 -0
  200. package/bin/utils/type-check.d.ts +15 -0
  201. package/bin/utils/type-check.d.ts.map +1 -0
  202. package/bin/utils/type-check.js +54 -0
  203. package/bin/utils/type-check.js.map +1 -0
  204. package/package.json +47 -0
  205. package/src/index.ts +6 -0
  206. package/src/interfaces/LiveInterfaceExtensions.ts +9 -0
  207. package/src/interfaces/index.ts +1 -0
  208. package/src/internal-hooks/index.ts +1 -0
  209. package/src/internal-hooks/useSharedStateRegistry.ts +163 -0
  210. package/src/live-hooks/index.ts +6 -0
  211. package/src/live-hooks/useLiveCanvas.ts +198 -0
  212. package/src/live-hooks/useLiveEvent.ts +117 -0
  213. package/src/live-hooks/useLivePresence.ts +124 -0
  214. package/src/live-hooks/useLiveState.ts +87 -0
  215. package/src/live-hooks/useLiveTimer.ts +184 -0
  216. package/src/live-hooks/useMediaSynchronizer.ts +200 -0
  217. package/src/providers/AzureProvider.tsx +177 -0
  218. package/src/providers/LiveShareProvider.tsx +140 -0
  219. package/src/providers/index.ts +2 -0
  220. package/src/shared-hooks/index.ts +4 -0
  221. package/src/shared-hooks/useDynamicDDS.ts +71 -0
  222. package/src/shared-hooks/useSharedMap.ts +132 -0
  223. package/src/shared-hooks/useSharedState.ts +85 -0
  224. package/src/shared-hooks/useTaskManager.ts +82 -0
  225. package/src/types/ActionTypes.ts +75 -0
  226. package/src/types/PropTypes.ts +10 -0
  227. package/src/types/ResultTypes.ts +187 -0
  228. package/src/types/index.ts +3 -0
  229. package/src/utils/index.ts +1 -0
  230. package/src/utils/type-check.ts +69 -0
  231. package/tinylicious.log +4 -0
  232. package/tsconfig.build.json +6 -0
  233. package/tsconfig.json +72 -0
@@ -0,0 +1,198 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import {
7
+ CanvasReferencePoint,
8
+ IBrush,
9
+ InkingManager,
10
+ InkingTool,
11
+ IPoint,
12
+ IUserInfo,
13
+ LiveCanvas,
14
+ } from "@microsoft/live-share-canvas";
15
+ import React from "react";
16
+ import { useDynamicDDS } from "../shared-hooks";
17
+ import { IUseLiveCanvasResults } from "../types";
18
+ import { isRefObject } from "../utils";
19
+
20
+ /**
21
+ * React hook for using a Live Share Canvas `LiveCanvas` and `InkingManager`.
22
+ *
23
+ * @remarks
24
+ * Use this hook to set up an `LiveCanvas` object, which is used for collaborative inking. It takes several input parameters, including a unique key,
25
+ * a reference to a canvas element, and various settings for the inking tool, brush, and canvas offset and scale.
26
+ *
27
+ * @param uniqueKey the unique key for the `LiveCanvas`. If one does not yet exist, a new one.
28
+ * @param canvasElementRef the HTML div element ref or document ID that `InkingManager` will use for canvas-based collaboration.
29
+ * @param active Optional. Stateful boolean that will activate/de-activate `InkingManager` accordingly.
30
+ * @param tool Optional. Stateful enum for what tool to use in the `InkingManager`.
31
+ * @param lineBrush Optional. Stateful lineBrush object for the selected lineBrush options to use in `InkingManager`.
32
+ * @param offset Optional. Stateful offset point to use in the `InkingManager`. Gets the viewport offset. Defaults to 0,0.
33
+ * @param scale Optional. Stateful scale number to use in the `InkingManager`. Defaults to 1 and must be greater than 0.
34
+ * @param referencePoint Optional. Stateful reference point enum to use in the `InkingManger`. Defaults to "center".
35
+ * @param isCursorShared Optional. Stateful boolean flag for whether cursor should be shared in `LiveCanvas`. Defaults to false.
36
+ * @param localUserCursor Optional. Stateful `IUserInfo` object for the local user's metadata to display alongside their cursor. Defaults to undefined.
37
+ * @returns IUseLiveCanvasResults object that contains the `liveCanvas` data object and `inkingManager`.
38
+ */
39
+ export function useLiveCanvas(
40
+ uniqueKey: string,
41
+ canvasElementRef: React.RefObject<HTMLElement | null> | string,
42
+ active?: boolean,
43
+ tool?: InkingTool,
44
+ lineBrush?: IBrush,
45
+ offset?: IPoint,
46
+ scale?: number,
47
+ referencePoint?: CanvasReferencePoint,
48
+ isCursorShared?: boolean,
49
+ localUserCursor?: IUserInfo
50
+ ): IUseLiveCanvasResults {
51
+ /**
52
+ * User facing: inking manager instance
53
+ */
54
+ const [inkingManager, setInkingManager] = React.useState<InkingManager>();
55
+
56
+ /**
57
+ * User facing: dynamically load the DDS for the given unique key.
58
+ */
59
+ const { dds: liveCanvas } = useDynamicDDS<LiveCanvas>(uniqueKey, LiveCanvas);
60
+
61
+ /**
62
+ * Setup the InkingManager and LiveCanvas
63
+ */
64
+ React.useEffect(() => {
65
+ // if the component is already listening or liveCanvas is not yet initialized, return
66
+ if (!liveCanvas) return;
67
+ // get the canvas element from the ref or document
68
+ let htmlElement: HTMLElement | null;
69
+ if (isRefObject(canvasElementRef)) {
70
+ htmlElement = canvasElementRef.current;
71
+ } else {
72
+ htmlElement = document.getElementById(canvasElementRef);
73
+ }
74
+ if (htmlElement === null) return;
75
+ // Create the InkingManager and initialize the liveCanvas with it
76
+ const inkingManager = new InkingManager(htmlElement);
77
+ setInkingManager(inkingManager);
78
+ liveCanvas.initialize(inkingManager);
79
+
80
+ // cleanup function to be called when the component is unmount
81
+ return () => {
82
+ liveCanvas.dispose();
83
+ inkingManager.removeAllListeners();
84
+ };
85
+ }, [liveCanvas]);
86
+
87
+ /**
88
+ * Activate or deactivate the inkingManager based on the 'active' prop
89
+ */
90
+ React.useEffect(() => {
91
+ if (inkingManager && active !== undefined) {
92
+ if (active) {
93
+ inkingManager.activate();
94
+ } else {
95
+ inkingManager.deactivate();
96
+ }
97
+ }
98
+ }, [active, inkingManager]);
99
+
100
+ /**
101
+ * Sets the tool of the inkingManager based on the 'tool' prop
102
+ */
103
+ React.useEffect(() => {
104
+ if (inkingManager && tool !== undefined) {
105
+ inkingManager.tool = tool;
106
+ }
107
+ }, [tool, inkingManager]);
108
+
109
+ /**
110
+ * Sets the offset of the inkingManager based on the 'offset' prop
111
+ */
112
+ React.useEffect(() => {
113
+ if (inkingManager && offset !== undefined) {
114
+ inkingManager.offset = {
115
+ x: offset.x,
116
+ y: offset.y,
117
+ };
118
+ }
119
+ }, [offset?.x, offset?.y, inkingManager]);
120
+
121
+ /**
122
+ * Sets the scale of the inkingManager based on the 'scale' prop
123
+ */
124
+ React.useEffect(() => {
125
+ if (inkingManager && scale !== undefined) {
126
+ inkingManager.scale = scale;
127
+ }
128
+ }, [scale, inkingManager]);
129
+
130
+ /**
131
+ * Sets the referencePoint of the inkingManager based on the 'referencePoint' prop
132
+ */
133
+ React.useEffect(() => {
134
+ if (inkingManager && referencePoint !== undefined) {
135
+ inkingManager.referencePoint = referencePoint;
136
+ }
137
+ }, [referencePoint, inkingManager]);
138
+
139
+ /**
140
+ * Sets the lineBrush of the inkingManager based on the 'lineBrush' prop
141
+ */
142
+ React.useEffect(() => {
143
+ if (inkingManager && lineBrush) {
144
+ inkingManager.lineBrush = {
145
+ color: lineBrush.color,
146
+ tip: lineBrush.tip,
147
+ tipSize: lineBrush.tipSize,
148
+ type: lineBrush.type,
149
+ endArrow: lineBrush.endArrow,
150
+ };
151
+ }
152
+ }, [
153
+ lineBrush?.color,
154
+ lineBrush?.tip,
155
+ lineBrush?.tipSize,
156
+ lineBrush?.type,
157
+ lineBrush?.endArrow,
158
+ inkingManager,
159
+ ]);
160
+
161
+ /**
162
+ * Sets the isCursorShared of the liveCanvas based on the 'isCursorShared' prop
163
+ */
164
+ React.useEffect(() => {
165
+ if (
166
+ liveCanvas &&
167
+ isCursorShared !== undefined
168
+ ) {
169
+ liveCanvas.isCursorShared = isCursorShared;
170
+ }
171
+ }, [isCursorShared, liveCanvas]);
172
+
173
+ /**
174
+ * Sets the onGetLocalUserInfo method of the liveCanvas based on the 'localUserCursor' prop
175
+ */
176
+ React.useEffect(() => {
177
+ if (liveCanvas && localUserCursor) {
178
+ liveCanvas.onGetLocalUserInfo = (): IUserInfo | undefined => {
179
+ return {
180
+ displayName: localUserCursor.displayName,
181
+ pictureUri: localUserCursor.pictureUri,
182
+ };
183
+ };
184
+ }
185
+ }, [
186
+ localUserCursor?.displayName,
187
+ localUserCursor?.pictureUri,
188
+ liveCanvas,
189
+ ]);
190
+
191
+ /**
192
+ * Return hook response
193
+ */
194
+ return {
195
+ inkingManager,
196
+ liveCanvas,
197
+ };
198
+ }
@@ -0,0 +1,117 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import {
7
+ LiveEventEvents,
8
+ UserMeetingRole,
9
+ LiveEvent,
10
+ } from "@microsoft/live-share";
11
+ import React from "react";
12
+ import {
13
+ IUseLiveEventResults,
14
+ OnReceivedLiveEventAction,
15
+ SendLiveEventAction,
16
+ } from "../types";
17
+ import { IReceiveLiveEvent } from "../interfaces";
18
+ import { useDynamicDDS } from "../shared-hooks";
19
+
20
+ /**
21
+ * React hook for using a Live Share `LiveEvent`.
22
+ *
23
+ * @remarks
24
+ * Use this hook if you want to send transient JSON objects to everyone connected to the Fluid container,
25
+ * such as when sending push notifications or reactions.
26
+ *
27
+ * @template TEvent Optional typing for events sent & received. Default is `object` type.
28
+ * @param uniqueKey the unique key for the `LiveEvent`. If one does not yet exist, a new one
29
+ * will be created, otherwise it will use the existing one.
30
+ * @param allowedRoles Optional. The meeting roles eligible to send events through this object.
31
+ * @param onReceivedEvent Optional. Callback method to be called when a new notification is received.
32
+ * @returns stateful `latestEvent` & `allEvents` list, `sendEvent` callback, and the `liveEvent` object.
33
+ */
34
+ export function useLiveEvent<TEvent extends object = object>(
35
+ uniqueKey: string,
36
+ allowedRoles?: UserMeetingRole[],
37
+ onReceivedEvent?: OnReceivedLiveEventAction<TEvent>
38
+ ): IUseLiveEventResults<TEvent> {
39
+ /**
40
+ * Stateful latest event (user facing) and its non-user-facing setter method.
41
+ */
42
+ const [latestEvent, setLatestReceived] =
43
+ React.useState<IReceiveLiveEvent<TEvent>>();
44
+ /**
45
+ * Reference for all received/sent events. The current value of this is user-facing. Because
46
+ * this is always set at the same time as latestEvent, it is effectively a stateful value.
47
+ */
48
+ const allEventsRef = React.useRef<IReceiveLiveEvent<TEvent>[]>([]);
49
+ /**
50
+ * User facing: dynamically load the DDS for the given unique key.
51
+ */
52
+ const { dds: liveEvent } = useDynamicDDS<LiveEvent>(uniqueKey, LiveEvent);
53
+
54
+ /**
55
+ * User facing: callback to send event through `LiveEvent`
56
+ */
57
+ const sendEvent: SendLiveEventAction<TEvent> = React.useCallback(
58
+ (event: TEvent) => {
59
+ if (liveEvent?.isInitialized === undefined) {
60
+ console.error(
61
+ new Error(
62
+ "Cannot call emitEvent when liveEvent is undefined"
63
+ )
64
+ );
65
+ return;
66
+ }
67
+ if (!liveEvent.isInitialized) {
68
+ console.error(
69
+ new Error(
70
+ "Cannot call emitEvent while liveEvent is not started"
71
+ )
72
+ );
73
+ return;
74
+ }
75
+ liveEvent?.sendEvent(event);
76
+ },
77
+ [liveEvent?.isInitialized]
78
+ );
79
+
80
+ /**
81
+ * Setup change listeners and start `LiveEvent` if needed
82
+ */
83
+ React.useEffect(() => {
84
+ if (liveEvent?.isInitialized === undefined)
85
+ return;
86
+ // Register event listener
87
+ const onEventReceived = (event: any, local: boolean) => {
88
+ // If developer passed the optional onReceivedEvent callback, we
89
+ // call it.
90
+ onReceivedEvent?.(event as TEvent, local);
91
+ // Set the received event to our local state
92
+ const received: IReceiveLiveEvent<TEvent> = {
93
+ event: event as TEvent,
94
+ local,
95
+ };
96
+ allEventsRef.current = [...allEventsRef.current, received];
97
+ setLatestReceived(received);
98
+ };
99
+ liveEvent.on(LiveEventEvents.received, onEventReceived);
100
+ if (!liveEvent.isInitialized) {
101
+ // Start live event
102
+ liveEvent.initialize(allowedRoles);
103
+ }
104
+
105
+ return () => {
106
+ // on unmount, remove event listeners
107
+ liveEvent?.off(LiveEventEvents.received, onEventReceived);
108
+ };
109
+ }, [liveEvent?.isInitialized]);
110
+
111
+ return {
112
+ latestEvent,
113
+ allEvents: allEventsRef.current,
114
+ sendEvent,
115
+ liveEvent,
116
+ };
117
+ }
@@ -0,0 +1,124 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import {
7
+ LivePresenceUser,
8
+ PresenceState,
9
+ LivePresence,
10
+ } from "@microsoft/live-share";
11
+ import React from "react";
12
+ import { IUseLivePresenceResults } from "../types";
13
+ import { useDynamicDDS } from "../shared-hooks";
14
+
15
+ /**
16
+ * React hook for using a Live Share `LivePresence`.
17
+ *
18
+ * @remarks
19
+ * Use this hook if you want to track presence for who is currently using this component, such as
20
+ * who is online or who is viewing a specific document. With presence, you can sent along any custom
21
+ * user data. This is useful for rendering a list of users, profile pictures, cursor positions, and more.
22
+ *
23
+ * @template TData Optional typing for the custom user presence data object. Default is `object` type.
24
+
25
+ * @param userId Optional. The unique ID for a user. If none is provided, a random UUID will be generated.
26
+ * @param initialData Optional. Initial presence data object for the user.
27
+ * @param initialPresenceState Optional. Initial status of the user's presence. Default is online.
28
+ * @param uniqueKey Optional. The unique key for `LivePresence`. If one does not yet exist, a new one
29
+ * will be created, otherwise it will use the existing one. Default value is ":<dds-default>"
30
+ * @returns stateful `localUser`, `otherUsers` list, and `allUsers` list. Also returns a callback method
31
+ * to update the local user's presence and the `LivePresence` Fluid object.
32
+ */
33
+ export function useLivePresence<TData extends object = object>(
34
+ userId?: string | undefined,
35
+ initialData?: TData | undefined,
36
+ initialPresenceState: PresenceState = PresenceState.online,
37
+ uniqueKey: string = ":<dds-default>"
38
+ ): IUseLivePresenceResults<TData> {
39
+ /**
40
+ * Stateful all user presence list and its non-user-facing setter method.
41
+ */
42
+ const [allUsers, setAllUsers] = React.useState<LivePresenceUser<TData>[]>(
43
+ []
44
+ );
45
+ /**
46
+ * User facing: dynamically load the DDS for the given unique key.
47
+ */
48
+ const { dds: livePresence } = useDynamicDDS<LivePresence<TData>>(uniqueKey, LivePresence<TData>);
49
+
50
+ /**
51
+ * User facing: list of non-local user's presence objects.
52
+ */
53
+ const otherUsers = React.useMemo<LivePresenceUser<TData>[]>(() => {
54
+ return [...allUsers.filter((user) => !user.isLocalUser)];
55
+ }, [allUsers]);
56
+
57
+ /**
58
+ * User facing: local user's presence object.
59
+ */
60
+ const localUser = React.useMemo<LivePresenceUser<TData> | undefined>(() => {
61
+ return allUsers.find((user) => user.isLocalUser);
62
+ }, [allUsers]);
63
+
64
+ /**
65
+ * User facing: callback to update the local user's presence.
66
+ */
67
+ const updatePresence = React.useCallback(
68
+ (state?: PresenceState | undefined, data?: TData | undefined) => {
69
+ if (!livePresence) {
70
+ console.error(
71
+ new Error(
72
+ "Cannot call updatePresence when presence is undefined"
73
+ )
74
+ );
75
+ return;
76
+ }
77
+ if (!livePresence.isInitialized) {
78
+ console.error(
79
+ new Error(
80
+ "Cannot call updatePresence while presence is not started"
81
+ )
82
+ );
83
+ return;
84
+ }
85
+ livePresence.updatePresence(state, data);
86
+ },
87
+ [livePresence]
88
+ );
89
+
90
+ /**
91
+ * Setup change listeners and start `LivePresence` if needed
92
+ */
93
+ React.useEffect(() => {
94
+ if (livePresence === undefined)
95
+ return;
96
+
97
+ const onPresenceChanged = () => {
98
+ const updatedLocalUsers: LivePresenceUser<TData>[] = [];
99
+ livePresence?.forEach((user) => {
100
+ updatedLocalUsers.push(user);
101
+ });
102
+ setAllUsers(updatedLocalUsers);
103
+ };
104
+ livePresence.on("presenceChanged", onPresenceChanged);
105
+
106
+ if (!livePresence.isInitialized) {
107
+ livePresence.initialize(userId, initialData, initialPresenceState);
108
+ } else {
109
+ onPresenceChanged();
110
+ }
111
+
112
+ return () => {
113
+ livePresence?.off("presenceChanged", onPresenceChanged);
114
+ };
115
+ }, [livePresence]);
116
+
117
+ return {
118
+ localUser,
119
+ otherUsers,
120
+ allUsers,
121
+ livePresence,
122
+ updatePresence,
123
+ };
124
+ }
@@ -0,0 +1,87 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import { LiveState, UserMeetingRole } from "@microsoft/live-share";
7
+ import React from "react";
8
+ import { SetLiveStateAction } from "../types";
9
+ import { useDynamicDDS } from "../shared-hooks";
10
+
11
+ /**
12
+ * React hook for using a Live Share `LiveState`.
13
+ *
14
+ * @remarks
15
+ * Use this hook if you want to synchronize app state that will reset when all users leave the session.
16
+ *
17
+ * @param uniqueKey the unique key for the `LiveEvent`. If one does not yet exist, a new one will be created, otherwise it will use the existing one.
18
+ * @param initialState Optional. the initial state value of type TState
19
+ * @param allowedRoles Optional. the user roles that are allowed to mutate the synchronized state
20
+ * @returns ordered values: first value is the synchronized state value and the second is a setter to change the state value.
21
+ */
22
+ export function useLiveState<
23
+ TState = any,
24
+ >(
25
+ uniqueKey: string,
26
+ initialState: TState,
27
+ allowedRoles?: UserMeetingRole[]
28
+ ): [TState, SetLiveStateAction<TState>] {
29
+ const [currentState, setCurrentState] = React.useState<TState>(initialState);
30
+ /**
31
+ * User facing: dynamically load the DDS for the given unique key.
32
+ */
33
+ const { dds: liveState } = useDynamicDDS<LiveState<TState>>(
34
+ uniqueKey,
35
+ LiveState<TState>
36
+ );
37
+
38
+ /**
39
+ * Change state callback that is user facing
40
+ */
41
+ const setState = React.useCallback(
42
+ (state: TState) => {
43
+ if (!liveState) {
44
+ console.error(
45
+ new Error(
46
+ "Cannot call changeState when liveState is undefined"
47
+ )
48
+ );
49
+ return;
50
+ }
51
+ if (!liveState.isInitialized) {
52
+ console.error(
53
+ new Error(
54
+ "Cannot call changeState while liveState is not started"
55
+ )
56
+ );
57
+ return;
58
+ }
59
+ liveState.set(state);
60
+ },
61
+ [liveState]
62
+ );
63
+
64
+ /**
65
+ * Setup change listeners and start `LiveState` if needed
66
+ */
67
+ React.useEffect(() => {
68
+ if (liveState === undefined) return;
69
+
70
+ const onStateChanged = (state: TState) => {
71
+ setCurrentState(state);
72
+ };
73
+ liveState.on("stateChanged", onStateChanged);
74
+ if (!liveState.isInitialized) {
75
+ liveState.initialize(initialState, allowedRoles);
76
+ }
77
+ if (JSON.stringify(liveState.state) !== JSON.stringify(initialState)) {
78
+ onStateChanged(liveState.state);
79
+ }
80
+
81
+ return () => {
82
+ liveState?.off("stateChanged", onStateChanged);
83
+ };
84
+ }, [liveState]);
85
+
86
+ return [currentState, setState];
87
+ }