AbstractEditor.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* eslint-disable @typescript-eslint/no-unused-vars */
  2. import React from 'react';
  3. import { ICodeMirror } from 'react-codemirror2';
  4. export interface AbstractEditorProps extends ICodeMirror {
  5. value?: string;
  6. isGfmMode?: boolean;
  7. onScrollCursorIntoView?: (line: number) => void;
  8. onSave?: () => Promise<void>;
  9. onPasteFiles?: (event: Event) => void;
  10. onCtrlEnter?: (event: Event) => void;
  11. }
  12. interface defaultProps {
  13. isGfmMode: true,
  14. }
  15. export default class AbstractEditor<T extends AbstractEditorProps> extends React.Component<T, Record<string, unknown>> {
  16. constructor(props: Readonly<T>) {
  17. super(props);
  18. this.forceToFocus = this.forceToFocus.bind(this);
  19. this.setCaretLine = this.setCaretLine.bind(this);
  20. this.setScrollTopByLine = this.setScrollTopByLine.bind(this);
  21. this.getStrFromBol = this.getStrFromBol.bind(this);
  22. this.getStrToEol = this.getStrToEol.bind(this);
  23. this.insertText = this.insertText.bind(this);
  24. this.insertLinebreak = this.insertLinebreak.bind(this);
  25. this.dispatchSave = this.dispatchSave.bind(this);
  26. }
  27. public static defaultProps: defaultProps = {
  28. isGfmMode: true,
  29. };
  30. forceToFocus(): void {}
  31. /**
  32. * set new value
  33. */
  34. setValue(_newValue: string): void {}
  35. /**
  36. * Enable/Disable GFM mode
  37. * @param {bool} _bool
  38. */
  39. setGfmMode(_bool: boolean): void {}
  40. /**
  41. * set caret position of codemirror
  42. * @param {string} number
  43. */
  44. setCaretLine(_line: number): void {}
  45. /**
  46. * scroll
  47. * @param {number} _line
  48. */
  49. setScrollTopByLine(_line: number): void {}
  50. /**
  51. * return strings from BOL(beginning of line) to current position
  52. */
  53. getStrFromBol(): Error {
  54. throw new Error('this method should be impelemented in subclass');
  55. }
  56. /**
  57. * return strings from current position to EOL(end of line)
  58. */
  59. getStrToEol(): Error {
  60. throw new Error('this method should be impelemented in subclass');
  61. }
  62. /**
  63. * return strings from BOL(beginning of line) to current position
  64. */
  65. getStrFromBolToSelectedUpperPos(): Error {
  66. throw new Error('this method should be impelemented in subclass');
  67. }
  68. /**
  69. * replace Beggining Of Line to current position with param 'text'
  70. * @param {string} _text
  71. */
  72. replaceBolToCurrentPos(_text: string): Error {
  73. throw new Error('this method should be impelemented in subclass');
  74. }
  75. /**
  76. * replace the current line with param 'text'
  77. * @param {string} _text
  78. */
  79. replaceLine(_text: string): Error {
  80. throw new Error('this method should be impelemented in subclass');
  81. }
  82. /**
  83. * insert text
  84. * @param {string} _text
  85. */
  86. insertText(_text: string): Error {
  87. throw new Error('this method should be impelemented in subclass');
  88. }
  89. /**
  90. * insert line break to the current position
  91. */
  92. insertLinebreak(): void {
  93. this.insertText('\n');
  94. }
  95. /**
  96. * dispatch onSave event
  97. */
  98. dispatchSave(): void {
  99. if (this.props.onSave != null) {
  100. this.props.onSave();
  101. }
  102. }
  103. /**
  104. * dispatch onPasteFiles event
  105. * @param {object} event
  106. */
  107. dispatchPasteFiles(event: Event): void {
  108. if (this.props.onPasteFiles != null) {
  109. this.props.onPasteFiles(event);
  110. }
  111. }
  112. /**
  113. * returns items(an array of react elements) in navigation bar for editor
  114. */
  115. getNavbarItems(): null {
  116. return null;
  117. }
  118. }