Skip to content

Commit 2677d21

Browse files
committed
refactor: editor index refactoring
Signed-off-by: Yukai Huang <[email protected]>
1 parent 282ba60 commit 2677d21

File tree

5 files changed

+126
-123
lines changed

5 files changed

+126
-123
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-env browser */
2+
/* global CodeMirror */
3+
4+
const defineCodeMirrorModes = () => {
5+
const ignoreOverlay = {
6+
token: function (stream, state) {
7+
stream.next()
8+
return null
9+
}
10+
}
11+
12+
const modeConfigs = [
13+
{ name: 'c', mime: 'text/x-csrc' },
14+
{ name: 'cpp', mime: 'text/x-c++src' },
15+
{ name: 'java', mime: 'text/x-java' },
16+
{ name: 'csharp', mime: 'text/x-csharp' },
17+
{ name: 'objectivec', mime: 'text/x-objectivec' },
18+
{ name: 'scala', mime: 'text/x-scala' },
19+
{ name: 'kotlin', mime: 'text/x-kotlin' },
20+
{ name: 'json', mime: 'application/json' },
21+
{ name: 'jsonld', mime: 'application/ld+json' },
22+
{ name: 'bash', mime: 'text/x-sh' },
23+
{ name: 'ocaml', mime: 'text/x-ocaml' },
24+
{ name: 'csvpreview', mime: 'csv' },
25+
{ name: 'vega', mime: 'application/ld+json' },
26+
{ name: 'markmap', mime: 'gfm' }
27+
]
28+
29+
modeConfigs.forEach(({ name, mime }) => {
30+
CodeMirror.defineMode(name, function (config, modeConfig) {
31+
return CodeMirror.overlayMode(CodeMirror.getMode(config, mime), ignoreOverlay)
32+
})
33+
})
34+
}
35+
36+
export default defineCodeMirrorModes
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint-env browser */
2+
/* global CodeMirror */
3+
4+
const isMac = CodeMirror.keyMap.default === CodeMirror.keyMap.macDefault
5+
const defaultEditorMode = 'gfm'
6+
const viewportMargin = 20
7+
8+
const jumpToAddressBarKeymapName = isMac ? 'Cmd-L' : 'Ctrl-L'
9+
10+
export { defaultEditorMode, viewportMargin, jumpToAddressBarKeymapName }

public/js/lib/editor/index.js

Lines changed: 7 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-env browser */
2-
/* global CodeMirror, $, editor, Cookies */
2+
/* global CodeMirror, $, editor */
33
import { options, Alignment, FormatType } from '@susisu/mte-kernel'
44
import debounce from 'lodash/debounce'
55

@@ -11,46 +11,10 @@ import { linterOptions } from './markdown-lint'
1111
import CodeMirrorSpellChecker, { supportLanguages, supportLanguageCodes } from './spellcheck'
1212
import { initTableEditor } from './table-editor'
1313
import { 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

5519
export 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) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint-env browser */
2+
/* global Cookies */
3+
import Storage from './storage'
4+
5+
const migratePreferences = () => {
6+
// Only run migration if window and localStorage are available
7+
if (typeof window === 'undefined' || typeof localStorage === 'undefined' || typeof Cookies === 'undefined') {
8+
return
9+
}
10+
11+
const preferencesToMigrate = [
12+
'indent_type',
13+
'tab_size',
14+
'space_units',
15+
'keymap',
16+
'theme',
17+
'spellcheck',
18+
'linter',
19+
'preferences-override-browser-keymap',
20+
'preferences-disable-table-shortcuts'
21+
]
22+
23+
preferencesToMigrate.forEach(key => {
24+
// Check if preference exists in cookies but not in localStorage
25+
const cookieValue = Cookies.get(key)
26+
if (cookieValue !== undefined && Storage.get(key) === null) {
27+
// Migrate the preference to localStorage
28+
Storage.set(key, cookieValue)
29+
console.log(`Migrated preference ${key} from cookies to localStorage`)
30+
}
31+
})
32+
}
33+
34+
export default migratePreferences

public/js/lib/editor/storage.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Storage utility class for localStorage operations
2+
class Storage {
3+
static get (key, defaultValue = null) {
4+
try {
5+
const value = window.localStorage.getItem(key)
6+
return value !== null ? value : defaultValue
7+
} catch (e) {
8+
console.error('Error getting from localStorage:', e)
9+
return defaultValue
10+
}
11+
}
12+
13+
static set (key, value, options = {}) {
14+
try {
15+
window.localStorage.setItem(key, value)
16+
return true
17+
} catch (e) {
18+
console.error('Error setting to localStorage:', e)
19+
return false
20+
}
21+
}
22+
23+
static remove (key) {
24+
try {
25+
try {
26+
window.localStorage.removeItem(key)
27+
return true
28+
} catch (e) {
29+
console.error('Error removing from localStorage:', e)
30+
return false
31+
}
32+
} catch (e) {
33+
console.error('Error removing from localStorage:', e)
34+
return false
35+
}
36+
}
37+
}
38+
39+
export default Storage

0 commit comments

Comments
 (0)