@atlaskit/renderer
Version:
Renderer component
39 lines • 1.09 kB
JavaScript
import React from 'react';
export const findTextString = reactNode => {
let result = null;
const children = React.Children.toArray(reactNode);
for (const childNode of children) {
if (result) {
break;
} else if (typeof childNode === 'string') {
result = childNode;
} else if (isReactElement(childNode) && childNode.props.children) {
result = findTextString(childNode.props.children);
}
}
return result;
};
function isReactElement(child) {
return !!child.type;
}
export const splitText = (text, {
startOffset,
endOffset
}) => {
if (endOffset > text.length || endOffset - startOffset <= 0) {
return null;
}
return [text.slice(0, startOffset), text.slice(startOffset, endOffset), text.slice(endOffset)].filter(Boolean);
};
export const calcTextSplitOffset = (position, textPosition, text) => {
const {
start,
end
} = textPosition;
const startOffset = Math.max(position.from - start, 0);
const endOffset = Math.min(Math.abs(end - position.to - text.length), text.length);
return {
startOffset,
endOffset
};
};