Giter Site home page Giter Site logo

nova4-tinymceeditor's Introduction

Nova4 TinyMCE Editor

Logo Nova4 TinyMce

Latest Version on Packagist Code Style Analyze Maintainability Total Downloads License Mit

Introduction

Unleash creativity within Laravel Nova using the TinyMCE plugin, making content creation a breeze with its user-friendly and dynamic editing capabilities.

Features

  • 📷 Upload images support (BETA)
  • 🌙 Dark mode support
  • 🔀 Switch between 5 or 6 versions of TinyMCE
  • ❌ Can be disabled (by passing readonly() to make method)

Extra

Important

Want some steroids for your TinyMCE? Check out our new * ChatGTP for TinyMCE* plugin! 🚀🚀🚀

Demo & Screenshots

Demo Nova4 TinyMce

Versioning

This package follows the following versioning scheme:

  • v1.x - TinyMCE 5 or 6
  • v0.x - TinyMCE version 5 (deprecated)

Prerequisites

  • Laravel >= 9
  • PHP >= 8.0
  • Laravel Nova >= 4
  • TinyMCE API Key (get one here)

How to install

In the root of your Laravel installation launch:

composer require murdercode/nova4-tinymce-editor

Then publish the config:

php artisan vendor:publish --provider="Murdercode\TinymceEditor\FieldServiceProvider"

A file in config/nova_tinymce_editor.php will appear as follows (you can change the default values):

<?php

return [
    'cloudChannel' => '6', // 5 or 6

    /**
     * Get your API key at https://www.tiny.cloud and put it here or in your .env file
     */
    'apiKey' => env('TINYMCE_API_KEY', ''),

    /**
     * The default skin to use.
     */
    'skin' => 'oxide-dark',

    /**
     * The default options to send to the editor.
     * See https://www.tiny.cloud/docs/configure/ for all available options (check for 5 or 6 version).
     */
    'init' => [
        'menubar' => false,
        'autoresize_bottom_margin' => 40,
        'branding' => false,
        'image_caption' => true,
        'paste_as_text' => true,
        'autosave_interval' => '20s',
        'autosave_retention' => '30m',
        'browser_spellcheck' => true,
        'contextmenu' => false,
        //'images_upload_url' => '/nova-vendor/murdercode/tinymce/upload', // Uncomment to enable image upload
    ],
    'plugins' => [
        'advlist',
        'anchor',
        'autolink',
        'autosave',
        'fullscreen',
        'lists',
        'link',
        'image',
        'media',
        'table',
        'code',
        'wordcount',
        'autoresize',
    ],
    'toolbar' => [
        'undo redo restoredraft | h2 h3 h4 |
                 bold italic underline strikethrough blockquote removeformat |
                 align bullist numlist outdent indent | link anchor table | code fullscreen spoiler',
    ],

    /**
     * Extra configurations for the editor.
     */
    'extra' => [
        'upload_images' => [
            'enabled' => false, // Uncomment to enable
            'folder' => 'images',
            'maxSize' => 2048, // KB
            'disk' => 'public',
        ],
    ],
];

In your .env file please add the key (get one here):

TINYMCE_API_KEY=[YOUR_PRECIOUS_PRIVATE_KEY]

Please make sure that you have added domain in your tiny.cloud account list or you will get an error notice message.

Register the Field

In your Nova/Resource.php add the field as following:

<?php

use Murdercode\TinymceEditor\TinymceEditor;

class Article extends Resource
{
    //...
    public function fields(NovaRequest $request)
    {
        return [
            TinymceEditor::make(__('Content'), 'content')
                ->rules(['required', 'min:20'])
                ->fullWidth()
                ->help(__('The content of the article.')),
        ];
    }
}
                //...

Enable Image Upload

Demo Nova4 TinyMce

Warning

This feature is in BETA and can be unstable or contain bugs/security flaws. We provide it as is, without any warranty. For this reason, is disabled by default.

To enable image upload, you must publish the configuration file with:

php artisan vendor:publish --provider="Murdercode\TinymceEditor\FieldServiceProvider"

then in your config file config/nova-tinymce-editor.php:

<?php

// Uncomment the following line
'images_upload_url' => '/nova-vendor/murdercode/tinymce/upload',

// Set the following to true
    'extra' => [
        'upload_images' => [
            'enabled' => false, // Uncomment to enable
            'folder' => 'images',
            'maxSize' => 2048, // KB
            'disk' => 'public',
        ],
    ],

Please be sure that image plugin and toolbar button are enabled in your config file.

Protect code

You can to control what contents should be protected from editing while it gets passed into the editor. This is useful for example when you want to protect PHP code from been formatted.

To do this, you must publish the configuration file and add the following line:

<?php

return [
'init' => [
        // ... Your awesome init config ...
         /**
         * Set what content should be protected while editing
         * This should be a regular expression
         * E.g "/<\?php.*?\?>/g" - Protect PHP code from been formatted
         */
         'protect' => []
];
//...

Use Alternative CDN / Self Hosted scripts

TinyMCE allows you to use an alternative mirror for scripts. It will be useful if you want to use a non-cloud version (and avoid the new mechanism pricing of Tiny.cloud).

You can simply add in app/Providers/NovaServiceProvider.php:

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        parent::boot();

        // TinyMCE Mirror
        Nova::script('custom', 'https://cdn.jsdelivr.net/npm/tinymce@6/tinymce.min.js');

        // ...
    }
}

TinyMCE will automatic check if there's a script and I'll ignore his script from tiny cloud.

Upgrade from 1.0.x to 1.1.x

