UNPKG

@atlaskit/adf-schema

Version:

Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs

94 lines (89 loc) 2.08 kB
import { rule as ruleFactory } from '../../next-schema/generated/nodeTypes'; import { uuid } from '../../utils/uuid'; /** * @name rule_node */ /** * @name rule_root_only_node */ /** * @name rule_with_attrs_node */ /** * @name rule_with_attrs_root_only_node */ const hrDOM = ['hr']; export const rule = ruleFactory({ parseDOM: [{ tag: 'hr' }], toDOM() { return hrDOM; } }); export const ruleWithLocalId = ruleFactory({ parseDOM: [{ tag: 'hr', getAttrs: () => ({ localId: uuid.generate() }) }], toDOM(node) { var _node$attrs; return ['hr', { 'data-local-id': (node === null || node === void 0 ? void 0 : (_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.localId) || undefined }]; } }); const getAttrs = domNode => { // eslint-disable-next-line @atlaskit/editor/no-as-casting const dom = domNode; const weight = dom.getAttribute('data-weight'); return { color: dom.getAttribute('data-color') || undefined, localId: dom.getAttribute('data-local-id') || undefined, style: dom.getAttribute('data-style') || undefined, weight: weight === null || weight === '' ? undefined : Number(weight) }; }; export const ruleWithAttrs = { ...ruleWithLocalId, attrs: { ...ruleWithLocalId.attrs, color: { default: null }, style: { default: null }, weight: { default: null } }, parseDOM: [{ tag: 'hr', getAttrs }], toDOM(node) { const { color, localId, style, weight } = node.attrs; return ['hr', { 'data-color': color || undefined, 'data-local-id': localId || undefined, 'data-style': style || undefined, 'data-weight': weight !== null && weight !== void 0 ? weight : undefined }]; } }; export const ruleRootOnlyStage0 = { ...ruleWithLocalId, marks: 'breakout unsupportedMark unsupportedNodeAttribute' }; export const ruleWithAttrsRootOnlyStage0 = { ...ruleWithAttrs, marks: 'breakout unsupportedMark unsupportedNodeAttribute' };