Path: blob/master/src/packages/frontend/account/editor-settings/checkboxes.tsx
5907 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// cSpell:ignore codebar67import { defineMessage, useIntl } from "react-intl";89import { Panel, Switch } from "@cocalc/frontend/antd-bootstrap";10import { Rendered } from "@cocalc/frontend/app-framework";11import { Icon, IconName } from "@cocalc/frontend/components";12import { BUILD_ON_SAVE_ICON_ENABLED } from "@cocalc/frontend/frame-editors/frame-tree/commands/const";13import { IntlMessage, isIntlMessage } from "@cocalc/frontend/i18n";14import { capitalize } from "@cocalc/util/misc";1516const EDITOR_SETTINGS_CHECKBOXES = {17extra_button_bar: defineMessage({18id: "account.editor-setting.checkbox.extra_button_bar",19defaultMessage:20"customizable button bar below menu bar with shortcuts to menu items",21}),22line_wrapping: defineMessage({23id: "account.editor-setting.checkbox.line_wrapping",24defaultMessage: "wrap long lines",25}),26line_numbers: defineMessage({27id: "account.editor-setting.checkbox.line_numbers",28defaultMessage: "show line numbers",29}),30jupyter_line_numbers: defineMessage({31id: "account.editor-setting.checkbox.jupyter_line_numbers",32defaultMessage: "show line numbers in Jupyter Notebooks",33}),34code_folding: defineMessage({35id: "account.editor-setting.checkbox.code_folding",36defaultMessage: "fold code using control+Q",37}),38smart_indent: defineMessage({39id: "account.editor-setting.checkbox.smart_indent",40defaultMessage: "context sensitive indentation",41}),42electric_chars: defineMessage({43id: "account.editor-setting.checkbox.electric_chars",44defaultMessage: "sometimes re-indent current line",45}),46match_brackets: defineMessage({47id: "account.editor-setting.checkbox.match_brackets",48defaultMessage: "highlight matching brackets near cursor",49}),50auto_close_brackets: defineMessage({51id: "account.editor-setting.checkbox.auto_close_brackets",52defaultMessage: "automatically close brackets",53}),54match_xml_tags: defineMessage({55id: "account.editor-setting.checkbox.match_xml_tags",56defaultMessage: "automatically match XML tags",57}),58auto_close_xml_tags: defineMessage({59id: "account.editor-setting.checkbox.auto_close_xml_tags",60defaultMessage: "automatically close XML tags",61}),62auto_close_latex: defineMessage({63id: "account.editor-setting.checkbox.auto_close_latex",64defaultMessage: "automatically close LaTeX environments",65}),66strip_trailing_whitespace: defineMessage({67id: "account.editor-setting.checkbox.strip_trailing_whitespace",68defaultMessage: "remove whenever file is saved",69}),70show_trailing_whitespace: defineMessage({71id: "account.editor-setting.checkbox.show_trailing_whitespace",72defaultMessage: "show spaces at ends of lines",73}),74spaces_instead_of_tabs: defineMessage({75id: "account.editor-setting.checkbox.spaces_instead_of_tabs",76defaultMessage: "send spaces when the tab key is pressed",77}),78build_on_save: defineMessage({79id: "account.editor-setting.checkbox.build_on_save",80defaultMessage: "build LaTex/Rmd files whenever it is saved to disk",81}),82show_exec_warning: defineMessage({83id: "account.editor-setting.checkbox.show_exec_warning",84defaultMessage: "warn that certain files are not directly executable",85}),86ask_jupyter_kernel: defineMessage({87id: "account.editor-setting.checkbox.ask_jupyter_kernel",88defaultMessage: "ask which kernel to use for a new Jupyter Notebook",89}),90show_my_other_cursors: "when editing the same file in multiple browsers",91disable_jupyter_virtualization: defineMessage({92id: "account.editor-setting.checkbox.disable_jupyter_virtualization",93defaultMessage:94"render entire Jupyter Notebook instead of just visible part (slower and not recommended)",95}),96disable_markdown_codebar: defineMessage({97id: "account.other-settings.markdown_codebar",98defaultMessage: `<strong>Disable the markdown code bar</strong> in all markdown documents.99Checking this hides the extra run, copy, and explain buttons in fenced code blocks.`,100}),101show_symbol_bar_labels: defineMessage({102id: "account.other-settings.symbol_bar_labels",103defaultMessage:104"<strong>Show Symbol Bar Labels:</strong> show labels in the frame editor symbol bar",105}),106} as const;107108// Type for valid checkbox keys109type CheckboxKey = keyof typeof EDITOR_SETTINGS_CHECKBOXES;110111// Group checkboxes into logical panels112const DISPLAY_SETTINGS: readonly CheckboxKey[] = [113"line_wrapping",114"line_numbers",115"jupyter_line_numbers",116"show_trailing_whitespace",117"show_my_other_cursors",118"show_symbol_bar_labels",119] as const;120121const EDITING_BEHAVIOR: readonly CheckboxKey[] = [122"code_folding",123"smart_indent",124"electric_chars",125"spaces_instead_of_tabs",126"strip_trailing_whitespace",127] as const;128129const AUTOCOMPLETION: readonly CheckboxKey[] = [130"match_brackets",131"auto_close_brackets",132"match_xml_tags",133"auto_close_xml_tags",134"auto_close_latex",135] as const;136137const FILE_OPERATIONS: readonly CheckboxKey[] = [138"build_on_save",139"show_exec_warning",140] as const;141142const JUPYTER_SETTINGS: readonly CheckboxKey[] = [143"ask_jupyter_kernel",144"disable_jupyter_virtualization",145] as const;146147const UI_ELEMENTS: readonly CheckboxKey[] = [148"extra_button_bar",149"disable_markdown_codebar",150] as const;151152// Settings that come from other_settings instead of editor_settings153const OTHER_SETTINGS_KEYS: readonly CheckboxKey[] = [154"disable_markdown_codebar",155"show_symbol_bar_labels",156] as const;157158function isOtherSetting(name: CheckboxKey): boolean {159return OTHER_SETTINGS_KEYS.includes(name);160}161162interface Props {163editor_settings;164other_settings?;165email_address?: string;166on_change: Function;167on_change_other_settings?: Function;168}169170export function EditorSettingsCheckboxes(props: Props) {171const intl = useIntl();172173function renderName(name: CheckboxKey) {174if (isOtherSetting(name)) return;175return (176<strong>177{capitalize(178name179.replace(/_/g, " ")180.replace(/-/g, " ")181.replace("xml", "XML")182.replace("latex", "LaTeX"),183) + ": "}184</strong>185);186}187188function label_checkbox(189name: CheckboxKey,190desc: IntlMessage | Rendered | string,191): Rendered {192return (193<span>194{renderName(name)}195{isIntlMessage(desc) ? intl.formatMessage(desc) : desc}196</span>197);198}199200function render_checkbox(201name: CheckboxKey,202desc: IntlMessage | Rendered | string,203): Rendered {204// Special handling for settings that are in other_settings205const is_other_setting = isOtherSetting(name);206const checked = is_other_setting207? !!props.other_settings?.get(name)208: !!props.editor_settings.get(name);209const onChange = is_other_setting210? (e) => props.on_change_other_settings?.(name, e.target.checked)211: (e) => props.on_change(name, e.target.checked);212213return (214<Switch checked={checked} key={name} onChange={onChange}>215{label_checkbox(name, desc)}216</Switch>217);218}219220function renderPanel(221header: string,222icon: IconName,223settingNames: readonly CheckboxKey[],224) {225return (226<Panel227size="small"228header={229<>230<Icon name={icon} /> {header}231</>232}233>234{settingNames.map((name) =>235render_checkbox(name, EDITOR_SETTINGS_CHECKBOXES[name]),236)}237</Panel>238);239}240241return (242<>243{renderPanel("Display Settings", "eye", DISPLAY_SETTINGS)}244{renderPanel("Editing Behavior", "edit", EDITING_BEHAVIOR)}245{renderPanel("Auto-completion", "code", AUTOCOMPLETION)}246{renderPanel(247"File Operations",248BUILD_ON_SAVE_ICON_ENABLED,249FILE_OPERATIONS,250)}251{renderPanel("Jupyter Settings", "jupyter", JUPYTER_SETTINGS)}252{renderPanel("UI Elements", "desktop", UI_ELEMENTS)}253</>254);255}256257258