AppContainer.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { Container } from 'unstated';
  2. // import { i18nFactory } from '../util/i18n';
  3. /**
  4. * Service container related to options for Application
  5. * @extends {Container} unstated Container
  6. */
  7. export default class AppContainer extends Container {
  8. constructor() {
  9. super();
  10. this.config = JSON.parse(document.getElementById('growi-context-hydrate').textContent || '{}');
  11. // init i18n
  12. const currentUserElem = document.getElementById('growi-current-user');
  13. let userLocaleId;
  14. if (currentUserElem != null) {
  15. const currentUser = JSON.parse(currentUserElem.textContent);
  16. userLocaleId = currentUser?.lang;
  17. }
  18. // this.i18n = i18nFactory(userLocaleId);
  19. this.containerInstances = {};
  20. this.componentInstances = {};
  21. }
  22. /**
  23. * Workaround for the mangling in production build to break constructor.name
  24. */
  25. static getClassName() {
  26. return 'AppContainer';
  27. }
  28. initApp() {
  29. this.injectToWindow();
  30. }
  31. initContents() {
  32. const body = document.querySelector('body');
  33. this.isDocSaved = true;
  34. const isPluginEnabled = body.dataset.pluginEnabled === 'true';
  35. if (isPluginEnabled) {
  36. this.initPlugins();
  37. }
  38. this.injectToWindow();
  39. }
  40. initPlugins() {
  41. const growiPlugin = window.growiPlugin;
  42. growiPlugin.installAll(this);
  43. }
  44. injectToWindow() {
  45. // for fix lint error
  46. // window.appContainer = this;
  47. // const growiRenderer = new GrowiRenderer(this.getConfig());
  48. // growiRenderer.init();
  49. // window.growiRenderer = growiRenderer;
  50. // // backward compatibility
  51. // window.crowi = this;
  52. // window.crowiRenderer = window.growiRenderer;
  53. // window.crowiPlugin = window.growiPlugin;
  54. }
  55. getConfig() {
  56. return this.config;
  57. }
  58. /**
  59. * Register unstated container instance
  60. * @param {object} instance unstated container instance
  61. */
  62. registerContainer(instance) {
  63. if (instance == null) {
  64. throw new Error('The specified instance must not be null');
  65. }
  66. const className = instance.constructor.getClassName();
  67. if (this.containerInstances[className] != null) {
  68. throw new Error('The specified instance couldn\'t register because the same type object has already been registered');
  69. }
  70. this.containerInstances[className] = instance;
  71. }
  72. /**
  73. * Get registered unstated container instance
  74. * !! THIS METHOD SHOULD ONLY BE USED FROM unstated CONTAINERS !!
  75. * !! From component instances, inject containers with `import { Subscribe } from 'unstated'` !!
  76. *
  77. * @param {string} className
  78. */
  79. getContainer(className) {
  80. return this.containerInstances[className];
  81. }
  82. /**
  83. * Register React component instance
  84. * @param {string} id
  85. * @param {object} instance React component instance
  86. */
  87. registerComponentInstance(id, instance) {
  88. if (instance == null) {
  89. throw new Error('The specified instance must not be null');
  90. }
  91. this.componentInstances[id] = instance;
  92. }
  93. /**
  94. * Get registered React component instance
  95. * @param {string} id
  96. */
  97. getComponentInstance(id) {
  98. return this.componentInstances[id];
  99. }
  100. }