Repository: jaredreich/pell
Branch: master
Commit: d5556baa2e0b
Files: 15
Total size: 26.6 KB
Directory structure:
gitextract_accefpa9/
├── .babelrc
├── .eslintignore
├── .eslintrc
├── .gitignore
├── LICENSE.md
├── README.md
├── demo.html
├── dist/
│ ├── pell.css
│ └── pell.js
├── examples/
│ ├── react.md
│ └── vue.md
├── gulpfile.js
├── package.json
└── src/
├── pell.js
└── pell.scss
================================================
FILE CONTENTS
================================================
================================================
FILE: .babelrc
================================================
{
"presets": [
["es2015", {"modules": false}],
"stage-0"
]
}
================================================
FILE: .eslintignore
================================================
dist
================================================
FILE: .eslintrc
================================================
{
"extends": "jared"
}
================================================
FILE: .gitignore
================================================
.DS_Store
node_modules
================================================
FILE: LICENSE.md
================================================
The MIT License (MIT)
Copyright (c) Jared Reich
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.
================================================
FILE: README.md
================================================
#### [v2 working branch](https://github.com/jaredreich/pell/tree/v2) and [v2 project board](https://github.com/jaredreich/pell/projects/1)
---
[](https://www.npmjs.com/package/pell)
[](https://cdnjs.com/libraries/pell)
> pell is the simplest and smallest WYSIWYG text editor for web, with no dependencies

## Comparisons
| library | size (min+gzip) | size (min) | jquery | bootstrap | react | link |
|---------------|-----------------|------------|--------|-----------|-------|------|
| pell | 1.38kB | 3.54kB | | | | https://github.com/jaredreich/pell |
| squire | 16kB | 49kB | | | | https://github.com/neilj/Squire |
| medium-editor | 27kB | 105kB | | | | https://github.com/yabwe/medium-editor |
| quill | 43kB | 205kB | | | | https://github.com/quilljs/quill |
| trix | 47kB | 204kB | | | | https://github.com/basecamp/trix |
| ckeditor | 163kB | 551kB | | | | https://ckeditor.com |
| trumbowyg | 8kB | 23kB | x | | | https://github.com/Alex-D/Trumbowyg |
| summernote | 26kB | 93kB | x | x | | https://github.com/summernote/summernote |
| draft | 46kB | 147kB | | | x | https://github.com/facebook/draft-js |
| froala | 52kB | 186kB | x | | | https://github.com/froala/wysiwyg-editor |
| tinymce | 157kB | 491kB | x | | | https://github.com/tinymce/tinymce |
## Features
* Pure JavaScript, no dependencies, written in ES6
* Easily customizable with the sass file (pell.scss) or overwrite the CSS
Included actions:
- Bold
- Italic
- Underline
- Strike-through
- Heading 1
- Heading 2
- Paragraph
- Quote
- Ordered List
- Unordered List
- Code
- Horizontal Rule
- Link
- Image
Other available actions (listed at https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand):
- Justify Center
- Justify Full
- Justify Left
- Justify Right
- Subscript
- Superscript
- Font Name
- Font Size
- Indent
- Outdent
- Clear Formatting
- Undo
- Redo
Or create any custom action!
## Browser Support
* IE 9+ (theoretically, but good luck)
* Chrome 5+
* Firefox 4+
* Safari 5+
* Opera 11.6+
## Installation
#### npm:
```bash
npm install --save pell
```
#### HTML:
```html
================================================
FILE: demo.html
================================================
'); } }, quote: { icon: '“ ”', title: 'Quote', result: function result() { return exec(formatBlock, '
');
}
},
olist: {
icon: '#',
title: 'Ordered List',
result: function result() {
return exec('insertOrderedList');
}
},
ulist: {
icon: '•',
title: 'Unordered List',
result: function result() {
return exec('insertUnorderedList');
}
},
code: {
icon: '</>',
title: 'Code',
result: function result() {
return exec(formatBlock, '');
}
},
line: {
icon: '―',
title: 'Horizontal Line',
result: function result() {
return exec('insertHorizontalRule');
}
},
link: {
icon: '🔗',
title: 'Link',
result: function result() {
var url = window.prompt('Enter the link URL');
if (url) exec('createLink', url);
}
},
image: {
icon: '📷',
title: 'Image',
result: function result() {
var url = window.prompt('Enter the image URL');
if (url) exec('insertImage', url);
}
}
};
var defaultClasses = {
actionbar: 'pell-actionbar',
button: 'pell-button',
content: 'pell-content',
selected: 'pell-button-selected'
};
var init = function init(settings) {
var actions = settings.actions ? settings.actions.map(function (action) {
if (typeof action === 'string') return defaultActions[action];else if (defaultActions[action.name]) return _extends({}, defaultActions[action.name], action);
return action;
}) : Object.keys(defaultActions).map(function (action) {
return defaultActions[action];
});
var classes = _extends({}, defaultClasses, settings.classes);
var defaultParagraphSeparator = settings[defaultParagraphSeparatorString] || 'div';
var actionbar = createElement('div');
actionbar.className = classes.actionbar;
appendChild(settings.element, actionbar);
var content = settings.element.content = createElement('div');
content.contentEditable = true;
content.className = classes.content;
content.oninput = function (_ref) {
var firstChild = _ref.target.firstChild;
if (firstChild && firstChild.nodeType === 3) exec(formatBlock, '<' + defaultParagraphSeparator + '>');else if (content.innerHTML === '
') content.innerHTML = '';
settings.onChange(content.innerHTML);
};
content.onkeydown = function (event) {
if (event.key === 'Enter' && queryCommandValue(formatBlock) === 'blockquote') {
setTimeout(function () {
return exec(formatBlock, '<' + defaultParagraphSeparator + '>');
}, 0);
}
};
appendChild(settings.element, content);
actions.forEach(function (action) {
var button = createElement('button');
button.className = classes.button;
button.innerHTML = action.icon;
button.title = action.title;
button.setAttribute('type', 'button');
button.onclick = function () {
return action.result() && content.focus();
};
if (action.state) {
var handler = function handler() {
return button.classList[action.state() ? 'add' : 'remove'](classes.selected);
};
addEventListener(content, 'keyup', handler);
addEventListener(content, 'mouseup', handler);
addEventListener(button, 'click', handler);
}
appendChild(actionbar, button);
});
if (settings.styleWithCSS) exec('styleWithCSS');
exec(defaultParagraphSeparatorString, defaultParagraphSeparator);
return settings.element;
};
var pell = { exec: exec, init: init };
exports.exec = exec;
exports.init = init;
exports['default'] = pell;
Object.defineProperty(exports, '__esModule', { value: true });
})));
================================================
FILE: examples/react.md
================================================
```js
// App.js
import React, { Component } from 'react';
import { init } from 'pell';
import 'pell/dist/pell.css'
class App extends Component {
editor = null
constructor (props) {
super(props)
this.state = { html: null }
}
componentDidMount () {
this.editor = init({
element: document.getElementById('editor'),
onChange: html => this.setState({ html }),
actions: ['bold', 'underline', 'italic'],
})
}
render() {
return (
Editor:
HTML Output:
{this.state.html}
);
}
}
export default App;
```
================================================
FILE: examples/vue.md
================================================
```vue
Editor:
HTML Output:
```
================================================
FILE: gulpfile.js
================================================
const babel = require('rollup-plugin-babel')
const cssnano = require('gulp-cssnano')
const del = require('del')
const gulp = require('gulp')
const rename = require('gulp-rename')
const Rollup = require('rollup')
const rollup = require('gulp-rollup')
const run = require('run-sequence')
const sass = require('gulp-sass')
const size = require('gulp-size')
const uglify = require('rollup-plugin-uglify')
gulp.task('clean', () => del(['./dist']))
const rollupConfig = minimize => ({
rollup: Rollup,
entry: './src/pell.js',
moduleName: 'pell',
format: 'umd',
exports: 'named',
plugins: [babel({ exclude: 'node_modules/**' })].concat(
minimize
? [
uglify({
compress: { warnings: false },
mangle: true,
sourceMap: false
})
]
: []
)
})
gulp.task('script', () => {
gulp.src('./src/*.js')
.pipe(rollup(rollupConfig(false)))
.pipe(size({ showFiles: true }))
.pipe(gulp.dest('./dist'))
gulp.src('./src/*.js')
.pipe(rollup(rollupConfig(true)))
.pipe(rename('pell.min.js'))
.pipe(size({ showFiles: true }))
.pipe(size({ gzip: true, showFiles: true }))
.pipe(gulp.dest('./dist'))
})
gulp.task('style', () => {
gulp.src(['./src/pell.scss'])
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./dist'))
.pipe(cssnano())
.pipe(rename('pell.min.css'))
.pipe(gulp.dest('./dist'))
})
gulp.task('default', ['clean'], () => {
run('script', 'style')
gulp.watch('./src/pell.scss', ['style'])
gulp.watch('./src/pell.js', ['script'])
})
================================================
FILE: package.json
================================================
{
"name": "pell",
"description": "pell - the simplest and smallest WYSIWYG text editor for web, with no dependencies",
"author": "Jared Reich",
"version": "1.0.6",
"main": "./dist/pell.min.js",
"scripts": {
"dev": "gulp",
"build": "gulp clean && gulp script && gulp style",
"lint": "./node_modules/.bin/eslint .js ./"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jaredreich/pell.git"
},
"keywords": [
"text editor",
"editor",
"rich text",
"wysiwyg",
"contenteditable"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/jaredreich/pell/issues"
},
"homepage": "https://github.com/jaredreich/pell",
"devDependencies": {
"babel-core": "6.23.1",
"babel-preset-es2015": "6.22.0",
"babel-preset-stage-0": "6.22.0",
"del": "2.2.2",
"eslint-config-jared": "1.2.0",
"gulp": "3.9.1",
"gulp-cssnano": "2.1.2",
"gulp-rename": "1.2.2",
"gulp-rollup": "2.14.0",
"gulp-sass": "3.1.0",
"gulp-size": "2.1.0",
"rollup": "0.45.1",
"rollup-plugin-babel": "2.7.1",
"rollup-plugin-uglify": "2.0.1",
"run-sequence": "1.2.2"
}
}
================================================
FILE: src/pell.js
================================================
const defaultParagraphSeparatorString = 'defaultParagraphSeparator'
const formatBlock = 'formatBlock'
const addEventListener = (parent, type, listener) => parent.addEventListener(type, listener)
const appendChild = (parent, child) => parent.appendChild(child)
const createElement = tag => document.createElement(tag)
const queryCommandState = command => document.queryCommandState(command)
const queryCommandValue = command => document.queryCommandValue(command)
export const exec = (command, value = null) => document.execCommand(command, false, value)
const defaultActions = {
bold: {
icon: 'B',
title: 'Bold',
state: () => queryCommandState('bold'),
result: () => exec('bold')
},
italic: {
icon: 'I',
title: 'Italic',
state: () => queryCommandState('italic'),
result: () => exec('italic')
},
underline: {
icon: 'U',
title: 'Underline',
state: () => queryCommandState('underline'),
result: () => exec('underline')
},
strikethrough: {
icon: 'S',
title: 'Strike-through',
state: () => queryCommandState('strikeThrough'),
result: () => exec('strikeThrough')
},
heading1: {
icon: 'H1',
title: 'Heading 1',
result: () => exec(formatBlock, '')
},
heading2: {
icon: 'H2',
title: 'Heading 2',
result: () => exec(formatBlock, '')
},
paragraph: {
icon: '¶',
title: 'Paragraph',
result: () => exec(formatBlock, '
')
},
quote: {
icon: '“ ”',
title: 'Quote',
result: () => exec(formatBlock, '
')
},
olist: {
icon: '#',
title: 'Ordered List',
result: () => exec('insertOrderedList')
},
ulist: {
icon: '•',
title: 'Unordered List',
result: () => exec('insertUnorderedList')
},
code: {
icon: '</>',
title: 'Code',
result: () => exec(formatBlock, '')
},
line: {
icon: '―',
title: 'Horizontal Line',
result: () => exec('insertHorizontalRule')
},
link: {
icon: '🔗',
title: 'Link',
result: () => {
const url = window.prompt('Enter the link URL')
if (url) exec('createLink', url)
}
},
image: {
icon: '📷',
title: 'Image',
result: () => {
const url = window.prompt('Enter the image URL')
if (url) exec('insertImage', url)
}
}
}
const defaultClasses = {
actionbar: 'pell-actionbar',
button: 'pell-button',
content: 'pell-content',
selected: 'pell-button-selected'
}
export const init = settings => {
const actions = settings.actions
? (
settings.actions.map(action => {
if (typeof action === 'string') return defaultActions[action]
else if (defaultActions[action.name]) return { ...defaultActions[action.name], ...action }
return action
})
)
: Object.keys(defaultActions).map(action => defaultActions[action])
const classes = { ...defaultClasses, ...settings.classes }
const defaultParagraphSeparator = settings[defaultParagraphSeparatorString] || 'div'
const actionbar = createElement('div')
actionbar.className = classes.actionbar
appendChild(settings.element, actionbar)
const content = settings.element.content = createElement('div')
content.contentEditable = true
content.className = classes.content
content.oninput = ({ target: { firstChild } }) => {
if (firstChild && firstChild.nodeType === 3) exec(formatBlock, `<${defaultParagraphSeparator}>`)
else if (content.innerHTML === '
') content.innerHTML = ''
settings.onChange(content.innerHTML)
}
content.onkeydown = event => {
if (event.key === 'Enter' && queryCommandValue(formatBlock) === 'blockquote') {
setTimeout(() => exec(formatBlock, `<${defaultParagraphSeparator}>`), 0)
}
}
appendChild(settings.element, content)
actions.forEach(action => {
const button = createElement('button')
button.className = classes.button
button.innerHTML = action.icon
button.title = action.title
button.setAttribute('type', 'button')
button.onclick = () => action.result() && content.focus()
if (action.state) {
const handler = () => button.classList[action.state() ? 'add' : 'remove'](classes.selected)
addEventListener(content, 'keyup', handler)
addEventListener(content, 'mouseup', handler)
addEventListener(button, 'click', handler)
}
appendChild(actionbar, button)
})
if (settings.styleWithCSS) exec('styleWithCSS')
exec(defaultParagraphSeparatorString, defaultParagraphSeparator)
return settings.element
}
export default { exec, init }
================================================
FILE: src/pell.scss
================================================
$pell-actionbar-color: #FFF !default;
$pell-border-color: rgba(10, 10, 10, 0.1) !default;
$pell-border-style: solid !default;
$pell-border-width: 1px !default;
$pell-button-height: 30px !default;
$pell-button-selected-color: #F0F0F0 !default;
$pell-button-width: 30px !default;
$pell-content-height: 300px !default;
$pell-content-padding: 10px !default;
.pell {
border: $pell-border-width $pell-border-style $pell-border-color;
box-sizing: border-box;
}
.pell-content {
box-sizing: border-box;
height: $pell-content-height;
outline: 0;
overflow-y: auto;
padding: $pell-content-padding;
}
.pell-actionbar {
background-color: $pell-actionbar-color;
border-bottom: $pell-border-width $pell-border-style $pell-border-color;
}
.pell-button {
background-color: transparent;
border: none;
cursor: pointer;
height: $pell-button-height;
outline: 0;
width: $pell-button-width;
vertical-align: bottom;
}
.pell-button-selected {
background-color: $pell-button-selected-color;
}