Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <p>
  <strong>Usage:</strong> start typing some of the Patterns, e.g. <em>"debug"</em> or <em>"Friends was only an okay show"</em> or add a new pattern to search and style.
</p>

<!-- Create the toolbar container -->
<div id="toolbar"></div>

<!-- Create the editor container -->
<div id="editor"></div>
<hr>
<button id="get-content">Get Content</button>
<br>
<textarea></textarea>

<!-- Configuration -->
<hr>
<h2>Custom Patterns:</h2>
<br>
<input id="pattern" placeholder="Pattern, e.g. OMG!">
<input id="style" placeholder="Style, e.g. color:red;">
<input id="blot" placeholder="Code, e.g. omg">
<button id="add-custom">Add Pattern</button>

<hr>
<h2>Loaded Patterns:</h2>
<ul class="config"></ul>



            
          
!
            
              /* Display Custom Block: */
body {
  font-family: Helvetica, Arial, sans-serif;
}
.ql-spanblock:after { content: "<sb/>"; }
.spanblock {
    color: orange;
    display: inline-block;
}
.config {
  list-style: none;
}
ul.config {
  background: #f0f0f0;
  padding: 20px;
}
ul.config li {
  margin-bottom: 0px;
}
ul.config li p {
  border: 1px solid black;
  padding: 10px;
  background: white;
  display: inline-block;
  margin-right: 5px;
}
input {
  padding: 10px 20px;
  margin: 0;
  font-size: 1rem;
}
button {
  padding: 10px 20px;
  background: yellowgreen;
  color: white;
  margin: 0;
  font-size: 1rem;
  border: none;
}
#get-content, textarea {
  margin-top: 10px;
}
textarea {
  width: 600px;
  height: 100px;
  background: #f0f0f0;
  margin-top: 10px;
  padding: 10px;
}
            
          
!
            
              console.clear();

// PATTERN CONFIGURATION:
let config = [
  { 
    str: "debug",
    blot: "debug",
    style: "background-color: hotpink; display: inline-block;" 
  },
  { 
    str: "Friends was only an okay show",
    blot: "friends",
    style: "background-color: orange; display: inline-block;" 
  },
];

//>> BABEL.JS: (Used for class prototyping)
function _typeof(obj) { 
  if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); 
}
function _possibleConstructorReturn(self, call) { 
  if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); 
}
function _assertThisInitialized(self) { 
  if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; 
}
function _getPrototypeOf(o) { 
  _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); 
}
function _inherits(subClass, superClass) { 
  if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); 
}
function _setPrototypeOf(o, p) { 
  _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); 
}
function _instanceof(left, right) { 
  if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } 
}
function _classCallCheck(instance, Constructor) { 
  if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } 
}
//<< BABEL.JS

let styleEl = document.createElement('style');
document.head.appendChild(styleEl);
let styleSheet = styleEl.sheet;
const Inline = Quill.import('blots/inline');

class Searcher {

  constructor(quill) {
  
    this.quill = quill;
    this.container = document.getElementById("search-container");

    quill.on('text-change', (delta, oldDelta, source)=>{
      // source: 'api' | 'user'
      console.log(source);

      if ( source === 'user' ){ 
        config.forEach((entry)=>{
          if ( !entry.blotCreated ){
            entry.blotCreated = true;
            createBlot( entry.blot, entry.style );
          }
          this.search( entry ); 
        })
      }
    });

  }

  search( entry ) {

    //  remove any previous search
    // this.removeStyle();

    if ( entry.str ) {

      let totalText = quill.getText();
      let re = new RegExp( entry.str, "gi" );
      let match = re.test( totalText );

      if ( match ) {

        let indices = Searcher.occurrencesIndices = this.getIndicesOf(
          totalText,
          entry.str
        );
        
        let length = (Searcher.SearchedStringLength = entry.str.length);

        indices.forEach(index =>{
          
          quill.formatText(index, length, entry.blot, true);
          quill.formatText(index+length, 1, entry.blot, false);

        });

      } else {
      
        Searcher.occurrencesIndices = null;
        Searcher.currentIndex = 0;

      }
    } 
    else {
      // this.removeStyle();
    }
  }

  getIndicesOf( str, searchStr ) {

    let searchStrLen = searchStr.length;
    let startIndex = 0, index, indices = [];

    while (
      (index = str.toUpperCase().indexOf(searchStr.toUpperCase(), startIndex)) >
      -1
    ) {
      indices.push(index);
      startIndex = index + searchStrLen;
    }
    return indices;
  }

}

Searcher.occurrencesIndices = null;
Searcher.currentIndex = 0;
Searcher.SearchedStringLength = null;

function createBlot( blotName, style ){
  
  console.log( "Creating Blot: " + blotName );

  styleSheet.insertRule( 
    `.ql-${blotName} { ${style} }`, 
    styleSheet.cssRules.length
  );

  let blotFullName = blotName + "Blot";
  window[blotFullName] =
    function( parentClass ) {
    function fn() {
      _classCallCheck(this, fn);
      return _possibleConstructorReturn(this, _getPrototypeOf(fn).apply(this, arguments));
    }
    _inherits( fn, parentClass );
    return fn;
  }(Inline);

  window[blotFullName].blotName = blotName;
  window[blotFullName].className = 'ql-' + blotName.toLowerCase();
  window[blotFullName].tagName = 'div';
  Quill.register(window[blotFullName]);

}

let toolbarOptions = ['bold', 'italic', 'underline', 'strike'];
// More Toolbar Options: https://quilljs.com/docs/modules/toolbar/

Quill.register('modules/Searcher', Searcher);

var quill = new Quill('#editor', {
  modules: { 
    toolbar: toolbarOptions, 
    Searcher: true,
  },
  theme: 'snow'
});

let configUl = document.querySelector("ul.config");
let addCustom = document.querySelector("#add-custom");
let pattern = document.querySelector("#pattern");
let style = document.querySelector("#style");
let blot = document.querySelector("#blot");
let textarea = document.querySelector("textarea");
let getContentButton = document.querySelector("#get-content");

addCustom.addEventListener("click",function(){
  if ( 
    pattern.value !== "" && 
    style.value !== "" && 
    blot.value !== ""
  ){
    let entry = {
    str: pattern.value,
    blot: blot.value,
    style: style.value
  };
    addEntry( entry );
  config.push( entry );
  }
});

config.forEach((entry)=> addEntry( entry ) );

function addEntry( entry ){
  let li = document.createElement("li");
  li.innerHTML = `<p>Pattern: <strong>${entry.str}</strong></p><p><strong>CSS:</strong> <code>${entry.style}</code></p><p><strong>Code:</strong> ${entry.blot}</p>`;
  configUl.appendChild( li );
}

getContentButton.addEventListener("click", handleGetContent );

function handleGetContent(){

  textarea.value = "";
   textarea.value = document.querySelector("#editor .ql-editor").innerHTML;
  
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console