From 70af3ef91889ab8886d641e66fc02e6a2eedc7a5 Mon Sep 17 00:00:00 2001 From: SergeyEzhin Date: Thu, 26 Oct 2023 15:27:15 +0200 Subject: [PATCH] Removed using and added check on local storage --- .../lib/component/ThemeColorPalette.jsx | 7 +++-- apps/common/mobile/utils/htmlutils.js | 31 +++++++++++++++---- .../mobile/src/controller/Main.jsx | 2 +- .../mobile/src/controller/Main.jsx | 2 +- .../mobile/src/controller/Main.jsx | 2 +- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/apps/common/mobile/lib/component/ThemeColorPalette.jsx b/apps/common/mobile/lib/component/ThemeColorPalette.jsx index 5970ac8b01..6666bf4893 100644 --- a/apps/common/mobile/lib/component/ThemeColorPalette.jsx +++ b/apps/common/mobile/lib/component/ThemeColorPalette.jsx @@ -1,6 +1,7 @@ import React, { useState, useEffect } from 'react'; import { f7, ListItem, List, Icon } from 'framework7-react'; import { useTranslation } from 'react-i18next'; +import { LocalStorage } from '../../utils/LocalStorage.mjs'; const ThemeColors = ({ themeColors, onColorClick, curColor, isTypeColors, isTypeCustomColors }) => { return ( @@ -110,7 +111,7 @@ const ThemeColorPalette = props => { let customColors = props.customColors; if (customColors.length < 1) { - customColors = localStorage.getItem('mobile-custom-colors'); + customColors = LocalStorage.getItem('mobile-custom-colors'); customColors = customColors ? customColors.toLowerCase().split(',') : []; } @@ -177,11 +178,11 @@ const CustomColorPicker = props => { }); const addNewColor = (color) => { - let colors = localStorage.getItem('mobile-custom-colors'); + let colors = LocalStorage.getItem('mobile-custom-colors'); colors = colors ? colors.split(',') : []; const newColor = color.slice(1); if (colors.push(newColor) > countDynamicColors) colors.shift(); // 10 - dynamiccolors - localStorage.setItem('mobile-custom-colors', colors.join().toLowerCase()); + LocalStorage.setItem('mobile-custom-colors', colors.join().toLowerCase()); props.onAddNewColor && props.onAddNewColor(colors, newColor); }; diff --git a/apps/common/mobile/utils/htmlutils.js b/apps/common/mobile/utils/htmlutils.js index bf65aa9634..67ff6fd6f6 100644 --- a/apps/common/mobile/utils/htmlutils.js +++ b/apps/common/mobile/utils/htmlutils.js @@ -8,18 +8,35 @@ const load_stylesheet = reflink => { document.getElementsByTagName("head")[0].appendChild(link); }; -if ( !!window.Android && window.Android.editorConfig ) { +if(!!window.Android && window.Android.editorConfig) { window.native = {editorConfig: window.Android.editorConfig()} } -if ( localStorage && localStorage.getItem('mobile-mode-direction') === 'rtl' ) { - load_stylesheet('./css/framework7-rtl.css') - document.body.classList.add('rtl'); +function isLocalStorageAvailable() { + try { + const testKey = 'test'; + localStorage.setItem(testKey, '1'); + localStorage.removeItem(testKey); + + return true; + } catch (e) { + return false; + } +} + +if(isLocalStorageAvailable()) { + if(localStorage.getItem('mobile-mode-direction') === 'rtl') { + load_stylesheet('./css/framework7-rtl.css'); + document.body.classList.add('rtl'); + } else { + load_stylesheet('./css/framework7.css') + } } else { load_stylesheet('./css/framework7.css') } -let obj = !localStorage ? {id: 'theme-light', type: 'light'} : JSON.parse(localStorage.getItem("mobile-ui-theme")); +let obj = !isLocalStorageAvailable() ? {id: 'theme-light', type: 'light'} : JSON.parse(localStorage.getItem("mobile-ui-theme")); + if ( !obj ) { if ( window.native && window.native.theme ) { if ( window.native.theme.type == 'dark' ) obj = {id: 'theme-dark', type: 'dark'}; @@ -30,7 +47,9 @@ if ( !obj ) { obj = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? {id: 'theme-dark', type: 'dark'} : {id: 'theme-light', type: 'light'}; - localStorage && localStorage.setItem("mobile-ui-theme", JSON.stringify(obj)); + if(isLocalStorageAvailable()) { + localStorage.setItem("mobile-ui-theme", JSON.stringify(obj)); + } } document.body.classList.add(`theme-type-${obj.type}`, `${window.asceditor}-editor`); diff --git a/apps/documenteditor/mobile/src/controller/Main.jsx b/apps/documenteditor/mobile/src/controller/Main.jsx index c7c822d16b..af5057858d 100644 --- a/apps/documenteditor/mobile/src/controller/Main.jsx +++ b/apps/documenteditor/mobile/src/controller/Main.jsx @@ -102,7 +102,7 @@ class MainController extends Component { } this.props.storeApplicationSettings.changeMacrosSettings(value); - value = localStorage.getItem("de-mobile-allow-macros-request"); + value = LocalStorage.getItem("de-mobile-allow-macros-request"); this.props.storeApplicationSettings.changeMacrosRequest((value !== null) ? parseInt(value) : 0); this.props.storeAppOptions.wopi = this.editorConfig.wopi; diff --git a/apps/presentationeditor/mobile/src/controller/Main.jsx b/apps/presentationeditor/mobile/src/controller/Main.jsx index b0aaee20be..5fc1656526 100644 --- a/apps/presentationeditor/mobile/src/controller/Main.jsx +++ b/apps/presentationeditor/mobile/src/controller/Main.jsx @@ -94,7 +94,7 @@ class MainController extends Component { } this.props.storeApplicationSettings.changeMacrosSettings(value); - value = localStorage.getItem("pe-mobile-allow-macros-request"); + value = LocalStorage.getItem("pe-mobile-allow-macros-request"); this.props.storeApplicationSettings.changeMacrosRequest((value !== null) ? parseInt(value) : 0); }; diff --git a/apps/spreadsheeteditor/mobile/src/controller/Main.jsx b/apps/spreadsheeteditor/mobile/src/controller/Main.jsx index 36670811e5..506c0d52ed 100644 --- a/apps/spreadsheeteditor/mobile/src/controller/Main.jsx +++ b/apps/spreadsheeteditor/mobile/src/controller/Main.jsx @@ -138,7 +138,7 @@ class MainController extends Component { } this.props.storeApplicationSettings.changeMacrosSettings(value); - value = localStorage.getItem("sse-mobile-allow-macros-request"); + value = LocalStorage.getItem("sse-mobile-allow-macros-request"); this.props.storeApplicationSettings.changeMacrosRequest((value !== null) ? parseInt(value) : 0); };