toolbar_generator.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import * as React from 'react';
  4. import { OptionsManager } from './options_manager';
  5. /**
  6. * Interface describing toolbar properties.
  7. *
  8. * @private
  9. */
  10. interface IToolbarProps {}
  11. /**
  12. * Interface describing toolbar state.
  13. *
  14. * @private
  15. */
  16. interface IToolbarState {
  17. /**
  18. * Boolean indicating whether numbering is enabled.
  19. */
  20. numbering: boolean;
  21. }
  22. /**
  23. * Returns a generator for rendering a Markdown table of contents toolbar.
  24. *
  25. * @private
  26. * @param options - generator options
  27. * @returns toolbar generator
  28. */
  29. function toolbar(options: OptionsManager) {
  30. return class extends React.Component<IToolbarProps, IToolbarState> {
  31. /**
  32. * Returns a generator for rendering a Markdown table of contents toolbar.
  33. *
  34. * @param props - toolbar properties
  35. * @returns toolbar generator
  36. */
  37. constructor(props: IToolbarProps) {
  38. super(props);
  39. this.state = { numbering: false };
  40. options.initializeOptions(false);
  41. }
  42. /**
  43. * Renders a toolbar.
  44. *
  45. * @returns rendered toolbar
  46. */
  47. render() {
  48. const toggleNumbering = () => {
  49. options.numbering = !options.numbering;
  50. this.setState({ numbering: options.numbering });
  51. };
  52. let icon;
  53. if (this.state.numbering) {
  54. icon = (
  55. <div
  56. className="toc-toolbar-auto-numbering-button toc-toolbar-button"
  57. onClick={event => toggleNumbering()}
  58. >
  59. <div
  60. role="text"
  61. aria-label="Toggle Auto-Numbering"
  62. title="Toggle Auto-Numbering"
  63. className="toc-toolbar-auto-numbering-icon toc-toolbar-icon-selected"
  64. />
  65. </div>
  66. );
  67. } else {
  68. icon = (
  69. <div
  70. className="toc-toolbar-auto-numbering-button toc-toolbar-button"
  71. onClick={event => toggleNumbering()}
  72. >
  73. <div
  74. role="text"
  75. aria-label="Toggle Auto-Numbering"
  76. title="Toggle Auto-Numbering"
  77. className="toc-toolbar-auto-numbering-icon toc-toolbar-icon"
  78. />
  79. </div>
  80. );
  81. }
  82. return (
  83. <div>
  84. <div className={'toc-toolbar'}>{icon}</div>
  85. </div>
  86. );
  87. }
  88. };
  89. }
  90. /**
  91. * Exports.
  92. */
  93. export { toolbar };