vova07 / yii2-imperavi-widget

Imperavi Redactor widget for Yii 2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No Choose, Upload, Delete buttons above editing field

sergmoro1 opened this issue · comments

No bundles loaded: ImageManagerAsset, FileManagerAsset
I change src/Widget.php
like this:

protected function registerClientScripts()
{
    $view = $this->getView();
    $selector = Json::encode($this->selector);
    $asset = Yii::$container->get(Asset::className());
    $asset = $asset::register($view);

    if (isset($this->settings['lang'])) {
        $asset->language = $this->settings['lang'];
    }
    if (isset($this->settings['plugins'])) {
        $asset->plugins = $this->settings['plugins'];
    }
// !!! Here changes !!!	
    bundles\ImageManagerAsset::register($this->view);
    bundles\FileManagerAsset::register($this->view);

Obviously this is not ideal, but working :) Apparently it is necessary to analyze imageManagerJson and fileManagerJson in settings['plugins'] or something else.

Hi @sergmoro1,
Why you didn't use the examples from README ?

Enable custom image manager with delete functionality

// DefaultController.php
public function actions()
{
    return [
        'images-get' => [
            'class' => 'vova07\imperavi\actions\GetImagesAction',
            'url' => 'http://my-site.com/images/', // Directory URL address, where files are stored.
            'path' => '@alias/to/my/path', // Or absolute path to directory where files are stored.
        ],
        'image-upload' => [
            'class' => 'vova07\imperavi\actions\UploadFileAction',
            'url' => 'http://my-site.com/images/', // Directory URL address, where files are stored.
            'path' => '@alias/to/my/path', // Or absolute path to directory where files are stored.
        ],
        'file-delete' => [
            'class' => 'vova07\imperavi\actions\DeleteFileAction',
            'url' => 'http://my-site.com/statics/', // Directory URL address, where files are stored.
            'path' => '/var/www/my-site.com/web/statics', // Or absolute path to directory where files are stored.
        ],
    ];
}

// View.php
echo \vova07\imperavi\Widget::widget([
    'selector' => '#my-textarea-id',
    'settings' => [
        'lang' => 'ru',
        'minHeight' => 200,
        'imageUpload' => Url::to(['/default/image-upload']),
        'imageDelete' => Url::to(['/default/file-delete']),
        'imageManagerJson' => Url::to(['/default/images-get']),
    ],
    'plugins' => [
        'imagemanager' => 'vova07\imperavi\bundles\ImageManagerAsset',              
    ],
]);

Enable custom file manager with delete functionality

// DefaultController.php
public function actions()
{
    return [
        'files-get' => [
            'class' => 'vova07\imperavi\actions\GetFilesAction',
            'url' => 'http://my-site.com/images/', // Directory URL address, where files are stored.
            'path' => '@alias/to/my/path', // Or absolute path to directory where files are stored.
        ],
        'file-upload' => [
            'class' => 'vova07\imperavi\actions\UploadAction',
            'url' => 'http://my-site.com/files/', // Directory URL address, where files are stored.
            'path' => '@alias/to/my/path', // Or absolute path to directory where files are stored.
            'uploadOnlyImage' => false, // For any kind of files uploading.
        ],
        'file-delete' => [
            'class' => 'vova07\imperavi\actions\DeleteFileAction',
            'url' => 'http://my-site.com/statics/', // Directory URL address, where files are stored.
            'path' => '/var/www/my-site.com/web/statics', // Or absolute path to directory where files are stored.
        ],
    ];
}

// View.php
echo \vova07\imperavi\Widget::widget([
    'selector' => '#my-textarea-id',
    'settings' => [
        'lang' => 'ru',
        'minHeight' => 200,
        'fileUpload' => Url::to(['/default/file-upload']),
        'fileDelete' => Url::to(['/default/file-delete']),
        'fileManagerJson' => Url::to(['/default/files-get']),
    ],
    'plugins' => [
        'filemanager' => 'vova07\imperavi\bundles\FileManagerAsset',              
    ],
]);

It's not obvious. If you define

