33 * License: MS-RSL – see LICENSE.md for details
44 */
55
6- import { CompressedPatch , Patch } from "./types" ;
7- import { diff_match_patch } from "@cocalc/util/dmp" ;
8-
9- const dmp = new diff_match_patch ( ) ;
10- dmp . Diff_Timeout = 0.2 ; // computing a diff won't block longer than about 0.2s
11-
12- // Here's what a diff-match-patch patch looks like
13- //
14- // [{"diffs":[[1,"{\"x\":5,\"y\":3}"]],"start1":0,"start2":0,"length1":0,"length2":13 },...]
15- //
16-
17- // TODO: we must explicitly type these as "Function" or typescript gives errors.
18- // We should of course explicitly type the inputs and outputs of each, which
19- // will make other code more robust. See above and look at the source...
20- export const diff_main : Function = dmp . diff_main . bind ( dmp ) ;
21- export const patch_make : Function = dmp . patch_make . bind ( dmp ) ;
22-
23- // The diff-match-patch library changed the format, but we must keep it the same
24- // for backward compat and two stay JSON friendly.
25-
26- const Diff = diff_match_patch . Diff ;
27-
28- function diffs_to_arrays ( diffs : any [ ] ) : any [ ] {
29- const v : any [ ] = [ ] ;
30- for ( const d of diffs ) {
31- v . push ( [ d [ 0 ] , d [ 1 ] ] ) ;
32- }
33- return v ;
34- }
35-
36- function arrays_to_diffs ( arrays : any [ ] ) : any [ ] {
37- const v : any [ ] = [ ] ;
38- for ( const x of arrays ) {
39- v . push ( new Diff ( x [ 0 ] , x [ 1 ] ) ) ;
40- }
41- return v ;
42- }
43-
44- export function compress_patch ( patch : CompressedPatch ) : CompressedPatch {
45- return patch . map ( ( p ) => [
46- diffs_to_arrays ( p . diffs ) ,
47- p . start1 ,
48- p . start2 ,
49- p . length1 ,
50- p . length2 ,
51- ] ) ;
52- }
53-
54- export function decompress_patch ( patch : CompressedPatch ) : CompressedPatch {
55- return patch . map ( ( p ) => ( {
56- diffs : arrays_to_diffs ( p [ 0 ] ) ,
57- start1 : p [ 1 ] ,
58- start2 : p [ 2 ] ,
59- length1 : p [ 3 ] ,
60- length2 : p [ 4 ] ,
61- } ) ) ;
62- }
63-
64- // return *a* compressed patch that transforms string s0 into string s1.
65- export function make_patch ( s0 : string , s1 : string ) : CompressedPatch {
66- // @ts -ignore
67- return compress_patch ( dmp . patch_make ( s0 , s1 ) ) ;
68- }
69-
70- // apply a compressed patch to a string.
71- export function apply_patch (
72- patch : CompressedPatch ,
73- s : string ,
74- ) : [ string , boolean ] {
75- let x ;
76- try {
77- x = dmp . patch_apply ( decompress_patch ( patch ) , s ) ;
78- //console.log('patch_apply ', misc.to_json(decompress_patch(patch)), x)
79- } catch ( err ) {
80- // If a patch is so corrupted it can't be parsed -- e.g., due to a bug in SMC -- we at least
81- // want to make application the identity map (i.e., "best effort"), so
82- // the document isn't completely unreadable!
83- console . warn ( `apply_patch -- ${ err } , ${ JSON . stringify ( patch ) } ` ) ;
84- return [ s , false ] ;
85- }
86- let clean = true ;
87- for ( const a of x [ 1 ] ) {
88- if ( ! a ) {
89- clean = false ;
90- break ;
91- }
92- }
93- return [ x [ 0 ] , clean ] ;
94- }
95-
6+ import { Patch } from "./types" ;
967import { cmp_array } from "@cocalc/util/misc" ;
8+ export * from "@cocalc/util/dmp" ;
9+ import { type CompressedPatch } from "@cocalc/util/dmp" ;
10+ export { type CompressedPatch } ;
9711
9812export function patch_cmp ( a : Patch , b : Patch ) : number {
9913 return cmp_array (
@@ -113,21 +27,6 @@ export function time_cmp(a: Date, b: Date): number {
11327 }
11428}
11529
116- // Do a 3-way **string** merge by computing patch that transforms
117- // base to remote, then applying that patch to local.
118- export function three_way_merge ( opts : {
119- base : string ;
120- local : string ;
121- remote : string ;
122- } ) : string {
123- if ( opts . base === opts . remote ) {
124- // trivial special case...
125- return opts . local ;
126- }
127- // @ts -ignore
128- return dmp . patch_apply ( dmp . patch_make ( opts . base , opts . remote ) , opts . local ) [ 0 ] ;
129- }
130-
13130export function isTestClient ( client : any ) {
13231 return ! ! client ?. isTestClient ?.( ) ;
13332}
0 commit comments