Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Support] Image Plugin configuration #22

Open
aurelienblais opened this issue Apr 8, 2020 · 1 comment
Open

[Support] Image Plugin configuration #22

aurelienblais opened this issue Apr 8, 2020 · 1 comment

Comments

@aurelienblais
Copy link

Hi,

I need some help with the Image Plugin configuration.
Here's my current "ImageModule" file

import {NgModule} from '@angular/core';
import {EDITOR_JS_TOOL_INJECTOR, PLUGIN_CONFIG, PluginTypes} from '@tinynodes/ngx-editorjs-plugins';
import Image from '@editorjs/image';

@NgModule({
  providers: [{
    provide: EDITOR_JS_TOOL_INJECTOR,
    useValue: Image,
    multi: true
  }, {
    provide: PLUGIN_CONFIG,
    useValue: {
      key: 'image',
      type: PluginTypes.Block,
      pluginName: 'EditorJS Image',
      config: {
        uploader: {
          uploadByFile(file) {
            this.attachmentSvc.create(file).subscribe((e: any) => {
              return {
                success: 1,
                file: {
                  url: e.filePath
                }
              };
            });
          }
        }
      }
    },
    multi: true
  }]
})
export class ImageModule {
}

The configuration is correctly injected and loaded by the library, but I need help to setup the "AttachmentService" call, which currently fail (as the service does'nt export any static method as it make use of the project Apollo-client instance).

Is there any way to call non-static method from the config ? If there's no workaround, how are we supposed to make our custom upload logics ?

Thanks for your help

@wSedlacek
Copy link

wSedlacek commented Jun 20, 2020

Hey @aurelienblais Not sure if you are still working on this but I figured I would follow up with you since I believe I figured out a solution for you.

In order to gain access to the attachmentSvc you would probably need to use a useFactory with deps rather then
useValue

Example
image-plugin.factory.ts

export const imagePluginFactory = (attachmentSvc: AttachmentService) => ({
      key: 'image',
      type: PluginTypes.Block,
      pluginName: 'EditorJS Image',
      config: {
        uploader: {
          uploadByFile(file) {
           attachmentSvc.create(file).subscribe((e: any) => {
              return {
                success: 1,
                file: {
                  url: e.filePath
                }
              };
            });
          }
        }
      }
    });
import { imagePluginFactory } from './image-plugin.factory';

@NgModule({
  providers: [{
    provide: EDITOR_JS_TOOL_INJECTOR,
    useValue: Image,
    multi: true
  }, {
    provide: PLUGIN_CONFIG,
    useFactory: imagePluginFactory,
    deps: [AttachmentService],
    multi: true
  }]
})
export class ImageModule {
}

Check out the documentation at https://angular.io/guide/dependency-injection-providers#factory-providers

You could even assign the uploader as the entire attachmentSvc then have that service have a method titled uploadByFile (You would probably want to build an interface that you implement like OnInit to ensure any services built this way work as expected)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants