• Stars
    star
    4,788
  • Rank 8,437 (Top 0.2 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 11 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

enjoy live editing (+markdown)

Pen Editor


pen editor - screenshot


0. source code

You can clone the source code from github, or using bower.

bower install pen

1. installation

1.1 init with id attribute

var editor = new Pen('#editor');

1.2 init with an element

var editor = new Pen(document.getElementById('editor'));

1.3 init with options

var options = {
  editor: document.body, // {DOM Element} [required]
  class: 'pen', // {String} class of the editor,
  debug: false, // {Boolean} false by default
  textarea: '<textarea name="content"></textarea>', // fallback for old browsers
  list: ['bold', 'italic', 'underline'], // editor menu list
  linksInNewWindow: true // open hyperlinks in a new windows/tab
}

var editor = new Pen(options);

2. configure

The following object sets up the default settings of Pen:

defaults = {
  class: 'pen',
  debug: false,
  textarea: '<textarea name="content"></textarea>',
  list: [
    'blockquote', 'h2', 'h3', 'p', 'insertorderedlist', 'insertunorderedlist',
    'indent', 'outdent', 'bold', 'italic', 'underline', 'createlink'
  ],
  stay: true,
  linksInNewWindow: false
}

If you want to customize the toolbar to fit your own project, you can instanciate Pen constructor with an options object like #1.3: init with options:

2.1 Fallback for old browser

You can set defaults.textarea to a piece of HTML string, by default, it's <textarea name="content"></textarea>。This will be set as innerHTML of your #editor.

2.2 Change the editor class

Pen will add .pen to your editor by default, if you want to change the class, make sure to replace the class name pen to your own in src/pen.css.

2.3 Enable debug mode

If options.debug is set to true, Pen will output logs to the Console of your browser.

debugger

2.4 Customize the toolbar

You can set options.list to an Array, add the following strings to make your own:

  • blockquote, h2, h3, p, pre: create a tag as its literal meaning
  • insertorderedlist: create an ol>li list
  • insertunorderedlist: create a ul>li list
  • indent: indent list / blockquote block
  • outdent: outdent list / blockquote block
  • bold: wrap the text selection in a b tag
  • italic: wrap the text selection in an i tag
  • underline: wrap the text selection in a u tag
  • createlink: insert link to the text selection
  • inserthorizontalrule: insert a hr tag
  • insertimage: insert an image (img) tag

2.5 Add tooltips to the toolbar icons

You can set options.titles to an object with properties that match the toolbar actions. The value of each property will be used as the title attribute on the icon. Most browsers will display the title attribute as a tooltip when the mouse hovers over the icon.

options.title = {
    'blockquote': 'Blockquote'
    'createlink': 'Hyperlink'
    'insertimage': 'Image'
}

If you are using Bootstrap or jQueryUI, you can standardize the tooltip style by adding $('i.pen-icon').tooltip() to your JavaScript.

2.6 Prevent unsafe page redirect

By default, Pen will prevent unsafe page redirect when editing, to shut down it, specific options.stay to false.

NOTE: if defaults.debug is set to true and default.stay is not set: defaults.stay == !defaults.debug.

2.7 Disable and Re-enable editor

You can disable the pen editor by call destroy() method of the var pen = new Pen(options) object. like:

var pen = new Pen('#editor');

pen.destroy(); // return itself

And, there's a corresponding method called rebuild() to re-enable the editor:

pen.rebuild(); // return itself

2.8 Export content as markdown

It's an experimental feature

var pen = new Pen('#editor');

pen.toMd(); // return a markdown string

3. markdown syntax support

3.1 install

The syntax convertor will be enabled automatically by linking markdown.js after `pen.js:

<script src="src/pen.js"></script>
<script src="src/markdown.js"></script>

3.2 usage

To use it, you can type action cmd + space key at a line start. like:

### This will create a h3 tag

The following cmds are allowed:

  • Headings: type 1~6 # at the line start
  • Unordered List: type - or *
  • Ordered List: type 1.
  • Code block: type ```
  • Block Quote: type >
  • Horizontal Rule: more than 3 -, *, . will create a <hr />, like ......

4. license

Licensed under MIT.

5. trusted by *

Teambition

More Repositories

1

typo.css

中文网页重设与排版:一致化浏览器排版效果,构建最适合中文阅读的网页排版
HTML
4,477
star
2

wechat.js

微信相关的 js 操作:分享、网络、菜单
JavaScript
920
star
3

express-guide

对英文版 ExpressJS 的一个翻译
363
star
4

validator.js

一个简单、轻量级,但功能强大的 Validator 组件
JavaScript
284
star
5

han

a module for tanslating Chinese(汉字) into /\w+/ or pinyin
JavaScript
200
star
6

essage

a more elegant way to show message
JavaScript
187
star
7

learn-js

老婆想学 js 这件事就是一个政治任务
142
star
8

everyday

从今天开始,要么写代码要么写文档
JavaScript
133
star
9

sofi.sh

program of http://sofi.sh
118
star
10

restcookbook

REST 小书
HTML
84
star
11

weather

weather app for iOS device
CSS
84
star
12

weather-api

提供一个从 weather.com.cn 上根据城市接口取天气信息的方法
JavaScript
56
star
13

node-api-docs

解读 node.js api 文档
54
star
14

stacktrace.js

record js stack trace
JavaScript
52
star
15

imageXSS.js

防止外部链接通过图片进行 XSS 攻击
JavaScript
46
star
16

performance.js

calculate performance timing for web page
JavaScript
46
star
17

proportion.js

meet the best proportion of the device.
HTML
41
star
18

hire

理论上是长期的,不定时更新
29
star
19

recorder.js

Using HTML5 to capture photo / video via the camera of your device
JavaScript
29
star
20

writedown

Stop thinking, start creating.
JavaScript
28
star
21

A-Poem-A-Day

给儿子念诗,每天一首
27
star
22

storage.js

a jQuery plugin that make text storage works with all mayor browsers (include IE6+)
19
star
23

sofish

13
star
24

mongoimport

import JSON to mongo collection
JavaScript
11
star
25

resize.js

use canvas to resize image
JavaScript
10
star
26

log2json

transform nginx log to json
JavaScript
9
star
27

goyd

自用有道翻译 CLI 小工具
Go
6
star
28

mask.js

create an opacity mask to a(all) specific element(s)
JavaScript
3
star
29

nodebb-plugin-qiniu-file

save images/files to qiniu.com
JavaScript
2
star
30

bx_suggest

百姓网搜索自动完成 workflow.
PHP
2
star
31

hasFlashPlayer.js

a snippet to detect whether a FlashPlayer is installed in browser.
JavaScript
1
star
32

log2mongo

automatic log to mongodb
JavaScript
1
star
33

donuts

hybrid sucks
JavaScript
1
star