Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
c75da766
Commit
c75da766
authored
Feb 26, 2019
by
Winnie Hellmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add autosave utilities
parent
e9d1393e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
91 additions
and
0 deletions
+91
-0
app/assets/javascripts/lib/utils/autosave.js
app/assets/javascripts/lib/utils/autosave.js
+27
-0
spec/frontend/lib/utils/autosave_spec.js
spec/frontend/lib/utils/autosave_spec.js
+64
-0
No files found.
app/assets/javascripts/lib/utils/autosave.js
0 → 100644
View file @
c75da766
export
const
clearDraft
=
autosaveKey
=>
{
try
{
window
.
localStorage
.
removeItem
(
`autosave/
${
autosaveKey
}
`
);
}
catch
(
e
)
{
// eslint-disable-next-line no-console
console
.
error
(
e
);
}
};
export
const
getDraft
=
autosaveKey
=>
{
try
{
return
window
.
localStorage
.
getItem
(
`autosave/
${
autosaveKey
}
`
);
}
catch
(
e
)
{
// eslint-disable-next-line no-console
console
.
error
(
e
);
return
null
;
}
};
export
const
updateDraft
=
(
autosaveKey
,
text
)
=>
{
try
{
window
.
localStorage
.
setItem
(
`autosave/
${
autosaveKey
}
`
,
text
);
}
catch
(
e
)
{
// eslint-disable-next-line no-console
console
.
error
(
e
);
}
};
spec/frontend/lib/utils/autosave_spec.js
0 → 100644
View file @
c75da766
import
{
clearDraft
,
getDraft
,
updateDraft
}
from
'
~/lib/utils/autosave
'
;
describe
(
'
autosave utils
'
,
()
=>
{
const
autosaveKey
=
'
dummy-autosave-key
'
;
const
text
=
'
some dummy text
'
;
describe
(
'
clearDraft
'
,
()
=>
{
beforeEach
(()
=>
{
localStorage
.
setItem
(
`autosave/
${
autosaveKey
}
`
,
text
);
});
afterEach
(()
=>
{
localStorage
.
removeItem
(
`autosave/
${
autosaveKey
}
`
);
});
it
(
'
removes the draft from localStorage
'
,
()
=>
{
clearDraft
(
autosaveKey
);
expect
(
localStorage
.
getItem
(
`autosave/
${
autosaveKey
}
`
)).
toBe
(
null
);
});
});
describe
(
'
getDraft
'
,
()
=>
{
beforeEach
(()
=>
{
localStorage
.
setItem
(
`autosave/
${
autosaveKey
}
`
,
text
);
});
afterEach
(()
=>
{
localStorage
.
removeItem
(
`autosave/
${
autosaveKey
}
`
);
});
it
(
'
returns the draft from localStorage
'
,
()
=>
{
const
result
=
getDraft
(
autosaveKey
);
expect
(
result
).
toBe
(
text
);
});
it
(
'
returns null if no entry exists in localStorage
'
,
()
=>
{
localStorage
.
removeItem
(
`autosave/
${
autosaveKey
}
`
);
const
result
=
getDraft
(
autosaveKey
);
expect
(
result
).
toBe
(
null
);
});
});
describe
(
'
updateDraft
'
,
()
=>
{
beforeEach
(()
=>
{
localStorage
.
setItem
(
`autosave/
${
autosaveKey
}
`
,
text
);
});
afterEach
(()
=>
{
localStorage
.
removeItem
(
`autosave/
${
autosaveKey
}
`
);
});
it
(
'
removes the draft from localStorage
'
,
()
=>
{
const
newText
=
'
new text
'
;
updateDraft
(
autosaveKey
,
newText
);
expect
(
localStorage
.
getItem
(
`autosave/
${
autosaveKey
}
`
)).
toBe
(
newText
);
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment