diff --git a/lib/workers/dmpWorker.ts b/lib/workers/dmpWorker.ts index 982555bb..825a8616 100644 --- a/lib/workers/dmpWorker.ts +++ b/lib/workers/dmpWorker.ts @@ -4,6 +4,7 @@ import DiffMatchPatch from "@hackmd/diff-match-patch"; // core import config from "../config"; import {logger} from "../logger"; +import {Revision} from "../models"; const dmp = new DiffMatchPatch() process.on('message', function (data) { @@ -57,25 +58,31 @@ process.on('message', function (data) { return null }) -function createPatch(lastDoc, currDoc) { +function createPatch(lastDoc: string, currDoc: string): string { const msStart = (new Date()).getTime() const diff = dmp.diff_main(lastDoc, currDoc) - let patch = dmp.patch_make(lastDoc, diff) - patch = dmp.patch_toText(patch) + const patch = dmp.patch_make(lastDoc, diff) + const patchText = dmp.patch_toText(patch) const msEnd = (new Date()).getTime() if (config.debug) { - logger.info(patch) + logger.info(patchText) logger.info((msEnd - msStart) + 'ms') } - return patch + return patchText } -function getRevision(revisions, count) { +interface DiffRevision { + content: string, + patch: Patch[], + authorship: string +} + +function getRevision(revisions: Revision[], count: number): DiffRevision { const msStart = (new Date()).getTime() let startContent = null - let lastPatch = [] + let lastPatch = "" let applyPatches = [] - let authorship = [] + let authorship = "" if (count <= Math.round(revisions.length / 2)) { // start from top to target for (let i = 0; i < count; i++) { diff --git a/tsconfig.json b/tsconfig.json index 29e1f16a..e06f932a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,12 +1,16 @@ { "compileOnSave": true, "compilerOptions": { + "baseUrl": ".", "allowJs": false, "target": "ES2019", "outDir": "./dist/", "module": "CommonJS", "esModuleInterop": true, - "typeRoots": ["./typings", "./node_modules/@types"] + "typeRoots": ["./typings", "./node_modules/@types"], + "paths": { + "@hackmd/diff-match-patch": ["./typings/diff-match-patch/"] + } }, "include": [ "./lib/**/*" diff --git a/typings/diff-match-patch/index.d.ts b/typings/diff-match-patch/index.d.ts new file mode 100644 index 00000000..7e6b5415 --- /dev/null +++ b/typings/diff-match-patch/index.d.ts @@ -0,0 +1,82 @@ +declare global { + module diffMatchPatch { + export const DIFF_DELETE = -1; + export const DIFF_INSERT = 1; + export const DIFF_EQUAL = 0; + } + + enum DiffType { + DIFF_DELETE = -1, + DIFF_INSERT = 1, + DIFF_EQUAL = 0 + } + + type Diff = { + [0]: DiffType + [1]: string + } + + interface Patch { + diffs: Diff[]; + + start1?: number + start2?: number + + length1: number + length2: number + + toString(): string + } + + class diffMatchPatch { + diff_main(text1: string, text2: string, opt_checklines?: boolean, + opt_deadline?: number): Diff[] + + diff_cleanupSemantic(diffs: Diff[]) + + diff_cleanupEfficiency(diffs: Diff[]) + + diff_cleanupSemanticLossless(diffs: Diff[]) + + diff_levenshtein(diffs: Diff[]): number + + diff_prettyHtml(diffs: Diff[]): string + + diff_xIndex(diffs: Diff[], loc: number): number + + diff_cleanupMerge(diffs: Diff[]) + + match_main(text: string, pattern: string, loc: number): number + + diff_fromDelta(text1: string, delta: string): Diff[] + + diff_toDelta(diffs: Diff[]): string + + diff_levenshtein(diffs: Diff[]): number + + diff_text1(diffs: Diff[]): string + + diff_text2(diffs: Diff[]): string + + patch_make(a: string, opt_b: string): Patch[] + patch_make(a: Diff[]): Patch[] + patch_make(a: string, opt_b: Diff[]): Patch[] + patch_make(a: string, opt_b: string, opt_c: Diff[]): Patch[] + + patch_splitMax(patches: Patch[]) + + patch_addPadding(patches: Patch[]): string + + patch_deepCopy(patches: Patch[]): Patch[] + + patch_toText(patches: Patch[]): string + + patch_fromText(textline: string): Patch[] + + patch_apply(patches: Patch[], text: string): { + [0]: string + [1]: boolean[] + } + } +} +export = diffMatchPatch