Welcome to mirror list, hosted at ThFree Co, Russian Federation.

autosave.js « utils « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 01316be06a2e7fd9762d11e3171102bd4f182148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { isString } from 'lodash';
import { capitalizeFirstCharacter } from '~/lib/utils/text_utility';

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`;

export const clearDraft = (autosaveKey) => {
  try {
    window.localStorage.removeItem(normalizeKey(autosaveKey));
    window.localStorage.removeItem(lockVersionKey(autosaveKey));
  } catch (e) {
    // eslint-disable-next-line no-console
    console.error(e);
  }
};

export const getDraft = (autosaveKey) => {
  try {
    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));
  } catch (e) {
    // eslint-disable-next-line no-console
    console.error(e);
    return null;
  }
};

export const updateDraft = (autosaveKey, text, lockVersion) => {
  try {
    window.localStorage.setItem(normalizeKey(autosaveKey), text);
    if (lockVersion) {
      window.localStorage.setItem(lockVersionKey(autosaveKey), lockVersion);
    }
  } catch (e) {
    // eslint-disable-next-line no-console
    console.error(e);
  }
};

export const getDiscussionReplyKey = (noteableType, discussionId) =>
  /* eslint-disable-next-line @gitlab/require-i18n-strings */
  ['Note', capitalizeFirstCharacter(noteableType), discussionId, 'Reply'].join('/');