GithubHelp home page GithubHelp logo

sofish / pen Goto Github PK

View Code? Open in Web Editor NEW
4.8K 140.0 410.0 265 KB

enjoy live editing (+markdown)

Home Page: https://sofish.github.io/pen

License: Other

JavaScript 66.70% HTML 14.94% CSS 18.36%
editor wysiwyg pen pen-editor

pen's Introduction

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

pen's People

Contributors

1989car avatar alirezas avatar anhulife avatar bkeepers avatar calebmer avatar chasingmaxwell avatar debiancc avatar fritx avatar grabus avatar gurik avatar iranana avatar kkirsche avatar lichgo avatar mattmontgomery avatar michalcarson avatar myst729 avatar nathanathan avatar nycto avatar robertsass avatar samlau95 avatar sofish avatar terrencekhng avatar thomastweets avatar toobug avatar trautwein avatar vadzappa avatar vyahhi avatar zensh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pen's Issues

Placeholder Text

Is there anyway to have placeholder text show up when the user deletes the content in the pen editor?

Inline styles added when setting as a list

When using either the ordered or unordered list buttons this style gets added inline to the element (at least in the live demo):

font-size: 1.36em; line-height: 1.45;

causing the element to bigger than it should, this behaviour latches and cannot be removed

Any appetite for tables?

It seems that most 'medium-style' editing experiences eschew HTML tables. I think I get the reasoning, but I also think there's certainly a place for one that includes simplified table management. Is there any appetite for this in your project?

是否考虑加入评论的功能?

类似于Medium那样,如果别人查看的话,可以对某个内容做评论,这样对于我们做编辑的同学就可以查看大家的内容和建议

Paste plain text

Should be cool, if we have option like..

var options = {
  PastePlainText : true
}

Just let user paste plain text only (so another DIV inserted or more kidding div)

I know a solution like..

$('[contenteditable]').on('paste', function (e) {
    e.preventDefault();
    document.execCommand('inserttext', false, prompt('Paste something.'));
});

But maybe you know better to do

EXTRA : Should be cool if this option listened option from list like list: ['bold', 'italic', 'underline']

So no H1 H2 LINK or another allowed

JUST A IDEA :D

Font Awesome doesn't works when pen is included

Hello,
When I've included pen.css, all my fontawesome icons started to appear really strange, like in that screenshot
Screenshot

Maybe pen overwrite fontawesome font ?
Do you have any ideas on how to solve that
Thanks in advance

default to <p> tag

An empty editor doesn't default to a

tag

Also, a line break after anything else but a p tag results in a

with the text and
's for linebreaks...

Markdown : <pre>

`First : readme.md

Code block: type ```

Change to :

Code block: type <code>`</code>

:P


Ok, I tested on your demo

screen shot 2013-09-17 at 11 22 57 pm

Coz I using Mac ? or what ?

I made a tester for see keycode

http://jsfiddle.net/FL6vk/

I paste (Shit + ´) , Show nothing..

H2,H3作用在整行

假如H2,H3标签作用在整行的话,那选中是不是就没有意义了?

可以将选中文字单独抽取出来添加H标签

Link bug in Firefox

Hi there!

i've got a bug in firefox when i insert a link and continue writing, the new text is contained in the link...
screen shot 2013-09-09 at 5 13 24 pm

Add support for automatic conversion of certain punctuations

I noticed that, when I type in ", ', ... or --, they're simply displayed as-is, however in Medium's editor, they're automatically converted to more appropriate forms. For example, "quotes" to “quotes” (&ldquo; and &rdquo;), ... to (&hellip;) and -- to (&mdash;). Maybe you could consider this feature in future releases. Thank you!

Multiple elements support ?

I wonder how to support multiple elements

Example :
<div class="editme_1"></div>

<div>Disalow</div>

<div class="editme_2"></div>

Bug when selecting text

When i try to select text in Chrome via keyboard shortcuts (option & arrow keys), the toolbar doesn't show up...

screen shot 2013-09-09 at 5 13 05 pm

markdown 的 # bug

使用#时,改变了前一行的标签,并删去当前行。

生成的 <HX> 会相互嵌套,导致字体大小混乱(默认单位是em)

export content

  1. easy to export content
  2. easy to bind to an hidden input/textarea

CASE STUDY : onClick example (jQuery)

I try to make onClick example by jQuery

var pen;
var pen_current;
$('.a, .b').on('click',function(){
    if(!$(this).is(pen_current)) {
        if(pen) {
            pen_current.removeClass('pen'); // Request #issus53
            $('.pen-menu').remove(); // Bug?
            pen.destroy();
        }
        pen = new Pen({
                        editor: $(this)[0],
                        list: ['bold', 'italic', 'underline']
                    });
        pen_current = $(this);
    }
});

http://jsfiddle.net/cmJXd/

All work now

But I seems some bug (or i do something wrong)

  • Duplicate Pen menu .pen-menu (they dont removed but display:none)
  • Class dosnt removed #53
  • Error when select a text (sometimes) TypeError: menu.querySelector(...) is null pen.js (line 227)

Destroy function support ?

Wanna know, How can I destroy Pen function ?

Example :

When I hover A element so

var penA = new Pen({editor: document.querySelector('.A')});

When mouseover how to destroy that ?

markdown用起来很奇怪呀

比如页面结果为:
test

+空格 test就会被影响。

我觉得空格后应该影响当前行后面的文字,而不是上一行的文字。
目前感觉markdown的书写体验不太好

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.