UNPKG

@atlaskit/adf-schema

Version:

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

121 lines (117 loc) 2.87 kB
import { expand as expandFactory } from '../../next-schema/generated/nodeTypes'; import { uuid } from '../../utils/uuid'; /** * @name expand_node */ /** * @name expand_with_no_mark_node */ /** * @name expand_root_only_node */ function getExpandAttrs(domNode) { const dom = domNode; return { title: dom.getAttribute('data-title'), __expanded: true }; } const nodeSpecOptions = { parseDOM: [{ context: 'table//', tag: 'div[data-node-type="expand"]', getAttrs: getExpandAttrs }, { context: 'expand//', tag: '[data-node-type="expand"]', getAttrs: getExpandAttrs }, { context: 'nestedExpand//', tag: '[data-node-type="expand"]', getAttrs: getExpandAttrs }, { tag: '[data-node-type="nestedExpand"] button', ignore: true }, { tag: '[data-node-type="expand"] button', ignore: true }, { tag: 'div[data-node-type="expand"]', getAttrs: getExpandAttrs }], toDOM(node) { const attrs = { 'data-node-type': 'expand', 'data-title': node.attrs.title, 'data-expanded': node.attrs.__expanded }; return ['div', attrs, 0]; } }; export const expandWithNestedExpand = expandFactory(nodeSpecOptions); export const expandWithNestedExpandLocalId = expandFactory({ parseDOM: [{ context: 'table//', tag: 'div[data-node-type="expand"]', getAttrs: dom => { const attrs = getExpandAttrs(dom); return { ...attrs, localId: uuid.generate() }; } }, { context: 'expand//', tag: '[data-node-type="expand"]', getAttrs: dom => { const attrs = getExpandAttrs(dom); return { ...attrs, localId: uuid.generate() }; } }, { context: 'nestedExpand//', tag: '[data-node-type="expand"]', getAttrs: dom => { const attrs = getExpandAttrs(dom); return { ...attrs, localId: uuid.generate() }; } }, { tag: '[data-node-type="nestedExpand"] button', ignore: true }, { tag: '[data-node-type="expand"] button', ignore: true }, { tag: 'div[data-node-type="expand"]', getAttrs: dom => { const attrs = getExpandAttrs(dom); return { ...attrs, localId: uuid.generate() }; } }], toDOM(node) { var _node$attrs; const attrs = { 'data-node-type': 'expand', 'data-title': node.attrs.title, 'data-expanded': node.attrs.__expanded, '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 }; return ['div', attrs, 0]; } }); export const toJSON = node => ({ attrs: Object.keys(node.attrs).filter(key => !key.startsWith('__')).reduce((obj, key) => { return { ...obj, [key]: node.attrs[key] }; }, {}) });