From 2e83a51148014e4bc07b37d686a689ad1de5b787 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Thu, 30 Jul 2020 14:29:16 +0300
Subject: [PATCH] fix highlight plugin

---
 wax-prosemirror-plugins/index.js              |  9 ++-
 wax-prosemirror-plugins/package.json          |  4 +-
 .../src/highlightPlugin.js                    | 56 +++++++++++++++++++
 wax-prosemirror-services/package.json         |  5 +-
 .../src/CodeBlockService/CodeBlockService.js  |  5 +-
 5 files changed, 67 insertions(+), 12 deletions(-)
 create mode 100644 wax-prosemirror-plugins/src/highlightPlugin.js

diff --git a/wax-prosemirror-plugins/index.js b/wax-prosemirror-plugins/index.js
index 5377fcb72..72d283b67 100644
--- a/wax-prosemirror-plugins/index.js
+++ b/wax-prosemirror-plugins/index.js
@@ -1,6 +1,5 @@
-export {
-  default as TrackChangePlugin
-} from "./src/trackChanges/TrackChangePlugin";
+export { default as TrackChangePlugin } from './src/trackChanges/TrackChangePlugin';
 
-export { default as CommentPlugin } from "./src/comments/CommentPlugin";
-export { default as WaxSelectionPlugin } from "./src/WaxSelectionPlugin";
+export { default as CommentPlugin } from './src/comments/CommentPlugin';
+export { default as WaxSelectionPlugin } from './src/WaxSelectionPlugin';
+export { default as highlightPlugin } from './src/highlightPlugin';
diff --git a/wax-prosemirror-plugins/package.json b/wax-prosemirror-plugins/package.json
index eb7b8de50..28d548f45 100644
--- a/wax-prosemirror-plugins/package.json
+++ b/wax-prosemirror-plugins/package.json
@@ -16,6 +16,8 @@
     "wax-prosemirror-components": "^0.0.13",
     "wax-prosemirror-core": "^0.0.13",
     "wax-prosemirror-layouts": "^0.0.13",
-    "wax-prosemirror-utilities": "^0.0.13"
+    "wax-prosemirror-utilities": "^0.0.13",
+    "highlight.js": "^10.1.1",
+    "prosemirror-highlightjs": "^0.2.0"
   }
 }
diff --git a/wax-prosemirror-plugins/src/highlightPlugin.js b/wax-prosemirror-plugins/src/highlightPlugin.js
new file mode 100644
index 000000000..40ee57e29
--- /dev/null
+++ b/wax-prosemirror-plugins/src/highlightPlugin.js
@@ -0,0 +1,56 @@
+import hljs from 'highlight.js/lib/core';
+import { getHighlightDecorations } from 'prosemirror-highlightjs';
+import { DecorationSet } from 'prosemirror-view';
+import { Plugin } from 'prosemirror-state';
+
+const highlightPlugin = (nodeTypes = ['code_block']) => {
+  return new Plugin({
+    state: {
+      init(_, instance) {
+        const content = getHighlightDecorations(
+          instance.doc,
+          hljs,
+          nodeTypes,
+          () => undefined,
+        );
+        return DecorationSet.create(instance.doc, content);
+      },
+      apply(tr, set, state) {
+        if (!tr.docChanged) {
+          return set.map(tr.mapping, tr.doc);
+        }
+
+        const {
+          selection: { $from, $to },
+          doc,
+        } = state;
+        let codeBlock = false;
+
+        doc.nodesBetween($from.pos, $to.pos, (node, from) => {
+          if (node.type.name === 'code_block') {
+            codeBlock = true;
+          }
+        });
+
+        if (codeBlock) {
+          const content = getHighlightDecorations(
+            tr.doc,
+            hljs,
+            nodeTypes,
+            () => undefined,
+          );
+
+          return DecorationSet.create(tr.doc, content);
+        }
+        return set.map(tr.mapping, tr.doc);
+      },
+    },
+    props: {
+      decorations(state) {
+        return this.getState(state);
+      },
+    },
+  });
+};
+
+export default highlightPlugin;
diff --git a/wax-prosemirror-services/package.json b/wax-prosemirror-services/package.json
index 266963898..22559ee12 100644
--- a/wax-prosemirror-services/package.json
+++ b/wax-prosemirror-services/package.json
@@ -10,12 +10,10 @@
     "build": "BABEL_ENV=production rollup -c"
   },
   "dependencies": {
-    "highlight.js": "^10.1.1",
     "inversify": "^5.0.1",
     "inversify-inject-decorators": "^3.1.0",
     "lodash": "^4.17.4",
     "prosemirror-commands": "^1.1.3",
-    "prosemirror-highlightjs": "^0.2.0",
     "prosemirror-history": "^1.1.3",
     "prosemirror-keymap": "^1.1.3",
     "prosemirror-schema-list": "^1.1.2",
@@ -27,7 +25,8 @@
     "wax-prosemirror-core": "^0.0.13",
     "wax-prosemirror-layouts": "^0.0.13",
     "wax-prosemirror-schema": "^0.0.13",
-    "wax-prosemirror-utilities": "^0.0.13"
+    "wax-prosemirror-utilities": "^0.0.13",
+    "wax-prosemirror-plugins": "^0.0.13"
   },
   "peerDependencies": {
     "react": "^16.13.1",
diff --git a/wax-prosemirror-services/src/CodeBlockService/CodeBlockService.js b/wax-prosemirror-services/src/CodeBlockService/CodeBlockService.js
index b1fef7016..2e851d322 100644
--- a/wax-prosemirror-services/src/CodeBlockService/CodeBlockService.js
+++ b/wax-prosemirror-services/src/CodeBlockService/CodeBlockService.js
@@ -1,5 +1,4 @@
-import hljs from 'highlight.js/lib/core';
-import { highlightPlugin } from 'prosemirror-highlightjs';
+import { highlightPlugin } from 'wax-prosemirror-plugins';
 import 'highlight.js/styles/github.css';
 
 import { codeBlockNode } from 'wax-prosemirror-schema';
@@ -8,7 +7,7 @@ import CodeBlockTool from './CodeBlockTool';
 
 export default class CodeBlockService extends Service {
   boot() {
-    this.app.PmPlugins.add('highlightPlugin', highlightPlugin(hljs));
+    this.app.PmPlugins.add('highlightPlugin', highlightPlugin());
   }
 
   register() {
-- 
GitLab