11/* eslint-env browser */
2- /* global CodeMirror, $, editor, Cookies */
2+ /* global CodeMirror, $, editor */
33import { options , Alignment , FormatType } from '@susisu/mte-kernel'
44import debounce from 'lodash/debounce'
55
@@ -11,46 +11,10 @@ import { linterOptions } from './markdown-lint'
1111import CodeMirrorSpellChecker , { supportLanguages , supportLanguageCodes } from './spellcheck'
1212import { initTableEditor } from './table-editor'
1313import { availableThemes } from './constants'
14-
15- // Storage utility class for localStorage operations
16- class Storage {
17- static get ( key , defaultValue = null ) {
18- try {
19- const value = localStorage . getItem ( key )
20- return value !== null ? value : defaultValue
21- } catch ( e ) {
22- console . error ( 'Error getting from localStorage:' , e )
23- return defaultValue
24- }
25- }
26-
27- static set ( key , value , options = { } ) {
28- try {
29- localStorage . setItem ( key , value )
30- return true
31- } catch ( e ) {
32- console . error ( 'Error setting to localStorage:' , e )
33- return false
34- }
35- }
36-
37- static remove ( key ) {
38- try {
39- localStorage . removeItem ( key )
40- return true
41- } catch ( e ) {
42- console . error ( 'Error removing from localStorage:' , e )
43- return false
44- }
45- }
46- }
47-
48- /* config section */
49- const isMac = CodeMirror . keyMap . default === CodeMirror . keyMap . macDefault
50- const defaultEditorMode = 'gfm'
51- const viewportMargin = 20
52-
53- const jumpToAddressBarKeymapName = isMac ? 'Cmd-L' : 'Ctrl-L'
14+ import Storage from './storage'
15+ import { isMac , defaultEditorMode , viewportMargin , jumpToAddressBarKeymapName } from './editor-config'
16+ import defineCodeMirrorModes from './codemirror-modes'
17+ import migratePreferences from './preferences-migration'
5418
5519export default class Editor {
5620 constructor ( ) {
@@ -166,90 +130,10 @@ export default class Editor {
166130 this . eventListeners = { }
167131 this . config = config
168132
169- // define modes from mode mime
170- const ignoreOverlay = {
171- token : function ( stream , state ) {
172- stream . next ( )
173- return null
174- }
175- }
176-
177- CodeMirror . defineMode ( 'c' , function ( config , modeConfig ) {
178- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'text/x-csrc' ) , ignoreOverlay )
179- } )
180- CodeMirror . defineMode ( 'cpp' , function ( config , modeConfig ) {
181- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'text/x-c++src' ) , ignoreOverlay )
182- } )
183- CodeMirror . defineMode ( 'java' , function ( config , modeConfig ) {
184- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'text/x-java' ) , ignoreOverlay )
185- } )
186- CodeMirror . defineMode ( 'csharp' , function ( config , modeConfig ) {
187- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'text/x-csharp' ) , ignoreOverlay )
188- } )
189- CodeMirror . defineMode ( 'objectivec' , function ( config , modeConfig ) {
190- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'text/x-objectivec' ) , ignoreOverlay )
191- } )
192- CodeMirror . defineMode ( 'scala' , function ( config , modeConfig ) {
193- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'text/x-scala' ) , ignoreOverlay )
194- } )
195- CodeMirror . defineMode ( 'kotlin' , function ( config , modeConfig ) {
196- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'text/x-kotlin' ) , ignoreOverlay )
197- } )
198- CodeMirror . defineMode ( 'json' , function ( config , modeConfig ) {
199- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'application/json' ) , ignoreOverlay )
200- } )
201- CodeMirror . defineMode ( 'jsonld' , function ( config , modeConfig ) {
202- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'application/ld+json' ) , ignoreOverlay )
203- } )
204- CodeMirror . defineMode ( 'bash' , function ( config , modeConfig ) {
205- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'text/x-sh' ) , ignoreOverlay )
206- } )
207- CodeMirror . defineMode ( 'ocaml' , function ( config , modeConfig ) {
208- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'text/x-ocaml' ) , ignoreOverlay )
209- } )
210- CodeMirror . defineMode ( 'csvpreview' , function ( config , modeConfig ) {
211- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'csv' ) , ignoreOverlay )
212- } )
213- CodeMirror . defineMode ( 'vega' , function ( config , modeConfig ) {
214- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'application/ld+json' ) , ignoreOverlay )
215- } )
216-
217- CodeMirror . defineMode ( 'markmap' , function ( config , modeConfig ) {
218- return CodeMirror . overlayMode ( CodeMirror . getMode ( config , 'gfm' ) , ignoreOverlay )
219- } )
133+ defineCodeMirrorModes ( )
220134
221135 // Migrate preferences from cookies to localStorage
222- this . migratePreferences ( )
223- }
224-
225- // Migrate preferences from cookies to localStorage
226- migratePreferences ( ) {
227- // Only run migration if window and localStorage are available
228- if ( typeof window === 'undefined' || typeof localStorage === 'undefined' || typeof Cookies === 'undefined' ) {
229- return
230- }
231-
232- const preferencesToMigrate = [
233- 'indent_type' ,
234- 'tab_size' ,
235- 'space_units' ,
236- 'keymap' ,
237- 'theme' ,
238- 'spellcheck' ,
239- 'linter' ,
240- 'preferences-override-browser-keymap' ,
241- 'preferences-disable-table-shortcuts'
242- ]
243-
244- preferencesToMigrate . forEach ( key => {
245- // Check if preference exists in cookies but not in localStorage
246- const cookieValue = Cookies . get ( key )
247- if ( cookieValue !== undefined && Storage . get ( key ) === null ) {
248- // Migrate the preference to localStorage
249- Storage . set ( key , cookieValue )
250- console . log ( `Migrated preference ${ key } from cookies to localStorage` )
251- }
252- } )
136+ migratePreferences ( )
253137 }
254138
255139 on ( event , cb ) {
0 commit comments