GithubHelp home page GithubHelp logo

notsimplecontenteditable's Introduction

Not so simple content editable

I wanted to use a contenteditable that I could alter it's content on each edit to make a rich editor. It isn't so simple as to just use el.innerHTML because that updates the whole DOM tree and breaks the editing experience (cursor placement, selections etc...)

So I hacked up a solution involving importing React and the react-contenteditable which implements a VDOM-based way of setting the html and removes common contenteditable nuisances. The rest is just glue to make it work without using React or any specific framework.

Install:

Just download the bundle.js. Rename it to something that makes sense for you.

Usage example:

This will make your content bigger when your content ends with "big"

<body>
  <div
    <h1>Hello</h1>
    <div id="richEditor"></div>
    <script src="bundle.js"></script>
    <script>
      window.onload = function() {
            setupRichEdit("richEditor","Hello buddies",(str,cb) => {
              if(str.endsWith("big")) {
                cb(`<h1>${str}</h1>`)
              }
              else {
                cb(`<h2>${str}</h2>`)
              }
            })
        }
    </script>
</script>
</body>

Info: Particularly tailored to my use case I strip all tags before sending to the function. But that is easily tweaked to whatever you want to do.

Build it yourself:

It's all bundled up by browserify (https://browserify.org/) so it removes all the node-isms:

npm install -g browserify
browserify contenteditable.js -o bundle.js

try.html is a page where you can see it working.

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.