Giter Club home page Giter Club logo

laraberg's Introduction

Latest Version License

Laraberg aims to provide an easy way to integrate the Gutenberg editor with your Laravel projects. It takes the Gutenberg editor and adds all the communication and data it needs function in a Laravel environment.

Table of Contents

Installation

Install package using composer:

composer require van-ons/laraberg

Add vendor files to your project (CSS, JS & Config):

php artisan vendor:publish --provider="VanOns\Laraberg\LarabergServiceProvider"

JavaScript and CSS files

The package provides a JS and CSS file that should be present on the page you want to use the editor on:

<link rel="stylesheet" href="{{asset('vendor/laraberg/css/laraberg.css')}}">

<script src="{{ asset('vendor/laraberg/js/laraberg.js') }}"></script>

Dependencies

The Gutenberg editor expects React, ReactDOM, Moment and JQuery to be in the environment it runs in. An easy way to do this would be to add the following lines to your page:

<script src="https://unpkg.com/[email protected]/umd/react.production.min.js"></script>

<script src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script>

Updating

When updating Laraberg you have to publish the vendor files again by running this command:

php artisan vendor:publish --provider="VanOns\Laraberg\LarabergServiceProvider" --tag="public" --force

Usage

Initializing the Editor

The Gutenberg editor should replace an existing textarea in a form. On submit the raw content from the editor will be put in the 'value' attribute of this textarea.

<textarea id="[id_here]" name="[name_here]" hidden></textarea>

In order to edit content on an already existing model we have to set the value of the textarea to the raw content that the Gutenberg editor provided.

<textarea id="[id_here]" name="[name_here]" hidden>{{ $model->content }}</textarea>

To initialize the editor all we have to do is call the initialize function with the id of the textarea. You probably want to do this inside a DOMContentLoaded event.

And that's it! The editor will replace the textarea in the DOM and on a form submit the editor content will be available in the textarea's value attribute.

Laraberg.init('[id_here]')

Configuration options

The init() function takes an optional configuration object which can be used to change Laraberg's behaviour in some ways.

const options = {}
Laraberg.init('[id_here]', options)

The options object should be a EditorSettings object.

interface EditorSettings {
    height?: string;
    mediaUpload?: (upload: MediaUpload) => void;
    fetchHandler?: FetchHandler;
    disabledCoreBlocks?: string[];
    alignWide?: boolean;
    supportsLayout?: boolean;
    maxWidth?: number;
    imageEditing?: boolean;
    colors?: Color[];
    gradients?: Gradient[];
    fontSizes?: FontSize[];
}

Models

In order to add the editor content to a model Laraberg provides the 'RendersContent' trait.

use VanOns\Laraberg\Traits\RendersContent;

class MyModel extends Model {
  use RendersContent;
}

This adds the render method to your model which takes care of rendering the raw editor content. By default the render methods renders the content in the content column, the column can be changed by changing the $contentColumn property on your model to the column that you want to use instead.

use VanOns\Laraberg\Traits\RendersContent;

class MyModel extends Model {
  use RendersContent;

  protected $contentColumn = 'my_column';
}

Or by passing the column name to the render method.

$model->render('my_column');

Custom Blocks

Gutenberg allows developers to create custom blocks. For information on how to create a custom block you should read the Gutenberg documentation.

Registering custom blocks is fairly easy. A Gutenberg block requires the properties title, icon, and categories. It also needs to implement the functions edit() and save().

const myBlock =  {
  title: 'My First Block!',
  icon: 'universal-access-alt',
  category: 'my-category',

  edit() {
    return <h1>Hello editor.</h1>
  },

  save() {
    return <h1>Hello saved content.</h1>
  }
}

Laraberg.registerBlockType('my-namespace/my-block', myBlock)

Server-side blocks

Server-side blocks can be registered in Laravel. You probably want to create a ServiceProvider and register your server-side blocks in it's boot method.

class BlockServiceProvider extends ServiceProvider {
    public function boot() {
        Laraberg::registerBlockType(
          'my-namespace/my-block',
          [],
          function ($attributes, $content) {
            return view('blocks.my-block', compact('attributes', 'content'));
          }
        );
    }
}

WordPress exports

