@activecollab/components
Version:
ActiveCollab Components
136 lines (131 loc) • 7.48 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PortableText = void 0;
var _react = _interopRequireWildcard(require("react"));
var _renderers = require("./renderers");
var _types = require("./types");
var _Stack = require("../Stack");
var _excluded = ["content", "components", "space"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
var defaultUnknownTypeHandler = function defaultUnknownTypeHandler(kind, type) {
console.warn("PortableText: no renderer for ".concat(kind, " type \"").concat(type, "\"."));
};
/**
* A span's marks wrap its text inside out. An unknown mark is reported and
* skipped — the text stays, because dropping a word would break the sentence.
*/
var renderSpan = function renderSpan(span, ctx) {
var _span$marks;
return ((_span$marks = span.marks) !== null && _span$marks !== void 0 ? _span$marks : []).reduce(function (children, mark) {
var _ctx$components$marks, _ctx$components;
var type = typeof mark === "string" ? mark : mark.type;
var Renderer = (_ctx$components$marks = (_ctx$components = ctx.components) === null || _ctx$components === void 0 || (_ctx$components = _ctx$components.marks) === null || _ctx$components === void 0 ? void 0 : _ctx$components[type]) !== null && _ctx$components$marks !== void 0 ? _ctx$components$marks : _renderers.portableTextDefaultMarkRenderers[type];
if (!Renderer) {
ctx.report("mark", type);
return children;
}
return /*#__PURE__*/_react.default.createElement(Renderer, {
mark: typeof mark === "string" ? undefined : mark
}, children);
}, span.text);
};
/**
* Traversal is structural, so the walker keeps no list of block types of its
* own: a `children` array holds spans or nested blocks, a nested `content`
* holds a node list, and anything else has no children to render.
*/
var renderBlockChildren = function renderBlockChildren(block, ctx) {
var _ref = block,
children = _ref.children,
content = _ref.content;
if (Array.isArray(children)) {
return children.map(function (child, index) {
return /*#__PURE__*/_react.default.createElement(_react.Fragment, {
key: index
}, (0, _types.isPortableTextSpan)(child) ? renderSpan(child, ctx) : (0, _types.isPortableTextBlock)(child) ? renderBlock(child, ctx) : null);
});
}
return (0, _types.isPortableTextContent)(content) ? renderNodes(content, ctx) : undefined;
};
var renderBlock = function renderBlock(block, ctx) {
var _ctx$components$block, _ctx$components2;
var type = block.type;
var Renderer = (_ctx$components$block = (_ctx$components2 = ctx.components) === null || _ctx$components2 === void 0 || (_ctx$components2 = _ctx$components2.blocks) === null || _ctx$components2 === void 0 ? void 0 : _ctx$components2[type]) !== null && _ctx$components$block !== void 0 ? _ctx$components$block : _renderers.portableTextDefaultBlockRenderers[type];
if (!Renderer) {
ctx.report("block", block.type);
return null;
}
// Blocks that carry spans outside `children` (table cells) render them
// through this engine-bound helper, so mark injection works there too.
var renderSpans = function renderSpans(spans) {
return spans.map(function (span, index) {
return /*#__PURE__*/_react.default.createElement(_react.Fragment, {
key: index
}, renderSpan(span, ctx));
});
};
return /*#__PURE__*/_react.default.createElement(Renderer, {
block: block,
renderSpans: renderSpans
}, renderBlockChildren(block, ctx));
};
var renderObject = function renderObject(node, ctx) {
var _ctx$components3;
var Renderer = (_ctx$components3 = ctx.components) === null || _ctx$components3 === void 0 || (_ctx$components3 = _ctx$components3.objects) === null || _ctx$components3 === void 0 ? void 0 : _ctx$components3[node._type];
if (!Renderer) {
ctx.report("object", node._type);
return null;
}
return /*#__PURE__*/_react.default.createElement(Renderer, {
node: node
}, (0, _types.isPortableTextContent)(node.content) ? renderNodes(node.content, ctx) : undefined);
};
var renderNodes = function renderNodes(nodes, ctx) {
return nodes.map(function (node, index) {
return /*#__PURE__*/_react.default.createElement(_react.Fragment, {
key: index
}, (0, _types.isPortableTextObjectNode)(node) ? renderObject(node, ctx) : renderBlock(node, ctx));
});
};
/**
* The Portable Text engine: walks a {@link PortableTextContent} envelope and
* renders it with the default renderers for the universal set, plus whatever
* the application injects through `components`. The engine itself is hollow —
* it knows no product element; an unknown block or object renders nothing and
* is reported once per type through `onUnknownType` (console.warn when the
* hook is absent).
*/
var PortableText = exports.PortableText = /*#__PURE__*/(0, _react.forwardRef)(function (_ref2, ref) {
var content = _ref2.content,
components = _ref2.components,
_ref2$space = _ref2.space,
space = _ref2$space === void 0 ? "md" : _ref2$space,
rest = _objectWithoutProperties(_ref2, _excluded);
// Memoized so unknown types are reported once per type for a given
// content/components pair, not again on every parent re-render.
var nodes = (0, _react.useMemo)(function () {
var reported = new Set();
var report = function report(kind, type) {
var _components$onUnknown;
var key = "".concat(kind, ":").concat(type);
if (reported.has(key)) return;
reported.add(key);
((_components$onUnknown = components === null || components === void 0 ? void 0 : components.onUnknownType) !== null && _components$onUnknown !== void 0 ? _components$onUnknown : defaultUnknownTypeHandler)(kind, type);
};
return renderNodes(content, {
components,
report
});
}, [content, components]);
return /*#__PURE__*/_react.default.createElement(_Stack.Stack, _extends({
ref: ref,
space: space
}, rest), nodes);
});
PortableText.displayName = "PortableText";
//# sourceMappingURL=PortableText.js.map