Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft PR for issue #261 #286

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions jupyter_collaboration/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This file is auto-generated by Hatchling. As such, do not:
# - modify
# - track in version control e.g. be sure to add to .gitignore
__version__ = VERSION = '2.0.11'
44 changes: 42 additions & 2 deletions packages/collaboration/src/cursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import {
Annotation,
EditorSelection,
EditorState,
Extension,
Facet
Facet,
StateField,
} from '@codemirror/state';
import {
EditorView,
Expand All @@ -15,7 +17,11 @@ import {
RectangleMarker,
tooltips,
ViewPlugin,
ViewUpdate
ViewUpdate,
Tooltip,
showTooltip,
Decoration,
DecorationSet,
} from '@codemirror/view';
import { User } from '@jupyterlab/services';
import { JSONExt } from '@lumino/coreutils';
Expand Down Expand Up @@ -276,6 +282,39 @@ const userHover = hoverTooltip(
}
);

/**
* Extension displaying display name on the cursor currently typing
*/
const userTypingTooltipField = StateField.define<readonly Tooltip[]>({
create: (state) => getUserTypingTooltips(state),
update: (tooltips, tr) => {
if (!tr.docChanged && !tr.selection) return tooltips;
return getUserTypingTooltips(tr.state);
},
provide: (f) => showTooltip.computeN([f], (state) => state.field(f)),
});

function getUserTypingTooltips(state: EditorState): readonly Tooltip[] {
const { awareness } = state.facet(editorAwarenessFacet);
const localAwarenessState = awareness.getLocalState() as IAwarenessState | null;

if (localAwarenessState && localAwarenessState.state.cursors?.length > 0) {
return localAwarenessState.state.cursors.map((cursor: { head: { index: any; }; }) => ({
pos: cursor.head.index,
above: true,
create: () => {
const dom = document.createElement("div");
dom.classList.add("jp-remote-userInfo");
dom.style.backgroundColor = localAwarenessState.user?.color ?? "darkgrey";
dom.textContent = localAwarenessState.user?.display_name ?? "Anonymous";
return { dom };
},
}));
}

return [];
}

/**
* Extension defining a new editor layer storing the remote selections
*/
Expand Down Expand Up @@ -428,6 +467,7 @@ const showCollaborators = ViewPlugin.fromClass(
remoteCursorsLayer,
remoteSelectionLayer,
userHover,
userTypingTooltipField,
// As we use relative positioning of widget, the tooltip must be positioned absolutely
// And we attach the tooltip to the body to avoid overflow rules
tooltips({ position: 'absolute', parent: document.body })
Expand Down