11/* global CodeMirror, $, editor, Cookies */
2+ import { options , Alignment , FormatType } from '@susisu/mte-kernel'
3+ import debounce from 'lodash/debounce'
4+
25import * as utils from './utils'
36import config from './config'
47import statusBarTemplate from './statusbar.html'
58import toolBarTemplate from './toolbar.html'
69import './markdown-lint'
710import { initTableEditor } from './table-editor'
8- import { options , Alignment , FormatType } from '@susisu/mte-kernel '
11+ import { availableThemes } from './constants '
912
1013/* config section */
1114const isMac = CodeMirror . keyMap . default === CodeMirror . keyMap . macDefault
@@ -325,6 +328,8 @@ export default class Editor {
325328 this . setSpellcheck ( )
326329 this . setLinter ( )
327330 this . setPreferences ( )
331+
332+ this . handleStatusBarResize ( )
328333 }
329334
330335 updateStatusBar ( ) {
@@ -349,6 +354,21 @@ export default class Editor {
349354 }
350355 }
351356
357+ handleStatusBarResize ( ) {
358+ const onResize = debounce ( ( ) => {
359+ if ( ! this . statusBar ) {
360+ return
361+ }
362+
363+ const maxHeight = window . innerHeight - this . statusBar . height ( ) - 50 /* navbar height */ - 10 /* spacing */
364+ this . statusBar . find ( '.status-theme ul.dropdown-menu' ) . css ( 'max-height' , `${ maxHeight } px` )
365+ } , 300 )
366+
367+ $ ( window ) . resize ( onResize )
368+
369+ onResize ( )
370+ }
371+
352372 setIndent ( ) {
353373 var cookieIndentType = Cookies . get ( 'indent_type' )
354374 var cookieTabSize = parseInt ( Cookies . get ( 'tab_size' ) )
@@ -488,38 +508,37 @@ export default class Editor {
488508 }
489509
490510 setTheme ( ) {
491- var cookieTheme = Cookies . get ( 'theme' )
492- if ( cookieTheme ) {
493- this . editor . setOption ( 'theme' , cookieTheme )
494- }
511+ this . statusIndicators . find ( '.status-theme ul.dropdown-menu' ) . append ( availableThemes . map ( theme => {
512+ return $ ( `<li value="${ theme . value } "><a>${ theme . name } </a></li>` )
513+ } ) )
495514
496- var themeToggle = this . statusTheme . find ( '.ui-theme-toggle' )
497-
498- const checkTheme = ( ) => {
499- var theme = this . editor . getOption ( 'theme' )
500- if ( theme === 'one-dark' ) {
501- themeToggle . removeClass ( 'active' )
502- } else {
503- themeToggle . addClass ( 'active' )
504- }
515+ const activateThemeListItem = ( theme ) => {
516+ this . statusIndicators . find ( '.status-theme li' ) . removeClass ( 'active' )
517+ this . statusIndicators . find ( `.status-theme li[value="${ theme } "]` ) . addClass ( 'active' )
505518 }
506519
507- themeToggle . click ( ( ) => {
508- var theme = this . editor . getOption ( 'theme' )
509- if ( theme === 'one-dark' ) {
510- theme = 'default'
511- } else {
512- theme = 'one-dark'
513- }
520+ const setTheme = theme => {
514521 this . editor . setOption ( 'theme' , theme )
515522 Cookies . set ( 'theme' , theme , {
516523 expires : 365
517524 } )
525+ this . statusIndicators . find ( '.status-theme li' ) . removeClass ( 'active' )
526+ this . statusIndicators . find ( `.status-theme li[value="${ theme } "]` ) . addClass ( 'active' )
527+ }
518528
519- checkTheme ( )
520- } )
529+ const cookieTheme = Cookies . get ( 'theme' )
530+ if ( cookieTheme && availableThemes . find ( theme => cookieTheme === theme . value ) ) {
531+ setTheme ( cookieTheme )
532+ activateThemeListItem ( cookieTheme )
533+ } else {
534+ activateThemeListItem ( this . editor . getOption ( 'theme' ) )
535+ }
521536
522- checkTheme ( )
537+ this . statusIndicators . find ( '.status-theme li' ) . click ( function ( ) {
538+ const theme = $ ( this ) . attr ( 'value' )
539+ setTheme ( theme )
540+ activateThemeListItem ( theme )
541+ } )
523542 }
524543
525544 setSpellcheck ( ) {
0 commit comments