Skip to content

Commit

Permalink
Merge pull request #26 from jd-dotlogics/feature/styles-manger
Browse files Browse the repository at this point in the history
Feature/styles manger
  • Loading branch information
mjawad096 authored May 31, 2022
2 parents 6e82a2e + f8715f7 commit 780ad39
Show file tree
Hide file tree
Showing 19 changed files with 566 additions and 47 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 jd-dotlogics
Copyright (c) 2022 jd-dotlogics

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion dist/assets/editor.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/assets/editor.js

Large diffs are not rendered by default.

32 changes: 27 additions & 5 deletions src/App/Editor/Canvas.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,44 @@ class Canvas
public array $styles = [];
public array $scripts = [];

function __construct()
function __construct($styles = null, $scripts = null)
{
$this->styles = config('laravel-grapesjs.canvas.styles', []);
$this->scripts = config('laravel-grapesjs.canvas.scripts', []);
$this->initStylesAndScripts();

if(!empty($styles)){
$this->mergeStyles($styles);
}

if(!empty($scripts)){
$this->mergeScripts($scripts);
}
}

protected function mapScriptsUrls($urls)
{
$urls = collect($urls)->filter()->values()->toArray();

return array_map('url', $urls);
}

protected function initStylesAndScripts()
{
collect(['styles', 'scripts'])
->each(function($type){
$this->{$type} = $this->mapScriptsUrls(config("laravel-grapesjs.canvas.{$type}", []));
});
}

public function mergeStyles($styles)
{
$this->styles = array_merge($this->styles, $styles);
$this->styles = array_merge($this->styles, $this->mapScriptsUrls($styles));

return $this;
}

public function mergeScripts($scripts)
{
$this->scripts = array_merge($this->scripts, $scripts);
$this->scripts = array_merge($this->scripts, $this->mapScriptsUrls($scripts));

return $this;
}
Expand Down
11 changes: 6 additions & 5 deletions src/App/Editor/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class Config
// Management
public Canvas $canvas;
public PluginManager $pluginManager;
public ?StorageManager $storageManager;
public ?AssetManager $assetManager;
public StorageManager $storageManager;
public AssetManager $assetManager;
public StyleManager $styleManager;

