Przeglądaj źródła

Move common utility methods to utils/common.js.

Maciej Gołaszewski 5 lat temu
rodzic
commit
c210d2583b
26 zmienionych plików z 95 dodań i 101 usunięć
  1. 1 1
      packages/ckeditor5-table/src/commands/insertcolumncommand.js
  2. 1 1
      packages/ckeditor5-table/src/commands/insertrowcommand.js
  3. 1 1
      packages/ckeditor5-table/src/commands/mergecellcommand.js
  4. 1 1
      packages/ckeditor5-table/src/commands/mergecellscommand.js
  5. 1 1
      packages/ckeditor5-table/src/commands/removecolumncommand.js
  6. 1 1
      packages/ckeditor5-table/src/commands/removerowcommand.js
  7. 1 1
      packages/ckeditor5-table/src/commands/selectcolumncommand.js
  8. 1 1
      packages/ckeditor5-table/src/commands/selectrowcommand.js
  9. 7 2
      packages/ckeditor5-table/src/commands/setheadercolumncommand.js
  10. 1 2
      packages/ckeditor5-table/src/commands/setheaderrowcommand.js
  11. 0 73
      packages/ckeditor5-table/src/commands/utils.js
  12. 1 1
      packages/ckeditor5-table/src/converters/table-layout-post-fixer.js
  13. 1 1
      packages/ckeditor5-table/src/converters/upcasttable.js
  14. 1 2
      packages/ckeditor5-table/src/tableclipboard.js
  15. 1 1
      packages/ckeditor5-table/src/tablenavigation.js
  16. 1 1
      packages/ckeditor5-table/src/tableproperties/commands/tablepropertycommand.js
  17. 1 1
      packages/ckeditor5-table/src/tableselection.js
  18. 1 1
      packages/ckeditor5-table/src/tableselection/croptable.js
  19. 1 1
      packages/ckeditor5-table/src/tableutils.js
  20. 1 1
      packages/ckeditor5-table/src/ui/utils.js
  21. 65 1
      packages/ckeditor5-table/src/utils/common.js
  22. 1 1
      packages/ckeditor5-table/src/utils/selection.js
  23. 1 1
      packages/ckeditor5-table/src/utils/ui/widget.js
  24. 1 1
      packages/ckeditor5-table/tests/commands/utils.js
  25. 1 1
      packages/ckeditor5-table/tests/manual/tablemocking.js
  26. 1 1
      packages/ckeditor5-table/tests/ui/utils.js

+ 1 - 1
packages/ckeditor5-table/src/commands/insertcolumncommand.js

@@ -8,8 +8,8 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import { findAncestor } from './utils';
 import { getColumnIndexes, getSelectionAffectedTableCells } from '../utils/selection';
