contenido
Version:
Contenido is a library with a set of tools to help you create your own rich text editor on top of draft-js without thinking about how to handle things.
8 lines (7 loc) • 14.6 kB
JavaScript
import{jsx as t}from"react/jsx-runtime";import*as e from"react";import n from"draft-js";var r=function(){return r=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t},r.apply(this,arguments)};function i(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(t);i<r.length;i++)e.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(t,r[i])&&(n[r[i]]=t[r[i]])}return n}function o(t,e,n){if(n||2===arguments.length)for(var r,i=0,o=e.length;i<o;i++)!r&&i in e||(r||(r=Array.prototype.slice.call(e,0,i)),r[i]=e[i]);return t.concat(r||Array.prototype.slice.call(e))}function u(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}"function"==typeof SuppressedError&&SuppressedError;var l,a={exports:{}};
/*!
Copyright (c) 2018 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
*/l=a,function(){var t={}.hasOwnProperty;function e(){for(var n=[],r=0;r<arguments.length;r++){var i=arguments[r];if(i){var o=typeof i;if("string"===o||"number"===o)n.push(i);else if(Array.isArray(i)){if(i.length){var u=e.apply(null,i);u&&n.push(u)}}else if("object"===o){if(i.toString!==Object.prototype.toString&&!i.toString.toString().includes("[native code]")){n.push(i.toString());continue}for(var l in i)t.call(i,l)&&i[l]&&n.push(l)}}}return n.join(" ")}l.exports?(e.default=e,l.exports=e):window.classNames=e}();var c=u(a.exports),f={exports:{}};const s=n,{AtomicBlockUtils:g,CompositeDecorator:y,Editor:h,EditorState:d,Modifier:p,RichUtils:S,SelectionState:E,KeyBindingUtil:v,convertToRaw:C,convertFromRaw:I,genKey:R,getDefaultKeyBinding:m}=s;f.exports={AtomicBlockUtils:g,CompositeDecorator:y,Editor:h,EditorState:d,Modifier:p,RichUtils:S,SelectionState:E,KeyBindingUtil:v,convertToRaw:C,convertFromRaw:I,genKey:R,getDefaultKeyBinding:m},f.exports.DraftEntityMutability=s.DraftEntityMutability,f.exports.DraftDecorator=s.DraftDecorator;var x=f.exports,T=function(t,e){var n=t.getSelection();return t.getCurrentContent().getBlockForKey(n.getStartKey()).getType()===e},U=function(t){return T(t,"blockquote")},L=function(t){return T(t,"ordered-list-item")},O=function(t){return T(t,"unordered-list-item")},b=function(n){var o=n.editorRef,u=n.placeholder,l=n.rtlPlaceholder,a=n.textDirectionality,f=i(n,["editorRef","placeholder","rtlPlaceholder","textDirectionality"]),s=e.useState(!1),g=s[0],y=s[1],h=e.useCallback((function(){return!(U(n.editorState)||L(n.editorState)||O(n.editorState))}),[n.editorState]);e.useEffect((function(){y(h())}),[h]);var d=function(){return n.editorState.getCurrentContent().hasText()};return t("div",r({className:c({"rtl-placeholder":l&&!d()})||void 0},{children:t(x.Editor,r({ref:o,placeholder:g&&u?u:void 0,textDirectionality:a||(l&&!d()?"RTL":void 0)},f))}))},k=function(t,e,n,i,o){var u=t.getCurrentContent().createEntity(n,o||"IMMUTABLE",r({},i)),l=u.getLastCreatedEntityKey(),a=x.EditorState.set(t,{currentContent:u});return e(x.AtomicBlockUtils.insertAtomicBlock(a,l," ")),l},B=function(t,e,n){return k(t,e,"audio",n)},D=function(t,e,n){return k(t,e,"file",n)},K=function(t,e,n){return k(t,e,"image",n)},N=function(t,e,n){return k(t,e,"video",n)},P={image:"image",video:"video",audio:"audio",file:"file"},A=function(t,e,n,i){var o=t.getCurrentContent(),u=o.getEntity(n),l=r(r({},u.getData()),i),a=o.mergeEntityData(n,l);e(x.EditorState.push(t,a,"apply-entity"))},w=function(e,n){return function(i){if("atomic"===i.getType())return{component:function(n){var i=n.contentState.getEntity(n.block.getEntityAt(0)),o=n.block.getEntityAt(0),u=i.getData(),l=i.getMutability(),a=i.getType();return t(e,r({},u,{blockType:a,mutability:l,entityKey:o}))},editable:!1};n&&n(i)}},j={"text-align-left":"text-align-left","text-align-center":"text-align-center","text-align-right":"text-align-right","text-align-justify":"text-align-justify",blockquote:"blockquote",unstyled:"unstyled"},M=function(t,e){var n=t.getType(),i=j;return e&&(null==e?void 0:e.length)>0&&e.forEach((function(t){return i=r(r({},i),t)})),i[n||"unstyled"]},H={H1:"header-one",H2:"header-two",H3:"header-three",H4:"header-four",H5:"header-five",H6:"header-six",OL:"ordered-list-item",UL:"unordered-list-item",P:"paragraph",Blockquote:"blockquote"},_=function(t){return T(t,"header-one")},q=function(t){return T(t,"header-two")},F=function(t){return T(t,"header-three")},G=function(t){return T(t,"header-four")},V=function(t){return T(t,"header-five")},J=function(t){return T(t,"header-six")},z=function(t){return T(t,"paragraph")||T(t,"unstyled")},W=function(t,e){return T(t,e||"text-align-center")},X=function(t,e){return T(t,e||"text-align-justify")},Z=function(t,e){return T(t,e||"text-align-left")},Q=function(t,e){return T(t,e||"text-align-right")},Y=function(t,e,n){e(x.RichUtils.toggleBlockType(t,n))},$=function(t,e,n){return Y(t,e,n||"blockquote")},tt=function(t,e,n){return Y(t,e,n||"header-one")},et=function(t,e,n){return Y(t,e,n||"header-two")},nt=function(t,e,n){return Y(t,e,n||"header-three")},rt=function(t,e,n){return Y(t,e,n||"header-four")},it=function(t,e,n){return Y(t,e,n||"header-five")},ot=function(t,e,n){return Y(t,e,n||"header-six")},ut=function(t,e,n){return Y(t,e,n||"ordered-list-item")},lt=function(t,e,n){return Y(t,e,n||"paragraph")},at=function(t,e,n){return Y(t,e,n||"text-align-right")},ct=function(t,e,n){return Y(t,e,n||"unordered-list-item")},ft=function(t,e,n,r){var i=t.getCurrentContent(),o=i.createEntity(null!=r?r:"link","IMMUTABLE",n),u=i.getLastCreatedEntityKey(),l=x.EditorState.set(t,{currentContent:o}),a=l.getSelection();e(x.RichUtils.toggleLink(l,a,u))},st=function(e){return function(n){var o=n.contentState;n.blockKey,n.decoratedText;var u=n.entityKey,l=n.offsetKey,a=n.end,c=n.start,f=i(n,["contentState","blockKey","decoratedText","entityKey","offsetKey","end","start"]),s=o.getEntity(u),g=s.getType(),y=s.getMutability,h=s.getData();return t(e,r({blockType:g,mutability:y,entityKey:u,offsetKey:l,end:a,start:c},f,h))}},gt=function(t){var e=[];return t.forEach((function(t){return e.push({strategy:t.strategy,component:st(t.component),props:t.props})})),new x.CompositeDecorator(e)},yt=function(t,e){var n=t.getSelection();n.isCollapsed()||e(x.RichUtils.toggleLink(t,n,null))},ht=function(t){return function(e,n,r){e.findEntityRanges((function(e){var n=e.getEntity();return null!==n&&r.getEntity(n).getType()===t}),n)}},dt=ht("link"),pt=function(){var t=Date.now().toString(36),e=Math.random().toString(36).substring(2,7);return"".concat(t).concat(e).toUpperCase()},St=function(t,e){var n=t.getSelection(),r=t.getCurrentContent(),i=r.getBlockForKey(n.getStartKey()).getEntityAt(n.getStartOffset());return!!i&&r.getEntity(i).getType()===(null!=e?e:"link")},Et=function(t){var e=t.getCurrentContent().getBlockMap(),n=t.getSelection(),r=n.getStartKey(),i=n.getEndKey();return r===i?[e.get(r)]:e.toSeq().skipUntil((function(t,e){return e===r})).takeUntil((function(t,e){return e===i})).toArray()},vt=function(t,e){var n=t.getCurrentContent(),r=Et(t).reduce((function(t,e){if("unstyled"!==e.getType()){var n=x.SelectionState.createEmpty(e.getKey()).merge({focusOffset:0,anchorOffset:e.getText().length});return x.Modifier.setBlockType(t,n,"unstyled")}return t}),n),i=x.EditorState.push(t,r,"change-block-type");return e(i),i},Ct=function(t,e){var n=t.getCurrentContent(),r=t.getSelection(),i=x.Modifier.applyEntity(n,r,null),o=x.EditorState.push(t,i,"apply-entity");return e(o),o},It={BOLD:"BOLD",ITALIC:"ITALIC",UNDERLINE:"UNDERLINE",LINE_THROUGH:"LINE_THROUGH",OVERLINE:"OVERLINE",SUPERSCRIPT:"SUPERSCRIPT",SUBSCRIPT:"SUBSCRIPT"},Rt=function(t,e,n){var r=t.getCurrentContent(),i=t.getSelection(),o=Object.keys(It);n&&n.forEach((function(t){o.includes(t)||o.push(t)}));var u=o.reduce((function(t,e){return x.Modifier.removeInlineStyle(t,i,e)}),r),l=x.EditorState.push(t,u,"change-inline-style");return e(l),l},mt=function(t,e){return[Rt,vt,Ct].reduce((function(t,n){return n(t,e)}),t)},xt={entityMap:{},blocks:[{text:"",key:"LG6AABOZOBAMJ",type:"unstyled",entityRanges:[],depth:0,inlineStyleRanges:[]}]},Tt=function(t){var e;t&&(null===(e=t.current)||void 0===e||e.focus())},Ut=function(t,e){var n=0,r=t.getCurrentContent();return r.getBlockMap().forEach((function(t){t&&"atomic"===t.getType()&&(r.getEntity(t.getEntityAt(0)).getType()===e&&n++)})),n},Lt=function(t){return Ut(t,"audio")},Ot=function(t){var e=t.getSelection();return t.getCurrentContent().getBlockForKey(e.getStartKey()).getType()},bt=function(t){var e=t.getCurrentContent().getPlainText().match(/\S+/g);return e?e.join("").length:0},kt=function(t,e){var n=0,r=t.getCurrentContent(),i=Object.values(x.convertToRaw(r).entityMap);return i.length>0&&i.forEach((function(t){t.type===e&&n++})),n},Bt=function(t){return Ut(t,"file")},Dt=function(t){return Ut(t,"image")},Kt=function(t){return kt(t,"link")},Nt=function(t){if("string"==typeof t){var e=[];try{var n=JSON.parse(t);if(n&&n.blocks)for(var r=0,i=n.blocks;r<i.length;r++){var o=i[r];o&&o.text&&e.push(o.text)}}catch(t){console.error("The provided value to get the plain text is neither a valid JSON nor a draft-js state.")}return e.join(" ")}return t.getCurrentContent().getPlainText()},Pt=function(t){return Ut(t,"video")},At=function(t){var e=t.getCurrentContent().getPlainText().match(/\S+/g);return e||[]},wt=function(t){var e=t.getCurrentContent().getPlainText().match(/\S+/g);return e?e.length:0},jt=function(t,e){var n=x.RichUtils.insertSoftNewline(t);return e(n),n},Mt=function(t){return!t.getCurrentInlineStyle().isEmpty()},Ht=function(t,e){return t.getCurrentInlineStyle().has(e)},_t={LINE_THROUGH:{textDecoration:"line-through"},OVERLINE:{textDecoration:"overline"},SUPERSCRIPT:{verticalAlign:"top",fontSize:"0.75em"},SUBSCRIPT:{verticalAlign:"sub",fontSize:"0.75em"}},qt=function(t,e){return t.getCurrentInlineStyle().has(e||"BOLD")},Ft=function(t,e){return t.getCurrentInlineStyle().has(e||"ITALIC")},Gt=function(t,e){return t.getCurrentInlineStyle().has(e||"LINE_THROUGH")},Vt=function(t,e){return t.getCurrentInlineStyle().has(e||"OVERLINE")},Jt=function(t,e){return t.getCurrentInlineStyle().has(e||"SUBSCRIPT")},zt=function(t,e){return t.getCurrentInlineStyle().has(e||"SUPERSCRIPT")},Wt=function(t,e){return t.getCurrentInlineStyle().has(e||"UNDERLINE")},Xt=function(t,e,n){e(x.RichUtils.toggleInlineStyle(t,n||"BOLD"))},Zt=function(t,e,n){e(x.RichUtils.toggleInlineStyle(t,n))},Qt=function(t,e,n){e(x.RichUtils.toggleInlineStyle(t,n||"ITALIC"))},Yt=function(t,e,n){e(x.RichUtils.toggleInlineStyle(t,n||"LINE_THROUGH"))},$t=function(t,e,n){e(x.RichUtils.toggleInlineStyle(t,n||"OVERLINE"))},te=function(t,e,n){e(x.RichUtils.toggleInlineStyle(t,n||"SUBSCRIPT"))},ee=function(t,e,n){e(x.RichUtils.toggleInlineStyle(t,n||"SUPERSCRIPT"))},ne=function(t,e,n){e(x.RichUtils.toggleInlineStyle(t,n||"UNDERLINE"))},re=[{style:"BOLD",shortcut:["ctrl","b"],handler:Xt},{style:"ITALIC",shortcut:["ctrl","i"],handler:Qt},{style:"UNDERLINE",shortcut:["ctrl","u"],handler:ne},{style:"SOFT_NEW_LINE",shortcut:function(t){return x.KeyBindingUtil.isSoftNewlineEvent(t)?"SOFT_NEW_LINE":""},handler:jt}],ie=function(t,e){"Alt"===t.key&&t.preventDefault();for(var n=0,r=e||re;n<r.length;n++){var i=r[n];if(Array.isArray(i.shortcut)){if(i.shortcut.every((function(e,n){return!(!["ctrl","control","cmd","command"].includes(e.toLocaleLowerCase())||!x.KeyBindingUtil.hasCommandModifier(t))||(!("alt"!==e.toLocaleLowerCase()&&"option"!==e.toLocaleLowerCase()||!x.KeyBindingUtil.isOptionKeyCommand(t))||("shift"===e.toLocaleLowerCase()?t.shiftKey:e===t.key))})))return i.style}else if(i.shortcut(t))return i.shortcut(t)}return x.getDefaultKeyBinding(t)},oe=function(t,e){return function(n,r){var i=e||re,o=i.findIndex((function(t){return t.style===n}));return o>-1?(i[o].handler(r,t),"handled"):"not-handled"}},ue=o(o([],re,!0),[{style:"LINE_THROUGH",shortcut:["ctrl","shift","X"],handler:Yt},{style:"SUPERSCRIPT",shortcut:["ctrl","shift","+"],handler:ee},{style:"SUBSCRIPT",shortcut:["ctrl","="],handler:te},{style:"text-align-right",shortcut:["ctrl","shift","R"],handler:function(t,e){return at(t,e,"text-align-right")}},{style:"text-align-center",shortcut:["ctrl","shift","E"],handler:function(t,e){return at(t,e,"text-align-center")}},{style:"text-align-left",shortcut:["ctrl","shift","L"],handler:function(t,e){return at(t,e,"text-align-left")}},{style:"text-align-justify",shortcut:["ctrl","shift","J"],handler:function(t,e){return at(t,e,"text-align-justify")}},{style:"OL",shortcut:["ctrl","shift","&"],handler:ut},{style:"UL",shortcut:["ctrl","shift","*"],handler:ct},{style:"blockquote",shortcut:["ctrl","shift","("],handler:$},{style:"unstyled",shortcut:["ctrl","\\"],handler:mt}],!1);export{b as Editor,k as addAtomicBlock,B as addAudio,D as addFile,K as addImage,ft as addLink,N as addVideo,w as blockRendererFn,M as blockStyleFn,mt as clear,vt as clearBlockTypes,Ct as clearEntities,Rt as clearInlineStyles,gt as createDecorator,xt as emptyRawContentState,ht as findEntitiesOf,dt as findEntitiesOfLink,Tt as focusOnEditor,pt as generateKey,Ut as getAtomicCount,Lt as getAudioCount,Ot as getBlockType,bt as getCharCount,ie as getDefaultKeyBindingFn,kt as getEntityCount,Bt as getFileCount,Dt as getImageCount,Kt as getLinkCount,Nt as getPlainText,Et as getSelectedBlocksMap,Pt as getVideoCount,At as getWordArray,wt as getWordCount,T as hasBlockTypeOf,Mt as hasInlineStyle,Ht as hasInlineStyleOf,re as initial,P as initialAtomicTypes,j as initialBlockStyle,H as initialBlockTypes,_t as initialStyleMap,It as inlineStyles,jt as insertSoftNewline,U as isBlockquote,qt as isBold,_ as isH1,q as isH2,F as isH3,G as isH4,V as isH5,J as isH6,Ft as isItalic,Gt as isLineThrough,St as isLink,L as isOL,Vt as isOverline,z as isParagraph,Jt as isSub,zt as isSup,W as isTextCenterAligned,X as isTextJustifyAligned,Z as isTextLeftAligned,Q as isTextRightAligned,O as isUL,Wt as isUnderline,yt as removeLink,oe as shortcutHandler,ue as shortcutsMap,Y as toggleBlockType,$ as toggleBlockquote,Xt as toggleBold,tt as toggleH1,et as toggleH2,nt as toggleH3,rt as toggleH4,it as toggleH5,ot as toggleH6,Zt as toggleInlineStyle,Qt as toggleItalic,Yt as toggleLineThrough,ut as toggleOL,$t as toggleOverline,lt as toggleParagraph,te as toggleSub,ee as toggleSup,at as toggleTextAlign,ct as toggleUL,ne as toggleUnderline,A as updateAtomicData,st as withDecoratorComponent};
//# sourceMappingURL=index.esm.js.map