@guestbell/react-page-plugins
Version:
Plugins we use in GuestBell for working with amazing react-page package
165 lines • 5.09 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { jsx } from 'slate-hyperscript';
import { Editor } from 'slate';
import { ParagraphType } from '../paragraph/withParagraph';
import { ListTypes } from '../lists/listTypes';
import { HeadingType } from '../heading/headingType';
import { QuoteType } from '../quote/quoteType';
import { LinkType } from '../links/linkType';
import { EmphasizeTypes } from '../emphasize/emphasizeTypes';
var ELEMENT_TAGS = {
A: function A(el) {
return {
type: LinkType,
url: el.getAttribute('href')
};
},
BLOCKQUOTE: function BLOCKQUOTE() {
return {
type: QuoteType
};
},
H1: function H1() {
return {
type: HeadingType,
level: 1
};
},
H2: function H2() {
return {
type: HeadingType,
level: 2
};
},
H3: function H3() {
return {
type: HeadingType,
level: 3
};
},
H4: function H4() {
return {
type: HeadingType,
level: 3
};
},
H5: function H5() {
return {
type: HeadingType,
level: 3
};
},
H6: function H6() {
return {
type: HeadingType,
level: 3
};
},
LI: function LI() {
return {
type: ListTypes.LI
};
},
OL: function OL() {
return {
type: ListTypes.OL
};
},
P: function P() {
return {
type: ParagraphType
};
},
UL: function UL() {
return {
type: ListTypes.UL
};
}
};
var MARK_TAGS = {
EM: function EM() {
return _defineProperty({}, EmphasizeTypes.Italic, true);
},
I: function I() {
return _defineProperty({}, EmphasizeTypes.Italic, true);
},
STRONG: function STRONG() {
return _defineProperty({}, EmphasizeTypes.Bold, true);
},
U: function U() {
return _defineProperty({}, EmphasizeTypes.Underline, true);
}
};
var checkEmpty = function checkEmpty(potentialString) {
return !(potentialString && typeof potentialString === 'string' && potentialString.length > 0 && !/^(\r\n|\r|\n)$/.test(potentialString));
};
var _deserialize = function deserialize(el) {
if (el.nodeType === 3) {
return el.textContent && el.textContent.replace('\n', '');
} else if (el.nodeType !== 1) {
return null;
} else if (el.nodeName === 'BR') {
return '\n';
}
var nodeName = el.nodeName;
var parent = el;
var children = Array.from(parent.childNodes).map(_deserialize);
if (el.nodeName === 'BODY') {
if (!checkEmpty(children[0])) {
children.slice(1);
}
if (checkEmpty(children[children.length - 1])) {
children.pop();
}
return jsx('fragment', {}, children);
}
if (ELEMENT_TAGS[nodeName]) {
var attrs = ELEMENT_TAGS[nodeName](el);
return jsx('element', attrs, children);
}
if (MARK_TAGS[nodeName]) {
var _attrs = MARK_TAGS[nodeName](el);
return children.map(function (child) {
return jsx('text', _attrs, child);
});
}
return children;
};
export { _deserialize as deserialize };
export var imageType = 'image';
export var withHtml = function withHtml(editor) {
var isInline = editor.isInline,
isVoid = editor.isVoid,
insertData = editor.insertData;
editor.isInline = function (element) {
return element.type === LinkType ? true : isInline(element);
};
editor.isVoid = function (element) {
return element.type === 'image' ? true : isVoid(element);
};
editor.insertData = function (data) {
var html = data.getData('text/html');
if (html) {
var parsed = new DOMParser().parseFromString(html, 'text/html');
var fragment = _deserialize(parsed.body);
var selection = editor.selection;
var selectedNode = Editor.node(editor, selection)[0];
var selectedNodeEmpty = selectedNode && selectedNode.text !== undefined && selectedNode.text.length === 0 ? true : false;
if (!selectedNodeEmpty && fragment.length > 1) {
fragment = fragment.slice(1);
}
Editor.insertFragment(editor, fragment);
if (selectedNodeEmpty && fragment.length > 1) {
editor.selection = selection;
editor.deleteFragment();
}
return;
}
insertData(data);
};
return editor;
};
//# sourceMappingURL=withHtmlPaste.js.map