function __construct(){
$this->exposeApi = config('laravel-grapesjs.expose_api', false);
Expand All @@ -38,15 +39,15 @@ public function initialize(Editable $editable)
$pluginManager = app(PluginManager::class, ['templates_url' => $editable->templates_url]);
$assetManager = app(AssetManager::class);
$storageManager = app(StorageManager::class, ['save_url' => $editable->store_url]);
$styleManager = app(StyleManager::class);

$canvas = app(Canvas::class)
->mergeStyles($editable->style_sheet_links)
->mergeScripts($editable->script_links);
$canvas = app(Canvas::class, ['styles' => $editable->style_sheet_links, 'scripts' => $editable->script_links]);

$this->pluginManager = $pluginManager;
$this->assetManager = $assetManager;
$this->canvas = $canvas;
$this->storageManager = $storageManager;
$this->styleManager = $styleManager;

$this->components = $editable->components;
$this->style = $editable->styles;
Expand Down
14 changes: 14 additions & 0 deletions src/App/Editor/StyleManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Dotlogics\Grapesjs\App\Editor;

class StyleManager
{
function __construct()
{
if(config('laravel-grapesjs.style_manager.limited_selectors', false)){
$this->sectors = [];
}

}
}
1 change: 1 addition & 0 deletions src/App/Http/Controllers/EditorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function templates(Request $request, $model, $id)

$content = view("laravel-grapesjs::{$view_base}{$file_name}")->render();

// dd($content);
$templates [] = [
'id' => $id_prefix . $fileInfo->getFilename(),
'category' => $category,
Expand Down
16 changes: 15 additions & 1 deletion src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
| Force Class
|--------------------------------------------------------------------------
|
| See https://github.com/artf/grapesjs/issues/546
| @See https://github.com/artf/grapesjs/issues/546
|
*/

Expand Down Expand Up @@ -80,6 +80,20 @@
'upload_url' => null,
],

/*
|--------------------------------------------------------------------------
| Style Manager
|--------------------------------------------------------------------------
|
| Enable/Disable selectors.
| @see https://grapesjs.com/docs/api/style_manager.html#stylemanager
|
*/

'style_manager' => [
'limited_selectors' => true,
],

/*
|--------------------------------------------------------------------------
| Storage Manager
Expand Down
3 changes: 3 additions & 0 deletions src/resources/js/gjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import BackButton from "./plugins/back-button"
import Templates from "./plugins/templates"
import CustomTypes from "./plugins/custom-types"
import DeviceButtons from './plugins/device-buttons'
import BackgroundImage from "./plugins/background-image"
import PluginsLoader from "./plugins/plugins-loader"

let config = window.editorConfig;
Expand Down Expand Up @@ -48,6 +49,7 @@ if(config.pluginManager.templates){
plugins = [
...plugins,
CustomFontFamily,
BackgroundImage,
Loader,
Notifications,
CustomTypes,
Expand All @@ -60,6 +62,7 @@ plugins = [

pluginsOpts = {
...pluginsOpts,
[BackgroundImage]: {},
[CustomFontFamily]: {fonts: config.pluginManager.customFonts},
[Loader]: {},
[Notifications]: {},
Expand Down
8 changes: 8 additions & 0 deletions src/resources/js/plugins/background-image/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
private/
/locale
node_modules/
*.log
_index.html
dist/
stats.json
7 changes: 7 additions & 0 deletions src/resources/js/plugins/background-image/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.*
*.log
*.html
**/tsconfig.json
**/webpack.config.js
node_modules
src
9 changes: 9 additions & 0 deletions src/resources/js/plugins/background-image/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2022-current Background Image

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
145 changes: 145 additions & 0 deletions src/resources/js/plugins/background-image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Background Image

[DEMO](##)
> **Provide a live demo of your plugin**
For a better user engagement create a simple live demo by using services like [JSFiddle](https://jsfiddle.net) [CodeSandbox](https://codesandbox.io) [CodePen](https://codepen.io) and link it here in your README (attaching a screenshot/gif will also be a plus).
To help you in this process here below you will find the necessary HTML/CSS/JS, so it just a matter of copy-pasting on some of those services. After that delete this part and update the link above

### HTML
```html
<link href="https://unpkg.com/grapesjs/dist/css/grapes.min.css" rel="stylesheet">
<script src="https://unpkg.com/grapesjs"></script>
<script src="https://unpkg.com/background-image"></script>

<div id="gjs"></div>
```

### JS
```js
const editor = grapesjs.init({
container: '#gjs',
height: '100%',
fromElement: true,
storageManager: false,
plugins: ['background-image'],
});
```

### CSS
```css
body, html {
margin: 0;
height: 100%;
}
```


## Summary

* Plugin name: `background-image`
* Components
* `component-id-1`
* `component-id-2`
* ...
* Blocks
* `block-id-1`
* `block-id-2`
* ...



## Options

| Option | Description | Default |
|-|-|-
| `option1` | Description option | `default value` |



## Download

* CDN
* `https://unpkg.com/background-image`
* NPM
* `npm i background-image`
* GIT
* `git clone https://github.com/YOUR-USERNAME/background-image.git`



## Usage

Directly in the browser
```html
<link href="https://unpkg.com/grapesjs/dist/css/grapes.min.css" rel="stylesheet"/>
<script src="https://unpkg.com/grapesjs"></script>
<script src="path/to/background-image.min.js"></script>

<div id="gjs"></div>

<script type="text/javascript">
var editor = grapesjs.init({
container: '#gjs',
// ...
plugins: ['background-image'],
pluginsOpts: {
'background-image': { /* options */ }
}
});
</script>
```

Modern javascript
```js
import grapesjs from 'grapesjs';
import plugin from 'background-image';
import 'grapesjs/dist/css/grapes.min.css';

const editor = grapesjs.init({
container : '#gjs',
// ...
plugins: [plugin],
pluginsOpts: {
[plugin]: { /* options */ }
}
// or
plugins: [
editor => plugin(editor, { /* options */ }),
],
});
```



## Development

Clone the repository

```sh
$ git clone https://github.com/YOUR-USERNAME/background-image.git
$ cd background-image
```

Install dependencies

```sh
$ npm i
```

Start the dev server

```sh
$ npm start
```

Build the source

```sh
$ npm run build
```



## License

MIT
18 changes: 18 additions & 0 deletions src/resources/js/plugins/background-image/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.jd-bg-settings {
.gjs-sm-property {
&:not(.gjs-sm-property--full){
width: 50%;
}

.background-image-preview {
.gjs-sm-preview-file-cnt{
height: 180px;
}
}

[type=color]{
height: 50px;
padding-right: 5px;
}
}
}
23 changes: 23 additions & 0 deletions src/resources/js/plugins/background-image/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "background-image",
"version": "1.0.0",
"description": "Background Image",
"main": "src/index.js",
"repository": {
"type": "git",
"url": "https://github.com/YOUR-USERNAME/background-image.git"
},
"scripts": {
"start": "grapesjs-cli serve",
"build": "grapesjs-cli build",
"bump": "npm version patch -m 'Bump v%s'"
},
"keywords": [
"grapesjs",
"plugin"
],
"devDependencies": {
"grapesjs-cli": "^3.0.0"
},
"license": "MIT"
}
Loading

0 comments on commit 780ad39

Please sign in to comment.