Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public createDomRef(): HTMLElement {
const { toDOM } = this.node.type.spec;
if (toDOM) {
const domSpec = toDOM(this.node);
if (isString(domSpec)) {
return document.createElement(domSpec);
}
if (isDOMNode(domSpec)) {
if (!isElementDOMNode(domSpec)) {
throw new Error('Invalid HTML Element provided in the DOM Spec');
}
return domSpec;
}
// Use the outer element string to render the dom node
return document.createElement(domSpec[0]);
}
return this.node.isInline ? document.createElement('span') : document.createElement('div');
}
apply: (tr, decorationSet) => {
// Map the decoration based on the changes to the document.
decorationSet = decorationSet.map(tr.mapping, tr.doc);
// Get tracker updates from the meta data
const tracker = getPluginMeta(key, tr);
if (isNullOrUndefined(tracker)) {
return decorationSet;
}
if (tracker.add) {
const { defaultClassName, defaultElement } = this.options;
const { className, element = defaultElement } = tracker.add;
const widget = isString(element) ? document.createElement(element) : element;
const classNames = className ? [defaultClassName, className] : [defaultClassName];
widget.classList.add(...classNames);
const deco = Decoration.widget(tracker.add.pos, widget, {
id: tracker.add.id,
type: name,
});
return decorationSet.add(tr.doc, [deco]);
}
if (tracker.remove) {
const { remove } = tracker;
const found = decorationSet.find(undefined, undefined, spec => spec.id === remove.id);
fallback = EMPTY_PARAGRAPH_NODE,
}: CreateDocumentNodeParams): ProsemirrorNode => {
if (isProsemirrorNode(content)) {
return content;
}
if (isObjectNode(content)) {
try {
return schema.nodeFromJSON(content);
} catch (e) {
console.error(e);
return fallbackContent({ fallback, schema });
}
}
if (isString(content) && stringHandler) {
return stringHandler({ doc, content, schema });
}
return fallbackContent({ fallback, schema });
};
export const cssValueUnits = (val: unknown): string | undefined =>
isString(val) ? val.replace(/[0-9 ]+/g, '') : undefined;
public setDomAttrs(node: ProsemirrorNode, element: HTMLElement) {
const { toDOM } = this.node.type.spec;
if (toDOM) {
const domSpec = toDOM(node);
if (isString(domSpec) || isDOMNode(domSpec)) {
return;
}
const attrs = domSpec[1];
if (isPlainObject(attrs)) {
keys(attrs).forEach(attr => {
element.setAttribute(attr, String(attrs[attr]));
});
return;
}
}
keys(node.attrs).forEach(attr => {
element.setAttribute(attr, node.attrs[attr]);
const createValidPercent = (value: number | string) => {
const percent = isString(value) ? Number(value.replace(/[%\s]+/g, '')) : value;
if (!isValidPercent(percent)) {
throw new Error(`Invalid 'percentage': ${value}`);
}
return percent;
};
const createValidAlpha = (value: number | string | undefined): Alpha => {
const alpha = isString(value)
? value.includes('%')
? Number(value.replace(/[%\s]+/g, ''))
: Number(value) * 100
: value;
if (isUndefined(alpha)) {
return createValidPercent(100);
}
return createValidPercent(alpha);
};
export const isExitReason = (value: unknown): value is ExitReason =>
isString(value) && Object.values(ExitReason).includes(value as ExitReason);
export const isChangeReason = (value: unknown): value is ChangeReason =>