1 import {$createParagraphNode, ElementNode} from 'lexical';
3 export class Callout extends ElementNode {
12 return new Callout(node.__category, node.__key);
15 constructor(category, key) {
17 this.__category = category;
20 createDOM(_config, _editor) {
21 const element = document.createElement('p');
22 element.classList.add('callout', this.__category || '');
26 updateDOM(prevNode, dom) {
27 // Returning false tells Lexical that this node does not need its
28 // DOM element replacing with a new copy from createDOM.
32 insertNewAfter(selection, restoreSelection) {
33 const anchorOffset = selection ? selection.anchor.offset : 0;
34 const newElement = anchorOffset === this.getTextContentSize() || !selection
35 ? $createParagraphNode() : $createCalloutNode(this.__category);
37 newElement.setDirection(this.getDirection());
38 this.insertAfter(newElement, restoreSelection);
40 if (anchorOffset === 0 && !this.isEmpty() && selection) {
41 const paragraph = $createParagraphNode();
43 this.replace(paragraph, true);
52 if (node.classList.contains('callout')) {
54 conversion: element => {
55 let category = 'info';
56 const categories = ['info', 'success', 'warning', 'danger'];
58 for (const c of categories) {
59 if (element.classList.contains(c)) {
66 node: new Callout(category),
79 ...super.exportJSON(),
82 category: this.__category,
86 static importJSON(serializedNode) {
87 return $createCalloutNode(serializedNode.category);
92 export function $createCalloutNode(category = 'info') {
93 return new Callout(category);
96 export function $isCalloutNode(node) {
97 return node instanceof Callout;