+import { findAncestor } from '../utils/common';
 
 /**
  * The insert column command.

+ 1 - 1
packages/ckeditor5-table/src/commands/insertrowcommand.js

@@ -8,8 +8,8 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import { findAncestor } from './utils';
 import { getRowIndexes, getSelectionAffectedTableCells } from '../utils/selection';
+import { findAncestor } from '../utils/common';
 
 /**
  * The insert row command.

+ 1 - 1
packages/ckeditor5-table/src/commands/mergecellcommand.js

@@ -9,8 +9,8 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import TableWalker from '../tablewalker';
-import { isHeadingColumnCell, findAncestor } from './utils';
 import { getTableCellsContainingSelection } from '../utils/selection';
+import { findAncestor, isHeadingColumnCell } from '../utils/common';
 
 /**
  * The merge cell command.

+ 1 - 1
packages/ckeditor5-table/src/commands/mergecellscommand.js

@@ -9,8 +9,8 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import TableUtils from '../tableutils';
-import { findAncestor, updateNumericAttribute } from './utils';
 import { getSelectedTableCells, isSelectionRectangular } from '../utils/selection';
+import { findAncestor, updateNumericAttribute } from '../utils/common';
 
 /**
  * The merge cells command.

+ 1 - 1
packages/ckeditor5-table/src/commands/removecolumncommand.js

@@ -10,8 +10,8 @@
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
 import TableWalker from '../tablewalker';
-import { findAncestor } from './utils';
 import { getColumnIndexes, getSelectionAffectedTableCells } from '../utils/selection';
+import { findAncestor } from '../utils/common';
 
 /**
  * The remove column command.

+ 1 - 1
packages/ckeditor5-table/src/commands/removerowcommand.js

@@ -9,8 +9,8 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
-import { findAncestor } from './utils';
 import { getRowIndexes, getSelectionAffectedTableCells } from '../utils/selection';
+import { findAncestor } from '../utils/common';
 
 /**
  * The remove row command.

+ 1 - 1
packages/ckeditor5-table/src/commands/selectcolumncommand.js

@@ -10,8 +10,8 @@
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
 import TableWalker from '../tablewalker';
-import { findAncestor } from './utils';
 import { getSelectionAffectedTableCells } from '../utils/selection';
+import { findAncestor } from '../utils/common';
 
 /**
  * The select column command.

+ 1 - 1
packages/ckeditor5-table/src/commands/selectrowcommand.js

@@ -9,8 +9,8 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
-import { findAncestor } from './utils';
 import { getRowIndexes, getSelectionAffectedTableCells } from '../utils/selection';
+import { findAncestor } from '../utils/common';
 
 /**
  * The select row command.

+ 7 - 2
packages/ckeditor5-table/src/commands/setheadercolumncommand.js

@@ -9,8 +9,13 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
-import { findAncestor, isHeadingColumnCell, updateNumericAttribute } from './utils';
-import { getHorizontallyOverlappingCells, splitVertically } from '../utils/common';
+import {
+	findAncestor,
+	getHorizontallyOverlappingCells,
+	isHeadingColumnCell,
+	splitVertically,
+	updateNumericAttribute
+} from '../utils/common';
 import { getColumnIndexes, getSelectionAffectedTableCells } from '../utils/selection';
 
 /**

+ 1 - 2
packages/ckeditor5-table/src/commands/setheaderrowcommand.js

@@ -9,8 +9,7 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
-import { findAncestor, updateNumericAttribute } from './utils';
-import { getVerticallyOverlappingCells, splitHorizontally } from '../utils/common';
+import { findAncestor, getVerticallyOverlappingCells, splitHorizontally, updateNumericAttribute } from '../utils/common';
 import { getRowIndexes, getSelectionAffectedTableCells } from '../utils/selection';
 
 /**

+ 0 - 73
packages/ckeditor5-table/src/commands/utils.js

@@ -1,73 +0,0 @@
-/**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
-
-/**
- * @module table/commands/utils
- */
-
-/**
- * Returns the parent element of the given name. Returns undefined if the position or the element is not inside the desired parent.
- *
- * @param {String} parentName The name of the parent element to find.
- * @param {module:engine/model/position~Position|module:engine/model/position~Position} positionOrElement The position or
- * the parentElement to start searching.
- * @returns {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment}
- */
-export function findAncestor( parentName, positionOrElement ) {
-	let parent = positionOrElement.parent;
-
-	while ( parent ) {
-		if ( parent.name === parentName ) {
-			return parent;
-		}
-
-		parent = parent.parent;
-	}
-}
-
-/**
- * A common method to update the numeric value. If a value is the default one, it will be unset.
- *
- * @param {String} key An attribute key.
- * @param {*} value The new attribute value.
- * @param {module:engine/model/item~Item} item A model item on which the attribute will be set.
- * @param {module:engine/model/writer~Writer} writer
- * @param {*} defaultValue The default attribute value. If a value is lower or equal, it will be unset.
- */
-export function updateNumericAttribute( key, value, item, writer, defaultValue = 1 ) {
-	if ( value > defaultValue ) {
-		writer.setAttribute( key, value, item );
-	} else {
-		writer.removeAttribute( key, item );
-	}
-}
-
-/**
- * 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.
- *
- * @param {module:engine/model/writer~Writer} writer The model writer.
- * @param {module:engine/model/position~Position} insertPosition The position at which the table cell should be inserted.
- * @param {Object} attributes The element attributes.
- */
-export function createEmptyTableCell( writer, insertPosition, attributes = {} ) {
-	const tableCell = writer.createElement( 'tableCell', attributes );
-	writer.insertElement( 'paragraph', tableCell );
-	writer.insert( tableCell, insertPosition );
-}
-
-/**
- * Checks if a table cell belongs to the heading column section.
- *
- * @param {module:table/tableutils~TableUtils} tableUtils
- * @param {module:engine/model/element~Element} tableCell
- * @returns {Boolean}
- */
-export function isHeadingColumnCell( tableUtils, tableCell ) {
-	const table = tableCell.parent.parent;
-	const headingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 );
-	const { column } = tableUtils.getCellLocation( tableCell );
-
-	return !!headingColumns && column < headingColumns;
-}

+ 1 - 1
packages/ckeditor5-table/src/converters/table-layout-post-fixer.js

@@ -7,8 +7,8 @@
  * @module table/converters/table-layout-post-fixer
  */
 
-import { createEmptyTableCell, findAncestor, updateNumericAttribute } from './../commands/utils';
 import TableWalker from './../tablewalker';
+import { createEmptyTableCell, findAncestor, updateNumericAttribute } from '../utils/common';
 
 /**
  * Injects a table layout post-fixer into the model.

+ 1 - 1
packages/ckeditor5-table/src/converters/upcasttable.js

@@ -7,7 +7,7 @@
  * @module table/converters/upcasttable
  */
 
