@atlaskit/adf-schema
Version:
Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs
311 lines (300 loc) • 12.3 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toJSONTableHeader = exports.toJSONTableCell = exports.tableToJSON = exports.tableRow = exports.tablePrefixSelector = exports.tableHeaderSelector = exports.tableHeader = exports.tableCellSelector = exports.tableCellContentWrapperSelector = exports.tableCellContentDomSelector = exports.tableCell = exports.tableBackgroundColorPalette = exports.tableBackgroundColorNames = exports.tableBackgroundBorderColor = exports.table = exports.getCellDomAttrs = exports.getCellAttrs = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _editorPalette = require("@atlaskit/editor-palette");
var _tokens = require("@atlaskit/tokens");
var _colors = require("../../utils/colors");
var _uuid = require("../../utils/uuid");
var tablePrefixSelector = 'pm-table';
exports.tablePrefixSelector = tablePrefixSelector;
var tableCellSelector = "".concat(tablePrefixSelector, "-cell-content-wrap");
exports.tableCellSelector = tableCellSelector;
var tableHeaderSelector = "".concat(tablePrefixSelector, "-header-content-wrap");
exports.tableHeaderSelector = tableHeaderSelector;
var tableCellContentWrapperSelector = "".concat(tablePrefixSelector, "-cell-nodeview-wrapper");
exports.tableCellContentWrapperSelector = tableCellContentWrapperSelector;
var tableCellContentDomSelector = "".concat(tablePrefixSelector, "-cell-nodeview-content-dom");
exports.tableCellContentDomSelector = tableCellContentDomSelector;
var DEFAULT_TABLE_HEADER_CELL_BACKGROUND = _colors.N20.toLocaleLowerCase();
var getCellAttrs = function getCellAttrs(dom) {
var defaultValues = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var widthAttr = dom.getAttribute('data-colwidth');
var width = widthAttr && /^\d+(,\d+)*$/.test(widthAttr) ? widthAttr.split(',').map(function (str) {
return Number(str);
}) : null;
var colspan = Number(dom.getAttribute('colspan') || 1);
var backgroundColor = dom.style.backgroundColor;
/**
* We have pivoted to store background color information in
* data-cell-background.
* We will have original hex code (which we map to DST token)
* stored in data-cell-background, use that.
* More details at https://product-fabric.atlassian.net/wiki/spaces/EUXQ/pages/3472556903/Tokenising+tableCell+background+colors#Update-toDom-and-parseDom-to-store-and-read-background-color-from-data-cell-background-attribute.4
*/
var dataCellBackground = dom.getAttribute('data-cell-background');
var dataCellBackgroundHexCode = dataCellBackground && (0, _colors.isHex)(dataCellBackground) ? dataCellBackground : undefined;
// ignore setting background attr if ds neutral token is detected
if (backgroundColor.includes('--ds-background-neutral')) {
backgroundColor = '';
} else {
if (backgroundColor && (0, _colors.isRgb)(backgroundColor)) {
var result = (0, _colors.rgbToHex)(backgroundColor);
if (result !== null) {
backgroundColor = result;
}
}
}
var backgroundHexCode = dataCellBackgroundHexCode || (backgroundColor && backgroundColor !== defaultValues['background'] ? backgroundColor : null);
return {
colspan: colspan,
rowspan: Number(dom.getAttribute('rowspan') || 1),
colwidth: width && width.length === colspan ? width : null,
background: backgroundHexCode
};
};
exports.getCellAttrs = getCellAttrs;
/**
* gets cell dom attributes based on node attributes
* @returns CellDomAttrs
*/
var getCellDomAttrs = function getCellDomAttrs(node) {
var attrs = {};
var nodeType = node.type.name;
if (node.attrs.colspan !== 1) {
attrs.colspan = node.attrs.colspan;
}
if (node.attrs.rowspan !== 1) {
attrs.rowspan = node.attrs.rowspan;
}
if (node.attrs.colwidth) {
attrs['data-colwidth'] = node.attrs.colwidth.join(',');
}
if (node.attrs.background) {
var background = node.attrs.background;
// to ensure that we don't overwrite product's style:
// - it clears background color for <th> if its set to gray
// - it clears background color for <td> if its set to white
// - it clears background color for <th> if ds neutral token is detected
var ignored = nodeType === 'tableHeader' && background === tableBackgroundColorNames.get('light gray') || nodeType === 'tableCell' && background === tableBackgroundColorNames.get('white') || nodeType === 'tableHeader' && background.includes('--ds-background-neutral');
if (ignored) {
attrs.style = '';
} else {
var color = (0, _colors.isRgb)(background) ? (0, _colors.rgbToHex)(background) : background;
var tokenName = (0, _editorPalette.hexToEditorBackgroundPaletteColorTokenName)(color);
// eslint-disable-next-line @atlaskit/design-system/no-unsafe-design-token-usage
var tokenColor = tokenName ? (0, _tokens.getTokenValue)(tokenName) : color;
attrs.style = "".concat(attrs.style || '', "background-color: ").concat(tokenColor, ";");
/**
* Storing hex code in data-cell-background because
* we want to have DST token (css variable) or
* DST token value (value (hex code) of css variable) in
* inline style to correct render table cell background
* based on selected theme.
* Currently we rely on background color hex code stored in
* inline style.
* Because of that when we copy and paste table, we end up
* having DST token or DST token value in ADF instead of
* original hex code which we map to DST token.
* So, introducing data-cell-background.
* More details at https://product-fabric.atlassian.net/wiki/spaces/EUXQ/pages/3472556903/Tokenising+tableCell+background+colors#Update-toDom-and-parseDom-to-store-and-read-background-color-from-data-cell-background-attribute.4
*/
if (color) {
attrs['data-cell-background'] = color;
}
attrs.colorname = tableBackgroundColorPalette.get(color);
}
}
if (nodeType === 'tableHeader') {
attrs.class = tableHeaderSelector;
} else {
attrs.class = tableCellSelector;
}
return attrs;
};
exports.getCellDomAttrs = getCellDomAttrs;
var tableBackgroundColorPalette = new Map();
exports.tableBackgroundColorPalette = tableBackgroundColorPalette;
var tableBackgroundBorderColor = (0, _colors.hexToRgba)(_colors.N800, 0.12) || _colors.N0;
exports.tableBackgroundBorderColor = tableBackgroundBorderColor;
var tableBackgroundColorNames = new Map();
exports.tableBackgroundColorNames = tableBackgroundColorNames;
[[_colors.N0, 'White'], [_colors.B50, 'Light blue'], [_colors.T50, 'Light teal'], [_colors.G50, 'Light green'], [_colors.Y50, 'Light yellow'], [_colors.R50, 'Light red'], [_colors.P50, 'Light purple'], [_colors.N20, 'Light gray'], [_colors.B75, 'Blue'], [_colors.T75, 'Teal'], [_colors.G75, 'Green'], [_colors.Y75, 'Yellow'], [_colors.R75, 'Red'], [_colors.P75, 'Purple'], [_colors.N60, 'Gray'], [_colors.B100, 'Dark blue'], [_colors.T100, 'Dark teal'], [_colors.G200, 'Dark green'], [_colors.Y200, 'Dark yellow'], [_colors.R100, 'Dark red'], [_colors.P100, 'Dark purple']].forEach(function (_ref) {
var _ref2 = (0, _slicedToArray2.default)(_ref, 2),
colorValue = _ref2[0],
colorName = _ref2[1];
tableBackgroundColorPalette.set(colorValue.toLowerCase(), colorName);
tableBackgroundColorNames.set(colorName.toLowerCase(), colorValue.toLowerCase());
});
/**
* @name table_node
*/
/**
* @name table_row_node
*/
/**
* @name table_cell_content
* @minItems 1
* @allowUnsupportedBlock true
*/
/**
* @name table_cell_node
*/
/**
* @name table_header_node
*/
// TODO: Fix any, potential issue. ED-5048
var createTableSpec = function createTableSpec() {
var attrs = {
isNumberColumnEnabled: {
default: false
},
layout: {
default: 'default'
},
__autoSize: {
default: false
},
localId: {
default: ''
}
};
var tableNodeSpec = {
content: 'tableRow+',
attrs: attrs,
marks: 'unsupportedMark unsupportedNodeAttribute',
tableRole: 'table',
isolating: true,
selectable: true,
group: 'block',
parseDOM: [{
tag: 'table',
getAttrs: function getAttrs(node) {
var _dom$parentElement;
var dom = node;
var breakoutWrapper = (_dom$parentElement = dom.parentElement) === null || _dom$parentElement === void 0 ? void 0 : _dom$parentElement.parentElement;
return {
isNumberColumnEnabled: dom.getAttribute('data-number-column') === 'true' ? true : false,
layout:
// copying from editor
dom.getAttribute('data-layout') || ( // copying from renderer
breakoutWrapper === null || breakoutWrapper === void 0 ? void 0 : breakoutWrapper.getAttribute('data-layout')) || 'default',
__autoSize: dom.getAttribute('data-autosize') === 'true' ? true : false,
localId: dom.getAttribute('data-table-local-id') || _uuid.uuid.generate()
};
}
}],
toDOM: function toDOM(node) {
var attrs = {
'data-number-column': node.attrs.isNumberColumnEnabled,
'data-layout': node.attrs.layout,
'data-autosize': node.attrs.__autoSize,
'data-table-local-id': node.attrs.localId
};
return ['table', attrs, ['tbody', 0]];
}
};
return tableNodeSpec;
};
var table = createTableSpec();
exports.table = table;
var shouldIncludeAttribute = function shouldIncludeAttribute(key, value) {
return !key.startsWith('__') && (key !== 'localId' || !!value);
};
var tableToJSON = function tableToJSON(node) {
return {
attrs: Object.keys(node.attrs).filter(function (key) {
return shouldIncludeAttribute(key, node.attrs[key]);
}).reduce(function (obj, key) {
obj[key] = node.attrs[key];
return obj;
}, {})
};
};
exports.tableToJSON = tableToJSON;
var tableRow = {
selectable: false,
content: '(tableCell | tableHeader)+',
marks: 'unsupportedMark unsupportedNodeAttribute',
tableRole: 'row',
parseDOM: [{
tag: 'tr'
}],
toDOM: function toDOM() {
return ['tr', 0];
}
};
exports.tableRow = tableRow;
var cellAttrs = {
colspan: {
default: 1
},
rowspan: {
default: 1
},
colwidth: {
default: null
},
background: {
default: null
}
};
var tableCell = {
selectable: false,
content: '(paragraph | panel | blockquote | orderedList | bulletList | rule | heading | codeBlock | mediaSingle | mediaGroup | decisionList | taskList | blockCard | embedCard | extension | nestedExpand | unsupportedBlock)+',
attrs: cellAttrs,
tableRole: 'cell',
marks: 'alignment dataConsumer fragment unsupportedMark unsupportedNodeAttribute',
isolating: true,
parseDOM: [
// Ignore number cell copied from renderer
{
tag: '.ak-renderer-table-number-column',
ignore: true
}, {
tag: 'td',
getAttrs: function getAttrs(dom) {
return getCellAttrs(dom);
}
}],
toDOM: function toDOM(node) {
return ['td', getCellDomAttrs(node), 0];
}
};
exports.tableCell = tableCell;
var toJSONTableCell = function toJSONTableCell(node) {
return {
attrs: Object.keys(node.attrs).reduce(function (obj, key) {
if (cellAttrs[key].default !== node.attrs[key]) {
obj[key] = node.attrs[key];
}
return obj;
}, {})
};
};
exports.toJSONTableCell = toJSONTableCell;
var tableHeader = {
selectable: false,
content: '(paragraph | panel | blockquote | orderedList | bulletList | rule | heading | codeBlock | mediaSingle | mediaGroup | decisionList | taskList | blockCard | embedCard | extension | nestedExpand)+',
attrs: cellAttrs,
tableRole: 'header_cell',
isolating: true,
marks: 'alignment dataConsumer fragment unsupportedMark unsupportedNodeAttribute',
parseDOM: [{
tag: 'th',
getAttrs: function getAttrs(dom) {
return getCellAttrs(dom, {
background: DEFAULT_TABLE_HEADER_CELL_BACKGROUND
});
}
}],
toDOM: function toDOM(node) {
return ['th', getCellDomAttrs(node), 0];
}
};
exports.tableHeader = tableHeader;
var toJSONTableHeader = toJSONTableCell;
exports.toJSONTableHeader = toJSONTableHeader;
;