@vericus/slate-kit-align
Version:
Plugins to provide a way to align blocks on slate
2 lines (1 loc) • 7.06 kB
JavaScript
!function(t,e){if("function"==typeof define&&define.amd)define(["exports","@vericus/slate-kit-utils-register-helpers","immutable","slate"],e);else if("undefined"!=typeof exports)e(exports,require("@vericus/slate-kit-utils-register-helpers"),require("immutable"),require("slate"));else{var n={exports:{}};e(n.exports,t.slateKitUtilsRegisterHelpers,t.immutable,t.slate),t.unknown=n.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(t,e,n,r){var o;function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function a(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function u(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function c(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function l(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?c(Object(n),!0).forEach((function(e){u(t,e,n[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):c(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))}))}return t}function f(t){return(f=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function s(t,e){return(s=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function d(t,e){return!e||"object"!=typeof e&&"function"!=typeof e?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):e}function g(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}();return function(){var n,r=f(t);if(e){var o=f(this).constructor;n=Reflect.construct(r,arguments,o)}else n=r.apply(this,arguments);return d(this,n)}}function p(t){return function(t){if(Array.isArray(t))return y(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||function(t,e){if(!t)return;if("string"==typeof t)return y(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return y(t,e)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function y(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}t.__esModule=!0,t.default=void 0,e=(o=e)&&o.__esModule?o:{default:o};var b={floatBlocks:[],textBlocks:["paragraph","heading-one","heading-two","heading-three","heading-four","blockquote"],alignments:["left","right","center","justify"],dataField:"textAlign",label:"align"},h=function(t){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&s(t,e)}(u,t);var e,n,r,o=g(u);function u(){return i(this,u),o.apply(this,arguments)}return e=u,r=[{key:"create",value:function(t){return new u(l(l(l({},b),t),{},{floatBlocks:[].concat(p(b.floatBlocks),p(t.floatBlocks?t.floatBlocks:[])),alignments:[].concat(p(b.alignments),p(t.alignments?t.alignments:[])),textBlocks:[].concat(p(b.textBlocks),p(t.textBlocks?t.textBlocks:[]))}))}}],(n=null)&&a(e.prototype,n),r&&a(e,r),u}((0,n.Record)(b));function v(t,e){if(!e||!r.Block.isBlock(e))return[];var n=t.textBlocks,o=t.floatBlocks;return e.isLeafBlock()?n.includes(e.type)||o.includes(e.type)?[e]:[]:e.nodes.reduce((function(e,n){return[].concat(p(e),p(v(t,n)))}),[])}function m(t){return{getAlignBlocks:function(e){return function(t,e){var r=e.getHighestSelectedBlocks();return 0===r.size?[]:(0,n.List)(r).reduce((function(e,n){return[].concat(p(e),p(v(t,n)))}),[])}(t,e)},getAlignBlocksInBlock:function(e,n){return v(t,n)},getAlignment:function(e,n){return function(t,e){var n=t.dataField;return e&&e.data.get(n)||"left"}(t,n)},isAligned:function(e,n){return function(t,e,n){var r=e.getAlignBlocks(t);return 0!==r.length&&r.every((function(t){return t&&e.getAlignment(t)===n}))}(t,e,n)},isAlignable:function(t){return function(t,e){return 0!==e.getAlignBlocks().length}(0,t)}}}function k(t){var e=t.dataField;return function(t,n){var r=t.value;t.withoutNormalizing((function(){t.getAlignBlocks(r).filter((function(t){return n&&t.data&&t.data.get(e)===n})).forEach((function(n){return t.setNodeByKey(n.key,{data:n.data.delete(e)})}))}))}}function O(t){var e=t.dataField;return function(n,r){var o=n.value,i=t.alignments;i&&i.includes(r)&&n.withoutNormalizing((function(){n.getAlignBlocks(o).forEach((function(t){n.setNodeByKey(t.key,{data:t.data.set(e,r)})}))}))}}function B(t){return{removeAlign:k(t),setAlign:O(t)}}function j(t){var e=t.floatBlocks,n=t.textBlocks;return function(t){if(!t.node||"block"!==t.node.object||!n.includes(t.node.type)&&!e.includes(t.node.type))return t;var r=t.editor.getAlignment(t.node),o=t.attributes&&t.attributes.style||{};return n.includes(t.node.type)&&(o=l(l({},o),{},{textAlign:r})),e.includes(t.node.type)&&(o=l(l({},o),{},{float:r})),l(l({},t),{},{attributes:l(l({},t.attributes),{},{style:o})})}}function A(t){var e=t.floatBlocks,n=t.textBlocks,o=t.dataField,i=t.alignments;return{blocks:[].concat(p(e),p(n)).reduce((function(t,e){return l(l({},t),{},u({},e,{data:u({},o,(function(t){return!t||t&&i.includes(t)})),normalize:function(t,e){"node_data_invalid"===e.code&&t.withoutNormalizing((function(){r.Text.isText(e.node)||t.setNodeByKey(e.node.key,{data:e.node.data.delete(o)})}))}}))}),{})}}var w=[].concat(["div","p","h1","h2","h3","h4","h5","h6"],["div","p","h1","h2","h3","img"]),x=["left","center","right","justify"];function P(t){var e=t.dataField;return{getData:function(t,n){return function(t,e){if(!e||!t.tagName||!w.includes(t.tagName.toLowerCase()))return{};if(t.style&&"string"==typeof t.style.textAlign){var n=t.style.textAlign;if(n&&x.includes(n))return{data:u({},e,n)}}if(t.getAttribute("align")){var r=t.getAttribute("align");if(r&&x.includes(r))return{data:u({},e,r)}}if(t.style&&"string"==typeof t.style.cssFloat&&"clear"!==t.style.cssFloat){var o=t.style.cssFloat;if(o&&x.includes(o))return{data:u({},e,o)}}return{}}(n,e)}}}var _=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=h.create(t),r=m(n),o=B(n),i=j(n),a=A(n),u=P(n),c=u.getData;return[(0,e.default)({props:i,getData:c,options:n}),{queries:r,commands:o,schema:a}]};t.default=_}));