Fix cursor moving perf
This commit is contained in:
parent
6afb828bd9
commit
2ff1384fde
1 changed files with 46 additions and 30 deletions
|
|
@ -102,39 +102,11 @@ export class RemoteCursorsPluginValue implements PluginValue {
|
||||||
const original = update.startState.doc.toString();
|
const original = update.startState.doc.toString();
|
||||||
const edited = update.state.doc.toString();
|
const edited = update.state.doc.toString();
|
||||||
|
|
||||||
const updatedPositions: number[] = [];
|
RemoteCursorsPluginValue.interpolateRemoteCursorPositions(
|
||||||
const reconciled = reconcileWithHistory(
|
|
||||||
original,
|
original,
|
||||||
{
|
edited
|
||||||
text: original,
|
|
||||||
cursors: RemoteCursorsPluginValue.cursors.flatMap(
|
|
||||||
({ span }, i) => [
|
|
||||||
{ id: i * 2, position: span.start },
|
|
||||||
{ id: i * 2 + 1, position: span.end }
|
|
||||||
]
|
|
||||||
)
|
|
||||||
},
|
|
||||||
edited,
|
|
||||||
"Character"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
reconciled.cursors.forEach(({ id, position }) => {
|
|
||||||
const whereToJump = findWhereToMoveCursor(
|
|
||||||
position,
|
|
||||||
reconciled.history
|
|
||||||
);
|
|
||||||
if (whereToJump !== null) {
|
|
||||||
updatedPositions[id] = whereToJump;
|
|
||||||
} else {
|
|
||||||
updatedPositions[id] = position;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
RemoteCursorsPluginValue.cursors.forEach(({ span }, i) => {
|
|
||||||
span.start = updatedPositions[i * 2];
|
|
||||||
span.end = updatedPositions[i * 2 + 1];
|
|
||||||
});
|
|
||||||
|
|
||||||
const decorations: Range<Decoration>[] = [];
|
const decorations: Range<Decoration>[] = [];
|
||||||
|
|
||||||
RemoteCursorsPluginValue.cursors.forEach(
|
RemoteCursorsPluginValue.cursors.forEach(
|
||||||
|
|
@ -207,6 +179,50 @@ export class RemoteCursorsPluginValue implements PluginValue {
|
||||||
|
|
||||||
this.decorations = Decoration.set(decorations, true);
|
this.decorations = Decoration.set(decorations, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static interpolateRemoteCursorPositions(
|
||||||
|
original: string,
|
||||||
|
edited: string
|
||||||
|
): void {
|
||||||
|
if (
|
||||||
|
original === edited ||
|
||||||
|
RemoteCursorsPluginValue.cursors.length === 0
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedPositions: number[] = [];
|
||||||
|
const reconciled = reconcileWithHistory(
|
||||||
|
original,
|
||||||
|
{
|
||||||
|
text: original,
|
||||||
|
cursors: RemoteCursorsPluginValue.cursors.flatMap(
|
||||||
|
({ span }, i) => [
|
||||||
|
{ id: i * 2, position: span.start },
|
||||||
|
{ id: i * 2 + 1, position: span.end }
|
||||||
|
]
|
||||||
|
)
|
||||||
|
},
|
||||||
|
edited
|
||||||
|
);
|
||||||
|
|
||||||
|
reconciled.cursors.forEach(({ id, position }) => {
|
||||||
|
const whereToJump = findWhereToMoveCursor(
|
||||||
|
position,
|
||||||
|
reconciled.history
|
||||||
|
);
|
||||||
|
if (whereToJump !== null) {
|
||||||
|
updatedPositions[id] = whereToJump;
|
||||||
|
} else {
|
||||||
|
updatedPositions[id] = position;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
RemoteCursorsPluginValue.cursors.forEach(({ span }, i) => {
|
||||||
|
span.start = updatedPositions[i * 2];
|
||||||
|
span.end = updatedPositions[i * 2 + 1];
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const remoteCursorsPlugin = ViewPlugin.fromClass(
|
export const remoteCursorsPlugin = ViewPlugin.fromClass(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue