EditorContainer.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { Container } from 'unstated';
  2. import loggerFactory from '@alias/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. const mainContent = document.querySelector('#content-main');
  14. if (mainContent == null) {
  15. logger.debug('#content-main element is not exists');
  16. return;
  17. }
  18. this.state = {
  19. tags: null,
  20. isSlackEnabled: false,
  21. slackChannels: mainContent.getAttribute('data-slack-channels') || '',
  22. grant: 1, // default: public
  23. grantGroupId: null,
  24. grantGroupName: null,
  25. editorOptions: {},
  26. previewOptions: {},
  27. indentSize: this.appContainer.config.adminPreferredIndentSize || 4,
  28. };
  29. this.isSetBeforeunloadEventHandler = false;
  30. this.initStateGrant();
  31. this.initDrafts();
  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 page permission
  43. */
  44. initStateGrant() {
  45. const mainContent = document.getElementById('content-main');
  46. if (mainContent == null) {
  47. logger.debug('#content-main element is not exists');
  48. return;
  49. }
  50. this.state.grant = +mainContent.getAttribute('data-page-grant');
  51. const grantGroupId = mainContent.getAttribute('data-page-grant-group');
  52. if (grantGroupId != null && grantGroupId.length > 0) {
  53. this.state.grantGroupId = grantGroupId;
  54. this.state.grantGroupName = mainContent.getAttribute('data-page-grant-group-name');
  55. }
  56. }
  57. /**
  58. * initialize state for drafts
  59. */
  60. initDrafts() {
  61. this.drafts = {};
  62. // restore data from localStorage
  63. const contents = window.localStorage.drafts;
  64. if (contents != null) {
  65. try {
  66. this.drafts = JSON.parse(contents);
  67. }
  68. catch (e) {
  69. window.localStorage.removeItem('drafts');
  70. }
  71. }
  72. if (this.state.pageId == null) {
  73. const draft = this.findDraft(this.state.path);
  74. if (draft != null) {
  75. this.state.markdown = draft;
  76. }
  77. }
  78. }
  79. initEditorOptions(stateKey, localStorageKey, defaultOptions) {
  80. // load from localStorage
  81. const optsStr = window.localStorage[localStorageKey];
  82. let loadedOpts = {};
  83. // JSON.parseparse
  84. if (optsStr != null) {
  85. try {
  86. loadedOpts = JSON.parse(optsStr);
  87. }
  88. catch (e) {
  89. this.localStorage.removeItem(localStorageKey);
  90. }
  91. }
  92. // set to state obj
  93. this.state[stateKey] = Object.assign(defaultOptions, loadedOpts);
  94. }
  95. saveOptsToLocalStorage() {
  96. window.localStorage.setItem('editorOptions', JSON.stringify(this.state.editorOptions));
  97. window.localStorage.setItem('previewOptions', JSON.stringify(this.state.previewOptions));
  98. }
  99. setCaretLine(line) {
  100. const pageEditor = this.appContainer.getComponentInstance('PageEditor');
  101. if (pageEditor != null) {
  102. pageEditor.setCaretLine(line);
  103. }
  104. }
  105. focusToEditor() {
  106. const pageEditor = this.appContainer.getComponentInstance('PageEditor');
  107. if (pageEditor != null) {
  108. pageEditor.focusToEditor();
  109. }
  110. }
  111. getCurrentOptionsToSave() {
  112. const opt = {
  113. isSlackEnabled: this.state.isSlackEnabled,
  114. slackChannels: this.state.slackChannels,
  115. grant: this.state.grant,
  116. pageTags: this.state.tags,
  117. };
  118. if (this.state.grantGroupId != null) {
  119. opt.grantUserGroupId = this.state.grantGroupId;
  120. }
  121. return opt;
  122. }
  123. // See https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
  124. showUnsavedWarning(e) {
  125. // Cancel the event
  126. e.preventDefault();
  127. // display browser default message
  128. e.returnValue = '';
  129. return '';
  130. }
  131. disableUnsavedWarning() {
  132. window.removeEventListener('beforeunload', this.showUnsavedWarning);
  133. this.isSetBeforeunloadEventHandler = false;
  134. }
  135. enableUnsavedWarning() {
  136. if (!this.isSetBeforeunloadEventHandler) {
  137. window.addEventListener('beforeunload', this.showUnsavedWarning);
  138. this.isSetBeforeunloadEventHandler = true;
  139. }
  140. }
  141. clearDraft(path) {
  142. delete this.drafts[path];
  143. window.localStorage.setItem('drafts', JSON.stringify(this.drafts));
  144. }
  145. clearAllDrafts() {
  146. window.localStorage.removeItem('drafts');
  147. }
  148. saveDraft(path, body) {
  149. this.drafts[path] = body;
  150. window.localStorage.setItem('drafts', JSON.stringify(this.drafts));
  151. }
  152. findDraft(path) {
  153. if (this.drafts != null && this.drafts[path]) {
  154. return this.drafts[path];
  155. }
  156. return null;
  157. }
  158. }