EditorContainer.js 4.6 KB

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