EditorContainer.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { Container } from 'unstated';
  2. import loggerFactory from '~/utils/logger';
  3. import { apiv3Get } from '../util/apiv3-client';
  4. const logger = loggerFactory('growi:services:EditorContainer');
  5. /**
  6. * Service container related to options for Editor/Preview
  7. * @extends {Container} unstated Container
  8. */
  9. export default class EditorContainer extends Container {
  10. constructor(appContainer, defaultEditorOptions, defaultPreviewOptions) {
  11. super();
  12. this.appContainer = appContainer;
  13. this.appContainer.registerContainer(this);
  14. // this.retrieveEditorSettings = this.retrieveEditorSettings.bind(this);
  15. const mainContent = document.querySelector('#content-main');
  16. if (mainContent == null) {
  17. logger.debug('#content-main element is not exists');
  18. return;
  19. }
  20. this.state = {
  21. tags: null,
  22. editorOptions: {},
  23. previewOptions: {},
  24. // Defaults to null to show modal when not in DB
  25. // isTextlintEnabled: null,
  26. // textlintRules: [],
  27. indentSize: this.appContainer.config.adminPreferredIndentSize || 4,
  28. };
  29. this.isSetBeforeunloadEventHandler = false;
  30. this.initDrafts();
  31. this.editorOptions = null;
  32. this.initEditorOptions('editorOptions', 'editorOptions', defaultEditorOptions);
  33. this.initEditorOptions('previewOptions', 'previewOptions', defaultPreviewOptions);
  34. }
  35. /**
  36. * Workaround for the mangling in production build to break constructor.name
  37. */
  38. static getClassName() {
  39. return 'EditorContainer';
  40. }
  41. /**
  42. * initialize state for drafts
  43. */
  44. initDrafts() {
  45. this.drafts = {};
  46. // restore data from localStorage
  47. const contents = window.localStorage.drafts;
  48. if (contents != null) {
  49. try {
  50. this.drafts = JSON.parse(contents);
  51. }
  52. catch (e) {
  53. window.localStorage.removeItem('drafts');
  54. }
  55. }
  56. if (this.state.pageId == null) {
  57. const draft = this.findDraft(this.state.path);
  58. if (draft != null) {
  59. this.state.markdown = draft;
  60. }
  61. }
  62. }
  63. initEditorOptions(stateKey, localStorageKey, defaultOptions) {
  64. // load from localStorage
  65. const optsStr = window.localStorage[localStorageKey];
  66. let loadedOpts = {};
  67. // JSON.parseparse
  68. if (optsStr != null) {
  69. try {
  70. loadedOpts = JSON.parse(optsStr);
  71. }
  72. catch (e) {
  73. this.localStorage.removeItem(localStorageKey);
  74. }
  75. }
  76. // set to state obj
  77. this.state[stateKey] = Object.assign(defaultOptions, loadedOpts);
  78. }
  79. saveOptsToLocalStorage() {
  80. window.localStorage.setItem('editorOptions', JSON.stringify(this.state.editorOptions));
  81. window.localStorage.setItem('previewOptions', JSON.stringify(this.state.previewOptions));
  82. }
  83. setCaretLine(line) {
  84. const pageEditor = this.appContainer.getComponentInstance('PageEditor');
  85. if (pageEditor != null) {
  86. pageEditor.setCaretLine(line);
  87. }
  88. }
  89. focusToEditor() {
  90. const pageEditor = this.appContainer.getComponentInstance('PageEditor');
  91. if (pageEditor != null) {
  92. pageEditor.focusToEditor();
  93. }
  94. }
  95. // TODO: Remove when SWR is complete
  96. getCurrentOptionsToSave() {
  97. const opt = {
  98. // isSlackEnabled: this.state.isSlackEnabled,
  99. // slackChannels: this.state.slackChannels,
  100. // grant: this.state.grant,
  101. pageTags: this.state.tags,
  102. };
  103. // if (this.state.grantGroupId != null) {
  104. // opt.grantUserGroupId = this.state.grantGroupId;
  105. // }
  106. return opt;
  107. }
  108. // See https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
  109. showUnsavedWarning(e) {
  110. // Cancel the event
  111. e.preventDefault();
  112. // display browser default message
  113. e.returnValue = '';
  114. return '';
  115. }
  116. disableUnsavedWarning() {
  117. window.removeEventListener('beforeunload', this.showUnsavedWarning);
  118. this.isSetBeforeunloadEventHandler = false;
  119. }
  120. enableUnsavedWarning() {
  121. if (!this.isSetBeforeunloadEventHandler) {
  122. window.addEventListener('beforeunload', this.showUnsavedWarning);
  123. this.isSetBeforeunloadEventHandler = true;
  124. }
  125. }
  126. clearDraft(path) {
  127. delete this.drafts[path];
  128. window.localStorage.setItem('drafts', JSON.stringify(this.drafts));
  129. }
  130. clearAllDrafts() {
  131. window.localStorage.removeItem('drafts');
  132. }
  133. saveDraft(path, body) {
  134. this.drafts[path] = body;
  135. window.localStorage.setItem('drafts', JSON.stringify(this.drafts));
  136. }
  137. findDraft(path) {
  138. if (this.drafts != null && this.drafts[path]) {
  139. return this.drafts[path];
  140. }
  141. return null;
  142. }
  143. /**
  144. * Retrieve Editor Settings
  145. */
  146. // async retrieveEditorSettings() {
  147. // if (this.appContainer.isGuestUser) {
  148. // return;
  149. // }
  150. // const { data } = await apiv3Get('/personal-setting/editor-settings');
  151. // if (data?.textlintSettings == null) {
  152. // return;
  153. // }
  154. // // Defaults to null to show modal when not in DB
  155. // const { isTextlintEnabled = null, textlintRules = [] } = data.textlintSettings;
  156. // this.setState({
  157. // isTextlintEnabled,
  158. // textlintRules,
  159. // });
  160. // }
  161. }