Laraberg uses the WordPress Gutenberg packages under the hood, a lot of those packages expose functionality that let's you customize the editor. You can find these packages in Javascript in the global Laraberg object.

  • Laraberg.wordpress.blockEditor
  • Laraberg.wordpress.blocks
  • Laraberg.wordpress.components
  • Laraberg.wordpress.data
  • Laraberg.wordpress.element
  • Laraberg.wordpress.hooks
  • Laraberg.wordpress.serverSideRender

laraberg's People

Contributors

alexvanderbist avatar arman-arif avatar dnwjn avatar israelortuno avatar mauricewijnia avatar mikebronner avatar shaunluedeke avatar szepeviktor avatar winkelco avatar

Stargazers

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

Watchers

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

laraberg's Issues

Tools > Manage All Reusable Blocks doesn't work

Clicking the 3 dots menu > Tools > Manage All Reusable Blocks the following query string is appended to the current url "?post_type=wp_block"

Is it somehow "expected" or can it be filed as a bug ?
Tnx

Error When Toggling Custom Blocks

I'm getting the following error when click Options > Custom Fields:

TypeError: null is not an object (evaluating 'document.getElementById("toggle-custom-fields-form").submit')

Is this something on my end, or is that part not implemented just yet?

Thanks! :)

Translations

I've enabled translation but it seems that some part are not taken into consideration.
Especially block name.

Since I've found this : WordPress/gutenberg#5410
I guess this is not really a laraberg bug, but I suppose it's more related to gutenberg.js
Do you know on which version it is based on ?

Maybe someone already faced it and knows what to do.

Suggestion :) maybe an option for locale code set could be added to the init function

Unauthenticated exception

Seems like I am doing something wrong and it would be of real help if someone could point me out to my mistake.

I have an custom auth guard:
'admin' => [ 'driver' => 'session', 'provider' => 'admins', ],

Which I am using in the laraberg:
'use_package_routes' => true,
'middlewares' => ['web', 'auth:admin'],
'prefix' => 'laraberg'

Thing is, everything works like a charm but when I enter debug in VSCode and click on a block in laraberg I get an error like this:
https://i.imgur.com/9Isprpx.jpg

It does not quite make a problem but still I don't like errors in my projects :D
Thank you.

Request: demo

Would be nice to have a demo page. Yes, I know that you can check out Gutenberg on Wordpress but personally I would like to see a Laravel powered demo.

Goed werk mannen!

Custom blocks

Hello folks, first of all THANK YOU for porting the only good thing from wordpress into our favorite framework :D

I am still testing laraberg, trying yo figure out my way through it. I managed to create a custom block but there is no any wiki info on the fields for custom block. I see that it is referring to gutemberg wiki but I quite can't figure out how should I write it. In gutemberg we use ACF to make the fields but what should we do with laraberg? Can we get a little more details about this?

Thank you!

Prevent saving content in lb_contents if parent model was not saved

As of now, whenever I update my model attribute lbContent, the content in the lb_contents table is saved, even if I didn't save the parent model. I consider this a bug.

The expected behaviour should be to update the lb_contents content once the primary model has been saved (created or updated). Same happens when deleting a model, it's bound to the deleting event, and if for some reason, other event cancels the deletion, the lb_content content will be lost.

I would like to know if this was intentional for some reason and if you would consider a PR to fix this issue and standardize the reactions to the events.

Initialization of packages

Describe the bug
Webpack throwing errors when I'm trying to run npm run watch

To Reproduce
Steps to reproduce the behavior:

  1. Clone or download rep in any folder.
  2. Open the terminal and run npm i
  3. then npm run watch
  4. Get errors because of unknown Gutenberg packages.

Expected behavior
Run npm run watch without errors

Screenshots
One of the erorrs:
Снимок экрана 2019-08-07 в 16 44 11

Environment:

  • Laraberg Version: 1.0.0-rc.1
  • OS: macOS Mojave 10.14.5
  • Browser: –
  • Version: –

Using Laraberg in Vue

When using Laraberg in Vue, I have the problem that each time I run init for a different field without having a completely new response, I get about 59 JavaScript errors, saying that the blocks are already registered:

Block "core/paragraph" is already registered.

Is there any way to re-init Laraberg with our doing a page refresh?

Thanks!

Click on element after unselect make it disappear

Describe the bug
Adding an image on the block image, then clicking on the "Insert from URL" after closing it a first time close the block until you click again on the place it's supposed to be.

To Reproduce
Steps to reproduce the behavior:

  1. Go to the insert post
  2. Click on insert image
  3. Click on "Insert From Url"
  4. Click outside
  5. Try to click on "Insert From Url again"
  6. See error

