Skip to content

Commit

Permalink
Merge pull request #35 from jd-dotlogics/feature/linkable-image
Browse files Browse the repository at this point in the history
Feature/linkable image
  • Loading branch information
mjawad096 authored Jun 13, 2022
2 parents 6928fc5 + 1630110 commit 4bfe7a7
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dist/assets/editor.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/resources/js/gjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import DeviceButtons from './plugins/device-buttons'
import BackgroundImage from "./plugins/background-image"
import PluginsLoader from "./plugins/plugins-loader"
import StyleEditor from "./plugins/style-editor"
import LinkableImage from "./plugins/linkable-image"

let config = window.editorConfig;
delete window.editorConfig;
Expand Down Expand Up @@ -60,6 +61,7 @@ plugins = [
DeviceButtons,
PluginsLoader,
StyleEditor,
LinkableImage,
]

pluginsOpts = {
Expand All @@ -75,6 +77,7 @@ pluginsOpts = {
[DeviceButtons]: {},
[PluginsLoader]: config.pluginManager.pluginsLoader,
[StyleEditor]: {},
[LinkableImage]: {},
};

config.plugins = plugins
Expand Down
2 changes: 1 addition & 1 deletion src/resources/js/plugins/background-image/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export default (editor, opts = {}) => {
const commandExists = toolbar.some(item => item.command === COMMAND_ID);

// if it doesn't already exist, add it
if (!commandExists && !component.is('image')) {
if (!commandExists && !component.is('image') && component.get('tagName') !== 'body') {
let tool = {
attributes: { 'class': TOOL_ICON },
command: COMMAND_ID
Expand Down
8 changes: 8 additions & 0 deletions src/resources/js/plugins/linkable-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/linkable-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/linkable-image/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2022-current Linkable 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/linkable-image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Linkable 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/linkable-image"></script>

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

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

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


## Summary

* Plugin name: `linkable-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/linkable-image`
* NPM
* `npm i linkable-image`
* GIT
* `git clone https://github.com/YOUR-USERNAME/linkable-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/linkable-image.min.js"></script>

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

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

Modern javascript
```js
import grapesjs from 'grapesjs';
import plugin from 'linkable-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/linkable-image.git
$ cd linkable-image
```

Install dependencies

```sh
$ npm i
```

Start the dev server

```sh
$ npm start
```

Build the source

```sh
$ npm run build
```



## License

MIT
23 changes: 23 additions & 0 deletions src/resources/js/plugins/linkable-image/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "linkable-image",
"version": "1.0.0",
"description": "Linkable Image",
"main": "src/index.js",
"repository": {
"type": "git",
"url": "https://github.com/YOUR-USERNAME/linkable-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"
}
82 changes: 82 additions & 0 deletions src/resources/js/plugins/linkable-image/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
export default (editor, opts = {}) => {
const LINK_COMMAND_ID = 'jd-add-link-image';
const UNLINK_COMMAND_ID = 'jd-remove-link-image';
const LINK_TOOL_ICON = 'fa fa-link';
const UNLINK_TOOL_ICON = 'fa fa-unlink';

editor.on('component:selected', () => {
const component = editor.getSelected();
if(!component) return;

const toolbar = component.get('toolbar');

if (component.is('image')) {
let command, icon;

if(component.closest('a') === 0){
icon = LINK_TOOL_ICON;
command = LINK_COMMAND_ID;
}else{
let parent = component.parent();
if(parent.get('tagName') == 'a' && parent.components().length == 1){
icon = UNLINK_TOOL_ICON;
command = UNLINK_COMMAND_ID;
}
}

if(command && !toolbar.some(item => item.command === command)){
toolbar.splice(-2, 0, {
attributes: { 'class': icon },
command
});

component.set('toolbar', toolbar);
}
}
});

editor.Commands.add(LINK_COMMAND_ID, {
run: (editor, sender) => {
let component = editor.getSelected();
if(!component || component.closest('a') !== 0) return;

let toolbar = component.get('toolbar');
let toolIndex = toolbar.findIndex(item => item.command === LINK_COMMAND_ID)
toolbar.splice(toolIndex, 1);

component.set('toolbar', toolbar);
let new_component = component.replaceWith('<a href="#"></a>');
new_component.components(component = component.clone())

editor.select();
editor.select(new_component);

document.querySelector('.gjs-pn-panels .gjs-pn-views .gjs-pn-buttons [title="Settings"]').click();
let hrefInput = document.querySelector('.gjs-pn-panels .gjs-pn-views-container .gjs-trt-trait__wrp-href input');

hrefInput.focus();
hrefInput.select();
},
});

editor.Commands.add(UNLINK_COMMAND_ID, {
run: (editor, sender) => {
let component = editor.getSelected();
if(!component) return;

let parent = component.parent();
if(!parent || !(parent.get('tagName') == 'a' && parent.components().length == 1)) return;

if(!confirm('Are you sure?')) return;

let toolbar = component.get('toolbar');
let toolIndex = toolbar.findIndex(item => item.command === UNLINK_COMMAND_ID)
toolbar.splice(toolIndex, 1);

component.set('toolbar', toolbar);
parent.replaceWith(component = component.clone());
editor.select()
editor.select(component)
},
});
};
26 changes: 26 additions & 0 deletions src/resources/js/plugins/linkable-image/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"sourceMap": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": false
},
"include": [
"src"
]
}

0 comments on commit 4bfe7a7

Please sign in to comment.