-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from yacchin1205/fix/update-pad
Fix history corruption on title updates
- Loading branch information
Showing
6 changed files
with
122 additions
and
116 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,59 @@ | ||
const api = require("ep_etherpad-lite/node/db/API"); | ||
const padMessageHandler = require("ep_etherpad-lite/node/handler/PadMessageHandler"); | ||
const CustomError = require('ep_etherpad-lite/node/utils/customError'); | ||
const padManager = require('ep_etherpad-lite/node/db/PadManager'); | ||
|
||
// based on https://github.com/ether/etherpad-lite/blob/develop/src/node/db/API.ts | ||
// ... This is required to use spliceText, which is not exported in the API | ||
|
||
// gets a pad safe | ||
const getPadSafe = async (padID: string|object, shouldExist: boolean, text?:string, authorId:string = '') => { | ||
// check if padID is a string | ||
if (typeof padID !== 'string') { | ||
throw new CustomError('padID is not a string', 'apierror'); | ||
} | ||
|
||
// check if the padID maches the requirements | ||
if (!padManager.isValidPadId(padID)) { | ||
throw new CustomError('padID did not match requirements', 'apierror'); | ||
} | ||
|
||
// check if the pad exists | ||
const exists = await padManager.doesPadExists(padID); | ||
|
||
if (!exists && shouldExist) { | ||
// does not exist, but should | ||
throw new CustomError('padID does not exist', 'apierror'); | ||
} | ||
|
||
if (exists && !shouldExist) { | ||
// does exist, but shouldn't | ||
throw new CustomError('padID does already exist', 'apierror'); | ||
} | ||
|
||
// pad exists, let's get it | ||
return padManager.getPad(padID, text, authorId); | ||
}; | ||
|
||
export type ReplaceSet = { | ||
start: number; | ||
ndel: number; | ||
text: string; | ||
}; | ||
|
||
export async function applyReplaceSet( | ||
padID: string, | ||
replaceSet: ReplaceSet[], | ||
authorId: string = "" | ||
) { | ||
const pad = await getPadSafe(padID, true); | ||
|
||
const sortedReplaceSet = new Array<ReplaceSet>(...replaceSet) | ||
.sort((a, b) => a.start - b.start) | ||
.reverse(); | ||
for (const replaceSet of sortedReplaceSet) { | ||
const { start, ndel, text } = replaceSet; | ||
await pad.spliceText(start, ndel, text, authorId); | ||
} | ||
await padMessageHandler.updatePadClients(pad); | ||
} |
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