UNPKG

2.46 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5import { downcastAttributeToStyle, upcastStyleToAttribute } from './../converters/tableproperties';
6/**
7 * A common method to update the numeric value. If a value is the default one, it will be unset.
8 *
9 * @param key An attribute key.
10 * @param value The new attribute value.
11 * @param item A model item on which the attribute will be set.
12 * @param defaultValue The default attribute value. If a value is lower or equal, it will be unset.
13 */
14export function updateNumericAttribute(key, value, item, writer, defaultValue = 1) {
15 if (value !== undefined && value !== null && defaultValue !== undefined && defaultValue !== null && value > defaultValue) {
16 writer.setAttribute(key, value, item);
17 }
18 else {
19 writer.removeAttribute(key, item);
20 }
21}
22/**
23 * A common method to create an empty table cell. It creates a proper model structure as a table cell must have at least one block inside.
24 *
25 * @param writer The model writer.
26 * @param insertPosition The position at which the table cell should be inserted.
27 * @param attributes The element attributes.
28 * @returns Created table cell.
29 */
30export function createEmptyTableCell(writer, insertPosition, attributes = {}) {
31 const tableCell = writer.createElement('tableCell', attributes);
32 writer.insertElement('paragraph', tableCell);
33 writer.insert(tableCell, insertPosition);
34 return tableCell;
35}
36/**
37 * Checks if a table cell belongs to the heading column section.
38 */
39export function isHeadingColumnCell(tableUtils, tableCell) {
40 const table = tableCell.parent.parent;
41 const headingColumns = parseInt(table.getAttribute('headingColumns') || '0');
42 const { column } = tableUtils.getCellLocation(tableCell);
43 return !!headingColumns && column < headingColumns;
44}
45/**
46 * Enables conversion for an attribute for simple view-model mappings.
47 *
48 * @param options.defaultValue The default value for the specified `modelAttribute`.
49 */
50export function enableProperty(schema, conversion, options) {
51 const { modelAttribute } = options;
52 schema.extend('tableCell', {
53 allowAttributes: [modelAttribute]
54 });
55 upcastStyleToAttribute(conversion, { viewElement: /^(td|th)$/, ...options });
56 downcastAttributeToStyle(conversion, { modelElement: 'tableCell', ...options });
57}