Skip to content

Commit

Permalink
Add basic undo functionality
Browse files Browse the repository at this point in the history
This adds very basic undo functionality.
It works very well in a non collaborative environment, however undoing
your local changes when other remote changes occured can have unexpected consequences.
  • Loading branch information
rroohhh committed Aug 31, 2023
1 parent 381cc79 commit 75678cc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"reconnecting-websocket": "^4.4.0",
"slate": "^0.94.0",
"slate-automerge-doc": "github:bugbakery/slate-automerge-doc",
"slate-history": "^0.93.0",
"slate-react": "^0.94.0",
"swr": "^2.1.0",
"tailwindcss": "^3.2.7",
Expand Down
12 changes: 12 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions frontend/src/editor/automerge_websocket_editor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useLocation } from 'wouter';
import { useEffect, useRef, useState } from 'react';
import { Editor, createEditor } from 'slate';
import { withHistory, HistoryEditor } from 'slate-history';
import { withReact } from 'slate-react';
import { withAutomergeDoc } from 'slate-automerge-doc';
import { unstable as Automerge } from '@automerge/automerge';
Expand Down Expand Up @@ -53,7 +54,7 @@ export function useAutomergeWebsocketEditor(
const createNewEditor = (doc: Automerge.Doc<Document>) => {
const baseEditor = createEditor();
const editorWithReact = withReact(baseEditor);
const editor = withAutomergeDoc(editorWithReact, Automerge.init());
const editor = withHistory(withAutomergeDoc(editorWithReact, Automerge.init()));
editor.addDocChangeListener(sendDocChange);

const migratedDoc = migrateDocument(doc as Automerge.Doc<Document>);
Expand Down Expand Up @@ -83,7 +84,9 @@ export function useAutomergeWebsocketEditor(
const [newDoc] = Automerge.applyChanges(editorRef.current.doc, [msg]);
console.timeEnd('automerge');
console.time('setDoc');
editorRef.current?.setDoc(newDoc);
HistoryEditor.withoutSaving(editorRef.current, () => {
editorRef.current?.setDoc(newDoc);
});
console.timeEnd('setDoc');
} else if (msg_type === MessageSyncType.ChangeBacklogComplete) {
console.info('backlog complete');
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/editor/slate_types.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { BaseEditor } from 'slate';
import { ReactEditor } from 'slate-react';
import { HistoryEditor } from 'slate-history';
import { Paragraph, Text, Document } from './types';
import { AutomergeEditor } from 'slate-automerge-doc';

declare module 'slate' {
interface CustomTypes {
Editor: BaseEditor & ReactEditor & AutomergeEditor<Document>;
Editor: BaseEditor & ReactEditor & HistoryEditor & AutomergeEditor<Document>;
Element: Paragraph;
Text: Text;
}
Expand Down

0 comments on commit 75678cc

Please sign in to comment.