EditorContainer.js 4.9 KB

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