Commit 6734d895 authored by Paul Slaughter's avatar Paul Slaughter

Merge branch '213652-don-t-lose-user-content-in-wiki' into 'master'

Resolve "Don't lose user content in wiki"

See merge request gitlab-org/gitlab!30037
parents 62518064 fbecbacb
...@@ -46,6 +46,7 @@ export default { ...@@ -46,6 +46,7 @@ export default {
}, },
methods: { methods: {
onSubmit() { onSubmit() {
window.onbeforeunload = null;
this.$refs.form.submit(); this.$refs.form.submit();
}, },
}, },
......
...@@ -44,6 +44,19 @@ export default class Wikis { ...@@ -44,6 +44,19 @@ export default class Wikis {
linkExample.innerHTML = MARKDOWN_LINK_TEXT[e.target.value]; linkExample.innerHTML = MARKDOWN_LINK_TEXT[e.target.value];
}); });
} }
const wikiTextarea = document.querySelector('form.wiki-form #wiki_content');
const wikiForm = document.querySelector('form.wiki-form');
if (wikiTextarea) {
wikiTextarea.addEventListener('input', () => {
window.onbeforeunload = () => '';
});
wikiForm.addEventListener('submit', () => {
window.onbeforeunload = null;
});
}
} }
handleWikiTitleChange(e) { handleWikiTitleChange(e) {
......
---
title: Warn user before losing wiki content
merge_request: 30037
author:
type: fixed
...@@ -14,6 +14,7 @@ describe('Wikis', () => { ...@@ -14,6 +14,7 @@ describe('Wikis', () => {
<option value="asciidoc">AsciiDoc</option> <option value="asciidoc">AsciiDoc</option>
<option value="org">Org</option> <option value="org">Org</option>
</select> </select>
<textarea id="wiki_content"></textarea>
<code class="js-markup-link-example">{Link title}[link:page-slug]</code> <code class="js-markup-link-example">{Link title}[link:page-slug]</code>
</form> </form>
`; `;
...@@ -24,6 +25,10 @@ describe('Wikis', () => { ...@@ -24,6 +25,10 @@ describe('Wikis', () => {
let changeFormatSelect; let changeFormatSelect;
let linkExample; let linkExample;
const findBeforeUnloadWarning = () => window.onbeforeunload?.();
const findContent = () => document.getElementById('wiki_content');
const findForm = () => document.querySelector('.wiki-form');
describe('when the wiki page is being created', () => { describe('when the wiki page is being created', () => {
const formHtmlFixture = editFormHtmlFixture({ newPage: true }); const formHtmlFixture = editFormHtmlFixture({ newPage: true });
...@@ -94,6 +99,27 @@ describe('Wikis', () => { ...@@ -94,6 +99,27 @@ describe('Wikis', () => {
expect(linkExample.innerHTML).toBe(text); expect(linkExample.innerHTML).toBe(text);
}); });
it('starts with no unload warning', () => {
expect(findBeforeUnloadWarning()).toBeUndefined();
});
describe('when wiki content is updated', () => {
beforeEach(() => {
const content = findContent();
content.value = 'Lorem ipsum dolar sit!';
content.dispatchEvent(new Event('input'));
});
it('sets before unload warning', () => {
expect(findBeforeUnloadWarning()).toBe('');
});
it('when form submitted, unsets before unload warning', () => {
findForm().dispatchEvent(new Event('submit'));
expect(findBeforeUnloadWarning()).toBeUndefined();
});
});
}); });
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment