Code Monkey home page Code Monkey logo

yii2-content-tools's Introduction

yii2-content-tools

Latest Stable Version Total Downloads License

ContentTools editor implementation for Yii 2.

ContentTools

Check out ContentTools website http://getcontenttools.com for more information about the editor.

Installation

Add the package to your composer.json:

{
    "require": {
        "bizley/contenttools": "^1.4"
    }
}

and run composer update or alternatively run composer require bizley/contenttools

Bower Asset

This package depends on Bower package fetched through https://asset-packagist.org/
Make sure you can fetch it by configuring your composer.json properly (see instructions at the link above).

Usage

1. The widget.

Wrap any part of the content in \bizley\contenttools\ContentTools::begin(); and \bizley\contenttools\ContentTools::end();.

<?php \bizley\contenttools\ContentTools::begin(); ?>
This is the part of view that is editable.
<p>There are paragraphs</p>
<div>and more...</div>
<?php \bizley\contenttools\ContentTools::end(); ?>

You can use the widget multiple times on one page.

2. Backend.

Yii 2 ContentTools saves content and uploaded images asynchronously and it requires some preparation on the backend side.

You have to create few controllers' actions:

  • "upload new image" action,
  • "rotate uploaded image" action,
  • "insert & crop uploaded image" action,
  • "save content" action.

Three first actions are already prepared if you don't want any special operations. You can find them in 'actions' folder.

  • UploadAction - takes care of validating the uploaded images using \bizley\contenttools\models\ImageForm (jpg, png and gif images are allowed, maximum width and height is 1000px and maximum size is 2 MB), images are saved in content-tools-uploads folder accessible from web.
  • RotateAction - takes care of rotating the uploaded image using Imagine library (through yii2-imagine extension required in the composer.json).
  • InsertAction - takes care of inserting image into the content with optional cropping using Imagine library.

The default option for the image URLs is:

'imagesEngine' => [
    'upload' => '/site/content-tools-image-upload',
    'rotate' => '/site/content-tools-image-rotate',
    'insert' => '/site/content-tools-image-insert',
],

So if you don't want to change the imagesEngine parameter add in your SiteController:

public function actions()
{
    return [
        'content-tools-image-upload' => \bizley\contenttools\actions\UploadAction::className(),
        'content-tools-image-insert' => \bizley\contenttools\actions\InsertAction::className(),
        'content-tools-image-rotate' => \bizley\contenttools\actions\RotateAction::className(),
    ];
}

See Standalone Actions section in Yii 2 Guide for more info about adding actions.

The last "save content" action is not prepared because it depends on the business logic of your application. See Saving content part at the end of this file.

Default configuration for this is:

'saveEngine' => [
    'save' => '/site/save-content',
],

Options

You can add options for the widget by passing the configuration array in the begin() method.

id

default: null
Identifier of the editable region (must be unique). If left empty it is automatically set to 'contentToolsXXX' where XXX is the number of next widget.

page

default: null
Page identifier. If null it is set to the current URL.

tag

default: 'div'
HTML tag that is used to wrap the editable content.

dataName

default: 'name'
Name of the data-* attribute that stores the identifier of editable region.

dataInit

default: 'editable'
Name of the data-* attribute that marks the region as editable.

options

default: []
Array of HTML options that are applied to editable region's tag.

imagesEngine

default:

[
    'upload' => '/site/content-tools-image-upload',
    'rotate' => '/site/content-tools-image-rotate',
    'insert' => '/site/content-tools-image-insert',
]

Array of the URLs of the image actions OR false to switch off the default image engine (you will have to prepare JS for handling images on your own).

saveEngine

default:

[
    'save' => '/site/save-content',
]

Array with the URL of the content saving action OR false to switch off the default saving engine (you will have to prepare JS for handling content saving on your own).

styles

default: []
Array of styles that can be applied to the edited content.
Every style should be added in separate array like:

'Name of the style' => [
    'class' => 'Name of the CSS class',
    'tags'  => [Array of the html tags this can be applied to] or 'comma-separated list of the html tags this can be applied to'
],

Example:

'Bootstrap Green' => [
    'class' => 'text-success',
    'tags'  => ['p', 'h2', 'h1']
],

tags key is optional and if omitted style can be applied to every element.

language

default: false
Boolean flag or language code of the widget translation.
You can find the list of prepared translations in @bower/contenttools/translations folder.
false means that widget will not be translated (default language is English).
true means that widget will be translated using the application language.
If this parameter is a string widget tries to load the translation file with the given name.
If it cannot be found and string is longer that 2 characters widget tries again this time with parameter shortened to 2 characters.
If again it cannot be found language sets back to default.

globalConfig

default: true
Boolean flag whether the configuration should be global.
Global configuration means that every succeeding widget ignores page, tag, dataName, dataInit, imagesEngine, saveEngine, and language parameters and sets them to be the same as in the first one. Also styles are added only if they've got unique names.

customJs

default: null
String with custom JS to be initialized with editor.
Use editor variable to point to instance of ContentTools.EditorApp.get().
See http://getcontenttools.com/api/content-tools for more details about CT API.

Actions callbacks

The default JS image callbacks assume the following action response:

{
    'size': [image-width-in-px, image-height-in-px],
    'url': image-url
}

with optional 'alt' for insert-action. In case of any errors response should be:

{
    'errors': [array-of-error-descriptions]
}

At the moment errors are only displayed in browser's console (user sees only the big transparent cross).

Saving content

Action responsible for saving the content should expect the array of every page region data in pairs 'region-identifier' => 'region-content'.

Typical structure could look like this:

[
    'contentTools0' => '...', // HTML content of the contentTools0 region
    'contentTools1' => '...', // HTML content of the contentTools1 region
    '_csrf' => '...', // CRSF token value
    'page' => '/site/index' // page identifier
]

Now you just need to validate and save the regions linked to the page with the given identifier.

Example implementation of saving content can be found in the https://github.com/bizley/yii2-content-tools/blob/master/example.php

yii2-content-tools's People

Contributors

dependabot-preview[bot] avatar jeffrico 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yii2-content-tools's Issues

Update to 1.0 Bower issues

Hi man,

Great extension!

I was updating the extension today and get following error:


  [Composer\Downloader\TransportException]                                     
  The "https://bower.herokuapp.com/packages/search/contenttools" file could n  
  ot be downloaded (HTTP/1.1 404 Not Found)   

Any suggestions?
Thanks again

Case sensitive directory name on linux systems

Invalid Argument โ€“ yii\base\InvalidArgumentException The file or directory to be published does not exist: /var/www/html/vendor/bower/contenttools/build
Directory name should be bower/ContentTools/build

Dependabot can't resolve your PHP dependency files

Dependabot can't resolve your PHP dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:

Your requirements could not be resolved to an installable set of packages.
  Problem 1
    - yiisoft/yii2 2.0.38 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.37 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.36 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.35 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.34 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.33 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.32 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.31 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.30 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.29 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.28 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.27 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.26 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.25 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.24 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.23 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.22 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.21 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.20 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.19 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.18 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.17 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.16.1 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.16 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.15.1 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.15 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.14.2 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.14.1 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.14 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.13.3 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.13.2 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.13.1 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - yiisoft/yii2 2.0.13 requires bower-asset/inputmask ~3.2.2 | ~3.3.5 -> no matching package found.
    - roave/security-advisories dev-master conflicts with yiisoft/yii2[2.0.10].
    - roave/security-advisories dev-master conflicts with yiisoft/yii2[2.0.11].
    - roave/security-advisories dev-master conflicts with yiisoft/yii2[2.0.11.1].
    - roave/security-advisories dev-master conflicts with yiisoft/yii2[2.0.11.2].
    - roave/security-advisories dev-master conflicts with yiisoft/yii2[2.0.12].
    - roave/security-advisories dev-master conflicts with yiisoft/yii2[2.0.12.1].
    - roave/security-advisories dev-master conflicts with yiisoft/yii2[2.0.12.2].
    - roave/security-advisories dev-master conflicts with yiisoft/yii2[2.0.5].
    - roave/security-advisories dev-master conflicts with yiisoft/yii2[2.0.6].
    - roave/security-advisories dev-master conflicts with yiisoft/yii2[2.0.7].
    - roave/security-advisories dev-master conflicts with yiisoft/yii2[2.0.8].
    - roave/security-advisories dev-master conflicts with yiisoft/yii2[2.0.9].
    - Installation request for roave/security-advisories dev-master -> satisfiable by roave/security-advisories[dev-master].
    - Installation request for yiisoft/yii2 >=2.0.5 <2.1.0 -> satisfiable by yiisoft/yii2[2.0.10, 2.0.11, 2.0.11.1, 2.0.11.2, 2.0.12, 2.0.12.1, 2.0.12.2, 2.0.13, 2.0.13.1, 2.0.13.2, 2.0.13.3, 2.0.14, 2.0.14.1, 2.0.14.2, 2.0.15, 2.0.15.1, 2.0.16, 2.0.16.1, 2.0.17, 2.0.18, 2.0.19, 2.0.20, 2.0.21, 2.0.22, 2.0.23, 2.0.24, 2.0.25, 2.0.26, 2.0.27, 2.0.28, 2.0.29, 2.0.30, 2.0.31, 2.0.32, 2.0.33, 2.0.34, 2.0.35, 2.0.36, 2.0.37, 2.0.38, 2.0.5, 2.0.6, 2.0.7, 2.0.8, 2.0.9].

Potential causes:
 - A typo in the package name
 - The package is not available in a stable-enough version according to your minimum-stability setting
   see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details.
 - It's a private package and you forgot to add a custom repository to find it

Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.

If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

View the update logs.

Unable to upload image file using editor

    public function run()
    {
        if (!Yii::$app->request->isPost) {
            return Json::encode(['errors' => ['POST parameters are missing!']]);
        }
        try {
            $data = Yii::$app->request->post();
            if (empty($data['url'])) {
                throw new InvalidParamException('Parameter "url" is missing!');
            }
            $url = trim($data['url']);
            if (strpos($url, '/') === 0) {
                $url = substr($url, 1);
            }
            if (strpos($url, '?_ignore=') !== false) {
                $url = substr($url, 0, strpos($url, '?_ignore='));
            }
            $imageSizeInfo = @getimagesize($url);
            if ($imageSizeInfo === false) {
                throw new InvalidParamException('Parameter "url" seems to be invalid!');
            }
            if (!empty($data['crop'])) {
                $crop = explode(',', $data['crop']);
                if (count($crop) !== 4) {
                    throw new InvalidParamException('Parameter "crop" is invalid!');
                }
                $positions = [];
                foreach ($crop as $position) {
                    $position = trim($position);
                    if (!is_numeric($position) || $position < 0 || $position > 1) {
                        throw new InvalidParamException('Parameter "crop" contains invalid value!');
                    }
                    $positions[] = $position;
                }
                list($width, $height) = $imageSizeInfo;
                Image::crop(
                    $url,
                    floor($width * $positions[3] - $width * $positions[1]),
                    floor($height * $positions[2] - $height * $positions[0]),
                    [
                        floor($width * $positions[1]),
                        floor($height * $positions[0])
                    ]
                )->save($url);
            }
            return Json::encode([
                'size' => @getimagesize($url),
                'url' => '/' . $url,
                'alt' => basename($url)
            ]);
        } catch (Exception $e) {
            return Json::encode(['errors' => [$e->getMessage()]]);
        }
    }

Error ```
$imageSizeInfo = @getimagesize($url);
if ($imageSizeInfo === false) {
throw new InvalidParamException('Parameter "url" seems to be invalid!');
}

image preview not working

Image preview inside upload modal form not working, it triggered "Image URL seems to be invalid!" error message, but file uploaded to folder successfully.

my workaround is add few code (inside your UploadAction.php)

if ($model->validate()) {
if ($model->upload()) {
$image_path = Yii::getAlias('@webroot') . $model->url;
$imageSizeInfo = @getimagesize($image_path);

//$imageSizeInfo = @getimagesize($model->url);

and now it works :)

bizley/contenttools 1.5.0 requires bower-asset/contenttools ^1.0 -> no matching package found.


Using version ^1.5 for bizley/contenttools
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for bizley/contenttools ^1.5 -> satisfiable by bizley/contenttools[1.5.0].
    - bizley/contenttools 1.5.0 requires bower-asset/contenttools ^1.0 -> no matching package found.

Potential causes:
 - A typo in the package name
 - The package is not available in a stable-enough version according to your minimum-stability setting
   see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details.
 - It's a private package and you forgot to add a custom repository to find it

Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.

bizley/contenttools 1.5.0 requires bower-asset/contenttools ^1.0 -> no matching package found.

Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for bizley/contenttools ^1.5 -> satisfiable by bizley/contenttools[1.5.0].
- bizley/contenttools 1.5.0 requires bower-asset/contenttools ^1.0 -> no matching package found.

Potential causes:

Read https://getcomposer.org/doc/articles/troubleshooting.md for further common problems.

Installation failed, reverting ./composer.json to its original content.

composer.json

"repositories": { "packagist": { "type": "composer", "url": "https://packagist.phpcomposer.com" } }

Problem with images

Hello I have some problems a doubts with images.

1. Need a div with a class
I have an image that will be replace with other javascript in background.
if i do this, with the div before code:

<div class="background-image-holder">
   <?php \bizley\contenttools\ContentTools::begin(); ?>
      <img class="nsp" alt="image" src="/img/Ethiopia-National-Geographic.jpg">
   <?php \bizley\contenttools\ContentTools::end(); ?>
</div>

the image will be not appears, other way if i will replace like this, with the internal div:

<?php \bizley\contenttools\ContentTools::begin(); ?>
   <div class="background-image-holder">
      <img class="nsp" alt="image" src="/img/Ethiopia-National-Geographic.jpg">
   </div>
<?php \bizley\contenttools\ContentTools::end(); ?>

the image will be appear but not editable

I read in doc maybe the solution is by some options, like tag or style but i am not sure and i don't know how to use.
I try it without success

2 The normal images
with the normal images

<?php \bizley\contenttools\ContentTools::begin(); ?>
   <img class="nsp" alt="Pic" src="/img/men.jpg"> 
<?php \bizley\contenttools\ContentTools::end(); ?>

if i press edit button, that will take a different dimension, maybe the original one.. the script put in html different dimensions and is not nice to see.
it is possible not change the dimensions taking those of the website?
maybe directly javascript file, but will be better if there is some solution by php

edit:
For this problem I found a solution by javascript.
in content-tools.js line 4059

if (this._attributes['width']) {
// Changed this line from this._attributes['width'] to 100% fixed
   style += "width:100%;";
}
if (this._attributes['height']) {
   style += "height:" + this._attributes['height'] + "px;";
}

thanks
Gabriele

saveEngine

Hello, in my SiteController I put the new save path but is not working

/**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'content-tools-image-upload' => \bizley\contenttools\actions\UploadAction::className(),
            'content-tools-image-insert' => \bizley\contenttools\actions\InsertAction::className(),
            'content-tools-image-rotate' => \bizley\contenttools\actions\RotateAction::className(),
            'saveEngine' => [
                'save' => '/site/savecontent',
            ],
        ];
    }`
it is correct ?

Editable Toolbox?

Hi! How about edit/change toolbox? I want to remove some buttons.

It can be something like this: editor.toolbox().tools({$this->toolbox});
in /src/ContentTools.php:307 InitEditor method?

multi language content

is it possible to create content with Yii::t in the views and edit it?
for example this:
Yii::t('app',editable content)
or add sth in editor which we can capable to add multi language content?

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.