All Projects → VanOns → Laraberg

VanOns / Laraberg

Licence: gpl-3.0
A Gutenberg implementation for Laravel

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Laraberg

Chatify
A Laravel package that allows you to add a complete user messaging system into your new/existing Laravel application.
Stars: ✭ 885 (-6.74%)
Mutual labels:  laravel
Doorman
Limit access to your Laravel applications by using invite codes
Stars: ✭ 913 (-3.79%)
Mutual labels:  laravel
Laravel Dev Booter
🚧 Boost your Laravel app by registering Prod services only on Prod.
Stars: ✭ 28 (-97.05%)
Mutual labels:  laravel
Laravel Tabler
Laravel Package for integrating Tabler template and this package is Laravel Mix friendly.
Stars: ✭ 20 (-97.89%)
Mutual labels:  laravel
Pusher Chatkit Laravel
Laravel wrapper for the Chatkit PHP SDK. ChatKit is shutting down 😕 - https://blog.pusher.com/narrowing-our-product-focus
Stars: ✭ 21 (-97.79%)
Mutual labels:  laravel
Laravel Nuxt
A Laravel-Nuxt starter kit.
Stars: ✭ 943 (-0.63%)
Mutual labels:  laravel
Freshdesk Laravel
Freshdesk Service Provider for Laravel 5 and Lumen
Stars: ✭ 14 (-98.52%)
Mutual labels:  laravel
Laravel Vue Favorite
Create a favorite/like/recommend component with Laravel and Vuejs.
Stars: ✭ 29 (-96.94%)
Mutual labels:  laravel
Orderable
My very first Laravel package
Stars: ✭ 21 (-97.79%)
Mutual labels:  laravel
Laravel Sftp
SFTP filesystem service provider for Laravel
Stars: ✭ 28 (-97.05%)
Mutual labels:  laravel
Easyhttp
A Laravel HTTP-client to make HTTP request easier and log requests and responses
Stars: ✭ 20 (-97.89%)
Mutual labels:  laravel
Laravel Scrum Dev Paas
Laravel后端服务,提供团队协作管理平台的后端业务逻辑
Stars: ✭ 21 (-97.79%)
Mutual labels:  laravel
Eloquent Driver
A package that allows you to store Statamic entries in a database.
Stars: ✭ 28 (-97.05%)
Mutual labels:  laravel
Laravel Scout Elastic
Elastic Driver for Laravel Scout
Stars: ✭ 886 (-6.64%)
Mutual labels:  laravel
Gorose
GoRose(go orm), a mini database ORM for golang, which inspired by the famous php framwork laravle's eloquent. It will be friendly for php developer and python or ruby developer. Currently provides six major database drivers: mysql,sqlite3,postgres,oracle,mssql, Clickhouse.
Stars: ✭ 947 (-0.21%)
Mutual labels:  laravel
Phperinterviewguide
相对于 PHPer 新手而言比较有指导意义的 PHPer 面试指南
Stars: ✭ 886 (-6.64%)
Mutual labels:  laravel
Dt54
Laravel 5.4 DataTables Demo Application (WIP)
Stars: ✭ 27 (-97.15%)
Mutual labels:  laravel
Laradown
Markdown parser for Laravel built on parsedown
Stars: ✭ 29 (-96.94%)
Mutual labels:  laravel
Base
Multilingual CMS built with Laravel.
Stars: ✭ 949 (+0%)
Mutual labels:  laravel
Larawiz
Larawiz is a easy project scaffolder for Laravel
Stars: ✭ 28 (-97.05%)
Mutual labels:  laravel

Latest Version License Gitter

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. A demo can be found at demo.laraberg.io. If you would like to see an example of how to implement Laraberg you can take a look at the code from the demo over here.

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"

In order to store the data for the Gutenberg editor, Laraberg needs to run a database migration:

php artisan migrate

This will create the 'lb_contents' and 'lb_blocks' tables.

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->getRawContent()}}</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 insde 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]')

Using the Editor Wihout a Form

If you want to use the editor, but for some reason do not want to deal with submitting forms there is a way to get the content from the editor through JavaScript:

let content = Laraberg.getContent()

Setting the editor content

It's possible to set the the editor's content using JavaScript:

Laraberg.setContent('content')

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 can contain the following optional keys:

Key Type Description
sidebar Boolean Enables the Laraberg sidebar if true
prefix String The API prefix to use for requests (only use this if you changed the API location manually)
laravelFilemanager Bool/Object Enables Laravel Filemanager for fileuploads if value is truth. Can be an object that contains configuration options. See Laravel File Manager.
sidebar Boolean Enables the Laraberg sidebar if true
searchCb Function Will be called when using certain search fields within Gutenberg. See Search Callback.
height String Sets the height of the editor. Value must be a valid css height value (e.g. '10px', '50%', '100vh').
minHeight String Sets the minHeight of the editor. Value must be a valid css min-height value (e.g. '10px', '50%', '100vh').
maxHeight String Sets the maxHeight of the editor. Value must be a valid css max-height value (e.g. '10px', '50%', '100vh').

Models

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

use VanOns\Laraberg\Models\Gutenbergable;

class MyModel extends Model {
  use Gutenbergable;
}

This adds multiple attributes to your model that will help you with creating/updating/rendering the Gutenberg content.

$content // This is the raw content from the Gutenberg editor
$model = new MyModel;

// Add or update the content
$model->lb_content = $content;

// Save the model
$model->save();

// Get the rendered HTML
$model->lb_content;

// Get the raw Gutenberg output, this should be in the target textarea when updating content
$model->lb_raw_content;

Note: The old renderContent, getRawContent, getRenderedContent and setContent methods from v0.0.4-beta and lower are deprecated and will be removed in a future release.

Renaming Gutenbergable method names

There is always the possibility that your model already implements a method with the same name as one of the Gutenbergable methods. Luckily PHP Traits provide an easy way to rename the methods from a trait:

class MyModel extends Model {
  use Gutenbergable {
    renderContent as renderLBContent;
  }

  public function renderContent() {
    // Your method
  }
}

In this example you can just call the 'renderLBContent' method to render the content.

Rendering Gutenberg Content

Rendering the Gutenberg content is very simple and happens like this:

<div id="your_container"> <!-- The element you want to render the content in -->
  {!! $page->lb_content !!}
</div>

Keep in mind that in order to correctly display some of the Wordpress styling the Laraberg CSS has to be present on the page:

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

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 Blocks

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.registerBlock('my-namespace/my-block', myBlock)

Registering Categories

If you want to add your custom block to a new category you need to add that category first:

let title = 'My Category'
let slug = 'my-category'
Laraberg.registerCategory(title, slug)

Events

Laraberg implements Laravel events that you can use to implement your own listeners. The events contain a 'content' attribute that contains the relevant Content object. For information on how Laravel events work you can read the Laravel documentation.

  • VanOns\Laraberg\Events\ContentCreated
  • VanOns\Laraberg\Events\ContentRendered
  • VanOns\Laraberg\Events\ContentUpdated

Sidebar

Laraberg provides a way to put your form fields in a seperate sidebar in the Gutenberg editor. This way you can let the editor take the entire screen while still having a place for your form fields. This is done by putting the input element in a parent element with the .laraberg-sidebar class. This is currently supported for the following input types:

  • Checkbox
  • Radio
  • Select
  • Text
  • Textarea

For the labels Laraberg first checks if there is a label assigned to the element and will try to use that. If there is no label assigned it will check if there is a placeholder and use that as the label.

To enable the sidebar set the sidebar option to true when initializing Laraberg:

Laraberg.init('[id_here]', { sidebar: true })

Checkbox

<div class="laraberg-sidebar">
  ...
  <label for="article-public">Public</label>
  <input id="article-public" name="public" type="checkbox">
</div>

Radio

<div class="laraberg-sidebar">
  ...
  <label for="letter-a">A</label>
  <input type="radio" name="letter" id="letter-a" value="a" checked> 
  <label for="letter-b">B</label>
  <input type="radio" name="letter" id="letter-b" value="b"> 
  <label for="letter-c">C</label>
  <input type="radio" name="letter" id="letter-c" value="c">
</div>

Select

<div class="laraberg-sidebar">
  ...
  <label for="article-month">Month</label>
  <select id="article-month" name="month" class="uk-select uk-form-large ">
      <option value="1">January</option>
      <option value="2">February</option>
      <option value="3">March</option>
      <option value="4">April</option>
      ...
  </select>
</div>

Text

<div class="laraberg-sidebar">
  ...
  <input id="article-title" type="text" name="title" placeholder="Title" />
</div>

Textarea

<div class="laraberg-sidebar">
  ...
  <textarea name="excerpt" id="article-excerpt" placeholder="Excerpt"></textarea>
</div>

Configuration

When initializing the editor there are a number of configuration options you can provide. This is still a work in progress!

Styling

It is possible to set the height, maxHeight, and minHeight of the editor by providing the desired values in the options object:

Laraberg.init('[id_here]', { height: '500px' })

Laraberg.init('[id_here]', { maxHeight: '500px' })

Laraberg.init('[id_here]', { minHeight: '500px' })

Placeholder

You can change the default Gutenberg placeholder by adding a placeholder attribute to your textarea:

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

API Routes

After publishing the vendor files you can find the 'laraberg.php' file in your config folder. This file allows you to configure the API Routes. Here you can change the URL prefix and the middleware for the routes.

When you change the route prefix you also have to provide the prefix when you initialize the editor like this:

Laraberg.init('[id_here]', { prefix: '/[prefix_here]' })

If you wish to define the routes yourself you can do that by setting 'use_package_routes' to 'false' in the config. Then you can take the following routes and make changes as you see fit:

Route::group(['prefix' => 'laraberg', 'middleware' => ['web', 'auth']], function() {
  Route::apiResource('blocks', 'VanOns\Laraberg\Http\Controllers\BlockController');
  Route::get('oembed', 'VanOns\Laraberg\Http\Controllers\OEmbedController');
});

Laravel File Manager

Laraberg supports Laravel File Manager for uploading files. To enable uploading media through Laravel File Manager the laravelFilemanager field should be set to true. This will add a 'File Manager' button to the Gutenberg media blocks that will open Laravel File Manager for uploading and selecting media files.

Laraberg.init('[id_here]', { laravelFilemanager: true })

If you are not using the default routes for Laravel File Manager you can provide the location of your Laravel File Manager endpoints in the options object like this:

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

Note: Laraberg does not do any configuration on your Laravel File Manager setup. By default a lot of media filetypes can not be uploaded unless they are whitelisted in the Laravel File Manager configuration file. For more information on this you can check the Laravel File Manager documentation.

Search Callback

The button block has a field that searches for pages or aritcles. In order to use this functionality you can pass a callback function when initializing Laraberg. This callback functions will receive a search, perPage and type parameter. The callback should return an array of objects that contain a title and an URL or a promise that resolves to such an array.

let customSearch = (search, perPage, type) => {
  return [
    {
      title: 'Laraberg Demo',
      url: 'demo.laraberg.io'
    }
  ]
}

Laraberg.init('content', { searchCb: customSearch })

Missing Blocks

Since we have disabled direct file uploading, some of the media blocks require a media library to operate. This means that the following blocks will only be enabled when you're using Laravel Filemanager for now:

  • Cover
  • Gallery
  • Media & Text

Contributing

If you want to contribute to Laraberg checkout the CONTRIBUTING.md

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].