Expected behavior
Close the block (or is hidden ?).

Screenshots
If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • Laraberg Version:
  • OS: macOS
  • Browser : Chrome
  • Version @dev

Additional context
Got some errors :
Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of PopoverDetectOutside which is inside StrictMode. Instead, add a ref directly to the element you want to reference.

Block "Gallery", "Cover" and "Media & Text" and laravel-filemanager bug

When using laravel-filemanager and laraberg you cannot use files from the filemanager for blocks "Gallery", "Cover" and "Media & Text" because when inserting media, you try to find the element with class ".editor-media-placeholder__url-input-container" and this element don't exists for those blocks.

Mabye this is not implemented yet but just wanted to report it.

Sidebar should be custimizable

At the moment the sidebar can be either enabled or disabled.
As I add fields to the sidebar, I want the panel title to reflect the purpose. Also if someone clicks the Unpin from toolbar the sidebar disappears for good. The only way to get it back is to clear the localStorage.

There should be a way to

  • Change title of the sidebar panel
  • toggle back the sidebar
  • disable/hide the Unpin from toolbar

Edit Existing Model

I add Laraberg to an existing model. when i edit the model i receive this error :

Trying to get property 'raw_content' of non-object

i think because there is no relation between my model and the Content model.

So i create a simple function

public function hasContent()
{
return $this->content ? true : false;
}

and to edit :

<textarea class="form-control" id="description" name="description" rows="30" hidden>{{ $region->hasContent() ? $region->lb_raw_content : null }}</textarea>

Save content in a custom db column

My reusable and embedded block appears not correctly on the show site.

This is the code for saveing in db:

public function update( Request $request, $id ) {

    $validatedData = $request->validate( [
        'page_title' => 'required|max:255',
        'image'      => 'image|mimes:jpeg,png,jpg,svg|max:2048'
    ] );

    $page               = Page::findOrFail( $id );
    $page->page_title   = $request->page_title;
    $page->page_content = $request->page_content;

    $page->save();

    return redirect()->route( 'pages.edit', $page->id );
}

I do not have not the same database table and column .
What can I do to make it work properly?

My reusable blocks stores correctly and the table (lb_blocks) is present in the database.

Document Available Init Options

I would love to learn more about the available init options. Could you document what they are, or where to learn more about them? Thanks! :)

Conflicts with Laravel Nova's DateTimePicker

I'm attempting to update nova-laraberg to the new 1.x version. I'm running into a problem where <script src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script> is breaking Nova's use of moment-timezone.

I have commented out the other 2 dependencies to make sure this is the actual culprit. Any suggestions as to how to implement it -- is there a no-conflict mode?

Thanks!

Vue.js support

First of all, thank you for this project!

Do you have any plans to support Vue.js? As I can see, Gutenberg closely related to React.

Mass assignment functionality

Is your feature request related to a problem? Please describe.
With this package spatie/laravel-tags, you can assign an array of tags in the model like this
$newsItem = NewsItem::create([
'name' => 'testModel',
'tags' => ['tag', 'tag2'], //tags will be created if they don't exist
]);
It will be a lot more convenient to have this kind of functionality built within.

Describe the solution you'd like
Allow mass assignment with Laraberg.

Getting "Your site doesn't include support for the block" Error

I'm getting this "Your site doesn't include support for the block" when I save the content.

Steps to reproduce the behaviour:

  1. Set the content to the model
$model->lb_content = request('body');
$model->save();
  1. View the content in the laraberg editor
<textarea id="laraberg">{{$model->lb_content}}</textarea>

Expected behaviour
The content should be saved without any extra markup.

Screenshots

Screenshot from 2019-08-30 14-15-07
Screenshot from 2019-08-30 14-15-15
Screenshot from 2019-08-30 14-15-23

Environment (please complete the following information):

  • Laraberg Version: dev-master (As of August 30, 2019)
  • OS: Ubuntu 19.04
  • Browser Firefox

laraberg Install problem

I encountered an error while installing into a new project. my version laravel 5.5.45, php version 7.0.33
`D:\laragon\www\Xb>composer require van-ons/laraberg

[InvalidArgumentException]
Could not find a version of package van-ons/laraberg matching your minimum-stability (stable). Require it with an e
xplicit version constraint allowing its desired stability.
require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-suggest] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--update-with-all-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--] []...
`

