EditorContainer.js 4.7 KB

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