Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const getPossibleStandardName = (key: string): string => {
if (!hasOwnProperty(possibleStandardNames, key)) {
return key;
}
return possibleStandardNames[key] || key;
};
export const offsetTags = (tags: Tags, offset: number): Tags => {
const result: Tags = Object.create(null);
for (const name in tags) {
if (hasOwnProperty(tags, name)) {
result[name] = tags[name] + offset;
}
}
return result;
};
export const nodeFactory = ({
name,
schema,
attrs,
marks,
}: NodeFactoryParams) => {
const nodeBuilder = hasOwnProperty(schema.nodes, name) ? schema.nodes[name] : undefined;
if (!nodeBuilder) {
throw new Error(
`Node: "${name}" doesn't exist in schema. It's usually caused by lacking of the extension that contributes this node. Schema contains following nodes: ${Object.keys(
schema.nodes,
).join(', ')}`,
);
}
return (...content: TaggedContentWithText[]): TaggedProsemirrorNode => {
const { nodes, tags } = coerce({ content, schema });
const node = nodeBuilder.createChecked(attrs, nodes, marks) as TaggedProsemirrorNode;
node.tags = tags;
return node;
};
};
export const gatherToDOM = (
specs: Record,
) => {
const result: Record = Object.create(null);
for (const name in specs) {
if (!hasOwnProperty(specs, name)) {
continue;
}
const toDOM = specs[name].toDOM;
if (toDOM) {
result[name] = toDOM;
}
}
return result;
};
export const markFactory = ({ name, schema, attrs, allowDupes = false }: MarkFactoryParams) => {
const markBuilder = hasOwnProperty(schema.marks, name) ? schema.marks[name] : undefined;
if (!markBuilder) {
throw new Error(
`Mark: "${name}" doesn't exist in schema. It's usually caused by lacking of the extension that contributes this mark. Schema contains following marks: ${Object.keys(
schema.marks,
).join(', ')}`,
);
}
return (...content: TaggedContentWithText[]): TaggedProsemirrorNode[] => {
const mark = markBuilder.create(attrs);
const { nodes } = coerce({ content, schema });
return nodes.map(node => {
if (!allowDupes && mark.type.isInSet(node.marks)) {
return node;
} else {
const taggedNode = node.mark(mark.addToSet(node.marks)) as TaggedProsemirrorNode;
export const mapProps = (props: PlainObject) => {
const transformedProps: PlainObject = Object.create(null);
for (const key in props) {
if (!hasOwnProperty(props, key)) {
continue;
}
const name = getPossibleStandardName(key);
transformedProps[name] = props[key];
if (name === 'contentEditable') {
transformedProps.suppressContentEditableWarning = true;
}
}
return transformedProps;
};