-
Notifications
You must be signed in to change notification settings - Fork 21
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
Remote cursors pvh #25
Open
pvh
wants to merge
8
commits into
presence-color
Choose a base branch
from
remote-cursors-pvh
base: presence-color
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bdc82f9
work in progress on remote-cursors in cm
pvh f974606
crude cursor sharing
pvh 117e198
fragile selection spans now too
pvh 9032219
user colors now propagated
pvh b87dad7
continuing to futz with CSS, time to fix the duplicate cursors
pvh afc777c
use a ref. of course.
pvh 7a29cbb
remote cursors work
pvh 5c47b9c
remove unused file
pvh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {EditorView, WidgetType} from "@codemirror/view"; | ||
|
||
export class CursorWidget extends WidgetType { | ||
element: HTMLElement | null; | ||
timer: NodeJS.Timeout; | ||
|
||
constructor(private user: string, private color: string) { | ||
super(); | ||
} | ||
|
||
eq(other) { | ||
return other.user === this.user && other.color === this.color; | ||
} | ||
|
||
toDOM(view: EditorView) { | ||
//const cursorCoords = view.coordsAtPos(cursorPos); | ||
console.log(view) | ||
|
||
|
||
this.element = document.createElement("span"); | ||
this.element.className = "remote-cursor"; | ||
this.element.style.setProperty("--user-name", `"${this.user}"`); | ||
this.element.style.setProperty("--user-color", this.color); | ||
|
||
// Initially show the user name | ||
this.element.setAttribute("data-show-name", "true"); | ||
|
||
// Trigger the animation by toggling an attribute | ||
this.showAndHideName(this.element); | ||
|
||
return this.element; | ||
} | ||
|
||
showAndHideName(element) { | ||
// Reset the animation by removing and re-adding the attribute | ||
element.setAttribute("data-show-name", "true"); | ||
// Use a timeout to hide the name after a brief period | ||
if (this.timer) clearTimeout(this.timer); | ||
this.timer = setTimeout(() => { | ||
if (element) { // Check if the element still exists | ||
element.setAttribute("data-show-name", "false"); | ||
}}, 1500); // Matches the animation duration | ||
} | ||
|
||
ignoreEvent() { | ||
return false; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { EditorView, ViewPlugin, ViewUpdate } from "@codemirror/view"; | ||
import { SelectionData } from "."; | ||
|
||
export const collaborativePlugin = (remoteStateField, setLocalSelections: (s: SelectionData) => void) => ViewPlugin.fromClass(class { | ||
view: EditorView; | ||
constructor(view: EditorView) { | ||
this.view = view | ||
this.emitLocalChanges(view); | ||
} | ||
|
||
update(update: ViewUpdate) { | ||
if (update.selectionSet || update.docChanged) { | ||
this.emitLocalChanges(update.view); | ||
} | ||
} | ||
|
||
emitLocalChanges(view: EditorView) { | ||
const {state} = view; | ||
const selections = state.selection.ranges.map(r => ({from: r.from, to: r.to})); | ||
const cursor = state.selection.main.head; | ||
setLocalSelections({selections, cursor}) | ||
} | ||
}, { | ||
decorations: plugin => plugin.view.state.field(remoteStateField) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import {EditorView, Decoration, DecorationSet, ViewPlugin, ViewUpdate} from "@codemirror/view" | ||
import {StateField, StateEffect} from "@codemirror/state" | ||
|
||
export interface UserData { | ||
name: string, | ||
color: string | ||
} | ||
|
||
export interface SelectionData { | ||
selections: {from: number, to: number}[], | ||
cursor: number | ||
} | ||
|
||
export interface UserSelectionData { | ||
peerId: string, | ||
user: UserData, | ||
selection: SelectionData | ||
} | ||
|
||
// Effects to update remote selections and cursors | ||
import { CursorWidget } from "./CursorWidget"; | ||
|
||
export const setPeerSelectionData = StateEffect.define<UserSelectionData[]>(); | ||
|
||
// State field to track remote selections and cursors | ||
const remoteStateField = StateField.define<DecorationSet>({ | ||
create() { | ||
return Decoration.none; | ||
}, | ||
update(decorations, tr) { | ||
for (const effect of tr.effects) { | ||
if (effect.is(setPeerSelectionData)) { | ||
decorations = Decoration.none; | ||
effect.value.forEach(({user, selection}) => { | ||
if (!user || !selection) { console.log("missing", user, selection); return } | ||
// Make a widget for the cursor position. | ||
const widget = Decoration.widget({ | ||
widget: new CursorWidget(user.name, user.color), | ||
side: 1, | ||
}).range(selection.cursor); | ||
|
||
// Now mark for highlight any selected ranges. | ||
const ranges = selection.selections.filter(({from, to}) => (from !== to)).map(({from, to}) => | ||
Decoration.mark({class: "remote-selection", attributes: {style: `background-color: color-mix(in srgb, ${user.color} 20%, transparent)`}}).range(from, to) | ||
); | ||
|
||
// Add all this to the decorations set. (We could optimize this by avoiding recreating unchanged values later.) | ||
decorations = decorations.update({add: [widget, ...ranges], sort: true}); | ||
}); | ||
} | ||
} | ||
return decorations; | ||
}, | ||
provide: f => EditorView.decorations.from(f) | ||
}); | ||
|
||
const emitterPlugin = (setLocalSelections: (s: SelectionData) => void) => ViewPlugin.fromClass(class { | ||
view: EditorView; | ||
constructor(view: EditorView) { | ||
this.view = view | ||
this.emitLocalChanges(view); | ||
} | ||
|
||
update(update: ViewUpdate) { | ||
if (update.selectionSet || update.docChanged) { | ||
this.emitLocalChanges(update.view); | ||
} | ||
} | ||
|
||
emitLocalChanges(view: EditorView) { | ||
const {state} = view; | ||
const selections = state.selection.ranges.map(r => ({from: r.from, to: r.to})); | ||
const cursor = state.selection.main.head; | ||
setLocalSelections({selections, cursor}) | ||
} | ||
}, { | ||
decorations: plugin => plugin.view.state.field(remoteStateField) | ||
}); | ||
|
||
export const collaborativePlugin = (setLocalSelections: (s: SelectionData) => void) => [ | ||
emitterPlugin(setLocalSelections), remoteStateField | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PS: this hook is driving me crazy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
specifically, it's annoying that i have to check several levels of hierarchy (account, currentaccount, doc) and then i still have the type / anonymous stuff where if the type is anonymous i have to reinvent the name & a color every time...
my suggestion is that we set the name to anonymous (at least in the hook but i think in the doc too) and pick a random presence color and leave it on the contact doc.