-import { createEmptyTableCell } from '../commands/utils';
+import { createEmptyTableCell } from '../utils/common';
 
 /**
  * View table element to model table element conversion helper.

+ 1 - 2
packages/ckeditor5-table/src/tableclipboard.js

@@ -15,9 +15,8 @@ import {
 	getVerticallyOverlappingCells,
 	getHorizontallyOverlappingCells,
 	splitHorizontally,
-	splitVertically
+	splitVertically, findAncestor
 } from './utils/common';
-import { findAncestor } from './commands/utils';
 import { cropTableToDimensions, trimTableCellIfNeeded } from './tableselection/croptable';
 import TableUtils from './tableutils';
 import { getColumnIndexes, getRowIndexes, getSelectionAffectedTableCells, isSelectionRectangular } from './utils/selection';

+ 1 - 1
packages/ckeditor5-table/src/tablenavigation.js

@@ -9,13 +9,13 @@
 
 import TableSelection from './tableselection';
 import TableWalker from './tablewalker';
-import { findAncestor } from './commands/utils';
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
 import priorities from '@ckeditor/ckeditor5-utils/src/priorities';
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import { getSelectedTableCells, getTableCellsContainingSelection } from './utils/selection';
+import { findAncestor } from './utils/common';
 
 /**
  * This plugin enables keyboard navigation for tables.

+ 1 - 1
packages/ckeditor5-table/src/tableproperties/commands/tablepropertycommand.js

@@ -9,7 +9,7 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
-import { findAncestor } from '../../commands/utils';
+import { findAncestor } from '../../utils/common';
 
 /**
  * The table cell attribute command.

+ 1 - 1
packages/ckeditor5-table/src/tableselection.js

@@ -13,11 +13,11 @@ import first from '@ckeditor/ckeditor5-utils/src/first';
 import TableWalker from './tablewalker';
 import TableUtils from './tableutils';
 import MouseEventsObserver from './tableselection/mouseeventsobserver';
-import { findAncestor } from './commands/utils';
 import { cropTableToDimensions } from './tableselection/croptable';
 
 import '../theme/tableselection.css';
 import { getColumnIndexes, getRowIndexes, getSelectedTableCells, getTableCellsContainingSelection } from './utils/selection';
+import { findAncestor } from './utils/common';
 
 /**
  * This plugin enables the advanced table cells, rows and columns selection.

+ 1 - 1
packages/ckeditor5-table/src/tableselection/croptable.js

@@ -7,8 +7,8 @@
  * @module table/tableselection/croptable
  */
 
-import { createEmptyTableCell, updateNumericAttribute } from '../commands/utils';
 import TableWalker from '../tablewalker';
+import { createEmptyTableCell, updateNumericAttribute } from '../utils/common';
 
 /**
  * Returns a cropped table according to given dimensions.

+ 1 - 1
packages/ckeditor5-table/src/tableutils.js

@@ -10,7 +10,7 @@
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 
 import TableWalker from './tablewalker';
-import { createEmptyTableCell, updateNumericAttribute } from './commands/utils';
+import { createEmptyTableCell, updateNumericAttribute } from './utils/common';
 
 /**
  * The table utilities plugin.

+ 1 - 1
packages/ckeditor5-table/src/ui/utils.js

@@ -13,10 +13,10 @@ import Collection from '@ckeditor/ckeditor5-utils/src/collection';
 import Model from '@ckeditor/ckeditor5-ui/src/model';
 import ColorInputView from './colorinputview';
 import { isColor, isLength, isPercentage } from '@ckeditor/ckeditor5-engine/src/view/styles/utils';
-import { findAncestor } from '../commands/utils';
 import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
 import { centeredBalloonPositionForLongWidgets } from '@ckeditor/ckeditor5-widget/src/utils';
 import { getTableWidgetAncestor } from '../utils/ui/widget';
+import { findAncestor } from '../utils/common';
 
 const DEFAULT_BALLOON_POSITIONS = BalloonPanelView.defaultPositions;
 const BALLOON_POSITIONS = [

+ 65 - 1
packages/ckeditor5-table/src/utils/common.js

@@ -7,7 +7,6 @@
  * @module table/utils/common
  */
 
