1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00

Desktop: Fixed note history line count information (Regression)

This commit is contained in:
Laurent Cozic 2021-06-21 10:06:36 +01:00
parent 65e9268e83
commit caabdbd815

View File

@ -22,6 +22,16 @@ export default class Revision extends BaseItem {
}
public static createTextPatch(oldText: string, newText: string): string {
// Note that, once parsed, the resulting object will not exactly be like
// a dmp patch object. This is because the library overrides the
// toString() prototype function of the dmp patch object, and uses it in
// certain functions. For example, in patch_toText(). It means that when
// calling patch_toText() with an object that has been JSON-stringified
// and JSON-parsed, it will not work.
//
// This is mostly fine for our purpose. It's only a problem in
// Revision.patchStats() because it was based on parsing the GNU diff
// as returned by patch_toText().
return JSON.stringify(dmp.patch_make(oldText, newText));
}
@ -105,9 +115,35 @@ export default class Revision extends BaseItem {
return output;
}
static patchStats(patch: string) {
// Turn a new-style patch into an approximation of a GNU diff format.
// Approximation, because the only goal is to put "+" or "-" before each
// line, so that it can be processed by patchStats().
private static newPatchToDiffFormat(patch: string): string {
const changeList: string[] = [];
const patchArray = JSON.parse(patch);
for (const patchItem of patchArray) {
for (const d of patchItem.diffs) {
if (d[0] !== 0) changeList.push(d[0] < 0 ? `-${d[1].replace(/[\n\r]/g, ' ')}` : `+${d[1].trim().replace(/[\n\r]/g, ' ')}`);
}
}
return changeList.join('\n');
}
public static patchStats(patch: string) {
if (typeof patch === 'object') throw new Error('Not implemented');
if (this.isNewPatch(patch)) {
try {
patch = this.newPatchToDiffFormat(patch);
} catch (error) {
// Normally it should work but if it doesn't we don't want it to
// crash the app since it's just presentational. But log an
// error so that it can eventually be fixed.
console.error('Could not generate diff:', error, patch);
return { added: 0, removed: 0 };
}
}
const countChars = (diffLine: string) => {
return unescape(diffLine).length - 1;
};