Skip to content

Commit 1dbe34e

Browse files
izruffwrjgoldRichDom2185
authored
Implement participant indicator for sessions (#2948)
* Added classes for cursors UI * Added basic implementation of participant cursors * Added support for multiple cursors * Added more info to user presence * Added user name and color identifier into cursors * Fixed formatting * added functions to check for cursors and clean up cursors when user exits sessions * Added random colour generator * Passed cursor manager to ShareAce binding instead of handling in frontend * Removed comments and added credits * Install `ace-collab-ext` dependency * Replace files with those from library * Restore CSS import * Fix hooks warning --------- Co-authored-by: wrjgold <trexwrj@gmail.com> Co-authored-by: Richard Dominick <34370238+RichDom2185@users.noreply.github.com>
1 parent 0eb286d commit 1dbe34e

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
105105
"@babel/preset-typescript": "^7.24.1",
106106
"@babel/runtime": "^7.24.5",
107+
"@convergencelabs/ace-collab-ext": "^0.6.0",
107108
"@craco/craco": "^7.1.0",
108109
"@svgr/webpack": "^8.0.0",
109110
"@testing-library/jest-dom": "^6.0.0",

src/commons/editor/UseShareAce.tsx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import '@convergencelabs/ace-collab-ext/dist/css/ace-collab-ext.css';
2+
3+
import { AceMultiCursorManager } from '@convergencelabs/ace-collab-ext';
14
import * as Sentry from '@sentry/browser';
25
import sharedbAce from '@sourceacademy/sharedb-ace';
3-
import React from 'react';
6+
import React, { useMemo } from 'react';
47

58
import { getDocInfoFromSessionId, getSessionUrl } from '../collabEditing/CollabEditingHelper';
9+
import { useSession } from '../utils/Hooks';
610
import { showSuccessMessage } from '../utils/notifications/NotificationsHelper';
711
import { EditorHook } from './Editor';
812

@@ -21,20 +25,27 @@ const useShareAce: EditorHook = (inProps, outProps, keyBindings, reactAceRef) =>
2125

2226
const { editorSessionId, sessionDetails } = inProps;
2327

28+
const { name } = useSession();
29+
30+
const user = useMemo(() => ({ name, color: getColor() }), [name]);
31+
2432
React.useEffect(() => {
2533
if (!editorSessionId || !sessionDetails) {
2634
return;
2735
}
2836

2937
const editor = reactAceRef.current!.editor;
38+
const cursorManager = new AceMultiCursorManager(editor.getSession());
3039
const ShareAce = new sharedbAce(sessionDetails.docId, {
40+
user,
41+
cursorManager,
3142
WsUrl: getSessionUrl(editorSessionId, true),
3243
pluginWsUrl: null,
3344
namespace: 'sa'
3445
});
3546

3647
ShareAce.on('ready', () => {
37-
ShareAce.add(editor, ['contents'], []);
48+
ShareAce.add(editor, cursorManager, ['contents'], []);
3849
propsRef.current.handleSetSharedbConnected!(true);
3950

4051
// Disables editor in a read-only session
@@ -87,8 +98,23 @@ const useShareAce: EditorHook = (inProps, outProps, keyBindings, reactAceRef) =>
8798

8899
// Resets editor to normal after leaving the session
89100
editor.setReadOnly(false);
101+
102+
// Removes all cursors
103+
cursorManager.removeAll();
90104
};
91-
}, [editorSessionId, sessionDetails, reactAceRef]);
105+
}, [editorSessionId, sessionDetails, reactAceRef, user]);
92106
};
93107

108+
function getColor() {
109+
return (
110+
'hsl(' +
111+
360 * Math.random() +
112+
',' +
113+
(25 + 70 * Math.random()) +
114+
'%,' +
115+
(50 + 20 * Math.random()) +
116+
'%)'
117+
);
118+
}
119+
94120
export default useShareAce;

yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,13 @@
12861286
resolved "https://registry.yarnpkg.com/@commander-js/extra-typings/-/extra-typings-12.0.1.tgz#4a74a9ccf1d19ef24e71df59359c6d90a605a469"
12871287
integrity sha512-OvkMobb1eMqOCuJdbuSin/KJkkZr7n24/UNV+Lcz/0Dhepf3r2p9PaGwpRpAWej7A+gQnny4h8mGhpFl4giKkg==
12881288

1289+
"@convergencelabs/ace-collab-ext@^0.6.0":
1290+
version "0.6.0"
1291+
resolved "https://registry.yarnpkg.com/@convergencelabs/ace-collab-ext/-/ace-collab-ext-0.6.0.tgz#dc914080f84312916940ed1e0d476fed7582a691"
1292+
integrity sha512-QizJXn66bHu688jrMD6X18YmePu0YXmL8a/4EOxMOPFepDioS34oz1TTmL7oq1AHRBhCRAxKHJao53LbqyJ8vQ==
1293+
dependencies:
1294+
ace-builds "^1.4.12"
1295+
12891296
"@craco/craco@^7.1.0":
12901297
version "7.1.0"
12911298
resolved "https://registry.yarnpkg.com/@craco/craco/-/craco-7.1.0.tgz#12bd394c7f0334e214302e4d35a1768f68042fbb"
@@ -3684,7 +3691,7 @@ accepts@~1.3.4, accepts@~1.3.5, accepts@~1.3.8:
36843691
mime-types "~2.1.34"
36853692
negotiator "0.6.3"
36863693

3687-
ace-builds@^1.4.14:
3694+
ace-builds@^1.4.12, ace-builds@^1.4.14:
36883695
version "1.15.3"
36893696
resolved "https://registry.yarnpkg.com/ace-builds/-/ace-builds-1.15.3.tgz#9e35d174acb80297c6f236be2c8bb8574786f719"
36903697
integrity sha512-hq8+4DfQcUYcUyAF3vF7UoGFXwNxXST5A2IdarUOp9/Xg1thWTfxusPI2HAlTvXRTVjLDQOj9O34uPoTehEs0A==

0 commit comments

Comments
 (0)