<?= $form->field($model, 'content')->widget(Widget::className(), [
	'settings' => [
		'lang' => 'ru',
		'minHeight' => 200,
		'replaceDivs' => false,
		'fileUpload' => Url::to(['site/file-upload']),			
		'imageUpload' => Url::to(['site/image-upload']),
		'fileDelete' => Url::to(['site/delete-file']),
		'imageDelete' => Url::to(['site/delete-file']),
		'fileManagerJson' => Url::to(['site/get-files']),		
		'imageManagerJson' => Url::to(['site/get-images']),		

you can expect (see two last lines) that all needed Assets will be loaded.
But I see, how it woks now. Thanks.

That's why I provide a basic documentation.
However it will be really ugly code in case I'll do that assets registration automatically in the code.
Moreover this work like that because these are custom plugins and they are just used as universally solution.
I suppose I can close this issue. Feel free to re-open it in case you need that.

Ugly, why? In a settings I wrote

'fileManagerJson' => Url::to(['site/get-files']),

It's mean Assets must be loaded, isn't it?

And I change code as you recommeded:

<?= $form->field($model, 'content')->widget(Widget::className(), [
	'settings' => [
		'lang' => 'ru',
		'minHeight' => 200,
		'replaceDivs' => false,
		'fileUpload' => Url::to(['site/file-upload']),			
		'imageUpload' => Url::to(['site/image-upload']),
		'fileDelete' => Url::to(['site/delete-file']),
		'imageDelete' => Url::to(['site/delete-file']),
		'fileManagerJson' => Url::to(['site/get-files']),		
		'imageManagerJson' => Url::to(['site/get-images']),		
		'plugins' => [
			'clips',
			'fullscreen',
			'table',
		],
	'plugins' => [
		'filemanager' => 'vova07\imperavi\bundles\FileManagerAsset',              
		'imagemanager' => 'vova07\imperavi\bundles\ImageManagerAsset',              
	        ],
	]
]); ?>

It's again not work.

Yes, but doing that for one plugin we should do that for all of them.
That mean that the code itself will contain a lot of isset() which actually didn't give a lot of benefits.
Moreover that make me required to learn more about any conflicts and exception that JS library may have.
Hope that's not a big problem for you, and hope it solve you needs.
Thanks!

@sergmoro1 what's actually doesn't work ?
Can you check the browser console, is there any errors ?

I see. You mean many widgets on a page.

Nothing. And js code not still loaded. site/get-images work well in a browser.

Not only that but also this list of plugins: https://github.com/vova07/yii2-imperavi-widget/tree/master/src/assets/plugins which I should handle also in case I do that for at least of of them.
Moreover you have a code you use now which contain two times plugins the one is the for default redactor's plugins and the second for custom plugins, that also make a lot of problems, because we should not only check if there is a specific option enabled, but also we should check if there are not custom plugins, which may work with default settings, but doesn't require at all the default plugin script.
So doing that we add a lot of bugs in the widget.

@sergmoro1 I've just tested you config.
It works for me. I see the image and file managers.
Can you please provide a screenshot of your redactor toolbar ?

I found the problem.

<?= $form->field($model, 'content')->widget(Widget::className(), [
	'settings' => [
		'lang' => 'ru',
		'minHeight' => 200,
		'replaceDivs' => false,
		'fileUpload' => Url::to(['site/file-upload']),			
		'imageUpload' => Url::to(['site/image-upload']),
		'fileDelete' => Url::to(['site/delete-file']),
		'imageDelete' => Url::to(['site/delete-file']),
		'fileManagerJson' => Url::to(['site/get-files']),		
		'imageManagerJson' => Url::to(['site/get-images']),		
		'plugins' => [
			'clips',
			'fullscreen',
			'table',
		],
	],
            // Problem was here. 'plugins' was in a 'settings'
	'plugins' => [
		'filemanager' => 'vova07\imperavi\bundles\FileManagerAsset',              
		'imagemanager' => 'vova07\imperavi\bundles\ImageManagerAsset',              
	],
]); ?>

Thanks a lot for explanation and help.