2019-10-14 05:22:04 +00:00
|
|
|
export interface EditorType {
|
|
|
|
setLanguage(identifier: string): void
|
|
|
|
getValue(): string
|
|
|
|
setValue(text: string): void
|
2019-12-21 16:47:25 +00:00
|
|
|
update(): void
|
2019-10-14 05:22:04 +00:00
|
|
|
unload(): void
|
|
|
|
}
|
|
|
|
|
2019-10-09 14:47:08 +00:00
|
|
|
export const types = {
|
|
|
|
textarea: {
|
|
|
|
name: 'Textarea',
|
|
|
|
async createView() {
|
|
|
|
return (await import('./views/editor-types/textarea')).default
|
|
|
|
},
|
|
|
|
},
|
|
|
|
codemirror: {
|
|
|
|
name: 'CodeMirror',
|
|
|
|
async createView() {
|
|
|
|
return (await import('./views/editor-types/codemirror/codemirror')).default
|
|
|
|
},
|
|
|
|
},
|
2019-10-11 09:10:11 +00:00
|
|
|
monaco: {
|
|
|
|
name: 'Monaco (Visual Studio Code)',
|
|
|
|
async createView() {
|
|
|
|
return (await import('./views/editor-types/monaco/monaco')).default
|
|
|
|
}
|
|
|
|
},
|
2019-10-09 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 18:25:35 +00:00
|
|
|
export function getCurrentEditor() {
|
|
|
|
return localStorage.getItem('editorType') || 'codemirror'
|
2019-10-09 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setCurrentEditor(newEditor) {
|
2019-10-30 18:25:35 +00:00
|
|
|
return localStorage.setItem('editorType', newEditor)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function onChange(callback: (createEditor: (textArea: HTMLTextAreaElement, onChange: () => void) => EditorType) => void) {
|
|
|
|
addEventListener('storage', async ({ key, newValue }) => {
|
|
|
|
if (key === 'editorType') {
|
|
|
|
callback(await types[newValue].createView())
|
|
|
|
}
|
|
|
|
})
|
2019-10-09 14:47:08 +00:00
|
|
|
}
|