UNPKG

@atlaskit/adf-schema

Version:

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

76 lines (73 loc) 3.04 kB
import { panel as panelFactory } from '../../next-schema/generated/nodeTypes'; import { uuid } from '../../utils/uuid'; export let PanelType = /*#__PURE__*/function (PanelType) { PanelType["INFO"] = "info"; PanelType["NOTE"] = "note"; PanelType["TIP"] = "tip"; PanelType["WARNING"] = "warning"; PanelType["ERROR"] = "error"; PanelType["SUCCESS"] = "success"; PanelType["CUSTOM"] = "custom"; return PanelType; }({}); /** * @name panel_node */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const getDomAttrs = nodeAttrs => { const attrs = { 'data-panel-type': nodeAttrs.panelType, 'data-panel-icon': nodeAttrs.panelIcon, 'data-panel-icon-id': nodeAttrs.panelIconId, 'data-panel-icon-text': nodeAttrs.panelIconText, 'data-panel-color': nodeAttrs.panelColor, 'data-local-id': (nodeAttrs === null || nodeAttrs === void 0 ? void 0 : nodeAttrs.localId) || undefined }; return attrs; }; const getParseDOMAttrs = (allowCustomPanel, dom, generateLocalId) => { let parseDOMAttrs = { // eslint-disable-next-line @atlaskit/editor/no-as-casting, @typescript-eslint/no-non-null-assertion panelType: dom.getAttribute('data-panel-type') }; if (generateLocalId) { parseDOMAttrs.localId = uuid.generate(); } if (allowCustomPanel) { parseDOMAttrs = { ...parseDOMAttrs, // eslint-disable-next-line @atlaskit/editor/no-as-casting, @typescript-eslint/no-non-null-assertion panelIcon: dom.getAttribute('data-panel-icon'), // eslint-disable-next-line @atlaskit/editor/no-as-casting, @typescript-eslint/no-non-null-assertion panelIconId: dom.getAttribute('data-panel-icon-id'), // eslint-disable-next-line @atlaskit/editor/no-as-casting, @typescript-eslint/no-non-null-assertion panelIconText: dom.getAttribute('data-panel-icon-text'), // eslint-disable-next-line @atlaskit/editor/no-as-casting, @typescript-eslint/no-non-null-assertion panelColor: dom.getAttribute('data-panel-color') }; } else { parseDOMAttrs.panelType = parseDOMAttrs.panelType === PanelType.CUSTOM ? PanelType.INFO : parseDOMAttrs.panelType; } return parseDOMAttrs; }; const createPanelNodeSpecOptions = (allowCustomPanel, generateLocalId) => ({ parseDOM: [{ tag: 'div[data-panel-type]', getAttrs: dom => getParseDOMAttrs(allowCustomPanel, dom, generateLocalId) }], toDOM(node) { const attrs = getDomAttrs(node.attrs); const contentAttrs = { 'data-panel-content': 'true' }; return ['div', attrs, ['div', contentAttrs, 0]]; } }); /** * @name extended_panel * @description it allows more content to be nested as compared to panel node. * Specifically, it allows Media, action, code-block, rule and decision nodes in * addition to content allowed inside panel */ export const extendedPanel = allowCustomPanel => panelFactory(createPanelNodeSpecOptions(allowCustomPanel)); export const extendedPanelWithLocalId = allowCustomPanel => panelFactory(createPanelNodeSpecOptions(allowCustomPanel, true));