Skip to content

Commit 462d9fc

Browse files
authored
fix: undo/redo pos restore at the end of the text (#35)
* fix: undo/redo pos restore at the end of the text * chore: upgrade loro-crdt * chore: use 1.5.13 in storybook * chore: fix lint issue
1 parent 5c35467 commit 462d9fc

10 files changed

Lines changed: 90 additions & 100 deletions

File tree

examples/stories/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"build-storybook": "storybook build"
1313
},
1414
"dependencies": {
15-
"loro-crdt": "^1.5.5",
15+
"loro-crdt": "^1.5.13",
1616
"loro-prosemirror": "workspace:*",
1717
"prosemirror-commands": "^1.5.2",
1818
"prosemirror-example-setup": "^1.2.2",
@@ -27,6 +27,7 @@
2727
"react-dom": "^18.2.0"
2828
},
2929
"devDependencies": {
30+
"loro-crdt": "^1.5.13",
3031
"@chromatic-com/storybook": "^1.3.3",
3132
"@storybook/addon-essentials": "^8.0.8",
3233
"@storybook/addon-interactions": "^8.0.8",

examples/stories/src/stories/prompt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export abstract class Field {
157157
read(dom: HTMLElement) { return (dom as any).value }
158158

159159
/// A field-type-specific validation function.
160-
validateType(value: any): string | null { return null }
160+
validateType(_value: any): string | null { return null }
161161

162162
/// @internal
163163
validate(value: any): string | null {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "loro-prosemirror",
33
"version": "0.3.0",
44
"description": "Prosemirror Binding for Loro",
5-
"packageManager": "pnpm@9.15.2",
5+
"packageManager": "pnpm@10.15.0",
66
"main": "dist/index.js",
77
"module": "dist/index.mjs",
88
"types": "dist/index.d.ts",
@@ -39,13 +39,13 @@
3939
"prosemirror-view": "^1.28.0"
4040
},
4141
"devDependencies": {
42-
"loro-crdt": "^1.4.0",
4342
"@rollup/plugin-node-resolve": "^15.0.1",
4443
"@typescript-eslint/parser": "^7.4.0",
4544
"@vitest/coverage-v8": "^1.4.0",
4645
"chromatic": "^11.5.3",
4746
"eslint": "^8.57.0",
4847
"lib0": "^0.2.93",
48+
"loro-crdt": "^1.5.13",
4949
"prettier": "^3.2.5",
5050
"prosemirror-model": "^1.18.1",
5151
"prosemirror-state": "^1.4.1",

pnpm-lock.yaml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cursor-plugin.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const loroCursorPluginKey = new PluginKey<{ awarenessUpdated: boolean }>(
3030
function createDecorations(
3131
state: EditorState,
3232
awareness: CursorAwareness,
33-
plugin: Plugin<DecorationSet>,
33+
_plugin: Plugin<DecorationSet>,
3434
createSelection: (user: PeerID) => DecorationAttrs,
3535
createCursor: (user: PeerID) => Element,
3636
): DecorationSet {
@@ -106,13 +106,15 @@ export const LoroCursorPlugin = (
106106
},
107107
) => {
108108
const getSelection = options.getSelection || ((state) => state.selection);
109-
const createSelection = options.createSelection ||
109+
const createSelection =
110+
options.createSelection ||
110111
((user) => ({
111112
class: "loro-selection",
112113
"data-peer": user,
113114
style: `background-color: rgba(228, 208, 102, 0.5)`,
114115
}));
115-
const createCursor = options.createCursor ||
116+
const createCursor =
117+
options.createCursor ||
116118
((user) => {
117119
const awarenessData = awareness.getAllStates();
118120
const cursorUserData = awarenessData[user];
@@ -152,9 +154,8 @@ export const LoroCursorPlugin = (
152154
},
153155
apply(tr, prevState, _oldState, newState) {
154156
const loroState = loroSyncPluginKey.getState(newState);
155-
const loroCursorState: { awarenessUpdated: boolean } = tr.getMeta(
156-
loroCursorPluginKey,
157-
);
157+
const loroCursorState: { awarenessUpdated: boolean } =
158+
tr.getMeta(loroCursorPluginKey);
158159
if (
159160
(loroState && loroState.changedBy !== "local") ||
160161
(loroCursorState && loroCursorState.awarenessUpdated)
@@ -252,14 +253,15 @@ export function convertPmSelectionToCursors(
252253
loroState.doc as LoroDocType,
253254
loroState.mapping,
254255
);
255-
const focus = selection.head == selection.anchor
256-
? anchor
257-
: absolutePositionToCursor(
258-
pmRootNode,
259-
selection.head,
260-
loroState.doc as LoroDocType,
261-
loroState.mapping,
262-
);
256+
const focus =
257+
selection.head == selection.anchor
258+
? anchor
259+
: absolutePositionToCursor(
260+
pmRootNode,
261+
selection.head,
262+
loroState.doc as LoroDocType,
263+
loroState.mapping,
264+
);
263265
return { anchor, focus };
264266
}
265267

@@ -279,7 +281,8 @@ function absolutePositionToCursor(
279281
const nodeParent = pos.node(pos.depth);
280282
const offset = pos.parentOffset;
281283

282-
const loroId = WEAK_NODE_TO_LORO_CONTAINER_MAPPING.get(nodeParent) ??
284+
const loroId =
285+
WEAK_NODE_TO_LORO_CONTAINER_MAPPING.get(nodeParent) ??
283286
getByValue(mapping, nodeParent);
284287
if (!loroId) {
285288
if (anchor > 1) {
@@ -301,11 +304,7 @@ function absolutePositionToCursor(
301304
const child = children.get(childIndex);
302305
childIndex += 1;
303306
if (child instanceof LoroText) {
304-
if (child.length >= index) {
305-
return child.getCursor(index);
306-
} else {
307-
index -= child.length;
308-
}
307+
return child.getCursor(index);
309308
} else {
310309
if (index == 0) {
311310
// This happens when user selects an image or a horizontal rule

0 commit comments

Comments
 (0)