UNPKG

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) 15.1 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react/jsx-runtime"),e=require("react"),n=require("draft-js");function r(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}function o(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,Object.freeze(e)}var i=o(e),l=r(n),u=function(){return u=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},u.apply(this,arguments)};function a(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 o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}function c(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}function s(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}"function"==typeof SuppressedError&&SuppressedError;var g,f={exports:{}}; /*! Copyright (c) 2018 Jed Watson. Licensed under the MIT License (MIT), see http://jedwatson.github.io/classnames */g=f,function(){var t={}.hasOwnProperty;function e(){for(var n=[],r=0;r<arguments.length;r++){var o=arguments[r];if(o){var i=typeof o;if("string"===i||"number"===i)n.push(o);else if(Array.isArray(o)){if(o.length){var l=e.apply(null,o);l&&n.push(l)}}else if("object"===i){if(o.toString!==Object.prototype.toString&&!o.toString.toString().includes("[native code]")){n.push(o.toString());continue}for(var u in o)t.call(o,u)&&o[u]&&n.push(u)}}}return n.join(" ")}g.exports?(e.default=e,g.exports=e):window.classNames=e}();var p=s(f.exports),y={exports:{}};const d=l.default,{AtomicBlockUtils:h,CompositeDecorator:x,Editor:S,EditorState:E,Modifier:v,RichUtils:C,SelectionState:I,KeyBindingUtil:T,convertToRaw:R,convertFromRaw:O,genKey:m,getDefaultKeyBinding:L}=d;y.exports={AtomicBlockUtils:h,CompositeDecorator:x,Editor:S,EditorState:E,Modifier:v,RichUtils:C,SelectionState:I,KeyBindingUtil:T,convertToRaw:R,convertFromRaw:O,genKey:m,getDefaultKeyBinding:L},y.exports.DraftEntityMutability=d.DraftEntityMutability,y.exports.DraftDecorator=d.DraftDecorator;var k=y.exports,U=function(t,e){var n=t.getSelection();return t.getCurrentContent().getBlockForKey(n.getStartKey()).getType()===e},b=function(t){return U(t,"blockquote")},B=function(t){return U(t,"ordered-list-item")},A=function(t){return U(t,"unordered-list-item")},D=function(t,e,n,r,o){var i=t.getCurrentContent().createEntity(n,o||"IMMUTABLE",u({},r)),l=i.getLastCreatedEntityKey(),a=k.EditorState.set(t,{currentContent:i});return e(k.AtomicBlockUtils.insertAtomicBlock(a,l," ")),l},P={"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"},K=function(t,e,n){e(k.RichUtils.toggleBlockType(t,n))},j=function(t,e,n){return K(t,e,n||"blockquote")},N=function(t,e,n){return K(t,e,n||"ordered-list-item")},H=function(t,e,n){return K(t,e,n||"text-align-right")},w=function(t,e,n){return K(t,e,n||"unordered-list-item")},M=function(e){return function(n){var r=n.contentState;n.blockKey,n.decoratedText;var o=n.entityKey,i=n.offsetKey,l=n.end,c=n.start,s=a(n,["contentState","blockKey","decoratedText","entityKey","offsetKey","end","start"]),g=r.getEntity(o),f=g.getType(),p=g.getMutability,y=g.getData();return t.jsx(e,u({blockType:f,mutability:p,entityKey:o,offsetKey:i,end:l,start:c},s,y))}},_=function(t){return function(e,n,r){e.findEntityRanges((function(e){var n=e.getEntity();return null!==n&&r.getEntity(n).getType()===t}),n)}},q=_("link"),F=function(t){var e=t.getCurrentContent().getBlockMap(),n=t.getSelection(),r=n.getStartKey(),o=n.getEndKey();return r===o?[e.get(r)]:e.toSeq().skipUntil((function(t,e){return e===r})).takeUntil((function(t,e){return e===o})).toArray()},G=function(t,e){var n=t.getCurrentContent(),r=F(t).reduce((function(t,e){if("unstyled"!==e.getType()){var n=k.SelectionState.createEmpty(e.getKey()).merge({focusOffset:0,anchorOffset:e.getText().length});return k.Modifier.setBlockType(t,n,"unstyled")}return t}),n),o=k.EditorState.push(t,r,"change-block-type");return e(o),o},V=function(t,e){var n=t.getCurrentContent(),r=t.getSelection(),o=k.Modifier.applyEntity(n,r,null),i=k.EditorState.push(t,o,"apply-entity");return e(i),i},J={BOLD:"BOLD",ITALIC:"ITALIC",UNDERLINE:"UNDERLINE",LINE_THROUGH:"LINE_THROUGH",OVERLINE:"OVERLINE",SUPERSCRIPT:"SUPERSCRIPT",SUBSCRIPT:"SUBSCRIPT"},W=function(t,e,n){var r=t.getCurrentContent(),o=t.getSelection(),i=Object.keys(J);n&&n.forEach((function(t){i.includes(t)||i.push(t)}));var l=i.reduce((function(t,e){return k.Modifier.removeInlineStyle(t,o,e)}),r),u=k.EditorState.push(t,l,"change-inline-style");return e(u),u},z=function(t,e){return[W,G,V].reduce((function(t,n){return n(t,e)}),t)},X=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},Z=function(t,e){var n=0,r=t.getCurrentContent(),o=Object.values(k.convertToRaw(r).entityMap);return o.length>0&&o.forEach((function(t){t.type===e&&n++})),n},Q=function(t,e){var n=k.RichUtils.insertSoftNewline(t);return e(n),n},Y=function(t,e,n){e(k.RichUtils.toggleInlineStyle(t,n||"BOLD"))},$=function(t,e,n){e(k.RichUtils.toggleInlineStyle(t,n||"ITALIC"))},tt=function(t,e,n){e(k.RichUtils.toggleInlineStyle(t,n||"LINE_THROUGH"))},et=function(t,e,n){e(k.RichUtils.toggleInlineStyle(t,n||"SUBSCRIPT"))},nt=function(t,e,n){e(k.RichUtils.toggleInlineStyle(t,n||"SUPERSCRIPT"))},rt=function(t,e,n){e(k.RichUtils.toggleInlineStyle(t,n||"UNDERLINE"))},ot=[{style:"BOLD",shortcut:["ctrl","b"],handler:Y},{style:"ITALIC",shortcut:["ctrl","i"],handler:$},{style:"UNDERLINE",shortcut:["ctrl","u"],handler:rt},{style:"SOFT_NEW_LINE",shortcut:function(t){return k.KeyBindingUtil.isSoftNewlineEvent(t)?"SOFT_NEW_LINE":""},handler:Q}],it=c(c([],ot,!0),[{style:"LINE_THROUGH",shortcut:["ctrl","shift","X"],handler:tt},{style:"SUPERSCRIPT",shortcut:["ctrl","shift","+"],handler:nt},{style:"SUBSCRIPT",shortcut:["ctrl","="],handler:et},{style:"text-align-right",shortcut:["ctrl","shift","R"],handler:function(t,e){return H(t,e,"text-align-right")}},{style:"text-align-center",shortcut:["ctrl","shift","E"],handler:function(t,e){return H(t,e,"text-align-center")}},{style:"text-align-left",shortcut:["ctrl","shift","L"],handler:function(t,e){return H(t,e,"text-align-left")}},{style:"text-align-justify",shortcut:["ctrl","shift","J"],handler:function(t,e){return H(t,e,"text-align-justify")}},{style:"OL",shortcut:["ctrl","shift","&"],handler:N},{style:"UL",shortcut:["ctrl","shift","*"],handler:w},{style:"blockquote",shortcut:["ctrl","shift","("],handler:j},{style:"unstyled",shortcut:["ctrl","\\"],handler:z}],!1);exports.Editor=function(e){var n=e.editorRef,r=e.placeholder,o=e.rtlPlaceholder,l=e.textDirectionality,c=a(e,["editorRef","placeholder","rtlPlaceholder","textDirectionality"]),s=i.useState(!1),g=s[0],f=s[1],y=i.useCallback((function(){return!(b(e.editorState)||B(e.editorState)||A(e.editorState))}),[e.editorState]);i.useEffect((function(){f(y())}),[y]);var d=function(){return e.editorState.getCurrentContent().hasText()};return t.jsx("div",u({className:p({"rtl-placeholder":o&&!d()})||void 0},{children:t.jsx(k.Editor,u({ref:n,placeholder:g&&r?r:void 0,textDirectionality:l||(o&&!d()?"RTL":void 0)},c))}))},exports.addAtomicBlock=D,exports.addAudio=function(t,e,n){return D(t,e,"audio",n)},exports.addFile=function(t,e,n){return D(t,e,"file",n)},exports.addImage=function(t,e,n){return D(t,e,"image",n)},exports.addLink=function(t,e,n,r){var o=t.getCurrentContent(),i=o.createEntity(null!=r?r:"link","IMMUTABLE",n),l=o.getLastCreatedEntityKey(),u=k.EditorState.set(t,{currentContent:i}),a=u.getSelection();e(k.RichUtils.toggleLink(u,a,l))},exports.addVideo=function(t,e,n){return D(t,e,"video",n)},exports.blockRendererFn=function(e,n){return function(r){if("atomic"===r.getType())return{component:function(n){var r=n.contentState.getEntity(n.block.getEntityAt(0)),o=n.block.getEntityAt(0),i=r.getData(),l=r.getMutability(),a=r.getType();return t.jsx(e,u({},i,{blockType:a,mutability:l,entityKey:o}))},editable:!1};n&&n(r)}},exports.blockStyleFn=function(t,e){var n=t.getType(),r=P;return e&&(null==e?void 0:e.length)>0&&e.forEach((function(t){return r=u(u({},r),t)})),r[n||"unstyled"]},exports.clear=z,exports.clearBlockTypes=G,exports.clearEntities=V,exports.clearInlineStyles=W,exports.createDecorator=function(t){var e=[];return t.forEach((function(t){return e.push({strategy:t.strategy,component:M(t.component),props:t.props})})),new k.CompositeDecorator(e)},exports.emptyRawContentState={entityMap:{},blocks:[{text:"",key:"LG6AABOZOBAMJ",type:"unstyled",entityRanges:[],depth:0,inlineStyleRanges:[]}]},exports.findEntitiesOf=_,exports.findEntitiesOfLink=q,exports.focusOnEditor=function(t){var e;t&&(null===(e=t.current)||void 0===e||e.focus())},exports.generateKey=function(){var t=Date.now().toString(36),e=Math.random().toString(36).substring(2,7);return"".concat(t).concat(e).toUpperCase()},exports.getAtomicCount=X,exports.getAudioCount=function(t){return X(t,"audio")},exports.getBlockType=function(t){var e=t.getSelection();return t.getCurrentContent().getBlockForKey(e.getStartKey()).getType()},exports.getCharCount=function(t){var e=t.getCurrentContent().getPlainText().match(/\S+/g);return e?e.join("").length:0},exports.getDefaultKeyBindingFn=function(t,e){"Alt"===t.key&&t.preventDefault();for(var n=0,r=e||ot;n<r.length;n++){var o=r[n];if(Array.isArray(o.shortcut)){if(o.shortcut.every((function(e,n){return!(!["ctrl","control","cmd","command"].includes(e.toLocaleLowerCase())||!k.KeyBindingUtil.hasCommandModifier(t))||(!("alt"!==e.toLocaleLowerCase()&&"option"!==e.toLocaleLowerCase()||!k.KeyBindingUtil.isOptionKeyCommand(t))||("shift"===e.toLocaleLowerCase()?t.shiftKey:e===t.key))})))return o.style}else if(o.shortcut(t))return o.shortcut(t)}return k.getDefaultKeyBinding(t)},exports.getEntityCount=Z,exports.getFileCount=function(t){return X(t,"file")},exports.getImageCount=function(t){return X(t,"image")},exports.getLinkCount=function(t){return Z(t,"link")},exports.getPlainText=function(t){if("string"==typeof t){var e=[];try{var n=JSON.parse(t);if(n&&n.blocks)for(var r=0,o=n.blocks;r<o.length;r++){var i=o[r];i&&i.text&&e.push(i.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()},exports.getSelectedBlocksMap=F,exports.getVideoCount=function(t){return X(t,"video")},exports.getWordArray=function(t){var e=t.getCurrentContent().getPlainText().match(/\S+/g);return e||[]},exports.getWordCount=function(t){var e=t.getCurrentContent().getPlainText().match(/\S+/g);return e?e.length:0},exports.hasBlockTypeOf=U,exports.hasInlineStyle=function(t){return!t.getCurrentInlineStyle().isEmpty()},exports.hasInlineStyleOf=function(t,e){return t.getCurrentInlineStyle().has(e)},exports.initial=ot,exports.initialAtomicTypes={image:"image",video:"video",audio:"audio",file:"file"},exports.initialBlockStyle=P,exports.initialBlockTypes={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"},exports.initialStyleMap={LINE_THROUGH:{textDecoration:"line-through"},OVERLINE:{textDecoration:"overline"},SUPERSCRIPT:{verticalAlign:"top",fontSize:"0.75em"},SUBSCRIPT:{verticalAlign:"sub",fontSize:"0.75em"}},exports.inlineStyles=J,exports.insertSoftNewline=Q,exports.isBlockquote=b,exports.isBold=function(t,e){return t.getCurrentInlineStyle().has(e||"BOLD")},exports.isH1=function(t){return U(t,"header-one")},exports.isH2=function(t){return U(t,"header-two")},exports.isH3=function(t){return U(t,"header-three")},exports.isH4=function(t){return U(t,"header-four")},exports.isH5=function(t){return U(t,"header-five")},exports.isH6=function(t){return U(t,"header-six")},exports.isItalic=function(t,e){return t.getCurrentInlineStyle().has(e||"ITALIC")},exports.isLineThrough=function(t,e){return t.getCurrentInlineStyle().has(e||"LINE_THROUGH")},exports.isLink=function(t,e){var n=t.getSelection(),r=t.getCurrentContent(),o=r.getBlockForKey(n.getStartKey()).getEntityAt(n.getStartOffset());return!!o&&r.getEntity(o).getType()===(null!=e?e:"link")},exports.isOL=B,exports.isOverline=function(t,e){return t.getCurrentInlineStyle().has(e||"OVERLINE")},exports.isParagraph=function(t){return U(t,"paragraph")||U(t,"unstyled")},exports.isSub=function(t,e){return t.getCurrentInlineStyle().has(e||"SUBSCRIPT")},exports.isSup=function(t,e){return t.getCurrentInlineStyle().has(e||"SUPERSCRIPT")},exports.isTextCenterAligned=function(t,e){return U(t,e||"text-align-center")},exports.isTextJustifyAligned=function(t,e){return U(t,e||"text-align-justify")},exports.isTextLeftAligned=function(t,e){return U(t,e||"text-align-left")},exports.isTextRightAligned=function(t,e){return U(t,e||"text-align-right")},exports.isUL=A,exports.isUnderline=function(t,e){return t.getCurrentInlineStyle().has(e||"UNDERLINE")},exports.removeLink=function(t,e){var n=t.getSelection();n.isCollapsed()||e(k.RichUtils.toggleLink(t,n,null))},exports.shortcutHandler=function(t,e){return function(n,r){var o=e||ot,i=o.findIndex((function(t){return t.style===n}));return i>-1?(o[i].handler(r,t),"handled"):"not-handled"}},exports.shortcutsMap=it,exports.toggleBlockType=K,exports.toggleBlockquote=j,exports.toggleBold=Y,exports.toggleH1=function(t,e,n){return K(t,e,n||"header-one")},exports.toggleH2=function(t,e,n){return K(t,e,n||"header-two")},exports.toggleH3=function(t,e,n){return K(t,e,n||"header-three")},exports.toggleH4=function(t,e,n){return K(t,e,n||"header-four")},exports.toggleH5=function(t,e,n){return K(t,e,n||"header-five")},exports.toggleH6=function(t,e,n){return K(t,e,n||"header-six")},exports.toggleInlineStyle=function(t,e,n){e(k.RichUtils.toggleInlineStyle(t,n))},exports.toggleItalic=$,exports.toggleLineThrough=tt,exports.toggleOL=N,exports.toggleOverline=function(t,e,n){e(k.RichUtils.toggleInlineStyle(t,n||"OVERLINE"))},exports.toggleParagraph=function(t,e,n){return K(t,e,n||"paragraph")},exports.toggleSub=et,exports.toggleSup=nt,exports.toggleTextAlign=H,exports.toggleUL=w,exports.toggleUnderline=rt,exports.updateAtomicData=function(t,e,n,r){var o=t.getCurrentContent(),i=o.getEntity(n),l=u(u({},i.getData()),r),a=o.mergeEntityData(n,l);e(k.EditorState.push(t,a,"apply-entity"))},exports.withDecoratorComponent=M; //# sourceMappingURL=index.js.map