json-joy
Version:
Collection of libraries for building collaborative editing apps.
52 lines (51 loc) • 2.57 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.RenderInline = void 0;
const tslib_1 = require("tslib");
// biome-ignore lint: React is used for JSX
const React = tslib_1.__importStar(require("react"));
const react_1 = require("../../../web/react");
const hooks_1 = require("../../../web/react/hooks");
const constants_1 = require("../constants");
const json_crdt_extensions_1 = require("../../../../json-crdt-extensions");
const Spoiler_1 = require("./Spoiler");
const RenderInlineSelection = (props) => {
const { children, selection } = props;
const { dom } = (0, react_1.usePeritext)();
const focus = (0, hooks_1.useSyncStoreOpt)(dom?.cursor.focus) || false;
const [left, right] = selection;
const style = {
backgroundColor: focus ? constants_1.DefaultRendererColors.ActiveSelection : constants_1.DefaultRendererColors.InactiveSelection,
borderRadius: left === 'anchor' ? '.25em 1px 1px .25em' : right === 'anchor' ? '1px .25em .25em 1px' : '1px',
};
return React.createElement("span", { style: style }, children);
};
const RenderInline = (props) => {
const { inline, children } = props;
const attr = inline.attr();
const selection = inline.selection();
let element = children;
if (attr[json_crdt_extensions_1.CommonSliceType.code])
element = React.createElement("code", null, element);
if (attr[json_crdt_extensions_1.CommonSliceType.mark])
element = React.createElement("mark", null, element);
if (attr[json_crdt_extensions_1.CommonSliceType.del])
element = React.createElement("del", null, element);
if (attr[json_crdt_extensions_1.CommonSliceType.ins])
element = React.createElement("ins", null, element);
if (attr[json_crdt_extensions_1.CommonSliceType.sup])
element = React.createElement("sup", null, element);
if (attr[json_crdt_extensions_1.CommonSliceType.sub])
element = React.createElement("sub", null, element);
if (attr[json_crdt_extensions_1.CommonSliceType.math])
element = React.createElement("code", null, element);
if (attr[json_crdt_extensions_1.CommonSliceType.kbd])
element = React.createElement("kbd", null, element);
if (attr[json_crdt_extensions_1.CommonSliceType.spoiler])
element = React.createElement(Spoiler_1.Spoiler, null, element);
if (selection) {
element = (React.createElement(RenderInlineSelection, { ...props, selection: selection }, element));
}
return element;
};
exports.RenderInline = RenderInline;
;