@gechiui/block-editor
Version:
76 lines (65 loc) • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = FormatEdit;
var _element = require("@gechiui/element");
var _richText = require("@gechiui/rich-text");
var _lodash = require("lodash");
/**
* GeChiUI dependencies
*/
/**
* External dependencies
*/
function FormatEdit(_ref) {
let {
formatTypes,
onChange,
onFocus,
value,
forwardedRef
} = _ref;
return formatTypes.map(settings => {
const {
name,
edit: Edit
} = settings;
if (!Edit) {
return null;
}
const activeFormat = (0, _richText.getActiveFormat)(value, name);
let isActive = activeFormat !== undefined;
const activeObject = (0, _richText.getActiveObject)(value);
const isObjectActive = activeObject !== undefined && activeObject.type === name; // Edge case: un-collapsed link formats.
// If there is a missing link format at either end of the selection
// then we shouldn't show the Edit UI because the selection has exceeded
// the bounds of the link format.
// Also if the format objects don't match then we're dealing with two separate
// links so we should not allow the link to be modified over the top.
if (name === 'core/link' && !(0, _richText.isCollapsed)(value)) {
const formats = value.formats;
const linkFormatAtStart = (0, _lodash.find)(formats[value.start], {
type: 'core/link'
});
const linkFormatAtEnd = (0, _lodash.find)(formats[value.end - 1], {
type: 'core/link'
});
if (!linkFormatAtStart || !linkFormatAtEnd || linkFormatAtStart !== linkFormatAtEnd) {
isActive = false;
}
}
return (0, _element.createElement)(Edit, {
key: name,
isActive: isActive,
activeAttributes: isActive ? activeFormat.attributes || {} : {},
isObjectActive: isObjectActive,
activeObjectAttributes: isObjectActive ? activeObject.attributes || {} : {},
value: value,
onChange: onChange,
onFocus: onFocus,
contentRef: forwardedRef
});
});
}
//# sourceMappingURL=format-edit.js.map