AbstractEditor.tsx 3.1 KB

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