-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from jd-dotlogics/feature/linkable-image
Feature/linkable image
- Loading branch information
Showing
10 changed files
with
305 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.* | ||
*.log | ||
*.html | ||
**/tsconfig.json | ||
**/webpack.config.js | ||
node_modules | ||
src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |