Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Composer</h1>
<rich-text-editor></rich-text-editor>

<script type="module">
import { Component } from "//unpkg.com/can@5/core.mjs";

Component.extend({
  tag: "rich-text-editor",
  view: `
    <div class="controls">
      <button on:click="exec('bold')" class="bold">B</button>
      <button on:click="exec('italic')" class="italic">I</button>
      <button on:click="copyAll()">Copy All</button>
      <button on:click="funky()" class="funky">Funky</button>
    </div>
    <div class="editbox" contenteditable="true">
      <ol>
        <li>Learn <b>about</b> CanJS.</li>
        <li>Learn <i>execCommand</i>.</li>
        <li>Learn about selection and ranges.</li>
        <li>Get Funky.</li>
      </ol>
      <div>Celebrate!</div>
    </div>
  `,
  ViewModel: {
    exec(cmd) {
      document.execCommand(cmd, false, false);
    },
    element: "any",
    connectedCallback(el) {
      this.element = el;
    },
    copyAll() {
      const editBox = this.element.querySelector(".editbox");
      const editBoxRange = document.createRange();
      editBoxRange.selectNodeContents(editBox);

      const selection = window.getSelection();
      selection.removeAllRanges();
      selection.addRange(editBoxRange);

      document.execCommand("copy");
    },
    funky() {
      const editBox = this.element.querySelector(".editbox");
      const editBoxRange = document.createRange();
      editBoxRange.selectNodeContents(editBox);

      const selection = window.getSelection();
      if (selection && selection.rangeCount) {
        const selectedRange = selection.getRangeAt(0);
        if (rangeContains(editBoxRange, selectedRange)) {
          getElementsInRange(selectedRange, "span").forEach(el => {
            el.classList.add("funky");
          });
        }
      }
    }
  }
});

function getElementsInRange(range, wrapNodeName) {
  const elements = [];

  function addSiblingElement(element) {
    // We are going to wrap all text nodes with a span.
    if (element.nodeType === Node.TEXT_NODE) {
      // If there’s something other than a space:
      if (/[^\s\n]/.test(element.nodeValue)) {
        const span = document.createElement(wrapNodeName);
        element.parentNode.insertBefore(span, element);
        span.appendChild(element);
        elements.push(span);
      }
    } else {
      elements.push(element);
    }
  }

  const startContainer = range.startContainer,
    endContainer = range.endContainer,
    commonAncestor = range.commonAncestorContainer;

  if (startContainer === commonAncestor) {
    const wrapper = document.createElement(wrapNodeName);
    range.surroundContents(wrapper);
    elements.push(wrapper);
  } else {
    // Split the starting text node.
    const startWrap = splitRangeStart(range, wrapNodeName);
    addSiblingElement(startWrap);

    // Add nested siblings from startWrap up to the first line.
    const startLine = siblingThenParentUntil(
      "nextSibling",
      startWrap,
      commonAncestor,
      addSiblingElement
    );

    // Split the ending text node.
    const endWrap = splitRangeEnd(range, wrapNodeName);
    addSiblingElement(endWrap);

    // Add nested siblings from endWrap up to the last line.
    const endLine = siblingThenParentUntil(
      "previousSibling",
      endWrap,
      commonAncestor,
      addSiblingElement
    );

    // Add lines between start and end to elements.
    let cur = startLine.nextSibling;
    while (cur !== endLine) {
      addSiblingElement(cur);
      cur = cur.nextSibling;
    }

    // Update the ranges
    range.setStart(startWrap, 0);
    range.setEnd(endWrap.firstChild, endWrap.textContent.length);
  }

  return elements;
}

function rangeContains(outer, inner) {
  return (
    outer.compareBoundaryPoints(Range.START_TO_START, inner) <= 0 &&
    outer.compareBoundaryPoints(Range.END_TO_END, inner) >= 0
  );
}
</script>

<script>
  // from start, this will try `direction` (nextSibling or previousSibling)
  // and call `callback` with each sibling until there are no more siblings
  // it will then move up the parent.  It will end with the parent's parent is `parent`.
  function siblingThenParentUntil(direction, start, parent, callback) {
    var cur = start;
    while (cur.parentNode !== parent) {
      if (cur[direction]) {
        // move to sibling
        cur = cur[direction];
        callback(cur);
      } else {
        // move to parent
        cur = cur.parentNode;
      }
    }
    return cur;
  }

  function splitRangeStart(range, wrapNodeName) {
    var startContainer = range.startContainer;
    var startWrap = document.createElement(wrapNodeName);
    startWrap.textContent = startContainer.nodeValue.substr(range.startOffset);
    startContainer.nodeValue = startContainer.nodeValue.substr(0, range.startOffset);
    startContainer.parentNode.insertBefore(startWrap, startContainer.nextSibling);
    return startWrap;
  }

  function splitRangeEnd(range, wrapNodeName) {
    var endContainer = range.endContainer;
    var endWrap = document.createElement(wrapNodeName);
    endWrap.textContent = endContainer.nodeValue.substr(0, range.endOffset);
    endContainer.nodeValue = endContainer.nodeValue.substr(range.endOffset);
    endContainer.parentNode.insertBefore(endWrap, endContainer);
    return endWrap;
  }
</script>
              
            
!

CSS

              
                .editbox {
  min-height: 100px;
  max-height: 300px;
  border: 1px #aaaaaa solid;
  padding: 6px;
  overflow: scroll;
}

.bold {font-weight: bold;}
.italic {font-style: italic;}

@-webkit-keyframes glowing {
  0% { background-color: #FF0000; box-shadow: 0 0 0px #FF0000;}
  9.1% { background-color: #FF8800; box-shadow: 0 0 0px #FF8800;}
  18.2% { background-color: #888800; box-shadow: 0 0 0px #888800;}
  27.3% { background-color: #88FF00; box-shadow: 0 0 0px #88FF00;}
  36.4% { background-color: #00FF00; box-shadow: 0 0 0px #00FF00;}
  45.5% { background-color: #00FF88; box-shadow: 0 0 0px #00FF88;}
  54.6% { background-color: #00FFFF; box-shadow: 0 0 0px #00FFFF;}
  63.7% { background-color: #0088FF; box-shadow: 0 0 0px #0088FF;}
  72.8% { background-color: #0000FF; box-shadow: 0 0 0px #0000FF;}
  81.9% { background-color: #8800FF; box-shadow: 0 0 0px #8800FF;}
  91.0% { background-color: #FF00FF; box-shadow: 0 0 0px #FF00FF;}
  100% { background-color: #FF0088; box-shadow: 0 0 0px #FF0088;}
}

@-moz-keyframes glowing {
  0% { background-color: #FF0000; box-shadow: 0 0 0px #FF0000;}
  9.1% { background-color: #FF8800; box-shadow: 0 0 0px #FF8800;}
  18.2% { background-color: #888800; box-shadow: 0 0 0px #888800;}
  27.3% { background-color: #88FF00; box-shadow: 0 0 0px #88FF00;}
  36.4% { background-color: #00FF00; box-shadow: 0 0 0px #00FF00;}
  45.5% { background-color: #00FF88; box-shadow: 0 0 0px #00FF88;}
  54.6% { background-color: #00FFFF; box-shadow: 0 0 0px #00FFFF;}
  63.7% { background-color: #0088FF; box-shadow: 0 0 0px #0088FF;}
  72.8% { background-color: #0000FF; box-shadow: 0 0 0px #0000FF;}
  81.9% { background-color: #8800FF; box-shadow: 0 0 0px #8800FF;}
  91.0% { background-color: #FF00FF; box-shadow: 0 0 0px #FF00FF;}
  100% { background-color: #FF0088; box-shadow: 0 0 0px #FF0088;}
}

@-o-keyframes glowing {
  0% { background-color: #FF0000; box-shadow: 0 0 0px #FF0000;}
  9.1% { background-color: #FF8800; box-shadow: 0 0 0px #FF8800;}
  18.2% { background-color: #888800; box-shadow: 0 0 0px #888800;}
  27.3% { background-color: #88FF00; box-shadow: 0 0 0px #88FF00;}
  36.4% { background-color: #00FF00; box-shadow: 0 0 0px #00FF00;}
  45.5% { background-color: #00FF88; box-shadow: 0 0 0px #00FF88;}
  54.6% { background-color: #00FFFF; box-shadow: 0 0 0px #00FFFF;}
  63.7% { background-color: #0088FF; box-shadow: 0 0 0px #0088FF;}
  72.8% { background-color: #0000FF; box-shadow: 0 0 0px #0000FF;}
  81.9% { background-color: #8800FF; box-shadow: 0 0 0px #8800FF;}
  91.0% { background-color: #FF00FF; box-shadow: 0 0 0px #FF00FF;}
  100% { background-color: #FF0088; box-shadow: 0 0 0px #FF0088;}
}

@keyframes glowing {
  0% { background-color: #FF0000; box-shadow: 0 0 0px #FF0000;}
  9.1% { background-color: #FF8800; box-shadow: 0 0 0px #FF8800;}
  18.2% { background-color: #888800; box-shadow: 0 0 0px #888800;}
  27.3% { background-color: #88FF00; box-shadow: 0 0 0px #88FF00;}
  36.4% { background-color: #00FF00; box-shadow: 0 0 0px #00FF00;}
  45.5% { background-color: #00FF88; box-shadow: 0 0 0px #00FF88;}
  54.6% { background-color: #00FFFF; box-shadow: 0 0 0px #00FFFF;}
  63.7% { background-color: #0088FF; box-shadow: 0 0 0px #0088FF;}
  72.8% { background-color: #0000FF; box-shadow: 0 0 0px #0000FF;}
  81.9% { background-color: #8800FF; box-shadow: 0 0 0px #8800FF;}
  91.0% { background-color: #FF00FF; box-shadow: 0 0 0px #FF00FF;}
  100% { background-color: #FF0088; box-shadow: 0 0 0px #FF0088;}
}

button.funky {
  -webkit-border-radius: 3px;
  border-radius: 3px;
  border: none;
  color: #FFFFFF;
  display: inline-block;
  text-decoration: none;
}

.funky {
  background-color: #004A7F;
  -webkit-animation: glowing 1500ms infinite;
  -moz-animation: glowing 1500ms infinite;
  -o-animation: glowing 1500ms infinite;
  animation: glowing 2000ms infinite;
}
              
            
!

JS

              
                
              
            
!
999px

Console