diff --git a/public/js/lib/editor/index.js b/public/js/lib/editor/index.js index eb887da694..af04f980fa 100644 --- a/public/js/lib/editor/index.js +++ b/public/js/lib/editor/index.js @@ -1,3 +1,4 @@ +/* eslint-env browser */ /* global CodeMirror, $, editor, Cookies */ import { options, Alignment, FormatType } from '@susisu/mte-kernel' import debounce from 'lodash/debounce' @@ -11,6 +12,39 @@ import CodeMirrorSpellChecker, { supportLanguages, supportLanguageCodes } from ' import { initTableEditor } from './table-editor' import { availableThemes } from './constants' +// Storage utility class for localStorage operations +class Storage { + static get (key, defaultValue = null) { + try { + const value = localStorage.getItem(key) + return value !== null ? value : defaultValue + } catch (e) { + console.error('Error getting from localStorage:', e) + return defaultValue + } + } + + static set (key, value, options = {}) { + try { + localStorage.setItem(key, value) + return true + } catch (e) { + console.error('Error setting to localStorage:', e) + return false + } + } + + static remove (key) { + try { + localStorage.removeItem(key) + return true + } catch (e) { + console.error('Error removing from localStorage:', e) + return false + } + } +} + /* config section */ const isMac = CodeMirror.keyMap.default === CodeMirror.keyMap.macDefault const defaultEditorMode = 'gfm' @@ -183,6 +217,39 @@ export default class Editor { CodeMirror.defineMode('markmap', function (config, modeConfig) { return CodeMirror.overlayMode(CodeMirror.getMode(config, 'gfm'), ignoreOverlay) }) + + // Migrate preferences from cookies to localStorage + this.migratePreferences() + } + + // Migrate preferences from cookies to localStorage + migratePreferences () { + // Only run migration if window and localStorage are available + if (typeof window === 'undefined' || typeof localStorage === 'undefined' || typeof Cookies === 'undefined') { + return + } + + const preferencesToMigrate = [ + 'indent_type', + 'tab_size', + 'space_units', + 'keymap', + 'theme', + 'spellcheck', + 'linter', + 'preferences-override-browser-keymap', + 'preferences-disable-table-shortcuts' + ] + + preferencesToMigrate.forEach(key => { + // Check if preference exists in cookies but not in localStorage + const cookieValue = Cookies.get(key) + if (cookieValue !== undefined && Storage.get(key) === null) { + // Migrate the preference to localStorage + Storage.set(key, cookieValue) + console.log(`Migrated preference ${key} from cookies to localStorage`) + } + }) } on (event, cb) { @@ -423,24 +490,24 @@ export default class Editor { } setIndent () { - var cookieIndentType = Cookies.get('indent_type') - var cookieTabSize = parseInt(Cookies.get('tab_size')) - var cookieSpaceUnits = parseInt(Cookies.get('space_units')) - if (cookieIndentType) { - if (cookieIndentType === 'tab') { + var storedIndentType = Storage.get('indent_type') + var storedTabSize = parseInt(Storage.get('tab_size')) + var storedSpaceUnits = parseInt(Storage.get('space_units')) + if (storedIndentType) { + if (storedIndentType === 'tab') { this.editor.setOption('indentWithTabs', true) - if (cookieTabSize) { - this.editor.setOption('indentUnit', cookieTabSize) + if (storedTabSize) { + this.editor.setOption('indentUnit', storedTabSize) } - } else if (cookieIndentType === 'space') { + } else if (storedIndentType === 'space') { this.editor.setOption('indentWithTabs', false) - if (cookieSpaceUnits) { - this.editor.setOption('indentUnit', cookieSpaceUnits) + if (storedSpaceUnits) { + this.editor.setOption('indentUnit', storedSpaceUnits) } } } - if (cookieTabSize) { - this.editor.setOption('tabSize', cookieTabSize) + if (storedTabSize) { + this.editor.setOption('tabSize', storedTabSize) } var type = this.statusIndicators.find('.indent-type') @@ -449,14 +516,10 @@ export default class Editor { const setType = () => { if (this.editor.getOption('indentWithTabs')) { - Cookies.set('indent_type', 'tab', { - expires: 365 - }) + Storage.set('indent_type', 'tab') type.text('Tab Size:') } else { - Cookies.set('indent_type', 'space', { - expires: 365 - }) + Storage.set('indent_type', 'space') type.text('Spaces:') } } @@ -465,13 +528,9 @@ export default class Editor { const setUnit = () => { var unit = this.editor.getOption('indentUnit') if (this.editor.getOption('indentWithTabs')) { - Cookies.set('tab_size', unit, { - expires: 365 - }) + Storage.set('tab_size', unit) } else { - Cookies.set('space_units', unit, { - expires: 365 - }) + Storage.set('space_units', unit) } widthLabel.text(unit) } @@ -480,16 +539,16 @@ export default class Editor { type.click(() => { if (this.editor.getOption('indentWithTabs')) { this.editor.setOption('indentWithTabs', false) - cookieSpaceUnits = parseInt(Cookies.get('space_units')) - if (cookieSpaceUnits) { - this.editor.setOption('indentUnit', cookieSpaceUnits) + storedSpaceUnits = parseInt(Storage.get('space_units')) + if (storedSpaceUnits) { + this.editor.setOption('indentUnit', storedSpaceUnits) } } else { this.editor.setOption('indentWithTabs', true) - cookieTabSize = parseInt(Cookies.get('tab_size')) - if (cookieTabSize) { - this.editor.setOption('indentUnit', cookieTabSize) - this.editor.setOption('tabSize', cookieTabSize) + storedTabSize = parseInt(Storage.get('tab_size')) + if (storedTabSize) { + this.editor.setOption('indentUnit', storedTabSize) + this.editor.setOption('tabSize', storedTabSize) } } setType() @@ -525,9 +584,9 @@ export default class Editor { } setKeymap () { - var cookieKeymap = Cookies.get('keymap') - if (cookieKeymap) { - this.editor.setOption('keyMap', cookieKeymap) + var storedKeymap = Storage.get('keymap') + if (storedKeymap) { + this.editor.setOption('keyMap', storedKeymap) } var label = this.statusIndicators.find('.ui-keymap-label') @@ -537,9 +596,7 @@ export default class Editor { const setKeymapLabel = () => { var keymap = this.editor.getOption('keyMap') - Cookies.set('keymap', keymap, { - expires: 365 - }) + Storage.set('keymap', keymap) label.text(keymap) this.restoreOverrideEditorKeymap() this.setOverrideBrowserKeymap() @@ -572,17 +629,15 @@ export default class Editor { const setTheme = theme => { this.editor.setOption('theme', theme) - Cookies.set('theme', theme, { - expires: 365 - }) + Storage.set('theme', theme) this.statusIndicators.find('.status-theme li').removeClass('active') this.statusIndicators.find(`.status-theme li[value="${theme}"]`).addClass('active') } - const cookieTheme = Cookies.get('theme') - if (cookieTheme && availableThemes.find(theme => cookieTheme === theme.value)) { - setTheme(cookieTheme) - activateThemeListItem(cookieTheme) + const storedTheme = Storage.get('theme') + if (storedTheme && availableThemes.find(theme => storedTheme === theme.value)) { + setTheme(storedTheme) + activateThemeListItem(storedTheme) } else { activateThemeListItem(this.editor.getOption('theme')) } @@ -613,10 +668,10 @@ export default class Editor { } getExistingSpellcheckLang () { - const cookieSpellcheck = Cookies.get('spellcheck') + const storedSpellcheck = Storage.get('spellcheck') - if (cookieSpellcheck) { - return cookieSpellcheck === 'false' ? undefined : cookieSpellcheck + if (storedSpellcheck) { + return storedSpellcheck === 'false' ? undefined : storedSpellcheck } else { return undefined } @@ -637,18 +692,18 @@ export default class Editor { return $(`