@lexical/mark
Version:
This package contains helpers and nodes for wrapping content in marks for Lexical.
10 lines (8 loc) • 3.14 kB
JavaScript
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import{ElementNode as t,$getDocument as e,addClassNamesToElement as r,removeClassNamesFromElement as n,$isRangeSelection as s,$applyNodeReplacement as i,$isTextNode as o,$createRangeSelection as c,$isElementNode as u,$isDecoratorNode as l,defineExtension as a}from"lexical";const f=[];class d extends t{__ids;static getType(){return"mark"}static clone(t){return new d(t.__ids,t.__key)}afterCloneFrom(t){super.afterCloneFrom(t),this.__ids=t.__ids}static importDOM(){return null}static importJSON(t){return h().updateFromJSON(t)}updateFromJSON(t){return super.updateFromJSON(t).setIDs(t.ids)}exportJSON(){return{...super.exportJSON(),ids:this.getIDs()}}constructor(t=f,e){super(e),this.__ids=t}createDOM(t){const n=e().createElement("mark");return r(n,t.theme.mark),this.__ids.length>1&&r(n,t.theme.markOverlap),n}updateDOM(t,e,s){const i=t.__ids,o=this.__ids,c=i.length,u=o.length,l=s.theme.markOverlap;return c!==u&&(1===c?2===u&&r(e,l):1===u&&n(e,l)),!1}hasID(t){return this.getIDs().includes(t)}getIDs(){return Array.from(this.getLatest().__ids)}setIDs(t){const e=this.getWritable();return e.__ids=t,e}addID(t){const e=this.getWritable();return e.__ids.includes(t)?e:e.setIDs([...e.__ids,t])}deleteID(t){const e=this.getWritable(),r=e.__ids.indexOf(t);if(-1===r)return e;const n=Array.from(e.__ids);return n.splice(r,1),e.setIDs(n)}insertNewAfter(t,e=!0){const r=h(this.__ids);return this.insertAfter(r,e),r}canInsertTextBefore(){return!1}canInsertTextAfter(){return!1}canBeEmpty(){return!1}isInline(){return!0}extractWithChild(t,e,r){if(!s(e)||"html"===r)return!1;const n=e.anchor,i=e.focus,o=n.getNode(),c=i.getNode(),u=e.isBackward()?n.offset-i.offset:i.offset-n.offset;return this.isParentOf(o)&&this.isParentOf(c)&&this.getTextContent().length===u}excludeFromCopy(t){return"clone"!==t}}function h(t=f){return i(new d(t))}function _(t){return t instanceof d}function m(t){const e=t.getChildren();let r=null;for(let n=0;n<e.length;n++){const s=e[n];null===r?t.insertBefore(s):r.insertAfter(s),r=s}t.remove()}function g(t,e,r,n){const s=c(),[i,a]=t.isBackward()?[t.focus,t.anchor]:[t.anchor,t.focus];let f,d;s.anchor.set(i.key,i.offset,i.type),s.focus.set(a.key,a.offset,a.type);const m=s.extract();for(const t of m){if(u(d)&&d.isParentOf(t))continue;let e=null;if(o(t))e=t;else{if(_(t))continue;(u(t)||l(t))&&t.isInline()&&(e=t)}if(null!==e){if(e&&e.is(f))continue;const t=e.getParent();if(null!=t&&t.is(f)||(d=void 0),f=t,void 0===d){d=(n||h)([r]),e.insertBefore(d)}d.append(e)}else f=void 0,d=void 0}u(d)&&(e?d.selectStart():d.selectEnd())}function p(t,e){let r=t;for(;null!==r;){if(_(r))return r.getIDs();if(o(r)&&e===r.getTextContentSize()){const t=r.getNextSibling();if(_(t))return t.getIDs()}r=r.getParent()}return null}const I=/* @__PURE__ */a({name:"@lexical/mark",nodes:()=>[d]});export{h as $createMarkNode,p as $getMarkIDs,_ as $isMarkNode,m as $unwrapMarkNode,g as $wrapSelectionInMarkNode,I as MarkExtension,d as MarkNode};