@microsoft/live-share-react 1.0.0-preview.5
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.
- package/.nycrc.json +10 -0
- package/LICENSE +87 -0
- package/README.md +543 -0
- package/bin/index.d.ts +7 -0
- package/bin/index.d.ts.map +1 -0
- package/bin/index.js +7 -0
- package/bin/index.js.map +1 -0
- package/bin/interfaces/LiveInterfaceExtensions.d.ts +9 -0
- package/bin/interfaces/LiveInterfaceExtensions.d.ts.map +1 -0
- package/bin/interfaces/LiveInterfaceExtensions.js +6 -0
- package/bin/interfaces/LiveInterfaceExtensions.js.map +1 -0
- package/bin/interfaces/index.d.ts +2 -0
- package/bin/interfaces/index.d.ts.map +1 -0
- package/bin/interfaces/index.js +2 -0
- package/bin/interfaces/index.js.map +1 -0
- package/bin/internal-hooks/index.d.ts +2 -0
- package/bin/internal-hooks/index.d.ts.map +1 -0
- package/bin/internal-hooks/index.js +2 -0
- package/bin/internal-hooks/index.js.map +1 -0
- package/bin/internal-hooks/useDynamicDDSRegistry.d.ts +14 -0
- package/bin/internal-hooks/useDynamicDDSRegistry.d.ts.map +1 -0
- package/bin/internal-hooks/useDynamicDDSRegistry.js +142 -0
- package/bin/internal-hooks/useDynamicDDSRegistry.js.map +1 -0
- package/bin/internal-hooks/useSharedStateRegistry.d.ts +31 -0
- package/bin/internal-hooks/useSharedStateRegistry.d.ts.map +1 -0
- package/bin/internal-hooks/useSharedStateRegistry.js +100 -0
- package/bin/internal-hooks/useSharedStateRegistry.js.map +1 -0
- package/bin/internal-hooks/useStateRef.d.ts +7 -0
- package/bin/internal-hooks/useStateRef.d.ts.map +1 -0
- package/bin/internal-hooks/useStateRef.js +15 -0
- package/bin/internal-hooks/useStateRef.js.map +1 -0
- package/bin/live-hooks/index.d.ts +7 -0
- package/bin/live-hooks/index.d.ts.map +1 -0
- package/bin/live-hooks/index.js +7 -0
- package/bin/live-hooks/index.js.map +1 -0
- package/bin/live-hooks/useLiveAICompletion.d.ts +29 -0
- package/bin/live-hooks/useLiveAICompletion.d.ts.map +1 -0
- package/bin/live-hooks/useLiveAICompletion.js +186 -0
- package/bin/live-hooks/useLiveAICompletion.js.map +1 -0
- package/bin/live-hooks/useLiveCanvas.d.ts +28 -0
- package/bin/live-hooks/useLiveCanvas.d.ts.map +1 -0
- package/bin/live-hooks/useLiveCanvas.js +167 -0
- package/bin/live-hooks/useLiveCanvas.js.map +1 -0
- package/bin/live-hooks/useLiveCoPilot.d.ts +28 -0
- package/bin/live-hooks/useLiveCoPilot.d.ts.map +1 -0
- package/bin/live-hooks/useLiveCoPilot.js +178 -0
- package/bin/live-hooks/useLiveCoPilot.js.map +1 -0
- package/bin/live-hooks/useLiveEvent.d.ts +22 -0
- package/bin/live-hooks/useLiveEvent.d.ts.map +1 -0
- package/bin/live-hooks/useLiveEvent.js +86 -0
- package/bin/live-hooks/useLiveEvent.js.map +1 -0
- package/bin/live-hooks/useLivePresence.d.ts +26 -0
- package/bin/live-hooks/useLivePresence.d.ts.map +1 -0
- package/bin/live-hooks/useLivePresence.js +93 -0
- package/bin/live-hooks/useLivePresence.js.map +1 -0
- package/bin/live-hooks/useLiveState.d.ts +20 -0
- package/bin/live-hooks/useLiveState.d.ts.map +1 -0
- package/bin/live-hooks/useLiveState.js +71 -0
- package/bin/live-hooks/useLiveState.js.map +1 -0
- package/bin/live-hooks/useLiveTimer.d.ts +8 -0
- package/bin/live-hooks/useLiveTimer.d.ts.map +1 -0
- package/bin/live-hooks/useLiveTimer.js +132 -0
- package/bin/live-hooks/useLiveTimer.js.map +1 -0
- package/bin/live-hooks/useMediaSynchronizer.d.ts +23 -0
- package/bin/live-hooks/useMediaSynchronizer.d.ts.map +1 -0
- package/bin/live-hooks/useMediaSynchronizer.js +162 -0
- package/bin/live-hooks/useMediaSynchronizer.js.map +1 -0
- package/bin/live-share-turbo/AzureTurboClient.d.ts +39 -0
- package/bin/live-share-turbo/AzureTurboClient.d.ts.map +1 -0
- package/bin/live-share-turbo/AzureTurboClient.js +56 -0
- package/bin/live-share-turbo/AzureTurboClient.js.map +1 -0
- package/bin/live-share-turbo/LiveShareTurboClient.d.ts +53 -0
- package/bin/live-share-turbo/LiveShareTurboClient.d.ts.map +1 -0
- package/bin/live-share-turbo/LiveShareTurboClient.js +69 -0
- package/bin/live-share-turbo/LiveShareTurboClient.js.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboDataObject.d.ts +31 -0
- package/bin/live-share-turbo/dds-objects/TurboDataObject.d.ts.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboDataObject.js +75 -0
- package/bin/live-share-turbo/dds-objects/TurboDataObject.js.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveCanvas.d.ts +48 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveCanvas.d.ts.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveCanvas.js +104 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveCanvas.js.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveEvent.d.ts +28 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveEvent.d.ts.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveEvent.js +68 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveEvent.js.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveMediaSession.d.ts +60 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveMediaSession.d.ts.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveMediaSession.js +111 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveMediaSession.js.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboLivePresence.d.ts +77 -0
- package/bin/live-share-turbo/dds-objects/TurboLivePresence.d.ts.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboLivePresence.js +137 -0
- package/bin/live-share-turbo/dds-objects/TurboLivePresence.js.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveState.d.ts +30 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveState.d.ts.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveState.js +74 -0
- package/bin/live-share-turbo/dds-objects/TurboLiveState.js.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboSharedMap.d.ts +61 -0
- package/bin/live-share-turbo/dds-objects/TurboSharedMap.d.ts.map +1 -0
- package/bin/live-share-turbo/dds-objects/TurboSharedMap.js +108 -0
- package/bin/live-share-turbo/dds-objects/TurboSharedMap.js.map +1 -0
- package/bin/live-share-turbo/dds-objects/index.d.ts +8 -0
- package/bin/live-share-turbo/dds-objects/index.d.ts.map +1 -0
- package/bin/live-share-turbo/dds-objects/index.js +8 -0
- package/bin/live-share-turbo/dds-objects/index.js.map +1 -0
- package/bin/live-share-turbo/index.d.ts +5 -0
- package/bin/live-share-turbo/index.d.ts.map +1 -0
- package/bin/live-share-turbo/index.js +5 -0
- package/bin/live-share-turbo/index.js.map +1 -0
- package/bin/live-share-turbo/interfaces/IFluidTurboClient.d.ts +15 -0
- package/bin/live-share-turbo/interfaces/IFluidTurboClient.d.ts.map +1 -0
- package/bin/live-share-turbo/interfaces/IFluidTurboClient.js +2 -0
- package/bin/live-share-turbo/interfaces/IFluidTurboClient.js.map +1 -0
- package/bin/live-share-turbo/interfaces/index.d.ts +3 -0
- package/bin/live-share-turbo/interfaces/index.d.ts.map +1 -0
- package/bin/live-share-turbo/interfaces/index.js +3 -0
- package/bin/live-share-turbo/interfaces/index.js.map +1 -0
- package/bin/live-share-turbo/internals/FluidTurboClient.d.ts +39 -0
- package/bin/live-share-turbo/internals/FluidTurboClient.d.ts.map +1 -0
- package/bin/live-share-turbo/internals/FluidTurboClient.js +121 -0
- package/bin/live-share-turbo/internals/FluidTurboClient.js.map +1 -0
- package/bin/live-share-turbo/internals/index.d.ts +2 -0
- package/bin/live-share-turbo/internals/index.d.ts.map +1 -0
- package/bin/live-share-turbo/internals/index.js +2 -0
- package/bin/live-share-turbo/internals/index.js.map +1 -0
- package/bin/live-share-turbo/internals/types.d.ts +4 -0
- package/bin/live-share-turbo/internals/types.d.ts.map +1 -0
- package/bin/live-share-turbo/internals/types.js +2 -0
- package/bin/live-share-turbo/internals/types.js.map +1 -0
- package/bin/providers/AzureProvider.d.ts +34 -0
- package/bin/providers/AzureProvider.d.ts.map +1 -0
- package/bin/providers/AzureProvider.js +102 -0
- package/bin/providers/AzureProvider.js.map +1 -0
- package/bin/providers/FluidContextProvider.d.ts +34 -0
- package/bin/providers/FluidContextProvider.d.ts.map +1 -0
- package/bin/providers/FluidContextProvider.js +99 -0
- package/bin/providers/FluidContextProvider.js.map +1 -0
- package/bin/providers/LiveShareContextProvider.d.ts +42 -0
- package/bin/providers/LiveShareContextProvider.d.ts.map +1 -0
- package/bin/providers/LiveShareContextProvider.js +63 -0
- package/bin/providers/LiveShareContextProvider.js.map +1 -0
- package/bin/providers/LiveShareProvider.d.ts +42 -0
- package/bin/providers/LiveShareProvider.d.ts.map +1 -0
- package/bin/providers/LiveShareProvider.js +66 -0
- package/bin/providers/LiveShareProvider.js.map +1 -0
- package/bin/providers/index.d.ts +3 -0
- package/bin/providers/index.d.ts.map +1 -0
- package/bin/providers/index.js +3 -0
- package/bin/providers/index.js.map +1 -0
- package/bin/shared-hooks/index.d.ts +5 -0
- package/bin/shared-hooks/index.d.ts.map +1 -0
- package/bin/shared-hooks/index.js +5 -0
- package/bin/shared-hooks/index.js.map +1 -0
- package/bin/shared-hooks/useDynamicDDS.d.ts +19 -0
- package/bin/shared-hooks/useDynamicDDS.d.ts.map +1 -0
- package/bin/shared-hooks/useDynamicDDS.js +68 -0
- package/bin/shared-hooks/useDynamicDDS.js.map +1 -0
- package/bin/shared-hooks/useLockedTask.d.ts +11 -0
- package/bin/shared-hooks/useLockedTask.d.ts.map +1 -0
- package/bin/shared-hooks/useLockedTask.js +82 -0
- package/bin/shared-hooks/useLockedTask.js.map +1 -0
- package/bin/shared-hooks/useSharedMap.d.ts +22 -0
- package/bin/shared-hooks/useSharedMap.d.ts.map +1 -0
- package/bin/shared-hooks/useSharedMap.js +106 -0
- package/bin/shared-hooks/useSharedMap.js.map +1 -0
- package/bin/shared-hooks/useSharedState.d.ts +21 -0
- package/bin/shared-hooks/useSharedState.d.ts.map +1 -0
- package/bin/shared-hooks/useSharedState.js +63 -0
- package/bin/shared-hooks/useSharedState.js.map +1 -0
- package/bin/shared-hooks/useTaskManager.d.ts +13 -0
- package/bin/shared-hooks/useTaskManager.d.ts.map +1 -0
- package/bin/shared-hooks/useTaskManager.js +86 -0
- package/bin/shared-hooks/useTaskManager.js.map +1 -0
- package/bin/types/ActionTypes.d.ts +30 -0
- package/bin/types/ActionTypes.d.ts.map +1 -0
- package/bin/types/ActionTypes.js +6 -0
- package/bin/types/ActionTypes.js.map +1 -0
- package/bin/types/PropTypes.d.ts +8 -0
- package/bin/types/PropTypes.d.ts.map +1 -0
- package/bin/types/PropTypes.js +6 -0
- package/bin/types/PropTypes.js.map +1 -0
- package/bin/types/ResultTypes.d.ts +163 -0
- package/bin/types/ResultTypes.d.ts.map +1 -0
- package/bin/types/ResultTypes.js +6 -0
- package/bin/types/ResultTypes.js.map +1 -0
- package/bin/types/index.d.ts +4 -0
- package/bin/types/index.d.ts.map +1 -0
- package/bin/types/index.js +4 -0
- package/bin/types/index.js.map +1 -0
- package/bin/utils/getContainerSchema.d.ts +8 -0
- package/bin/utils/getContainerSchema.d.ts.map +1 -0
- package/bin/utils/getContainerSchema.js +41 -0
- package/bin/utils/getContainerSchema.js.map +1 -0
- package/bin/utils/index.d.ts +2 -0
- package/bin/utils/index.d.ts.map +1 -0
- package/bin/utils/index.js +2 -0
- package/bin/utils/index.js.map +1 -0
- package/bin/utils/type-check.d.ts +15 -0
- package/bin/utils/type-check.d.ts.map +1 -0
- package/bin/utils/type-check.js +54 -0
- package/bin/utils/type-check.js.map +1 -0
- package/package.json +47 -0
- package/src/index.ts +6 -0
- package/src/interfaces/LiveInterfaceExtensions.ts +9 -0
- package/src/interfaces/index.ts +1 -0
- package/src/internal-hooks/index.ts +1 -0
- package/src/internal-hooks/useSharedStateRegistry.ts +163 -0
- package/src/live-hooks/index.ts +6 -0
- package/src/live-hooks/useLiveCanvas.ts +198 -0
- package/src/live-hooks/useLiveEvent.ts +117 -0
- package/src/live-hooks/useLivePresence.ts +124 -0
- package/src/live-hooks/useLiveState.ts +108 -0
- package/src/live-hooks/useLiveTimer.ts +184 -0
- package/src/live-hooks/useMediaSynchronizer.ts +200 -0
- package/src/providers/AzureProvider.tsx +177 -0
- package/src/providers/LiveShareProvider.tsx +140 -0
- package/src/providers/index.ts +2 -0
- package/src/shared-hooks/index.ts +4 -0
- package/src/shared-hooks/useDynamicDDS.ts +71 -0
- package/src/shared-hooks/useSharedMap.ts +132 -0
- package/src/shared-hooks/useSharedState.ts +85 -0
- package/src/shared-hooks/useTaskManager.ts +82 -0
- package/src/types/ActionTypes.ts +76 -0
- package/src/types/PropTypes.ts +10 -0
- package/src/types/ResultTypes.ts +187 -0
- package/src/types/index.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/type-check.ts +69 -0
- package/tinylicious.log +4 -0
- package/tsconfig.build.json +6 -0
- 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?.isCursorShared !== undefined &&
|
|
167
|
+
isCursorShared !== undefined
|
|
168
|
+
) {
|
|
169
|
+
liveCanvas.isCursorShared = isCursorShared;
|
|
170
|
+
}
|
|
171
|
+
}, [isCursorShared, liveCanvas?.isCursorShared]);
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Sets the onGetLocalUserInfo method of the liveCanvas based on the 'localUserCursor' prop
|
|
175
|
+
*/
|
|
176
|
+
React.useEffect(() => {
|
|
177
|
+
if (liveCanvas?.onGetLocalUserInfo !== undefined && 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?.onGetLocalUserInfo,
|
|
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,108 @@
|
|
|
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
|
+
interface ILiveStateStatus<
|
|
12
|
+
TState extends string = string,
|
|
13
|
+
TData extends object = object
|
|
14
|
+
> {
|
|
15
|
+
state?: TState;
|
|
16
|
+
data?: TData;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* React hook for using a Live Share `LiveState`.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* Use this hook if you want to synchronize app state that will reset when all users leave the session.
|
|
24
|
+
*
|
|
25
|
+
* @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.
|
|
26
|
+
* @param initialState Optional. the initial state value of type TState
|
|
27
|
+
* @param initialData Optional. the initial data value of type TData
|
|
28
|
+
* @param allowedRoles Optional. the user roles that are allowed to mutate the synchronized state
|
|
29
|
+
* @returns ordered values: first value is the synchronized state value, second is synchronized data value, and third is a setter to change the state/data values.
|
|
30
|
+
*/
|
|
31
|
+
export function useLiveState<
|
|
32
|
+
TState extends string = string,
|
|
33
|
+
TData extends object = object
|
|
34
|
+
>(
|
|
35
|
+
uniqueKey: string,
|
|
36
|
+
initialState?: TState,
|
|
37
|
+
initialData?: TData,
|
|
38
|
+
allowedRoles?: UserMeetingRole[]
|
|
39
|
+
): [TState | undefined, TData | undefined, SetLiveStateAction<TState, TData>] {
|
|
40
|
+
const [current, setCurrent] = React.useState<
|
|
41
|
+
ILiveStateStatus<TState, TData>
|
|
42
|
+
>({
|
|
43
|
+
state: initialState,
|
|
44
|
+
data: initialData,
|
|
45
|
+
});
|
|
46
|
+
/**
|
|
47
|
+
* User facing: dynamically load the DDS for the given unique key.
|
|
48
|
+
*/
|
|
49
|
+
const { dds: liveState } = useDynamicDDS<LiveState<TData>>(
|
|
50
|
+
uniqueKey,
|
|
51
|
+
LiveState<TData>
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Change state callback that is user facing
|
|
56
|
+
*/
|
|
57
|
+
const changeState = React.useCallback(
|
|
58
|
+
(state: TState, value?: TData | undefined) => {
|
|
59
|
+
if (!liveState) {
|
|
60
|
+
console.error(
|
|
61
|
+
new Error(
|
|
62
|
+
"Cannot call changeState when liveState is undefined"
|
|
63
|
+
)
|
|
64
|
+
);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (!liveState.isInitialized) {
|
|
68
|
+
console.error(
|
|
69
|
+
new Error(
|
|
70
|
+
"Cannot call changeState while liveState is not started"
|
|
71
|
+
)
|
|
72
|
+
);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
liveState?.changeState(state, value);
|
|
76
|
+
},
|
|
77
|
+
[liveState]
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Setup change listeners and start `LiveState` if needed
|
|
82
|
+
*/
|
|
83
|
+
React.useEffect(() => {
|
|
84
|
+
if (liveState === undefined) return;
|
|
85
|
+
|
|
86
|
+
const onStateChanged = (state: TState, data: TData | undefined) => {
|
|
87
|
+
setCurrent({
|
|
88
|
+
state,
|
|
89
|
+
data,
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
liveState.on("stateChanged", onStateChanged);
|
|
93
|
+
if (!liveState.isInitialized) {
|
|
94
|
+
liveState.initialize(allowedRoles, initialState, initialData);
|
|
95
|
+
if (liveState.state) {
|
|
96
|
+
onStateChanged(liveState.state as TState, liveState.data);
|
|
97
|
+
}
|
|
98
|
+
} else if (liveState.state) {
|
|
99
|
+
onStateChanged(liveState.state as TState, liveState.data);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return () => {
|
|
103
|
+
liveState?.off("stateChanged", onStateChanged);
|
|
104
|
+
};
|
|
105
|
+
}, [liveState?.isInitialized]);
|
|
106
|
+
|
|
107
|
+
return [current?.state, current?.data, changeState];
|
|
108
|
+
}
|