EditorContainer.js 4.9 KB

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