| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- import React from 'react';
- import PropTypes from 'prop-types';
- import { withTranslation } from 'react-i18next';
- import {
- Dropdown, DropdownToggle, DropdownMenu, DropdownItem,
- } from 'reactstrap';
- import { withUnstatedContainers } from '../UnstatedUtils';
- import AppContainer from '../../services/AppContainer';
- import EditorContainer from '../../services/EditorContainer';
- export const defaultEditorOptions = {
- theme: 'elegant',
- keymapMode: 'default',
- styleActiveLine: false,
- };
- export const defaultPreviewOptions = {
- renderMathJaxInRealtime: false,
- };
- class OptionsSelector extends React.Component {
- constructor(props) {
- super(props);
- const config = this.props.appContainer.getConfig();
- const isMathJaxEnabled = !!config.env.MATHJAX;
- this.state = {
- isCddMenuOpened: false,
- isMathJaxEnabled,
- };
- this.availableThemes = [
- 'eclipse', 'elegant', 'neo', 'mdn-like', 'material', 'dracula', 'monokai', 'twilight',
- ];
- this.keymapModes = {
- default: 'Default',
- vim: 'Vim',
- emacs: 'Emacs',
- sublime: 'Sublime Text',
- };
- this.onChangeTheme = this.onChangeTheme.bind(this);
- this.onChangeKeymapMode = this.onChangeKeymapMode.bind(this);
- this.onClickStyleActiveLine = this.onClickStyleActiveLine.bind(this);
- this.onClickRenderMathJaxInRealtime = this.onClickRenderMathJaxInRealtime.bind(this);
- this.onClickMarkdownTableAutoFormatting = this.onClickMarkdownTableAutoFormatting.bind(this);
- this.onToggleConfigurationDropdown = this.onToggleConfigurationDropdown.bind(this);
- }
- onChangeTheme(newValue) {
- const { editorContainer } = this.props;
- const newOpts = Object.assign(editorContainer.state.editorOptions, { theme: newValue });
- editorContainer.setState({ editorOptions: newOpts });
- // save to localStorage
- editorContainer.saveOptsToLocalStorage();
- }
- onChangeKeymapMode(newValue) {
- const { editorContainer } = this.props;
- const newOpts = Object.assign(editorContainer.state.editorOptions, { keymapMode: newValue });
- editorContainer.setState({ editorOptions: newOpts });
- // save to localStorage
- editorContainer.saveOptsToLocalStorage();
- }
- onClickStyleActiveLine(event) {
- const { editorContainer } = this.props;
- // keep dropdown opened
- this._cddForceOpen = true;
- const newValue = !editorContainer.state.editorOptions.styleActiveLine;
- const newOpts = Object.assign(editorContainer.state.editorOptions, { styleActiveLine: newValue });
- editorContainer.setState({ editorOptions: newOpts });
- // save to localStorage
- editorContainer.saveOptsToLocalStorage();
- }
- onClickRenderMathJaxInRealtime(event) {
- const { editorContainer } = this.props;
- const newValue = !editorContainer.state.previewOptions.renderMathJaxInRealtime;
- const newOpts = Object.assign(editorContainer.state.previewOptions, { renderMathJaxInRealtime: newValue });
- editorContainer.setState({ previewOptions: newOpts });
- // save to localStorage
- editorContainer.saveOptsToLocalStorage();
- }
- onClickMarkdownTableAutoFormatting(event) {
- const { editorContainer } = this.props;
- const newValue = !editorContainer.state.editorOptions.ignoreMarkdownTableAutoFormatting;
- const newOpts = Object.assign(editorContainer.state.editorOptions, { ignoreMarkdownTableAutoFormatting: newValue });
- editorContainer.setState({ editorOptions: newOpts });
- // save to localStorage
- editorContainer.saveOptsToLocalStorage();
- }
- onToggleConfigurationDropdown(newValue) {
- this.setState({ isCddMenuOpened: !this.state.isCddMenuOpened });
- }
- renderThemeSelector() {
- const { editorContainer } = this.props;
- const selectedTheme = editorContainer.state.editorOptions.theme;
- const menuItems = this.availableThemes.map((theme) => {
- return <button key={theme} className="dropdown-item" type="button" onClick={() => this.onChangeTheme(theme)}>{theme}</button>;
- });
- return (
- <div className="input-group flex-nowrap">
- <div className="input-group-prepend">
- <span className="input-group-text" id="igt-theme">Theme</span>
- </div>
- <div className="input-group-append dropup">
- <button
- type="button"
- className="btn btn-outline-secondary dropdown-toggle"
- data-toggle="dropdown"
- aria-haspopup="true"
- aria-expanded="false"
- aria-describedby="igt-theme"
- >
- {selectedTheme}
- </button>
- <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
- {menuItems}
- </div>
- </div>
- </div>
- );
- }
- renderKeymapModeSelector() {
- const { editorContainer } = this.props;
- const selectedKeymapMode = editorContainer.state.editorOptions.keymapMode;
- const menuItems = Object.keys(this.keymapModes).map((mode) => {
- const label = this.keymapModes[mode];
- const icon = (mode !== 'default')
- ? <img src={`/images/icons/${mode}.png`} width="16px" className="mr-2"></img>
- : null;
- return <button key={mode} className="dropdown-item" type="button" onClick={() => this.onChangeKeymapMode(mode)}>{icon}{label}</button>;
- });
- return (
- <div className="input-group flex-nowrap">
- <div className="input-group-prepend">
- <span className="input-group-text" id="igt-keymap">Keymap</span>
- </div>
- <div className="input-group-append dropup">
- <button
- type="button"
- className="btn btn-outline-secondary dropdown-toggle"
- data-toggle="dropdown"
- aria-haspopup="true"
- aria-expanded="false"
- aria-describedby="igt-keymap"
- >
- {selectedKeymapMode}
- </button>
- <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
- {menuItems}
- </div>
- </div>
- </div>
- );
- }
- renderConfigurationDropdown() {
- return (
- <div className="my-0 form-group">
- <Dropdown
- direction="up"
- className="grw-editor-configuration-dropdown"
- isOpen={this.state.isCddMenuOpened}
- toggle={this.onToggleConfigurationDropdown}
- >
- <DropdownToggle color="outline-secondary" caret>
- <i className="icon-settings"></i>
- </DropdownToggle>
- <DropdownMenu>
- {this.renderActiveLineMenuItem()}
- {this.renderRealtimeMathJaxMenuItem()}
- {this.renderMarkdownTableAutoFormattingMenuItem()}
- {/* <DropdownItem divider /> */}
- </DropdownMenu>
- </Dropdown>
- </div>
- );
- }
- renderActiveLineMenuItem() {
- const { t, editorContainer } = this.props;
- const isActive = editorContainer.state.editorOptions.styleActiveLine;
- const iconClasses = ['text-info'];
- if (isActive) {
- iconClasses.push('ti-check');
- }
- const iconClassName = iconClasses.join(' ');
- return (
- <DropdownItem toggle={false} onClick={this.onClickStyleActiveLine}>
- <div className="d-flex justify-content-between">
- <span className="icon-container"></span>
- <span className="menuitem-label">{ t('page_edit.Show active line') }</span>
- <span className="icon-container"><i className={iconClassName}></i></span>
- </div>
- </DropdownItem>
- );
- }
- renderRealtimeMathJaxMenuItem() {
- if (!this.state.isMathJaxEnabled) {
- return;
- }
- const { editorContainer } = this.props;
- const isEnabled = this.state.isMathJaxEnabled;
- const isActive = isEnabled && editorContainer.state.previewOptions.renderMathJaxInRealtime;
- const iconClasses = ['text-info'];
- if (isActive) {
- iconClasses.push('ti-check');
- }
- const iconClassName = iconClasses.join(' ');
- return (
- <DropdownItem toggle={false} onClick={this.onClickRenderMathJaxInRealtime}>
- <div className="d-flex justify-content-between">
- <span className="icon-container"><img src="/images/icons/fx.svg" width="14px" alt="fx"></img></span>
- <span className="menuitem-label">MathJax Rendering</span>
- <span className="icon-container"><i className={iconClassName}></i></span>
- </div>
- </DropdownItem>
- );
- }
- renderMarkdownTableAutoFormattingMenuItem() {
- const { t, editorContainer } = this.props;
- // Auto-formatting was enabled before optionalizing, so we made it a disabled option(ignoreMarkdownTableAutoFormatting).
- const isActive = !editorContainer.state.editorOptions.ignoreMarkdownTableAutoFormatting;
- const iconClasses = ['text-info'];
- if (isActive) {
- iconClasses.push('ti-check');
- }
- const iconClassName = iconClasses.join(' ');
- return (
- <DropdownItem toggle={false} onClick={this.onClickMarkdownTableAutoFormatting}>
- <div className="d-flex justify-content-between">
- <span className="icon-container"></span>
- <span className="menuitem-label">{ t('page_edit.auto_format_table') }</span>
- <span className="icon-container"><i className={iconClassName}></i></span>
- </div>
- </DropdownItem>
- );
- }
- render() {
- return (
- <div className="d-flex flex-row">
- <span>{this.renderThemeSelector()}</span>
- <span className="ml-2 ml-sm-4">{this.renderKeymapModeSelector()}</span>
- <span className="ml-2 ml-sm-4">{this.renderConfigurationDropdown()}</span>
- </div>
- );
- }
- }
- /**
- * Wrapper component for using unstated
- */
- const OptionsSelectorWrapper = withUnstatedContainers(OptionsSelector, [AppContainer, EditorContainer]);
- OptionsSelector.propTypes = {
- t: PropTypes.func.isRequired, // i18next
- appContainer: PropTypes.instanceOf(AppContainer).isRequired,
- editorContainer: PropTypes.instanceOf(EditorContainer).isRequired,
- };
- export default withTranslation()(OptionsSelectorWrapper);
|