2
0

EditorContainer.js 5.6 KB

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