slate-react-placeholder
Version:
A Slate plugin to render a placeholder with React.
2 lines (1 loc) • 2.15 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t(e.SlateReactPlaceholder={},e.React)}(this,function(e,t){"use strict";t=t&&t.hasOwnProperty("default")?t.default:t;var r=function(e,t){if(!e)throw new Error("Invariant failed")},n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},a=function(){return function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,t){var r=[],n=!0,a=!1,o=void 0;try{for(var i,c=e[Symbol.iterator]();!(n=(i=c.next()).done)&&(r.push(i.value),!t||r.length!==t);n=!0);}catch(e){a=!0,o=e}finally{try{!n&&c.return&&c.return()}finally{if(a)throw o}}return r}(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),o=function(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)},i=0;e.default=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},c=i++,l=e.placeholder,u=e.when,f=e.style,d=void 0===f?{}:f;return r("string"==typeof l,"You must pass `SlateReactPlaceholder` an `options.placeholder` string."),r("string"==typeof u||"function"==typeof u,"You must pass `SlateReactPlaceholder` an `options.when` query."),{decorateNode:function(e,t,r){if(!t.query(u,e))return r();var n=r(),i=e.texts(),l=a(i,1)[0],f=e.texts({direction:"backward"}),d=a(f,1)[0],s=a(l,2),p=s[0],y=s[1],h=a(d,2),v=h[0],w=h[1],g={type:"placeholder",data:{key:c},anchor:{key:p.key,offset:0,path:y},focus:{key:v.key,offset:v.text.length,path:w}};return[].concat(o(n),[g])},renderDecoration:function(e,r,a){var o=e.children,i=e.decoration;if("placeholder"===i.type&&i.data.get("key")===c){var u=n({pointerEvents:"none",display:"inline-block",width:"0",maxWidth:"100%",whiteSpace:"nowrap",opacity:"0.333",verticalAlign:"text-top"},d);return t.createElement("span",null,t.createElement("span",{contentEditable:!1,style:u},l),o)}return a()}}},Object.defineProperty(e,"__esModule",{value:!0})});