The transition to 1.1 involves the use of a new configuration layout compatible with the previous version.

However, if you want to use the new image upload and version change features, it is recommended that you make a new php artisan vendor:publish.

Upgrade from 0.x to 1.x

In composer.json change the version of the package to

"murdercode/nova4-tinymce-editor": "^1.0"

and run composer update.

Also, you must change the format of the plugin snippet in nova-tinymce-editor as follows:

0.x

'plugins' => [
            'anchor advlist autolink autoresize autosave code fullscreen link lists image imagetools media
            paste wordcount',
        ],

1.x

'plugins' => [
            'anchor',
            'advlist',
            // etc...
        ],

Feedback and Support

Test, PR (also of this doc) are welcome.

nova4-tinymceeditor's People

Contributors

ahmic avatar joemires avatar murdercode avatar oskar-mikael avatar wijaksanapanji avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

nova4-tinymceeditor's Issues

Always show on detail page

Hi

How to always show content on detail page?

UPD:

please, change TinymceEditor.php (I can't create pull request)

namespace Murdercode\TinymceEditor;

use Laravel\Nova\Fields\Field;
use Laravel\Nova\Fields\SupportsDependentFields;
use Laravel\Nova\Fields\Expandable;

class TinymceEditor extends Field
{
    use SupportsDependentFields;
    use Expandable;

    /**
     * The field's component.
     *
     * @var string
     */
    public $component = 'tinymce-editor';

    /**
     * Indicates if the element should be shown on the index view.
     *
     * @var bool
     */
    public $showOnIndex = false;

    public function __construct(string $name, $attribute = null, callable $resolveCallback = null)
    {
        parent::__construct($name, $attribute);
        $this->resolveCallback = $resolveCallback;

        $this->withMeta([
            'options' => config('nova-tinymce-editor', []),
        ]);
    }

    public function options($options)
    {
        $inlineOptions = $this->meta['options'] ?? [];

        return $this->withMeta([
            'options' => array_merge($inlineOptions, $options),
        ]);
    }

    /**
     * Prepare the element for JSON serialization.
     *
     * @return array
     */
    public function jsonSerialize(): array
    {
        return array_merge(parent::jsonSerialize(), [
            'shouldShow' => $this->shouldBeExpanded(),
        ]);
    }
}

Use specific filesystem storage disk for image upload

Hello and thank you for your awesome work on this package!

I just updated to the latest version in order to get the image upload feature on the editor. However, I don't know if it's possible to upload images to a specific disk defined in Laravel's config/filesystems.php?

For example, I'm working on a blog and would like to have my image hosted on my s3 bucket which is defined in my config/filesystems.php:

<?php

return [
    'disks' => [
        // ...
        
        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],
];

Is this possible yet? It would be very helpful as it would allow us to profit from CDN cache on the blog posts featuring images.

Thank you

DependsOn not work

Hello,

I have code:

Select::make('Template', 'template2')
                ->options([
                    '1' => 'Template 1',
                    '2' => 'Template 2',
                ]),

                TinymceEditor::make('Notes', 'notes')
                    ->fullWidth()
                    ->rules('required')
                    ->dependsOn('template2', function (TinymceEditor $field, NovaRequest $request, FormData $formData) {
                        $tpl = $formData->get('template');
                            $field->value = ('<h1>Template '.$tpl.'</h1>');
                    })

but changing Template does not result changing content of Tinymce editor. Instead of, dependsOn looks like run on refresh button.

Don't work with json fields :(

My code is :

  TinymceEditor::make('Content', 'data->main->text->en')
        ->fullWidth()
        
  or
  
  TinymceEditor::make('Text', 'data->text')
         ->fullWidth(),

Знімок екрана 2024-05-23 о 10 48 05

Support 3rd party Plugins

I wanted to add a third-party bootstrap plugin - TinyMCE Bootstrap Plugin

It required me to add a custom object in the editor options, but it's not getting initialized

 'bootstrapConfig' => [
                    'url' =>'bootstrap5/plugin/',
                    'iconFont' => 'fontawesome5',
                    'imagesPath' => '/assets/images',
                    'key' => config('services.tinymce_plugin_key')
                ],

Any ideas on how to add third party plugins, or any plans to include that in the documentation?

Javascript filtered

Hey
I noticed javascript codes are filtered when saving or rendering into the editor, how can I enable javascript usage in the editor

Styling output

Hi

any information how to styling output content?

For example I add codesample plugin and in editor I see (code type php)

iScreen Shoter - Google Chrome - 230620170746

but in detail page I see another style

iScreen Shoter - Google Chrome - 230620170832

Add new config

Hello Sir.
Thank you for your package
I need to add an extra config for the nova-tinymce-editor.php one of them is smart_paste
I need to prevent paste images in the editor.
Any advice, please?

Add extra paragraph when displaying content

Hi,
I ran into some problem when I added some simple text to the TinymceEditor field on Laravel Nova.
I added this text:

<p>Row one</p>
<p>&nbsp;</p>
<p>Row two</p>

So i pushed an enter between the 2 row, I checked the database and it saved like this there.
When I open up the item for editing, then it will return this for me:

<p>Row one</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Row two</p>

Seems for some reason added 2 more line break. After I hit the save button again in the database I will get this result:

<p>Row one</p>
<br/>
<p>&nbsp;</p>
<br/>
<p>Row two</p>

I tried inserting to the config file something like these:
'forced_root_block' => false,
'force_p_newlines' => false,
'force_br_newlines' => false,

None of them worked, could you give some explanation about this case?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.