Prosemirror - powerful instrument to build rich-text editor with high ability to extend and full control to document contents, rendering and user interaction. We can easily implement mentions, tables, integrate with entities from other parts of application and extend functionality by many ways. In terms of prosemirror entity that represent specific content called a Node.
In this article I’m going to show how to declare custom prosemirror node, how to render it with
toDom method and how improve that with custom
NodeView using React component.
Good example we have is callout node - text block that can be highlighted with color, icon and indention to emphasize or warn about something in main narration:
In scope of this article we will only talk about rendering this node, but initial idea is to allow user to set callout type, and insert/delete callouts. We will focus on that interactions in next articles about plugin system and transactions.
What is interesting here and most probably you already get that from image: we basically just wrap area of editor content with our new node, allowing user to have all editing features in place. And here we going to see how to achieve that using react component.
We will declare our node in a directory called
callout to encapsulate custom logic and rendering process from editor setup. React components, types etc will also go there.
So, our editor structure will look like this:
We will focus more on contents of
editor/callout in a next few sections.
User can choose callout type from
callout here is the same key that will be used to determine node spec in editor schema. Now we’ll look at the node definition:
group - group or space-separated groups to which this node belongs. can be referred from content expression.
content - declares what we can expect in node content. has a special expression syntax
defining - makes node persist in replace (paste) operation
selectable - is node can be selected with cursor
isolating - set node boundaries for editing operations
attrs - custom node attributes, only used to set
type attribute, which set by default to
toDOM - we will talk about it in details in a next section.
Easiest way to implement node render is to declare
toDOM node method, that we saw for paragraph node above. It clear, intuitive and you basically don’t have to worry about anything else with that approach. Yet it has its disadvantages: it’s not very comfortable to describe really complex html and there is no good way to make it interactive. You probably want to start with this when you describe custom nodes, and turn to nodeviews only when you have to.
We are going to do both in turn. Look again on this code piece:
Callout node allows user to enter any
block+ content as we declared in corresponding node attribute. It means that we let prosemirror deal with user input by itself. And we see here that we have 0 - “content hole” inside nested div. With that in place, callout will look like this:
Okay, but what if we need to have more control over html? We also want to display icon and properly handle callout type with corresponding icon and color. Obviously it can be achieved here in
toDOM method by creating more nested elements and maybe little bit of business logic.. But we’d like to distinguish things like that, right?
For such cases there is a NodeView.
For some use cases, such as embedded node-specific editing interfaces, you want more control over the behavior of a node's in-editor representation, and need to define a custom node view.
contentDom properties as a container and content elements respectively. Then in constructor we create div elements for both and call
destroy are default methods and have to be implemented as prosemirror uses them internally. It’s quite simple: we check for node type in update and call render for matching node. And unmount component on destroy
Most interesting part here is
render method. We bootstrap a react component to dom element and use
handleRef to add content to a DOM node. We’re talking about ref in a more details a bit later. Now let’s check the React component.
As we work with regular react code here we can use component to render icon depending on type:
But most important and not obvious here is
contentDOMRef. We implement a callback ref that fills DOM node with it’s html representation we have from nodeview’s
contentDOM attribute. It’s a quite tricky moment that we get content from editor and render it with react. And when user change callout contents, we trigger
handleRef that adds that content to dom node . So roughly speaking it work like this:
To sum up let’s review steps required to render custom prosemirror node with React:
Node and add it to schema
NodeView and bind to custom node on
Render react component using
Here we initialize editor with callout node and nodeview in another react component:
Prosemirror is a beautiful tool that allow you to control things on any level. It has its learning curve, but in the end you can build really flexible and logically clear editors with it.
Yet it may look difficult to understand form the first sight, the main concepts is simple:
model (schema) - state - view
And this is something similar to what we all know from different MVP-like libraries and tools. With schema we describe entities (nodes), state uses schema and describe data structure, and view is responsible for how it look on page.
By the way, here is live demo so you can play with and check the full code.
In next articles we will talk about plugin system and how it can be used to make nodes interactive with react. We will implement menu to change callout type and make it interact with prosemirror transactions and update state.
Earlier in our previous article "Improve efficiency of your SELECT queries" we discussed ways to profile and optimize the performance of SELECT queries. However, to write complex yet efficient SQL queries, there is a thing to remember about.
SQL is a fairly complicated language with a steep learning curve. For a large number of people who make use of SQL, learning to apply it efficiently takes lots of trials and errors. Here are some tips on how you can make your SELECT queries better. The majority of tips should be applicable to any relational database management system, but the terminology and exact namings will be taken from PostgreSQL.
Programmatic generation of PDF files is a frequent task when developing applications that can export reports, bills, or questionnaires. In this article, we will consider three common tools for creating PDFs, including their installation and converting principles.
I just found out that __or__ and __and__ are defined for QuerySet. This means that to do queries union or intersection, you can do:User.objects.filter(...) | User.objects.filter(...)User.objects.filter(...) & User.objects.filter(...)