1
1
let cacheActionsLines = [ ] ;
2
- let diffActionsNode , diffEditor , ignoreChange = false ;
2
+ let diffActionsNode ,
3
+ diffEditor ,
4
+ ignoreChange = false ;
3
5
4
- export function render ( _diffEditor , { rightPath, notSupportedFile, leftContent, rightContent, theme, tabSize } ) {
6
+ export function render (
7
+ _diffEditor ,
8
+ { rightPath, notSupportedFile, leftContent, rightContent, theme, tabSize }
9
+ ) {
5
10
diffEditor = _diffEditor ;
6
11
window . diffEditor = _diffEditor ;
7
12
diffEditor . setModel ( {
8
- original : monaco . editor . createModel ( leftContent , null , generateMonacoFakeUri ( rightPath , 'org' ) ) ,
9
- modified : monaco . editor . createModel ( rightContent , null , generateMonacoFakeUri ( rightPath , 'mod' ) )
13
+ original : monaco . editor . createModel (
14
+ leftContent ,
15
+ null ,
16
+ generateMonacoFakeUri ( rightPath , 'org' )
17
+ ) ,
18
+ modified : monaco . editor . createModel (
19
+ rightContent ,
20
+ null ,
21
+ generateMonacoFakeUri ( rightPath , 'mod' )
22
+ ) ,
10
23
} ) ;
11
24
diffActionsNode = createDiffActionsContainer ( diffEditor ) ;
12
25
window . diffNavigator = monaco . editor . createDiffNavigator ( diffEditor ) ;
@@ -18,10 +31,11 @@ export function render(_diffEditor, { rightPath, notSupportedFile, leftContent,
18
31
}
19
32
20
33
function setTabSize ( tabSize ) {
21
- const { originalEditor, modifiedEditor } = diffEditor ;
22
- const updateTabSize = model => model . updateOptions ( { tabSize, detectIndentation : false } ) ;
23
- updateTabSize ( originalEditor . getModel ( ) ) ;
24
- updateTabSize ( modifiedEditor . getModel ( ) ) ;
34
+ const { originalEditor, modifiedEditor } = diffEditor ;
35
+ const updateTabSize = ( model ) =>
36
+ model . updateOptions ( { tabSize, detectIndentation : false } ) ;
37
+ updateTabSize ( originalEditor . getModel ( ) ) ;
38
+ updateTabSize ( modifiedEditor . getModel ( ) ) ;
25
39
}
26
40
27
41
function setEditorValue ( editor , value ) {
@@ -40,7 +54,7 @@ export function swap() {
40
54
function generateMonacoFakeUri ( path , qs ) {
41
55
if ( path ) {
42
56
const prefixPath = path . startsWith ( '/' ) ? '' : '/' ;
43
- return monaco . Uri . parse ( `vscode://${ prefixPath } ${ path } ?${ qs } ` )
57
+ return monaco . Uri . parse ( `vscode://${ prefixPath } ${ path } ?${ qs } ` ) ;
44
58
}
45
59
return null ;
46
60
}
@@ -51,115 +65,134 @@ function onDidUpdateDiff() {
51
65
return ;
52
66
}
53
67
vscode . postMessage ( {
54
- command : 'change'
68
+ command : 'change' ,
55
69
} ) ;
56
70
}
57
71
58
72
export function addDiffActions ( diffEditor ) {
59
- const changes = diffEditor . getLineChanges ( ) ;
60
- waitForChangesDecorations ( )
61
- . then ( ( ) => {
62
- const changesData = changes . map ( change => ( {
63
- change,
64
- ...getStrategy ( change )
65
- } ) ) ;
66
- const actionsLines = changesData . map ( ( { top} ) => top ) ;
67
- const actions = Array . from ( diffActionsNode . querySelectorAll ( '.diffAction' ) ) ;
68
-
69
- changesData . forEach ( ( { change, top, replacer} ) => {
70
- createOrUpdateDiffAction ( diffActionsNode , top , ( ) => {
71
- const originalLines = getChangeOriginalValue ( change , diffEditor ) ;
72
- applyOriginalLines ( originalLines , replacer , diffEditor ) ;
73
- } ) ;
74
- } ) ;
75
- cacheActionsLines . forEach ( actionLine => {
76
- if ( ! actionsLines . includes ( actionLine ) ) {
77
- diffActionsNode . removeChild ( actions . find ( action => action . style . top === `${ actionLine } px` ) ) ;
78
- }
73
+ waitForChangesDecorations ( ) . then ( ( ) => {
74
+ const changes = diffEditor . getLineChanges ( ) ;
75
+ const changesData = changes . map ( ( change ) => ( {
76
+ change,
77
+ ...getStrategy ( change ) ,
78
+ } ) ) ;
79
+ const actionsLines = changesData . map ( ( { top } ) => top ) ;
80
+ const actions = Array . from ( diffActionsNode . querySelectorAll ( '.diffAction' ) ) ;
81
+
82
+ changesData . forEach ( ( { change, top, replacer } ) => {
83
+ createOrUpdateDiffAction ( diffActionsNode , top , ( ) => {
84
+ const originalLines = getChangeOriginalValue ( change , diffEditor ) ;
85
+ applyOriginalLines ( originalLines , replacer , diffEditor ) ;
79
86
} ) ;
80
- cacheActionsLines = actionsLines ;
81
87
} ) ;
88
+ cacheActionsLines . forEach ( ( actionLine ) => {
89
+ if ( ! actionsLines . includes ( actionLine ) ) {
90
+ diffActionsNode . removeChild (
91
+ actions . find ( ( action ) => action . style . top === `${ actionLine } px` )
92
+ ) ;
93
+ }
94
+ } ) ;
95
+ cacheActionsLines = actionsLines ;
96
+ } ) ;
82
97
}
83
98
84
99
function getStrategy ( change ) {
85
100
const isChangeInOriginalSide = change . modifiedEndLineNumber === 0 ;
86
101
const isChangeInModifiedSide = change . originalEndLineNumber === 0 ;
87
102
if ( isChangeInModifiedSide ) {
88
103
return {
89
- top : diffEditor . modifiedEditor . getTopForLineNumber ( change . modifiedStartLineNumber ) ,
104
+ top : diffEditor . modifiedEditor . getTopForLineNumber (
105
+ change . modifiedStartLineNumber
106
+ ) ,
90
107
replacer : ( ) => {
91
108
const startLine = change . modifiedStartLineNumber - 1 ;
92
109
return {
93
110
startLine,
94
111
linesToRemove : change . modifiedEndLineNumber - startLine ,
95
- }
96
- }
97
- }
112
+ } ;
113
+ } ,
114
+ } ;
98
115
} else if ( isChangeInOriginalSide ) {
99
116
return {
100
- top : diffEditor . originalEditor . getTopForLineNumber ( change . originalStartLineNumber ) ,
117
+ top : diffEditor . originalEditor . getTopForLineNumber (
118
+ change . originalStartLineNumber
119
+ ) ,
101
120
replacer : ( ) => {
102
121
const startLine = change . modifiedStartLineNumber ;
103
122
return {
104
123
startLine,
105
124
linesToRemove : 0 ,
106
- }
107
- }
108
- }
125
+ } ;
126
+ } ,
127
+ } ;
109
128
}
110
129
return {
111
- top : diffEditor . originalEditor . getTopForLineNumber ( change . originalStartLineNumber ) ,
130
+ top : diffEditor . originalEditor . getTopForLineNumber (
131
+ change . originalStartLineNumber
132
+ ) ,
112
133
replacer : ( ) => {
113
134
const startLine = change . modifiedStartLineNumber - 1 ;
114
135
return {
115
136
startLine,
116
- linesToRemove : ( change . modifiedEndLineNumber - change . modifiedStartLineNumber ) + 1 ,
117
- }
118
- }
119
- }
137
+ linesToRemove :
138
+ change . modifiedEndLineNumber - change . modifiedStartLineNumber + 1 ,
139
+ } ;
140
+ } ,
141
+ } ;
120
142
}
121
143
122
144
function applyOriginalLines ( originalLines , replacer , diffEditor ) {
123
- let { startLine, linesToRemove} = replacer ( ) ;
145
+ let { startLine, linesToRemove } = replacer ( ) ;
124
146
const diff = {
125
- range : new monaco . Range ( ++ startLine , 0 , startLine + linesToRemove , 0 ) ,
126
- text : originalLines
127
- } ;
147
+ range : new monaco . Range ( ++ startLine , 0 , startLine + linesToRemove , 0 ) ,
148
+ text : originalLines ,
149
+ } ;
128
150
diffEditor . modifiedEditor . executeEdits ( 'diff-merge' , [ diff ] ) ;
129
151
}
130
152
131
153
function getChangeOriginalValue ( change , diffEditor ) {
132
- return diffEditor . originalEditor . getValue ( )
133
- . split ( / (?< = [ \n \r ] ) / gm)
134
- . slice ( change . originalStartLineNumber - 1 , change . originalEndLineNumber )
135
- . join ( '' ) ;
154
+ return diffEditor . originalEditor
155
+ . getValue ( )
156
+ . split ( / (?< = [ \n \r ] ) / gm)
157
+ . slice ( change . originalStartLineNumber - 1 , change . originalEndLineNumber )
158
+ . join ( '' ) ;
136
159
}
137
160
138
161
function createOrUpdateDiffAction ( diffActionsNode , top , onCopy ) {
139
162
// action is already in place
140
163
if ( cacheActionsLines . includes ( top ) ) {
141
- const action = diffActionsNode . querySelector ( `.diffAction[data-top="${ top } "]` ) ;
164
+ const action = diffActionsNode . querySelector (
165
+ `.diffAction[data-top="${ top } "]`
166
+ ) ;
142
167
action . onclick = onCopy ;
143
168
} else {
144
169
const action = document . createElement ( 'div' ) ;
145
170
action . className = 'diffAction' ;
146
171
action . dataset . top = top ;
147
172
action . innerHTML = '→' ;
148
173
action . style . top = `${ top } px` ;
149
- action . onclick = onCopy ;
174
+ action . onclick = onCopy ;
150
175
diffActionsNode . appendChild ( action ) ;
151
176
}
152
177
}
153
178
154
- function createDiffActionsContainer ( diffEditor ) {
179
+ export function layoutDiffContainer ( diffActions = diffActionsNode ) {
155
180
const modifedEditorNode = diffEditor . modifiedEditor . getDomNode ( ) ;
181
+ diffActions . style . left = `${
182
+ modifedEditorNode . getBoundingClientRect ( ) . left
183
+ } px`;
184
+ }
185
+
186
+ function createDiffActionsContainer ( diffEditor ) {
156
187
const diffActions = document . createElement ( 'div' ) ;
157
188
diffActions . className = 'diffActions diffOverview' ;
158
- diffActions . style . height = `${ diffEditor . originalEditor . getScrollHeight ( ) } px`
159
- modifedEditorNode . appendChild ( diffActions ) ;
160
- diffEditor . modifiedEditor . onDidScrollChange ( ( { scrollTop} ) => {
189
+ diffActions . style . height = `${ diffEditor . originalEditor . getScrollHeight ( ) } px` ;
190
+ document . querySelector ( '#container' ) . appendChild ( diffActions ) ;
191
+
192
+ diffEditor . modifiedEditor . onDidScrollChange ( ( { scrollTop } ) => {
161
193
diffActions . style . top = `-${ scrollTop } px` ;
162
194
} ) ;
195
+ layoutDiffContainer ( diffActions ) ;
163
196
return diffActions ;
164
197
}
165
198
@@ -180,31 +213,35 @@ function waitForChangesDecorations() {
180
213
}
181
214
182
215
function isSaveShortcut ( e ) {
183
- return ( window . navigator . platform . match ( 'Mac' ) ?
184
- e . metaKey :
185
- e . ctrlKey ) &&
186
- e . keyCode == 83 ;
216
+ return (
217
+ ( window . navigator . platform . match ( 'Mac' ) ? e . metaKey : e . ctrlKey ) &&
218
+ e . keyCode == 83
219
+ ) ;
187
220
}
188
221
189
222
function bindSaveShortcut ( ) {
190
- document . addEventListener ( 'keydown' , e => {
191
- if ( isSaveShortcut ( e ) ) {
192
- e . preventDefault ( ) ;
193
- vscode . postMessage ( {
194
- command : 'save' ,
195
- contents : {
196
- left : diffEditor . originalEditor . getValue ( ) ,
197
- right : diffEditor . modifiedEditor . getValue ( )
198
- }
199
- } ) ;
200
- }
201
- } ,
202
- false
223
+ document . addEventListener (
224
+ 'keydown' ,
225
+ ( e ) => {
226
+ if ( isSaveShortcut ( e ) ) {
227
+ e . preventDefault ( ) ;
228
+ vscode . postMessage ( {
229
+ command : 'save' ,
230
+ contents : {
231
+ left : diffEditor . originalEditor . getValue ( ) ,
232
+ right : diffEditor . modifiedEditor . getValue ( ) ,
233
+ } ,
234
+ } ) ;
235
+ }
236
+ } ,
237
+ false
203
238
) ;
204
239
}
205
240
206
241
function extractEditorStyles ( diffEditor ) {
207
- const lineHeight = diffEditor . modifiedEditor . getOption ( monaco . editor . EditorOption . lineHeight ) ;
242
+ const lineHeight = diffEditor . modifiedEditor . getOption (
243
+ monaco . editor . EditorOption . lineHeight
244
+ ) ;
208
245
document . body . style . setProperty ( '--diff-merge-lineheight' , `${ lineHeight } px` ) ;
209
246
}
210
247
@@ -217,13 +254,15 @@ function setTheme(theme) {
217
254
}
218
255
219
256
function retrieveCssVariables ( ) {
220
- const isNumber = s => ! isNaN ( Number ( s ) ) ;
257
+ const isNumber = ( s ) => ! isNaN ( Number ( s ) ) ;
221
258
222
259
const htmlTag = document . querySelector ( 'html' ) ;
223
260
const compotedStyle = getComputedStyle ( htmlTag ) ;
224
261
225
- return Object . keys ( htmlTag . style ) . filter ( isNumber ) . reduce ( ( ol , ne ) => {
226
- ol [ htmlTag . style [ ne ] ] = compotedStyle . getPropertyValue ( htmlTag . style [ ne ] )
227
- return ol ;
228
- } , { } )
229
- }
262
+ return Object . keys ( htmlTag . style )
263
+ . filter ( isNumber )
264
+ . reduce ( ( ol , ne ) => {
265
+ ol [ htmlTag . style [ ne ] ] = compotedStyle . getPropertyValue ( htmlTag . style [ ne ] ) ;
266
+ return ol ;
267
+ } , { } ) ;
268
+ }
0 commit comments