Translate Laraberg Manually

Hi!
I need to translate Laraberg for my project but i didn't find the strings ...where do you store strings

Remove mention of Wordpress in code

First, I want to say thanks from the entire October CMS community for your work.

I have a small question. Are you planning to remove the mention of "WP:" in the content code?

Image block broken if saved with out selected image

Describe the bug
If an image block without selected image is saved, the next time the editor is opened the block will be broken and unusable and even new image blocks will be unuseable.

Screenshot 2019-09-08 at 20 58 15
Screenshot 2019-09-08 at 21 01 40

While looking in the causing lines of code, it seem like Gutenberg tries to load the default wp media library

To Reproduce
Reproduce able in the demo
Steps to reproduce the behavior:

  1. Create Image block without image
  2. Save content
  3. Open editor
  4. See error

Expected behavior
The block should display the default empty state

Environment (please complete the following information):

  • Laraberg Version: 1.0.1
  • OS: macOS
  • Browser: Chrome, Safari

Routes

I noticed that the config allows the changing of the route path, but the routes are static within the JS (api-fetch.js).

Have forked for now.

migration


SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'json not null, raw_content text null, rendered_content text null, status v' at line 1 (SQL: create table lb_blocks (id int unsigned not null auto_increment primary key, title json not null, raw_content text null, rendered_content text null, status varchar(191) not null, slug varchar(191) not null, type varchar(191) not null default 'wp_block', created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

i used laravel 5.8 fresh app

os - ubuntu
php version : 7.2

php extention enabled

etc/php/7.2/cli/conf.d/10-opcache.ini,
/etc/php/7.2/cli/conf.d/10-pdo.ini,
/etc/php/7.2/cli/conf.d/15-xml.ini,
/etc/php/7.2/cli/conf.d/20-calendar.ini,
/etc/php/7.2/cli/conf.d/20-ctype.ini,
/etc/php/7.2/cli/conf.d/20-curl.ini,
/etc/php/7.2/cli/conf.d/20-dom.ini,
/etc/php/7.2/cli/conf.d/20-exif.ini,
/etc/php/7.2/cli/conf.d/20-fileinfo.ini,
/etc/php/7.2/cli/conf.d/20-ftp.ini,
/etc/php/7.2/cli/conf.d/20-gettext.ini,
/etc/php/7.2/cli/conf.d/20-iconv.ini,
/etc/php/7.2/cli/conf.d/20-json.ini,
/etc/php/7.2/cli/conf.d/20-mbstring.ini,
/etc/php/7.2/cli/conf.d/20-mysqli.ini,
/etc/php/7.2/cli/conf.d/20-pdo_mysql.ini,
/etc/php/7.2/cli/conf.d/20-phar.ini,
/etc/php/7.2/cli/conf.d/20-posix.ini,
/etc/php/7.2/cli/conf.d/20-readline.ini,
/etc/php/7.2/cli/conf.d/20-shmop.ini,
/etc/php/7.2/cli/conf.d/20-simplexml.ini,
/etc/php/7.2/cli/conf.d/20-sockets.ini,
/etc/php/7.2/cli/conf.d/20-sysvmsg.ini,
/etc/php/7.2/cli/conf.d/20-sysvsem.ini,
/etc/php/7.2/cli/conf.d/20-sysvshm.ini,
/etc/php/7.2/cli/conf.d/20-tokenizer.ini,
/etc/php/7.2/cli/conf.d/20-wddx.ini,
/etc/php/7.2/cli/conf.d/20-xmlreader.ini,
/etc/php/7.2/cli/conf.d/20-xmlwriter.ini,
/etc/php/7.2/cli/conf.d/20-xsl.ini

Object(...) is not a function init.js line 29

I get a javascript error when initializing the editor: Object(...) is not a function in init.js

To Reproduce
Steps to reproduce the behavior:

  1. Fresh install of Laraberg on Laravel 5.6 project, migrate and publish files
  2. Add dependencies
  3. Add textarea to view: <textarea id="laraberg_content" name="my_content"></textarea>
  4. Add init call: Laraberg.init('laraberg_content');

Expected behavior
I was expecting to see the editor :)

Screenshots
Screenshot 2019-07-31 at 14 35 40

Environment:

  • Laraberg Version: "van-ons/laraberg": "^1.0@RC"
  • OS: Mac OSX 10.14.5
  • Browser [ff, chrome, safari]
  • Version [ff: 68.0.1, Chrome: 76.0.3809.87, safari: 12.1.1

Additional context
I tried three times with different projects but get the same outcome. Maybe I just missed something :(

Not null violation: 7 ERROR: null value in column "contentable_id"

Hello
Im using postgreSql

MYcode ---

$rawContent = Contract::find(1);
if (empty($rawContent)) {
$contract = new Contract();
$contract->mycontent = "TestCon";
$contract->save();
$rawContent = Contract::find(1);
}

    $model = new Contract();
    $model->setContent($rawContent,true);

i have a problem while i am saving raw content throw below error
"
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "contentable_id" violates not-null constraint DETAIL: Failing row contains (9, null, null, App\Contract, null, page, 2019-04-30 09:45:19, 2019-04-30 09:45:19). (SQL: insert into "lb_contents" ("contentable_id", "contentable_type", "updated_at", "created_at") values (, App\Contract, 2019-04-30 09:45:19, 2019-04-30 09:45:19) returning "id")

"

help me

Drag'n'Drop doesn't work in "media" Block

Trying to drag an image, video or audio inside the block generate api error
"API handler not found. /wp/v2/media"

Blocks involved (tested)

  • Gallery
  • Image
  • Video
  • Audio
  • Cover
  • File

Gutenbergable trait error on table containing column called content

I've come across an issue whilst attempting to implement Laraberg in a Laravel Backpack project using their PageManager add-on (https://github.com/Laravel-Backpack/PageManager). I'd written an Observer on their 'pages' table so that when a page gets created or updated I could use the observer to trigger a lb_content call from the Gutenbergable trait to updated the Laraberg tables. Whenever this happened a call to a member function setContent() on string error gets thrown in the setLbContentAttribute method in the trait.

I tracked the issue down to the fact their pages table (on which I'd applied the Gutenbergable trait) contains a column called 'content' which overrides the $this->content in the method.

To Reproduce

  1. In a Laravel project, create a table containing a column called 'my_content'
  2. Create a Model for the table and apply the Gutenbergable trait to the model
  3. Create a new instance of the model, set the my_content field to contain some suitable content and save the instance
  4. Use lb_content from the trait to update the Laraberg tables
  5. Everything works as expected
  6. Rename the column from 'my_content' to 'content'
  7. Create a new instance of the model, set the content field to contain some suitable content and save the instance
  8. Use lb_content from the trait to update the Laraberg tables
  9. The following error gets thrown

laraberg_error

I know I can get round the issue by rewriting the PageMaker add-on to use a different column name, but given the usefulness of Laraberg in replacing other editors in other CMS style systems, it's possible that this issue might occur elsewhere.

Any thoughts as to another way to remedy the issue?

Filemanager

Hello there,
first thank you for integrating Gutenberg in Laravel. I got it up an running in no time.
Now I am trying to add Laravel File Manager, but without success. It opens the default URL no matter what I insert as prefix.

Laraberg.initGutenberg('[id_here]', { laravelFilemanager: { prefix: '/[lfm_prefix_here]' } })

Any advice?

Thank you in advanced

Input tag in Sidebar doesn't understand any type attribute except text

I want to add a field to the Laraberg sidebar, a select in this case.

Steps To Reproduce

  1. Add a textarea tag with an id attribute of body
<textarea id="body" name="body" rows="50" hidden></textarea>
  1. Initialize Laraberg with sidebar option set to true
document.addEventListener('DOMContentLoaded',  function () {
    	Laraberg.init('body',
        {
            minHeight: '500px',
            laravelFilemanager: true,
            sidebar: true,
	})
})
  1. Add input markup inside a div with class name laraberg-sidebar
  2. Chnage type from text to date
<div class="laraberg-sidebar">
  <input id="article-title" type="text" name="title" placeholder="Title" />
</div>
  1. Reload the page

Expected Behavior

The input date field should appear in the sidebar of Laraberg

Current Behavior

Nothing appears in the sidebar

Env/Version Info

Laravel: 5.8.*

Laraberg: 0.0.4@beta

OS: Ubuntu 19.04

Browsers: Firefox, Chrome

Add table doesn't work

Adding/creating table doesn't work
When i select table and click on create table nothing happened. Also no console error.
Hope you can fix it :))

version: "van-ons/laraberg": "^1.0",

image

image

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.