-import { createEmptyTableCell, updateNumericAttribute } from '../commands/utils';
 import TableWalker from '../tablewalker';
 
 /**
@@ -51,6 +50,71 @@ export function getVerticallyOverlappingCells( table, overlapRow, startRow = 0 )
 	return cells;
 }
 
+/**
+ * Returns the parent element of the given name. Returns undefined if the position or the element is not inside the desired parent.
+ *
+ * @param {String} parentName The name of the parent element to find.
+ * @param {module:engine/model/position~Position|module:engine/model/position~Position} positionOrElement The position or
+ * the parentElement to start searching.
+ * @returns {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment}
+ */
+export function findAncestor( parentName, positionOrElement ) {
+	let parent = positionOrElement.parent;
+
+	while ( parent ) {
+		if ( parent.name === parentName ) {
+			return parent;
+		}
+
+		parent = parent.parent;
+	}
+}
+
+/**
+ * A common method to update the numeric value. If a value is the default one, it will be unset.
+ *
+ * @param {String} key An attribute key.
+ * @param {*} value The new attribute value.
+ * @param {module:engine/model/item~Item} item A model item on which the attribute will be set.
+ * @param {module:engine/model/writer~Writer} writer
+ * @param {*} defaultValue The default attribute value. If a value is lower or equal, it will be unset.
+ */
+export function updateNumericAttribute( key, value, item, writer, defaultValue = 1 ) {
+	if ( value > defaultValue ) {
+		writer.setAttribute( key, value, item );
+	} else {
+		writer.removeAttribute( key, item );
+	}
+}
+
+/**
+ * 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.
+ *
+ * @param {module:engine/model/writer~Writer} writer The model writer.
+ * @param {module:engine/model/position~Position} insertPosition The position at which the table cell should be inserted.
+ * @param {Object} attributes The element attributes.
+ */
+export function createEmptyTableCell( writer, insertPosition, attributes = {} ) {
+	const tableCell = writer.createElement( 'tableCell', attributes );
+	writer.insertElement( 'paragraph', tableCell );
+	writer.insert( tableCell, insertPosition );
+}
+
+/**
+ * Checks if a table cell belongs to the heading column section.
+ *
+ * @param {module:table/tableutils~TableUtils} tableUtils
+ * @param {module:engine/model/element~Element} tableCell
+ * @returns {Boolean}
+ */
+export function isHeadingColumnCell( tableUtils, tableCell ) {
+	const table = tableCell.parent.parent;
+	const headingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 );
+	const { column } = tableUtils.getCellLocation( tableCell );
+
+	return !!headingColumns && column < headingColumns;
+}
+
 /**
  * Splits the table cell horizontally.
  *

+ 1 - 1
packages/ckeditor5-table/src/utils/selection.js

@@ -7,8 +7,8 @@
  * @module table/utils/selection
  */
 
-import { findAncestor } from '../commands/utils';
 import TableWalker from '../tablewalker';
+import { findAncestor } from './common';
 
 /**
  * Returns all model table cells that are fully selected (from the outside)

+ 1 - 1
packages/ckeditor5-table/src/utils/ui/widget.js

@@ -7,8 +7,8 @@
  * @module table/utils/ui/widget
  */
 
-import { findAncestor } from '../../commands/utils';
 import { isWidget } from '@ckeditor/ckeditor5-widget/src/utils';
+import { findAncestor } from '../common';
 
 /**
  * Returns a table widget editing view element if one is selected.

+ 1 - 1
packages/ckeditor5-table/tests/commands/utils.js

@@ -7,7 +7,7 @@ import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltestedit
 import TableUtils from '../../src/tableutils';
 import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
-import { findAncestor, isHeadingColumnCell } from '../../src/commands/utils';
+import { findAncestor, isHeadingColumnCell } from '../../src/utils/common';
 
 describe( 'commands utils', () => {
 	let editor, model, modelRoot, tableUtils;

+ 1 - 1
packages/ckeditor5-table/tests/manual/tablemocking.js

@@ -14,8 +14,8 @@ import { diffString } from 'json-diff';
 import { debounce } from 'lodash-es';
 import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
 import TableWalker from '../../src/tablewalker';
-import { findAncestor } from '../../src/commands/utils';
 import { getSelectionAffectedTableCells } from '../../src/utils/selection';
+import { findAncestor } from '../../src/utils/common';
 
 ClassicEditor
 	.create( document.querySelector( '#editor' ), {

+ 1 - 1
packages/ckeditor5-table/tests/ui/utils.js

@@ -6,7 +6,6 @@
 import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
 import Table from '../../src/table';
 import TableCellProperties from '../../src/tablecellproperties';
-import { findAncestor } from '../../src/commands/utils';
 import global from '@ckeditor/ckeditor5-utils/src/dom/global';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import View from '@ckeditor/ckeditor5-ui/src/view';
@@ -31,6 +30,7 @@ import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import { centeredBalloonPositionForLongWidgets } from '@ckeditor/ckeditor5-widget/src/utils';
 import { modelTable } from '../_utils/utils';
 import { getTableCellsContainingSelection } from '../../src/utils/selection';
+import { findAncestor } from '../../src/utils/common';
 
 describe( 'UI Utils', () => {
 	let editor, editingView, balloon, editorElement;