From 3bf0758bc4b356772f1db1676994dc304ea2cd92 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Wed, 29 Mar 2023 10:24:38 +0300
Subject: [PATCH] disallow img pasted plugin

---
 editors/demo/src/Editoria/config/config.js    | 16 ++++++++-
 wax-prosemirror-services/index.js             |  4 +++
 .../src/ImageService/ImageService.js          |  6 ++++
 .../plugins/disallowPasteImagesPlugin.js      | 33 +++++++++++++++++++
 4 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 wax-prosemirror-services/src/ImageService/plugins/disallowPasteImagesPlugin.js

diff --git a/editors/demo/src/Editoria/config/config.js b/editors/demo/src/Editoria/config/config.js
index 332bad041..0cce640a9 100644
--- a/editors/demo/src/Editoria/config/config.js
+++ b/editors/demo/src/Editoria/config/config.js
@@ -44,6 +44,7 @@ import {
   CustomTagBlockToolGroupService,
   CustomTagService,
   YjsService,
+  disallowPasteImagesPlugin,
 } from 'wax-prosemirror-services';
 
 import { EditoriaSchema } from 'wax-prosemirror-core';
@@ -72,6 +73,10 @@ const updateTrackStatus = status => {
   // console.log('status', status);
 };
 
+const onWarning = message => {
+  console.log(message);
+};
+
 export default {
   MenuService: [
     {
@@ -140,7 +145,16 @@ export default {
       reject: true,
     },
   },
-  PmPlugins: [columnResizing(), tableEditing(), invisibles([hardBreak()])],
+  PmPlugins: [
+    columnResizing(),
+    tableEditing(),
+    invisibles([hardBreak()]),
+    disallowPasteImagesPlugin(() =>
+      onWarning(
+        'Images are not allowed. Please upload them through filemanager',
+      ),
+    ),
+  ],
   ImageService: { showAlt: true },
   CustomTagService: {
     tags: [
diff --git a/wax-prosemirror-services/index.js b/wax-prosemirror-services/index.js
index f58d39841..e413c6e6a 100644
--- a/wax-prosemirror-services/index.js
+++ b/wax-prosemirror-services/index.js
@@ -68,3 +68,7 @@ export { default as QuestionsDropDownToolGroupService } from './src/WaxToolGroup
 export { default as OENContainersToolGroupService } from './src/WaxToolGroups/OENContainersToolGroupService/OENContainersToolGroupService';
 export { default as OENLeftToolGroupService } from './src/WaxToolGroups/OENLeftToolGroupService/OENLeftToolGroupService';
 export { default as FindAndReplaceToolGroupService } from './src/WaxToolGroups/FindAndReplaceToolGroupService/FindAndReplaceToolGroupService';
+
+/* Plugins */
+
+export { default as disallowPasteImagesPlugin } from './src/ImageService/plugins/disallowPasteImagesPlugin';
diff --git a/wax-prosemirror-services/src/ImageService/ImageService.js b/wax-prosemirror-services/src/ImageService/ImageService.js
index e23e55aac..f24ac9200 100644
--- a/wax-prosemirror-services/src/ImageService/ImageService.js
+++ b/wax-prosemirror-services/src/ImageService/ImageService.js
@@ -2,6 +2,7 @@ import { Service } from 'wax-prosemirror-core';
 import { imageNode, figureCaptionNode, figureNode } from './schema';
 import PlaceHolderPlugin from './plugins/placeHolderPlugin';
 import captionPlugin from './plugins/captionPlugin';
+import disallowPasteImagesPlugin from './plugins/disallowPasteImagesPlugin';
 import Image from './Image';
 import './image.css';
 import AltComponent from './AltComponent';
@@ -10,6 +11,11 @@ class ImageService extends Service {
   name = 'ImageService';
 
   boot() {
+    // this.app.PmPlugins.add(
+    //   'disallowPasteImagesPlugin',
+    //   disallowPasteImagesPlugin('disallowPasteImagesPlugin', this.app.context),
+    // );
+
     this.app.PmPlugins.add(
       'imagePlaceHolder',
       PlaceHolderPlugin('imagePlaceHolder'),
diff --git a/wax-prosemirror-services/src/ImageService/plugins/disallowPasteImagesPlugin.js b/wax-prosemirror-services/src/ImageService/plugins/disallowPasteImagesPlugin.js
new file mode 100644
index 000000000..bc562c059
--- /dev/null
+++ b/wax-prosemirror-services/src/ImageService/plugins/disallowPasteImagesPlugin.js
@@ -0,0 +1,33 @@
+/* eslint-disable no-param-reassign */
+import { Plugin, PluginKey } from 'prosemirror-state';
+
+const disallowPasteImagesPlugin = new PluginKey('disallowPasteImagesPlugin');
+
+export default onWarning => {
+  return new Plugin({
+    key: disallowPasteImagesPlugin,
+    props: {
+      transformPasted: slice => {
+        const {
+          content: { content },
+        } = slice;
+
+        let imageFound = false;
+        content.forEach(node => {
+          if (node.type.name === 'image') {
+            node.attrs.src = '';
+            node.attrs.alt = '';
+            imageFound = true;
+          }
+          if (node.type.name === 'figure') {
+            node.lastChild.attrs.src = '';
+            node.lastChild.attrs.alt = '';
+            imageFound = true;
+          }
+        });
+        if (imageFound) onWarning();
+        return slice;
+      },
+    },
+  });
+};
-- 
GitLab