mirror of
https://github.com/Alamantus/Lexiconga.git
synced 2025-11-11 21:57:01 +01:00
69 lines
2.3 KiB
JavaScript
69 lines
2.3 KiB
JavaScript
import { setCookie } from "../StackOverflow/cookie";
|
|
import { DELETED_WORDS_LOCALSTORAGE_KEY } from "./constants";
|
|
import { getTimestampInSeconds, cloneObject } from "../../helpers";
|
|
import { DEFAULT_DICTIONARY } from "../../constants";
|
|
|
|
export function saveToken(token) {
|
|
setCookie('token', token, 30);
|
|
}
|
|
|
|
export function dictionaryIsDefault() {
|
|
const defaultDictionary = cloneObject(DEFAULT_DICTIONARY);
|
|
delete defaultDictionary.settings.theme;
|
|
delete defaultDictionary.lastUpdated;
|
|
delete defaultDictionary.createdOn;
|
|
delete defaultDictionary.version;
|
|
const currentDictionary = cloneObject(window.currentDictionary);
|
|
delete currentDictionary.settings.theme;
|
|
delete currentDictionary.lastUpdated;
|
|
delete currentDictionary.createdOn;
|
|
delete currentDictionary.version;
|
|
return JSON.stringify(defaultDictionary) === JSON.stringify(currentDictionary);
|
|
}
|
|
|
|
export function getPublicLink() {
|
|
const { externalID } = window.currentDictionary;
|
|
let path;
|
|
if (externalID) {
|
|
path = window.location.pathname.match(new RegExp(externalID + '$'))
|
|
? window.location.pathname
|
|
: (window.location.pathname.indexOf(externalID) > -1
|
|
? window.location.pathname.substring(0, window.location.pathname.indexOf(externalID)) + externalID
|
|
: window.location.pathname + externalID
|
|
);
|
|
} else {
|
|
path = '';
|
|
}
|
|
return 'https://' + document.domain + path;
|
|
}
|
|
|
|
export function saveDeletedWordsLocally(wordIds) {
|
|
let storedDeletedWords = getLocalDeletedWords();
|
|
wordIds.forEach(wordId => {
|
|
if (storedDeletedWords.findIndex(stored => stored.id === wordId) < 0) {
|
|
storedDeletedWords.push({
|
|
id: wordId,
|
|
deletedOn: getTimestampInSeconds(),
|
|
});
|
|
}
|
|
});
|
|
window.localStorage.setItem(DELETED_WORDS_LOCALSTORAGE_KEY, JSON.stringify(storedDeletedWords));
|
|
}
|
|
|
|
export function saveDeletedWordLocally(wordId) {
|
|
saveDeletedWordsLocally([wordId]);
|
|
}
|
|
|
|
export function getLocalDeletedWords() {
|
|
let storedDeletedWords = window.localStorage.getItem(DELETED_WORDS_LOCALSTORAGE_KEY);
|
|
if (!storedDeletedWords) {
|
|
storedDeletedWords = [];
|
|
} else {
|
|
storedDeletedWords = JSON.parse(storedDeletedWords);
|
|
}
|
|
return storedDeletedWords;
|
|
}
|
|
|
|
export function clearLocalDeletedWords() {
|
|
window.localStorage.removeItem(DELETED_WORDS_LOCALSTORAGE_KEY);
|
|
}
|