debian-mirror-gitlab/app/assets/javascripts/lib/utils/autosave.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-11-25 23:54:43 +05:30
import { isString } from 'lodash';
2019-07-07 11:18:12 +05:30
import { capitalizeFirstCharacter } from '~/lib/utils/text_utility';
2022-11-25 23:54:43 +05:30
const normalizeKey = (autosaveKey) => {
let normalizedKey;
if (Array.isArray(autosaveKey) && autosaveKey.every(isString)) {
normalizedKey = autosaveKey.join('/');
} else if (isString(autosaveKey)) {
normalizedKey = autosaveKey;
} else {
// eslint-disable-next-line @gitlab/require-i18n-strings
throw new Error('Invalid autosave key');
}
return `autosave/${normalizedKey}`;
};
const lockVersionKey = (autosaveKey) => `${normalizeKey(autosaveKey)}/lockVersion`;
2021-03-08 18:12:59 +05:30
export const clearDraft = (autosaveKey) => {
2019-07-07 11:18:12 +05:30
try {
2022-11-25 23:54:43 +05:30
window.localStorage.removeItem(normalizeKey(autosaveKey));
window.localStorage.removeItem(lockVersionKey(autosaveKey));
2019-07-07 11:18:12 +05:30
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
};
2021-03-08 18:12:59 +05:30
export const getDraft = (autosaveKey) => {
2019-07-07 11:18:12 +05:30
try {
2022-11-25 23:54:43 +05:30
return window.localStorage.getItem(normalizeKey(autosaveKey));
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
return null;
}
};
export const getLockVersion = (autosaveKey) => {
try {
return window.localStorage.getItem(lockVersionKey(autosaveKey));
2019-07-07 11:18:12 +05:30
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
return null;
}
};
2022-11-25 23:54:43 +05:30
export const updateDraft = (autosaveKey, text, lockVersion) => {
2019-07-07 11:18:12 +05:30
try {
2022-11-25 23:54:43 +05:30
window.localStorage.setItem(normalizeKey(autosaveKey), text);
if (lockVersion) {
window.localStorage.setItem(lockVersionKey(autosaveKey), lockVersion);
}
2019-07-07 11:18:12 +05:30
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
};
export const getDiscussionReplyKey = (noteableType, discussionId) =>
2020-04-22 19:07:51 +05:30
/* eslint-disable-next-line @gitlab/require-i18n-strings */
2019-07-07 11:18:12 +05:30
['Note', capitalizeFirstCharacter(noteableType), discussionId, 'Reply'].join('/');