From 06934c96d10ab01b721f33a988d802b92970fdde Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Fri, 13 Dec 2019 12:59:48 +0200
Subject: [PATCH] feat(RuleService): ability to add a Rule from any other
 service

---
 editors/editoria/src/config/config.js         |  2 +-
 .../src/Config/defaultConfig.js               |  4 +-
 wax-prosemirror-core/src/Wax.js               |  5 +-
 .../PlaceholderService/PlaceholderService.js  |  3 +
 .../src/RulesService/Rules.js                 | 64 ++++++++++---------
 .../src/RulesService/RulesService.js          |  7 +-
 6 files changed, 46 insertions(+), 39 deletions(-)

diff --git a/editors/editoria/src/config/config.js b/editors/editoria/src/config/config.js
index 82fb3775f..4138b0607 100644
--- a/editors/editoria/src/config/config.js
+++ b/editors/editoria/src/config/config.js
@@ -13,7 +13,7 @@ export default {
   ],
   RulesService: [
     {
-      rules: [emDash, ellipsis]
+      rules: [ellipsis]
     }
   ]
 };
diff --git a/wax-prosemirror-core/src/Config/defaultConfig.js b/wax-prosemirror-core/src/Config/defaultConfig.js
index 7cb00a632..10ea27853 100644
--- a/wax-prosemirror-core/src/Config/defaultConfig.js
+++ b/wax-prosemirror-core/src/Config/defaultConfig.js
@@ -11,13 +11,13 @@ import {
 
 export default {
   services: [
+    new RulesService(),
     new LayoutService(),
     new MenuService(),
     new RedoUndoService(),
     new AnnotationService(),
     new TextStyleService(),
     new PlaceholderService(),
-    new ImageService(),
-    new RulesService()
+    new ImageService()
   ]
 };
diff --git a/wax-prosemirror-core/src/Wax.js b/wax-prosemirror-core/src/Wax.js
index 0665cda4f..91666d49e 100644
--- a/wax-prosemirror-core/src/Wax.js
+++ b/wax-prosemirror-core/src/Wax.js
@@ -51,6 +51,7 @@ class Wax extends Component {
     const { value, onChange, options } = this.props;
     const { schema, plugins, keys, rules } = options;
     const WaxOnchange = onChange ? onChange : value => true;
+    this.application.bootServices();
 
     // const WaxShortCuts = keys
     //   ? keys
@@ -63,7 +64,7 @@ class Wax extends Component {
       placeholder({ content: this.props.placeholder }),
       ...this.application.getPlugins()
     ]);
-
+    console.log("cretated?");
     this.WaxOptions = {
       schema,
       plugins: finalPlugins
@@ -73,8 +74,6 @@ class Wax extends Component {
     const serialize = serializer(schema);
     this.WaxOptions.doc = parse(editorContent);
 
-    this.application.bootServices();
-
     this.onChange = debounce(
       value => {
         WaxOnchange(serialize(value));
diff --git a/wax-prosemirror-plugins/src/PlaceholderService/PlaceholderService.js b/wax-prosemirror-plugins/src/PlaceholderService/PlaceholderService.js
index a3c08b71f..0b16f671f 100644
--- a/wax-prosemirror-plugins/src/PlaceholderService/PlaceholderService.js
+++ b/wax-prosemirror-plugins/src/PlaceholderService/PlaceholderService.js
@@ -1,11 +1,14 @@
 import Service from "wax-prosemirror-core/src/services/Service";
 import placeholderPlugin from "./pmPlugins/placeholderPlugin";
 const PLUGIN_KEY = "imagePlaceHolder";
+import { emDash } from "prosemirror-inputrules";
 
 export default class PlaceholderService extends Service {
   name = "PlaceholderService";
 
   register() {
+    const rules = this.container.get("Rules");
+    rules.addRule([emDash]);
     this.app.PmPlugins.add(PLUGIN_KEY, placeholderPlugin(PLUGIN_KEY));
   }
 }
diff --git a/wax-prosemirror-plugins/src/RulesService/Rules.js b/wax-prosemirror-plugins/src/RulesService/Rules.js
index e2c79deda..041ac1c75 100644
--- a/wax-prosemirror-plugins/src/RulesService/Rules.js
+++ b/wax-prosemirror-plugins/src/RulesService/Rules.js
@@ -11,43 +11,45 @@ class Rules {
   constructor(schema, plugins) {
     this.PmPlugins = plugins;
     this.schema = schema;
+    this.extendedRules = this.allRules();
   }
 
   addRule(rules) {
-    const rulesCreated = inputRules(this.allRules(rules));
+    this.extendedRules.push(...rules);
+  }
+
+  createRules() {
+    const rulesCreated = inputRules({ rules: this.extendedRules });
     this.PmPlugins.add("rules", rulesCreated);
   }
 
-  allRules(rules = []) {
-    return {
-      rules: [
-        ...smartQuotes,
-        ...rules,
-        // > blockquote
-        wrappingInputRule(/^\s*>\s$/, this.schema.nodes.blockquote),
-
-        // 1. ordered list
-        wrappingInputRule(
-          /^(\d+)\.\s$/,
-          this.schema.nodes.ordered_list,
-          match => ({ order: +match[1] }),
-          (match, node) => node.childCount + node.attrs.order === +match[1]
-        ),
-
-        // * bullet list
-        wrappingInputRule(/^\s*([-+*])\s$/, this.schema.nodes.bullet_list),
-
-        // ``` code block
-        textblockTypeInputRule(/^```$/, this.schema.nodes.code_block),
-
-        // # heading
-        textblockTypeInputRule(
-          new RegExp("^(#{1,6})\\s$"),
-          this.schema.nodes.heading,
-          match => ({ level: match[1].length })
-        )
-      ]
-    };
+  allRules() {
+    return [
+      ...smartQuotes,
+      // > blockquote
+      wrappingInputRule(/^\s*>\s$/, this.schema.nodes.blockquote),
+
+      // 1. ordered list
+      wrappingInputRule(
+        /^(\d+)\.\s$/,
+        this.schema.nodes.ordered_list,
+        match => ({ order: +match[1] }),
+        (match, node) => node.childCount + node.attrs.order === +match[1]
+      ),
+
+      // * bullet list
+      wrappingInputRule(/^\s*([-+*])\s$/, this.schema.nodes.bullet_list),
+
+      // ``` code block
+      textblockTypeInputRule(/^```$/, this.schema.nodes.code_block),
+
+      // # heading
+      textblockTypeInputRule(
+        new RegExp("^(#{1,6})\\s$"),
+        this.schema.nodes.heading,
+        match => ({ level: match[1].length })
+      )
+    ];
   }
 }
 
diff --git a/wax-prosemirror-plugins/src/RulesService/RulesService.js b/wax-prosemirror-plugins/src/RulesService/RulesService.js
index b64f26214..88b8487fc 100644
--- a/wax-prosemirror-plugins/src/RulesService/RulesService.js
+++ b/wax-prosemirror-plugins/src/RulesService/RulesService.js
@@ -4,16 +4,19 @@ import Rules from "./Rules";
 export default class RulesService extends Service {
   name = "RulesService";
 
+  boot() {
+    this.container.get("Rules").createRules();
+  }
+
   register() {
     const { schema } = this.container.get("config").options;
-    const configRules = this.config[0].rules;
     const PmPlugins = this.app.PmPlugins;
 
     this.container
       .bind("Rules")
       .toDynamicValue(() => new Rules(schema, PmPlugins))
       .inSingletonScope();
-
+    const configRules = this.config[0].rules;
     const rules = this.container.get("Rules");
     rules.addRule(configRules);
   }
-- 
GitLab