EditorContainer.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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) {
  10. super();
  11. this.appContainer = appContainer;
  12. this.appContainer.registerContainer(this);
  13. this.state = {
  14. tags: null,
  15. };
  16. this.isSetBeforeunloadEventHandler = false;
  17. this.initDrafts();
  18. }
  19. /**
  20. * Workaround for the mangling in production build to break constructor.name
  21. */
  22. static getClassName() {
  23. return 'EditorContainer';
  24. }
  25. /**
  26. * initialize state for drafts
  27. */
  28. initDrafts() {
  29. this.drafts = {};
  30. // restore data from localStorage
  31. const contents = window.localStorage.drafts;
  32. if (contents != null) {
  33. try {
  34. this.drafts = JSON.parse(contents);
  35. }
  36. catch (e) {
  37. window.localStorage.removeItem('drafts');
  38. }
  39. }
  40. if (this.state.pageId == null) {
  41. const draft = this.findDraft(this.state.path);
  42. if (draft != null) {
  43. this.state.markdown = draft;
  44. }
  45. }
  46. }
  47. setCaretLine(line) {
  48. const pageEditor = this.appContainer.getComponentInstance('PageEditor');
  49. if (pageEditor != null) {
  50. pageEditor.setCaretLine(line);
  51. }
  52. }
  53. focusToEditor() {
  54. const pageEditor = this.appContainer.getComponentInstance('PageEditor');
  55. if (pageEditor != null) {
  56. pageEditor.focusToEditor();
  57. }
  58. }
  59. getCurrentOptionsToSave() {
  60. const opt = {
  61. pageTags: this.state.tags,
  62. };
  63. return opt;
  64. }
  65. // See https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
  66. showUnsavedWarning(e) {
  67. // Cancel the event
  68. e.preventDefault();
  69. // display browser default message
  70. e.returnValue = '';
  71. return '';
  72. }
  73. disableUnsavedWarning() {
  74. window.removeEventListener('beforeunload', this.showUnsavedWarning);
  75. this.isSetBeforeunloadEventHandler = false;
  76. }
  77. enableUnsavedWarning() {
  78. if (!this.isSetBeforeunloadEventHandler) {
  79. window.addEventListener('beforeunload', this.showUnsavedWarning);
  80. this.isSetBeforeunloadEventHandler = true;
  81. }
  82. }
  83. clearDraft(path) {
  84. delete this.drafts[path];
  85. window.localStorage.setItem('drafts', JSON.stringify(this.drafts));
  86. }
  87. clearAllDrafts() {
  88. window.localStorage.removeItem('drafts');
  89. }
  90. saveDraft(path, body) {
  91. this.drafts[path] = body;
  92. window.localStorage.setItem('drafts', JSON.stringify(this.drafts));
  93. }
  94. findDraft(path) {
  95. if (this.drafts != null && this.drafts[path]) {
  96. return this.drafts[path];
  97. }
  98. return null;
  99. }
  100. }