From 90be9b8d1d70f7bf7ca9d674abfd4145be6f4283 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Tue, 1 Jun 2021 09:38:10 +0300
Subject: [PATCH] boot services only once

---
 editors/demo/src/Editoria/Editoria.js         |  1 +
 wax-prosemirror-core/src/Application.js       | 29 ++++++++++---------
 wax-prosemirror-core/src/WaxView.js           | 10 +++++--
 .../src/ShortCutsService/ShortCutsService.js  | 15 +++++++---
 4 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/editors/demo/src/Editoria/Editoria.js b/editors/demo/src/Editoria/Editoria.js
index 45d5b114e..a13bed431 100644
--- a/editors/demo/src/Editoria/Editoria.js
+++ b/editors/demo/src/Editoria/Editoria.js
@@ -5,6 +5,7 @@ import { Wax } from 'wax-prosemirror-core';
 import { EditoriaLayout, EditoriaMobileLayout } from './layout';
 import { config, configMobile } from './config';
 import { demo } from './demo';
+import HHMI from '../HHMI/HHMI';
 
 const renderImage = file => {
   const reader = new FileReader();
diff --git a/wax-prosemirror-core/src/Application.js b/wax-prosemirror-core/src/Application.js
index 63b621a86..0d0fe6b80 100644
--- a/wax-prosemirror-core/src/Application.js
+++ b/wax-prosemirror-core/src/Application.js
@@ -13,25 +13,28 @@ export default class Application {
 
   registerServices() {
     let count = 0;
-    while (count < this.config.get('config.services').length) {
-      const allServices = this.config.get('config.services');
-      const service = this.config.get('config.services')[count];
-      /*
+    console.log(this.config);
+    if (this.config) {
+      while (count < this.config.get('config.services').length) {
+        const allServices = this.config.get('config.services');
+        const service = this.config.get('config.services')[count];
+        /*
         set App to every service
         so services can have access to containers and config
         */
-      service.setApp(this);
+        service.setApp(this);
 
-      if (service.dependencies) {
-        const servicePos = count;
-        allServices.splice(servicePos + 1, 0, ...service.dependencies);
-      }
+        if (service.dependencies) {
+          const servicePos = count;
+          allServices.splice(servicePos + 1, 0, ...service.dependencies);
+        }
 
-      if (service.register) {
-        service.register();
-      }
+        if (service.register) {
+          service.register();
+        }
 
-      count += 1;
+        count += 1;
+      }
     }
   }
 
diff --git a/wax-prosemirror-core/src/WaxView.js b/wax-prosemirror-core/src/WaxView.js
index e6faf536c..beb610271 100644
--- a/wax-prosemirror-core/src/WaxView.js
+++ b/wax-prosemirror-core/src/WaxView.js
@@ -4,6 +4,7 @@ import React, {
   useCallback,
   useMemo,
   useEffect,
+  useState,
 } from 'react';
 
 import applyDevTools from 'prosemirror-dev-tools';
@@ -21,11 +22,12 @@ import WaxOptions from './WaxOptions';
 const WaxPortals = ComponentPlugin('waxPortals');
 
 let previousDoc;
-let view;
+
 export default props => {
   const { readonly, onBlur, debug, autoFocus, user, targetFormat } = props;
   const editorRef = useRef();
-
+  let view;
+  const [mounted, setMounted] = useState(false);
   const context = useContext(WaxContext);
   const { createPortal } = useContext(PortalContext);
 
@@ -33,7 +35,7 @@ export default props => {
 
   const schema = context.app.getSchema();
 
-  if (!view) {
+  if (!mounted) {
     context.app.bootServices();
   }
 
@@ -78,6 +80,8 @@ export default props => {
           },
         );
 
+        setMounted(true);
+
         context.updateView(
           {
             main: view,
diff --git a/wax-prosemirror-services/src/ShortCutsService/ShortCutsService.js b/wax-prosemirror-services/src/ShortCutsService/ShortCutsService.js
index cbccb3caa..1fd9a92c1 100644
--- a/wax-prosemirror-services/src/ShortCutsService/ShortCutsService.js
+++ b/wax-prosemirror-services/src/ShortCutsService/ShortCutsService.js
@@ -9,16 +9,23 @@ export default class ShortCutsService extends Service {
     shortCuts.createShortCuts();
   }
 
+  // TODO start ShortCuts as Schema is initiated
   register() {
     const { PmPlugins } = this.app;
     this.container
       .bind('ShortCuts')
       .toDynamicValue(() => {
-        const {
-          schema: { schema },
-        } = this.app;
+        if (this.app.schema) {
+          return new ShortCuts(
+            PmPlugins,
+            this.container.get('Schema').getSchema(),
+          );
+        }
 
-        return new ShortCuts(PmPlugins, schema);
+        return new ShortCuts(
+          PmPlugins,
+          this.container.get('Schema').getSchema(),
+        );
       })
       .inSingletonScope();
   }
-- 
GitLab