9
9
GlFormInput ,
10
10
GlFormSelect ,
11
11
} from ' @gitlab/ui' ;
12
+ import { getDraft , clearDraft , updateDraft } from ' ~/lib/utils/autosave' ;
12
13
import csrf from ' ~/lib/utils/csrf' ;
13
14
import { setUrlFragment } from ' ~/lib/utils/url_utility' ;
14
15
import { s__ , sprintf } from ' ~/locale' ;
@@ -33,6 +34,29 @@ const MARKDOWN_LINK_TEXT = {
33
34
org: ' [[page-slug]]' ,
34
35
};
35
36
37
+ function getPagePath (pageInfo ) {
38
+ return pageInfo .persisted ? pageInfo .path : pageInfo .createPath ;
39
+ }
40
+
41
+ const autosaveKey = (pageInfo , field ) => {
42
+ const path = pageInfo .persisted ? pageInfo .path : pageInfo .createPath ;
43
+
44
+ return ` ${ path} /${ field} ` ;
45
+ };
46
+
47
+ const titleAutosaveKey = (pageInfo ) => autosaveKey (pageInfo, ' title' );
48
+ const formatAutosaveKey = (pageInfo ) => autosaveKey (pageInfo, ' format' );
49
+ const contentAutosaveKey = (pageInfo ) => autosaveKey (pageInfo, ' content' );
50
+ const commitAutosaveKey = (pageInfo ) => autosaveKey (pageInfo, ' commit' );
51
+
52
+ const getTitle = (pageInfo ) => getDraft (titleAutosaveKey (pageInfo)) || pageInfo .title ? .trim () || ' ' ;
53
+ const getFormat = (pageInfo ) =>
54
+ getDraft (formatAutosaveKey (pageInfo)) || pageInfo .format || ' markdown' ;
55
+ const getContent = (pageInfo ) => getDraft (contentAutosaveKey (pageInfo)) || pageInfo .content || ' ' ;
56
+ const getCommitMessage = (pageInfo ) =>
57
+ getDraft (commitAutosaveKey (pageInfo)) || pageInfo .commitMessage || ' ' ;
58
+ const getIsFormDirty = (pageInfo ) => Boolean (getDraft (titleAutosaveKey (pageInfo)));
59
+
36
60
export default {
37
61
i18n: {
38
62
title: {
@@ -87,13 +111,14 @@ export default {
87
111
data () {
88
112
return {
89
113
editingMode: ' source' ,
90
- title: this .pageInfo .title ? .trim () || ' ' ,
91
- format: this .pageInfo .format || ' markdown' ,
92
- content: this .pageInfo .content || ' ' ,
93
- commitMessage: ' ' ,
94
- isDirty: false ,
114
+ title: getTitle (this .pageInfo ),
115
+ format: getFormat (this .pageInfo ),
116
+ content: getContent (this .pageInfo ),
117
+ commitMessage: getCommitMessage (this .pageInfo ),
95
118
contentEditorEmpty: false ,
96
119
isContentEditorActive: false ,
120
+ switchEditingControlDisabled: false ,
121
+ isFormDirty: getIsFormDirty (this .pageInfo ),
97
122
};
98
123
},
99
124
computed: {
@@ -104,7 +129,7 @@ export default {
104
129
return csrf .token ;
105
130
},
106
131
formAction () {
107
- return this .pageInfo . persisted ? this . pageInfo . path : this . pageInfo . createPath ;
132
+ return getPagePath ( this .pageInfo ) ;
108
133
},
109
134
helpPath () {
110
135
return setUrlFragment (
@@ -151,7 +176,7 @@ export default {
151
176
},
152
177
},
153
178
mounted () {
154
- this .updateCommitMessage ();
179
+ if ( ! this . commitMessage ) this .updateCommitMessage ();
155
180
156
181
window .addEventListener (' beforeunload' , this .onPageUnload );
157
182
},
@@ -160,6 +185,8 @@ export default {
160
185
},
161
186
methods: {
162
187
async handleFormSubmit (e ) {
188
+ this .isFormDirty = false ;
189
+
163
190
e .preventDefault ();
164
191
165
192
this .trackFormSubmit ();
@@ -169,18 +196,33 @@ export default {
169
196
await this .$nextTick ();
170
197
171
198
e .target .submit ();
199
+ },
172
200
173
- this .isDirty = false ;
201
+ updateDrafts () {
202
+ updateDraft (titleAutosaveKey (this .pageInfo ), this .title );
203
+ updateDraft (formatAutosaveKey (this .pageInfo ), this .format );
204
+ updateDraft (contentAutosaveKey (this .pageInfo ), this .content );
205
+ updateDraft (commitAutosaveKey (this .pageInfo ), this .commitMessage );
174
206
},
175
207
176
- onPageUnload (event ) {
177
- if (! this .isDirty ) return undefined ;
208
+ clearDrafts () {
209
+ clearDraft (titleAutosaveKey (this .pageInfo ));
210
+ clearDraft (formatAutosaveKey (this .pageInfo ));
211
+ clearDraft (contentAutosaveKey (this .pageInfo ));
212
+ clearDraft (commitAutosaveKey (this .pageInfo ));
213
+ },
178
214
179
- event .preventDefault ();
215
+ handleContentEditorChange ({ empty, markdown }) {
216
+ this .contentEditorEmpty = empty;
217
+ this .content = markdown;
218
+ },
180
219
181
- // eslint-disable-next-line no-param-reassign
182
- event .returnValue = ' ' ;
183
- return ' ' ;
220
+ onPageUnload () {
221
+ if (this .isFormDirty ) {
222
+ this .updateDrafts ();
223
+ } else {
224
+ this .clearDrafts ();
225
+ }
184
226
},
185
227
186
228
updateCommitMessage () {
@@ -222,10 +264,6 @@ export default {
222
264
trackContentEditorLoaded () {
223
265
this .track (CONTENT_EDITOR_LOADED_ACTION );
224
266
},
225
-
226
- checkDirty (markdown ) {
227
- this .isDirty = this .pageInfo .content !== markdown;
228
- },
229
267
},
230
268
};
231
269
< / script>
@@ -236,6 +274,7 @@ export default {
236
274
method= " post"
237
275
class = " wiki-form common-note-form gl-mt-3 js-quick-submit"
238
276
@submit= " handleFormSubmit"
277
+ @input= " isFormDirty = true"
239
278
>
240
279
< input : value= " csrfToken" type= " hidden" name= " authenticity_token" / >
241
280
< input v- if = " pageInfo.persisted" type= " hidden" name= " _method" value= " put" / >
@@ -306,7 +345,6 @@ export default {
306
345
form- field- name= " wiki[content]"
307
346
@contentEditor= " notifyContentEditorActive"
308
347
@markdownField= " notifyContentEditorInactive"
309
- @input =" checkDirty"
310
348
/ >
311
349
< div class = " form-text gl-text-gray-600" >
312
350
< gl- sprintf
@@ -358,9 +396,14 @@ export default {
358
396
: disabled= " disableSubmitButton"
359
397
> {{ submitButtonText }}< / gl- button
360
398
>
361
- <gl-button data-testid =" wiki-cancel-button" :href =" cancelFormPath" class =" float-right" >{{
362
- $options.i18n.cancel
363
- }}</gl-button >
399
+ < gl- button
400
+ data- testid= " wiki-cancel-button"
401
+ : href= " cancelFormPath"
402
+ class = " float-right"
403
+ @click= " isFormDirty = false"
404
+ >
405
+ {{ $options .i18n .cancel }}< / gl- button
406
+ >
364
407
< / div>
365
408
< / gl- form>
366
409
< / template>
0 commit comments