-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: support undo/redo for ProEditor
- Loading branch information
Showing
5 changed files
with
135 additions
and
13 deletions.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Doc, UndoManager } from 'yjs'; | ||
import { DocOpts } from 'yjs/dist/src/utils/Doc'; | ||
|
||
export interface UserActionParams { | ||
type: string; | ||
name: string; | ||
timestamp: number; | ||
} | ||
|
||
class UserAction { | ||
type; | ||
name; | ||
timestamp; | ||
constructor(params: UserActionParams) { | ||
this.type = params.type; | ||
this.name = params.name; | ||
this.timestamp = params.timestamp; | ||
} | ||
} | ||
|
||
export class DocWithHistoryManager<T = object> extends Doc { | ||
private _internalHistoryKey = '__INTERNAL_HISTORY_MAP__'; | ||
|
||
constructor(params?: DocOpts) { | ||
super(params); | ||
|
||
this.undoManager = new UndoManager(this.getHistoryMap(), { | ||
trackedOrigins: new Set([UserAction]), | ||
}); | ||
} | ||
|
||
public undoManager: UndoManager; | ||
|
||
updateHistoryData = (value: Partial<T>) => { | ||
const map = this.getMap(this._internalHistoryKey); | ||
|
||
Object.entries(value).forEach(([key, value]) => { | ||
map.set(key, value); | ||
}); | ||
}; | ||
|
||
recordHistoryData = (value: Partial<T>, userAction: UserActionParams) => { | ||
this.transact(() => { | ||
this.updateHistoryData(value); | ||
}, new UserAction(userAction)); | ||
}; | ||
|
||
getHistoryMap = () => { | ||
return this.getMap(this._internalHistoryKey); | ||
}; | ||
|
||
getHistoryJSON = () => { | ||
const map = this.getMap(this._internalHistoryKey); | ||
|
||
return map.toJSON() as T; | ||
}; | ||
|
||
redo = () => { | ||
return this.undoManager.redo(); | ||
}; | ||
|
||
undo = () => { | ||
return this.undoManager.undo(); | ||
}; | ||
} |