Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a custom format/blot with complex substructure?

Tags:

quill

I'm investigating the use of Quill for a project, and I need to know if it's possible to create a custom format/blot with more complexity than a single element and a single parameter.

An example of one of the layouts I want would be:

<span class="format-container">
    <span class="format-info" data-attr="param 1 (non-displayed)">
        param 2 (displayed to user -- click to invoke application UI to edit)
    </span>
    <span class="format-content">
        User's text/child elements go here
    </span>
</span>

In all cases I'm looking into, the custom formats are of inline scope and still have a single parent container and a single place for the child content to go.

Custom formats in Quill don't seem to be very well documented at the moment. I poked around in the sources and was able to figure out that this most likely isn't possible in 0.20.1. However, I feel like it could be doable in the 1.0.0 beta w/ Parchment, I'm just not sure on the specifics of what I'd actually need to write.

So is this possible in 1.0.0? If so, how could it be done?

EDIT: This is what I'm going for: Example

like image 707
Stephen Cohen Avatar asked Jun 13 '16 18:06

Stephen Cohen


2 Answers

So, I figured out how to do this in the end with minimal effort. It involves defining a new blot type for Quill 1.3 or greater, the same code should work on older versions however is untested.

See the code snippet for a working example. The crux is to extend the existing Embed blot 'blots/embed' and define your own toolbar handler to inject arbitrary DOM node instances.

// utility function used to inherit non prototypical methods/properties
function extend(target, base) {
  for (var prop in base) {
    target[prop] = base[prop];
  }
}

// definition of a custom Blot.
(function(Embed) {
  'use strict';

  function Span() {
    Object.getPrototypeOf(Embed).apply(this, arguments);
  }

  Span.prototype = Object.create(Embed && Embed.prototype);
  Span.prototype.constructor = Span;
  extend(Span, Embed);

  Span.create = function create(value) {
    return value; // expects a domNode as value
  };

  Span.value = function value(domNode) {
    return domNode;
  };

  Span.blotName = 'span';
  Span.tagName = 'SPAN';
  Span.className = 'complex';

  Quill.register(Span, true);
})(Quill.import('blots/embed')); // import the embed blot. This is important as this is being extended

// custom handler for the toolbar button
var handler = function() {
  var complexSpan = document.getElementById('complextype').firstElementChild.cloneNode(true);
  var selection = quill.getSelection();

  quill.insertEmbed(selection.index, 'span', complexSpan);
}

// instantiate quill. Note that modules.toolbar.handlers has a 'span' handler. Quill parses this from // the class on the button in the toolbar markup: 'ql-span' this is 'ql-' + blotName
var quill = new Quill('#editor', {
  modules: {
    toolbar: {
      container: '.toolbar',
      handlers: {
        'span': handler
      }
    }
  },
  theme: 'snow'
});
:root {
  --complex-bgcolor: #767676;
  --font-color: #FFFFFF;
}

html {
  font-size: 10px;
}

button.ql-span {
  width: 15rem !important;
}

.complex {
  border-radius: 1rem;
  border: 0.2rem solid black;
  margin: 0.3rem;
}

.inner {
  border-radius: 1rem;
  background: var(--complex-bgcolor);
  color: var(--font-color);
  padding-left: 0.6rem;
  padding-right: 0.6rem;
}

.formatting {
  font-weight: bold;
  font-style: italic;
  text-decoration: underline;
}

.nested {
  margin-left: 0.3rem;
  margin-right: 0.3rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/quill/1.3.1/quill.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/quill/1.3.1/quill.snow.css" />
<div id="wrapper">
  <div id="toolbar" class="toolbar">
    <span class="ql-formats">
      <button class="ql-bold"></button>
      <button class="ql-italic"></button>
      <button class="ql-underline"></button>
    </span>
    <span class="ql-formats">
      <button class="ql-span">Complex Span Type</button>
    </span>
  </div>

  <div id="editor">Lorem Ipsum 
    <span contenteditable="false">
      <span class="complex" spellcheck="false">
        <span class="inner">Format applied</span>
        <span class="nested">More text</span>
        <span class="formatting">with formatting</span>
        <span class="nested">dolor</span>
      </span>
    </span> sit amet
  </div>
</div>

<div id="complextype" style="display:none;">
<span contenteditable="false"><span class="complex" spellcheck="false"><span class="inner">Format applied</span><span class="nested">More text</span><span class="formatting">with formatting</span><span class="nested">dolor</span></span></span>
</div>
like image 78
Daniel Lane Avatar answered Nov 12 '22 08:11

Daniel Lane


Documentation and guides are still being written but a good place to look is how existing custom formats are implemented. The formula format in particular seems very similar to your use case.

like image 30
jhchen Avatar answered Nov 12 '22 07:11

jhchen